Author: enridaga
Date: Sat Jul 2 08:57:59 2011
New Revision: 1142178
URL: http://svn.apache.org/viewvc?rev=1142178&view=rev
Log:
Fixed the way components are loaded from the bundle context.
Removed useless (and wrong) local instantiation of components when not found in
he context.
Fixed the way the Classify() resource n the reasoners/web module prepare the
rules to compute. This metod strangely did the convertion to the jena model and
then back to the owlapi model. This was odd.
(STANBOL-177)
Modified:
incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/resources/SessionResource.java
incubator/stanbol/trunk/reasoners/web/src/main/java/org/apache/stanbol/reasoners/web/resources/Classify.java
incubator/stanbol/trunk/rules/web/src/main/java/org/apache/stanbol/rules/web/resources/RecipeResource.java
incubator/stanbol/trunk/rules/web/src/main/java/org/apache/stanbol/rules/web/resources/RuleResource.java
Modified:
incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/resources/SessionResource.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/resources/SessionResource.java?rev=1142178&r1=1142177&r2=1142178&view=diff
==============================================================================
---
incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/resources/SessionResource.java
(original)
+++
incubator/stanbol/trunk/ontologymanager/web/src/main/java/org/apache/stanbol/ontologymanager/web/resources/SessionResource.java
Sat Jul 2 08:57:59 2011
@@ -60,7 +60,7 @@ public class SessionResource extends Bas
this.onm = (ONManager)
ContextHelper.getServiceFromContext(ONManager.class, servletContext);
}
- @PUT
+ @POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response addOntology(@FormDataParam("scope") String scope,
@FormDataParam("import") InputStream
importOntology,
@@ -92,15 +92,31 @@ public class SessionResource extends Bas
}
- @PUT
+ /**
+ *
+ * If the session param is missing, this method creates a new session,
ignoring other params
+ *
+ * @param scope
+ * @param session
+ * @param location
+ * @param uriInfo
+ * @param headers
+ * @param servletContext
+ * @return
+ */
+ @POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
+ @Produces(value = {KRFormat.RDF_XML, KRFormat.OWL_XML, KRFormat.TURTLE,
KRFormat.FUNCTIONAL_OWL,
+ KRFormat.MANCHESTER_OWL, KRFormat.RDF_JSON})
public Response addOntology(@FormParam("scope") String scope,
@FormParam("session") String session,
@FormParam("location") String location,
@Context UriInfo uriInfo,
@Context HttpHeaders headers,
@Context ServletContext servletContext) {
-
+ if(session==null||session.equals("")){
+ return createSession(scope, uriInfo, headers);
+ }else{
IRI scopeIRI = IRI.create(scope);
IRI sessionIRI = IRI.create(session);
IRI ontologyIRI = IRI.create(location);
@@ -116,17 +132,22 @@ public class SessionResource extends Bas
} catch (OWLOntologyCreationException e) {
return Response.status(INTERNAL_SERVER_ERROR).build();
}
-
+ }
}
-
- @POST
- @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
- @Produces(value = {KRFormat.RDF_XML, KRFormat.OWL_XML, KRFormat.TURTLE,
KRFormat.FUNCTIONAL_OWL,
- KRFormat.MANCHESTER_OWL, KRFormat.RDF_JSON})
- public Response createSession(@FormParam("scope") String scope,
- @Context UriInfo uriInfo,
- @Context HttpHeaders headers) {
-
+ /**
+ * This method creates a session.
+ *
+ * @param scope
+ * @param uriInfo
+ * @param headers
+ * @return
+ */
+ private Response createSession(String scope,
+ UriInfo uriInfo,
+ HttpHeaders headers) {
+ if(scope==null||scope.equals("")){
+ return Response.status(INTERNAL_SERVER_ERROR).build();
+ }
Session ses = null;
SessionManager mgr = onm.getSessionManager();
Modified:
incubator/stanbol/trunk/reasoners/web/src/main/java/org/apache/stanbol/reasoners/web/resources/Classify.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/reasoners/web/src/main/java/org/apache/stanbol/reasoners/web/resources/Classify.java?rev=1142178&r1=1142177&r2=1142178&view=diff
==============================================================================
---
incubator/stanbol/trunk/reasoners/web/src/main/java/org/apache/stanbol/reasoners/web/resources/Classify.java
(original)
+++
incubator/stanbol/trunk/reasoners/web/src/main/java/org/apache/stanbol/reasoners/web/resources/Classify.java
Sat Jul 2 08:57:59 2011
@@ -7,6 +7,8 @@ package org.apache.stanbol.reasoners.web
import java.io.File;
import java.net.URL;
+import java.util.ArrayList;
+import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
@@ -46,16 +48,20 @@ import org.apache.stanbol.rules.manager.
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.AddImport;
import org.semanticweb.owlapi.model.IRI;
+import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLClassAssertionAxiom;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLDataProperty;
+import org.semanticweb.owlapi.model.OWLDatatype;
import org.semanticweb.owlapi.model.OWLIndividual;
import org.semanticweb.owlapi.model.OWLLiteral;
import org.semanticweb.owlapi.model.OWLObjectProperty;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyChange;
+import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
+import org.semanticweb.owlapi.model.SWRLRule;
import org.semanticweb.owlapi.reasoner.InconsistentOntologyException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -118,15 +124,15 @@ public class Classify extends BaseStanbo
*
* @param owl
* {OWLOntology object contains a single recipe}
- * @return {A jena rdf model contains the SWRL rule.}
+ * @return {An Set<SWRLRule> that contains the SWRL rule.}
*/
- private Model fromRecipeToModel(OWLOntology owl)
+ private Set<SWRLRule> 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);
- Model jenamodel = ModelFactory.createDefaultModel();
+ //Model jenamodel = ModelFactory.createDefaultModel();
OWLDataFactory factory = owl.getOWLOntologyManager()
.getOWLDataFactory();
@@ -160,18 +166,38 @@ public class Classify extends BaseStanbo
}
//"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) ]");
+ System.out.println(kReSRules);
+ // We escape back the XML entities
+ //kReSRules = kReSRules.replaceAll("<", "<");
+ //kReSRules = kReSRules.replaceAll(">", ">");
+ System.out.println(kReSRules);
+
KB kReSKB = RuleParserImpl.parse(kReSRules);
RuleList listrules = kReSKB.getkReSRuleList();
Iterator<Rule> iterule = listrules.iterator();
+ Set<SWRLRule> swrlrules = new HashSet<SWRLRule>();
while(iterule.hasNext()){
Rule singlerule = iterule.next();
- Resource resource = singlerule.toSWRL(jenamodel);
+ System.out.println("Single rule: "+singlerule.toSPARQL());
+ System.out.println("To KReS Syntax: "+singlerule.toKReSSyntax());
+ System.out.println("Single OWLAPI SWRL:
"+singlerule.toSWRL(factory));
+ //Resource resource = singlerule.toSWRL(jenamodel); <-- FIXME This
method does not work properly
+ swrlrules.add(singlerule.toSWRL(factory));
}
-
- return jenamodel;
+ return swrlrules;
}
-
+ @POST
+ @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
+ @Produces(value = { KRFormat.RDF_XML, KRFormat.TURTLE,
+ KRFormat.OWL_XML })
+ public Response classify(
+ @FormParam(value = "session") String session,
+ @FormParam(value = "scope") String scope,
+ @FormParam(value = "recipe") String recipe
+ ){
+ return ontologyClassify(session,scope,recipe,null,null,null);
+ }
/**
* To run a classifying reasoner on 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
@@ -209,9 +235,9 @@ public class Classify extends BaseStanbo
@FormDataParam(value = "input-graph") String
input_graph,
@FormDataParam(value = "file") File file,
@FormDataParam(value = "owllink-endpoint") String
owllink_endpoint) {
-
- if(true)
- return Response.status(Status.OK).build();
+ System.out.println("CLASSIFY");
+ /* if(true)
+ return Response.status(Status.OK).build();*/
try{
if((session!=null)&&(scope==null)){
@@ -349,20 +375,39 @@ public class Classify extends BaseStanbo
//Run HermiT if the reasonerURL is null;
if(owllink_endpoint==null){
-
+ /**
+ * If we run hermit, we must remove all datatype assertions
+ * from the ontology. Non default datatypes, such
http://dbpedia.org/datatype/hour
+ * would break the process
+ */
+ Set<OWLAxiom> removeThese = new HashSet<OWLAxiom>();
+ for(OWLAxiom axiom: inputowl.getAxioms()){
+ if(!axiom.getDatatypesInSignature().isEmpty()){
+ Set<OWLDatatype> datatypes =
axiom.getDatatypesInSignature();
+ for(OWLDatatype datatype : datatypes){
+ if(!datatype.isBuiltIn()){
+ removeThese.add(axiom);
+ break;
+ }
+ }
+ }
+ }
+ inputowl.getOWLOntologyManager().removeAxioms(inputowl, removeThese);
+ inputowl =
inputowl.getOWLOntologyManager().getOntology(inputowl.getOntologyID());
try{
if(recipe!=null) {
- OWLOntology recipeowl =
OWLManager
-
.createOWLOntologyManager()
+ OWLOntologyManager mngr = OWLManager
+ .createOWLOntologyManager();
+ OWLOntology recipeowl = mngr
.loadOntologyFromOntologyDocument(
IRI.create(recipe));
- // Get Jea RDF model of SWRL
rule contained in the
- // recipe
- Model swrlmodel = fromRecipeToModel(recipeowl);
-
+ OWLOntology rulesOntology =
mngr.createOntology();
+ Set<SWRLRule> swrlRules =
fromRecipeToModel(recipeowl);
+ mngr.addAxioms(rulesOntology,
swrlRules);
+ rulesOntology =
mngr.getOntology(rulesOntology.getOntologyID());
// Create a reasoner to run
rules contained in the
// recipe
- RunRules rulereasoner = new
RunRules(swrlmodel,
+ RunRules rulereasoner = new
RunRules(rulesOntology,
inputowl);
// Run the rule reasoner to the
input RDF with the added
// top-ontology
@@ -407,16 +452,19 @@ public class Classify extends BaseStanbo
try{
if(recipe!=null) {
- OWLOntology recipeowl =
OWLManager
-
.createOWLOntologyManager()
+
+ OWLOntologyManager mngr = OWLManager
+ .createOWLOntologyManager();
+ OWLOntology recipeowl = mngr
.loadOntologyFromOntologyDocument(
IRI.create(recipe));
- // Get Jea RDF model of SWRL
rule contained in the
- // recipe
- Model swrlmodel = fromRecipeToModel(recipeowl);
+ OWLOntology rulesOntology =
mngr.createOntology();
+ Set<SWRLRule> swrlRules =
fromRecipeToModel(recipeowl);
+ mngr.addAxioms(rulesOntology,
swrlRules);
+ rulesOntology =
mngr.getOntology(rulesOntology.getOntologyID());
// Create a reasoner to run
rules contained in the
// recipe by using the server
and-point
- RunRules rulereasoner = new
RunRules(swrlmodel,
+ RunRules rulereasoner = new
RunRules(rulesOntology,
inputowl, new
URL(owllink_endpoint));
// Run the rule reasoner to the
input RDF with the added
// top-ontology
Modified:
incubator/stanbol/trunk/rules/web/src/main/java/org/apache/stanbol/rules/web/resources/RecipeResource.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/rules/web/src/main/java/org/apache/stanbol/rules/web/resources/RecipeResource.java?rev=1142178&r1=1142177&r2=1142178&view=diff
==============================================================================
---
incubator/stanbol/trunk/rules/web/src/main/java/org/apache/stanbol/rules/web/resources/RecipeResource.java
(original)
+++
incubator/stanbol/trunk/rules/web/src/main/java/org/apache/stanbol/rules/web/resources/RecipeResource.java
Sat Jul 2 08:57:59 2011
@@ -30,11 +30,13 @@ import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.apache.clerezza.rdf.core.access.TcManager;
+import org.apache.stanbol.commons.web.base.ContextHelper;
import org.apache.stanbol.commons.web.base.format.KRFormat;
import org.apache.stanbol.commons.web.base.resource.BaseStanbolResource;
import org.apache.stanbol.ontologymanager.ontonet.api.ONManager;
import org.apache.stanbol.ontologymanager.ontonet.impl.ONManagerImpl;
import
org.apache.stanbol.ontologymanager.ontonet.impl.io.ClerezzaOntologyStorage;
+import org.apache.stanbol.rules.base.api.RuleStore;
import org.apache.stanbol.rules.manager.changes.AddRecipe;
import org.apache.stanbol.rules.manager.changes.GetRecipe;
import org.apache.stanbol.rules.manager.changes.RemoveRecipe;
@@ -76,32 +78,9 @@ public class RecipeResource extends Base
* {To get the context where the REST service is running.}
*/
public RecipeResource(@Context ServletContext servletContext) {
- this.kresRuleStore = (RuleStoreImpl)
servletContext.getAttribute(RuleStoreImpl.class.getName());
- this.onm = (ONManager)
servletContext.getAttribute(ONManager.class.getName());
- // this.storage = (OntologyStorage) servletContext
- // .getAttribute(OntologyStorage.class.getName());
- // Contingency code for missing components follows.
-
- /*
- * FIXME! The following code is required only for the
tests. This should
- * be removed and the test should work without this
code.
- */
- if (onm == null) {
- log.warn("No ONManager in servlet context. Instantiating
manually...");
- onm = new ONManagerImpl(new TcManager(), null,
- new Hashtable<String, Object>());
- }
- this.storage = onm.getOntologyStore();
- if (storage == null) {
- log.warn("No OntologyStorage in servlet context.
Instantiating manually...");
- storage = new ClerezzaOntologyStorage(new TcManager(),null);
- }
- if (kresRuleStore == null) {
- log.warn("No RuleStore with stored rules and recipes found in
servlet context. Instantiating manually with default values...");
-
- this.kresRuleStore = new RuleStoreImpl(onm, new
Hashtable<String,Object>(), "");
-
- }
+ this.kresRuleStore = (RuleStoreImpl)
ContextHelper.getServiceFromContext(RuleStore.class, servletContext);
+ this.onm = (ONManager)
ContextHelper.getServiceFromContext(ONManager.class, servletContext);
+ this.storage = onm.getOntologyStore();
}
/**
Modified:
incubator/stanbol/trunk/rules/web/src/main/java/org/apache/stanbol/rules/web/resources/RuleResource.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/rules/web/src/main/java/org/apache/stanbol/rules/web/resources/RuleResource.java?rev=1142178&r1=1142177&r2=1142178&view=diff
==============================================================================
---
incubator/stanbol/trunk/rules/web/src/main/java/org/apache/stanbol/rules/web/resources/RuleResource.java
(original)
+++
incubator/stanbol/trunk/rules/web/src/main/java/org/apache/stanbol/rules/web/resources/RuleResource.java
Sat Jul 2 08:57:59 2011
@@ -32,6 +32,7 @@ import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.apache.clerezza.rdf.core.access.TcManager;
+import org.apache.stanbol.commons.web.base.ContextHelper;
import org.apache.stanbol.commons.web.base.format.KRFormat;
import org.apache.stanbol.commons.web.base.resource.BaseStanbolResource;
import org.apache.stanbol.ontologymanager.ontonet.api.ONManager;
@@ -80,32 +81,9 @@ public class RuleResource extends BaseSt
* {To get the context where the REST service is running.}
*/
public RuleResource(@Context ServletContext servletContext){
- this.kresRuleStore = (RuleStore) servletContext
- .getAttribute(RuleStore.class.getName());
- this.onm = (ONManager) servletContext
- .getAttribute(ONManager.class.getName());
-// this.storage = (OntologyStorage) servletContext
-// .getAttribute(OntologyStorage.class.getName());
- // Contingency code for missing components follows.
- /*
- * FIXME! The following code is required only for the tests.
This should
- * be removed and the test should work without this code.
- */
- if (onm == null) {
- log.warn("No KReSONManager in servlet context. Instantiating
manually...");
- onm = new ONManagerImpl(new TcManager(), null,new
Hashtable<String, Object>());
- }
- this.storage = onm.getOntologyStore();
- if (storage == null) {
- log.warn("No OntologyStorage in servlet context. Instantiating
manually...");
- storage = new ClerezzaOntologyStorage(new TcManager(),null);
- }
-
- 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());
- }
+ this.kresRuleStore = (RuleStoreImpl)
ContextHelper.getServiceFromContext(RuleStore.class, servletContext);
+ this.onm = (ONManager)
ContextHelper.getServiceFromContext(ONManager.class, servletContext);
+ this.storage = this.onm.getOntologyStore();
}
/**