merge changes from trunk to rdf api branch and fix issue with listeners notifying before triples are actually added/removed
This commit is contained in:
parent
94a34e274b
commit
6e8a4dfff1
27 changed files with 1429 additions and 1316 deletions
|
@ -1,3 +1,5 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.dao.jena;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.dao.jena;
|
||||
|
||||
import java.util.Set;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.dao.jena;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.dao.jena;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.dao.jena;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.dao.jena;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.dao.jena;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.dao.jena;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
@ -30,7 +32,7 @@ import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFServiceException;
|
|||
|
||||
public class RDFServiceGraphBulkUpdater extends SimpleBulkUpdateHandler {
|
||||
|
||||
private static final Log log = LogFactory.getLog(SparqlGraphBulkUpdater.class);
|
||||
private static final Log log = LogFactory.getLog(RDFServiceGraphBulkUpdater.class);
|
||||
private RDFServiceGraph graph;
|
||||
|
||||
public RDFServiceGraphBulkUpdater(RDFServiceGraph graph) {
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.dao.jena;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.dao.jena;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.dao.jena;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.dao.jena;
|
||||
|
||||
import java.io.StringWriter;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.dao.jena;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
|
|
@ -35,6 +35,7 @@ import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties;
|
|||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactoryConfig;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelContext;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelector;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.RDFServiceDataset;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.SparqlDataset;
|
||||
|
@ -90,7 +91,8 @@ public class WebappDaoFactorySDBPrep implements Filter {
|
|||
}
|
||||
}
|
||||
|
||||
OntModelSelector oms = (OntModelSelector) _ctx.getAttribute("unionOntModelSelector");
|
||||
OntModelSelector oms = ModelContext.getUnionOntModelSelector(_ctx);
|
||||
OntModelSelector baseOms = ModelContext.getBaseOntModelSelector(_ctx);
|
||||
String defaultNamespace = (String) _ctx.getAttribute("defaultNamespace");
|
||||
WebappDaoFactory wadf = null;
|
||||
VitroRequest vreq = new VitroRequest((HttpServletRequest) request);
|
||||
|
@ -112,7 +114,7 @@ public class WebappDaoFactorySDBPrep implements Filter {
|
|||
Dataset dataset = new RDFServiceDataset(rdfService);
|
||||
wadf = new WebappDaoFactorySDB(rdfService, oms, config);
|
||||
WebappDaoFactory assertions = new WebappDaoFactorySDB(
|
||||
rdfService, oms, config, SDBDatasetMode.ASSERTIONS_ONLY);
|
||||
rdfService, baseOms, config, SDBDatasetMode.ASSERTIONS_ONLY);
|
||||
vreq.setWebappDaoFactory(wadf);
|
||||
vreq.setAssertionsWebappDaoFactory(assertions);
|
||||
vreq.setFullWebappDaoFactory(wadf);
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.rdfservice;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.rdfservice.impl;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.rdfservice.ChangeListener;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.rdfservice.impl;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
|
|
@ -0,0 +1,217 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.rdfservice.impl.sdb;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import com.hp.hpl.jena.graph.BulkUpdateHandler;
|
||||
import com.hp.hpl.jena.graph.Capabilities;
|
||||
import com.hp.hpl.jena.graph.Graph;
|
||||
import com.hp.hpl.jena.graph.GraphEventManager;
|
||||
import com.hp.hpl.jena.graph.GraphStatisticsHandler;
|
||||
import com.hp.hpl.jena.graph.Node;
|
||||
import com.hp.hpl.jena.graph.Reifier;
|
||||
import com.hp.hpl.jena.graph.TransactionHandler;
|
||||
import com.hp.hpl.jena.graph.Triple;
|
||||
import com.hp.hpl.jena.graph.TripleMatch;
|
||||
import com.hp.hpl.jena.graph.impl.GraphWithPerform;
|
||||
import com.hp.hpl.jena.graph.impl.SimpleBulkUpdateHandler;
|
||||
import com.hp.hpl.jena.graph.impl.SimpleEventManager;
|
||||
import com.hp.hpl.jena.graph.query.QueryHandler;
|
||||
import com.hp.hpl.jena.graph.query.SimpleQueryHandler;
|
||||
import com.hp.hpl.jena.shared.AddDeniedException;
|
||||
import com.hp.hpl.jena.shared.DeleteDeniedException;
|
||||
import com.hp.hpl.jena.shared.PrefixMapping;
|
||||
import com.hp.hpl.jena.shared.impl.PrefixMappingImpl;
|
||||
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
|
||||
import com.hp.hpl.jena.util.iterator.WrappedIterator;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.EmptyReifier;
|
||||
import edu.cornell.mannlib.vitro.webapp.rdfservice.ModelChange;
|
||||
|
||||
public class ListeningGraph implements GraphWithPerform {
|
||||
|
||||
private static final Log log = LogFactory.getLog(ListeningGraph.class);
|
||||
|
||||
private RDFServiceSDB rdfServiceSDB;
|
||||
private String graphURI;
|
||||
|
||||
private BulkUpdateHandler bulkUpdateHandler;
|
||||
private GraphEventManager eventManager;
|
||||
private PrefixMapping prefixMapping = new PrefixMappingImpl();
|
||||
private Reifier reifier = new EmptyReifier(this);
|
||||
private QueryHandler queryHandler;
|
||||
|
||||
public ListeningGraph(String graphURI, RDFServiceSDB rdfServiceSDB) {
|
||||
this.graphURI = graphURI;
|
||||
this.rdfServiceSDB = rdfServiceSDB;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(Triple triple) throws AddDeniedException {
|
||||
performAdd(triple);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performAdd(Triple triple) throws AddDeniedException {
|
||||
this.rdfServiceSDB.notifyListeners(triple, ModelChange.Operation.ADD, graphURI);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Triple triple) throws DeleteDeniedException {
|
||||
performDelete(triple);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performDelete(Triple triple) throws DeleteDeniedException {
|
||||
this.rdfServiceSDB.notifyListeners(triple, ModelChange.Operation.REMOVE, graphURI);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Triple arg0) {
|
||||
return contains(arg0.getSubject(), arg0.getPredicate(), arg0.getObject());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Node subject, Node predicate, Node object) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean dependsOn(Graph arg0) {
|
||||
return false; // who knows?
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExtendedIterator<Triple> find(TripleMatch arg0) {
|
||||
Triple t = arg0.asTriple();
|
||||
return find(t.getSubject(), t.getPredicate(), t.getObject());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExtendedIterator<Triple> find(Node subject, Node predicate, Node object) {
|
||||
List<Triple> triplist = new ArrayList<Triple>();
|
||||
return WrappedIterator.create(triplist.iterator());
|
||||
}
|
||||
|
||||
@Override
|
||||
public BulkUpdateHandler getBulkUpdateHandler() {
|
||||
if (this.bulkUpdateHandler == null) {
|
||||
this.bulkUpdateHandler = new SimpleBulkUpdateHandler(this);
|
||||
}
|
||||
return this.bulkUpdateHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Capabilities getCapabilities() {
|
||||
return capabilities;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GraphEventManager getEventManager() {
|
||||
if (eventManager == null) {
|
||||
eventManager = new SimpleEventManager(this);
|
||||
}
|
||||
return eventManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PrefixMapping getPrefixMapping() {
|
||||
return prefixMapping;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Reifier getReifier() {
|
||||
return reifier;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GraphStatisticsHandler getStatisticsHandler() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TransactionHandler getTransactionHandler() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isClosed() {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return (size() == 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isIsomorphicWith(Graph arg0) {
|
||||
throw new UnsupportedOperationException("isIsomorphicWith() not supported " +
|
||||
"by SPARQL graphs");
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryHandler queryHandler() {
|
||||
if (queryHandler == null) {
|
||||
queryHandler = new SimpleQueryHandler(this);
|
||||
}
|
||||
return queryHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
int size = find(null, null, null).toList().size();
|
||||
return size;
|
||||
}
|
||||
|
||||
private final static Capabilities capabilities = new Capabilities() {
|
||||
|
||||
public boolean addAllowed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean addAllowed(boolean everyTriple) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean canBeEmpty() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean deleteAllowed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean deleteAllowed(boolean everyTriple) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean findContractSafe() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean handlesLiteralTyping() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean iteratorRemoveAllowed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean sizeAccurate() {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
|
@ -1,3 +1,5 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.rdfservice.impl.sdb;
|
||||
|
||||
import org.apache.commons.dbcp.BasicDataSource;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.rdfservice.impl.sdb;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
@ -35,6 +37,7 @@ import com.hp.hpl.jena.sdb.Store;
|
|||
import com.hp.hpl.jena.sdb.StoreDesc;
|
||||
import com.hp.hpl.jena.sdb.sql.SDBConnection;
|
||||
import com.hp.hpl.jena.shared.Lock;
|
||||
import com.hp.hpl.jena.vocabulary.OWL;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.DatasetWrapper;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.SparqlGraph;
|
||||
|
@ -83,8 +86,6 @@ public class RDFServiceSDB extends RDFServiceImpl implements RDFService {
|
|||
return false;
|
||||
}
|
||||
|
||||
Iterator<ModelChange> csIt = changeSet.getModelChanges().iterator();
|
||||
|
||||
SDBConnection conn = null;
|
||||
try {
|
||||
conn = new SDBConnection(bds.getConnection());
|
||||
|
@ -97,46 +98,46 @@ public class RDFServiceSDB extends RDFServiceImpl implements RDFService {
|
|||
boolean transaction = conn.getTransactionHandler().transactionsSupported();
|
||||
|
||||
try {
|
||||
|
||||
if (transaction) {
|
||||
conn.getTransactionHandler().begin();
|
||||
} else {
|
||||
for (Object o : changeSet.getPreChangeEvents()) {
|
||||
this.notifyListenersOfEvent(o);
|
||||
}
|
||||
}
|
||||
|
||||
for (Object o : changeSet.getPreChangeEvents()) {
|
||||
this.notifyListenersOfEvent(o);
|
||||
}
|
||||
|
||||
Iterator<ModelChange> csIt = changeSet.getModelChanges().iterator();
|
||||
while (csIt.hasNext()) {
|
||||
ModelChange modelChange = csIt.next();
|
||||
modelChange.getSerializedModel().mark(Integer.MAX_VALUE);
|
||||
dataset.getLock().enterCriticalSection(Lock.WRITE);
|
||||
try {
|
||||
Model model = dataset.getNamedModel(modelChange.getGraphURI());
|
||||
model.enterCriticalSection(Lock.WRITE);
|
||||
try {
|
||||
model.register(new ModelListener(modelChange.getGraphURI(), this));
|
||||
if (modelChange.getOperation() == ModelChange.Operation.ADD) {
|
||||
model.read(modelChange.getSerializedModel(), null,
|
||||
getSerializationFormatString(modelChange.getSerializationFormat()));
|
||||
} else if (modelChange.getOperation() == ModelChange.Operation.REMOVE) {
|
||||
model.remove(parseModel(modelChange));
|
||||
removeBlankNodesWithSparqlUpdate(dataset, model, modelChange.getGraphURI());
|
||||
} else {
|
||||
log.error("unrecognized operation type");
|
||||
}
|
||||
} finally {
|
||||
model.leaveCriticalSection();
|
||||
}
|
||||
operateOnModel(model, modelChange, dataset);
|
||||
} finally {
|
||||
dataset.getLock().leaveCriticalSection();
|
||||
}
|
||||
}
|
||||
|
||||
if (transaction) {
|
||||
for (Object o : changeSet.getPreChangeEvents()) {
|
||||
this.notifyListenersOfEvent(o);
|
||||
}
|
||||
conn.getTransactionHandler().commit();
|
||||
}
|
||||
|
||||
// notify listeners of triple changes
|
||||
csIt = changeSet.getModelChanges().iterator();
|
||||
while (csIt.hasNext()) {
|
||||
ModelChange modelChange = csIt.next();
|
||||
modelChange.getSerializedModel().reset();
|
||||
Model model = ModelFactory.createModelForGraph(
|
||||
new ListeningGraph(modelChange.getGraphURI(), this));
|
||||
operateOnModel(model, modelChange, null);
|
||||
}
|
||||
|
||||
for (Object o : changeSet.getPostChangeEvents()) {
|
||||
this.notifyListenersOfEvent(o);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error(e, e);
|
||||
if (transaction) {
|
||||
|
@ -150,6 +151,25 @@ public class RDFServiceSDB extends RDFServiceImpl implements RDFService {
|
|||
return true;
|
||||
}
|
||||
|
||||
private void operateOnModel(Model model, ModelChange modelChange, Dataset dataset) {
|
||||
model.enterCriticalSection(Lock.WRITE);
|
||||
try {
|
||||
if (modelChange.getOperation() == ModelChange.Operation.ADD) {
|
||||
model.read(modelChange.getSerializedModel(), null,
|
||||
getSerializationFormatString(modelChange.getSerializationFormat()));
|
||||
} else if (modelChange.getOperation() == ModelChange.Operation.REMOVE) {
|
||||
model.remove(parseModel(modelChange));
|
||||
if (dataset != null) {
|
||||
removeBlankNodesWithSparqlUpdate(dataset, model, modelChange.getGraphURI());
|
||||
}
|
||||
} else {
|
||||
log.error("unrecognized operation type");
|
||||
}
|
||||
} finally {
|
||||
model.leaveCriticalSection();
|
||||
}
|
||||
}
|
||||
|
||||
private void removeBlankNodesWithSparqlUpdate(Dataset dataset, Model model, String graphURI) {
|
||||
|
||||
Model blankNodeModel = ModelFactory.createDefaultModel();
|
||||
|
@ -414,12 +434,10 @@ public class RDFServiceSDB extends RDFServiceImpl implements RDFService {
|
|||
}
|
||||
|
||||
public void addedStatement(Statement stmt) {
|
||||
log.debug("adding " + stmt + " to " + graphURI);
|
||||
s.notifyListeners(stmt.asTriple(), ModelChange.Operation.ADD, graphURI);
|
||||
}
|
||||
|
||||
public void removedStatement(Statement stmt) {
|
||||
log.debug("removing " + stmt + " from " + graphURI);
|
||||
s.notifyListeners(stmt.asTriple(), ModelChange.Operation.REMOVE, graphURI);
|
||||
}
|
||||
|
||||
|
|
|
@ -22,4 +22,7 @@ public interface ReasonerPlugin {
|
|||
Model aboxInferencesModel,
|
||||
OntModel TBoxInferencesModel);
|
||||
|
||||
public void setSimpleReasoner(SimpleReasoner simpleReasoner);
|
||||
|
||||
public SimpleReasoner getSimpleReasoner();
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -49,12 +49,14 @@ public class SimpleReasonerTBoxListener extends StatementListener {
|
|||
|
||||
@Override
|
||||
public void addedStatement(Statement statement) {
|
||||
|
||||
ModelUpdate mu = new ModelUpdate(statement, ModelUpdate.Operation.ADD, JenaDataSourceSetupBase.JENA_TBOX_ASSERTIONS_MODEL);
|
||||
processUpdate(mu);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removedStatement(Statement statement) {
|
||||
|
||||
ModelUpdate mu = new ModelUpdate(statement, ModelUpdate.Operation.RETRACT, JenaDataSourceSetupBase.JENA_TBOX_ASSERTIONS_MODEL);
|
||||
processUpdate(mu);
|
||||
}
|
||||
|
|
|
@ -115,6 +115,7 @@ public class SimpleReasonerSetup implements ServletContextListener {
|
|||
try {
|
||||
ReasonerPlugin plugin = (ReasonerPlugin) Class.forName(
|
||||
classname).getConstructors()[0].newInstance();
|
||||
plugin.setSimpleReasoner(simpleReasoner);
|
||||
pluginList.add(plugin);
|
||||
} catch(Throwable t) {
|
||||
ss.info(this, "Could not instantiate reasoner plugin " + classname);
|
||||
|
|
|
@ -42,44 +42,42 @@ public class SimpleReasonerInversePropertyTest extends AbstractTestClass {
|
|||
@Test
|
||||
public void addABoxAssertion1() {
|
||||
|
||||
// set up the tbox
|
||||
OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
|
||||
|
||||
OntProperty P = tBox.createOntProperty("http://test.vivo/P");
|
||||
P.setLabel("property P", "en-US");
|
||||
|
||||
OntProperty Q = tBox.createOntProperty("http://test.vivo/Q");
|
||||
Q.setLabel("property Q", "en-US");
|
||||
|
||||
P.addInverseOf(Q);
|
||||
|
||||
// this is the model to receive inferences
|
||||
// this is the model to receive abox inferences
|
||||
Model inf = ModelFactory.createDefaultModel();
|
||||
|
||||
// create an ABox and register the SimpleReasoner listener with it
|
||||
// create an abox and register the SimpleReasoner listener with it
|
||||
OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
|
||||
aBox.register(new SimpleReasoner(tBox, aBox, inf));
|
||||
|
||||
// Individuals a, b, c and d
|
||||
// add assertions to the abox and verify inferences
|
||||
Resource a = aBox.createResource("http://test.vivo/a");
|
||||
Resource b = aBox.createResource("http://test.vivo/b");
|
||||
Resource c = aBox.createResource("http://test.vivo/c");
|
||||
Resource d = aBox.createResource("http://test.vivo/d");
|
||||
|
||||
// b Q a is inferred from a P b
|
||||
aBox.add(a,P,b);
|
||||
Assert.assertTrue(inf.contains(b,Q,a));
|
||||
|
||||
// d P c is inferred from c Q d.
|
||||
aBox.add(c,Q,d);
|
||||
Assert.assertTrue(inf.contains(d,P,c));
|
||||
}
|
||||
|
||||
/*
|
||||
* don't infer if it's already in the abox
|
||||
* don't infer statements already in the abox
|
||||
* (never infer because it's in the abox already)
|
||||
*/
|
||||
@Test
|
||||
public void addABoxAssertion2() {
|
||||
|
||||
// set up the tbox
|
||||
OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
|
||||
|
||||
OntProperty P = tBox.createOntProperty("http://test.vivo/P");
|
||||
|
@ -88,10 +86,10 @@ public class SimpleReasonerInversePropertyTest extends AbstractTestClass {
|
|||
Q.setLabel("property Q", "en-US");
|
||||
P.addInverseOf(Q);
|
||||
|
||||
// this is the model to receive inferences
|
||||
// this is the model to receive abox inferences
|
||||
Model inf = ModelFactory.createDefaultModel();
|
||||
|
||||
// create an ABox and add statement b Q a
|
||||
// create an ABox and add data (no inferencing happening yet)
|
||||
OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
|
||||
// Individuals a, b, c and d
|
||||
Resource a = aBox.createResource("http://test.vivo/a");
|
||||
|
@ -101,38 +99,42 @@ public class SimpleReasonerInversePropertyTest extends AbstractTestClass {
|
|||
// register SimpleReasoner
|
||||
aBox.register(new SimpleReasoner(tBox, aBox, inf));
|
||||
|
||||
// b Q a is inferred from a P b, but it is already in the abox
|
||||
Assert.assertFalse(inf.contains(b,Q,a));
|
||||
|
||||
// add data and verify inferences
|
||||
aBox.add(a,P,b);
|
||||
Assert.assertFalse(inf.contains(b,Q,a));
|
||||
}
|
||||
|
||||
/*
|
||||
* don't infer if it's already in the abox
|
||||
* don't infer statements already in the abox
|
||||
* (remove the inference when it is asserted)
|
||||
*/
|
||||
@Test
|
||||
public void addABoxAssertion3() {
|
||||
OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
|
||||
|
||||
// set up the tbox
|
||||
OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
|
||||
OntProperty P = tBox.createOntProperty("http://test.vivo/P");
|
||||
P.setLabel("property P", "en-US");
|
||||
OntProperty Q = tBox.createOntProperty("http://test.vivo/Q");
|
||||
Q.setLabel("property Q", "en-US");
|
||||
P.addInverseOf(Q);
|
||||
|
||||
// this is the model to receive inferences
|
||||
// this is the model to receive abox inferences
|
||||
Model inf = ModelFactory.createDefaultModel();
|
||||
|
||||
// create an ABox and register SimpleReasoner
|
||||
// create abox and register SimpleReasoner
|
||||
OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
|
||||
aBox.register(new SimpleReasoner(tBox, aBox, inf));
|
||||
|
||||
// Individuals a, b, c and d
|
||||
// add statements to the abox and verify inferences
|
||||
Resource a = aBox.createResource("http://test.vivo/a");
|
||||
Resource b = aBox.createResource("http://test.vivo/b");
|
||||
|
||||
// b Q a is inferred from a P b, but it is already in the abox
|
||||
aBox.add(a,P,b);
|
||||
aBox.add(b,Q,a);
|
||||
Assert.assertTrue(inf.contains(b,Q,a));
|
||||
aBox.add(b,Q,a); // this should cause the inference to be removed
|
||||
Assert.assertFalse(inf.contains(b,Q,a));
|
||||
}
|
||||
|
||||
|
@ -143,8 +145,8 @@ public class SimpleReasonerInversePropertyTest extends AbstractTestClass {
|
|||
@Test
|
||||
public void addABoxAssertion4() {
|
||||
|
||||
// set up the tbox
|
||||
OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
|
||||
|
||||
OntProperty P = tBox.createOntProperty("http://test.vivo/P");
|
||||
P.setLabel("property P", "en-US");
|
||||
OntProperty R = tBox.createOntProperty("http://test.vivo/R");
|
||||
|
@ -153,27 +155,24 @@ public class SimpleReasonerInversePropertyTest extends AbstractTestClass {
|
|||
Q.setLabel("property Q", "en-US");
|
||||
|
||||
R.addEquivalentProperty(P);
|
||||
P.addEquivalentProperty(R);
|
||||
P.addInverseOf(Q);
|
||||
|
||||
// this is the model to receive inferences
|
||||
// this is the model to receive abox inferences
|
||||
Model inf = ModelFactory.createDefaultModel();
|
||||
|
||||
// create an ABox and register the SimpleReasoner listener with it
|
||||
// create an ABox and register the SimpleReasoner with it
|
||||
OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
|
||||
aBox.register(new SimpleReasoner(tBox, aBox, inf));
|
||||
|
||||
// a, b, c and d
|
||||
// add abox statements and verify inferences
|
||||
Resource a = aBox.createResource("http://test.vivo/a");
|
||||
Resource b = aBox.createResource("http://test.vivo/b");
|
||||
Resource c = aBox.createResource("http://test.vivo/c");
|
||||
Resource d = aBox.createResource("http://test.vivo/d");
|
||||
|
||||
// b Q a is inferred from a R b.
|
||||
aBox.add(a,R,b);
|
||||
Assert.assertTrue(inf.contains(b,Q,a));
|
||||
|
||||
// d P c is inferred from c Q d.
|
||||
aBox.add(c,Q,d);
|
||||
Assert.assertTrue(inf.contains(d,P,c));
|
||||
Assert.assertTrue(inf.contains(d,R,c));
|
||||
|
@ -181,11 +180,14 @@ public class SimpleReasonerInversePropertyTest extends AbstractTestClass {
|
|||
|
||||
/*
|
||||
* basic scenarios around removing abox data
|
||||
* don't remove an inference if it's still
|
||||
* entailed by something else in the abox.
|
||||
*/
|
||||
@Test
|
||||
public void removedABoxAssertion1() {
|
||||
OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
|
||||
|
||||
// set up the tbox
|
||||
OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
|
||||
OntProperty P = tBox.createOntProperty("http://test.vivo/P");
|
||||
P.setLabel("property P", "en-US");
|
||||
OntProperty Q = tBox.createOntProperty("http://test.vivo/Q");
|
||||
|
@ -195,24 +197,23 @@ public class SimpleReasonerInversePropertyTest extends AbstractTestClass {
|
|||
P.addInverseOf(Q);
|
||||
P.addInverseOf(T);
|
||||
|
||||
// this is the model to receive inferences
|
||||
// this is the model to receive abox inferences
|
||||
Model inf = ModelFactory.createDefaultModel();
|
||||
|
||||
// create an ABox and register the SimpleReasoner listener with it
|
||||
// create an ABox and register the SimpleReasoner with it
|
||||
OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
|
||||
aBox.register(new SimpleReasoner(tBox, aBox, inf));
|
||||
|
||||
// Individuals a, b, c and d
|
||||
// add statements to the abox and verify inferences
|
||||
Resource a = aBox.createResource("http://test.vivo/a");
|
||||
Resource b = aBox.createResource("http://test.vivo/b");
|
||||
Resource c = aBox.createResource("http://test.vivo/c");
|
||||
Resource d = aBox.createResource("http://test.vivo/d");
|
||||
|
||||
// b Q a is inferred from a P b.
|
||||
aBox.add(a,P,b);
|
||||
Assert.assertTrue(inf.contains(b,Q,a));
|
||||
|
||||
// d P c is inferred from c Q d and from c T d
|
||||
// d P c is inferred from c Q d and also from c T d
|
||||
aBox.add(c,Q,d);
|
||||
aBox.add(c,T,d);
|
||||
Assert.assertTrue(inf.contains(d,P,c));
|
||||
|
@ -221,16 +222,18 @@ public class SimpleReasonerInversePropertyTest extends AbstractTestClass {
|
|||
Assert.assertFalse(inf.contains(b,Q,a));
|
||||
|
||||
aBox.remove(c,Q,d);
|
||||
Assert.assertTrue(inf.contains(d,P,c));
|
||||
Assert.assertTrue(inf.contains(d,P,c)); // still inferred from c T d
|
||||
}
|
||||
|
||||
/*
|
||||
* removing abox data with equivalent and inverse properties
|
||||
* don't remove inference if it's still inferred.
|
||||
*/
|
||||
@Test
|
||||
public void removedABoxAssertion2() {
|
||||
OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
|
||||
|
||||
// set up the tbox
|
||||
OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
|
||||
OntProperty P = tBox.createOntProperty("http://test.vivo/P");
|
||||
P.setLabel("property P", "en-US");
|
||||
OntProperty Q = tBox.createOntProperty("http://test.vivo/Q");
|
||||
|
@ -238,26 +241,29 @@ public class SimpleReasonerInversePropertyTest extends AbstractTestClass {
|
|||
OntProperty T = tBox.createOntProperty("http://test.vivo/T");
|
||||
Q.setLabel("property T", "en-US");
|
||||
P.addInverseOf(Q);
|
||||
Q.addInverseOf(P);
|
||||
P.addEquivalentProperty(T);
|
||||
T.addEquivalentProperty(P);
|
||||
|
||||
// this is the model to receive inferences
|
||||
// not clear what these will do
|
||||
tBox.rebind();
|
||||
tBox.prepare();
|
||||
|
||||
// this is the model to receive abox inferences
|
||||
Model inf = ModelFactory.createDefaultModel();
|
||||
|
||||
// create an ABox and register the SimpleReasoner listener with it
|
||||
// create an abox and register the SimpleReasoner listener with it
|
||||
OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
|
||||
aBox.register(new SimpleReasoner(tBox, aBox, inf));
|
||||
|
||||
// Individuals a, b, c and d
|
||||
// add abox data and verify inferences
|
||||
Resource a = aBox.createResource("http://test.vivo/a");
|
||||
Resource b = aBox.createResource("http://test.vivo/b");
|
||||
|
||||
// b Q a is inferred from a P b and a T b.
|
||||
// b Q a is inferred from a P b and also from a T b.
|
||||
aBox.add(a,P,b);
|
||||
aBox.add(a,T,b);
|
||||
Assert.assertTrue(inf.contains(b,Q,a));
|
||||
Assert.assertFalse(inf.contains(a,P,b));
|
||||
Assert.assertFalse(inf.contains(a,T,b));
|
||||
|
||||
aBox.remove(a,P,b);
|
||||
Assert.assertTrue(inf.contains(b,Q,a));
|
||||
|
@ -268,80 +274,81 @@ public class SimpleReasonerInversePropertyTest extends AbstractTestClass {
|
|||
*/
|
||||
@Test
|
||||
public void removedABoxAssertion3() {
|
||||
OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
|
||||
|
||||
//set up the tbox
|
||||
OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
|
||||
OntProperty P = tBox.createOntProperty("http://test.vivo/P");
|
||||
P.setLabel("property P", "en-US");
|
||||
OntProperty Q = tBox.createOntProperty("http://test.vivo/Q");
|
||||
Q.setLabel("property Q", "en-US");
|
||||
P.addInverseOf(Q);
|
||||
|
||||
// this is the model to receive inferences
|
||||
tBox.rebind(); // not sure what effect this has
|
||||
|
||||
// this is the model to receive abox inferences
|
||||
Model inf = ModelFactory.createDefaultModel();
|
||||
|
||||
// create an ABox and register the SimpleReasoner listener with it
|
||||
// create the abox and add some data - no reasoning is happening yet
|
||||
OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
|
||||
|
||||
Resource a = aBox.createResource("http://test.vivo/a");
|
||||
Resource b = aBox.createResource("http://test.vivo/b");
|
||||
aBox.add(a,P,b);
|
||||
|
||||
// register the SimpleReasoner
|
||||
aBox.register(new SimpleReasoner(tBox, aBox, inf));
|
||||
|
||||
// add abox statements and verify inferences
|
||||
aBox.add(b,Q,a);
|
||||
|
||||
Assert.assertFalse(inf.contains(b,Q,a));
|
||||
Assert.assertFalse(inf.contains(a,P,b));
|
||||
|
||||
Assert.assertFalse(inf.contains(a,P,b)); // this could be inferred from b Q a, but
|
||||
// it's already in the abox
|
||||
aBox.remove(a,P,b);
|
||||
Assert.assertTrue(inf.contains(a,P,b));
|
||||
Assert.assertTrue(inf.contains(a,P,b)); // now it gets added to inference model
|
||||
// when it's removed from the abox
|
||||
}
|
||||
|
||||
/*
|
||||
* Basic scenario around adding an inverseOf assertion to the
|
||||
* TBox
|
||||
* adding an inverseOf assertion to the tbox
|
||||
*/
|
||||
@Test
|
||||
public void addTBoxInverseAssertion1() throws InterruptedException {
|
||||
|
||||
// Create TBox, ABox and Inference models and register
|
||||
// the ABox reasoner listeners with the ABox and TBox
|
||||
// Pellet will compute TBox inferences
|
||||
|
||||
// Set up the TBox.
|
||||
OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
|
||||
|
||||
OntProperty P = tBox.createOntProperty("http://test.vivo/P");
|
||||
P.setLabel("property P", "en-US");
|
||||
|
||||
OntProperty Q = tBox.createOntProperty("http://test.vivo/Q");
|
||||
Q.setLabel("property Q", "en-US");
|
||||
|
||||
OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
|
||||
// this is the model to receive abox inferences
|
||||
Model inf = ModelFactory.createDefaultModel();
|
||||
|
||||
SimpleReasoner simpleReasoner = new SimpleReasoner(tBox, aBox, inf);
|
||||
// abox
|
||||
OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
|
||||
|
||||
// set up SimpleReasoner and register it with abox. register
|
||||
// SimpleReasonerTBoxListener with the tbox.
|
||||
SimpleReasoner simpleReasoner = new SimpleReasoner(tBox, aBox, inf);
|
||||
aBox.register(simpleReasoner);
|
||||
SimpleReasonerTBoxListener simpleReasonerTBoxListener = getTBoxListener(simpleReasoner);
|
||||
tBox.register(simpleReasonerTBoxListener);
|
||||
|
||||
// Individuals a, b, c and d
|
||||
// add abox data
|
||||
Resource a = aBox.createResource("http://test.vivo/a");
|
||||
Resource b = aBox.createResource("http://test.vivo/b");
|
||||
Resource c = aBox.createResource("http://test.vivo/c");
|
||||
Resource d = aBox.createResource("http://test.vivo/d");
|
||||
|
||||
// abox statements
|
||||
aBox.add(a,P,b);
|
||||
aBox.add(c,P,d);
|
||||
aBox.add(b,Q,a);
|
||||
|
||||
// Assert P and Q as inverses and wait for SimpleReasoner TBox
|
||||
// thread to end
|
||||
// Assert P and Q as inverses and wait for
|
||||
// SimpleReasonerTBoxListener thread to end
|
||||
|
||||
Q.addInverseOf(P);
|
||||
|
||||
tBox.rebind();
|
||||
tBox.prepare();
|
||||
|
||||
while (!VitroBackgroundThread.getLivingThreads().isEmpty()) {
|
||||
Thread.sleep(delay);
|
||||
}
|
||||
|
@ -349,47 +356,43 @@ public class SimpleReasonerInversePropertyTest extends AbstractTestClass {
|
|||
// Verify inferences
|
||||
Assert.assertTrue(inf.contains(d,Q,c));
|
||||
Assert.assertFalse(inf.contains(b,Q,a));
|
||||
Assert.assertFalse(inf.contains(a,P,b));
|
||||
|
||||
simpleReasonerTBoxListener.setStopRequested();
|
||||
}
|
||||
|
||||
/*
|
||||
* Basic scenario around removing an inverseOf assertion to the
|
||||
* TBox
|
||||
* removing an inverseOf assertion from the tbox
|
||||
*/
|
||||
@Test
|
||||
public void removeTBoxInverseAssertion1() throws InterruptedException {
|
||||
|
||||
// Create TBox, ABox and Inference models and register
|
||||
// the ABox reasoner listeners with the ABox and TBox
|
||||
// Pellet will compute TBox inferences
|
||||
|
||||
// set up the tbox.
|
||||
OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
|
||||
|
||||
// set up TBox and Abox
|
||||
|
||||
OntProperty P = tBox.createOntProperty("http://test.vivo/propP");
|
||||
P.setLabel("property P", "en-US");
|
||||
|
||||
OntProperty Q = tBox.createOntProperty("http://test.vivo/propQ");
|
||||
Q.setLabel("property Q", "en-US");
|
||||
|
||||
Q.addInverseOf(P);
|
||||
|
||||
OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
|
||||
Model inf = ModelFactory.createDefaultModel();
|
||||
// this is the model to receive abox inferences
|
||||
Model inf = ModelFactory.createDefaultModel();
|
||||
|
||||
SimpleReasoner simpleReasoner = new SimpleReasoner(tBox, aBox, inf);
|
||||
// abox
|
||||
OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
|
||||
|
||||
// set up SimpleReasoner and SimpleReasonerTBox listener,
|
||||
// register them with abox and tbox
|
||||
SimpleReasoner simpleReasoner = new SimpleReasoner(tBox, aBox, inf);
|
||||
aBox.register(simpleReasoner);
|
||||
SimpleReasonerTBoxListener simpleReasonerTBoxListener = getTBoxListener(simpleReasoner);
|
||||
tBox.register(simpleReasonerTBoxListener);
|
||||
// Individuals a, b, c and d
|
||||
|
||||
// add statements to the abox and verify inference
|
||||
Resource c = aBox.createResource("http://test.vivo/c");
|
||||
Resource d = aBox.createResource("http://test.vivo/d");
|
||||
|
||||
// abox statements
|
||||
aBox.add(c,P,d);
|
||||
|
||||
Assert.assertTrue(inf.contains(d,Q,c));
|
||||
|
||||
// Remove P and Q inverse relationship and wait for
|
||||
|
@ -397,14 +400,11 @@ public class SimpleReasonerInversePropertyTest extends AbstractTestClass {
|
|||
|
||||
Q.removeInverseProperty(P);
|
||||
|
||||
tBox.rebind();
|
||||
tBox.prepare();
|
||||
|
||||
while (!VitroBackgroundThread.getLivingThreads().isEmpty()) {
|
||||
Thread.sleep(delay);
|
||||
}
|
||||
|
||||
// Verify inferences
|
||||
// Verify inference has been removed
|
||||
Assert.assertFalse(inf.contains(d,Q,c));
|
||||
|
||||
simpleReasonerTBoxListener.setStopRequested();
|
||||
|
@ -416,12 +416,9 @@ public class SimpleReasonerInversePropertyTest extends AbstractTestClass {
|
|||
@Test
|
||||
public void recomputeABox1() throws InterruptedException {
|
||||
|
||||
// Create TBox, ABox and Inference models and register
|
||||
// the ABox reasoner listeners with the ABox and TBox
|
||||
// Pellet will compute TBox inferences
|
||||
// set up tbox
|
||||
OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
|
||||
|
||||
// set up TBox and Abox
|
||||
OntProperty P = tBox.createOntProperty("http://test.vivo/propP");
|
||||
P.setLabel("property P", "en-US");
|
||||
OntProperty Q = tBox.createOntProperty("http://test.vivo/propQ");
|
||||
|
@ -434,21 +431,23 @@ public class SimpleReasonerInversePropertyTest extends AbstractTestClass {
|
|||
Q.setLabel("property Y", "en-US");
|
||||
X.addInverseOf(Y);
|
||||
|
||||
// create abox and abox inf model and register simplereasoner
|
||||
// with abox.
|
||||
OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
|
||||
Model inf = ModelFactory.createDefaultModel();
|
||||
|
||||
SimpleReasoner simpleReasoner = new SimpleReasoner(tBox, aBox, inf);
|
||||
aBox.register(simpleReasoner);
|
||||
// Individuals a, b, c and d
|
||||
|
||||
// abox statements
|
||||
Resource a = aBox.createResource("http://test.vivo/a");
|
||||
Resource b = aBox.createResource("http://test.vivo/b");
|
||||
Resource c = aBox.createResource("http://test.vivo/c");
|
||||
Resource d = aBox.createResource("http://test.vivo/d");
|
||||
|
||||
// abox statements
|
||||
aBox.add(a,P,b);
|
||||
aBox.add(c,X,d);
|
||||
|
||||
//recompute whole abox
|
||||
simpleReasoner.recompute();
|
||||
|
||||
while (simpleReasoner.isRecomputing()) {
|
||||
|
|
|
@ -8,6 +8,7 @@ import org.junit.Before;
|
|||
import org.junit.Test;
|
||||
import org.mindswap.pellet.jena.PelletReasonerFactory;
|
||||
|
||||
import com.hp.hpl.jena.ontology.AnnotationProperty;
|
||||
import com.hp.hpl.jena.ontology.OntClass;
|
||||
import com.hp.hpl.jena.ontology.OntModel;
|
||||
import com.hp.hpl.jena.ontology.OntModelSpec;
|
||||
|
@ -16,6 +17,7 @@ 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 com.hp.hpl.jena.rdf.model.Resource;
|
||||
import com.hp.hpl.jena.rdf.model.ResourceFactory;
|
||||
import com.hp.hpl.jena.vocabulary.OWL;
|
||||
import com.hp.hpl.jena.vocabulary.RDF;
|
||||
|
||||
|
@ -25,6 +27,7 @@ import edu.cornell.mannlib.vitro.webapp.utils.threads.VitroBackgroundThread;
|
|||
public class SimpleReasonerSameAsTest extends AbstractTestClass {
|
||||
|
||||
long delay = 50;
|
||||
private static final String mostSpecificTypePropertyURI = "http://vitro.mannlib.cornell.edu/ns/vitro/0.7#mostSpecificType";
|
||||
|
||||
@Before
|
||||
public void suppressErrorOutput() {
|
||||
|
@ -103,10 +106,10 @@ public class SimpleReasonerSameAsTest extends AbstractTestClass {
|
|||
OntProperty Q = tBox.createObjectProperty("http://test.vivo/Q");
|
||||
Q.setLabel("property Q", "en-US");
|
||||
|
||||
OntProperty S = tBox.createDatatypeProperty("http://test.vivo/");
|
||||
OntProperty S = tBox.createDatatypeProperty("http://test.vivo/data1");
|
||||
S.setLabel("property S", "en-US");
|
||||
|
||||
OntProperty T = tBox.createDatatypeProperty("http://test.vivo/");
|
||||
OntProperty T = tBox.createDatatypeProperty("http://test.vivo/data2");
|
||||
T.setLabel("property T", "en-US");
|
||||
|
||||
Literal literal1 = tBox.createLiteral("Literal value 1");
|
||||
|
@ -147,8 +150,7 @@ public class SimpleReasonerSameAsTest extends AbstractTestClass {
|
|||
}
|
||||
|
||||
/*
|
||||
* basic scenario of adding an abox assertion for
|
||||
* an individual is sameAs another.
|
||||
* adding abox assertion for individual in sameAs chain.
|
||||
*/
|
||||
@Test
|
||||
public void addABoxAssertion1() {
|
||||
|
@ -182,10 +184,23 @@ public class SimpleReasonerSameAsTest extends AbstractTestClass {
|
|||
Resource b = aBox.createResource("http://test.vivo/b");
|
||||
Resource c = aBox.createResource("http://test.vivo/c");
|
||||
Resource d = aBox.createResource("http://test.vivo/d");
|
||||
Resource e = aBox.createResource("http://test.vivo/e");
|
||||
Resource f = aBox.createResource("http://test.vivo/f");
|
||||
|
||||
aBox.add(a,OWL.sameAs,b);
|
||||
aBox.add(b,OWL.sameAs,e);
|
||||
aBox.add(e,OWL.sameAs,f);
|
||||
|
||||
Assert.assertTrue(inf.contains(b,OWL.sameAs,a));
|
||||
Assert.assertTrue(inf.contains(e,OWL.sameAs,a));
|
||||
Assert.assertTrue(inf.contains(e,OWL.sameAs,b));
|
||||
Assert.assertTrue(inf.contains(a,OWL.sameAs,e));
|
||||
Assert.assertTrue(inf.contains(f,OWL.sameAs,e));
|
||||
Assert.assertTrue(inf.contains(f,OWL.sameAs,b));
|
||||
Assert.assertTrue(inf.contains(f,OWL.sameAs,a));
|
||||
Assert.assertTrue(inf.contains(b,OWL.sameAs,f));
|
||||
Assert.assertTrue(inf.contains(a,OWL.sameAs,f));
|
||||
|
||||
|
||||
aBox.add(a,P,c);
|
||||
aBox.add(a,S,literal1);
|
||||
|
@ -196,24 +211,48 @@ public class SimpleReasonerSameAsTest extends AbstractTestClass {
|
|||
Assert.assertTrue(inf.contains(b,S,literal1));
|
||||
Assert.assertTrue(inf.contains(a,Q,d));
|
||||
Assert.assertTrue(inf.contains(a,T,literal2));
|
||||
Assert.assertTrue(inf.contains(e,P,c));
|
||||
Assert.assertTrue(inf.contains(e,S,literal1));
|
||||
Assert.assertTrue(inf.contains(e,Q,d));
|
||||
Assert.assertTrue(inf.contains(e,T,literal2));
|
||||
Assert.assertTrue(inf.contains(f,P,c));
|
||||
Assert.assertTrue(inf.contains(f,S,literal1));
|
||||
Assert.assertTrue(inf.contains(f,Q,d));
|
||||
Assert.assertTrue(inf.contains(f,T,literal2));
|
||||
|
||||
aBox.remove(b,OWL.sameAs,e);
|
||||
|
||||
Assert.assertTrue(inf.contains(b,OWL.sameAs,a));
|
||||
Assert.assertFalse(inf.contains(e,OWL.sameAs,a));
|
||||
Assert.assertFalse(inf.contains(e,OWL.sameAs,b));
|
||||
Assert.assertFalse(inf.contains(a,OWL.sameAs,e));
|
||||
Assert.assertTrue(inf.contains(f,OWL.sameAs,e));
|
||||
Assert.assertFalse(inf.contains(f,OWL.sameAs,b));
|
||||
Assert.assertFalse(inf.contains(f,OWL.sameAs,a));
|
||||
Assert.assertFalse(inf.contains(b,OWL.sameAs,f));
|
||||
Assert.assertFalse(inf.contains(a,OWL.sameAs,f));
|
||||
|
||||
Assert.assertTrue(inf.contains(b,P,c));
|
||||
Assert.assertTrue(inf.contains(b,S,literal1));
|
||||
Assert.assertTrue(inf.contains(a,Q,d));
|
||||
Assert.assertTrue(inf.contains(a,T,literal2));
|
||||
Assert.assertFalse(inf.contains(e,P,c));
|
||||
Assert.assertFalse(inf.contains(e,S,literal1));
|
||||
Assert.assertFalse(inf.contains(e,Q,d));
|
||||
Assert.assertFalse(inf.contains(e,T,literal2));
|
||||
Assert.assertFalse(inf.contains(f,P,c));
|
||||
Assert.assertFalse(inf.contains(f,S,literal1));
|
||||
Assert.assertFalse(inf.contains(f,Q,d));
|
||||
Assert.assertFalse(inf.contains(f,T,literal2));
|
||||
}
|
||||
|
||||
/*
|
||||
* adding abox assertion for individuals that are sameAs
|
||||
* each other.
|
||||
* sameAs with datatype properties
|
||||
*/
|
||||
@Test
|
||||
public void addABoxAssertion2() {
|
||||
OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
|
||||
|
||||
OntClass classA = tBox.createClass("http://test.vivo/A");
|
||||
classA.setLabel("class A", "en-US");
|
||||
|
||||
OntClass classB = tBox.createClass("http://test.vivo/B");
|
||||
classB.setLabel("class B", "en-US");
|
||||
|
||||
classA.addSubClass(classB);
|
||||
|
||||
OntProperty desc = tBox.createDatatypeProperty("http://test.vivo/desc");
|
||||
desc.setLabel("property desc", "en-US");
|
||||
|
||||
|
@ -234,14 +273,10 @@ public class SimpleReasonerSameAsTest extends AbstractTestClass {
|
|||
aBox.add(a,desc,desc1);
|
||||
aBox.add(b,desc,desc2);
|
||||
aBox.add(a,OWL.sameAs,b);
|
||||
aBox.add(a, RDF.type, classB);
|
||||
|
||||
Assert.assertTrue(inf.contains(a,desc,desc2));
|
||||
Assert.assertTrue(inf.contains(a,RDF.type,classA));
|
||||
Assert.assertTrue(inf.contains(b,desc,desc1));
|
||||
Assert.assertTrue(inf.contains(b,OWL.sameAs,a));
|
||||
Assert.assertTrue(inf.contains(b,RDF.type,classB));
|
||||
Assert.assertTrue(inf.contains(b,RDF.type,classA));
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -295,11 +330,11 @@ public class SimpleReasonerSameAsTest extends AbstractTestClass {
|
|||
}
|
||||
|
||||
/*
|
||||
* adding an inverseOf assertion for individuals who are sameAs
|
||||
* each other.
|
||||
* adding and removing an inverseOf assertion for individuals who
|
||||
* are sameAs each other.
|
||||
*/
|
||||
@Test
|
||||
public void addTBoxInverseAssertion1() throws InterruptedException {
|
||||
public void tBoxInverseAssertion1() throws InterruptedException {
|
||||
|
||||
// Create TBox, ABox and Inference models and register
|
||||
// the ABox reasoner listeners with the ABox and TBox
|
||||
|
@ -329,26 +364,274 @@ public class SimpleReasonerSameAsTest extends AbstractTestClass {
|
|||
aBox.add(a,P,b);
|
||||
aBox.add(a, OWL.sameAs,b);
|
||||
|
||||
// Assert P and Q as inverses and wait for SimpleReasoner TBox
|
||||
// thread to end
|
||||
|
||||
Q.addInverseOf(P);
|
||||
|
||||
tBox.rebind();
|
||||
tBox.prepare();
|
||||
|
||||
while (!VitroBackgroundThread.getLivingThreads().isEmpty()) {
|
||||
Thread.sleep(delay);
|
||||
}
|
||||
|
||||
// Verify inferences
|
||||
Assert.assertTrue(inf.contains(b,Q,a));
|
||||
Assert.assertTrue(inf.contains(b,OWL.sameAs,a));
|
||||
Assert.assertTrue(inf.contains(b,P,b));
|
||||
Assert.assertTrue(inf.contains(a,Q,a));
|
||||
|
||||
Q.removeInverseProperty(P);
|
||||
|
||||
while (!VitroBackgroundThread.getLivingThreads().isEmpty()) {
|
||||
Thread.sleep(delay);
|
||||
}
|
||||
|
||||
Assert.assertFalse(inf.contains(b,Q,a));
|
||||
Assert.assertTrue(inf.contains(b,OWL.sameAs,a));
|
||||
Assert.assertTrue(inf.contains(b,P,b));
|
||||
Assert.assertFalse(inf.contains(a,Q,a));
|
||||
|
||||
simpleReasonerTBoxListener.setStopRequested();
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* adding and removing a type assertion for an individual who has
|
||||
* a sameAs individual.
|
||||
*/
|
||||
//@Test
|
||||
public void tBoxTypeAssertion1() throws InterruptedException {
|
||||
|
||||
// Create a Tbox with a simple class hierarchy. B is a subclass of A.
|
||||
// Pellet will compute TBox inferences
|
||||
OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
|
||||
|
||||
OntClass classA = tBox.createClass("http://test.vivo/A");
|
||||
classA.setLabel("class A", "en-US");
|
||||
|
||||
OntClass classB = tBox.createClass("http://test.vivo/B");
|
||||
classB.setLabel("class B", "en-US");
|
||||
|
||||
classA.addSubClass(classB);
|
||||
|
||||
// this is the model to receive inferences
|
||||
Model inf = ModelFactory.createDefaultModel();
|
||||
|
||||
// create an ABox and register the SimpleReasoner listener with it
|
||||
OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
|
||||
aBox.register(new SimpleReasoner(tBox, aBox, inf));
|
||||
|
||||
Resource x = aBox.createResource("http://test.vivo/x");
|
||||
Resource y = aBox.createResource("http://test.vivo/y");
|
||||
Resource z = aBox.createResource("http://test.vivo/z");
|
||||
|
||||
aBox.add(x,OWL.sameAs,y);
|
||||
aBox.add(y,OWL.sameAs,z);
|
||||
aBox.add(x,RDF.type,classB);
|
||||
|
||||
Assert.assertTrue(inf.contains(y,RDF.type,classB));
|
||||
Assert.assertTrue(inf.contains(y,RDF.type,classA));
|
||||
Assert.assertTrue(inf.contains(z,RDF.type,classB));
|
||||
Assert.assertTrue(inf.contains(z,RDF.type,classA));
|
||||
|
||||
aBox.remove(x,RDF.type,classB);
|
||||
Assert.assertFalse(inf.contains(y,RDF.type,classB));
|
||||
Assert.assertFalse(inf.contains(y,RDF.type,classA));
|
||||
Assert.assertFalse(inf.contains(z,RDF.type,classB));
|
||||
Assert.assertFalse(inf.contains(z,RDF.type,classA));
|
||||
}
|
||||
|
||||
/*
|
||||
* adding and removing subclass assertion when there is an
|
||||
* individual member who has a sameAs individual.
|
||||
*/
|
||||
@Test
|
||||
public void tBoxSubclassAssertion1() throws InterruptedException {
|
||||
|
||||
//create aBox and tBox, and SimpleReasoner to listen to them
|
||||
OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
|
||||
OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
|
||||
Model inf = ModelFactory.createDefaultModel();
|
||||
|
||||
SimpleReasoner simpleReasoner = new SimpleReasoner(tBox, aBox, inf);
|
||||
aBox.register(simpleReasoner);
|
||||
SimpleReasonerTBoxListener simpleReasonerTBoxListener = getTBoxListener(simpleReasoner);
|
||||
tBox.register(simpleReasonerTBoxListener);
|
||||
|
||||
// set up TBox
|
||||
OntClass classA = tBox.createClass("http://test.vivo/A");
|
||||
classA.setLabel("class A", "en-US");
|
||||
OntClass classB = tBox.createClass("http://test.vivo/B");
|
||||
classB.setLabel("class B", "en-US");
|
||||
OntClass classC = tBox.createClass("http://test.vivo/C");
|
||||
classC.setLabel("class C", "en-US");
|
||||
|
||||
// set up ABox
|
||||
Resource a = aBox.createResource("http://test.vivo/a");
|
||||
Resource b = aBox.createResource("http://test.vivo/b");
|
||||
Resource c = aBox.createResource("http://test.vivo/c");
|
||||
|
||||
aBox.add(a, RDF.type, classC);
|
||||
aBox.add(a, OWL.sameAs, b);
|
||||
aBox.add(c, OWL.sameAs, a);
|
||||
|
||||
// update TBox
|
||||
classA.addSubClass(classB);
|
||||
|
||||
// wait for SimpleReasonerTBoxListener thread to end
|
||||
while (!VitroBackgroundThread.getLivingThreads().isEmpty()) {
|
||||
Thread.sleep(delay);
|
||||
}
|
||||
|
||||
classB.addSubClass(classC);
|
||||
classA.addSubClass(classC); // simulate what Pellet would infer, and
|
||||
// thus what the SimpleReasonerTBoxListener
|
||||
// would be notified of.
|
||||
|
||||
// wait for SimpleReasonerTBoxListener thread to end
|
||||
while (!VitroBackgroundThread.getLivingThreads().isEmpty()) {
|
||||
Thread.sleep(delay);
|
||||
}
|
||||
|
||||
// Verify inferences
|
||||
Assert.assertFalse(inf.contains(a, RDF.type, classC));
|
||||
Assert.assertTrue(inf.contains(a, RDF.type, classB));
|
||||
Assert.assertTrue(inf.contains(a, RDF.type, classA));
|
||||
|
||||
Assert.assertTrue(inf.contains(b, RDF.type, classC));
|
||||
Assert.assertTrue(inf.contains(b, RDF.type, classB));
|
||||
Assert.assertTrue(inf.contains(b, RDF.type, classA));
|
||||
|
||||
Assert.assertTrue(inf.contains(c, RDF.type, classC));
|
||||
Assert.assertTrue(inf.contains(c, RDF.type, classB));
|
||||
Assert.assertTrue(inf.contains(c, RDF.type, classA));
|
||||
|
||||
// update TBox
|
||||
classA.removeSubClass(classB);
|
||||
classA.removeSubClass(classC); // simulate what Pellet would infer, and
|
||||
// thus what the SimpleReasonerTBoxListener
|
||||
// would be notified of.
|
||||
|
||||
// wait for SimpleReasonerTBoxListener thread to end
|
||||
while (!VitroBackgroundThread.getLivingThreads().isEmpty()) {
|
||||
Thread.sleep(delay);
|
||||
}
|
||||
|
||||
// Verify inferences
|
||||
Assert.assertFalse(inf.contains(a, RDF.type, classC));
|
||||
Assert.assertTrue(inf.contains(a, RDF.type, classB));
|
||||
Assert.assertFalse(inf.contains(a, RDF.type, classA));
|
||||
|
||||
Assert.assertTrue(inf.contains(b, RDF.type, classC));
|
||||
Assert.assertTrue(inf.contains(b, RDF.type, classB));
|
||||
Assert.assertFalse(inf.contains(b, RDF.type, classA));
|
||||
|
||||
Assert.assertTrue(inf.contains(c, RDF.type, classC));
|
||||
Assert.assertTrue(inf.contains(c, RDF.type, classB));
|
||||
Assert.assertFalse(inf.contains(c, RDF.type, classA));
|
||||
|
||||
// update TBox
|
||||
classB.removeSubClass(classC);
|
||||
|
||||
// wait for SimpleReasonerTBoxListener thread to end
|
||||
while (!VitroBackgroundThread.getLivingThreads().isEmpty()) {
|
||||
Thread.sleep(delay);
|
||||
}
|
||||
|
||||
// Verify inferences
|
||||
Assert.assertFalse(inf.contains(a, RDF.type, classC));
|
||||
Assert.assertFalse(inf.contains(a, RDF.type, classB));
|
||||
Assert.assertFalse(inf.contains(a, RDF.type, classA));
|
||||
|
||||
Assert.assertTrue(inf.contains(b, RDF.type, classC));
|
||||
Assert.assertFalse(inf.contains(b, RDF.type, classB));
|
||||
Assert.assertFalse(inf.contains(b, RDF.type, classA));
|
||||
|
||||
Assert.assertTrue(inf.contains(c, RDF.type, classC));
|
||||
Assert.assertFalse(inf.contains(c, RDF.type, classB));
|
||||
Assert.assertFalse(inf.contains(c, RDF.type, classA));
|
||||
|
||||
simpleReasonerTBoxListener.setStopRequested();
|
||||
}
|
||||
|
||||
/*
|
||||
* test that mostSpecificType inferences propagate to sameAs
|
||||
* individuals
|
||||
*/
|
||||
//@Test
|
||||
public void mostSpecificTypeTest1() throws InterruptedException {
|
||||
|
||||
// set up tbox. Pellet is reasoning; SimpleReasonerTBoxListener is not being used.
|
||||
OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
|
||||
AnnotationProperty mostSpecificType = tBox.createAnnotationProperty(mostSpecificTypePropertyURI);
|
||||
|
||||
OntClass classA = tBox.createClass("http://test.vivo/A");
|
||||
classA.setLabel("class A", "en-US");
|
||||
OntClass classC = tBox.createClass("http://test.vivo/C");
|
||||
classC.setLabel("class C", "en-US");
|
||||
OntClass classD = tBox.createClass("http://test.vivo/D");
|
||||
classD.setLabel("class D", "en-US");
|
||||
OntClass classE = tBox.createClass("http://test.vivo/E");
|
||||
classE.setLabel("class E", "en-US");
|
||||
|
||||
classA.addSubClass(classC);
|
||||
classC.addSubClass(classD);
|
||||
classC.addSubClass(classE);
|
||||
|
||||
// this will receive the abox inferences
|
||||
Model inf = ModelFactory.createDefaultModel();
|
||||
|
||||
// abox
|
||||
OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
|
||||
|
||||
// set up SimpleReasoner and register it with abox
|
||||
SimpleReasoner simpleReasoner = new SimpleReasoner(tBox, aBox, inf);
|
||||
aBox.register(simpleReasoner);
|
||||
|
||||
// add & remove ABox type statements and verify inferences
|
||||
Resource a = aBox.createResource("http://test.vivo/a");
|
||||
Resource b = aBox.createResource("http://test.vivo/b");
|
||||
Resource c = aBox.createResource("http://test.vivo/c");
|
||||
Resource d = aBox.createResource("http://test.vivo/d");
|
||||
|
||||
aBox.add(a, OWL.sameAs, b);
|
||||
aBox.add(c, OWL.sameAs, b);
|
||||
aBox.add(d, OWL.sameAs, a);
|
||||
|
||||
aBox.add(a, RDF.type, classD);
|
||||
aBox.add(d, RDF.type, classC);
|
||||
Assert.assertFalse(inf.contains(a,RDF.type,classD));
|
||||
Assert.assertTrue(inf.contains(a,RDF.type,classC));
|
||||
Assert.assertTrue(inf.contains(b,RDF.type, classD));
|
||||
Assert.assertTrue(inf.contains(b,RDF.type, classC));
|
||||
Assert.assertTrue(inf.contains(c,RDF.type, classD));
|
||||
Assert.assertTrue(inf.contains(c,RDF.type, classC));
|
||||
Assert.assertTrue(inf.contains(d,RDF.type, classD));
|
||||
Assert.assertFalse(inf.contains(d,RDF.type, classC));
|
||||
|
||||
Assert.assertTrue(inf.contains(a, mostSpecificType, ResourceFactory.createResource(classD.getURI())));
|
||||
Assert.assertTrue(inf.contains(b, mostSpecificType, ResourceFactory.createResource(classD.getURI())));
|
||||
Assert.assertTrue(inf.contains(c, mostSpecificType, ResourceFactory.createResource(classD.getURI())));
|
||||
Assert.assertTrue(inf.contains(d, mostSpecificType, ResourceFactory.createResource(classD.getURI())));
|
||||
Assert.assertFalse(inf.contains(a, mostSpecificType, ResourceFactory.createResource(classC.getURI())));
|
||||
Assert.assertFalse(inf.contains(b, mostSpecificType, ResourceFactory.createResource(classC.getURI())));
|
||||
Assert.assertFalse(inf.contains(c, mostSpecificType, ResourceFactory.createResource(classC.getURI())));
|
||||
Assert.assertFalse(inf.contains(d, mostSpecificType, ResourceFactory.createResource(classC.getURI())));
|
||||
|
||||
aBox.remove(a, RDF.type, classD);
|
||||
Assert.assertFalse(inf.contains(a,RDF.type,classD));
|
||||
Assert.assertTrue(inf.contains(a,RDF.type,classC));
|
||||
Assert.assertFalse(inf.contains(b,RDF.type, classD));
|
||||
Assert.assertTrue(inf.contains(b,RDF.type, classC));
|
||||
Assert.assertFalse(inf.contains(c,RDF.type, classD));
|
||||
Assert.assertTrue(inf.contains(c,RDF.type, classC));
|
||||
Assert.assertFalse(inf.contains(d,RDF.type, classD));
|
||||
Assert.assertFalse(inf.contains(d,RDF.type, classC));
|
||||
Assert.assertTrue(inf.contains(a, mostSpecificType, ResourceFactory.createResource(classC.getURI())));
|
||||
Assert.assertTrue(inf.contains(b, mostSpecificType, ResourceFactory.createResource(classC.getURI())));
|
||||
Assert.assertTrue(inf.contains(c, mostSpecificType, ResourceFactory.createResource(classC.getURI())));
|
||||
Assert.assertTrue(inf.contains(d, mostSpecificType, ResourceFactory.createResource(classC.getURI())));
|
||||
Assert.assertFalse(inf.contains(a, mostSpecificType, ResourceFactory.createResource(classD.getURI())));
|
||||
Assert.assertFalse(inf.contains(b, mostSpecificType, ResourceFactory.createResource(classD.getURI())));
|
||||
Assert.assertFalse(inf.contains(c, mostSpecificType, ResourceFactory.createResource(classD.getURI())));
|
||||
Assert.assertFalse(inf.contains(d, mostSpecificType, ResourceFactory.createResource(classD.getURI())));
|
||||
}
|
||||
|
||||
/*
|
||||
* Basic scenario around recomputing the ABox inferences
|
||||
*/
|
||||
|
|
|
@ -13,7 +13,6 @@ import com.hp.hpl.jena.ontology.AnnotationProperty;
|
|||
import com.hp.hpl.jena.ontology.OntClass;
|
||||
import com.hp.hpl.jena.ontology.OntModel;
|
||||
import com.hp.hpl.jena.ontology.OntModelSpec;
|
||||
import com.hp.hpl.jena.ontology.OntProperty;
|
||||
import com.hp.hpl.jena.rdf.model.Model;
|
||||
import com.hp.hpl.jena.rdf.model.ModelFactory;
|
||||
import com.hp.hpl.jena.rdf.model.Resource;
|
||||
|
@ -28,7 +27,6 @@ import edu.cornell.mannlib.vitro.webapp.utils.threads.VitroBackgroundThread;
|
|||
|
||||
public class SimpleReasonerTest extends AbstractTestClass {
|
||||
|
||||
private Resource objectProperty = ResourceFactory.createResource("http://www.w3.org/2002/07/owl#ObjectProperty");
|
||||
private static final String mostSpecificTypePropertyURI = "http://vitro.mannlib.cornell.edu/ns/vitro/0.7#mostSpecificType";
|
||||
long delay = 50;
|
||||
|
||||
|
@ -591,8 +589,12 @@ public class SimpleReasonerTest extends AbstractTestClass {
|
|||
* inference graph.
|
||||
*
|
||||
*/
|
||||
//@Test - enable this in 1.5 - will need to add in PelletListener infrastructure
|
||||
public void removeTBoxSubClassAssertion2() throws InterruptedException {
|
||||
//@Test - this test would need PelletListener infrastructure, which we're not
|
||||
// testing in this suite. The reason it doesn't work as it is because
|
||||
// the SimpleReasonerTBoxListener is not listening to the tBox inference
|
||||
// model as Pellet is updating it. I could simulate it by adding to the
|
||||
// tBox assertions what we can count on Pellet to infer.
|
||||
public void bcdTest() throws InterruptedException {
|
||||
// Create TBox, ABox and Inference models and register
|
||||
// the ABox reasoner listeners with the ABox and TBox
|
||||
// Pellet will compute TBox inferences
|
||||
|
@ -621,6 +623,9 @@ public class SimpleReasonerTest extends AbstractTestClass {
|
|||
LivingThing.addSubClass(Flora);
|
||||
Flora.addSubClass(Brassica);
|
||||
|
||||
tBox.rebind();
|
||||
tBox.prepare();
|
||||
|
||||
while (!VitroBackgroundThread.getLivingThreads().isEmpty()) {
|
||||
Thread.sleep(delay);
|
||||
}
|
||||
|
@ -632,6 +637,8 @@ public class SimpleReasonerTest extends AbstractTestClass {
|
|||
// Remove the statement that Brassica is a subclass of Flora from the TBox
|
||||
Flora.removeSubClass(Brassica);
|
||||
|
||||
tBox.rebind();
|
||||
tBox.prepare();
|
||||
while (!VitroBackgroundThread.getLivingThreads().isEmpty()) {
|
||||
Thread.sleep(delay);
|
||||
}
|
||||
|
@ -647,411 +654,6 @@ public class SimpleReasonerTest extends AbstractTestClass {
|
|||
simpleReasonerTBoxListener.setStopRequested();
|
||||
}
|
||||
|
||||
/*
|
||||
* tests rdfs:subPropertyOf materialization for object properties.
|
||||
*/
|
||||
// @Test uncomment when sub/equiv property inferencing is enabled. sjm222 5/13/2011
|
||||
public void addABoxAssertion1() throws InterruptedException {
|
||||
|
||||
// Create TBox, ABox and Inference models and register
|
||||
// the ABox reasoner listeners with the ABox and TBox
|
||||
// Pellet will compute TBox inferences
|
||||
|
||||
OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
|
||||
OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
|
||||
Model inf = ModelFactory.createDefaultModel();
|
||||
|
||||
SimpleReasoner simpleReasoner = new SimpleReasoner(tBox, aBox, inf);
|
||||
aBox.register(simpleReasoner);
|
||||
SimpleReasonerTBoxListener simpleReasonerTBoxListener = getTBoxListener(simpleReasoner);
|
||||
tBox.register(simpleReasonerTBoxListener);
|
||||
|
||||
// Add object properties propA and propB to the TBox.
|
||||
// propB is a sub-property of propA.
|
||||
|
||||
OntProperty propA = tBox.createOntProperty("http://test.vivo/propA");
|
||||
tBox.add(propA, RDF.type, objectProperty);
|
||||
propA.setLabel("propA", "en-US");
|
||||
|
||||
OntProperty propB = tBox.createOntProperty("http://test.vivo/propB");
|
||||
tBox.add(propB, RDF.type, objectProperty);
|
||||
propB.setLabel("propB", "en-US");
|
||||
|
||||
propA.addSubProperty(propB);
|
||||
|
||||
// Add the assertion that x propB y to the ABox
|
||||
Resource ind_x = aBox.createResource("http://test.vivo/x");
|
||||
Resource ind_y = aBox.createResource("http://test.vivo/y");
|
||||
aBox.add(ind_x, propB, ind_y);
|
||||
|
||||
// Verify that x propA y was inferred
|
||||
Statement xay = ResourceFactory.createStatement(ind_x, propA, ind_y);
|
||||
Assert.assertTrue(inf.contains(xay));
|
||||
}
|
||||
|
||||
/*
|
||||
* Test that when a statement is asserted, that it not
|
||||
* added to the inference graph.
|
||||
*/
|
||||
// @Test uncomment when sub/equiv property inferencing is enabled. sjm222 5/13/2011
|
||||
public void addABoxAssertion2(){
|
||||
|
||||
// Create TBox, ABox and Inference models and register
|
||||
// the ABox reasoner listeners with the ABox and TBox
|
||||
// Pellet will compute TBox inferences
|
||||
|
||||
OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
|
||||
OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
|
||||
Model inf = ModelFactory.createDefaultModel();
|
||||
|
||||
SimpleReasoner simpleReasoner = new SimpleReasoner(tBox, aBox, inf);
|
||||
aBox.register(simpleReasoner);
|
||||
SimpleReasonerTBoxListener simpleReasonerTBoxListener = getTBoxListener(simpleReasoner);
|
||||
tBox.register(simpleReasonerTBoxListener);
|
||||
|
||||
// Add object properties propA and propB to the TBox.
|
||||
// propB is a sub-property of propA.
|
||||
|
||||
OntProperty propA = tBox.createOntProperty("http://test.vivo/propA");
|
||||
tBox.add(propA, RDF.type, objectProperty);
|
||||
propA.setLabel("propA", "en-US");
|
||||
|
||||
OntProperty propB = tBox.createOntProperty("http://test.vivo/propB");
|
||||
tBox.add(propB, RDF.type, objectProperty);
|
||||
propB.setLabel("propB", "en-US");
|
||||
|
||||
propA.addSubProperty(propB);
|
||||
|
||||
// Add the assertion that x propB y to the ABox
|
||||
Resource ind_x = aBox.createResource("http://test.vivo/x");
|
||||
Resource ind_y = aBox.createResource("http://test.vivo/y");
|
||||
aBox.add(ind_x, propB, ind_y);
|
||||
|
||||
// Verify that x propA y was inferred
|
||||
Statement xby = ResourceFactory.createStatement(ind_x, propB, ind_y);
|
||||
Assert.assertFalse(inf.contains(xby));
|
||||
}
|
||||
|
||||
/*
|
||||
* Test inference based on property equivalence
|
||||
*/
|
||||
// @Test uncomment when sub/equiv property inferencing is enabled. sjm222 5/13/2011
|
||||
public void addABoxAssertion4(){
|
||||
|
||||
// Create TBox, ABox and Inference models and register
|
||||
// the ABox reasoner listeners with the ABox and TBox
|
||||
// Pellet will compute TBox inferences
|
||||
|
||||
OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
|
||||
OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
|
||||
Model inf = ModelFactory.createDefaultModel();
|
||||
|
||||
SimpleReasoner simpleReasoner = new SimpleReasoner(tBox, aBox, inf);
|
||||
aBox.register(simpleReasoner);
|
||||
SimpleReasonerTBoxListener simpleReasonerTBoxListener = getTBoxListener(simpleReasoner);
|
||||
tBox.register(simpleReasonerTBoxListener);
|
||||
|
||||
// Add properties A, B and C to the TBox
|
||||
// A is equivalent to B
|
||||
// C is a subclass of A
|
||||
|
||||
OntProperty propA = tBox.createOntProperty("http://test.vivo/A");
|
||||
tBox.add(propA, RDF.type, objectProperty);
|
||||
propA.setLabel("property A", "en-US");
|
||||
|
||||
OntProperty propB = tBox.createOntProperty("http://test.vivo/B");
|
||||
tBox.add(propB, RDF.type, objectProperty);
|
||||
propB.setLabel("property B", "en-US");
|
||||
|
||||
OntProperty propC = tBox.createOntProperty("http://test.vivo/C");
|
||||
tBox.add(propC, RDF.type, objectProperty);
|
||||
propC.setLabel("property C", "en-US");
|
||||
|
||||
propA.addEquivalentProperty(propB);
|
||||
propA.addSubProperty(propC);
|
||||
|
||||
// Add a statement that individual x is of type C to the ABox
|
||||
Resource ind_x = aBox.createResource("http://test.vivo/x");
|
||||
Resource ind_y = aBox.createResource("http://test.vivo/y");
|
||||
|
||||
aBox.add(ind_x, propC, ind_y);
|
||||
|
||||
// Verify that "x A y" was inferred
|
||||
Statement xAy = ResourceFactory.createStatement(ind_x, propA, ind_y);
|
||||
Assert.assertTrue(inf.contains(xAy));
|
||||
|
||||
// Verify that "x B y" was inferred
|
||||
Statement xBy = ResourceFactory.createStatement(ind_x, propB, ind_y);
|
||||
Assert.assertTrue(inf.contains(xBy));
|
||||
}
|
||||
|
||||
/*
|
||||
* The sub-property is not of the same type as the super
|
||||
* property so no inference should be materialized.
|
||||
*/
|
||||
|
||||
// @Test uncomment when sub/equiv property inferencing is enabled. sjm222 5/13/2011
|
||||
public void addABoxAssertion5(){
|
||||
|
||||
// Create TBox, ABox and Inference models and register
|
||||
// the ABox reasoner listeners with the ABox and TBox
|
||||
// Pellet will compute TBox inferences
|
||||
|
||||
OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
|
||||
OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
|
||||
Model inf = ModelFactory.createDefaultModel();
|
||||
|
||||
SimpleReasoner simpleReasoner = new SimpleReasoner(tBox, aBox, inf);
|
||||
aBox.register(simpleReasoner);
|
||||
SimpleReasonerTBoxListener simpleReasonerTBoxListener = getTBoxListener(simpleReasoner);
|
||||
tBox.register(simpleReasonerTBoxListener);
|
||||
|
||||
// Add object properties propA and propB to the TBox.
|
||||
// propB is a sub-property of propA.
|
||||
|
||||
OntProperty propA = tBox.createOntProperty("http://test.vivo/propA");
|
||||
tBox.add(propA, RDF.type, objectProperty);
|
||||
propA.setLabel("propA", "en-US");
|
||||
|
||||
OntProperty propB = tBox.createOntProperty("http://test.vivo/propB");
|
||||
Resource datatypeProperty = ResourceFactory.createResource("http://www.w3.org/2002/07/owl#DatatypeProperty");
|
||||
tBox.add(propB, RDF.type, datatypeProperty);
|
||||
propB.setLabel("propB", "en-US");
|
||||
|
||||
propA.addSubProperty(propB);
|
||||
|
||||
// Add the assertion that x propB y to the ABox
|
||||
Resource ind_x = aBox.createResource("http://test.vivo/x");
|
||||
Resource ind_y = aBox.createResource("http://test.vivo/y");
|
||||
aBox.add(ind_x, propB, ind_y);
|
||||
|
||||
// Verify that x propA y was not inferred
|
||||
Statement xay = ResourceFactory.createStatement(ind_x, propA, ind_y);
|
||||
Assert.assertFalse(inf.contains(xay));
|
||||
}
|
||||
|
||||
/*
|
||||
* Test inference based on property equivalence
|
||||
*/
|
||||
// @Test uncomment when sub/equiv property inferencing is enabled. sjm222 5/13/2011
|
||||
public void addABoxAssertion6() {
|
||||
|
||||
// Create TBox, ABox and Inference models and register
|
||||
// the ABox reasoner listeners with the ABox and TBox
|
||||
// Pellet will compute TBox inferences
|
||||
|
||||
OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
|
||||
OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
|
||||
Model inf = ModelFactory.createDefaultModel();
|
||||
|
||||
SimpleReasoner simpleReasoner = new SimpleReasoner(tBox, aBox, inf);
|
||||
aBox.register(simpleReasoner);
|
||||
SimpleReasonerTBoxListener simpleReasonerTBoxListener = getTBoxListener(simpleReasoner);
|
||||
tBox.register(simpleReasonerTBoxListener);
|
||||
|
||||
// Add properties A and B to the TBox
|
||||
// A is equivalent to B
|
||||
|
||||
OntProperty propA = tBox.createOntProperty("http://test.vivo/A");
|
||||
tBox.add(propA, RDF.type, objectProperty);
|
||||
propA.setLabel("property A", "en-US");
|
||||
|
||||
OntProperty propB = tBox.createOntProperty("http://test.vivo/B");
|
||||
tBox.add(propB, RDF.type, objectProperty);
|
||||
propB.setLabel("property B", "en-US");
|
||||
|
||||
propA.addEquivalentProperty(propB);
|
||||
|
||||
// Add the statement x B y to the ABox
|
||||
Resource ind_x = aBox.createResource("http://test.vivo/x");
|
||||
Resource ind_y = aBox.createResource("http://test.vivo/y");
|
||||
aBox.add(ind_x, propB, ind_y);
|
||||
|
||||
// Verify that "x A y" was inferred
|
||||
Statement xAy = ResourceFactory.createStatement(ind_x, propA, ind_y);
|
||||
Assert.assertTrue(inf.contains(xAy));
|
||||
|
||||
// Remove the statement that x B y from the ABox
|
||||
aBox.remove(ind_x, propB, ind_y);
|
||||
|
||||
// Verify that "x is of type A" was removed from the inference graph
|
||||
Assert.assertFalse(inf.contains(xAy));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Test the addition of a subPropertyOf statement to
|
||||
* the TBox. The instance data that is the basis
|
||||
* for the inference is in the ABox.
|
||||
*
|
||||
* Since the addition of an owl:equivalentProperty
|
||||
* statement is implemented as two calls to the
|
||||
* method that handles the addition of an
|
||||
* rdfs:subPropetyOf statement, this test serves
|
||||
* as a test of equivalentProperty assertions also.
|
||||
*/
|
||||
// @Test uncomment when sub/equiv property inferencing is enabled. sjm222 5/13/2011
|
||||
public void addTBoxSubPropertyAssertion1(){
|
||||
|
||||
// Create TBox, ABox and Inference models and register
|
||||
// the ABox reasoner listeners with the ABox and TBox
|
||||
// Pellet will compute TBox inferences
|
||||
|
||||
OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
|
||||
OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
|
||||
Model inf = ModelFactory.createDefaultModel();
|
||||
|
||||
SimpleReasoner simpleReasoner = new SimpleReasoner(tBox, aBox, inf);
|
||||
aBox.register(simpleReasoner);
|
||||
SimpleReasonerTBoxListener simpleReasonerTBoxListener = getTBoxListener(simpleReasoner);
|
||||
tBox.register(simpleReasonerTBoxListener);
|
||||
|
||||
// Add properties A, B, C and D to the TBox
|
||||
OntProperty propA = tBox.createOntProperty("http://test.vivo/A");
|
||||
tBox.add(propA, RDF.type, objectProperty);
|
||||
propA.setLabel("property A", "en-US");
|
||||
|
||||
OntProperty propB = tBox.createOntProperty("http://test.vivo/B");
|
||||
tBox.add(propB, RDF.type, objectProperty);
|
||||
propB.setLabel("property B", "en-US");
|
||||
|
||||
OntProperty propC = tBox.createOntProperty("http://test.vivo/C");
|
||||
tBox.add(propC, RDF.type, objectProperty);
|
||||
propB.setLabel("property C", "en-US");
|
||||
|
||||
OntProperty propD = tBox.createOntProperty("http://test.vivo/D");
|
||||
tBox.add(propD, RDF.type, objectProperty);
|
||||
propB.setLabel("property D", "en-US");
|
||||
|
||||
// Add the statement "x C y" to the ABox
|
||||
Resource ind_x = aBox.createResource("http://test.vivo/x");
|
||||
Resource ind_y = aBox.createResource("http://test.vivo/y");
|
||||
aBox.add(ind_x, propC, ind_y);
|
||||
|
||||
// Add a statement that C is a sub-property of A to the TBox
|
||||
|
||||
propA.addSubProperty(propC);
|
||||
|
||||
// Verify that "x A y" was inferred
|
||||
Statement xAy = ResourceFactory.createStatement(ind_x, propA, ind_y);
|
||||
Assert.assertTrue(inf.contains(xAy));
|
||||
|
||||
// Verify that "x B y" was not inferred
|
||||
Statement xBy = ResourceFactory.createStatement(ind_x, propB, ind_y);
|
||||
Assert.assertFalse(inf.contains(xBy));
|
||||
|
||||
// Verify that "x D y" was not inferred
|
||||
Statement xDy = ResourceFactory.createStatement(ind_x, propD, ind_y);
|
||||
Assert.assertFalse(inf.contains(xDy));
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* Test the removal of a subPropertyOf statement from
|
||||
* the TBox. The instance data that is the basis
|
||||
* for the inference is in the ABox graph and the
|
||||
* inference graph.
|
||||
*
|
||||
* Since the addition of an owl:equivalentProperty
|
||||
* statement is implemented as two calls to the
|
||||
* method that handles the addition of an
|
||||
* rdfs:subPropertyOf statement, this test serves
|
||||
* as a test of equivalentProperty assertions also.
|
||||
*
|
||||
*/
|
||||
// @Test uncomment when sub/equiv property inferencing is enabled. sjm222 5/13/2011
|
||||
public void removeTBoxSubPropertyAssertion1(){
|
||||
// Create TBox, ABox and Inference models and register
|
||||
// the ABox reasoner listeners with the ABox and TBox
|
||||
// Pellet will compute TBox inferences
|
||||
|
||||
OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
|
||||
OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
|
||||
Model inf = ModelFactory.createDefaultModel();
|
||||
|
||||
SimpleReasoner simpleReasoner = new SimpleReasoner(tBox, aBox, inf);
|
||||
aBox.register(simpleReasoner);
|
||||
SimpleReasonerTBoxListener simpleReasonerTBoxListener = getTBoxListener(simpleReasoner);
|
||||
tBox.register(simpleReasonerTBoxListener);
|
||||
|
||||
// Add properties A, B, C, D, E, F, G and H to the TBox.
|
||||
// B, C and D are subproperties of A.
|
||||
// E is a subproperty of B.
|
||||
// F and G are subproperties of C.
|
||||
// H is a subproperty of D.
|
||||
|
||||
OntProperty propA = tBox.createOntProperty("http://test.vivo/A");
|
||||
tBox.add(propA, RDF.type, objectProperty);
|
||||
propA.setLabel("property A", "en-US");
|
||||
|
||||
OntProperty propB = tBox.createOntProperty("http://test.vivo/B");
|
||||
tBox.add(propB, RDF.type, objectProperty);
|
||||
propB.setLabel("property B", "en-US");
|
||||
|
||||
OntProperty propC = tBox.createOntProperty("http://test.vivo/C");
|
||||
tBox.add(propC, RDF.type, objectProperty);
|
||||
propB.setLabel("property C", "en-US");
|
||||
|
||||
OntProperty propD = tBox.createOntProperty("http://test.vivo/D");
|
||||
tBox.add(propD, RDF.type, objectProperty);
|
||||
propB.setLabel("property D", "en-US");
|
||||
|
||||
OntProperty propE = tBox.createOntProperty("http://test.vivo/E");
|
||||
tBox.add(propE, RDF.type, objectProperty);
|
||||
propB.setLabel("property E", "en-US");
|
||||
|
||||
OntProperty propF = tBox.createOntProperty("http://test.vivo/F");
|
||||
tBox.add(propF, RDF.type, objectProperty);
|
||||
propB.setLabel("property F", "en-US");
|
||||
|
||||
OntProperty propG = tBox.createOntProperty("http://test.vivo/G");
|
||||
tBox.add(propG, RDF.type, objectProperty);
|
||||
propB.setLabel("property G", "en-US");
|
||||
|
||||
OntProperty propH = tBox.createOntProperty("http://test.vivo/H");
|
||||
tBox.add(propH, RDF.type, objectProperty);
|
||||
propB.setLabel("property H", "en-US");
|
||||
|
||||
propA.addSubProperty(propB);
|
||||
propA.addSubProperty(propC);
|
||||
propA.addSubProperty(propD);
|
||||
propB.addSubProperty(propE);
|
||||
propC.addSubProperty(propF);
|
||||
propC.addSubProperty(propG);
|
||||
propD.addSubProperty(propH);
|
||||
|
||||
// Add the statement "x E y" to the ABox
|
||||
Resource ind_x = aBox.createResource("http://test.vivo/x");
|
||||
Resource ind_y = aBox.createResource("http://test.vivo/y");
|
||||
aBox.add(ind_x, propE, ind_y);
|
||||
|
||||
// Remove the statement that B is a subproperty of A from the TBox
|
||||
propA.removeSubProperty(propB);
|
||||
|
||||
// Verify that "x A y" is not in the inference graph
|
||||
Statement xAy = ResourceFactory.createStatement(ind_x, propA, ind_y);
|
||||
Assert.assertFalse(inf.contains(xAy));
|
||||
|
||||
// Verify that "x B y" is in the inference graph
|
||||
Statement xBy = ResourceFactory.createStatement(ind_x, propB, ind_y);
|
||||
Assert.assertTrue(inf.contains(xBy));
|
||||
|
||||
// Add statements "w F z" and "w H z" to the ABox
|
||||
Resource ind_z = aBox.createResource("http://test.vivo/z");
|
||||
Resource ind_w = aBox.createResource("http://test.vivo/w");
|
||||
aBox.add(ind_w, propF, ind_z);
|
||||
aBox.add(ind_w, propH, ind_z);
|
||||
|
||||
// Remove the statement that C is a subproperty of A from the TBox
|
||||
propA.removeSubProperty(propC);
|
||||
|
||||
// Verify that "w A z" is in the inference graph
|
||||
Statement wAz = ResourceFactory.createStatement(ind_w, propA, ind_z);
|
||||
Assert.assertTrue(inf.contains(wAz));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Test computation of mostSpecificType annotations in response
|
||||
* to an added/removed ABox type assertion.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue