From f46e4b46f0049c385768160cbd64728db5325766 Mon Sep 17 00:00:00 2001 From: brianjlowe Date: Fri, 22 Jul 2011 19:58:53 +0000 Subject: [PATCH] NIHVIVO-3036 fixed lockup of system when adding superclass of class with many individuals --- .../dao/jena/Classes2ClassesDaoJena.java | 10 +- .../vitro/webapp/reasoner/SimpleReasoner.java | 99 +++++++++---------- 2 files changed, 52 insertions(+), 57 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/Classes2ClassesDaoJena.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/Classes2ClassesDaoJena.java index 830ed4060..1fd062ccd 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/Classes2ClassesDaoJena.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/Classes2ClassesDaoJena.java @@ -30,9 +30,11 @@ public class Classes2ClassesDaoJena extends JenaBaseDao implements Classes2Class deleteClasses2Classes(c2c, getOntModelSelector().getTBoxModel()); } +//TODO restore write locks once reasoner is able to handle these update asynchronously. + public void deleteClasses2Classes( Classes2Classes c2c, OntModel ontModel ) { - ontModel.enterCriticalSection(Lock.WRITE); +// ontModel.enterCriticalSection(Lock.WRITE); getOntModel().getBaseModel().notifyEvent(new EditEvent(getWebappDaoFactory().getUserURI(),true)); try { OntResource subclass = getOntClass(ontModel,c2c.getSubclassURI()); @@ -48,7 +50,7 @@ public class Classes2ClassesDaoJena extends JenaBaseDao implements Classes2Class } } finally { getOntModel().getBaseModel().notifyEvent(new EditEvent(getWebappDaoFactory().getUserURI(),false)); - ontModel.leaveCriticalSection(); +// ontModel.leaveCriticalSection(); } } @@ -58,7 +60,7 @@ public class Classes2ClassesDaoJena extends JenaBaseDao implements Classes2Class public void insertNewClasses2Classes( Classes2Classes c2c, OntModel ontModel ) { - ontModel.enterCriticalSection(Lock.WRITE); +// ontModel.enterCriticalSection(Lock.WRITE); getOntModel().getBaseModel().notifyEvent(new EditEvent(getWebappDaoFactory().getUserURI(),true)); try { Resource subclass = ontModel.getResource(c2c.getSubclassURI()); @@ -68,7 +70,7 @@ public class Classes2ClassesDaoJena extends JenaBaseDao implements Classes2Class } } finally { getOntModel().getBaseModel().notifyEvent(new EditEvent(getWebappDaoFactory().getUserURI(),false)); - ontModel.leaveCriticalSection(); +// ontModel.leaveCriticalSection(); } } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasoner.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasoner.java index c80cc8d1d..364f0c653 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasoner.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasoner.java @@ -602,35 +602,33 @@ public class SimpleReasoner extends StatementListener { * inferred model, assert that it is of type A. */ public void addedSubClass(OntClass subClass, OntClass superClass, Model inferenceModel) { - log.debug("subClass = " + subClass.getURI() + " superClass = " + superClass.getURI()); - - aboxModel.enterCriticalSection(Lock.WRITE); - inferenceModel.enterCriticalSection(Lock.WRITE); - + OntModel unionModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM); + unionModel.addSubModel(aboxModel); + unionModel.addSubModel(inferenceModel); + List subjectList = new ArrayList(); + aboxModel.enterCriticalSection(Lock.READ); try { - OntModel unionModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM); - unionModel.addSubModel(aboxModel); - unionModel.addSubModel(inferenceModel); - StmtIterator iter = unionModel.listStatements((Resource) null, RDF.type, subClass); - while (iter.hasNext()) { - Statement stmt = iter.next(); - Statement infStmt = ResourceFactory.createStatement(stmt.getSubject(), RDF.type, superClass); - - inferenceModel.enterCriticalSection(Lock.WRITE); - - if (!inferenceModel.contains(infStmt)) { - inferenceModel.add(infStmt); - setMostSpecificTypes(infStmt.getSubject(), inferenceModel, new HashSet()); - } - } + subjectList.add(stmt.getSubject()); + } } finally { aboxModel.leaveCriticalSection(); - inferenceModel.leaveCriticalSection(); } + for (Resource subject : subjectList) { + Statement infStmt = ResourceFactory.createStatement(subject, RDF.type, superClass); + inferenceModel.enterCriticalSection(Lock.WRITE); + try { + if (!inferenceModel.contains(infStmt)) { + inferenceModel.add(infStmt); + setMostSpecificTypes(subject, inferenceModel, new HashSet()); + } + } finally { + inferenceModel.leaveCriticalSection(); + } + } } /* @@ -641,38 +639,33 @@ public class SimpleReasoner extends StatementListener { * of A (including A itself) */ public void removedSubClass(OntClass subClass, OntClass superClass, Model inferenceModel) { - log.debug("subClass = " + subClass.getURI() + ". superClass = " + superClass.getURI()); - - aboxModel.enterCriticalSection(Lock.WRITE); - inferenceModel.enterCriticalSection(Lock.WRITE); - - try { - OntModel unionModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM); - unionModel.addSubModel(aboxModel); - unionModel.addSubModel(inferenceModel); - - StmtIterator iter = unionModel.listStatements((Resource) null, RDF.type, subClass); - - while (iter.hasNext()) { - - Statement stmt = iter.next(); - Resource ind = stmt.getSubject(); - - if (entailedType(ind,superClass)) continue; - - Statement infStmt = ResourceFactory.createStatement(ind, RDF.type, superClass); - - inferenceModel.enterCriticalSection(Lock.WRITE); - - if (inferenceModel.contains(infStmt)) { - inferenceModel.remove(infStmt); - setMostSpecificTypes(infStmt.getSubject(), inferenceModel, new HashSet()); - } - } - } finally { - aboxModel.leaveCriticalSection(); - inferenceModel.leaveCriticalSection(); + OntModel unionModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM); + unionModel.addSubModel(aboxModel); + unionModel.addSubModel(inferenceModel); + List subjectList = new ArrayList(); + aboxModel.enterCriticalSection(Lock.READ); + try { + StmtIterator iter = unionModel.listStatements((Resource) null, RDF.type, subClass); + while (iter.hasNext()) { + Statement stmt = iter.next(); + subjectList.add(stmt.getSubject()); + } + } finally { + aboxModel.leaveCriticalSection(); + } + for (Resource ind : subjectList) { + if (entailedType(ind,superClass)) continue; + Statement infStmt = ResourceFactory.createStatement(ind, RDF.type, superClass); + inferenceModel.enterCriticalSection(Lock.WRITE); + try { + if (inferenceModel.contains(infStmt)) { + inferenceModel.remove(infStmt); + setMostSpecificTypes(infStmt.getSubject(), inferenceModel, new HashSet()); + } + } finally { + inferenceModel.leaveCriticalSection(); + } } } @@ -1414,4 +1407,4 @@ public class SimpleReasoner extends StatementListener { "] [object = " + (statement.getObject().isLiteral() ? ((Literal)statement.getObject()).getLexicalForm() + " (Literal)" : ((Resource)statement.getObject()).getURI() + " (Resource)") + "]"; } -} \ No newline at end of file +}