You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stanbol.apache.org by al...@apache.org on 2011/07/06 15:51:59 UTC
svn commit: r1143414 [2/2] - in /incubator/stanbol/trunk/reasoners:
base/src/test/java/org/apache/stanbol/reasoners/base/
web/src/main/java/org/apache/stanbol/reasoners/web/resources/
Modified: incubator/stanbol/trunk/reasoners/web/src/main/java/org/apache/stanbol/reasoners/web/resources/ConsistencyCheck.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/reasoners/web/src/main/java/org/apache/stanbol/reasoners/web/resources/ConsistencyCheck.java?rev=1143414&r1=1143413&r2=1143414&view=diff
==============================================================================
--- incubator/stanbol/trunk/reasoners/web/src/main/java/org/apache/stanbol/reasoners/web/resources/ConsistencyCheck.java (original)
+++ incubator/stanbol/trunk/reasoners/web/src/main/java/org/apache/stanbol/reasoners/web/resources/ConsistencyCheck.java Wed Jul 6 13:51:59 2011
@@ -1,6 +1,7 @@
package org.apache.stanbol.reasoners.web.resources;
-import static javax.ws.rs.core.MediaType.TEXT_HTML;
+import static javax.ws.rs.core.MediaType.*;
+import static javax.ws.rs.core.Response.Status.*;
import java.io.File;
import java.net.URL;
@@ -19,9 +20,7 @@ import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
-import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
-import javax.ws.rs.core.Response.Status;
import org.apache.stanbol.commons.web.base.ContextHelper;
import org.apache.stanbol.commons.web.base.resource.BaseStanbolResource;
@@ -67,456 +66,389 @@ import com.hp.hpl.jena.rdf.model.Resourc
import com.sun.jersey.api.view.Viewable;
import com.sun.jersey.multipart.FormDataParam;
-
/**
- * This class implements the REST interface for the /check-consistency service
- * of KReS.
+ * This class implements the REST interface for the /check-consistency service of KReS.
*
* @author elvio
*/
@Path("/reasoners/check-consistency")
-public class ConsistencyCheck extends BaseStanbolResource{
+public class ConsistencyCheck extends BaseStanbolResource {
+
+ private RuleStore kresRuleStore;
+ private OWLOntology inputowl;
+
+ private final OWLDuplicateSafeLoader loader = new OWLDuplicateSafeLoader();
+ protected ONManager onm;
+ protected ClerezzaOntologyStorage storage;
+
+ protected ServletContext servletContext;
+
+ private Logger log = LoggerFactory.getLogger(getClass());
+
+ /**
+ * The constructor.
+ *
+ * @param servletContext
+ * {To get the context where the REST service is running.}
+ */
+ public ConsistencyCheck(@Context ServletContext servletContext) {
+ this.servletContext = servletContext;
+
+ // Retrieve the rule store
+ this.kresRuleStore = (RuleStore) ContextHelper.getServiceFromContext(RuleStore.class, servletContext);
+
+ // Retrieve the ontology network manager
+ this.onm = (ONManager) ContextHelper.getServiceFromContext(ONManager.class, servletContext);
+ this.storage = (ClerezzaOntologyStorage) ContextHelper.getServiceFromContext(
+ ClerezzaOntologyStorage.class, servletContext);
+
+ if (kresRuleStore == null) {
+ log.warn("No KReSRuleStore with stored rules and recipes found in servlet context. Instantiating manually with default values...");
+ this.kresRuleStore = new RuleStoreImpl(onm, new Hashtable<String,Object>(), "");
+ log.debug("PATH TO OWL FILE LOADED: " + kresRuleStore.getFilePath());
+ }
+
+ }
+
+ /**
+ * To trasform a sequence of rules to a Jena Model
+ *
+ * @param owl
+ * {OWLOntology object contains a single recipe}
+ * @return {A jena rdf model contains the SWRL rule.}
+ */
+ private Model fromRecipeToModel(OWLOntology owl) throws NoSuchRecipeException,
+ OWLOntologyCreationException {
+
+ // FIXME: why the heck is this method re-instantiating a rule store?!?
+ RuleStore store = new RuleStoreImpl(onm, new Hashtable<String,Object>(), owl);
+ Model jenamodel = ModelFactory.createDefaultModel();
+
+ OWLDataFactory factory = owl.getOWLOntologyManager().getOWLDataFactory();
+ OWLClass ontocls = factory.getOWLClass(IRI
+ .create("http://kres.iks-project.eu/ontology/meta/rmi.owl#Recipe"));
+ Set<OWLClassAssertionAxiom> cls = owl.getClassAssertionAxioms(ontocls);
+ Iterator<OWLClassAssertionAxiom> iter = cls.iterator();
+ IRI recipeiri = IRI.create(iter.next().getIndividual().toStringID());
+
+ OWLIndividual recipeIndividual = factory.getOWLNamedIndividual(recipeiri);
+
+ OWLObjectProperty objectProperty = factory.getOWLObjectProperty(IRI
+ .create("http://kres.iks-project.eu/ontology/meta/rmi.owl#hasRule"));
+ Set<OWLIndividual> rules = recipeIndividual.getObjectPropertyValues(objectProperty,
+ store.getOntology());
+ String kReSRules = "";
+ for (OWLIndividual rule : rules) {
+ OWLDataProperty hasBodyAndHead = factory.getOWLDataProperty(IRI
+ .create("http://kres.iks-project.eu/ontology/meta/rmi.owl#hasBodyAndHead"));
+ Set<OWLLiteral> kReSRuleLiterals = rule
+ .getDataPropertyValues(hasBodyAndHead, store.getOntology());
+ for (OWLLiteral kReSRuleLiteral : kReSRuleLiterals) {
+ kReSRules += kReSRuleLiteral.getLiteral() + System.getProperty("line.separator");
+ }
+ }
+
+ // kReSRules =
+ // "ProvaParent = <http://www.semanticweb.org/ontologies/2010/6/ProvaParent.owl#> . rule1[ has(ProvaParent:hasParent, ?x, ?y) . has(ProvaParent:hasBrother, ?y, ?z) -> has(ProvaParent:hasUncle, ?x, ?z) ]";
+ KB kReSKB = RuleParserImpl.parse(kReSRules);
+ RuleList listrules = kReSKB.getkReSRuleList();
+ Iterator<Rule> iterule = listrules.iterator();
+ while (iterule.hasNext()) {
+ Rule singlerule = iterule.next();
+ Resource resource = singlerule.toSWRL(jenamodel);
+ }
+
+ return jenamodel;
+
+ }
+
+ /**
+ * To check the consistency of an Ontology or a Scope (as top ontology) using the default reasoner
+ *
+ * @param uri
+ * {A string contains the IRI of RDF (either RDF/XML or owl or scope) to be checked.}
+ * @return Return: <br/>
+ * 200 No data is retrieved, the graph IS consistent <br/>
+ * 204 No data is retrieved, the graph IS NOT consistent <br/>
+ * 404 File not found. The ontology cannot be retrieved. <br/>
+ * 412 Precondition failed. The ontology cannot be checked. This happens, for example, if the
+ * ontology includes missing imports. <br/>
+ * 500 Some error occurred.
+ */
+ @GET
+ // @Path("{uri:.+}")
+ public Response GetSimpleConsistencyCheck(@QueryParam("uri") String uri) {
+ log.info("Start simple consistency check with input: " + uri, this);
+
+ if (uri == null) {
+ return Response.status(BAD_REQUEST).build();
+ }
+
+ try {
+ boolean ok = false;
+ OWLOntology owl;
+ try {
+ // First create a manager
+ OWLOntologyManager mng = OWLManager.createOWLOntologyManager();
+
+ /**
+ * We use the loader to support duplicate owl:imports
+ */
+ log.debug("Loading " + uri, this);
+ owl = loader.load(mng, uri);
+ // owl = mng.loadOntologyFromOntologyDocument(IRI.create(uri));
+ } catch (UnloadableImportException uu) {
+ log.debug("Some ontology import failed. Cannot continue.", uu);
+ return Response.status(PRECONDITION_FAILED).build();
+ } catch (Exception ee) {
+ log.error("Cannot fetch the ontology. Some error occurred. Cannot continue.", ee);
+ return Response.status(NOT_FOUND).build();
+ }
+ CreateReasoner newreasoner = new CreateReasoner(owl);
+ // KReSReasonerImpl reasoner = new KReSReasonerImpl();
+ try {
+ RunReasoner reasoner = new RunReasoner(newreasoner.getReasoner());
+ ok = reasoner.isConsistent();
+ } catch (InconsistentOntologyException exc) {
+ ok = false;
+ }
+
+ if (ok) {
+ log.debug("The given graph is consistent.", this);
+ // No data is retrieved, the graph IS consistent
+ return Response.ok().build();
+ } else {
+ log.debug("The given graph is NOT consistent.", this);
+ // No data is retrieved, the graph IS NOT consistent
+ return Response.status(NO_CONTENT).build();
+ }
+
+ } catch (Exception e) {
+ // Some error occurred
+ throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
+ }
+
+ }
+
+ /**
+ * To check the consistency of a RDF input File or IRI on the base of a Scope (or an ontology) and a
+ * recipe. Can be used either HermiT or an owl-link server reasoner end-point
+ *
+ * @param session
+ * {A string contains the session IRI used to check the consistency.}
+ * @param scope
+ * {A string contains either a specific scope's ontology or the scope IRI used to check the
+ * consistency.}
+ * @param recipe
+ * {A string contains the recipe IRI from the service
+ * http://localhost:port/kres/recipe/recipeName.}
+ * @Param file {A file in a RDF (eihter RDF/XML or owl) to be checked.}
+ * @Param input_graph {A string contains the IRI of RDF (either RDF/XML or OWL) to be checked.}
+ * @Param owllink_endpoint {A string contains the reasoner server end-point URL.}
+ * @return Return: <br/>
+ * 200 No data is retrieved, the graph IS consistent <br/>
+ * 204 No data is retrieved, the graph IS NOT consistent <br/>
+ * 400 To run the session is needed the scope <br/>
+ * 404 Scope either Ontology or recipe or RDF input not found <br/>
+ * 409 Too much RDF input <br/>
+ * 500 Some error occurred
+ */
+ @POST
+ @Consumes(MULTIPART_FORM_DATA)
+ public Response getConsistencyCheck(@FormDataParam(value = "session") String session,
+ @FormDataParam(value = "scope") String scope,
+ @FormDataParam(value = "recipe") String recipe,
+ @FormDataParam(value = "input-graph") String input_graph,
+ @FormDataParam(value = "file") File file,
+ @FormDataParam(value = "owllink-endpoint") String owllink_endpoint) {
+
+ log.info("Start consistency check.", this);
+
+ try {
+
+ if ((session != null) && (scope == null)) {
+ log.error("Cannot load session without scope.", this);
+ return Response.status(BAD_REQUEST).build();
+ }
+
+ // Check for input conflict. Only one input at once is allowed
+ if ((file != null) && (input_graph != null)) {
+ log.error("Too much RDF input", this);
+ return Response.status(CONFLICT).build();
+ }
+
+ // Load input file or graph
+ if (file != null) this.inputowl = OWLManager.createOWLOntologyManager()
+ .loadOntologyFromOntologyDocument(file);
+ if (input_graph != null) this.inputowl = OWLManager.createOWLOntologyManager()
+ .loadOntologyFromOntologyDocument(IRI.create(input_graph));
+ if (inputowl == null && (session == null || scope == null)) return Response.status(NOT_FOUND)
+ .build();
+ if (inputowl == null) {
+ if (scope != null) this.inputowl = OWLManager.createOWLOntologyManager().createOntology();
+ else {
+ this.inputowl = OWLManager.createOWLOntologyManager().createOntology();
+ }
+ }
+
+ // Create list to add ontologies as imported
+ OWLOntologyManager mgr = inputowl.getOWLOntologyManager();
+ OWLDataFactory factory = inputowl.getOWLOntologyManager().getOWLDataFactory();
+ List<OWLOntologyChange> additions = new LinkedList<OWLOntologyChange>();
+
+ boolean ok = false;
+
+ // Load ontologies from scope, RDF input and recipe
+ // Try to resolve scope IRI
+ if ((scope != null) && (session == null)) try {
+ IRI iri = IRI.create(scope);
+ ScopeRegistry reg = onm.getScopeRegistry();
+ OntologyScope ontoscope = reg.getScope(iri);
+ Iterator<OWLOntology> importscope = ontoscope.getCustomSpace().getOntologies().iterator();
+ Iterator<OntologySpace> importsession = ontoscope.getSessionSpaces().iterator();
+
+ // Add ontology as import form scope, if it is anonymus we
+ // try to add single axioms.
+ while (importscope.hasNext()) {
+ OWLOntology auxonto = importscope.next();
+ if (!auxonto.getOntologyID().isAnonymous()) {
+ additions.add(new AddImport(inputowl, factory.getOWLImportsDeclaration(auxonto
+ .getOWLOntologyManager().getOntologyDocumentIRI(auxonto))));
+ } else {
+ mgr.addAxioms(inputowl, auxonto.getAxioms());
+ }
+ }
+
+ // Add ontology form sessions
+ while (importsession.hasNext()) {
+ Iterator<OWLOntology> sessionontos = importsession.next().getOntologies().iterator();
+ while (sessionontos.hasNext()) {
+ OWLOntology auxonto = sessionontos.next();
+ if (!auxonto.getOntologyID().isAnonymous()) {
+ additions.add(new AddImport(inputowl, factory.getOWLImportsDeclaration(auxonto
+ .getOWLOntologyManager().getOntologyDocumentIRI(auxonto))));
+ } else {
+ mgr.addAxioms(inputowl, auxonto.getAxioms());
+ }
+ }
+
+ }
+
+ } catch (Exception e) {
+ throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
+ }
+
+ // Get Ontologies from session
+ if ((session != null) && (scope != null)) try {
+ IRI iri = IRI.create(scope);
+ ScopeRegistry reg = onm.getScopeRegistry();
+ OntologyScope ontoscope = reg.getScope(iri);
+ SessionOntologySpace sos = ontoscope.getSessionSpace(IRI.create(session));
+
+ Set<OWLOntology> ontos = sos.getOntologyManager().getOntologies();
+ Iterator<OWLOntology> iteronto = ontos.iterator();
+
+ // Add session ontologies as import, if it is anonymus we
+ // try to add single axioms.
+ while (iteronto.hasNext()) {
+ OWLOntology auxonto = iteronto.next();
+ if (!auxonto.getOntologyID().isAnonymous()) {
+ additions.add(new AddImport(inputowl, factory.getOWLImportsDeclaration(auxonto
+ .getOWLOntologyManager().getOntologyDocumentIRI(auxonto))));
+ } else {
+ mgr.addAxioms(inputowl, auxonto.getAxioms());
+ }
+ }
+
+ } catch (Exception e) {
+ throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
+ }
+
+ // After gathered the all ontology as imported now we apply the
+ // changes
+ if (additions.size() > 0) mgr.applyChanges(additions);
+
+ // Run HermiT if the reasonerURL is null;
+ if (owllink_endpoint == null) {
+
+ // Create the reasoner for the consistency check
+ try {
+
+ if (recipe != null) {
+ OWLOntology recipeowl = OWLManager.createOWLOntologyManager()
+ .loadOntologyFromOntologyDocument(IRI.create(recipe));
+
+ // Get Jea RDF model of SWRL rule contained in the
+ // recipe
+ Model swrlmodel = fromRecipeToModel(recipeowl);
+ // Create a reasoner to run rules contained in the
+ // recipe
+ RunRules rulereasoner = new RunRules(swrlmodel, inputowl);
+ // Run the rule reasoner to the input RDF with the added
+ // top-ontology
+ inputowl = rulereasoner.runRulesReasoner();
+ }
+ CreateReasoner newreasoner = new CreateReasoner(inputowl);
+ // Prepare and start the reasoner to check the consistence
+ RunReasoner reasoner = new RunReasoner(newreasoner.getReasoner());
+ ok = reasoner.isConsistent();
+ } catch (InconsistentOntologyException exc) {
+ ok = false;
+ }
+
+ if (ok) {
+ // No data is retrieved, the graph IS consistent
+ return Response.ok().build();
+ } else {
+ // No data is retrieved, the graph IS NOT consistent
+ return Response.status(NO_CONTENT).build();
+ }
+
+ // If there is an owl-link server end-point specified in the
+ // form
+ } else {
+
+ // Create the reasoner for the consistency check by using the
+ // server and-point
+ try {
+ if (recipe != null) {
+ OWLOntology recipeowl = OWLManager.createOWLOntologyManager()
+ .loadOntologyFromOntologyDocument(IRI.create(recipe));
+ // Get Jea RDF model of SWRL rule contained in the
+ // recipe
+ Model swrlmodel = fromRecipeToModel(recipeowl);
+
+ // Create a reasoner to run rules contained in the
+ // recipe by using the server and-point
+ RunRules rulereasoner = new RunRules(swrlmodel, inputowl, new URL(owllink_endpoint));
+ // Run the rule reasoner to the input RDF with the added
+ // top-ontology
+ inputowl = rulereasoner.runRulesReasoner();
+ }
+
+ CreateReasoner newreasoner = new CreateReasoner(inputowl, new URL(owllink_endpoint));
+ // Prepare and start the reasoner to check the consistence
+ RunReasoner reasoner = new RunReasoner(newreasoner.getReasoner());
+ ok = reasoner.isConsistent();
+ } catch (InconsistentOntologyException exc) {
+ ok = false;
+ }
+ if (ok) {
+ // No data is retrieved, the graph IS consistent
+ return Response.status(OK).build();
+ } else {
+ // No data is retrieved, the graph IS NOT consistent
+ return Response.status(NO_CONTENT).build();
+ }
+ }
+
+ } catch (Exception e) {
+ throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
+ }
+
+ }
- private RuleStore kresRuleStore;
- private OWLOntology inputowl;
- private OWLOntology scopeowl;
-
- private final OWLDuplicateSafeLoader loader = new OWLDuplicateSafeLoader();
- protected ONManager onm;
- protected ClerezzaOntologyStorage storage;
-
- protected ServletContext servletContext;
-
- private Logger log = LoggerFactory.getLogger(getClass());
-
- /**
- * The constructor.
- *
- * @param servletContext
- * {To get the context where the REST service is running.}
- */
- public ConsistencyCheck(@Context ServletContext servletContext) {
- this.servletContext = servletContext;
-
- // Retrieve the rule store
- this.kresRuleStore = (RuleStore) ContextHelper.getServiceFromContext(RuleStore.class, servletContext);
-
- // Retrieve the ontology network manager
- this.onm = (ONManager) ContextHelper.getServiceFromContext(ONManager.class, servletContext);
- this.storage = (ClerezzaOntologyStorage) ContextHelper.getServiceFromContext(ClerezzaOntologyStorage.class, servletContext);
-
- if (kresRuleStore == null) {
- log
- .warn("No KReSRuleStore with stored rules and recipes found in servlet context. Instantiating manually with default values...");
- this.kresRuleStore = new RuleStoreImpl(onm,
- new Hashtable<String, Object>(), "");
- log
- .debug("PATH TO OWL FILE LOADED: "
- + kresRuleStore.getFilePath());
- }
-
- }
-
- /**
- * To trasform a sequence of rules to a Jena Model
- *
- * @param owl
- * {OWLOntology object contains a single recipe}
- * @return {A jena rdf model contains the SWRL rule.}
- */
- private Model fromRecipeToModel(OWLOntology owl)
- throws NoSuchRecipeException, OWLOntologyCreationException {
-
- // FIXME: why the heck is this method re-instantiating a rule store?!?
- RuleStore store = new RuleStoreImpl(onm,
- new Hashtable<String, Object>(), owl);
- Model jenamodel = ModelFactory.createDefaultModel();
-
- OWLDataFactory factory = owl.getOWLOntologyManager()
- .getOWLDataFactory();
- OWLClass ontocls = factory
- .getOWLClass(IRI
- .create("http://kres.iks-project.eu/ontology/meta/rmi.owl#Recipe"));
- Set<OWLClassAssertionAxiom> cls = owl.getClassAssertionAxioms(ontocls);
- Iterator<OWLClassAssertionAxiom> iter = cls.iterator();
- IRI recipeiri = IRI.create(iter.next().getIndividual().toStringID());
-
- OWLIndividual recipeIndividual = factory
- .getOWLNamedIndividual(recipeiri);
-
- OWLObjectProperty objectProperty = factory
- .getOWLObjectProperty(IRI
- .create("http://kres.iks-project.eu/ontology/meta/rmi.owl#hasRule"));
- Set<OWLIndividual> rules = recipeIndividual.getObjectPropertyValues(
- objectProperty, store.getOntology());
- String kReSRules = "";
- for (OWLIndividual rule : rules) {
- OWLDataProperty hasBodyAndHead = factory
- .getOWLDataProperty(IRI
- .create("http://kres.iks-project.eu/ontology/meta/rmi.owl#hasBodyAndHead"));
- Set<OWLLiteral> kReSRuleLiterals = rule.getDataPropertyValues(
- hasBodyAndHead, store.getOntology());
- for (OWLLiteral kReSRuleLiteral : kReSRuleLiterals) {
- kReSRules += kReSRuleLiteral.getLiteral()
- + System.getProperty("line.separator");
- }
- }
-
- // kReSRules =
- // "ProvaParent = <http://www.semanticweb.org/ontologies/2010/6/ProvaParent.owl#> . rule1[ has(ProvaParent:hasParent, ?x, ?y) . has(ProvaParent:hasBrother, ?y, ?z) -> has(ProvaParent:hasUncle, ?x, ?z) ]";
- KB kReSKB = RuleParserImpl.parse(kReSRules);
- RuleList listrules = kReSKB.getkReSRuleList();
- Iterator<Rule> iterule = listrules.iterator();
- while (iterule.hasNext()) {
- Rule singlerule = iterule.next();
- Resource resource = singlerule.toSWRL(jenamodel);
- }
-
- return jenamodel;
-
- }
-
- /**
- * To check the consistency of an Ontology or a Scope (as top ontology)
- * using the default reasoner
- *
- * @param uri
- * {A string contains the IRI of RDF (either RDF/XML or owl or
- * scope) to be checked.}
- * @return Return: <br/>
- * 200 No data is retrieved, the graph IS consistent <br/>
- * 204 No data is retrieved, the graph IS NOT consistent <br/>
- * 404 File not found. The ontology cannot be retrieved. <br/>
- * 412 Precondition failed. The ontology cannot be checked. This
- * happens, for example, if the ontology includes missing imports. <br/>
- * 500 Some error occurred.
- */
- @GET
- //@Path("{uri:.+}")
- public Response GetSimpleConsistencyCheck(
- @QueryParam("uri") String uri) {
- log.info("Start simple consistency check with input: "+uri, this);
-
- if(uri==null){
- return Response.status(Status.BAD_REQUEST).build();
- }
-
- try {
- boolean ok = false;
- OWLOntology owl;
- try {
- // First create a manager
- OWLOntologyManager mng = OWLManager.createOWLOntologyManager();
-
- /**
- * We use the loader to support duplicate owl:imports
- */
- log.debug("Loading "+uri, this);
- owl = loader.load(mng, uri);
- // owl = mng.loadOntologyFromOntologyDocument(IRI.create(uri));
- } catch (UnloadableImportException uu) {
- log.debug("Some ontology import failed. Cannot continue.", uu);
- return Response.status(Status.PRECONDITION_FAILED).build();
- } catch (Exception ee) {
- log
- .error(
- "Cannot fetch the ontology. Some error occurred. Cannot continue.",
- ee);
- return Response.status(Status.NOT_FOUND).build();
- }
- CreateReasoner newreasoner = new CreateReasoner(owl);
- // KReSReasonerImpl reasoner = new KReSReasonerImpl();
- try {
- RunReasoner reasoner = new RunReasoner(newreasoner
- .getReasoner());
- ok = reasoner.isConsistent();
- } catch (InconsistentOntologyException exc) {
- ok = false;
- }
-
- if (ok) {
- log.debug("The give graph is consistent.",this);
- // No data is retrieved, the graph IS consistent
- return Response.status(Status.OK).build();
- } else {
- log.debug("The give graph is NOT consistent.",this);
- // No data is retrieved, the graph IS NOT consistent
- return Response.status(Status.NO_CONTENT).build();
- }
-
- } catch (Exception e) {
- // Some error occurred
- throw new WebApplicationException(e, Status.INTERNAL_SERVER_ERROR);
- }
-
- }
-
- /**
- * To check the consistency of a RDF input File or IRI on the base of a
- * Scope (or an ontology) and a recipe. Can be used either HermiT or an
- * owl-link server reasoner end-point
- *
- * @param session
- * {A string contains the session IRI used to check the
- * consistency.}
- * @param scope
- * {A string contains either a specific scope's ontology or the
- * scope IRI used to check the consistency.}
- * @param recipe
- * {A string contains the recipe IRI from the service
- * http://localhost:port/kres/recipe/recipeName.}
- * @Param file {A file in a RDF (eihter RDF/XML or owl) to be checked.}
- * @Param input_graph {A string contains the IRI of RDF (either RDF/XML or
- * OWL) to be checked.}
- * @Param owllink_endpoint {A string contains the reasoner server end-point
- * URL.}
- * @return Return: <br/>
- * 200 No data is retrieved, the graph IS consistent <br/>
- * 204 No data is retrieved, the graph IS NOT consistent <br/>
- * 400 To run the session is needed the scope <br/>
- * 404 Scope either Ontology or recipe or RDF input not found <br/>
- * 409 Too much RDF input <br/>
- * 500 Some error occurred
- */
- @POST
- @Consumes(MediaType.MULTIPART_FORM_DATA)
- public Response getConsistencyCheck(
- @FormDataParam(value = "session") String session,
- @FormDataParam(value = "scope") String scope,
- @FormDataParam(value = "recipe") String recipe,
- @FormDataParam(value = "input-graph") String input_graph,
- @FormDataParam(value = "file") File file,
- @FormDataParam(value = "owllink-endpoint") String owllink_endpoint
- ) {
-
- log.info("Start consistency check.", this);
-
-
- try {
-
- if ((session != null) && (scope == null)) {
- log.error("Cannot load session without scope.", this);
- return Response.status(Status.BAD_REQUEST).build();
- }
-
- // Check for input conflict. Only one input at once is allowed
- if ((file != null) && (input_graph != null)) {
- log.error("To much RDF input", this);
- return Response.status(Status.CONFLICT).build();
- }
-
- // Load input file or graph
- if (file != null)
- this.inputowl = OWLManager.createOWLOntologyManager()
- .loadOntologyFromOntologyDocument(file);
- if (input_graph != null)
- this.inputowl = OWLManager.createOWLOntologyManager()
- .loadOntologyFromOntologyDocument(
- IRI.create(input_graph));
- if (inputowl == null && (session == null || scope == null))
- return Response.status(Status.NOT_FOUND).build();
- if (inputowl == null) {
- if (scope != null)
- this.inputowl = OWLManager.createOWLOntologyManager()
- .createOntology();
- else {
- this.inputowl = OWLManager.createOWLOntologyManager()
- .createOntology();
- }
- }
-
- // Create list to add ontologies as imported
- OWLOntologyManager mgr = inputowl.getOWLOntologyManager();
- OWLDataFactory factory = inputowl.getOWLOntologyManager()
- .getOWLDataFactory();
- List<OWLOntologyChange> additions = new LinkedList<OWLOntologyChange>();
-
- boolean ok = false;
-
- // Load ontologies from scope, RDF input and recipe
- // Try to resolve scope IRI
- if ((scope != null) && (session == null))
- try {
- IRI iri = IRI.create(scope);
- ScopeRegistry reg = onm.getScopeRegistry();
- OntologyScope ontoscope = reg.getScope(iri);
- Iterator<OWLOntology> importscope = ontoscope
- .getCustomSpace().getOntologies().iterator();
- Iterator<OntologySpace> importsession = ontoscope
- .getSessionSpaces().iterator();
-
- // Add ontology as import form scope, if it is anonymus we
- // try to add single axioms.
- while (importscope.hasNext()) {
- OWLOntology auxonto = importscope.next();
- if (!auxonto.getOntologyID().isAnonymous()) {
- additions.add(new AddImport(inputowl, factory
- .getOWLImportsDeclaration(auxonto
- .getOWLOntologyManager()
- .getOntologyDocumentIRI(auxonto))));
- } else {
- mgr.addAxioms(inputowl, auxonto.getAxioms());
- }
- }
-
- // Add ontology form sessions
- while (importsession.hasNext()) {
- Iterator<OWLOntology> sessionontos = importsession
- .next().getOntologies().iterator();
- while (sessionontos.hasNext()) {
- OWLOntology auxonto = sessionontos.next();
- if (!auxonto.getOntologyID().isAnonymous()) {
- additions
- .add(new AddImport(
- inputowl,
- factory
- .getOWLImportsDeclaration(auxonto
- .getOWLOntologyManager()
- .getOntologyDocumentIRI(
- auxonto))));
- } else {
- mgr.addAxioms(inputowl, auxonto.getAxioms());
- }
- }
-
- }
-
- } catch (Exception e) {
- log.error("Problem with scope: " + scope, this);
- log.debug("Exception is ", e);
- Response.status(Status.NOT_FOUND).build();
- }
-
- // Get Ontologies from session
- if ((session != null) && (scope != null))
- try {
- IRI iri = IRI.create(scope);
- ScopeRegistry reg = onm.getScopeRegistry();
- OntologyScope ontoscope = reg.getScope(iri);
- SessionOntologySpace sos = ontoscope.getSessionSpace(IRI
- .create(session));
-
- Set<OWLOntology> ontos = sos.getOntologyManager()
- .getOntologies();
- Iterator<OWLOntology> iteronto = ontos.iterator();
-
- // Add session ontologies as import, if it is anonymus we
- // try to add single axioms.
- while (iteronto.hasNext()) {
- OWLOntology auxonto = iteronto.next();
- if (!auxonto.getOntologyID().isAnonymous()) {
- additions.add(new AddImport(inputowl, factory
- .getOWLImportsDeclaration(auxonto
- .getOWLOntologyManager()
- .getOntologyDocumentIRI(auxonto))));
- } else {
- mgr.addAxioms(inputowl, auxonto.getAxioms());
- }
- }
-
- } catch (Exception e) {
- log.error("Problem with session: " + session, this);
- log.debug("Exception is", e);
- Response.status(Status.NOT_FOUND).build();
- }
-
- // After gathered the all ontology as imported now we apply the
- // changes
- if (additions.size() > 0)
- mgr.applyChanges(additions);
-
- // Run HermiT if the reasonerURL is null;
- if (owllink_endpoint == null) {
-
- // Create the reasoner for the consistency check
- try {
-
- if (recipe != null) {
- OWLOntology recipeowl = OWLManager
- .createOWLOntologyManager()
- .loadOntologyFromOntologyDocument(
- IRI.create(recipe));
-
- // Get Jea RDF model of SWRL rule contained in the
- // recipe
- Model swrlmodel = fromRecipeToModel(recipeowl);
- // Create a reasoner to run rules contained in the
- // recipe
- RunRules rulereasoner = new RunRules(swrlmodel,
- inputowl);
- // Run the rule reasoner to the input RDF with the added
- // top-ontology
- inputowl = rulereasoner.runRulesReasoner();
- }
- CreateReasoner newreasoner = new CreateReasoner(
- inputowl);
- // Prepare and start the reasoner to check the consistence
- RunReasoner reasoner = new RunReasoner(newreasoner
- .getReasoner());
- ok = reasoner.isConsistent();
- } catch (InconsistentOntologyException exc) {
- ok = false;
- }
-
- if (ok) {
- // No data is retrieved, the graph IS consistent
- return Response.status(Status.OK).build();
- } else {
- // No data is retrieved, the graph IS NOT consistent
- return Response.status(Status.NO_CONTENT).build();
- }
-
- // If there is an owl-link server end-point specified in the
- // form
- } else {
-
- // Create the reasoner for the consistency check by using the
- // server and-point
- try {
- if (recipe != null) {
- OWLOntology recipeowl = OWLManager
- .createOWLOntologyManager()
- .loadOntologyFromOntologyDocument(
- IRI.create(recipe));
- // Get Jea RDF model of SWRL rule contained in the
- // recipe
- Model swrlmodel = fromRecipeToModel(recipeowl);
-
- // Create a reasoner to run rules contained in the
- // recipe by using the server and-point
- RunRules rulereasoner = new RunRules(swrlmodel,
- inputowl, new URL(owllink_endpoint));
- // Run the rule reasoner to the input RDF with the added
- // top-ontology
- inputowl = rulereasoner.runRulesReasoner();
- }
-
- CreateReasoner newreasoner = new CreateReasoner(
- inputowl, new URL(owllink_endpoint));
- // Prepare and start the reasoner to check the consistence
- RunReasoner reasoner = new RunReasoner(newreasoner
- .getReasoner());
- ok = reasoner.isConsistent();
- } catch (InconsistentOntologyException exc) {
- ok = false;
- }
-
- if (ok) {
- // No data is retrieved, the graph IS consistent
- return Response.status(Status.OK).build();
- } else {
- // No data is retrieved, the graph IS NOT consistent
- return Response.status(Status.NO_CONTENT).build();
- }
- }
-
- } catch (Exception e) {
- throw new WebApplicationException(e, Status.INTERNAL_SERVER_ERROR);
- }
-
- }
-
- @GET
+ @GET
@Produces(TEXT_HTML)
public Response getView() {
return Response.ok(new Viewable("index", this), TEXT_HTML).build();
Modified: incubator/stanbol/trunk/reasoners/web/src/main/java/org/apache/stanbol/reasoners/web/resources/ConsistentRefactoring.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/reasoners/web/src/main/java/org/apache/stanbol/reasoners/web/resources/ConsistentRefactoring.java?rev=1143414&r1=1143413&r2=1143414&view=diff
==============================================================================
--- incubator/stanbol/trunk/reasoners/web/src/main/java/org/apache/stanbol/reasoners/web/resources/ConsistentRefactoring.java (original)
+++ incubator/stanbol/trunk/reasoners/web/src/main/java/org/apache/stanbol/reasoners/web/resources/ConsistentRefactoring.java Wed Jul 6 13:51:59 2011
@@ -1,8 +1,8 @@
package org.apache.stanbol.reasoners.web.resources;
-import static javax.ws.rs.core.MediaType.TEXT_HTML;
-import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
-import static javax.ws.rs.core.Response.Status.NOT_FOUND;
+import static javax.ws.rs.core.MediaType.*;
+import static javax.ws.rs.core.Response.Status.*;
+import static org.apache.stanbol.commons.web.base.format.KRFormat.*;
import java.io.InputStream;
@@ -14,10 +14,8 @@ import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
-import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
-import org.apache.stanbol.commons.web.base.format.KRFormat;
import org.apache.stanbol.commons.web.base.resource.BaseStanbolResource;
import org.apache.stanbol.reasoners.base.api.ConsistentRefactorer;
import org.apache.stanbol.reasoners.base.api.InconcistencyException;
@@ -28,6 +26,8 @@ import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import com.sun.jersey.api.view.Viewable;
import com.sun.jersey.multipart.FormDataParam;
@@ -39,7 +39,9 @@ import com.sun.jersey.multipart.FormData
*
*/
@Path("/reasoners/refactor")
-public class ConsistentRefactoring extends BaseStanbolResource{
+public class ConsistentRefactoring extends BaseStanbolResource {
+
+ private Logger log = LoggerFactory.getLogger(getClass());
protected ConsistentRefactorer refactorer;
@@ -70,18 +72,19 @@ public class ConsistentRefactoring exten
} catch (RefactoringException e) {
return Response.status(INTERNAL_SERVER_ERROR).build();
} catch (NoSuchRecipeException e) {
- return Response.status(204).build();
+ return Response.status(NO_CONTENT).build();
} catch (InconcistencyException e) {
- return Response.status(415).build();
+ log.error("Cannot classify ionconsistent graph " + inputGraph, e);
+ return Response.status(PRECONDITION_FAILED).build();
}
}
@POST
@Path("/consistent")
- @Consumes(MediaType.MULTIPART_FORM_DATA)
- @Produces({KRFormat.TURTLE, KRFormat.FUNCTIONAL_OWL, KRFormat.MANCHESTER_OWL, KRFormat.RDF_XML,
- KRFormat.OWL_XML, KRFormat.RDF_JSON})
+ @Consumes(MULTIPART_FORM_DATA)
+ @Produces({TURTLE, FUNCTIONAL_OWL, MANCHESTER_OWL, RDF_XML,
+ OWL_XML, RDF_JSON})
public Response consistentRefactoringOfNewGraph(@FormDataParam("recipe") String recipe,
@FormDataParam("input") InputStream input) {
@@ -100,9 +103,10 @@ public class ConsistentRefactoring exten
} catch (RefactoringException e) {
return Response.status(INTERNAL_SERVER_ERROR).build();
} catch (NoSuchRecipeException e) {
- return Response.status(204).build();
+ return Response.status(NO_CONTENT).build();
} catch (InconcistencyException e) {
- return Response.status(415).build();
+ log.error("Cannot classify ionconsistent graph " + inputOntology, e);
+ return Response.status(PRECONDITION_FAILED).build();
}
return Response.ok(outputOntology).build();
@@ -111,7 +115,7 @@ public class ConsistentRefactoring exten
}
}
-
+
@GET
@Produces(TEXT_HTML)
public Response getView() {
Modified: incubator/stanbol/trunk/reasoners/web/src/main/java/org/apache/stanbol/reasoners/web/resources/Enrichment.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/reasoners/web/src/main/java/org/apache/stanbol/reasoners/web/resources/Enrichment.java?rev=1143414&r1=1143413&r2=1143414&view=diff
==============================================================================
--- incubator/stanbol/trunk/reasoners/web/src/main/java/org/apache/stanbol/reasoners/web/resources/Enrichment.java (original)
+++ incubator/stanbol/trunk/reasoners/web/src/main/java/org/apache/stanbol/reasoners/web/resources/Enrichment.java Wed Jul 6 13:51:59 2011
@@ -5,7 +5,8 @@
package org.apache.stanbol.reasoners.web.resources;
-import static javax.ws.rs.core.MediaType.TEXT_HTML;
+import static javax.ws.rs.core.MediaType.*;
+import static javax.ws.rs.core.Response.Status.*;
import java.io.File;
import java.net.URL;
@@ -23,9 +24,7 @@ import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
-import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
-import javax.ws.rs.core.Response.Status;
import org.apache.stanbol.commons.web.base.ContextHelper;
import org.apache.stanbol.commons.web.base.resource.BaseStanbolResource;
@@ -68,102 +67,92 @@ import com.hp.hpl.jena.rdf.model.Resourc
import com.sun.jersey.api.view.Viewable;
import com.sun.jersey.multipart.FormDataParam;
-
/**
*
*
*/
@Path("/reasoners/enrichment")
-public class Enrichment extends BaseStanbolResource{
+public class Enrichment extends BaseStanbolResource {
- private RuleStore kresRuleStore;
private OWLOntology inputowl;
- private OWLOntology scopeowl;
- protected ONManager onm;
- protected ClerezzaOntologyStorage storage;
- protected ServletContext servletContext;
+ private RuleStore kresRuleStore;
+
+ private Logger log = LoggerFactory.getLogger(getClass());
+
+ protected ONManager onm;
- private Logger log = LoggerFactory.getLogger(getClass());
+ protected ServletContext servletContext;
+
+ protected ClerezzaOntologyStorage storage;
/**
* To get the RuleStoreImpl where are stored the rules and the recipes
- *
- * @param servletContext
- * {To get the context where the REST service is running.}
+ *
+ * @param servletContext
+ * {To get the context where the REST service is running.}
*/
- public Enrichment(@Context ServletContext servletContext){
+ public Enrichment(@Context ServletContext servletContext) {
this.servletContext = servletContext;
-
- // Retrieve the rule store
- this.kresRuleStore = (RuleStore) ContextHelper.getServiceFromContext(RuleStore.class, servletContext);
-
- // Retrieve the ontology network manager
- this.onm = (ONManager) ContextHelper.getServiceFromContext(ONManager.class, servletContext);
- this.storage = (ClerezzaOntologyStorage) ContextHelper.getServiceFromContext(ClerezzaOntologyStorage.class, servletContext);
-
- if (kresRuleStore == null) {
- log
- .warn("No KReSRuleStore with stored rules and recipes found in servlet context. Instantiating manually with default values...");
- this.kresRuleStore = new RuleStoreImpl(onm,
- new Hashtable<String, Object>(), "");
- log
- .debug("PATH TO OWL FILE LOADED: "
- + kresRuleStore.getFilePath());
+
+ // Retrieve the rule store
+ this.kresRuleStore = (RuleStore) ContextHelper.getServiceFromContext(RuleStore.class, servletContext);
+
+ // Retrieve the ontology network manager
+ this.onm = (ONManager) ContextHelper.getServiceFromContext(ONManager.class, servletContext);
+ this.storage = (ClerezzaOntologyStorage) ContextHelper.getServiceFromContext(
+ ClerezzaOntologyStorage.class, servletContext);
+
+ if (kresRuleStore == null) {
+ log.warn("No KReSRuleStore with stored rules and recipes found in servlet context. Instantiating manually with default values...");
+ this.kresRuleStore = new RuleStoreImpl(onm, new Hashtable<String,Object>(), "");
+ log.debug("PATH TO OWL FILE LOADED: " + kresRuleStore.getFilePath());
}
}
- /**
+ /**
* To trasform a sequence of rules to a Jena Model
- *
- * @param owl
- * {OWLOntology object contains a single recipe}
+ *
+ * @param owl
+ * {OWLOntology object contains a single recipe}
* @return {A jena rdf model contains the SWRL rule.}
*/
- private Model fromRecipeToModel(OWLOntology owl)
- throws NoSuchRecipeException {
+ private Model fromRecipeToModel(OWLOntology owl) throws NoSuchRecipeException {
- // FIXME: why the heck is this method re-instantiating a rule store?!?
- RuleStore store = new RuleStoreImpl(onm,
- new Hashtable<String, Object>(), owl);
+ // FIXME: why the heck is this method re-instantiating a rule store?!?
+ RuleStore store = new RuleStoreImpl(onm, new Hashtable<String,Object>(), owl);
Model jenamodel = ModelFactory.createDefaultModel();
- OWLDataFactory factory = owl.getOWLOntologyManager()
- .getOWLDataFactory();
- OWLClass ontocls = factory
- .getOWLClass(IRI
- .create("http://kres.iks-project.eu/ontology/meta/rmi.owl#Recipe"));
+ OWLDataFactory factory = owl.getOWLOntologyManager().getOWLDataFactory();
+ OWLClass ontocls = factory.getOWLClass(IRI
+ .create("http://kres.iks-project.eu/ontology/meta/rmi.owl#Recipe"));
Set<OWLClassAssertionAxiom> cls = owl.getClassAssertionAxioms(ontocls);
Iterator<OWLClassAssertionAxiom> iter = cls.iterator();
IRI recipeiri = IRI.create(iter.next().getIndividual().toStringID());
- OWLIndividual recipeIndividual = factory
- .getOWLNamedIndividual(recipeiri);
+ OWLIndividual recipeIndividual = factory.getOWLNamedIndividual(recipeiri);
- OWLObjectProperty objectProperty = factory
- .getOWLObjectProperty(IRI
- .create("http://kres.iks-project.eu/ontology/meta/rmi.owl#hasRule"));
- Set<OWLIndividual> rules = recipeIndividual.getObjectPropertyValues(
- objectProperty, store.getOntology());
+ OWLObjectProperty objectProperty = factory.getOWLObjectProperty(IRI
+ .create("http://kres.iks-project.eu/ontology/meta/rmi.owl#hasRule"));
+ Set<OWLIndividual> rules = recipeIndividual.getObjectPropertyValues(objectProperty,
+ store.getOntology());
String kReSRules = "";
- for(OWLIndividual rule : rules){
- OWLDataProperty hasBodyAndHead = factory
- .getOWLDataProperty(IRI
- .create("http://kres.iks-project.eu/ontology/meta/rmi.owl#hasBodyAndHead"));
- Set<OWLLiteral> kReSRuleLiterals = rule.getDataPropertyValues(
- hasBodyAndHead, store.getOntology());
-
- for(OWLLiteral kReSRuleLiteral : kReSRuleLiterals){
- kReSRules += kReSRuleLiteral.getLiteral()
- + System.getProperty("line.separator");
- }
- }
+ for (OWLIndividual rule : rules) {
+ OWLDataProperty hasBodyAndHead = factory.getOWLDataProperty(IRI
+ .create("http://kres.iks-project.eu/ontology/meta/rmi.owl#hasBodyAndHead"));
+ Set<OWLLiteral> kReSRuleLiterals = rule
+ .getDataPropertyValues(hasBodyAndHead, store.getOntology());
+
+ for (OWLLiteral kReSRuleLiteral : kReSRuleLiterals) {
+ kReSRules += kReSRuleLiteral.getLiteral() + System.getProperty("line.separator");
+ }
+ }
- //"ProvaParent = <http://www.semanticweb.org/ontologies/2010/6/ProvaParent.owl#> . rule1[ has(ProvaParent:hasParent, ?x, ?y) . has(ProvaParent:hasBrother, ?y, ?z) -> has(ProvaParent:hasUncle, ?x, ?z) ]");
+ // "ProvaParent = <http://www.semanticweb.org/ontologies/2010/6/ProvaParent.owl#> . rule1[ has(ProvaParent:hasParent, ?x, ?y) . has(ProvaParent:hasBrother, ?y, ?z) -> has(ProvaParent:hasUncle, ?x, ?z) ]");
KB kReSKB = RuleParserImpl.parse(kReSRules);
RuleList listrules = kReSKB.getkReSRuleList();
Iterator<Rule> iterule = listrules.iterator();
- while(iterule.hasNext()){
+ while (iterule.hasNext()) {
Rule singlerule = iterule.next();
Resource resource = singlerule.toSWRL(jenamodel);
}
@@ -172,285 +161,229 @@ public class Enrichment extends BaseStan
}
- /**
- * To perform a rule based reasoning with a given recipe and scope (or an
- * ontology) to a RDF input specify via its IRI.
- *
- * @param session
- * {A string contains the session IRI used to inference the
- * input.}
- * @param scope
- * {A string contains either ontology or the scope IRI used to
- * inference the input.}
- * @param recipe
- * {A string contains the recipe IRI from the service
- * http://localhost:port/kres/recipe/recipeName.}
+ @GET
+ @Produces(TEXT_HTML)
+ public Response getView() {
+ return Response.ok(new Viewable("index", this), TEXT_HTML).build();
+ }
+
+ /**
+ * To perform a rule based reasoning with a given recipe and scope (or an ontology) to a RDF input specify
+ * via its IRI.
+ *
+ * @param session
+ * {A string contains the session IRI used to inference the input.}
+ * @param scope
+ * {A string contains either ontology or the scope IRI used to inference the input.}
+ * @param recipe
+ * {A string contains the recipe IRI from the service
+ * http://localhost:port/kres/recipe/recipeName.}
* @Param file {A file in a RDF (eihter RDF/XML or owl) to inference.}
- * @Param input_graph {A string contains the IRI of RDF (either RDF/XML or
- * OWL) to inference.}
- * @Param owllink_endpoint {A string contains the reasoner server end-point
- * URL.}
+ * @Param input_graph {A string contains the IRI of RDF (either RDF/XML or OWL) to inference.}
+ * @Param owllink_endpoint {A string contains the reasoner server end-point URL.}
* @return Return: <br/>
- * 200 Returns a graph with the enrichments <br/>
- * 204 No enrichments have been produced from the given graph <br/>
- * 400 To run the session is needed the scope <br/>
- * 404 The recipe/ontology/scope/input doesn't exist in the network <br/>
- * 409 Too much RDF inputs <br/>
- * 500 Some error occurred
+ * 200 Returns a graph with the enrichments <br/>
+ * 204 No enrichments have been produced from the given graph <br/>
+ * 400 To run the session is needed the scope <br/>
+ * 404 The recipe/ontology/scope/input doesn't exist in the network <br/>
+ * 409 Too much RDF inputs <br/>
+ * 500 Some error occurred
*/
@POST
- @Consumes(MediaType.MULTIPART_FORM_DATA)
+ @Consumes(MULTIPART_FORM_DATA)
@Produces("application/rdf+xml")
- public Response ontologyEnrichment(
- @FormDataParam(value = "session") String session,
- @FormDataParam(value = "scope") String scope,
- @FormDataParam(value = "recipe") String recipe,
- @FormDataParam(value = "input-graph") String input_graph,
- @FormDataParam(value = "file") File file,
- @FormDataParam(value = "owllink-endpoint") String owllink_endpoint) {
-
- try{
-
- if((session!=null)&&(scope==null)){
- System.err.println("ERROR: Cannot load session without scope.");
- return Response.status(Status.BAD_REQUEST).build();
- }
+ public Response ontologyEnrichment(@FormDataParam(value = "session") String session,
+ @FormDataParam(value = "scope") String scope,
+ @FormDataParam(value = "recipe") String recipe,
+ @FormDataParam(value = "input-graph") String input_graph,
+ @FormDataParam(value = "file") File file,
+ @FormDataParam(value = "owllink-endpoint") String owllink_endpoint) {
+
+ try {
+
+ if ((session != null) && (scope == null)) {
+ log.error("Unspecified scope parameter for session {} , cannot classify.", session);
+ return Response.status(BAD_REQUEST).build();
+ }
- //Check for input conflict. Only one input at once is allowed
- if((file!=null)&&(input_graph!=null)){
- System.err.println("ERROR: To much RDF input");
- return Response.status(Status.CONFLICT).build();
- }
-
- //Load input file or graph
- if(file!=null)
- this.inputowl = OWLManager.createOWLOntologyManager()
- .loadOntologyFromOntologyDocument(file);
- if(input_graph!=null)
- this.inputowl = OWLManager.createOWLOntologyManager()
- .loadOntologyFromOntologyDocument(
- IRI.create(input_graph));
-
- if(inputowl==null)
- return Response.status(Status.NOT_FOUND).build();
-
- //Create list to add ontologies as imported
- OWLOntologyManager mgr = inputowl.getOWLOntologyManager();
- OWLDataFactory factory = inputowl.getOWLOntologyManager()
- .getOWLDataFactory();
- List<OWLOntologyChange> additions = new LinkedList<OWLOntologyChange>();
-
- boolean ok = false;
-
- //Load ontologies from scope, RDF input and recipe
- //Try to resolve scope IRI
- if((scope!=null)&&(session==null))
- try{
- IRI iri = IRI.create(scope);
- ScopeRegistry reg = onm.getScopeRegistry();
- OntologyScope ontoscope = reg.getScope(iri);
- Iterator<OWLOntology> importscope = ontoscope
- .getCustomSpace().getOntologies().iterator();
- Iterator<OntologySpace> importsession = ontoscope
- .getSessionSpaces().iterator();
-
- // Add ontology as import form scope, if it is anonymus we
- // try to add single axioms.
- while(importscope.hasNext()){
- OWLOntology auxonto = importscope.next();
- if(!auxonto.getOntologyID().isAnonymous()){
- additions.add(new AddImport(inputowl, factory
- .getOWLImportsDeclaration(auxonto
- .getOWLOntologyManager()
- .getOntologyDocumentIRI(auxonto))));
- }else{
- mgr.addAxioms(inputowl,auxonto.getAxioms());
+ // Check for input conflict. Only one input at once is allowed
+ if ((file != null) && (input_graph != null)) {
+ log.error("Parameters file and input-graph are mutually exclusive and cannot be specified together.");
+ return Response.status(CONFLICT).build();
}
- }
- //Add ontology form sessions
- while(importsession.hasNext()){
- Iterator<OWLOntology> sessionontos = importsession
- .next().getOntologies().iterator();
- while(sessionontos.hasNext()){
- OWLOntology auxonto = sessionontos.next();
- if(!auxonto.getOntologyID().isAnonymous()){
- additions
- .add(new AddImport(
- inputowl,
- factory
- .getOWLImportsDeclaration(auxonto
- .getOWLOntologyManager()
- .getOntologyDocumentIRI(
- auxonto))));
- }else{
- mgr.addAxioms(inputowl,auxonto.getAxioms());
+ // Load input file or graph
+ if (file != null) this.inputowl = OWLManager.createOWLOntologyManager()
+ .loadOntologyFromOntologyDocument(file);
+ if (input_graph != null) this.inputowl = OWLManager.createOWLOntologyManager()
+ .loadOntologyFromOntologyDocument(IRI.create(input_graph));
+
+ if (inputowl == null) return Response.status(NOT_FOUND).build();
+
+ // Create list to add ontologies as imported
+ OWLOntologyManager mgr = inputowl.getOWLOntologyManager();
+ OWLDataFactory factory = inputowl.getOWLOntologyManager().getOWLDataFactory();
+ List<OWLOntologyChange> additions = new LinkedList<OWLOntologyChange>();
+
+ // Load ontologies from scope, RDF input and recipe
+ // Try to resolve scope IRI
+ if ((scope != null) && (session == null)) try {
+ IRI iri = IRI.create(scope);
+ ScopeRegistry reg = onm.getScopeRegistry();
+ OntologyScope ontoscope = reg.getScope(iri);
+ Iterator<OWLOntology> importscope = ontoscope.getCustomSpace().getOntologies().iterator();
+ Iterator<OntologySpace> importsession = ontoscope.getSessionSpaces().iterator();
+
+ // Add ontology as import form scope, if it is anonymus we
+ // try to add single axioms.
+ while (importscope.hasNext()) {
+ OWLOntology auxonto = importscope.next();
+ if (!auxonto.getOntologyID().isAnonymous()) {
+ additions.add(new AddImport(inputowl, factory.getOWLImportsDeclaration(auxonto
+ .getOWLOntologyManager().getOntologyDocumentIRI(auxonto))));
+ } else {
+ mgr.addAxioms(inputowl, auxonto.getAxioms());
+ }
}
- }
- }
+ // Add ontology form sessions
+ while (importsession.hasNext()) {
+ Iterator<OWLOntology> sessionontos = importsession.next().getOntologies().iterator();
+ while (sessionontos.hasNext()) {
+ OWLOntology auxonto = sessionontos.next();
+ if (!auxonto.getOntologyID().isAnonymous()) {
+ additions.add(new AddImport(inputowl, factory.getOWLImportsDeclaration(auxonto
+ .getOWLOntologyManager().getOntologyDocumentIRI(auxonto))));
+ } else {
+ mgr.addAxioms(inputowl, auxonto.getAxioms());
+ }
+ }
+
+ }
- }catch(Exception e){
- System.err.println("ERROR: Problem with scope: "+scope);
- e.printStackTrace();
- Response.status(Status.NOT_FOUND).build();
- }
-
- //Get Ontologies from session
- if((session!=null)&&(scope!=null))
- try{
- IRI iri = IRI.create(scope);
- ScopeRegistry reg = onm.getScopeRegistry();
- OntologyScope ontoscope = reg.getScope(iri);
- SessionOntologySpace sos = ontoscope.getSessionSpace(IRI
- .create(session));
-
- Set<OWLOntology> ontos = sos.getOntologyManager()
- .getOntologies();
- Iterator<OWLOntology> iteronto = ontos.iterator();
-
- // Add session ontologies as import, if it is anonymus we
- // try to add single axioms.
- while(iteronto.hasNext()){
- OWLOntology auxonto = iteronto.next();
- if(!auxonto.getOntologyID().isAnonymous()){
- additions.add(new AddImport(inputowl, factory
- .getOWLImportsDeclaration(auxonto
- .getOWLOntologyManager()
- .getOntologyDocumentIRI(auxonto))));
- }else{
- mgr.addAxioms(inputowl,auxonto.getAxioms());
+ } catch (Exception e) {
+ throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
}
- }
- }catch(Exception e){
- System.err.println("ERROR: Problem with session: "
- + session);
- e.printStackTrace();
- Response.status(Status.NOT_FOUND).build();
- }
-
- // After gathered the all ontology as imported now we apply the
- // changes
- if(additions.size()>0)
- mgr.applyChanges(additions);
-
- //Run HermiT if the reasonerURL is null;
- if(owllink_endpoint==null){
-
- try{
- if(recipe!=null) {
- OWLOntology recipeowl = OWLManager
- .createOWLOntologyManager()
- .loadOntologyFromOntologyDocument(
- IRI.create(recipe));
- // Get Jea RDF model of SWRL rule contained in the
- // recipe
- Model swrlmodel = fromRecipeToModel(recipeowl);
-
- // Create a reasoner to run rules contained in the
- // recipe
- RunRules rulereasoner = new RunRules(swrlmodel,
- inputowl);
- // Run the rule reasoner to the input RDF with the added
- // top-ontology
- inputowl = rulereasoner.runRulesReasoner();
- }
-
- //Create the reasoner for the enrichment
- CreateReasoner newreasoner = new CreateReasoner(
- inputowl);
- // Prepare and start the reasoner to enrich ontology's
- // resources
- RunReasoner reasoner = new RunReasoner(newreasoner
- .getReasoner());
-
- // Create a new OWLOntology model where to put the inferred
- // axioms
- OWLOntology output = OWLManager.createOWLOntologyManager()
- .createOntology(inputowl.getOntologyID());
- //Initial input axioms count
- int startax = output.getAxiomCount();
- //Run the classification
- output = reasoner.runGeneralInference(output);
- //End output axioms count
- int endax = output.getAxiomCount();
-
- if((endax-startax)>0){
- //Some inference is retrieved
- return Response.ok(output).build();
- }else{
- //No data is retrieved
- return Response.status(Status.NOT_FOUND).build();
+ // Get Ontologies from session
+ if ((session != null) && (scope != null)) try {
+ IRI iri = IRI.create(scope);
+ ScopeRegistry reg = onm.getScopeRegistry();
+ OntologyScope ontoscope = reg.getScope(iri);
+ SessionOntologySpace sos = ontoscope.getSessionSpace(IRI.create(session));
+
+ Set<OWLOntology> ontos = sos.getOntologyManager().getOntologies();
+ Iterator<OWLOntology> iteronto = ontos.iterator();
+
+ // Add session ontologies as import, if it is anonymous we
+ // try to add single axioms.
+ while (iteronto.hasNext()) {
+ OWLOntology auxonto = iteronto.next();
+ if (!auxonto.getOntologyID().isAnonymous()) {
+ additions.add(new AddImport(inputowl, factory.getOWLImportsDeclaration(auxonto
+ .getOWLOntologyManager().getOntologyDocumentIRI(auxonto))));
+ } else {
+ mgr.addAxioms(inputowl, auxonto.getAxioms());
+ }
+ }
+
+ } catch (Exception e) {
+ throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
}
- }catch (InconsistentOntologyException exc){
- System.err.println("CHECK ONTOLOGY CONSISTENCE");
- return Response.status(Status.NOT_FOUND).build();
- }
- // If there is an owl-link server end-point specified in the
- // form
- }else{
-
- try{
- if(recipe!=null) {
- OWLOntology recipeowl = OWLManager
- .createOWLOntologyManager()
- .loadOntologyFromOntologyDocument(
- IRI.create(recipe));
- // Get Jea RDF model of SWRL rule contained in the
- // recipe
- Model swrlmodel = fromRecipeToModel(recipeowl);
- // Create a reasoner to run rules contained in the
- // recipe by using the server and-point
- RunRules rulereasoner = new RunRules(swrlmodel,
- inputowl, new URL(owllink_endpoint));
- // Run the rule reasoner to the input RDF with the added
- // top-ontology
- inputowl = rulereasoner.runRulesReasoner();
- }
-
- // Create a new OWLOntology model where to put the inferred
- // axioms
- OWLOntology output = OWLManager.createOWLOntologyManager()
- .createOntology(inputowl.getOntologyID());
-
- //Create the reasoner for the enrichment
- CreateReasoner newreasoner = new CreateReasoner(
- inputowl, new URL(owllink_endpoint));
- // Prepare and start the reasoner to enrich ontology's
- // resources
- RunReasoner reasoner = new RunReasoner(newreasoner
- .getReasoner());
-
- //Initial input axioms count
- int startax = output.getAxiomCount();
- //Run the rule reasoner
- output = reasoner.runGeneralInference(output);
- //End output axioms count
- int endax = output.getAxiomCount();
-
- if((endax-startax)>0){
- //No data is retrieved, the graph IS consistent
- return Response.ok(output).build();
- }else{
- //No data is retrieved, the graph IS NOT consistent
- return Response.status(Status.NO_CONTENT).build();
- }
- }catch (InconsistentOntologyException exc){
- System.err.println("CHECK ONTOLOGY CONSISTENCE");
- return Response.status(Status.NOT_FOUND).build();
+
+ // After gathered the all ontology as imported now we apply the changes
+ if (additions.size() > 0) mgr.applyChanges(additions);
+
+ // Run HermiT if the reasonerURL is null;
+ if (owllink_endpoint == null) {
+
+ try {
+ if (recipe != null) {
+ OWLOntology recipeowl = OWLManager.createOWLOntologyManager()
+ .loadOntologyFromOntologyDocument(IRI.create(recipe));
+ // Get Jea RDF model of SWRL rule contained in the recipe
+ Model swrlmodel = fromRecipeToModel(recipeowl);
+
+ // Create a reasoner to run rules contained in the recipe
+ RunRules rulereasoner = new RunRules(swrlmodel, inputowl);
+ // Run the rule reasoner to the input RDF with the added top-ontology
+ inputowl = rulereasoner.runRulesReasoner();
+ }
+
+ // Create the reasoner for the enrichment
+ CreateReasoner newreasoner = new CreateReasoner(inputowl);
+ // Prepare and start the reasoner to enrich ontology's resources
+ RunReasoner reasoner = new RunReasoner(newreasoner.getReasoner());
+
+ // Create a new OWLOntology model where to put the inferred axioms
+ OWLOntology output = OWLManager.createOWLOntologyManager().createOntology(
+ inputowl.getOntologyID());
+ // Initial input axioms count
+ int startax = output.getAxiomCount();
+ // Run the classification
+ output = reasoner.runGeneralInference(output);
+ // End output axioms count
+ int endax = output.getAxiomCount();
+
+ if ((endax - startax) > 0) {
+ // Some inference is retrieved
+ return Response.ok(output).build();
+ } else {
+ // No data is retrieved
+ return Response.status(NOT_FOUND).build();
+ }
+ } catch (InconsistentOntologyException exc) {
+ log.error("Cannot classify ionconsistent ontology " + inputowl.getOntologyID(), exc);
+ return Response.status(PRECONDITION_FAILED).build();
+ }
+ // If there is an owl-link server end-point specified in the form
+ } else {
+
+ try {
+ if (recipe != null) {
+ OWLOntology recipeowl = OWLManager.createOWLOntologyManager()
+ .loadOntologyFromOntologyDocument(IRI.create(recipe));
+ // Get Jea RDF model of SWRL rule contained in the recipe
+ Model swrlmodel = fromRecipeToModel(recipeowl);
+ // Create a reasoner to run rules contained in the recipe by using the server
+ // and-point
+ RunRules rulereasoner = new RunRules(swrlmodel, inputowl, new URL(owllink_endpoint));
+ // Run the rule reasoner to the input RDF with the added top-ontology
+ inputowl = rulereasoner.runRulesReasoner();
+ }
+
+ // Create a new OWLOntology model where to put the inferred axioms
+ OWLOntology output = OWLManager.createOWLOntologyManager().createOntology(
+ inputowl.getOntologyID());
+
+ // Create the reasoner for the enrichment
+ CreateReasoner newreasoner = new CreateReasoner(inputowl, new URL(owllink_endpoint));
+ // Prepare and start the reasoner to enrich ontology resources
+ RunReasoner reasoner = new RunReasoner(newreasoner.getReasoner());
+
+ // Initial input axioms count
+ int startax = output.getAxiomCount();
+ // Run the rule reasoner
+ output = reasoner.runGeneralInference(output);
+ // End output axioms count
+ int endax = output.getAxiomCount();
+
+ if ((endax - startax) > 0) {
+ // No data is retrieved, the graph IS consistent
+ return Response.ok(output).build();
+ } else {
+ // No data is retrieved, the graph IS NOT consistent
+ return Response.status(NO_CONTENT).build();
+ }
+ } catch (InconsistentOntologyException exc) {
+ log.error("Cannot classify ionconsistent ontology " + inputowl.getOntologyID(), exc);
+ return Response.status(PRECONDITION_FAILED).build();
+ }
+ }
+ } catch (Exception e) {
+ throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
}
- }
- }catch(Exception e){
- throw new WebApplicationException(e, Status.INTERNAL_SERVER_ERROR);
- }
}
-
- @GET
- @Produces(TEXT_HTML)
- public Response getView() {
- return Response.ok(new Viewable("index", this), TEXT_HTML).build();
- }
-
+
}
Modified: incubator/stanbol/trunk/reasoners/web/src/main/java/org/apache/stanbol/reasoners/web/resources/ReasonersResource.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/reasoners/web/src/main/java/org/apache/stanbol/reasoners/web/resources/ReasonersResource.java?rev=1143414&r1=1143413&r2=1143414&view=diff
==============================================================================
--- incubator/stanbol/trunk/reasoners/web/src/main/java/org/apache/stanbol/reasoners/web/resources/ReasonersResource.java (original)
+++ incubator/stanbol/trunk/reasoners/web/src/main/java/org/apache/stanbol/reasoners/web/resources/ReasonersResource.java Wed Jul 6 13:51:59 2011
@@ -15,16 +15,12 @@ import org.apache.stanbol.commons.web.ba
import com.sun.jersey.api.view.ImplicitProduces;
import com.sun.jersey.api.view.Viewable;
-
@Path("/reasoners")
@ImplicitProduces(MediaType.TEXT_HTML + ";qs=2")
public class ReasonersResource extends BaseStanbolResource {
- public ReasonersResource(@Context ServletContext servletContext) {
-
+ public ReasonersResource(@Context ServletContext servletContext) {}
- }
-
@GET
@Produces(TEXT_HTML)
public Response get() {