NIHVIVO-2873 (threads in SimpleReasoner) (not completed yet)

This commit is contained in:
stellamit 2011-07-12 21:40:15 +00:00
parent ac769c7601
commit 756ced4996
5 changed files with 117 additions and 50 deletions

View file

@ -302,7 +302,7 @@ public class IndividualController extends FreemarkerHttpServlet {
}
// If still no custom template defined, and inferencing is asynchronous (under RDB), check
// the superclasses of the vclass for a custom template specification.
if (customTemplate == null && SimpleReasoner.isABoxReasoningAsynchronous(getServletContext())) {
if (customTemplate == null && SimpleReasoner.isSimpleReasonerSetupComplete(getServletContext())) {
log.debug("Checking superclasses for custom template specification because ABox reasoning is asynchronous");
for (VClass directVClass : directClasses) {
VClassDao vcDao = vreq.getWebappDaoFactory().getVClassDao();

View file

@ -32,30 +32,35 @@ public class SimpleReasonerRecomputeController extends FreemarkerHttpServlet {
String messageStr = "";
try {
SimpleReasoner simpleReasoner = SimpleReasoner
.getSimpleReasonerFromServletContext(
vreq.getSession().getServletContext());
if (simpleReasoner == null) {
if (!SimpleReasoner.isSimpleReasonerSetupComplete(vreq.getSession().getServletContext())) {
messageStr = "No SimpleReasoner has been set up.";
} else {
String signal = (String) vreq.getParameter("signal");
if (simpleReasoner.isRecomputing()) {
messageStr =
"The SimpleReasoner is currently in the process of " +
"recomputing inferences.";
} else{
String restart = (String)getServletContext().getAttribute("restart");
if(restart == null || restart.equals("showButton") || signal == null){
body.put("link", "show");
messageStr = null;
getServletContext().setAttribute("restart", "yes");
}
else if(signal!=null && signal.equals("Recompute")){
new Thread(new Recomputer(simpleReasoner)).start();
messageStr = "Recomputation of inferences started";
getServletContext().setAttribute("restart", "showButton");
}
}
Object simpleReasoner = vreq.getSession().getServletContext().getAttribute(SimpleReasoner.class.getName());
if (simpleReasoner instanceof SimpleReasoner) {
if (((SimpleReasoner)simpleReasoner).isRecomputing()) {
messageStr =
"The SimpleReasoner is currently in the process of " +
"recomputing inferences.";
} else{
String restart = (String)getServletContext().getAttribute("restart");
if(restart == null || restart.equals("showButton") || signal == null){
body.put("link", "show");
messageStr = null;
getServletContext().setAttribute("restart", "yes");
}
else if(signal!=null && signal.equals("Recompute")){
new Thread(new Recomputer(((SimpleReasoner)simpleReasoner))).start();
messageStr = "Recomputation of inferences started";
getServletContext().setAttribute("restart", "showButton");
}
}
} else {
log.equals("The attribute with name " + SimpleReasoner.class.getName() + " is not an instance of SimpleReasoner");
}
}
} catch (Exception e) {

View file

@ -63,6 +63,7 @@ public class SimpleReasoner extends StatementListener {
private CumulativeDeltaModeler aBoxDeltaModeler1 = null;
private CumulativeDeltaModeler aBoxDeltaModeler2 = null;
private boolean batchMode1, batchMode2;
private boolean stopRequested;
/**
* @param tboxModel - input. This model contains both asserted and inferred TBox axioms
@ -82,6 +83,7 @@ public class SimpleReasoner extends StatementListener {
this.batchMode2 = false;
aBoxDeltaModeler1 = new CumulativeDeltaModeler();
aBoxDeltaModeler2 = new CumulativeDeltaModeler();
stopRequested = false;
aboxModel.getBaseModel().register(this);
}
@ -103,6 +105,7 @@ public class SimpleReasoner extends StatementListener {
aBoxDeltaModeler2 = new CumulativeDeltaModeler();
this.batchMode1 = false;
this.batchMode2 = false;
stopRequested = false;
}
/*
@ -964,6 +967,11 @@ public class SimpleReasoner extends StatementListener {
if ((numStmts % 8000) == 0) {
log.info("Still computing class-based ABox inferences...");
}
if (stopRequested) {
log.info("a stopRequested signal was received during recomputeABox. Halting Processing.");
return;
}
}
/*
@ -1047,6 +1055,11 @@ public class SimpleReasoner extends StatementListener {
if ((num % 8000) == 0) {
log.info("Still updating ABox inference model (removing outdated inferences)...");
}
if (stopRequested) {
log.info("a stopRequested signal was received during recomputeABox. Halting Processing.");
return;
}
}
} catch (Exception e) {
log.error("Exception while reconciling the current and recomputed ABox inference models", e);
@ -1081,6 +1094,11 @@ public class SimpleReasoner extends StatementListener {
if ((num % 8000) == 0) {
log.info("Still updating ABox inference model (adding new inferences)...");
}
if (stopRequested) {
log.info("a stopRequested signal was received during recomputeABox. Halting Processing.");
return;
}
}
} catch (Exception e) {
log.error("Exception while reconciling the current and recomputed ABox inference models", e);
@ -1133,6 +1151,11 @@ public class SimpleReasoner extends StatementListener {
if ((numStmts % 8000) == 0) {
log.info("Still computing mostSpecificType annotations...");
}
if (stopRequested) {
log.info("a stopRequested signal was received during recomputeMostSpecificType. Halting Processing.");
return;
}
}
} catch (Exception e) {
log.error("Exception while recomputing ABox inference model", e);
@ -1174,6 +1197,11 @@ public class SimpleReasoner extends StatementListener {
if ((numStmts % 8000) == 0) {
log.info("Still updating ABox inference model with mostSpecificType annotations...");
}
if (stopRequested) {
log.info("a stopRequested signal was received during recomputeMostSpecificType. Halting Processing.");
return;
}
}
} catch (Exception e) {
log.error("Exception while reconciling the current and recomputed ABox inference models", e);
@ -1199,19 +1227,15 @@ public class SimpleReasoner extends StatementListener {
log.info("ABox inference model updated with mostSpecificType annotations");
}
public static SimpleReasoner getSimpleReasonerFromServletContext(ServletContext ctx) {
Object simpleReasoner = ctx.getAttribute("simpleReasoner");
public static boolean isSimpleReasonerSetupComplete(ServletContext ctx) {
Object string = ctx.getAttribute("SimpleReasonerSetupState");
if (simpleReasoner instanceof SimpleReasoner) {
return (SimpleReasoner) simpleReasoner;
if (string instanceof String) {
return true;
} else {
return null;
}
return false;
}
}
public static boolean isABoxReasoningAsynchronous(ServletContext ctx) {
return (getSimpleReasonerFromServletContext(ctx) == null);
}
@Override
public synchronized void notifyEvent(Model model, Object event) {
@ -1232,7 +1256,7 @@ public class SimpleReasoner extends StatementListener {
}
} else {
log.info("received BulkUpdateEvent(end)");
new Thread(new DeltaComputer()).start();
new Thread(new DeltaComputer(),"DeltaComputer").start();
}
}
}
@ -1249,7 +1273,7 @@ public class SimpleReasoner extends StatementListener {
boolean finished = (retractions.size() == 0);
String qualifier = "(1)";
while (!finished) {
while (!finished && !stopRequested) {
retractions.enterCriticalSection(Lock.READ);
try {
@ -1272,6 +1296,11 @@ public class SimpleReasoner extends StatementListener {
if ((num % 6000) == 0) {
log.info("still computing inferences for batch " + qualifier + " update...");
}
if (stopRequested) {
log.info("a stopRequested signal was received during DeltaComputer.run. Halting Processing.");
return;
}
}
} finally {
retractions.removeAll();
@ -1298,6 +1327,11 @@ public class SimpleReasoner extends StatementListener {
batchMode2 = false;
log.info("finished processing retractions in batch mode");
}
if (stopRequested) {
log.info("a stopRequested signal was received during DeltaComputer.run. Halting Processing.");
return;
}
}
if (aBoxDeltaModeler1.getRetractions().size() > 0) {
@ -1317,7 +1351,14 @@ public class SimpleReasoner extends StatementListener {
}
}
}
/**
* This is called when the system shuts down.
*/
public void setStopRequested() {
stopRequested = true;
}
public static String stmtString(Statement statement) {
return " [subject = " + statement.getSubject().getURI() +
"] [property = " + statement.getPredicate().getURI() +

View file

@ -96,6 +96,7 @@ public class SimpleReasonerSetup implements ServletContextListener {
// the simple reasoner will register itself as a listener to the ABox assertions
SimpleReasoner simpleReasoner = new SimpleReasoner(unionOms.getTBoxModel(), assertionsOms.getABoxModel(), inferencesOms.getABoxModel(), rebuildModel, scratchModel);
sce.getServletContext().setAttribute(SimpleReasoner.class.getName(),simpleReasoner);
if (isRecomputeRequired(sce.getServletContext())) {
log.info("ABox inference recompute required");
@ -109,7 +110,7 @@ public class SimpleReasonerSetup implements ServletContextListener {
sleeps++;
}
new Thread(new ABoxRecomputer(simpleReasoner,false)).start();
new Thread(new ABoxRecomputer(simpleReasoner),"ABoxRecompuer").start();
} else if ( isMSTComputeRequired(sce.getServletContext()) ) {
log.info("mostSpecificType computation required");
@ -122,12 +123,11 @@ public class SimpleReasonerSetup implements ServletContextListener {
sleeps++;
}
new Thread(new ABoxRecomputer(simpleReasoner,true)).start();
new Thread(new MostSpecificTypeRecomputer(simpleReasoner),"MostSpecificTypeComputer").start();
}
assertionsOms.getTBoxModel().register(new SimpleReasonerTBoxListener(simpleReasoner));
sce.getServletContext().setAttribute("simpleReasoner",simpleReasoner);
sce.getServletContext().setAttribute("SimpleReasonerSetupState","complete");
log.info("Simple reasoner connected for the ABox");
@ -137,8 +137,22 @@ public class SimpleReasonerSetup implements ServletContextListener {
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
// nothing to do
public void contextDestroyed(ServletContextEvent sce) {
SimpleReasoner simpleReasoner = getSimpleReasonerFromServletContext(sce.getServletContext());
if (simpleReasoner != null) {
simpleReasoner.setStopRequested();
}
}
public static SimpleReasoner getSimpleReasonerFromServletContext(ServletContext ctx) {
Object simpleReasoner = ctx.getAttribute(SimpleReasoner.class.getName());
if (simpleReasoner instanceof SimpleReasoner) {
return (SimpleReasoner) simpleReasoner;
} else {
return null;
}
}
private static final String RECOMPUTE_REQUIRED_ATTR =
@ -166,19 +180,26 @@ public class SimpleReasonerSetup implements ServletContextListener {
private class ABoxRecomputer implements Runnable {
private SimpleReasoner simpleReasoner;
private boolean justMST;
public ABoxRecomputer(SimpleReasoner simpleReasoner, boolean justMST) {
public ABoxRecomputer(SimpleReasoner simpleReasoner) {
this.simpleReasoner = simpleReasoner;
this.justMST = justMST;
}
public void run() {
if (justMST) {
simpleReasoner.recomputeMostSpecificType();
} else {
simpleReasoner.recompute();
}
simpleReasoner.recompute();
}
}
private class MostSpecificTypeRecomputer implements Runnable {
private SimpleReasoner simpleReasoner;
public MostSpecificTypeRecomputer(SimpleReasoner simpleReasoner) {
this.simpleReasoner = simpleReasoner;
}
public void run() {
simpleReasoner.recomputeMostSpecificType();
}
}
}

View file

@ -55,7 +55,7 @@ public abstract class BaseIndividualTemplateModel extends BaseTemplateModel {
boolean isVClass = individual.isVClass(vClassUri);
// If reasoning is asynchronous (under RDB), this inference may not have been made yet.
// Check the superclasses of the individual's vclass.
if (!isVClass && SimpleReasoner.isABoxReasoningAsynchronous(getServletContext())) {
if (!isVClass && SimpleReasoner.isSimpleReasonerSetupComplete(getServletContext())) {
log.debug("Checking superclasses to see if individual is a " + vClassUri + " because reasoning is asynchronous");
List<VClass> directVClasses = individual.getVClasses(true);
for (VClass directVClass : directVClasses) {