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/06/29 16:06:05 UTC
svn commit: r1141107 [1/2] - in /incubator/stanbol/trunk/rules/manager: ./
src/main/java/org/apache/stanbol/rules/manager/
src/main/java/org/apache/stanbol/rules/manager/changes/
src/main/java/org/apache/stanbol/rules/manager/parse/
Author: alexdma
Date: Wed Jun 29 14:06:05 2011
New Revision: 1141107
URL: http://svn.apache.org/viewvc?rev=1141107&view=rev
Log:
STANBOL-178 :
- Simplified local fallback for RuleStoreImpl when the rule ontology is missing
Modified:
incubator/stanbol/trunk/rules/manager/pom.xml
incubator/stanbol/trunk/rules/manager/src/main/java/org/apache/stanbol/rules/manager/RuleParser.java
incubator/stanbol/trunk/rules/manager/src/main/java/org/apache/stanbol/rules/manager/changes/RuleStoreImpl.java
incubator/stanbol/trunk/rules/manager/src/main/java/org/apache/stanbol/rules/manager/parse/RuleParserImpl.java
Modified: incubator/stanbol/trunk/rules/manager/pom.xml
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/rules/manager/pom.xml?rev=1141107&r1=1141106&r2=1141107&view=diff
==============================================================================
--- incubator/stanbol/trunk/rules/manager/pom.xml (original)
+++ incubator/stanbol/trunk/rules/manager/pom.xml Wed Jun 29 14:06:05 2011
@@ -41,13 +41,19 @@
<extensions>true</extensions>
<configuration>
<instructions>
+ <Bundle-ClassPath>
+ .,
+ META-INF,
+ META-INF/conf,
+ RuleOntology
+ </Bundle-ClassPath>
<Export-Package>
org.apache.stanbol.rules.manager.*;version=${stanbol-version}
- </Export-Package>
+ </Export-Package>
<Import-Package>
org.slf4j.*,
*
- </Import-Package>
+ </Import-Package>
<_nouses>true</_nouses>
</instructions>
</configuration>
Modified: incubator/stanbol/trunk/rules/manager/src/main/java/org/apache/stanbol/rules/manager/RuleParser.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/rules/manager/src/main/java/org/apache/stanbol/rules/manager/RuleParser.java?rev=1141107&r1=1141106&r2=1141107&view=diff
==============================================================================
--- incubator/stanbol/trunk/rules/manager/src/main/java/org/apache/stanbol/rules/manager/RuleParser.java (original)
+++ incubator/stanbol/trunk/rules/manager/src/main/java/org/apache/stanbol/rules/manager/RuleParser.java Wed Jun 29 14:06:05 2011
@@ -1,31 +1,23 @@
package org.apache.stanbol.rules.manager;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
-import com.hp.hpl.jena.ontology.EnumeratedClass;
+import org.apache.stanbol.rules.base.SWRL;
+
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.ontology.OntResource;
-import com.hp.hpl.jena.rdf.model.Bag;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
-import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFList;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
-import com.hp.hpl.jena.util.CollectionFactory;
import com.hp.hpl.jena.vocabulary.RDF;
-import org.apache.stanbol.rules.base.SWRL;
-
public class RuleParser {
public static String ruleNS = "http://www.prova.org/rules.rdf#";
Modified: incubator/stanbol/trunk/rules/manager/src/main/java/org/apache/stanbol/rules/manager/changes/RuleStoreImpl.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/rules/manager/src/main/java/org/apache/stanbol/rules/manager/changes/RuleStoreImpl.java?rev=1141107&r1=1141106&r2=1141107&view=diff
==============================================================================
--- incubator/stanbol/trunk/rules/manager/src/main/java/org/apache/stanbol/rules/manager/changes/RuleStoreImpl.java (original)
+++ incubator/stanbol/trunk/rules/manager/src/main/java/org/apache/stanbol/rules/manager/changes/RuleStoreImpl.java Wed Jun 29 14:06:05 2011
@@ -9,6 +9,7 @@ import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.net.URISyntaxException;
import java.net.URL;
import java.util.Collections;
import java.util.Dictionary;
@@ -50,8 +51,8 @@ import org.semanticweb.owlapi.model.OWLO
import org.semanticweb.owlapi.model.OWLOntologyIRIMapper;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.OWLOntologyStorageException;
-import org.semanticweb.owlapi.util.AutoIRIMapper;
import org.semanticweb.owlapi.util.OWLEntityRemover;
+import org.semanticweb.owlapi.util.SimpleIRIMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -62,37 +63,64 @@ import org.slf4j.LoggerFactory;
* @author andrea.nuzzolese
*
*/
-
@Component(immediate = true, metatype = true)
@Service(RuleStore.class)
public class RuleStoreImpl implements RuleStore {
- public static final String _RULE_ONTOLOGY_DEFAULT = "";
-
public static final String _RULE_NAMESPACE_DEFAULT = "http://kres.iks-project.eu/ontology/meta/rmi.owl#";
- @Reference
- ONManager onManager;
+ public static final String _RULE_ONTOLOGY_DEFAULT = "http://ontologydesignpatterns.org/ont/iks/kres/rmi_config.owl";
- @Property(name = RuleStore.RULE_ONTOLOGY, value = _RULE_ONTOLOGY_DEFAULT)
- private String ruleOntologyLocation;
+ /**
+ * Rewrites the rule ontology location and overwrites the given configuration.
+ *
+ * @param configuration
+ * @param filepath
+ * @return
+ */
+ private static Dictionary<String,Object> _reconfigureLocation(Dictionary<String,Object> configuration,
+ String filepath) {
- @Property(name = RuleStore.RULE_NAMESPACE, value = _RULE_NAMESPACE_DEFAULT)
- private String ruleNS;
+ String location;
+ if (filepath.isEmpty()) {
- // @Property(value = "http://kres.iks-project.eu/ontology/meta/rmi.owl#")
- // public static final String RULE_ONTOLOGY_NAMESPACE = "rule.ontology.namespace";
+ // Obsolete code
+ Properties configProps = System.getProperties();
+ String userdir = configProps.getProperty("user.dir");
- private final Logger log = LoggerFactory.getLogger(getClass());
- private OWLOntology owlmodel;
+ String respath = "KReSConf/"; // "src/main/resources/";
+ String filepath2 = "rmi_config.owl"; // "RuleOntology/rmi_config.owl";
+ // userdir = userdir.substring(0, userdir.lastIndexOf("kres.") + 5) + "rules/";
+
+ userdir += "/";
+
+ location = userdir + respath + filepath2;
+
+ } else location = filepath;
+
+ // We do this instead of setting the global, non-static var
+ configuration.put(RuleStore.RULE_ONTOLOGY, location);
+
+ return configuration;
+ }
private String alias;
- private RuleStore ruleStore;
- private File owlfile;
- private String ruleOntologyNS;
private RuleParserImpl kReSRuleParser;
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ @Reference
+ ONManager onManager;
+
+ private OWLOntology owlmodel;
+
+ @Property(name = RuleStore.RULE_NAMESPACE, value = _RULE_NAMESPACE_DEFAULT)
+ private String ruleNS;
+
+ @Property(name = RuleStore.RULE_ONTOLOGY, value = _RULE_ONTOLOGY_DEFAULT)
+ private String ruleOntologyLocation;
+
/**
* This construct returns RuleStoreImpl object with inside an ontology where to store the rules.
*
@@ -111,77 +139,8 @@ public class RuleStoreImpl implements Ru
* @param configuration
*/
public RuleStoreImpl(ONManager onm, Dictionary<String,Object> configuration) {
- this();
- this.onManager = onm;
- // activate(configuration);
- }
-
- /**
- * This construct returns an ontology where to store the rules.
- *
- * @param filepath
- * {Ontology file path previously stored.}
- */
- public RuleStoreImpl(ONManager onm, Dictionary<String,Object> configuration, String filepath) {
- /*
- * FIXME : This won't work if the activate() method is called at the end of the constructor, like it
- * is for other components. Constructors should not override activate().
- *
- * Normally, this recursive constructor call should also invoke activate()
- */
- this(onm, configuration);
-
- if (filepath.isEmpty()) {
- try {
-
- // Obsolete code
- Properties configProps = System.getProperties();
- String userdir = configProps.getProperty("user.dir");
-
- String respath = "KReSConf/";// "src/main/resources/";
- String filepath2 = "rmi_config.owl"; // "RuleOntology/rmi_config.owl";
- // userdir = userdir.substring(0, userdir.lastIndexOf("kres.") + 5) + "rules/";
-
- userdir += "/";
-
- this.ruleOntologyLocation = userdir + respath + filepath2;
- this.owlfile = new File(ruleOntologyLocation);
- owlfile.setWritable(true);
-
- // InputStream is = this.getClass().getResourceAsStream("/RuleOntology/rmi_config.owl");
- // URL url = this.getClass().getResource("/RuleOntology/rmi_config.owl");
- // System.err.println("DIOCANE "+url.toURI());
- // this.owlfile = new File(url.toURI());
- // owlfile.setWritable(true);
-
- OWLOntologyManager owlmanager = OWLManager.createOWLOntologyManager();
-
- // FIXME : awful
- URL u = getClass().getResource("/");
- OWLOntologyIRIMapper mapper = new AutoIRIMapper(new File(u.toURI()), true);
- owlmanager.addIRIMapper(mapper);
-
- this.owlmodel = owlmanager.loadOntology(IRI.create(owlfile));
-
- } catch (Exception io) {
- log.error(io.getLocalizedMessage(), io);
- this.owlmodel = null;
- }
- } else {
- this.ruleOntologyLocation = filepath;
- this.owlfile = new File(ruleOntologyLocation);
- owlfile.setWritable(true);
- OWLOntologyManager owlmanager = OWLManager.createOWLOntologyManager();
- try {
- this.owlmodel = owlmanager.loadOntologyFromOntologyDocument(owlfile);
- } catch (OWLOntologyCreationException oce) {
- log.error(oce.getLocalizedMessage(), oce);
- this.owlmodel = null;
- }
-
- }
- // activate(configuration);
-
+ // This recursive constructor call will also invoke activate()
+ this(null, null, (OWLOntology) null);
}
/**
@@ -191,42 +150,39 @@ public class RuleStoreImpl implements Ru
* {OWLOntology object contains rules and recipe}
*/
public RuleStoreImpl(ONManager onm, Dictionary<String,Object> configuration, OWLOntology owl) {
- /*
- * FIXME : This won't work if the activate() method is called at the end of the constructor, like it
- * is for other components. Constructors should not override activate().
- */
- // This recursive constructor call will also invoke activate()
- this(onm, configuration);
+ this();
+ this.onManager = onm;
try {
this.owlmodel = owl;
} catch (Exception e) {
log.error(e.getLocalizedMessage(), e);
this.owlmodel = null;
}
- // activate(configuration);
- }
- /**
- * Get the owl ontology model.
- *
- * @return {An OWLOntology object where to store the rules and the recipes.}
- */
- @Override
- public OWLOntology getOntology() {
- return this.owlmodel;
+ try {
+ // activator has a branch for existing owlfile
+ activate(configuration);
+ } catch (IOException e) {
+ log.error("Unable to access servlet context.", e);
+ }
+
}
/**
- * To set new OWLOntology with stored rules and recipes.
+ * This construct returns an ontology where to store the rules.
*
- * @param owl
- * {OWLOntology with new changes.}
+ * @param filepath
+ * {Ontology file path previously stored.}
*/
- @Override
- public void setStore(OWLOntology owl) {
-
- this.owlmodel = owl;
+ public RuleStoreImpl(ONManager onm, Dictionary<String,Object> configuration, String filepath) {
+ this();
+ this.onManager = onm;
+ try {
+ activate(_reconfigureLocation(configuration, filepath));
+ } catch (IOException e) {
+ log.error("Unable to access servlet context.", e);
+ }
}
/**
@@ -244,260 +200,104 @@ public class RuleStoreImpl implements Ru
activate((Dictionary<String,Object>) context.getProperties());
}
- protected void activate(Dictionary<String,Object> configuration) {
-
- // TODO This bit is the "good" code, and any non-default constructor should call it.
- ruleOntologyLocation = (String) configuration.get(RuleStore.RULE_ONTOLOGY);
- if (ruleOntologyLocation == null) ruleOntologyLocation = _RULE_ONTOLOGY_DEFAULT;
-
- ruleOntologyNS = (String) configuration.get(RuleStore.RULE_NAMESPACE);
- if (ruleOntologyNS == null) ruleOntologyNS = _RULE_NAMESPACE_DEFAULT;
-
- // this.file = (String) configuration.get(RULE_ONTOLOGY);
- // this.ruleOntologyNS = (String) configuration.get(RULE_NAMESPACE);
-
- // This about KReSConf is no good
- if (ruleOntologyLocation == null || ruleOntologyLocation.equals("")) {
- String sep = System.getProperty("file.separator");
- String filedir = System.getProperty("user.dir") + sep + "KReSConf" + sep + "rmi_config.owl";
-
- if ((new File(filedir)).exists()) {
- this.ruleOntologyLocation = filedir;
- try {
- owlmodel = OWLManager.createOWLOntologyManager().loadOntologyFromOntologyDocument(
- new File(ruleOntologyLocation));
- } catch (OWLOntologyCreationException e) {
- log.error("Cannot create the ontology " + filedir.toString(), e);
- } catch (Exception e) {
- log.error("1 Rule Store: no rule ontology available.");
- }
- } else {
- IRI inputontology = IRI
- .create("http://ontologydesignpatterns.org/ont/iks/kres/rmi_config.owl");
- try {
- owlmodel = OWLManager.createOWLOntologyManager().loadOntologyFromOntologyDocument(
- inputontology);
- } catch (OWLOntologyCreationException e) {
- log.error("Cannot create the ontology " + inputontology.toString(), e);
- } catch (Exception e) {
- log.error("Rule Store: no rule ontology available.");
- }
- }
-
- if (owlmodel != null) {
-
- File dirs = new File("./KReSConf");
- if (!dirs.exists()) dirs.mkdir();
- ruleOntologyLocation = "./KReSConf/rmi_config.owl";
-
- FileOutputStream fos;
- try {
- fos = new FileOutputStream(ruleOntologyLocation);
- OWLManager.createOWLOntologyManager().saveOntology(owlmodel,
- owlmodel.getOWLOntologyManager().getOntologyFormat(owlmodel), fos);
- } catch (FileNotFoundException e) {
- log.error("Cannot save the RMI configuration ontology", e);
- } catch (OWLOntologyStorageException e) {
- log.error("Cannot save the RMI configuration ontology", e);
- }
- }
+ /**
+ * Should be called within both OSGi and non-OSGi environments.
+ *
+ * @param configuration
+ * @throws IOException
+ */
+ protected void activate(Dictionary<String,Object> configuration) throws IOException {
+ if (this.owlmodel != null) {
+ ruleOntologyLocation = owlmodel.getOWLOntologyManager().getOntologyDocumentIRI(owlmodel)
+ .toString();
+ if (!owlmodel.isAnonymous()) ruleNS = owlmodel.getOntologyID().getOntologyIRI() + "#";
} else {
- File pathIri = new File(ruleOntologyLocation);
+ ruleOntologyLocation = (String) configuration.get(RuleStore.RULE_ONTOLOGY);
+ if (ruleOntologyLocation == null) ruleOntologyLocation = _RULE_ONTOLOGY_DEFAULT;
+ ruleNS = (String) configuration.get(RuleStore.RULE_NAMESPACE);
+ if (ruleNS == null) ruleNS = _RULE_NAMESPACE_DEFAULT;
+
+ // Setup local IRI mappings
+ OWLOntologyManager mgr = OWLManager.createOWLOntologyManager();
+ // FIXME : awful, remove as soon as we can
+ URL u = getClass().getResource("/rmi.owl");
+ OWLOntologyIRIMapper mapper1, mapper2;
try {
- owlmodel = OWLManager.createOWLOntologyManager().loadOntologyFromOntologyDocument(pathIri);
- } catch (OWLOntologyCreationException e) {
- log.error("Cannot load the RMI configuration ontology", e);
- } catch (Exception e) {
- log.error("Rule Store: no rule ontology available.");
+ mapper1 = new SimpleIRIMapper(
+ IRI.create("http://ontologydesignpatterns.org/ont/iks/kres/rmi.owl"), IRI.create(u));
+ mapper2 = new SimpleIRIMapper(
+ IRI.create("http://ontologydesignpatterns.org/ont/iks/kres/rmi_config.owl"),
+ IRI.create(getClass().getResource("/META-INF/conf/rmi_config.owl")));
+ // mapper = new AutoIRIMapper(new File(u.toURI()), true);
+ mgr.addIRIMapper(mapper1);
+ mgr.addIRIMapper(mapper2);
+ } catch (URISyntaxException e) {
+ log.error("Failed to add default IRI mapping for resource directory.", e);
}
- }
-
- }
-
- @Deactivate
- protected void deactivate(ComponentContext context) {
- log.info("in " + RuleStoreImpl.class + " deactivate with context " + context);
- }
-
- @Override
- public Set<IRI> listIRIRecipes() {
- Set<IRI> recipeIRIs = null;
- String ruleNS = owlmodel.getOntologyID().toString().replace("<", "").replace(">", "") + "#";
-
- OWLDataFactory factory = onManager.getOwlFactory();
- OWLClass recipeOWLClass = factory.getOWLClass(IRI.create(ruleNS + "Recipe"));
- Set<OWLIndividual> recipeIndividuals = recipeOWLClass.getIndividuals(owlmodel);
-
- if (recipeIndividuals != null && recipeIndividuals.size() > 0) {
- recipeIRIs = new HashSet<IRI>();
- for (OWLIndividual recipeIndividual : recipeIndividuals) {
- if (recipeIndividual instanceof OWLNamedIndividual) {
- recipeIRIs.add(((OWLNamedIndividual) recipeIndividual).getIRI());
- }
- }
- }
-
- return recipeIRIs;
- }
-
- @Override
- public Recipe getRecipe(IRI recipeIRI) throws NoSuchRecipeException {
- log.debug("Called get recipe for id: " + recipeIRI.toString());
- Recipe recipe = null;
-
- if (onManager != null && recipeIRI != null) {
- OWLDataFactory factory = onManager.getOwlFactory();
- OWLIndividual recipeIndividual = factory.getOWLNamedIndividual(recipeIRI);
- if (recipeIndividual != null) {
- // String ruleNS =
- // owlmodel.getOntologyID().toString().replace("<","").replace(">","")+"#";
- // OWLObjectProperty objectProperty =
- // factory.getOWLObjectProperty(IRI.create(ruleNS + "hasRule"));
-
- String ruleNS = "http://kres.iks-project.eu/ontology/meta/rmi.owl#";
-
- /**
- * First get the recipe description in the rule/recipe ontology.
- */
- OWLDataProperty hasDescription = factory.getOWLDataProperty(IRI.create(ruleNS
- + "hasDescription"));
-
- String recipeDescription = null;
-
- Set<OWLLiteral> descriptions = recipeIndividual.getDataPropertyValues(hasDescription,
- owlmodel);
- for (OWLLiteral description : descriptions) {
- recipeDescription = description.getLiteral();
- }
-
- /**
- * Then retrieve the rules associated to the recipe in the rule store.
- */
- OWLObjectProperty objectProperty = factory.getOWLObjectProperty(IRI
- .create(ruleNS + "hasRule"));
- Set<OWLIndividual> rules = recipeIndividual.getObjectPropertyValues(objectProperty, owlmodel);
-
- String kReSRulesInKReSSyntax = "";
-
- log.debug("The recipe " + recipeIRI.toString() + " has " + rules.size() + " rules.");
-
- /**
- * Fetch the rule content expressed as a literal in Rule Syntax.
- */
- boolean firstLoop = true;
- OWLDataProperty hasBodyAndHead = factory.getOWLDataProperty(IRI.create(ruleNS
- + "hasBodyAndHead"));
-
- for (OWLIndividual rule : rules) {
- log.debug("Getting rule : " + rule.toStringID(), this);
-
- Set<OWLLiteral> kReSRuleLiterals = rule.getDataPropertyValues(hasBodyAndHead, owlmodel);
-
- if (!firstLoop) {
- kReSRulesInKReSSyntax += " . ";
- } else {
- firstLoop = false;
- }
+ // Rule ontology location not set
+ if (ruleOntologyLocation == null || ruleOntologyLocation.equals("")) {
+ String sep = System.getProperty("file.separator");
+ String filedir = System.getProperty("user.dir") + sep + "KReSConf" + sep + "rmi_config.owl";
- for (OWLLiteral kReSRuleLiteral : kReSRuleLiterals) {
- String ruleTmp = kReSRuleLiteral.getLiteral().replace("<", "<");
- ruleTmp = ruleTmp.replace(">", ">");
- log.debug("Rule is: " + ruleTmp);
- kReSRulesInKReSSyntax += ruleTmp;
+ // default KReSConf dir exists
+ if ((new File(filedir)).exists()) {
+ this.ruleOntologyLocation = filedir;
+ try {
+ owlmodel = // OWLManager.createOWLOntologyManager()
+ mgr.loadOntologyFromOntologyDocument(new File(ruleOntologyLocation));
+ } catch (OWLOntologyCreationException e) {
+ log.error("Cannot create the ontology " + filedir.toString(), e);
+ } catch (Exception e) {
+ log.error("1 Rule Store: no rule ontology available.");
}
}
-
- /**
- * Create the Recipe object.
- */
- log.debug("Recipe in KReS Syntax : " + kReSRulesInKReSSyntax);
-
- RuleList ruleList = null;
-
- if (!kReSRulesInKReSSyntax.isEmpty()) {
- ruleList = generateKnowledgeBase(kReSRulesInKReSSyntax);
+ // default KReSConf dir does not exist
+ else {
+ IRI inputontology = IRI
+ .create("http://ontologydesignpatterns.org/ont/iks/kres/rmi_config.owl");
+ try {
+ owlmodel = // OWLManager.createOWLOntologyManager()
+ mgr.loadOntology/* FromOntologyDocument */(inputontology);
+ } catch (OWLOntologyCreationException e) {
+ log.error("Cannot create the ontology " + inputontology.toString(), e);
+ } catch (Exception e) {
+ log.error("Rule Store: no rule ontology available.");
+ }
}
- recipe = (Recipe) new RecipeImpl(recipeIRI, recipeDescription, ruleList);
-
- } else {
- throw new NoSuchRecipeException(recipeIRI);
- }
- }
-
- return recipe;
-
- }
-
- @Override
- public RecipeList listRecipes() {
- RecipeList recipies = null;
+ // Can we skip this step of saving the ontology?
+ if (owlmodel != null) {
- Set<IRI> recipeIRIs = listIRIRecipes();
-
- if (recipeIRIs != null && recipeIRIs.size() > 0) {
-
- recipies = new RecipeList();
- for (IRI recipeIRI : recipeIRIs) {
- try {
- recipies.add(getRecipe(recipeIRI));
- } catch (NoSuchRecipeException e) {
- log.error("Recipe missing: " + recipeIRI.toString(), e);
+ File dirs = new File("./KReSConf");
+ if (!dirs.exists()) dirs.mkdir();
+ ruleOntologyLocation = "./KReSConf/rmi_config.owl";
+
+ FileOutputStream fos;
+ try {
+ fos = new FileOutputStream(ruleOntologyLocation);
+ mgr/* OWLManager.createOWLOntologyManager() */.saveOntology(owlmodel, owlmodel
+ .getOWLOntologyManager().getOntologyFormat(owlmodel), fos);
+ } catch (FileNotFoundException e) {
+ log.error("Cannot save the RMI configuration ontology", e);
+ } catch (OWLOntologyStorageException e) {
+ log.error("Cannot save the RMI configuration ontology", e);
+ }
}
}
- }
- return recipies;
- }
-
- /**
- * To get the file path usde to load the ontology.
- *
- * @return {A string contains the complete file path.}
- */
- @Override
- public String getFilePath() {
- return this.ruleOntologyLocation;
- }
-
- /**
- * To save some change to the ontology loaded in the store.
- *
- * FIXME: save using the Clerezza TcManager, or the KReS wrapper for it
- */
- @Override
- public void saveOntology() {
- try {
- FileOutputStream fos;
- if (this.ruleOntologyLocation.isEmpty()) {
- String sep = System.getProperty("file.separator");
- this.ruleOntologyLocation = System.getProperty("user.dir") + sep + "KReSConf" + sep
- + "rmi_config.owl";
+ // Rule ontology location is set
+ else {
+ File pathIri = new File(ruleOntologyLocation);
try {
- fos = new FileOutputStream(ruleOntologyLocation);
- OWLManager.createOWLOntologyManager().saveOntology(owlmodel,
- owlmodel.getOWLOntologyManager().getOntologyFormat(owlmodel), fos);
- } catch (FileNotFoundException e) {
- log.error("Cannot store the ontology ", e);
- } catch (OWLOntologyStorageException e) {
- log.error("Cannot store the ontology ", e);
+ owlmodel = mgr
+ /* OWLManager.createOWLOntologyManager() */.loadOntologyFromOntologyDocument(pathIri);
+ } catch (OWLOntologyCreationException e) {
+ log.error("Cannot load the RMI configuration ontology", e);
+ } catch (Exception e) {
+ log.error("Rule Store: no rule ontology available.");
}
- } else {
- fos = new FileOutputStream(ruleOntologyLocation);
- this.owlmodel.getOWLOntologyManager().saveOntology(owlmodel, fos);
}
-
- } catch (OWLOntologyStorageException ex) {
- log.error("Cannot store the ontology ", ex);
- } catch (FileNotFoundException ex) {
- log.error("Cannot store the ontology ", ex);
}
-
- }
-
- @Override
- public String getRuleStoreNamespace() {
- return owlmodel.getOntologyID().getOntologyIRI() + "#";
}
/*
@@ -539,31 +339,16 @@ public class RuleStoreImpl implements Ru
}
} else {
- log.error("The recipe with name and the set of rules cannot be empity or null.");
- ok = false;
- return (ok);
- }
-
- if (ok) {
- setStore(owlmodel);
- }
-
- return (ok);
- }
-
- /**
- *
- * @param recipeIRI
- * the IRI of the recipe
- * @param kReSRule
- * the rule in Rule syntax
- */
- @Override
- public Recipe addRuleToRecipe(String recipeID, String kReSRuleInKReSSyntax) throws NoSuchRecipeException {
+ log.error("The recipe with name and the set of rules cannot be empity or null.");
+ ok = false;
+ return (ok);
+ }
- Recipe recipe = getRecipe(IRI.create(recipeID));
- return addRuleToRecipe(recipe, kReSRuleInKReSSyntax);
+ if (ok) {
+ setStore(owlmodel);
+ }
+ return (ok);
}
/**
@@ -636,6 +421,21 @@ public class RuleStoreImpl implements Ru
return recipe;
}
+ /**
+ *
+ * @param recipeIRI
+ * the IRI of the recipe
+ * @param kReSRule
+ * the rule in Rule syntax
+ */
+ @Override
+ public Recipe addRuleToRecipe(String recipeID, String kReSRuleInKReSSyntax) throws NoSuchRecipeException {
+
+ Recipe recipe = getRecipe(IRI.create(recipeID));
+ return addRuleToRecipe(recipe, kReSRuleInKReSSyntax);
+
+ }
+
@Override
public void createRecipe(String recipeID, String rulesInKReSSyntax) {
log.debug("Create recipe " + recipeID + " with rules in kres sytnax " + rulesInKReSSyntax, this);
@@ -663,11 +463,184 @@ public class RuleStoreImpl implements Ru
}
+ @Deactivate
+ protected void deactivate(ComponentContext context) {
+ log.info("in " + RuleStoreImpl.class + " deactivate with context " + context);
+ }
+
private RuleList generateKnowledgeBase(String kReSRulesInKReSSyntax) {
KB kb = RuleParserImpl.parse(kReSRulesInKReSSyntax);
return kb.getkReSRuleList();
}
+ /**
+ * To get the file path usde to load the ontology.
+ *
+ * @return {A string contains the complete file path.}
+ */
+ @Override
+ public String getFilePath() {
+ return this.ruleOntologyLocation;
+ }
+
+ /**
+ * Get the owl ontology model.
+ *
+ * @return {An OWLOntology object where to store the rules and the recipes.}
+ */
+ @Override
+ public OWLOntology getOntology() {
+ return this.owlmodel;
+ }
+
+ @Override
+ public Recipe getRecipe(IRI recipeIRI) throws NoSuchRecipeException {
+ log.debug("Called get recipe for id: " + recipeIRI.toString());
+ Recipe recipe = null;
+
+ if (onManager != null && recipeIRI != null) {
+ OWLDataFactory factory = onManager.getOwlFactory();
+ OWLIndividual recipeIndividual = factory.getOWLNamedIndividual(recipeIRI);
+ if (recipeIndividual != null) {
+ // String ruleNS =
+ // owlmodel.getOntologyID().toString().replace("<","").replace(">","")+"#";
+
+ // OWLObjectProperty objectProperty =
+ // factory.getOWLObjectProperty(IRI.create(ruleNS + "hasRule"));
+
+ String ruleNS = "http://kres.iks-project.eu/ontology/meta/rmi.owl#";
+
+ /**
+ * First get the recipe description in the rule/recipe ontology.
+ */
+ OWLDataProperty hasDescription = factory.getOWLDataProperty(IRI.create(ruleNS
+ + "hasDescription"));
+
+ String recipeDescription = null;
+
+ Set<OWLLiteral> descriptions = recipeIndividual.getDataPropertyValues(hasDescription,
+ owlmodel);
+ for (OWLLiteral description : descriptions) {
+ recipeDescription = description.getLiteral();
+ }
+
+ /**
+ * Then retrieve the rules associated to the recipe in the rule store.
+ */
+ OWLObjectProperty objectProperty = factory.getOWLObjectProperty(IRI
+ .create(ruleNS + "hasRule"));
+ Set<OWLIndividual> rules = recipeIndividual.getObjectPropertyValues(objectProperty, owlmodel);
+
+ String kReSRulesInKReSSyntax = "";
+
+ log.debug("The recipe " + recipeIRI.toString() + " has " + rules.size() + " rules.");
+
+ /**
+ * Fetch the rule content expressed as a literal in Rule Syntax.
+ */
+ boolean firstLoop = true;
+ OWLDataProperty hasBodyAndHead = factory.getOWLDataProperty(IRI.create(ruleNS
+ + "hasBodyAndHead"));
+
+ for (OWLIndividual rule : rules) {
+ log.debug("Getting rule : " + rule.toStringID(), this);
+
+ Set<OWLLiteral> kReSRuleLiterals = rule.getDataPropertyValues(hasBodyAndHead, owlmodel);
+
+ if (!firstLoop) {
+ kReSRulesInKReSSyntax += " . ";
+ } else {
+ firstLoop = false;
+ }
+
+ for (OWLLiteral kReSRuleLiteral : kReSRuleLiterals) {
+ String ruleTmp = kReSRuleLiteral.getLiteral().replace("<", "<");
+ ruleTmp = ruleTmp.replace(">", ">");
+ log.debug("Rule is: " + ruleTmp);
+ kReSRulesInKReSSyntax += ruleTmp;
+ }
+ }
+
+ /**
+ * Create the Recipe object.
+ */
+ log.debug("Recipe in KReS Syntax : " + kReSRulesInKReSSyntax);
+
+ RuleList ruleList = null;
+
+ if (!kReSRulesInKReSSyntax.isEmpty()) {
+ ruleList = generateKnowledgeBase(kReSRulesInKReSSyntax);
+ }
+
+ recipe = (Recipe) new RecipeImpl(recipeIRI, recipeDescription, ruleList);
+
+ } else {
+ throw new NoSuchRecipeException(recipeIRI);
+ }
+ }
+
+ return recipe;
+
+ }
+
+ @Override
+ public String getRuleStoreNamespace() {
+ return owlmodel.getOntologyID().getOntologyIRI() + "#";
+ }
+
+ @Override
+ public Set<IRI> listIRIRecipes() {
+ Set<IRI> recipeIRIs = null;
+ String ruleNS = owlmodel.getOntologyID().toString().replace("<", "").replace(">", "") + "#";
+
+ OWLDataFactory factory = onManager.getOwlFactory();
+ OWLClass recipeOWLClass = factory.getOWLClass(IRI.create(ruleNS + "Recipe"));
+ Set<OWLIndividual> recipeIndividuals = recipeOWLClass.getIndividuals(owlmodel);
+
+ if (recipeIndividuals != null && recipeIndividuals.size() > 0) {
+ recipeIRIs = new HashSet<IRI>();
+ for (OWLIndividual recipeIndividual : recipeIndividuals) {
+ if (recipeIndividual instanceof OWLNamedIndividual) {
+ recipeIRIs.add(((OWLNamedIndividual) recipeIndividual).getIRI());
+ }
+ }
+ }
+
+ return recipeIRIs;
+ }
+
+ @Override
+ public RecipeList listRecipes() {
+ RecipeList recipies = null;
+
+ Set<IRI> recipeIRIs = listIRIRecipes();
+
+ if (recipeIRIs != null && recipeIRIs.size() > 0) {
+
+ recipies = new RecipeList();
+ for (IRI recipeIRI : recipeIRIs) {
+ try {
+ recipies.add(getRecipe(recipeIRI));
+ } catch (NoSuchRecipeException e) {
+ log.error("Recipe missing: " + recipeIRI.toString(), e);
+ }
+ }
+ }
+ return recipies;
+ }
+
+ @Override
+ public boolean removeRecipe(IRI recipeIRI) {
+ Recipe recipe;
+ try {
+ recipe = getRecipe(recipeIRI);
+ return removeRecipe(recipe);
+ } catch (NoSuchRecipeException ex) {
+ log.error("Exception cougth: ", ex);
+ return false;
+ }
+ }
+
@Override
public boolean removeRecipe(Recipe recipe) {
@@ -692,18 +665,6 @@ public class RuleStoreImpl implements Ru
}
@Override
- public boolean removeRecipe(IRI recipeIRI) {
- Recipe recipe;
- try {
- recipe = getRecipe(recipeIRI);
- return removeRecipe(recipe);
- } catch (NoSuchRecipeException ex) {
- log.error("Exception cougth: ", ex);
- return false;
- }
- }
-
- @Override
public boolean removeRule(Rule rule) {
OWLOntologyManager mng = owlmodel.getOWLOntologyManager();
@@ -727,4 +688,50 @@ public class RuleStoreImpl implements Ru
}
+ /**
+ * To save some change to the ontology loaded in the store.
+ *
+ * FIXME: save using the Clerezza TcManager, or the KReS wrapper for it
+ */
+ @Override
+ public void saveOntology() {
+ try {
+ FileOutputStream fos;
+ if (this.ruleOntologyLocation.isEmpty()) {
+ String sep = System.getProperty("file.separator");
+ this.ruleOntologyLocation = System.getProperty("user.dir") + sep + "KReSConf" + sep
+ + "rmi_config.owl";
+ try {
+ fos = new FileOutputStream(ruleOntologyLocation);
+ OWLManager.createOWLOntologyManager().saveOntology(owlmodel,
+ owlmodel.getOWLOntologyManager().getOntologyFormat(owlmodel), fos);
+ } catch (FileNotFoundException e) {
+ log.error("Cannot store the ontology ", e);
+ } catch (OWLOntologyStorageException e) {
+ log.error("Cannot store the ontology ", e);
+ }
+ } else {
+ fos = new FileOutputStream(ruleOntologyLocation);
+ this.owlmodel.getOWLOntologyManager().saveOntology(owlmodel, fos);
+ }
+
+ } catch (OWLOntologyStorageException ex) {
+ log.error("Cannot store the ontology ", ex);
+ } catch (FileNotFoundException ex) {
+ log.error("Cannot store the ontology ", ex);
+ }
+
+ }
+
+ /**
+ * To set new OWLOntology with stored rules and recipes.
+ *
+ * @param owl
+ * {OWLOntology with new changes.}
+ */
+ @Override
+ public void setStore(OWLOntology owl) {
+ this.owlmodel = owl;
+ }
+
}