[VIVO-1031] Minor improvements to remove RDFServiceGraph usage from reinferencer.

This commit is contained in:
grahamtriggs 2015-10-31 14:13:44 +00:00
parent 5d6f4dada7
commit 1832bba831

View file

@ -57,7 +57,6 @@ public class ABoxRecomputer {
private OntModel tboxModel; // asserted and inferred TBox axioms private OntModel tboxModel; // asserted and inferred TBox axioms
private OntModel aboxModel; private OntModel aboxModel;
private Model inferenceModel;
private RDFService rdfService; private RDFService rdfService;
private SimpleReasoner simpleReasoner; private SimpleReasoner simpleReasoner;
private Object lock1 = new Object(); private Object lock1 = new Object();
@ -70,7 +69,6 @@ public class ABoxRecomputer {
/** /**
* @param tboxModel - input. This model contains both asserted and inferred TBox axioms * @param tboxModel - input. This model contains both asserted and inferred TBox axioms
* @param aboxModel - input. This model contains asserted ABox statements * @param aboxModel - input. This model contains asserted ABox statements
* @param inferenceModel - output. This is the model in which inferred (materialized) ABox statements are maintained (added or retracted).
*/ */
public ABoxRecomputer(OntModel tboxModel, public ABoxRecomputer(OntModel tboxModel,
OntModel aboxModel, OntModel aboxModel,
@ -80,8 +78,6 @@ public class ABoxRecomputer {
this.tboxModel = tboxModel; this.tboxModel = tboxModel;
this.aboxModel = aboxModel; this.aboxModel = aboxModel;
this.rdfService = rdfService; this.rdfService = rdfService;
this.inferenceModel = RDFServiceGraph.createRDFServiceModel(
new RDFServiceGraph(rdfService, ModelNames.ABOX_INFERENCES));
this.simpleReasoner = simpleReasoner; this.simpleReasoner = simpleReasoner;
this.searchIndexer = searchIndexer; this.searchIndexer = searchIndexer;
recomputing = false; recomputing = false;
@ -377,10 +373,10 @@ public class ABoxRecomputer {
" UNION \n" + " UNION \n" +
" { ?inv <" + OWL.inverseOf.getURI() + "> ?prop } \n" + " { ?inv <" + OWL.inverseOf.getURI() + "> ?prop } \n" +
"} \n"; "} \n";
return RDFServiceUtils.parseModel(
rdfService.sparqlConstructQuery( Model model = ModelFactory.createDefaultModel();
queryStr, RDFService.ModelSerializationFormat.N3) rdfService.sparqlConstructQuery(queryStr, model);
, RDFService.ModelSerializationFormat.N3); return model;
} }
private Model rewriteInferences(Model inferences, String aliasURI) { private Model rewriteInferences(Model inferences, String aliasURI) {
@ -460,6 +456,23 @@ public class ABoxRecomputer {
} }
protected void addInferenceStatementsFor(String individualUri, Model addTo) throws RDFServiceException {
StringBuilder builder = new StringBuilder();
builder.append("CONSTRUCT\n")
.append("{\n")
.append(" <" + individualUri + "> ?p ?o .\n")
.append("}\n")
.append("WHERE\n")
.append("{\n")
.append(" GRAPH <").append(ModelNames.ABOX_INFERENCES).append(">\n")
.append(" {\n")
.append(" <" + individualUri + "> ?p ?o .\n")
.append(" }\n")
.append("}\n");
rdfService.sparqlConstructQuery(builder.toString(), addTo);
}
/* /*
* reconcile a set of inferences into the application inference model * reconcile a set of inferences into the application inference model
*/ */
@ -467,22 +480,27 @@ public class ABoxRecomputer {
Collection<String> individuals) throws RDFServiceException { Collection<String> individuals) throws RDFServiceException {
Model existing = ModelFactory.createDefaultModel(); Model existing = ModelFactory.createDefaultModel();
for (String individualURI : individuals) { for (String individualURI : individuals) {
Resource subjInd = ResourceFactory.createResource(individualURI); addInferenceStatementsFor(individualURI, existing);
existing.add(inferenceModel.listStatements(subjInd, null, (RDFNode) null));
} }
Model retractions = existing.difference(rebuildModel); Model retractions = existing.difference(rebuildModel);
Model additions = rebuildModel.difference(existing); Model additions = rebuildModel.difference(existing);
if (additions.size() > 0 || retractions.size() > 0) {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
ChangeSet change = rdfService.manufactureChangeSet(); ChangeSet change = rdfService.manufactureChangeSet();
if (retractions.size() > 0) {
change.addRemoval(makeN3InputStream(retractions), change.addRemoval(makeN3InputStream(retractions),
RDFService.ModelSerializationFormat.N3, ModelNames.ABOX_INFERENCES); RDFService.ModelSerializationFormat.N3, ModelNames.ABOX_INFERENCES);
}
if (additions.size() > 0) {
change.addAddition(makeN3InputStream(additions), change.addAddition(makeN3InputStream(additions),
RDFService.ModelSerializationFormat.N3, ModelNames.ABOX_INFERENCES); RDFService.ModelSerializationFormat.N3, ModelNames.ABOX_INFERENCES);
}
rdfService.changeSetUpdate(change); rdfService.changeSetUpdate(change);
log.debug((System.currentTimeMillis() - start) + log.debug((System.currentTimeMillis() - start) +
" ms to retract " + retractions.size() + " ms to retract " + retractions.size() +
" statements and add " + additions.size() + " statements"); " statements and add " + additions.size() + " statements");
} }
}
private InputStream makeN3InputStream(Model m) { private InputStream makeN3InputStream(Model m) {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();