merging from branch

This commit is contained in:
stellamit 2011-07-22 20:51:28 +00:00
parent f46e4b46f0
commit 283ab767d9
2 changed files with 163 additions and 82 deletions

View file

@ -55,7 +55,7 @@ public class IndividualTypeOperationController extends BaseEditController {
return; return;
} }
IndividualDao dao = request.getFullWebappDaoFactory().getIndividualDao(); IndividualDao dao = request.getAssertionsWebappDaoFactory().getIndividualDao();
if (request.getParameter("_cancel") == null) { if (request.getParameter("_cancel") == null) {
try { try {

View file

@ -15,6 +15,13 @@ import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel; import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec; import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.ontology.OntProperty; import com.hp.hpl.jena.ontology.OntProperty;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.Syntax;
import com.hp.hpl.jena.rdf.listeners.StatementListener; import com.hp.hpl.jena.rdf.listeners.StatementListener;
import com.hp.hpl.jena.rdf.model.Literal; import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.Model;
@ -302,6 +309,27 @@ public class SimpleReasoner extends StatementListener {
} }
} }
/*
*
*/
public void addedABoxTypeAssertion(Resource individual, Model inferenceModel, HashSet<String> unknownTypes) {
StmtIterator iter = null;
aboxModel.enterCriticalSection(Lock.READ);
try {
iter = aboxModel.listStatements(individual, RDF.type, (RDFNode) null);
while (iter.hasNext()) {
Statement stmt = iter.next();
addedABoxTypeAssertion(stmt, inferenceModel, unknownTypes);
}
} finally {
iter.close();
aboxModel.leaveCriticalSection();
}
}
/* /*
* Performs incremental reasoning based on a new type assertion * Performs incremental reasoning based on a new type assertion
* added to the ABox (assertion that an individual is of a certain * added to the ABox (assertion that an individual is of a certain
@ -555,10 +583,15 @@ public class SimpleReasoner extends StatementListener {
tboxModel.enterCriticalSection(Lock.READ); tboxModel.enterCriticalSection(Lock.READ);
try { try {
ExtendedIterator<OntClass> iter = cls.listSubClasses(false); List<OntClass> subclasses = null;
subclasses = (cls.listSubClasses(false)).toList();
subclasses.addAll((cls.listEquivalentClasses()).toList());
Iterator<OntClass> iter = subclasses.iterator();
while (iter.hasNext()) { while (iter.hasNext()) {
OntClass childClass = iter.next(); OntClass childClass = iter.next();
if (childClass.equals(cls)) break;
Statement stmt = ResourceFactory.createStatement(subject, RDF.type, childClass); Statement stmt = ResourceFactory.createStatement(subject, RDF.type, childClass);
if (aboxModel.contains(stmt)) return true; if (aboxModel.contains(stmt)) return true;
} }
@ -939,23 +972,20 @@ public class SimpleReasoner extends StatementListener {
// recompute the inferences // recompute the inferences
inferenceRebuildModel.enterCriticalSection(Lock.WRITE); inferenceRebuildModel.enterCriticalSection(Lock.WRITE);
aboxModel.enterCriticalSection(Lock.WRITE);
tboxModel.enterCriticalSection(Lock.READ);
StmtIterator iter = null;
try { try {
log.info("Computing class-based ABox inferences."); log.info("Computing class-based ABox inferences.");
inferenceRebuildModel.removeAll(); inferenceRebuildModel.removeAll();
iter = aboxModel.listStatements((Resource) null, RDF.type, (RDFNode) null);
int numStmts = 0; int numStmts = 0;
ArrayList<String> individuals = this.getIndividualURIs();
for (String individualURI : individuals) {
Resource individual = ResourceFactory.createResource(individualURI);
while (iter.hasNext()) {
Statement stmt = iter.next();
try { try {
addedABoxTypeAssertion(stmt, inferenceRebuildModel, unknownTypes); addedABoxTypeAssertion(individual, inferenceRebuildModel, unknownTypes);
setMostSpecificTypes(stmt.getSubject(), inferenceRebuildModel, unknownTypes); setMostSpecificTypes(individual, inferenceRebuildModel, unknownTypes);
} catch (NullPointerException npe) { } catch (NullPointerException npe) {
log.error("a NullPointerException was received while recomputing the ABox inferences. Halting inference computation."); log.error("a NullPointerException was received while recomputing the ABox inferences. Halting inference computation.");
return; return;
@ -970,7 +1000,7 @@ public class SimpleReasoner extends StatementListener {
} }
numStmts++; numStmts++;
if ((numStmts % 8000) == 0) { if ((numStmts % 10000) == 0) {
log.info("Still computing class-based ABox inferences..."); log.info("Still computing class-based ABox inferences...");
} }
@ -1029,9 +1059,6 @@ public class SimpleReasoner extends StatementListener {
// where there isn't an exception // where there isn't an exception
return; return;
} finally { } finally {
iter.close();
aboxModel.leaveCriticalSection();
tboxModel.leaveCriticalSection();
inferenceRebuildModel.leaveCriticalSection(); inferenceRebuildModel.leaveCriticalSection();
} }
@ -1039,15 +1066,16 @@ public class SimpleReasoner extends StatementListener {
// reflect the recomputed inferences into the application inference // reflect the recomputed inferences into the application inference
// model. // model.
inferenceRebuildModel.enterCriticalSection(Lock.WRITE); log.info("Updating ABox inference model");
scratchpadModel.enterCriticalSection(Lock.WRITE); StmtIterator iter = null;
log.info("Updating ABox inference model");
// Remove everything from the current inference model that is not // Remove everything from the current inference model that is not
// in the recomputed inference model // in the recomputed inference model
int num = 0; int num = 0;
inferenceRebuildModel.enterCriticalSection(Lock.WRITE);
scratchpadModel.enterCriticalSection(Lock.WRITE);
try { try {
inferenceModel.enterCriticalSection(Lock.READ); inferenceModel.enterCriticalSection(Lock.READ);
try { try {
scratchpadModel.removeAll(); scratchpadModel.removeAll();
iter = inferenceModel.listStatements(); iter = inferenceModel.listStatements();
@ -1059,7 +1087,7 @@ public class SimpleReasoner extends StatementListener {
} }
num++; num++;
if ((num % 8000) == 0) { if ((num % 10000) == 0) {
log.info("Still updating ABox inference model (removing outdated inferences)..."); log.info("Still updating ABox inference model (removing outdated inferences)...");
} }
@ -1070,37 +1098,46 @@ public class SimpleReasoner extends StatementListener {
} }
} catch (Exception e) { } catch (Exception e) {
log.error("Exception while reconciling the current and recomputed ABox inference models", e); log.error("Exception while reconciling the current and recomputed ABox inference models", e);
return;
} finally { } finally {
iter.close(); iter.close();
inferenceModel.leaveCriticalSection(); inferenceModel.leaveCriticalSection();
} }
inferenceModel.enterCriticalSection(Lock.WRITE); iter = scratchpadModel.listStatements();
try { while (iter.hasNext()) {
inferenceModel.remove(scratchpadModel); Statement stmt = iter.next();
} catch (Exception e){
log.error("Exception while reconciling the current and recomputed ABox inference models", e); inferenceModel.enterCriticalSection(Lock.WRITE);
return; try {
} finally { inferenceModel.remove(stmt);
inferenceModel.leaveCriticalSection(); } catch (Exception e) {
log.error("Exception while reconciling the current and recomputed ABox inference models", e);
} finally {
inferenceModel.leaveCriticalSection();
}
} }
// Add everything from the recomputed inference model that is not already // Add everything from the recomputed inference model that is not already
// in the current inference model to the current inference model. // in the current inference model to the current inference model.
inferenceModel.enterCriticalSection(Lock.READ);
try { try {
scratchpadModel.removeAll(); scratchpadModel.removeAll();
iter = inferenceRebuildModel.listStatements(); iter = inferenceRebuildModel.listStatements();
while (iter.hasNext()) { while (iter.hasNext()) {
Statement stmt = iter.next(); Statement stmt = iter.next();
if (!inferenceModel.contains(stmt)) {
scratchpadModel.add(stmt); inferenceModel.enterCriticalSection(Lock.READ);
try {
if (!inferenceModel.contains(stmt)) {
scratchpadModel.add(stmt);
}
} finally {
inferenceModel.leaveCriticalSection();
} }
num++; num++;
if ((num % 8000) == 0) { if ((num % 10000) == 0) {
log.info("Still updating ABox inference model (adding new inferences)..."); log.info("Still updating ABox inference model (adding new inferences)...");
} }
@ -1114,19 +1151,24 @@ public class SimpleReasoner extends StatementListener {
return; return;
} finally { } finally {
iter.close(); iter.close();
inferenceModel.leaveCriticalSection();
} }
inferenceModel.enterCriticalSection(Lock.WRITE); iter = scratchpadModel.listStatements();
try { while (iter.hasNext()) {
inferenceModel.add(scratchpadModel); Statement stmt = iter.next();
} catch (Exception e){
log.error("Exception while reconciling the current and recomputed ABox inference models", e); inferenceModel.enterCriticalSection(Lock.WRITE);
return; try {
} finally { inferenceModel.add(stmt);
inferenceModel.leaveCriticalSection(); } catch (Exception e) {
log.error("Exception while reconciling the current and recomputed ABox inference models", e);
return;
} finally {
inferenceModel.leaveCriticalSection();
}
} }
} finally { } finally {
iter.close();
inferenceRebuildModel.removeAll(); inferenceRebuildModel.removeAll();
scratchpadModel.removeAll(); scratchpadModel.removeAll();
inferenceRebuildModel.leaveCriticalSection(); inferenceRebuildModel.leaveCriticalSection();
@ -1141,27 +1183,24 @@ public class SimpleReasoner extends StatementListener {
*/ */
public synchronized void computeMostSpecificType() { public synchronized void computeMostSpecificType() {
log.info("Computing mostSpecificType annotations.");
HashSet<String> unknownTypes = new HashSet<String>(); HashSet<String> unknownTypes = new HashSet<String>();
// recompute the inferences // recompute the inferences
inferenceRebuildModel.enterCriticalSection(Lock.WRITE); inferenceRebuildModel.enterCriticalSection(Lock.WRITE);
aboxModel.enterCriticalSection(Lock.WRITE);
tboxModel.enterCriticalSection(Lock.READ);
StmtIterator iter = null;
try { try {
inferenceRebuildModel.removeAll(); inferenceRebuildModel.removeAll();
iter = aboxModel.listStatements((Resource) null, RDF.type, (RDFNode) null);
log.info("Computing mostSpecificType annotations."); ArrayList<String> individuals = this.getIndividualURIs();
int numStmts = 0; int numStmts = 0;
for (String individualURI : individuals ) {
while (iter.hasNext()) { Resource individual = ResourceFactory.createResource(individualURI);
Statement stmt = iter.next();
try { try {
setMostSpecificTypes(stmt.getSubject(), inferenceRebuildModel, unknownTypes); setMostSpecificTypes(individual, inferenceRebuildModel, unknownTypes);
} catch (NullPointerException npe) { } catch (NullPointerException npe) {
log.error("a NullPointerException was received while computing mostSpecificType annotations. Halting inference computation."); log.error("a NullPointerException was received while computing mostSpecificType annotations. Halting inference computation.");
return; return;
@ -1176,7 +1215,7 @@ public class SimpleReasoner extends StatementListener {
} }
numStmts++; numStmts++;
if ((numStmts % 8000) == 0) { if ((numStmts % 10000) == 0) {
log.info("Still computing mostSpecificType annotations..."); log.info("Still computing mostSpecificType annotations...");
} }
@ -1191,9 +1230,6 @@ public class SimpleReasoner extends StatementListener {
// where there isn't an exception // where there isn't an exception
return; return;
} finally { } finally {
iter.close();
aboxModel.leaveCriticalSection();
tboxModel.leaveCriticalSection();
inferenceRebuildModel.leaveCriticalSection(); inferenceRebuildModel.leaveCriticalSection();
} }
@ -1201,15 +1237,15 @@ public class SimpleReasoner extends StatementListener {
// reflect the recomputed inferences into the application inference // reflect the recomputed inferences into the application inference
// model. // model.
inferenceRebuildModel.enterCriticalSection(Lock.WRITE);
scratchpadModel.enterCriticalSection(Lock.WRITE);
log.info("Updating ABox inference model with mostSpecificType annotations"); log.info("Updating ABox inference model with mostSpecificType annotations");
StmtIterator iter = null;
inferenceRebuildModel.enterCriticalSection(Lock.WRITE);
scratchpadModel.enterCriticalSection(Lock.WRITE);
try { try {
// Add everything from the recomputed inference model that is not already // Add everything from the recomputed inference model that is not already
// in the current inference model to the current inference model. // in the current inference model to the current inference model.
inferenceModel.enterCriticalSection(Lock.READ);
try { try {
scratchpadModel.removeAll(); scratchpadModel.removeAll();
iter = inferenceRebuildModel.listStatements(); iter = inferenceRebuildModel.listStatements();
@ -1218,12 +1254,18 @@ public class SimpleReasoner extends StatementListener {
while (iter.hasNext()) { while (iter.hasNext()) {
Statement stmt = iter.next(); Statement stmt = iter.next();
if (!inferenceModel.contains(stmt)) {
scratchpadModel.add(stmt); inferenceModel.enterCriticalSection(Lock.READ);
try {
if (!inferenceModel.contains(stmt)) {
scratchpadModel.add(stmt);
}
} finally {
inferenceModel.leaveCriticalSection();
} }
numStmts++; numStmts++;
if ((numStmts % 8000) == 0) { if ((numStmts % 10000) == 0) {
log.info("Still updating ABox inference model with mostSpecificType annotations..."); log.info("Still updating ABox inference model with mostSpecificType annotations...");
} }
@ -1237,17 +1279,21 @@ public class SimpleReasoner extends StatementListener {
return; return;
} finally { } finally {
iter.close(); iter.close();
inferenceModel.leaveCriticalSection();
} }
inferenceModel.enterCriticalSection(Lock.WRITE); iter = scratchpadModel.listStatements();
try { while (iter.hasNext()) {
inferenceModel.add(scratchpadModel); Statement stmt = iter.next();
} catch (Exception e){
log.error("Exception while reconciling the current and recomputed ABox inference models", e); inferenceModel.enterCriticalSection(Lock.WRITE);
return; try {
} finally { inferenceModel.add(stmt);
inferenceModel.leaveCriticalSection(); } catch (Exception e) {
log.error("Exception while reconciling the current and recomputed ABox inference models", e);
return;
} finally {
inferenceModel.leaveCriticalSection();
}
} }
} finally { } finally {
inferenceRebuildModel.removeAll(); inferenceRebuildModel.removeAll();
@ -1394,6 +1440,41 @@ public class SimpleReasoner extends StatementListener {
} }
} }
/**
*
*/
public ArrayList<String> getIndividualURIs() {
String queryString = "select distinct ?subject where {?subject <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?type}";
ArrayList<String> individuals = new ArrayList<String>();
aboxModel.enterCriticalSection(Lock.READ);
try {
try {
Query query = QueryFactory.create(queryString, Syntax.syntaxARQ);
QueryExecution qe = QueryExecutionFactory.create(query, aboxModel);
ResultSet results = qe.execSelect();
while (results.hasNext()) {
QuerySolution solution = results.next();
Resource resource = solution.getResource("subject");
if ((resource != null) && !resource.isAnon()) {
individuals.add(resource.getURI());
}
}
} catch (Exception e) {
log.error("exception while retrieving list of individuals ",e);
}
} finally {
aboxModel.leaveCriticalSection();
}
return individuals;
}
/** /**
* This is called when the system shuts down. * This is called when the system shuts down.
*/ */