Fixing dependent resource delete. NIHVIVO-88

This commit is contained in:
bdc34 2010-03-22 17:09:18 +00:00
parent 513ddcdc43
commit 56ef6da161
5 changed files with 656 additions and 620 deletions

View file

@ -88,7 +88,6 @@ public class VitroVocabulary {
public static final String IMAGEFILE = vitroURI+"imageFile"; public static final String IMAGEFILE = vitroURI+"imageFile";
public static final String IMAGETHUMB = vitroURI+"imageThumb"; public static final String IMAGETHUMB = vitroURI+"imageThumb";
public static final String DEPENDENT_RESOURCE = vitroURI + "DependentResource";
// ================== property related ================================= // ================== property related =================================
public static final String PROPERTY_ENTITYSORTDIRECTION = vitroURI+"individualSortDirectionAnnot"; public static final String PROPERTY_ENTITYSORTDIRECTION = vitroURI+"individualSortDirectionAnnot";
@ -106,8 +105,9 @@ public class VitroVocabulary {
public static final String PROPERTY_OFFERCREATENEWOPTIONANNOT = vitroURI+"offerCreateNewOptionAnnot"; public static final String PROPERTY_OFFERCREATENEWOPTIONANNOT = vitroURI+"offerCreateNewOptionAnnot";
public static final String PROPERTY_INPROPERTYGROUPANNOT = vitroURI+"inPropertyGroupAnnot"; public static final String PROPERTY_INPROPERTYGROUPANNOT = vitroURI+"inPropertyGroupAnnot";
public static final String PROPERTYGROUP = vitroURI + "PropertyGroup"; public static final String PROPERTYGROUP = vitroURI + "PropertyGroup";
public static final String MASKS_PROPERTY = VitroVocabulary.vitroURI + "masksProperty"; public static final String MASKS_PROPERTY = vitroURI + "masksProperty";
public static final String SKIP_EDIT_FORM = VitroVocabulary.vitroURI + "skipEditForm"; public static final String SKIP_EDIT_FORM = vitroURI + "skipEditForm";
public static final String PROPERTY_DEPENDENCYPROPERTYANNOT = vitroURI + "dependencyPropertyAnnot";
// ================== keyword related ================================== // ================== keyword related ==================================

View file

@ -9,26 +9,49 @@ import java.util.List;
import java.util.ListIterator; import java.util.ListIterator;
import java.util.Set; import java.util.Set;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode; import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource; import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement; import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator; import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.vocabulary.RDF;
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary; import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
/**
* This class handles deletion of resources based on the annotation vitro:dependentResourceAnnot.
*
* The vitro:dependentResourceAnnot
*
* For example, take a graph like:
*
* ex:bob ex:hasPositionHistory ex:positionHistory23 .
* ex:positionHistory23 ex:hasTitle "position 23 was great" .
* ex:hasPositionHistory vitro.dependentResourceAnnot "true"^^xsd:boolean .
*
* When the object property statement ex:bob ex:hasPositionHistory ex:positioinHisroty23 is
* deleted, then everything about ex:positionHistory23 should be deleted because
* ex:hasPositionHistory is a dependent resource property.
*
* @author bdc34
*
*/
public class DependentResourceDeleteJena { public class DependentResourceDeleteJena {
public static List<Statement> getDependentResourceDeleteList ( Statement stmt, Model model){ public static List<Statement> getDependentResourceDeleteList ( Statement stmt, Model model){
if( model == null ) throw new IllegalArgumentException("model must not be null."); if( model == null ) throw new IllegalArgumentException("model must not be null.");
return getDependentResourceDeleteList(stmt, model, new HashSet<String>()); return getDependentResourceDeleteList(stmt, model, new HashSet<String>(), false);
} }
public static List<Statement> getDependentResourceDeleteList (RDFNode node, Model model){ public static List<Statement> getDependentResourceDeleteList(Resource deleteMe, Model sourceModel) {
if( model == null ) throw new IllegalArgumentException("model must not be null."); List<Statement> deletes = new LinkedList<Statement>();
return getDependentResourceDeleteList(node, model, new HashSet<String>()); for( Statement stmt : getAllStatements(deleteMe, sourceModel)){
deletes.add(stmt);
deletes.addAll(getDependentResourceDeleteList(stmt, sourceModel, new HashSet<String>(), false));
}
return deletes;
} }
public static Model getDependentResourceDeleteForChange( Model assertions, Model retractions, Model sourceModel){ public static Model getDependentResourceDeleteForChange( Model assertions, Model retractions, Model sourceModel){
@ -103,55 +126,29 @@ public class DependentResourceDeleteJena {
return toRemove; return toRemove;
} }
private static List<Statement> getDependentResourceDeleteList ( Statement stmt, Model model, Set<String> visitedUris){ private static List<Statement> getDependentResourceDeleteList ( Statement stmt, Model model, Set<String> visitedUris, boolean perviousWasDependentResource ){
if( stmt == null ) if( stmt == null )
return Collections.emptyList(); return Collections.emptyList();
List<Statement> toDelete = new LinkedList<Statement>(); List<Statement> toDelete = new LinkedList<Statement>();
RDFNode obj = stmt.getObject();
if( obj.isLiteral() ){
toDelete.add(stmt); toDelete.add(stmt);
}else if( obj.isResource() ){ RDFNode obj = stmt.getObject();
if( ( obj.canAs(Resource.class) && isPredicateDependencyRelation(stmt.getPredicate(), model) )
|| ( obj.isAnon() && perviousWasDependentResource ) ){
Resource res = (Resource)obj.as(Resource.class); Resource res = (Resource)obj.as(Resource.class);
String id = res.isAnon()?res.getId().toString():res.getURI(); String id = res.isAnon()?res.getId().toString():res.getURI();
toDelete.add(stmt);
if(!visitedUris.contains(id) && isStubResource(res, model) ){ if( !visitedUris.contains(id) ){
visitedUris.add(id); visitedUris.add(id);
for( Statement stubStmt : getAllStatements(res, model)){ for( Statement stubStmt : getAllStatements(res, model)){
toDelete.addAll( getDependentResourceDeleteList(stubStmt, model,visitedUris)); toDelete.addAll( getDependentResourceDeleteList(stubStmt, model,visitedUris,true));
} }
} }
} }
return toDelete; return toDelete;
} }
private static List<Statement> getDependentResourceDeleteList (RDFNode node, Model model, Set<String> visitedUris){
if( node == null )
return Collections.emptyList();
List<Statement> toDelete = new LinkedList<Statement>();
if( node.isLiteral() ){
//Literals are ignored
}else if( node.isResource() ){
Resource res = (Resource)node.as(Resource.class);
String id = res.isAnon()?res.getId().toString():res.getURI();
if(!visitedUris.contains(id) && isStubResource(res, model) ){
visitedUris.add(id);
for( Statement stubStmt : getAllStatements(res, model)){
toDelete.addAll( getDependentResourceDeleteList(stubStmt, model,visitedUris) );
}
}
}
return toDelete;
}
private static boolean isStubResource(Resource res, Model model){
//boolean q = res.hasProperty(RDF.type, model.createProperty(VitroVocabulary.DEPENDENT_RESOURCE));
boolean q = model.contains(res, RDF.type,model.createProperty(VitroVocabulary.DEPENDENT_RESOURCE));
return q;
}
private static List<Statement> getAllStatements(Resource res, Model model){ private static List<Statement> getAllStatements(Resource res, Model model){
List<Statement> deleteUs = new LinkedList<Statement>(); List<Statement> deleteUs = new LinkedList<Statement>();
StmtIterator it = model.listStatements(null, null, res); StmtIterator it = model.listStatements(null, null, res);
@ -172,5 +169,13 @@ public class DependentResourceDeleteJena {
return deleteUs; return deleteUs;
} }
private static boolean isPredicateDependencyRelation( Property predicate , Model model){
return model.containsLiteral(
model.getResource(predicate.getURI()),
model.createProperty(VitroVocabulary.PROPERTY_DEPENDENCYPROPERTYANNOT),
true);
}
} }

View file

@ -879,4 +879,6 @@ public class IndividualJena extends IndividualImpl implements Individual {
log.error(e); log.error(e);
} }
} }
} }

View file

@ -12,11 +12,26 @@ import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFNode; import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Statement; import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator; import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.vocabulary.XSD;
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary; import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
public class DependentResourceDeleteJenaTest { public class DependentResourceDeleteJenaTest {
String depRes = "<" + VitroVocabulary.DEPENDENT_RESOURCE + ">"; String isDependentRelation =
" <"+VitroVocabulary.PROPERTY_DEPENDENCYPROPERTYANNOT+"> \"true\"^^xsd:boolean .\n" ;
String nosePropIsDependentRel =
"<"+VitroVocabulary.PROPERTY_DEPENDENCYPROPERTYANNOT+"> rdf:type owl:AnnotationProperty .\n" +
" ex:hasNose " + isDependentRelation;
String prefixesN3 =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" +
"@prefix xsd: <" + XSD.getURI() + "> . \n " +
"@prefix ex: <http://example.com/> . \n" +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n";
void printModels(Model expected, Model result){ void printModels(Model expected, Model result){
System.out.println("Expected:"); System.out.println("Expected:");
@ -28,11 +43,7 @@ public class DependentResourceDeleteJenaTest {
@org.junit.Test @org.junit.Test
public void testStmtNormalDelete() { public void testStmtNormalDelete() {
String n3 = String n3 =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:hasNose ex:nose1 . " ; " ex:bob ex:hasNose ex:nose1 . " ;
Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3"); Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3");
@ -53,19 +64,21 @@ public class DependentResourceDeleteJenaTest {
@org.junit.Test @org.junit.Test
public void testStmtSimpleForceDelete() { public void testStmtSimpleForceDelete() {
String n3 = String n3=
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" + nosePropIsDependentRel +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+ "ex:hasHair " + isDependentRelation +
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+ " ex:bob ex:hasNose ex:nose1 . \n" +
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+ " ex:nose1 ex:hasHair ex:hair23. \n" +
" ex:hair23 ex:hasHairCount \"23\". " ;
String expected =
prefixesN3 +
" ex:bob ex:hasNose ex:nose1 . \n" + " ex:bob ex:hasNose ex:nose1 . \n" +
" ex:nose1 rdf:type " + depRes + " . \n" +
" ex:nose1 ex:hasHair ex:hair23. \n" + " ex:nose1 ex:hasHair ex:hair23. \n" +
" ex:hair23 rdf:type " + depRes + " . \n" +
" ex:hair23 ex:hasHairCount \"23\". " ; " ex:hair23 ex:hasHairCount \"23\". " ;
Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3"); Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3");
Model expectedModel = (ModelFactory.createDefaultModel()).read(new StringReader(expected), "", "N3");
List<Statement> deletes = DependentResourceDeleteJena.getDependentResourceDeleteList( List<Statement> deletes = DependentResourceDeleteJena.getDependentResourceDeleteList(
model.createStatement( model.createStatement(
@ -78,32 +91,21 @@ public class DependentResourceDeleteJenaTest {
resultModel.add(deletes); resultModel.add(deletes);
//all statements should be deleted //all statements should be deleted
boolean same = resultModel.isIsomorphicWith( model ); Assert.assertTrue( resultModel.isIsomorphicWith( expectedModel ) ) ;
Assert.assertTrue( same);
} }
@org.junit.Test @org.junit.Test
public void testStmtNonForceDelete() { public void testStmtNonForceDelete() {
String n3 = String n3 =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:hasNose ex:nose1 . \n" + " ex:bob ex:hasNose ex:nose1 . \n" +
" ex:nose1 ex:hasHair ex:hair23. \n" + " ex:nose1 ex:hasHair ex:hair23. \n" +
" ex:hair23 rdf:type " + depRes + " . \n" +
" ex:hair23 ex:hasHairCount \"23\". " ; " ex:hair23 ex:hasHairCount \"23\". " ;
String expected = String expected =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:nose1 ex:hasHair ex:hair23. \n" + " ex:nose1 ex:hasHair ex:hair23. \n" +
" ex:hair23 rdf:type " + depRes + " . \n" +
" ex:hair23 ex:hasHairCount \"23\". " ; " ex:hair23 ex:hasHairCount \"23\". " ;
Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3"); Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3");
@ -127,26 +129,20 @@ public class DependentResourceDeleteJenaTest {
@org.junit.Test @org.junit.Test
public void testStmtForceDeleteWithLiterals() { public void testStmtForceDeleteWithLiterals() {
String n3 = String n3 =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" + nosePropIsDependentRel +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+ "ex:hasHair " + isDependentRelation +
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:a \"Bob\". \n" + " ex:bob ex:a \"Bob\". \n" +
" ex:bob ex:hasNose ex:nose1 . \n" + " ex:bob ex:hasNose ex:nose1 . \n" +
" ex:nose1 rdf:type " + depRes + " . \n" +
" ex:nose1 ex:a \"this is a literal\". \n" + " ex:nose1 ex:a \"this is a literal\". \n" +
" ex:nose1 ex:b \"2343\" . \n" + " ex:nose1 ex:b \"2343\" . \n" +
" ex:nose1 ex:hasHair ex:hair23. \n" + " ex:nose1 ex:hasHair ex:hair23. \n" +
" ex:hair23 rdf:type " + depRes + " . \n" +
" ex:hair23 ex:hasHairCount \"23\". " ; " ex:hair23 ex:hasHairCount \"23\". " ;
String expected = String expected =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" + nosePropIsDependentRel +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+ "ex:hasHair " + isDependentRelation +
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:a \"Bob\". \n" ; " ex:bob ex:a \"Bob\". \n" ;
Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3"); Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3");
@ -166,31 +162,58 @@ public class DependentResourceDeleteJenaTest {
Assert.assertTrue( same ); Assert.assertTrue( same );
} }
@org.junit.Test
public void testStmtForceDeleteWithSimpleCycles() {
String n3 =
prefixesN3 +
nosePropIsDependentRel +
"ex:hasHair " + isDependentRelation +
" ex:bob ex:a \"Bob\". \n" +
" ex:bob ex:hasNose ex:nose1 . \n" +
" ex:nose1 ex:c ex:bob ." ;
String expected =
prefixesN3 +
nosePropIsDependentRel +
"ex:hasHair " + isDependentRelation +
" ex:bob ex:a \"Bob\"." ;
Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3");
List<Statement> deletes = DependentResourceDeleteJena.getDependentResourceDeleteList(
model.createStatement(
model.createResource("http://example.com/bob"),
model.createProperty("http://example.com/hasNose"),
model.createResource("http://example.com/nose1")),
model);
model.remove(deletes);
Model expectedModel = (ModelFactory.createDefaultModel()).read(new StringReader(expected), "", "N3");
boolean same = expectedModel.isIsomorphicWith( model );
if( ! same ) printModels( expectedModel, model);
Assert.assertTrue( same );
}
@org.junit.Test @org.junit.Test
public void testStmtForceDeleteWithCycles() { public void testStmtForceDeleteWithCycles() {
String n3 = String n3 =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" + nosePropIsDependentRel +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+ "ex:hasHair " + isDependentRelation +
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:a \"Bob\". \n" + " ex:bob ex:a \"Bob\". \n" +
" ex:bob ex:hasNose ex:nose1 . \n" + " ex:bob ex:hasNose ex:nose1 . \n" +
" ex:nose1 rdf:type " + depRes + " . \n" +
" ex:nose1 ex:a \"this is a literal\". \n" + " ex:nose1 ex:a \"this is a literal\". \n" +
" ex:nose1 ex:b \"2343\" . \n" + " ex:nose1 ex:b \"2343\" . \n" +
" ex:nose1 ex:c ex:bob . \n" + " ex:nose1 ex:c ex:bob . \n" +
" ex:nose1 ex:hasHair ex:hair23. \n" + " ex:nose1 ex:hasHair ex:hair23. \n" +
" ex:hair23 rdf:type " + depRes + " . \n" +
" ex:hair23 ex:c ex:bob . \n" + " ex:hair23 ex:c ex:bob . \n" +
" ex:hair23 ex:hasHairCount \"23\". " ; " ex:hair23 ex:hasHairCount \"23\". " ;
String expected = String expected =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" + nosePropIsDependentRel +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+ "ex:hasHair " + isDependentRelation +
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:a \"Bob\". \n" ; " ex:bob ex:a \"Bob\". \n" ;
Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3"); Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3");
@ -213,25 +236,18 @@ public class DependentResourceDeleteJenaTest {
@org.junit.Test @org.junit.Test
public void testStmtForceDeleteWithCycles2() { public void testStmtForceDeleteWithCycles2() {
String n3 = String n3 =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" + nosePropIsDependentRel +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:a \"Bob\". \n" + " ex:bob ex:a \"Bob\". \n" +
" ex:bob ex:hasNose ex:nose1 . \n" + " ex:bob ex:hasNose ex:nose1 . \n" +
" ex:nose1 rdf:type " + depRes + " . \n" +
" ex:nose1 ex:a \"this is a literal\". \n" + " ex:nose1 ex:a \"this is a literal\". \n" +
" ex:nose1 ex:b \"2343\" . \n" + " ex:nose1 ex:b \"2343\" . \n" +
" ex:nose1 ex:c ex:nose1 . \n" + " ex:nose1 ex:c ex:nose1 . \n" +
" ex:nose1 ex:c ex:bob . \n" ; " ex:nose1 ex:c ex:bob . \n" ;
String expected = String expected =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" + nosePropIsDependentRel +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:a \"Bob\". \n" ; " ex:bob ex:a \"Bob\". \n" ;
Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3"); Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3");
@ -254,24 +270,17 @@ public class DependentResourceDeleteJenaTest {
@org.junit.Test @org.junit.Test
public void testStmtForceDeleteWithLinks() { public void testStmtForceDeleteWithLinks() {
String n3 = String n3 =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" + nosePropIsDependentRel +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:a \"Bob\". \n" + " ex:bob ex:a \"Bob\". \n" +
" ex:bob ex:hasNose ex:nose1 . \n" + " ex:bob ex:hasNose ex:nose1 . \n" +
" ex:nose1 rdf:type " + depRes + " . \n" +
" ex:nose1 ex:c ex:glasses65 . \n" + " ex:nose1 ex:c ex:glasses65 . \n" +
" ex:glasses65 ex:c ex:nose1 . \n" + " ex:glasses65 ex:c ex:nose1 . \n" +
" ex:glasses65 ex:a \"glasses 65\" ." ; " ex:glasses65 ex:a \"glasses 65\" ." ;
String expected = String expected =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" + nosePropIsDependentRel +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:a \"Bob\". \n" + " ex:bob ex:a \"Bob\". \n" +
" ex:glasses65 ex:a \"glasses 65\" ." ; " ex:glasses65 ex:a \"glasses 65\" ." ;
@ -295,24 +304,17 @@ public class DependentResourceDeleteJenaTest {
@org.junit.Test @org.junit.Test
public void testStmtForceDeleteWithBNodes() { public void testStmtForceDeleteWithBNodes() {
String n3 = String n3 =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" + nosePropIsDependentRel +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:a \"Bob\". \n" + " ex:bob ex:a \"Bob\". \n" +
" ex:bob ex:hasNose [ \n" + " ex:bob ex:hasNose [ \n" +
" rdf:type " + depRes + " ; \n" +
" ex:a \"this is a bnode\"; \n" + " ex:a \"this is a bnode\"; \n" +
" ex:c ex:glasses65 ] . \n" + " ex:c ex:glasses65 ] . \n" +
" ex:glasses65 ex:a \"glasses 65\" ." ; " ex:glasses65 ex:a \"glasses 65\" ." ;
String expected = String expected =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" + nosePropIsDependentRel +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:a \"Bob\". \n" + " ex:bob ex:a \"Bob\". \n" +
" ex:glasses65 ex:a \"glasses 65\" ." ; " ex:glasses65 ex:a \"glasses 65\" ." ;
@ -336,27 +338,20 @@ public class DependentResourceDeleteJenaTest {
@org.junit.Test @org.junit.Test
public void testStmtForceDeleteWithNestedBNodes() { public void testStmtForceDeleteWithNestedBNodes() {
String n3 = String n3 =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" + nosePropIsDependentRel +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:a \"Bob\". \n" + " ex:bob ex:a \"Bob\". \n" +
" ex:bob ex:hasNose [ \n" + " ex:bob ex:hasNose [ \n" +
" rdf:type " + depRes + " ; \n" +
" ex:a \"this is a bnode\"; \n" + " ex:a \"this is a bnode\"; \n" +
" ex:c ex:glasses65 ; \n" + " ex:c ex:glasses65 ; \n" +
" ex:c [ rdf:type " + depRes + " ;" + " ex:c [ " +
" ex:a \"this is a nested bnode\" ] " + " ex:a \"this is a nested bnode\" ] " +
"] . \n" + "] . \n" +
" ex:glasses65 ex:a \"glasses 65\" ." ; " ex:glasses65 ex:a \"glasses 65\" ." ;
String expected = String expected =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" + nosePropIsDependentRel +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:a \"Bob\". \n" + " ex:bob ex:a \"Bob\". \n" +
" ex:glasses65 ex:a \"glasses 65\" ." ; " ex:glasses65 ex:a \"glasses 65\" ." ;
@ -377,47 +372,51 @@ public class DependentResourceDeleteJenaTest {
Assert.assertTrue( same ); Assert.assertTrue( same );
} }
@org.junit.Test @org.junit.Test
public void testResNormalDelete() { public void testResNormalDelete() {
String n3 = String n3 =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" + nosePropIsDependentRel +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:hasNose ex:nose1 . " ; " ex:bob ex:hasNose ex:nose1 . " ;
String expected =
prefixesN3 +
nosePropIsDependentRel ;
Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3"); Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3");
Model expectedModel = (ModelFactory.createDefaultModel()).read(new StringReader(expected), "", "N3");
List<Statement> deletes = DependentResourceDeleteJena.getDependentResourceDeleteList( List<Statement> deletes = DependentResourceDeleteJena.getDependentResourceDeleteList(
model.createResource("http://example.com/nose1"),model); model.createResource("http://example.com/nose1"),model);
Assert.assertTrue( deletes != null && deletes.size() ==0 ); Model resultModel = model.remove(deletes);
//all statements should be deleted
boolean same = resultModel.isIsomorphicWith( expectedModel );
if( ! same ) printModels( expectedModel, model);
Assert.assertTrue( same);
} }
@org.junit.Test @org.junit.Test
public void testResSimpleForceDelete() { public void testResSimpleForceDelete() {
String n3 = String n3 =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" + nosePropIsDependentRel +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:hasNose ex:nose1 . \n" + " ex:bob ex:hasNose ex:nose1 . \n" +
" ex:nose1 rdf:type " + depRes + " . \n" +
" ex:nose1 ex:hasHair ex:hair23. \n" + " ex:nose1 ex:hasHair ex:hair23. \n" +
" ex:hair23 rdf:type " + depRes + " . \n" + " ex:hair23 ex:hasHairCount \"23\". " ;
String expected =
prefixesN3 +
nosePropIsDependentRel +
" ex:hair23 ex:hasHairCount \"23\". " ; " ex:hair23 ex:hasHairCount \"23\". " ;
Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3"); Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3");
Model expectedModel = (ModelFactory.createDefaultModel()).read(new StringReader(expected), "", "N3");
List<Statement> deletes = DependentResourceDeleteJena.getDependentResourceDeleteList( List<Statement> deletes = DependentResourceDeleteJena.getDependentResourceDeleteList(
model.createResource("http://example.com/nose1"),model); model.createResource("http://example.com/nose1"),model);
Model resultModel = ModelFactory.createDefaultModel(); Model resultModel = model.remove(deletes);
resultModel.add(deletes);
//all statements should be deleted //all statements should be deleted
boolean same = resultModel.isIsomorphicWith( model ); boolean same = resultModel.isIsomorphicWith( expectedModel );
if( ! same ) printModels( expectedModel, model);
Assert.assertTrue( same); Assert.assertTrue( same);
} }
@ -425,45 +424,68 @@ public class DependentResourceDeleteJenaTest {
@org.junit.Test @org.junit.Test
public void testResNonForceDelete() { public void testResNonForceDelete() {
String n3 = String n3 =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:hasNose ex:nose1 . \n" + " ex:bob ex:hasNose ex:nose1 . \n" +
" ex:nose1 ex:hasHair ex:hair23. \n" + " ex:nose1 ex:hasHair ex:hair23. \n" +
" ex:hair23 rdf:type " + depRes + " . \n" + " ex:hair23 ex:hasHairCount \"23\". " ;
String expected =
prefixesN3 +
" ex:hair23 ex:hasHairCount \"23\". " ; " ex:hair23 ex:hasHairCount \"23\". " ;
Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3"); Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3");
Model expectedModel = (ModelFactory.createDefaultModel()).read(new StringReader(expected), "", "N3");
List<Statement> deletes = DependentResourceDeleteJena.getDependentResourceDeleteList( List<Statement> deletes = DependentResourceDeleteJena.getDependentResourceDeleteList(
model.createResource("http://example.com/nose1"),model); model.createResource("http://example.com/nose1"),model);
Assert.assertTrue( deletes != null && deletes.size() == 0); Model resultModel = model.remove(deletes);
//all statements should be deleted
boolean same = resultModel.isIsomorphicWith( expectedModel );
if( ! same ) printModels( expectedModel, model);
Assert.assertTrue( same);
} }
@org.junit.Test
public void testResNonForceDelete2() {
String n3 =
prefixesN3 +
nosePropIsDependentRel +
" ex:bob ex:hasNose ex:nose1 . \n" +
" ex:nose1 ex:hasHair ex:hair23. \n" +
" ex:hair23 ex:hasHairCount \"23\". " ;
String expected =
prefixesN3 +
nosePropIsDependentRel +
" ex:hair23 ex:hasHairCount \"23\". " ;
Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3");
Model expectedModel = (ModelFactory.createDefaultModel()).read(new StringReader(expected), "", "N3");
List<Statement> deletes = DependentResourceDeleteJena.getDependentResourceDeleteList(
model.createResource("http://example.com/nose1"),model);
Model resultModel = model.remove(deletes);
//all statements should be deleted
boolean same = resultModel.isIsomorphicWith( expectedModel );
if( ! same ) printModels( expectedModel, model);
Assert.assertTrue( same);
}
@org.junit.Test @org.junit.Test
public void testResForceDeleteWithLiterals() { public void testResForceDeleteWithLiterals() {
String n3 = String n3 =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" + nosePropIsDependentRel +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+ " ex:hasHair " + isDependentRelation +
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:a \"Bob\". \n" + " ex:bob ex:a \"Bob\". \n" +
" ex:bob ex:hasNose ex:nose1 . \n" + " ex:bob ex:hasNose ex:nose1 . \n" +
" ex:nose1 rdf:type " + depRes + " . \n" +
" ex:nose1 ex:a \"this is a literal\". \n" + " ex:nose1 ex:a \"this is a literal\". \n" +
" ex:nose1 ex:b \"2343\" . \n" + " ex:nose1 ex:b \"2343\" . \n" +
" ex:nose1 ex:hasHair ex:hair23. \n" + " ex:nose1 ex:hasHair ex:hair23. \n" +
" ex:hair23 rdf:type " + depRes + " . \n" +
" ex:hair23 ex:hasHairCount \"23\". " ; " ex:hair23 ex:hasHairCount \"23\". " ;
String expected = String expected =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" + nosePropIsDependentRel +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+ " ex:hasHair " + isDependentRelation +
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:a \"Bob\". \n" ; " ex:bob ex:a \"Bob\". \n" ;
Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3"); Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3");
@ -481,28 +503,22 @@ public class DependentResourceDeleteJenaTest {
@org.junit.Test @org.junit.Test
public void testResForceDeleteWithCycles() { public void testResForceDeleteWithCycles() {
String n3 = String n3 =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" + nosePropIsDependentRel +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+ "ex:hasHair " + isDependentRelation +
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:a \"Bob\". \n" + " ex:bob ex:a \"Bob\". \n" +
" ex:bob ex:hasNose ex:nose1 . \n" + " ex:bob ex:hasNose ex:nose1 . \n" +
" ex:nose1 rdf:type " + depRes + " . \n" +
" ex:nose1 ex:a \"this is a literal\". \n" + " ex:nose1 ex:a \"this is a literal\". \n" +
" ex:nose1 ex:b \"2343\" . \n" + " ex:nose1 ex:b \"2343\" . \n" +
" ex:nose1 ex:c ex:bob . \n" + " ex:nose1 ex:c ex:bob . \n" +
" ex:nose1 ex:hasHair ex:hair23. \n" + " ex:nose1 ex:hasHair ex:hair23. \n" +
" ex:hair23 rdf:type " + depRes + " . \n" +
" ex:hair23 ex:c ex:bob . \n" + " ex:hair23 ex:c ex:bob . \n" +
" ex:hair23 ex:hasHairCount \"23\". " ; " ex:hair23 ex:hasHairCount \"23\". " ;
String expected = String expected =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" + nosePropIsDependentRel +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+ "ex:hasHair " + isDependentRelation +
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:a \"Bob\". \n" ; " ex:bob ex:a \"Bob\". \n" ;
Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3"); Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3");
@ -520,30 +536,22 @@ public class DependentResourceDeleteJenaTest {
@org.junit.Test @org.junit.Test
public void testResForceDeleteWithCycles2() { public void testResForceDeleteWithCycles2() {
String n3 = String n3 =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" + nosePropIsDependentRel +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:a \"Bob\". \n" + " ex:bob ex:a \"Bob\". \n" +
" ex:bob ex:hasNose ex:nose1 . \n" + " ex:bob ex:hasNose ex:nose1 . \n" +
" ex:nose1 rdf:type " + depRes + " . \n" +
" ex:nose1 ex:a \"this is a literal\". \n" + " ex:nose1 ex:a \"this is a literal\". \n" +
" ex:nose1 ex:b \"2343\" . \n" + " ex:nose1 ex:b \"2343\" . \n" +
" ex:nose1 ex:c ex:nose1 . \n" + " ex:nose1 ex:c ex:nose1 . \n" +
" ex:nose1 ex:c ex:bob . \n" ; " ex:nose1 ex:c ex:bob . \n" ;
String expected = String expected =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" + nosePropIsDependentRel ;
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:a \"Bob\". \n" ;
Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3"); Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3");
List<Statement> deletes = DependentResourceDeleteJena.getDependentResourceDeleteList( List<Statement> deletes = DependentResourceDeleteJena.getDependentResourceDeleteList(
model.createResource("http://example.com/nose1"),model); model.createResource("http://example.com/bob"),model);
model.remove(deletes); model.remove(deletes);
Model expectedModel = (ModelFactory.createDefaultModel()).read(new StringReader(expected), "", "N3"); Model expectedModel = (ModelFactory.createDefaultModel()).read(new StringReader(expected), "", "N3");
@ -555,24 +563,17 @@ public class DependentResourceDeleteJenaTest {
@org.junit.Test @org.junit.Test
public void testResForceDeleteWithLinks() { public void testResForceDeleteWithLinks() {
String n3 = String n3 =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" + nosePropIsDependentRel +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:a \"Bob\". \n" + " ex:bob ex:a \"Bob\". \n" +
" ex:bob ex:hasNose ex:nose1 . \n" + " ex:bob ex:hasNose ex:nose1 . \n" +
" ex:nose1 rdf:type " + depRes + " . \n" +
" ex:nose1 ex:c ex:glasses65 . \n" + " ex:nose1 ex:c ex:glasses65 . \n" +
" ex:glasses65 ex:c ex:nose1 . \n" + " ex:glasses65 ex:c ex:nose1 . \n" +
" ex:glasses65 ex:a \"glasses 65\" ." ; " ex:glasses65 ex:a \"glasses 65\" ." ;
String expected = String expected =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" + nosePropIsDependentRel +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:a \"Bob\". \n" + " ex:bob ex:a \"Bob\". \n" +
" ex:glasses65 ex:a \"glasses 65\" ." ; " ex:glasses65 ex:a \"glasses 65\" ." ;
Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3"); Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3");
@ -587,117 +588,97 @@ public class DependentResourceDeleteJenaTest {
Assert.assertTrue( same ); Assert.assertTrue( same );
} }
@org.junit.Test // @org.junit.Test
public void testResForceDeleteWithBNodes() { // public void testResForceDeleteWithBNodes() {
String n3 = // String n3 =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + // prefixesN3 +
"@prefix ex: <http://example.com/> . \n" + // nosePropIsDependentRel +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+ // " ex:bob ex:a \"Bob\". \n" +
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+ // " ex:bob ex:hasNose [ \n" +
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+ // " ex:a \"this is a bnode\"; \n" +
" ex:bob ex:a \"Bob\". \n" + // " ex:c ex:glasses65 ] . \n" +
" ex:bob ex:hasNose [ \n" + // " ex:glasses65 ex:a \"glasses 65\" ." ;
" rdf:type " + depRes + " ; \n" + //
" ex:a \"this is a bnode\"; \n" + // String expected =
" ex:c ex:glasses65 ] . \n" + // prefixesN3 +
" ex:glasses65 ex:a \"glasses 65\" ." ; // nosePropIsDependentRel +
// " ex:bob ex:a \"Bob\". \n" +
// " ex:glasses65 ex:a \"glasses 65\" ." ;
// Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3");
//
// StmtIterator stmtIt = model.listStatements(
// model.createResource("http://example.com/bob"),
// model.createProperty("http://example.com/hasNose"),
// (RDFNode)null);
//
// RDFNode bnode = stmtIt.nextStatement().getObject();
//
// List<Statement> deletes =
// DependentResourceDeleteJena.getDependentResourceDeleteList(bnode,model);
// model.remove(deletes);
//
// Model expectedModel = (ModelFactory.createDefaultModel()).read(new StringReader(expected), "", "N3");
// boolean same = expectedModel.isIsomorphicWith( model );
// if( ! same ) printModels( expectedModel, model);
// Assert.assertTrue( same );
// }
String expected = // @org.junit.Test
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + // public void testResForceDeleteWithNestedBNodes() {
"@prefix ex: <http://example.com/> . \n" + // String n3 =
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+ // prefixesN3 +
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+ // nosePropIsDependentRel +
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+ // " ex:bob ex:a \"Bob\". \n" +
" ex:bob ex:a \"Bob\". \n" + // " ex:bob ex:hasNose [ \n" +
" ex:glasses65 ex:a \"glasses 65\" ." ; // " ex:a \"this is a bnode\"; \n" +
Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3"); // " ex:c ex:glasses65 ; \n" +
// " ex:c [ " +
StmtIterator stmtIt = model.listStatements( // " ex:a \"this is a nested bnode\" ] " +
model.createResource("http://example.com/bob"), // "] . \n" +
model.createProperty("http://example.com/hasNose"), // " ex:glasses65 ex:a \"glasses 65\" ." ;
(RDFNode)null); //
// String expected =
RDFNode bnode = stmtIt.nextStatement().getObject(); // prefixesN3 +
// nosePropIsDependentRel +
List<Statement> deletes = // " ex:bob ex:a \"Bob\". \n" +
DependentResourceDeleteJena.getDependentResourceDeleteList(bnode,model); // " ex:glasses65 ex:a \"glasses 65\" ." ;
model.remove(deletes); //
// Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3");
Model expectedModel = (ModelFactory.createDefaultModel()).read(new StringReader(expected), "", "N3"); //
boolean same = expectedModel.isIsomorphicWith( model ); // StmtIterator stmtIt = model.listStatements(
if( ! same ) printModels( expectedModel, model); // model.createResource("http://example.com/bob"),
Assert.assertTrue( same ); // model.createProperty("http://example.com/hasNose"),
} // (RDFNode)null);
// RDFNode bnode = stmtIt.nextStatement().getObject();
@org.junit.Test //
public void testResForceDeleteWithNestedBNodes() { // List<Statement> deletes =
String n3 = // DependentResourceDeleteJena.getDependentResourceDeleteList(bnode,model);
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + // model.remove(deletes);
"@prefix ex: <http://example.com/> . \n" + //
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+ // Model expectedModel = (ModelFactory.createDefaultModel()).read(new StringReader(expected), "", "N3");
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+ // boolean same = expectedModel.isIsomorphicWith( model );
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+ // if( ! same ) printModels( expectedModel, model);
" ex:bob ex:a \"Bob\". \n" + // Assert.assertTrue( same );
" ex:bob ex:hasNose [ \n" + // }
" rdf:type " + depRes + " ; \n" +
" ex:a \"this is a bnode\"; \n" +
" ex:c ex:glasses65 ; \n" +
" ex:c [ rdf:type " + depRes + " ;" +
" ex:a \"this is a nested bnode\" ] " +
"] . \n" +
" ex:glasses65 ex:a \"glasses 65\" ." ;
String expected =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" +
"@prefix ex: <http://example.com/> . \n" +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:a \"Bob\". \n" +
" ex:glasses65 ex:a \"glasses 65\" ." ;
Model model = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3");
StmtIterator stmtIt = model.listStatements(
model.createResource("http://example.com/bob"),
model.createProperty("http://example.com/hasNose"),
(RDFNode)null);
RDFNode bnode = stmtIt.nextStatement().getObject();
List<Statement> deletes =
DependentResourceDeleteJena.getDependentResourceDeleteList(bnode,model);
model.remove(deletes);
Model expectedModel = (ModelFactory.createDefaultModel()).read(new StringReader(expected), "", "N3");
boolean same = expectedModel.isIsomorphicWith( model );
if( ! same ) printModels( expectedModel, model);
Assert.assertTrue( same );
}
@org.junit.Test @org.junit.Test
public void testDeleteForChange() { public void testDeleteForChange() {
String source = String source =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" + nosePropIsDependentRel +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+ "ex:hasHair " + isDependentRelation +
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:a \"Bob\". \n" + " ex:bob ex:a \"Bob\". \n" +
" ex:bob ex:hasNose ex:nose1 . \n" + " ex:bob ex:hasNose ex:nose1 . \n" +
" ex:nose1 rdf:type " + depRes + " . \n" +
" ex:nose1 ex:a \"this is a literal\". \n" + " ex:nose1 ex:a \"this is a literal\". \n" +
" ex:nose1 ex:b \"2343\" . \n" + " ex:nose1 ex:b \"2343\" . \n" +
" ex:nose1 ex:hasHair ex:hair23. \n" + " ex:nose1 ex:hasHair ex:hair23. \n" +
" ex:hair23 rdf:type " + depRes + " . \n" +
" ex:hair23 ex:hasHairCount \"23\". " ; " ex:hair23 ex:hasHairCount \"23\". " ;
String expected = String expected =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" + nosePropIsDependentRel +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+ "ex:hasHair " + isDependentRelation +
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:a \"Bob\". \n" ; " ex:bob ex:a \"Bob\". \n" ;
String retractions = String retractions =
@ -722,35 +703,25 @@ public class DependentResourceDeleteJenaTest {
@org.junit.Test @org.junit.Test
public void testDeleteForChangeWithReplace() { public void testDeleteForChangeWithReplace() {
String source = String source =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" + nosePropIsDependentRel +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:a \"Bob\". \n" + " ex:bob ex:a \"Bob\". \n" +
" ex:jim ex:a \"Jim\". \n" + " ex:jim ex:a \"Jim\". \n" +
" ex:bob ex:hasNose ex:nose1 . \n" + " ex:bob ex:hasNose ex:nose1 . \n" +
" ex:nose1 rdf:type " + depRes + " . \n" +
" ex:nose1 ex:a \"this is a literal\". \n" + " ex:nose1 ex:a \"this is a literal\". \n" +
" ex:nose1 ex:b \"2343\" . \n" + " ex:nose1 ex:b \"2343\" . \n" +
" ex:nose1 ex:hasHair ex:hair23. \n" + " ex:nose1 ex:hasHair ex:hair23. \n" +
" ex:hair23 rdf:type " + depRes + " . \n" +
" ex:hair23 ex:hasHairCount \"23\". " ; " ex:hair23 ex:hasHairCount \"23\". " ;
String expected = String expected =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" + nosePropIsDependentRel +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:jim ex:a \"Jim\". \n" + " ex:jim ex:a \"Jim\". \n" +
" ex:bob ex:a \"Bob\". \n" + " ex:bob ex:a \"Bob\". \n" +
" ex:jim ex:hasNose ex:nose1 . \n" + " ex:jim ex:hasNose ex:nose1 . \n" +
" ex:nose1 rdf:type " + depRes + " . \n" +
" ex:nose1 ex:a \"this is a literal\". \n" + " ex:nose1 ex:a \"this is a literal\". \n" +
" ex:nose1 ex:b \"2343\" . \n" + " ex:nose1 ex:b \"2343\" . \n" +
" ex:nose1 ex:hasHair ex:hair23. \n" + " ex:nose1 ex:hasHair ex:hair23. \n" +
" ex:hair23 rdf:type " + depRes + " . \n" +
" ex:hair23 ex:hasHairCount \"23\". " ; " ex:hair23 ex:hasHairCount \"23\". " ;
String additions = String additions =
@ -777,48 +748,51 @@ public class DependentResourceDeleteJenaTest {
Assert.assertTrue( same ); Assert.assertTrue( same );
} }
@org.junit.Ignore @org.junit.Test
@org.junit.Test
public void testDeleteWithNonZeroInDegree() { public void testDeleteWithNonZeroInDegree() {
/* /*
This tests deleteing a position context node from the orginization side. This tests deleting a position context node from the organization side.
Currently the required behaivor is that the position context node not be Currently the required behavior is that the position context node not be
deleted when the object property statement is deleted from the org side. deleted when the object property statement is deleted from the organization side.
*/ */
String source = String source =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" + " ex:personHasPosition " + isDependentRelation +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:a \"Bob\". \n" + " ex:bob ex:a \"Bob\". \n" +
" ex:orgP ex:a \"orgP\". \n" + " ex:orgP ex:a \"orgP\". \n" +
" ex:bob ex:hasPosition ex:position1 . \n" + " ex:bob ex:personHasPosition ex:position1 . \n" +
" ex:orgP ex:hasPosition ex:position1 . \n" + " ex:orgP ex:positionInOrganization ex:position1 . \n" +
" ex:position1 rdf:type " + depRes + " . \n" +
" ex:position1 ex:a \"This is Position1\". \n" + " ex:position1 ex:a \"This is Position1\". \n" +
" ex:position1 ex:b \"2343\" . "; " ex:position1 ex:b \"2343\" . ";
String expected = String expected =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" + prefixesN3 +
"@prefix ex: <http://example.com/> . \n" + " ex:personHasPosition " + isDependentRelation +
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . \n"+
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . \n"+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . \n"+
" ex:bob ex:a \"Bob\". \n" + " ex:bob ex:a \"Bob\". \n" +
" ex:orgP ex:a \"orgP\". \n" + " ex:orgP ex:a \"orgP\". \n" +
" ex:bob ex:hasPosition ex:position1 . \n" + " ex:bob ex:personHasPosition ex:position1 . \n" +
" ex:position1 rdf:type " + depRes + " . \n" +
" ex:position1 ex:a \"This is Position1\". \n" + " ex:position1 ex:a \"This is Position1\". \n" +
" ex:position1 ex:hasOrgName \"org xyz\" . \n" + " ex:position1 ex:hasOrgName \"org xyz\" . \n" +
" ex:position1 ex:b \"2343\" . "; " ex:position1 ex:b \"2343\" . ";
// prefixesN3 +
// " ex:bob ex:a \"Bob\". \n" +
// " ex:orgP ex:a \"orgP\". \n" +
// " ex:bob ex:hasPosition ex:position1 . \n" +
// " ex:position1 ex:a \"This is Position1\". \n" +
// " ex:position1 ex:hasOrgName \"org xyz\" . \n" +
// " ex:position1 ex:b \"2343\" . ";
String additions = String additions =
"@prefix ex: <http://example.com/> . \n" + "@prefix ex: <http://example.com/> . \n" +
"@prefix xsd: <" + XSD.getURI() + "> . \n " +
" ex:position1 ex:hasOrgName \"org xyz\" . "; " ex:position1 ex:hasOrgName \"org xyz\" . ";
String retractions = String retractions =
"@prefix ex: <http://example.com/> . \n" + "@prefix ex: <http://example.com/> . \n" +
" ex:orgP ex:hasPosition ex:position1 . "; "@prefix xsd: <" + XSD.getURI() + "> . \n " +
" ex:orgP ex:positionInOrganization ex:position1 . ";
Model sourceModel = (ModelFactory.createDefaultModel()).read(new StringReader(source), "", "N3"); Model sourceModel = (ModelFactory.createDefaultModel()).read(new StringReader(source), "", "N3");
Model additionsModel = (ModelFactory.createDefaultModel()).read(new StringReader(additions), "", "N3"); Model additionsModel = (ModelFactory.createDefaultModel()).read(new StringReader(additions), "", "N3");
@ -836,4 +810,50 @@ public class DependentResourceDeleteJenaTest {
Assert.assertTrue( same ); Assert.assertTrue( same );
} }
@org.junit.Test
public void testDeleteWithNonZeroInDegree2() {
/*
This tests deleting a position context node from the organization side.
Currently the required behavior is that the position context node not be
deleted when the object property statement is deleted from the organization side.
*/
String source =
prefixesN3 +
" ex:personHasPosition " + isDependentRelation +
" ex:bob ex:a \"Bob\". \n" +
" ex:orgP ex:a \"orgP\". \n" +
" ex:bob ex:personHasPosition ex:position1 . \n" +
" ex:orgP ex:positionInOrganization ex:position1 . \n" +
" ex:position1 ex:a \"This is Position1\". \n" +
" ex:position1 ex:b \"2343\" . ";
String expected =
prefixesN3 +
" ex:personHasPosition " + isDependentRelation +
" ex:bob ex:a \"Bob\". \n" +
" ex:orgP ex:a \"orgP\". \n" +
" ex:bob ex:personHasPosition ex:position1 . \n" +
" ex:position1 ex:a \"This is Position1\". \n" +
" ex:position1 ex:b \"2343\" . ";
String retractions =
"@prefix ex: <http://example.com/> . \n" +
"@prefix xsd: <" + XSD.getURI() + "> . \n " +
" ex:orgP ex:positionInOrganization ex:position1 . ";
Model sourceModel = (ModelFactory.createDefaultModel()).read(new StringReader(source), "", "N3");
Model additionsModel = ModelFactory.createDefaultModel(); //no additions
Model retractionsModel = (ModelFactory.createDefaultModel()).read(new StringReader(retractions), "", "N3");
Model depDeletes =
DependentResourceDeleteJena.getDependentResourceDeleteForChange(additionsModel, retractionsModel, sourceModel);
sourceModel.remove(depDeletes);
sourceModel.remove(retractionsModel);
sourceModel.add(additionsModel);
Model expectedModel = (ModelFactory.createDefaultModel()).read(new StringReader(expected), "", "N3");
boolean same = expectedModel.isIsomorphicWith( sourceModel );
if( ! same ) printModels( expectedModel, sourceModel);
Assert.assertTrue( same );
}
} }

View file

@ -13,7 +13,10 @@ import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.StmtIterator; import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.vocabulary.OWL;
import com.hp.hpl.jena.vocabulary.RDF; import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.RDFS;
import com.hp.hpl.jena.vocabulary.XSD;
import edu.cornell.mannlib.vitro.webapp.beans.DataProperty; import edu.cornell.mannlib.vitro.webapp.beans.DataProperty;
import edu.cornell.mannlib.vitro.webapp.beans.DataPropertyStatement; import edu.cornell.mannlib.vitro.webapp.beans.DataPropertyStatement;
@ -29,68 +32,53 @@ import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
public class JenaBaseDaoTest { public class JenaBaseDaoTest {
String isDependentRelation =
" <"+VitroVocabulary.PROPERTY_DEPENDENCYPROPERTYANNOT+"> \"true\"^^xsd:boolean .\n" ;
String nosePropIsDependentRel =
"<"+VitroVocabulary.PROPERTY_DEPENDENCYPROPERTYANNOT+"> rdf:type owl:AnnotationProperty .\n" +
" ex:hasNose " + isDependentRelation;
String prefixesN3 =
"@prefix vitro: <" + VitroVocabulary.vitroURI + "> . \n" +
"@prefix xsd: <" + XSD.getURI() + "> . \n " +
"@prefix rdf: <" + RDF.getURI() + "> . \n"+
"@prefix rdfs: <" + RDFS.getURI() + "> . \n"+
"@prefix owl: <" + OWL.getURI() + "> . \n" +
"@prefix ex: <http://example.com/> . \n" ;
@Test @Test
public void smartRemoveTestForIndivdiualDelete(){ public void smartRemoveTestForIndivdiualDelete(){
OntModel model = ModelFactory.createOntologyModel(); String n3 = prefixesN3 +
WebappDaoFactoryJena wdfj = new WebappDaoFactoryJena( model ); "ex:prop1 rdf:type owl:ObjectProperty ." +
"ex:prop1 rdfs:label \"Prop 1 Dependent Relation\" ." +
"ex:prop1 " + isDependentRelation;
/* Need to have the DEPENDENT_RESOURCE class in the model */ Model readInModel = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3");
VClass cls = new VClass(); OntModel ontModel = ModelFactory.createOntologyModel();
cls.setURI(VitroVocabulary.DEPENDENT_RESOURCE); ontModel.add(readInModel);
try { WebappDaoFactoryJena wdfj = new WebappDaoFactoryJena( ontModel);
wdfj.getVClassDao().insertNewVClass(cls);
} catch (InsertException e1) {
Assert.fail("could not create class for dependentResourc");
}
/* Need to have an Object Property */
ObjectProperty op = new ObjectProperty();
op.setURI("http://example.com/prop1");
try { try {
wdfj.getObjectPropertyDao().insertObjectProperty(op); ObjectProperty prop1 = wdfj.getObjectPropertyDao().getObjectPropertyByURI("http://example.com/prop1");
} catch (InsertException e1) { Assert.assertNotNull(prop1);
Assert.fail("Could not create object property.");
}
Individual ind = new IndividualImpl(); Individual ind = new IndividualImpl();
ind.setURI("http://example.com/bob"); ind.setURI("http://example.com/bob");
ind.setName("Smith, Bob"); ind.setName("Smith, Bob");
try {
wdfj.getIndividualDao().insertNewIndividual(ind); wdfj.getIndividualDao().insertNewIndividual(ind);
} catch (InsertException e) {
Assert.fail("Could not create new Individual Smith, Bob");
}
Individual indxyz = new IndividualImpl(); Individual indxyz = new IndividualImpl();
indxyz.setURI("http://example.com/depResXYZ"); indxyz.setURI("http://example.com/depResXYZ");
indxyz.setName("depResXYZ"); indxyz.setName("depResXYZ");
indxyz.setVClassURI(VitroVocabulary.DEPENDENT_RESOURCE);
try {
wdfj.getIndividualDao().insertNewIndividual(indxyz); wdfj.getIndividualDao().insertNewIndividual(indxyz);
} catch (InsertException e) {
Assert.fail("Could not create new Individual depResXYZ");
}
StmtIterator it = model.listStatements(model.createResource("http://example.com/depResXYZ"),
RDF.type, model.createResource(VitroVocabulary.DEPENDENT_RESOURCE));
Assert.assertTrue("depResXYZ did not get rdf:type vitro:dependentResource" ,
it != null && it.nextStatement() != null);
Individual indAbc = new IndividualImpl(); Individual indAbc = new IndividualImpl();
indAbc.setURI("http://example.com/depResNested"); indAbc.setURI("http://example.com/depResNested");
indAbc.setName("depResNested"); indAbc.setName("depResNested");
indAbc.setVClassURI(VitroVocabulary.DEPENDENT_RESOURCE);
try {
wdfj.getIndividualDao().insertNewIndividual(indAbc); wdfj.getIndividualDao().insertNewIndividual(indAbc);
} catch (InsertException e) {
Assert.fail("Could not create new Individual depResNested");
}
it = model.listStatements(model.createResource("http://example.com/depResNested"),
RDF.type, model.createResource(VitroVocabulary.DEPENDENT_RESOURCE));
Assert.assertTrue("depResNested did not get rdf:type vitro:dependentResource" ,
it != null && it.nextStatement() != null);
ObjectPropertyStatement ops = new ObjectPropertyStatementImpl(); ObjectPropertyStatement ops = new ObjectPropertyStatementImpl();
ops.setSubjectURI("http://example.com/bob"); ops.setSubjectURI("http://example.com/bob");
@ -104,65 +92,43 @@ public class JenaBaseDaoTest {
ops.setObjectURI("http://example.com/depResNested"); ops.setObjectURI("http://example.com/depResNested");
wdfj.getObjectPropertyStatementDao().insertNewObjectPropertyStatement(ops); wdfj.getObjectPropertyStatementDao().insertNewObjectPropertyStatement(ops);
String expected = "<rdf:RDF\n"+
" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n"+
" xmlns:j.0=\"http://vitro.mannlib.cornell.edu/ns/vitro/0.7#\"\n"+
" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema#\"\n"+
" xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\"\n"+
" xmlns:owl=\"http://www.w3.org/2002/07/owl#\"\n"+
" xmlns:j.1=\"http://example.com/\" > \n"+
" <rdf:Description rdf:about=\"http://example.com/bob\">\n"+
" <j.0:modTime rdf:datatype=\"http://www.w3.org/2001/XMLSchema#dateTime\">2010-01-25T15:27:54</j.0:modTime>\n"+
" <rdfs:label xml:lang=\"en-US\">Smith, Bob</rdfs:label>\n"+
" <rdf:type rdf:resource=\"http://www.w3.org/2002/07/owl#Thing\"/>\n"+
" </rdf:Description>\n"+
" <rdf:Description rdf:about=\"http://vitro.mannlib.cornell.edu/ns/vitro/0.7#DependentResource\">\n"+
" <j.0:displayRankAnnot rdf:datatype=\"http://www.w3.org/2001/XMLSchema#int\">-1</j.0:displayRankAnnot>\n"+
" <j.0:displayLimitAnnot rdf:datatype=\"http://www.w3.org/2001/XMLSchema#int\">-1</j.0:displayLimitAnnot>\n"+
" <rdf:type rdf:resource=\"http://www.w3.org/2002/07/owl#Class\"/>\n"+
" </rdf:Description>\n"+
" <rdf:Description rdf:about=\"http://example.com/prop1\">\n"+
" <j.0:selectFromExistingAnnot rdf:datatype=\"http://www.w3.org/2001/XMLSchema#boolean\">true</j.0:selectFromExistingAnnot>\n"+
" <j.0:displayLimitAnnot rdf:datatype=\"http://www.w3.org/2001/XMLSchema#int\">5</j.0:displayLimitAnnot>\n"+
" <rdf:type rdf:resource=\"http://www.w3.org/2002/07/owl#ObjectProperty\"/>\n"+
" </rdf:Description>\n"+
"</rdf:RDF>";
wdfj.getIndividualDao().deleteIndividual("http://example.com/depResXYZ"); wdfj.getIndividualDao().deleteIndividual("http://example.com/depResXYZ");
String expected =
"@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . "+
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> ."+
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> ."+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . "+
"<http://example.com/bob> a owl:Thing ; " +
" rdfs:label \"Smith, Bob\"@en-US . "+
"<http://example.com/prop1> " +
" a owl:ObjectProperty ; " +
" rdfs:label \"Prop 1 Dependent Relation\" ; " +
" <http://vitro.mannlib.cornell.edu/ns/vitro/0.7#dependencyPropertyAnnot> \"true\"^^xsd:boolean ." ;
Model expectedModel = (ModelFactory.createOntologyModel()).read(new StringReader(expected), "", "RDF/XML"); Model expectedModel = (ModelFactory.createOntologyModel()).read(new StringReader(expected), "", "N3");
//modtime times make it difficult to compare graphs //modtime times make it difficult to compare graphs
wipeOutModTime(expectedModel); wipeOutModTime(expectedModel);
wipeOutModTime(model); wipeOutModTime(ontModel);
Assert.assertTrue( model.isIsomorphicWith(expectedModel)); Assert.assertTrue( ontModel.isIsomorphicWith(expectedModel) );
} catch (InsertException e) {
Assert.fail(e.getMessage());
}
} }
@Test @Test
public void smartRemoveTestForObjPropStmtDelete(){ public void smartRemoveTestForObjPropStmtDelete(){
String n3 = prefixesN3 +
"ex:prop1 rdf:type owl:ObjectProperty ." +
"ex:prop1 rdfs:label \"Prop 1 Dependent Relation\" ." +
"ex:prop1 " + isDependentRelation;
Model readInModel = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3");
OntModel model = ModelFactory.createOntologyModel(); OntModel model = ModelFactory.createOntologyModel();
WebappDaoFactoryJena wdfj = new WebappDaoFactoryJena( model ); model.add(readInModel);
WebappDaoFactoryJena wdfj = new WebappDaoFactoryJena( model);
/* Need to have the DEPENDENT_RESOURCE class in the model */
VClass cls = new VClass();
cls.setURI(VitroVocabulary.DEPENDENT_RESOURCE);
try {
wdfj.getVClassDao().insertNewVClass(cls);
} catch (InsertException e1) {
Assert.fail("could not create class for dependentResourc");
}
/* Need to have an Object Property */
ObjectProperty op = new ObjectProperty();
op.setURI("http://example.com/prop1");
try {
wdfj.getObjectPropertyDao().insertObjectProperty(op);
} catch (InsertException e1) {
Assert.fail("Could not create object property.");
}
Individual ind = new IndividualImpl(); Individual ind = new IndividualImpl();
ind.setURI("http://example.com/bob"); ind.setURI("http://example.com/bob");
@ -176,31 +142,20 @@ public class JenaBaseDaoTest {
Individual indxyz = new IndividualImpl(); Individual indxyz = new IndividualImpl();
indxyz.setURI("http://example.com/depResXYZ"); indxyz.setURI("http://example.com/depResXYZ");
indxyz.setName("depResXYZ"); indxyz.setName("depResXYZ");
indxyz.setVClassURI(VitroVocabulary.DEPENDENT_RESOURCE);
try { try {
wdfj.getIndividualDao().insertNewIndividual(indxyz); wdfj.getIndividualDao().insertNewIndividual(indxyz);
} catch (InsertException e) { } catch (InsertException e) {
Assert.fail("Could not create new Individual depResXYZ"); Assert.fail("Could not create new Individual depResXYZ");
} }
StmtIterator it = model.listStatements(model.createResource("http://example.com/depResXYZ"),
RDF.type, model.createResource(VitroVocabulary.DEPENDENT_RESOURCE));
Assert.assertTrue("depResXYZ did not get rdf:type vitro:dependentResource" ,
it != null && it.nextStatement() != null);
Individual indAbc = new IndividualImpl(); Individual indAbc = new IndividualImpl();
indAbc.setURI("http://example.com/depResNested"); indAbc.setURI("http://example.com/depResNested");
indAbc.setName("depResNested"); indAbc.setName("depResNested");
indAbc.setVClassURI(VitroVocabulary.DEPENDENT_RESOURCE);
try { try {
wdfj.getIndividualDao().insertNewIndividual(indAbc); wdfj.getIndividualDao().insertNewIndividual(indAbc);
} catch (InsertException e) { } catch (InsertException e) {
Assert.fail("Could not create new Individual depResNested"); Assert.fail("Could not create new Individual depResNested");
} }
it = model.listStatements(model.createResource("http://example.com/depResNested"),
RDF.type, model.createResource(VitroVocabulary.DEPENDENT_RESOURCE));
Assert.assertTrue("depResNested did not get rdf:type vitro:dependentResource" ,
it != null && it.nextStatement() != null);
ObjectPropertyStatement ops = new ObjectPropertyStatementImpl(); ObjectPropertyStatement ops = new ObjectPropertyStatementImpl();
ops.setSubjectURI("http://example.com/bob"); ops.setSubjectURI("http://example.com/bob");
@ -214,38 +169,25 @@ public class JenaBaseDaoTest {
ops.setObjectURI("http://example.com/depResNested"); ops.setObjectURI("http://example.com/depResNested");
wdfj.getObjectPropertyStatementDao().insertNewObjectPropertyStatement(ops); wdfj.getObjectPropertyStatementDao().insertNewObjectPropertyStatement(ops);
String expected = "<rdf:RDF\n"+
" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n"+
" xmlns:j.0=\"http://vitro.mannlib.cornell.edu/ns/vitro/0.7#\"\n"+
" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema#\"\n"+
" xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\"\n"+
" xmlns:owl=\"http://www.w3.org/2002/07/owl#\"\n"+
" xmlns:j.1=\"http://example.com/\" > \n"+
" <rdf:Description rdf:about=\"http://example.com/bob\">\n"+
" <j.0:modTime rdf:datatype=\"http://www.w3.org/2001/XMLSchema#dateTime\">2010-01-25T15:27:54</j.0:modTime>\n"+
" <rdfs:label xml:lang=\"en-US\">Smith, Bob</rdfs:label>\n"+
" <rdf:type rdf:resource=\"http://www.w3.org/2002/07/owl#Thing\"/>\n"+
" </rdf:Description>\n"+
" <rdf:Description rdf:about=\"http://vitro.mannlib.cornell.edu/ns/vitro/0.7#DependentResource\">\n"+
" <j.0:displayRankAnnot rdf:datatype=\"http://www.w3.org/2001/XMLSchema#int\">-1</j.0:displayRankAnnot>\n"+
" <j.0:displayLimitAnnot rdf:datatype=\"http://www.w3.org/2001/XMLSchema#int\">-1</j.0:displayLimitAnnot>\n"+
" <rdf:type rdf:resource=\"http://www.w3.org/2002/07/owl#Class\"/>\n"+
" </rdf:Description>\n"+
" <rdf:Description rdf:about=\"http://example.com/prop1\">\n"+
" <j.0:selectFromExistingAnnot rdf:datatype=\"http://www.w3.org/2001/XMLSchema#boolean\">true</j.0:selectFromExistingAnnot>\n"+
" <j.0:displayLimitAnnot rdf:datatype=\"http://www.w3.org/2001/XMLSchema#int\">5</j.0:displayLimitAnnot>\n"+
" <rdf:type rdf:resource=\"http://www.w3.org/2002/07/owl#ObjectProperty\"/>\n"+
" </rdf:Description>\n"+
"</rdf:RDF>";
ops = new ObjectPropertyStatementImpl(); ops = new ObjectPropertyStatementImpl();
ops.setSubjectURI("http://example.com/bob"); ops.setSubjectURI("http://example.com/bob");
ops.setPropertyURI("http://example.com/prop1"); ops.setPropertyURI("http://example.com/prop1");
ops.setObjectURI("http://example.com/depResXYZ"); ops.setObjectURI("http://example.com/depResXYZ");
wdfj.getObjectPropertyStatementDao().deleteObjectPropertyStatement(ops); wdfj.getObjectPropertyStatementDao().deleteObjectPropertyStatement(ops);
Model expectedModel = (ModelFactory.createOntologyModel()).read(new StringReader(expected), "", "RDF/XML"); String expected =
"@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . "+
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> ."+
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> ."+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . "+
"<http://example.com/bob> a owl:Thing ; " +
" rdfs:label \"Smith, Bob\"@en-US . "+
"<http://example.com/prop1> " +
" a owl:ObjectProperty ; " +
" rdfs:label \"Prop 1 Dependent Relation\" ; " +
" <http://vitro.mannlib.cornell.edu/ns/vitro/0.7#dependencyPropertyAnnot> \"true\"^^xsd:boolean ." ;
Model expectedModel = (ModelFactory.createOntologyModel()).read(new StringReader(expected), "", "N3");
//modtime times make it difficult to compare graphs //modtime times make it difficult to compare graphs
wipeOutModTime(expectedModel); wipeOutModTime(expectedModel);
@ -254,67 +196,38 @@ public class JenaBaseDaoTest {
Assert.assertTrue( model.isIsomorphicWith(expectedModel)); Assert.assertTrue( model.isIsomorphicWith(expectedModel));
} }
@Test @Test
public void smartRemoveTestForObjPropDelete(){ public void smartRemoveTestForObjPropDelete(){
String n3 = prefixesN3 +
"ex:prop1 rdf:type owl:ObjectProperty ." +
"ex:prop1 rdfs:label \"Prop 1 Dependent Relation\" ." +
"ex:prop1 " + isDependentRelation;
OntModel model = ModelFactory.createOntologyModel(); Model readInModel = (ModelFactory.createDefaultModel()).read(new StringReader(n3), "", "N3");
WebappDaoFactoryJena wdfj = new WebappDaoFactoryJena( model ); OntModel ontModel = ModelFactory.createOntologyModel();
ontModel.add(readInModel);
WebappDaoFactoryJena wdfj = new WebappDaoFactoryJena( ontModel);
/* Need to have the DEPENDENT_RESOURCE class in the model */
VClass cls = new VClass();
cls.setURI(VitroVocabulary.DEPENDENT_RESOURCE);
try { try {
wdfj.getVClassDao().insertNewVClass(cls); ObjectProperty prop1 = wdfj.getObjectPropertyDao().getObjectPropertyByURI("http://example.com/prop1");
} catch (InsertException e1) { Assert.assertNotNull(prop1);
Assert.fail("could not create class for dependentResourc");
}
/* Need to have an Object Property */
ObjectProperty op = new ObjectProperty();
op.setURI("http://example.com/prop1");
try {
wdfj.getObjectPropertyDao().insertObjectProperty(op);
} catch (InsertException e1) {
Assert.fail("Could not create object property.");
}
Individual ind = new IndividualImpl(); Individual ind = new IndividualImpl();
ind.setURI("http://example.com/bob"); ind.setURI("http://example.com/bob");
ind.setName("Smith, Bob"); ind.setName("Smith, Bob");
try {
wdfj.getIndividualDao().insertNewIndividual(ind); wdfj.getIndividualDao().insertNewIndividual(ind);
} catch (InsertException e) {
Assert.fail("Could not create new Individual Smith, Bob");
}
Individual indxyz = new IndividualImpl(); Individual indxyz = new IndividualImpl();
indxyz.setURI("http://example.com/depResXYZ"); indxyz.setURI("http://example.com/depResXYZ");
indxyz.setName("depResXYZ"); indxyz.setName("depResXYZ");
indxyz.setVClassURI(VitroVocabulary.DEPENDENT_RESOURCE);
try {
wdfj.getIndividualDao().insertNewIndividual(indxyz); wdfj.getIndividualDao().insertNewIndividual(indxyz);
} catch (InsertException e) {
Assert.fail("Could not create new Individual depResXYZ");
}
StmtIterator it = model.listStatements(model.createResource("http://example.com/depResXYZ"),
RDF.type, model.createResource(VitroVocabulary.DEPENDENT_RESOURCE));
Assert.assertTrue("depResXYZ did not get rdf:type vitro:dependentResource" ,
it != null && it.nextStatement() != null);
Individual indAbc = new IndividualImpl(); Individual indAbc = new IndividualImpl();
indAbc.setURI("http://example.com/depResNested"); indAbc.setURI("http://example.com/depResNested");
indAbc.setName("depResNested"); indAbc.setName("depResNested");
indAbc.setVClassURI(VitroVocabulary.DEPENDENT_RESOURCE);
try {
wdfj.getIndividualDao().insertNewIndividual(indAbc); wdfj.getIndividualDao().insertNewIndividual(indAbc);
} catch (InsertException e) {
Assert.fail("Could not create new Individual depResNested");
}
it = model.listStatements(model.createResource("http://example.com/depResNested"),
RDF.type, model.createResource(VitroVocabulary.DEPENDENT_RESOURCE));
Assert.assertTrue("depResNested did not get rdf:type vitro:dependentResource" ,
it != null && it.nextStatement() != null);
ObjectPropertyStatement ops = new ObjectPropertyStatementImpl(); ObjectPropertyStatement ops = new ObjectPropertyStatementImpl();
ops.setSubjectURI("http://example.com/bob"); ops.setSubjectURI("http://example.com/bob");
@ -328,33 +241,129 @@ public class JenaBaseDaoTest {
ops.setObjectURI("http://example.com/depResNested"); ops.setObjectURI("http://example.com/depResNested");
wdfj.getObjectPropertyStatementDao().insertNewObjectPropertyStatement(ops); wdfj.getObjectPropertyStatementDao().insertNewObjectPropertyStatement(ops);
String expected = "<rdf:RDF\n"+ wdfj.getObjectPropertyDao().deleteObjectProperty(prop1);
" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n"+
" xmlns:j.0=\"http://vitro.mannlib.cornell.edu/ns/vitro/0.7#\"\n"+
" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema#\"\n"+
" xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\"\n"+
" xmlns:owl=\"http://www.w3.org/2002/07/owl#\"\n"+
" xmlns:j.1=\"http://example.com/\" > \n"+
" <rdf:Description rdf:about=\"http://example.com/bob\">\n"+
" <rdfs:label xml:lang=\"en-US\">Smith, Bob</rdfs:label>\n"+
" <rdf:type rdf:resource=\"http://www.w3.org/2002/07/owl#Thing\"/>\n"+
" </rdf:Description>\n"+
" <rdf:Description rdf:about=\"http://vitro.mannlib.cornell.edu/ns/vitro/0.7#DependentResource\">\n"+
" <j.0:displayRankAnnot rdf:datatype=\"http://www.w3.org/2001/XMLSchema#int\">-1</j.0:displayRankAnnot>\n"+
" <j.0:displayLimitAnnot rdf:datatype=\"http://www.w3.org/2001/XMLSchema#int\">-1</j.0:displayLimitAnnot>\n"+
" <rdf:type rdf:resource=\"http://www.w3.org/2002/07/owl#Class\"/>\n"+
" </rdf:Description>\n"+
"</rdf:RDF>";
wdfj.getObjectPropertyDao().deleteObjectProperty(op); String expected =
"@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . "+
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> ."+
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> ."+
"@prefix owl: <http://www.w3.org/2002/07/owl#> . "+
"<http://example.com/bob> a owl:Thing ; " +
" rdfs:label \"Smith, Bob\"@en-US . " ;
Model expectedModel = (ModelFactory.createOntologyModel()).read(new StringReader(expected), "", "RDF/XML"); Model expectedModel = (ModelFactory.createOntologyModel()).read(new StringReader(expected), "", "N3");
//modtime times make it difficult to compare graphs //modtime times make it difficult to compare graphs
wipeOutModTime(expectedModel); wipeOutModTime(expectedModel);
wipeOutModTime(model); wipeOutModTime(ontModel);
Assert.assertTrue( model.isIsomorphicWith(expectedModel));
Assert.assertTrue( ontModel.isIsomorphicWith(expectedModel) );
} catch (InsertException e) {
Assert.fail(e.getMessage());
} }
}
// @Test
// public void smartRemoveTestForObjPropDelete(){
//
// OntModel model = ModelFactory.createOntologyModel();
// WebappDaoFactoryJena wdfj = new WebappDaoFactoryJena( model );
//
// /* Need to have the DEPENDENT_RESOURCE class in the model */
// VClass cls = new VClass();
// //cls.setURI(VitroVocabulary.DEPENDENT_RESOURCE);
// try {
// wdfj.getVClassDao().insertNewVClass(cls);
// } catch (InsertException e1) {
// Assert.fail("could not create class for dependentResourc");
// }
//
// /* Need to have an Object Property */
// ObjectProperty op = new ObjectProperty();
// op.setURI("http://example.com/prop1");
// try {
// wdfj.getObjectPropertyDao().insertObjectProperty(op);
// } catch (InsertException e1) {
// Assert.fail("Could not create object property.");
// }
//
// Individual ind = new IndividualImpl();
// ind.setURI("http://example.com/bob");
// ind.setName("Smith, Bob");
// try {
// wdfj.getIndividualDao().insertNewIndividual(ind);
// } catch (InsertException e) {
// Assert.fail("Could not create new Individual Smith, Bob");
// }
//
// Individual indxyz = new IndividualImpl();
// indxyz.setURI("http://example.com/depResXYZ");
// indxyz.setName("depResXYZ");
// //indxyz.setVClassURI(VitroVocabulary.DEPENDENT_RESOURCE);
// try {
// wdfj.getIndividualDao().insertNewIndividual(indxyz);
// } catch (InsertException e) {
// Assert.fail("Could not create new Individual depResXYZ");
// }
//// StmtIterator it = model.listStatements(model.createResource("http://example.com/depResXYZ"),
//// RDF.type, model.createResource(VitroVocabulary.DEPENDENT_RESOURCE));
//// Assert.assertTrue("depResXYZ did not get rdf:type vitro:dependentResource" ,
//// it != null && it.nextStatement() != null);
//
// Individual indAbc = new IndividualImpl();
// indAbc.setURI("http://example.com/depResNested");
// indAbc.setName("depResNested");
//// indAbc.setVClassURI(VitroVocabulary.DEPENDENT_RESOURCE);
// try {
// wdfj.getIndividualDao().insertNewIndividual(indAbc);
// } catch (InsertException e) {
// Assert.fail("Could not create new Individual depResNested");
// }
//// it = model.listStatements(model.createResource("http://example.com/depResNested"),
//// RDF.type, model.createResource(VitroVocabulary.DEPENDENT_RESOURCE));
//// Assert.assertTrue("depResNested did not get rdf:type vitro:dependentResource" ,
//// it != null && it.nextStatement() != null);
//
//
// ObjectPropertyStatement ops = new ObjectPropertyStatementImpl();
// ops.setSubjectURI("http://example.com/bob");
// ops.setPropertyURI("http://example.com/prop1");
// ops.setObjectURI("http://example.com/depResXYZ");
// wdfj.getObjectPropertyStatementDao().insertNewObjectPropertyStatement(ops);
//
// ops = new ObjectPropertyStatementImpl();
// ops.setSubjectURI("http://example.com/depResXYZ");
// ops.setPropertyURI("http://example.com/prop1");
// ops.setObjectURI("http://example.com/depResNested");
// wdfj.getObjectPropertyStatementDao().insertNewObjectPropertyStatement(ops);
//
// String expected = "<rdf:RDF\n"+
// " xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n"+
// " xmlns:j.0=\"http://vitro.mannlib.cornell.edu/ns/vitro/0.7#\"\n"+
// " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema#\"\n"+
// " xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\"\n"+
// " xmlns:owl=\"http://www.w3.org/2002/07/owl#\"\n"+
// " xmlns:j.1=\"http://example.com/\" > \n"+
// " <rdf:Description rdf:about=\"http://example.com/bob\">\n"+
// " <rdfs:label xml:lang=\"en-US\">Smith, Bob</rdfs:label>\n"+
// " <rdf:type rdf:resource=\"http://www.w3.org/2002/07/owl#Thing\"/>\n"+
// " </rdf:Description>\n"+
// " <rdf:Description rdf:about=\"http://vitro.mannlib.cornell.edu/ns/vitro/0.7#DependentResource\">\n"+
// " <j.0:displayRankAnnot rdf:datatype=\"http://www.w3.org/2001/XMLSchema#int\">-1</j.0:displayRankAnnot>\n"+
// " <j.0:displayLimitAnnot rdf:datatype=\"http://www.w3.org/2001/XMLSchema#int\">-1</j.0:displayLimitAnnot>\n"+
// " <rdf:type rdf:resource=\"http://www.w3.org/2002/07/owl#Class\"/>\n"+
// " </rdf:Description>\n"+
// "</rdf:RDF>";
//
// wdfj.getObjectPropertyDao().deleteObjectProperty(op);
//
// Model expectedModel = (ModelFactory.createOntologyModel()).read(new StringReader(expected), "", "RDF/XML");
//
// //modtime times make it difficult to compare graphs
// wipeOutModTime(expectedModel);
// wipeOutModTime(model);
// Assert.assertTrue( model.isIsomorphicWith(expectedModel));
// }
void printModels(Model expected, Model result){ void printModels(Model expected, Model result){
System.out.println("Expected:"); System.out.println("Expected:");
expected.write(System.out); expected.write(System.out);