From 85b9067eee63cce322e3e1eeb2a417c8bd82d226 Mon Sep 17 00:00:00 2001 From: Jim Blake Date: Fri, 21 Nov 2014 14:04:01 -0500 Subject: [PATCH] VIVO-778 Separate PelletReasonerSetup from SimpleReasonerSetup. --- .../webapp/servlet/setup/FileGraphSetup.java | 7 -- .../servlet/setup/PelletReasonerSetup.java | 106 ++++++++++++++++++ .../servlet/setup/SimpleReasonerSetup.java | 67 +---------- .../servlet/setup/UpdateKnowledgeBase.java | 2 +- .../WEB-INF/resources/startup_listeners.txt | 1 + 5 files changed, 110 insertions(+), 73 deletions(-) create mode 100644 webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/PelletReasonerSetup.java diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/FileGraphSetup.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/FileGraphSetup.java index 4fbd5d8c2..8e4cc757c 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/FileGraphSetup.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/FileGraphSetup.java @@ -97,13 +97,6 @@ public class FileGraphSetup implements ServletContextListener { OntDocumentManager.getInstance().setProcessImports(false); } - /* - if (isUpdateRequired(ctx)) { - log.info("mostSpecificType will be computed because a knowledge base migration was performed." ); - SimpleReasonerSetup.setMSTComputeRequired(ctx); - } else - */ - if ( (aboxChanged || tboxChanged) && !isUpdateRequired(ctx)) { log.info("a full recompute of the Abox will be performed because" + " the filegraph abox(s) and/or tbox(s) have changed or are being read for the first time." ); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/PelletReasonerSetup.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/PelletReasonerSetup.java new file mode 100644 index 000000000..476717121 --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/PelletReasonerSetup.java @@ -0,0 +1,106 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.servlet.setup; + +import static edu.cornell.mannlib.vitro.webapp.modelaccess.ModelNames.TBOX_ASSERTIONS; +import static edu.cornell.mannlib.vitro.webapp.modelaccess.ModelNames.TBOX_INFERENCES; +import static edu.cornell.mannlib.vitro.webapp.modelaccess.ModelNames.TBOX_UNION; + +import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import com.hp.hpl.jena.ontology.OntModel; +import com.hp.hpl.jena.vocabulary.OWL; + +import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; +import edu.cornell.mannlib.vitro.webapp.dao.jena.WebappDaoFactoryJena; +import edu.cornell.mannlib.vitro.webapp.dao.jena.pellet.PelletListener; +import edu.cornell.mannlib.vitro.webapp.modelaccess.ContextModelAccess; +import edu.cornell.mannlib.vitro.webapp.modelaccess.ModelAccess; +import edu.cornell.mannlib.vitro.webapp.startup.StartupStatus; +import edu.cornell.mannlib.vitro.webapp.tboxreasoner.ReasonerConfiguration; + +/** + * Start the Pellet reasoner on the TBox. + */ +public class PelletReasonerSetup implements ServletContextListener { + private static final Log log = LogFactory.getLog(PelletReasonerSetup.class); + + @Override + public void contextInitialized(ServletContextEvent sce) { + ServletContext ctx = sce.getServletContext(); + StartupStatus ss = StartupStatus.getBean(ctx); + + ContextModelAccess contextModels = ModelAccess.on(ctx); + OntModel tboxAssertionsModel = contextModels + .getOntModel(TBOX_ASSERTIONS); + OntModel tboxInferencesModel = contextModels + .getOntModel(TBOX_INFERENCES); + OntModel tboxUnionModel = contextModels.getOntModel(TBOX_UNION); + WebappDaoFactory wadf = contextModels.getWebappDaoFactory(); + + if (!tboxAssertionsModel.getProfile().NAMESPACE() + .equals(OWL.NAMESPACE.getNameSpace())) { + ss.fatal(this, "Not connecting Pellet reasoner " + + "- the TBox assertions model is not an OWL model"); + return; + } + + // Set various Pellet options for incremental consistency checking, etc. + // PelletOptions.DL_SAFE_RULES = true; + // PelletOptions.USE_COMPLETION_QUEUE = true; + // PelletOptions.USE_TRACING = true; + // PelletOptions.TRACK_BRANCH_EFFECTS = true; + // PelletOptions.USE_INCREMENTAL_CONSISTENCY = true; + // PelletOptions.USE_INCREMENTAL_DELETION = true; + + PelletListener pelletListener = new PelletListener(tboxUnionModel, + tboxAssertionsModel, tboxInferencesModel, + ReasonerConfiguration.DEFAULT); + sce.getServletContext().setAttribute("pelletListener", pelletListener); + sce.getServletContext().setAttribute("pelletOntModel", + pelletListener.getPelletModel()); + + if (wadf instanceof WebappDaoFactoryJena) { + ((WebappDaoFactoryJena) wadf).setPelletListener(pelletListener); + } + + ss.info(this, "Pellet reasoner connected for the TBox"); + + waitForTBoxReasoning(sce); + } + + public static void waitForTBoxReasoning(ServletContextEvent sce) { + PelletListener pelletListener = (PelletListener) sce + .getServletContext().getAttribute("pelletListener"); + if (pelletListener == null) { + return; + } + int sleeps = 0; + // sleep at least once to make sure the TBox reasoning gets started + while ((0 == sleeps) + || ((sleeps < 1000) && pelletListener.isReasoning())) { + if (((sleeps - 1) % 10) == 0) { // print message at 10 second + // intervals + log.info("Waiting for initial TBox reasoning to complete"); + } + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // This should never happen. + e.printStackTrace(); + } + sleeps++; + } + } + + @Override + public void contextDestroyed(ServletContextEvent sce) { + // Nothing to tear down + } + +} diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/SimpleReasonerSetup.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/SimpleReasonerSetup.java index 48a2fb1e2..3422eccff 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/SimpleReasonerSetup.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/SimpleReasonerSetup.java @@ -21,11 +21,7 @@ import org.apache.commons.logging.LogFactory; import com.hp.hpl.jena.ontology.OntModel; import com.hp.hpl.jena.query.Dataset; import com.hp.hpl.jena.rdf.model.Model; -import com.hp.hpl.jena.vocabulary.OWL; -import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; -import edu.cornell.mannlib.vitro.webapp.dao.jena.WebappDaoFactoryJena; -import edu.cornell.mannlib.vitro.webapp.dao.jena.pellet.PelletListener; import edu.cornell.mannlib.vitro.webapp.modelaccess.ModelAccess; import edu.cornell.mannlib.vitro.webapp.modelaccess.ModelNames; import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService; @@ -33,7 +29,6 @@ import edu.cornell.mannlib.vitro.webapp.reasoner.ReasonerPlugin; import edu.cornell.mannlib.vitro.webapp.reasoner.SimpleReasoner; import edu.cornell.mannlib.vitro.webapp.reasoner.SimpleReasonerTBoxListener; import edu.cornell.mannlib.vitro.webapp.startup.StartupStatus; -import edu.cornell.mannlib.vitro.webapp.tboxreasoner.ReasonerConfiguration; import edu.cornell.mannlib.vitro.webapp.utils.threads.VitroBackgroundThread; public class SimpleReasonerSetup implements ServletContextListener { @@ -51,41 +46,10 @@ public class SimpleReasonerSetup implements ServletContextListener { ServletContext ctx = sce.getServletContext(); try { - // set up Pellet reasoning for the TBox OntModel tboxAssertionsModel = ModelAccess.on(ctx).getOntModel(ModelNames.TBOX_ASSERTIONS); OntModel tboxInferencesModel = ModelAccess.on(ctx).getOntModel(ModelNames.TBOX_INFERENCES); OntModel tboxUnionModel = ModelAccess.on(ctx).getOntModel(ModelNames.TBOX_UNION); - log.debug("tboxAssertionsModel=" + tboxAssertionsModel); - log.debug("tboxInferencesModel=" + tboxInferencesModel); - log.debug("tboxUnionModel=" + tboxUnionModel); - WebappDaoFactory wadf = ModelAccess.on(ctx).getWebappDaoFactory(); - - if (!tboxAssertionsModel.getProfile().NAMESPACE().equals(OWL.NAMESPACE.getNameSpace())) { - log.error("Not connecting Pellet reasoner - the TBox assertions model is not an OWL model"); - return; - } - - // Set various Pellet options for incremental consistency checking, etc. - //PelletOptions.DL_SAFE_RULES = true; - //PelletOptions.USE_COMPLETION_QUEUE = true; - //PelletOptions.USE_TRACING = true; - //PelletOptions.TRACK_BRANCH_EFFECTS = true; - //PelletOptions.USE_INCREMENTAL_CONSISTENCY = true; - //PelletOptions.USE_INCREMENTAL_DELETION = true; - - PelletListener pelletListener = new PelletListener(tboxUnionModel,tboxAssertionsModel,tboxInferencesModel,ReasonerConfiguration.DEFAULT); - sce.getServletContext().setAttribute("pelletListener",pelletListener); - sce.getServletContext().setAttribute("pelletOntModel", pelletListener.getPelletModel()); - - if (wadf instanceof WebappDaoFactoryJena) { - ((WebappDaoFactoryJena) wadf).setPelletListener(pelletListener); - } - - log.info("Pellet reasoner connected for the TBox"); - - waitForTBoxReasoning(sce); - // set up simple reasoning for the ABox RDFService rdfService = ModelAccess.on(ctx).getRDFService(); @@ -139,23 +103,6 @@ public class SimpleReasonerSetup implements ServletContextListener { } } - public static void waitForTBoxReasoning(ServletContextEvent sce) - throws InterruptedException { - PelletListener pelletListener = (PelletListener) sce.getServletContext().getAttribute("pelletListener"); - if (pelletListener == null) { - return ; - } - int sleeps = 0; - // sleep at least once to make sure the TBox reasoning gets started - while ((0 == sleeps) || ((sleeps < 1000) && pelletListener.isReasoning())) { - if (((sleeps - 1) % 10) == 0) { // print message at 10 second intervals - log.info("Waiting for initial TBox reasoning to complete"); - } - Thread.sleep(1000); - sleeps++; - } - } - @Override public void contextDestroyed(ServletContextEvent sce) { log.info("received contextDestroyed notification"); @@ -209,17 +156,6 @@ public class SimpleReasonerSetup implements ServletContextListener { return (RecomputeMode) ctx.getAttribute(RECOMPUTE_REQUIRED_ATTR); } - private static final String MSTCOMPUTE_REQUIRED_ATTR = - SimpleReasonerSetup.class.getName() + ".MSTComputeRequired"; - - public static void setMSTComputeRequired(ServletContext ctx) { - ctx.setAttribute(MSTCOMPUTE_REQUIRED_ATTR, true); - } - - private static boolean isMSTComputeRequired(ServletContext ctx) { - return (ctx.getAttribute(MSTCOMPUTE_REQUIRED_ATTR) != null); - } - /** * Read the names of the plugin classes classes. * @@ -278,7 +214,8 @@ public class SimpleReasonerSetup implements ServletContextListener { this.simpleReasoner = simpleReasoner; } - public void run() { + @Override + public void run() { simpleReasoner.recompute(); } } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/UpdateKnowledgeBase.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/UpdateKnowledgeBase.java index d7931035e..92a4e81e5 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/UpdateKnowledgeBase.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/UpdateKnowledgeBase.java @@ -89,7 +89,7 @@ public class UpdateKnowledgeBase implements ServletContextListener { putReportingPathsIntoSettings(ctx, settings); putNonReportingPathsIntoSettings(ctx, settings); - SimpleReasonerSetup.waitForTBoxReasoning(sce); + PelletReasonerSetup.waitForTBoxReasoning(sce); WebappDaoFactory wadf = ModelAccess.on(ctx).getWebappDaoFactory(); settings.setDefaultNamespace(wadf.getDefaultNamespace()); diff --git a/webapp/web/WEB-INF/resources/startup_listeners.txt b/webapp/web/WEB-INF/resources/startup_listeners.txt index c5c9803eb..02a1f48fd 100644 --- a/webapp/web/WEB-INF/resources/startup_listeners.txt +++ b/webapp/web/WEB-INF/resources/startup_listeners.txt @@ -34,6 +34,7 @@ edu.cornell.mannlib.vitro.webapp.servlet.setup.RemoveObsoletePermissions edu.cornell.mannlib.vitro.webapp.servlet.setup.FileGraphSetup +edu.cornell.mannlib.vitro.webapp.servlet.setup.PelletListenerSetup edu.cornell.mannlib.vitro.webapp.servlet.setup.SimpleReasonerSetup #edu.cornell.mannlib.vitro.webapp.servlet.setup.UpdateKnowledgeBase