Creating new versions that can handle multi-valued parameters for Edit Configuration and n3 editing

This commit is contained in:
hjkhjk54 2011-07-01 21:43:59 +00:00
parent fd7c20047f
commit 263cca2727
26 changed files with 2660 additions and 366 deletions

View file

@ -0,0 +1,55 @@
package edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
public class EditN3GeneratorVTwoTest {
@Test
public void testSubInMultiUris() {
String n3 = "?subject ?predicate ?multivalue ." ;
List<String> strs = new ArrayList<String>();
strs.add(n3);
Map<String,List<String>> keyToValues = new HashMap<String,List<String>>();
List<String> values = new ArrayList<String>();
values.add("http://a.com/2");
values.add("http://b.com/ont#2");
values.add("http://c.com/individual/n23431");
keyToValues.put("multivalue", values);
List<String> subject = new ArrayList<String>();
List<String> predicate = new ArrayList<String>();
subject.add("http://testsubject.com/1");
predicate.add("http://testpredicate.com/2");
keyToValues.put("subject", subject);
keyToValues.put("predicate", predicate);
List<String> n3results = EditN3GeneratorVTwo.subInMultiUris(keyToValues, strs);
Assert.assertNotNull(n3results);
Assert.assertTrue( n3results.size() == 1 );
String expected ="<http://testsubject.com/1> <http://testpredicate.com/2> <http://a.com/2>, <http://b.com/ont#2>, <http://c.com/individual/n23431> .";
Assert.assertEquals(expected, n3results.get(0));
//Replace subject and predicate with other variables
//make a model,
Model expectedModel = ModelFactory.createDefaultModel();
StringReader expectedReader = new StringReader(expected);
StringReader resultReader = new StringReader(n3results.get(0));
expectedModel.read(expectedReader, null, "N3");
Model resultModel = ModelFactory.createDefaultModel();
resultModel.read(resultReader, null, "N3");
Assert.assertTrue(expectedModel.isIsomorphicWith(resultModel));
}
}

View file

@ -8,34 +8,82 @@ import java.util.List;
import java.util.Map;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import edu.cornell.mannlib.vitro.webapp.edit.EditLiteral;
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.EditConfiguration;
import java.io.StringReader;
public class EditN3GeneratorTest {
EditN3Generator en3g ;
@Before
public void setUp() throws Exception {
en3g = new EditN3Generator((EditConfiguration) null);
}
@Test
public void testSubInMultiUris() {
// String n3 = "?subject ?predicate ?multivalue." ;
// List<String> strs = new ArrayList<String>();
// strs.add(n3);
//
// Map<String,List<String>> keyToValues = new HashMap<String,List<String>>();
// List<String> values = new ArrayList<String>();
// values.add("http://a.com/2");
// values.add("http://b.com/ont#2");
// values.add("http://c.com/individual/n23431");
// keyToValues.put("multivalue", values);
//
// List<String> n3results = EditN3Generator.subInMultiLiterals(keyToValues, strs);
//
// Assert.assertNotNull(n3results);
// Assert.assertTrue( n3results.size() == 1 );
// String expected ="?subject ?predicate <http://a.com/2>, <http://b.com/ont#2>, <http://c.com/individual/n23431>.";
// Assert.assertEquals(expected, n3results);
//
//make a model,
//add statements
//prase resultn3
//compare
public void testSubInLiterals() {
String var = "TestVar";
String target = "Fake n3 ?TestVar .";
Literal literal = null;
EditN3Generator en3g = new EditN3Generator((EditConfiguration) null);
String result = en3g.subInLiterals(var, literal, target);
Assert.assertNotNull( result );
}
@Test
public void testSubInLiteralsWithGroupReference() {
String var = "TestVar";
String target = "Fake n3 ?TestVar .";
Literal literal = new EditLiteral("should not a regex group --> ?2 <-- blblkj (lskdfj) " ,null,null);
EditN3Generator en3g = new EditN3Generator((EditConfiguration) null);
String result = en3g.subInLiterals(var, literal, target);
Assert.assertNotNull( result );
Assert.assertEquals("Fake n3 \"should not a regex group --> ?2 <-- blblkj (lskdfj) \" ." , result);
}
@Test
public void testConflictingVarNames(){
Map<String,String> varToExisting= new HashMap<String,String>();
varToExisting.put("bob", "http://uri.edu#BobTheElder");
varToExisting.put("bobJr", "http://uri.edu#BobTheSon");
String target = "SELECT ?cat WHERE{ ?bobJr <http://uri.edu#hasCat> ?cat }" ;
List<String> targets = new ArrayList<String>();
targets.add(target);
List<String> out = en3g.subInUris(varToExisting, targets);
Assert.assertNotNull(out);
Assert.assertNotNull( out.get(0) );
String expected = "SELECT ?cat WHERE{ <http://uri.edu#BobTheSon> <http://uri.edu#hasCat> ?cat }";
Assert.assertEquals(expected, out.get(0) );
//force a non match on a initial-partial var name
varToExisting= new HashMap<String,String>();
varToExisting.put("bob", "http://uri.edu#BobTheElder");
target = "SELECT ?cat WHERE{ ?bobJr <http://uri.edu#hasCat> ?cat }" ;
targets = new ArrayList<String>();
targets.add(target);
out = en3g.subInUris(varToExisting, targets);
Assert.assertNotNull(out);
Assert.assertNotNull( out.get(0) );
expected = target;
Assert.assertEquals(expected, out.get(0) );
}
}