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/03/16 16:31:11 UTC
svn commit: r1082166 [3/7] - in /incubator/stanbol/trunk/kres/rules/manager:
./ src/ src/main/ src/main/java/ src/main/java/org/
src/main/java/org/apache/ src/main/java/org/apache/stanbol/
src/main/java/org/apache/stanbol/rules/ src/main/java/org/apach...
Added: incubator/stanbol/trunk/kres/rules/manager/src/main/java/org/apache/stanbol/rules/manager/changes/KReSAddRecipe.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/kres/rules/manager/src/main/java/org/apache/stanbol/rules/manager/changes/KReSAddRecipe.java?rev=1082166&view=auto
==============================================================================
--- incubator/stanbol/trunk/kres/rules/manager/src/main/java/org/apache/stanbol/rules/manager/changes/KReSAddRecipe.java (added)
+++ incubator/stanbol/trunk/kres/rules/manager/src/main/java/org/apache/stanbol/rules/manager/changes/KReSAddRecipe.java Wed Mar 16 15:31:08 2011
@@ -0,0 +1,769 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package org.apache.stanbol.rules.manager.changes;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Vector;
+
+import org.apache.stanbol.rules.base.api.RuleStore;
+import org.semanticweb.owlapi.apibinding.OWLManager;
+import org.semanticweb.owlapi.model.AddImport;
+import org.semanticweb.owlapi.model.IRI;
+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.OWLDataPropertyAssertionAxiom;
+import org.semanticweb.owlapi.model.OWLImportsDeclaration;
+import org.semanticweb.owlapi.model.OWLNamedIndividual;
+import org.semanticweb.owlapi.model.OWLObjectProperty;
+import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom;
+import org.semanticweb.owlapi.model.OWLOntology;
+import org.semanticweb.owlapi.model.OWLOntologyChange;
+import org.semanticweb.owlapi.model.OWLOntologyCreationException;
+import org.semanticweb.owlapi.model.OWLOntologyManager;
+
+/**
+ * This class will add new recipe to the KReSRuleStore used as input.<br/>
+ * The KReSRuleStore object used as input is not changed and to get the new modified KReSRuleStore there is the method getStore().<br/>
+ * If a recipe with a same name or IRI is already inside the KReSRuleStore an error is lunched and the process stopped.<br/>
+ *
+ */
+public class KReSAddRecipe {
+
+ private OWLOntology owlmodel;
+ private OWLOntologyManager owlmanager;
+ private OWLDataFactory factory;
+ private String owlIDrmi;
+ private String owlID;
+ private RuleStore storeaux;
+
+ /**
+ * To create a list of imported ontlogy to be added as import declarations
+ *
+ * @param inowl {Input ontology where to get the import declarations}
+ * @return {A list of declarations}
+ */
+ private List<OWLOntologyChange> createImportList(OWLOntology inowl,OWLOntology toadd){
+
+ Iterator<OWLOntology> importedonto = inowl.getDirectImports().iterator();
+ List<OWLOntologyChange> additions = new LinkedList<OWLOntologyChange>();
+ OWLDataFactory auxfactory = inowl.getOWLOntologyManager().getOWLDataFactory();
+
+ while(importedonto.hasNext()){
+ OWLOntology auxonto = importedonto.next();
+ additions.add(new AddImport(toadd,auxfactory.getOWLImportsDeclaration(auxonto.getOWLOntologyManager().getOntologyDocumentIRI(auxonto))));
+ }
+
+ if(additions.size()==0){
+ Iterator<OWLImportsDeclaration> importedontob = inowl.getImportsDeclarations().iterator();
+ additions = new LinkedList<OWLOntologyChange>();
+ auxfactory = inowl.getOWLOntologyManager().getOWLDataFactory();
+
+ while(importedontob.hasNext()){
+ OWLImportsDeclaration auxontob = importedontob.next();
+ additions.add(new AddImport(toadd,auxontob));
+ }
+ }
+
+ return additions;
+ }
+
+ /**
+ * To clone ontology with all its axioms and imports declaration
+ *
+ * @param inowl {The onotlogy to be cloned}
+ * @return {An ontology with the same characteristics}
+ */
+ private void cloneOntology(OWLOntology inowl){
+
+ //Clone the targetontology
+ try {
+ this.owlmodel = OWLManager.createOWLOntologyManager().createOntology(inowl.getOntologyID().getOntologyIRI());
+ this.owlmanager = owlmodel.getOWLOntologyManager();
+ //Add axioms
+ owlmanager.addAxioms(owlmodel,inowl.getAxioms());
+ //Add import declaration
+ List<OWLOntologyChange> additions = createImportList(inowl,owlmodel);
+ if(additions.size()>0)
+ owlmanager.applyChanges(additions);
+
+ } catch (OWLOntologyCreationException ex) {
+ ex.printStackTrace();
+ }
+
+ }
+
+ /**
+ * Constructor, the input is a KReSRuleStore object.<br/>
+ * N.B. To get the new KReSRuleStore object there is the method getStore();<br/>
+ * @param store {The KReSRuleStore where to add the recipe.}
+ */
+ public KReSAddRecipe(RuleStore store){
+ this.storeaux = store;
+ //cloneOntology(storeaux.getOntology());
+ this.owlmanager = OWLManager.createOWLOntologyManager();
+ this.owlmodel = storeaux.getOntology();
+ this.factory = owlmanager.getOWLDataFactory();
+ this.owlIDrmi="http://kres.iks-project.eu/ontology/meta/rmi.owl#";
+ this.owlID = owlmodel.getOntologyID().getOntologyIRI().toString()+"#";
+ }
+
+ /**
+ * Constructor, the input is a KReSRuleStore object and a string contains the base iri of the resource.<br/>
+ * N.B. To get the new KReSRuleStore object there is the method getStore();<br/>
+ * @param store {The KReSRuleStore where to add the recipe.}
+ * @param owlid {The base iri of resource}
+ */
+ public KReSAddRecipe(RuleStore store, String owlid){
+ this.storeaux = store;
+ cloneOntology(storeaux.getOntology());
+ this.factory = owlmanager.getOWLDataFactory();
+ this.owlIDrmi="http://kres.iks-project.eu/ontology/meta/rmi.owl#";
+ this.owlID = owlid;
+ }
+
+ /**
+ * Method to add a Recipe. The inputs are: a recipe name string that doesn't exist in the ontology, a string vector with the IRI of each rule and eventualy a description of the recipe.
+ *
+ * @param recipeName {A string variable contains a name}
+ * @param rules {A string vector variable contains the IRI of each rule}
+ * @param recipeDescription {A briefly description of the rule}
+ * @return {A boolean that is true if the operation is ok}
+ */
+ public boolean addRecipe(String recipeName, Vector<IRI> rules, String recipeDescription){
+ boolean ok = false;
+
+ OWLClass ontocls = factory.getOWLClass(IRI.create(owlIDrmi+"Recipe"));
+ OWLClass kresrule = factory.getOWLClass(IRI.create(owlIDrmi+"KReSRule"));
+ OWLNamedIndividual ontoind = factory.getOWLNamedIndividual(IRI.create(owlID+recipeName));
+ OWLDataProperty description = factory.getOWLDataProperty(IRI.create(owlIDrmi+"hasDescription"));
+ OWLDataProperty sequence = factory.getOWLDataProperty(IRI.create(owlIDrmi+"hasSequence"));
+ OWLObjectProperty hasrule = factory.getOWLObjectProperty(IRI.create(owlIDrmi+"hasRule"));
+ OWLObjectProperty start = factory.getOWLObjectProperty(IRI.create(owlIDrmi+"startWith"));
+ OWLObjectProperty end = factory.getOWLObjectProperty(IRI.create(owlIDrmi+"endWith"));
+ OWLObjectProperty precedes = factory.getOWLObjectProperty(IRI.create("http://www.ontologydesignpatterns.org/cp/owl/sequence.owl#directlyPrecedes"));
+ OWLObjectPropertyAssertionAxiom objectPropAssertion;
+
+ if(((recipeName!=null)||!recipeName.toString().isEmpty())&&((rules!=null)||!rules.isEmpty())){
+ if(!owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(ontocls, ontoind))){
+
+ //Add the rule istance
+ OWLClassAssertionAxiom classAssertion = factory.getOWLClassAssertionAxiom(ontocls,ontoind);
+ owlmanager.addAxiom(owlmodel, classAssertion);
+
+ //start and end
+ OWLNamedIndividual ind = factory.getOWLNamedIndividual(rules.firstElement());
+ if(owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(kresrule, ind))){
+ objectPropAssertion = factory.getOWLObjectPropertyAssertionAxiom(start, ontoind,ind);
+ owlmanager.addAxiom(owlmodel, objectPropAssertion);
+ ok =true;
+ }else{
+ System.err.println("The rule with IRI "+ind.getIRI()+" is not inside the ontology. Pleas check its IRI.");
+ ok = false;
+ return(ok);
+ }
+
+ ind = factory.getOWLNamedIndividual(rules.lastElement());
+ if(owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(kresrule,ind))){
+ objectPropAssertion = factory.getOWLObjectPropertyAssertionAxiom(end, ontoind,ind);
+ owlmanager.addAxiom(owlmodel, objectPropAssertion);
+ ok = true;
+ }else{
+ System.err.println("The rule with IRI "+ind.getIRI()+" is not inside the ontology. Pleas check its IRI.");
+ ok = false;
+ return(ok);
+
+ }
+
+ //Add the sequence string
+ OWLDataPropertyAssertionAxiom dataPropAssertion = factory.getOWLDataPropertyAssertionAxiom(sequence, ontoind,rules.toString().replace("[","").replace("]",""));
+ owlmanager.addAxiom(owlmodel, dataPropAssertion);
+
+ //Add description
+ if((recipeDescription!=null)||!recipeDescription.isEmpty()){
+ //Add the rule description
+ dataPropAssertion = factory.getOWLDataPropertyAssertionAxiom(description, ontoind, recipeDescription);
+ owlmanager.addAxiom(owlmodel, dataPropAssertion);
+ ok=true;
+ }
+
+ //Add single rule
+ for(int r = 0; r<rules.size()-1; r++){
+ ind = factory.getOWLNamedIndividual(rules.get(r));
+ if(owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(kresrule, ind))){
+ //Add the rule to the recipes
+ objectPropAssertion = factory.getOWLObjectPropertyAssertionAxiom(hasrule, ontoind,ind);
+ owlmanager.addAxiom(owlmodel, objectPropAssertion);
+ ok = true;
+ //Add precedes
+ OWLNamedIndividual indf = factory.getOWLNamedIndividual(rules.get(r+1));
+ if(owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(kresrule, indf))){
+ objectPropAssertion = factory.getOWLObjectPropertyAssertionAxiom(precedes,ind,indf);
+ owlmanager.addAxiom(owlmodel, objectPropAssertion);
+ ok = true;
+ }else{
+ System.err.println("The rule with IRI "+indf.getIRI()+" is not inside the ontology. Pleas check its IRI.");
+ ok = false;
+ return(ok);
+ }
+ }else{
+ System.err.println("The rule with IRI "+ind.getIRI()+" is not inside the ontology. Pleas check its IRI.");
+ ok = false;
+ return(ok);
+ }
+
+ }
+ //Add last element
+ ind = factory.getOWLNamedIndividual(rules.lastElement());
+ if(owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(kresrule, ind))){
+ //Add the rule to the recipes
+ objectPropAssertion = factory.getOWLObjectPropertyAssertionAxiom(hasrule, ontoind,ind);
+ owlmanager.addAxiom(owlmodel, objectPropAssertion);
+ ok=true;
+ }else{
+ System.err.println("The rule with IRI "+ind.getIRI()+" is not inside the ontology. Pleas check its IRI.");
+ ok = false;
+ return(ok);
+ }
+
+ }else{
+ System.err.println("The recipe with name "+recipeName+" already exists. Pleas check the name.");
+ ok = false;
+ return(ok);
+ }
+
+ }else{
+ System.err.println("The recipe with name and the set of rules cannot be empity or null.");
+ ok=false;
+ return(ok);
+ }
+
+ if(ok)
+ storeaux.setStore(owlmodel);
+
+ return(ok);
+ }
+
+
+ /**
+ * Method to add a simple Recipe without rules. The inputs are: a recipe name string that doesn't exist in the ontology and eventualy a description of the recipe.
+ *
+ * @param recipeName {A string variable contains a name}
+ * @param recipeDescription {A briefly description of the rule}
+ * @return {A boolean that is true if the operation is ok}
+ */
+ public boolean addSimpleRecipe(String recipeName, String recipeDescription){
+ boolean ok = false;
+
+ OWLClass ontocls = factory.getOWLClass(IRI.create(owlIDrmi+"Recipe"));
+ OWLNamedIndividual ontoind = factory.getOWLNamedIndividual(IRI.create(owlID+recipeName));
+ OWLDataProperty description = factory.getOWLDataProperty(IRI.create(owlIDrmi+"hasDescription"));
+ OWLDataPropertyAssertionAxiom dataPropAssertion;
+
+
+ if((recipeName!=null||!recipeName.isEmpty())){
+ if(!owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(ontocls, ontoind))){
+
+ //Add the rule istance
+ OWLClassAssertionAxiom classAssertion = factory.getOWLClassAssertionAxiom(ontocls,ontoind);
+ owlmanager.addAxiom(owlmodel, classAssertion);
+
+ //Add description
+ if((recipeDescription!=null)||!recipeDescription.isEmpty()){
+ //Add the rule description
+ dataPropAssertion = factory.getOWLDataPropertyAssertionAxiom(description, ontoind, recipeDescription);
+ owlmanager.addAxiom(owlmodel, dataPropAssertion);
+ }
+
+ if(owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(ontocls, ontoind)))
+ ok = true;
+
+ }else{
+ System.err.println("The recipe with name "+recipeName+" already exists. Pleas check the name.");
+ ok = false;
+ return(ok);
+ }
+
+ }else{
+ System.err.println("The recipe with name and the set of rules cannot be empity or null.");
+ ok=false;
+ return(ok);
+ }
+
+ if(ok){
+ storeaux.setStore(owlmodel);
+ }
+
+
+ return(ok);
+ }
+
+ /**
+ * Method to add a simple Recipe without rules. The inputs are: a recipe name string that doesn't exist in the ontology and eventualy a description of the recipe.
+ *
+ * @param recipeIRI {An IRI contains the full recipe name}
+ * @param recipeDescription {A briefly description of the rule}
+ * @return {A boolean that is true if the operation is ok}
+ */
+ public boolean addSimpleRecipe(IRI recipeIRI, String recipeDescription){
+ boolean ok = false;
+
+ OWLClass ontocls = factory.getOWLClass(IRI.create(owlIDrmi+"Recipe"));
+ OWLNamedIndividual ontoind = factory.getOWLNamedIndividual(recipeIRI);
+ OWLDataProperty description = factory.getOWLDataProperty(IRI.create(owlIDrmi+"hasDescription"));
+ OWLDataPropertyAssertionAxiom dataPropAssertion;
+
+ if((recipeIRI!=null)){
+ if(!owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(ontocls, ontoind))){
+
+ //Add the rule istance
+ OWLClassAssertionAxiom classAssertion = factory.getOWLClassAssertionAxiom(ontocls,ontoind);
+ owlmanager.addAxiom(owlmodel, classAssertion);
+
+ //Add description
+ if((recipeDescription!=null)||!recipeDescription.isEmpty()){
+ //Add the rule description
+ dataPropAssertion = factory.getOWLDataPropertyAssertionAxiom(description, ontoind, recipeDescription);
+ owlmanager.addAxiom(owlmodel, dataPropAssertion);
+ ok=true;
+ }
+
+ }else{
+ System.err.println("The recipe with name "+recipeIRI+" already exists. Pleas check the name.");
+ ok = false;
+ return(ok);
+ }
+
+ }else{
+ System.err.println("The recipe with name and the set of rules cannot be empity or null.");
+ ok=false;
+ return(ok);
+ }
+
+ if(ok)
+ storeaux.setStore(owlmodel);
+
+ return(ok);
+ }
+
+ /**
+ * Method to add a Recipe. The inputs are: a recipe name string that doesn't exist in the ontology, a string vector with the IRI of each rule and eventualy a description of the recipe.
+ *
+ * @param recipeName {An IRI variable contains the complete recipe name}
+ * @param rules {A string vector variable contains the IRI of each rule}
+ * @param recipeDescription {A briefly description of the rule}
+ * @return {A boolean that is true if the operation is ok}
+ */
+ public boolean addRecipe(IRI recipeName, Vector<IRI> rules, String recipeDescription){
+ boolean ok = false;
+
+ OWLClass ontocls = factory.getOWLClass(IRI.create(owlIDrmi+"Recipe"));
+ OWLClass kresrule = factory.getOWLClass(IRI.create(owlIDrmi+"KReSRule"));
+ OWLNamedIndividual ontoind = factory.getOWLNamedIndividual(recipeName);
+ OWLDataProperty description = factory.getOWLDataProperty(IRI.create(owlIDrmi+"hasDescription"));
+ OWLDataProperty sequence = factory.getOWLDataProperty(IRI.create(owlIDrmi+"hasSequence"));
+ OWLObjectProperty hasrule = factory.getOWLObjectProperty(IRI.create(owlIDrmi+"hasRule"));
+ OWLObjectProperty start = factory.getOWLObjectProperty(IRI.create(owlIDrmi+"startWith"));
+ OWLObjectProperty end = factory.getOWLObjectProperty(IRI.create(owlIDrmi+"endWith"));
+ OWLObjectProperty precedes = factory.getOWLObjectProperty(IRI.create("http://www.ontologydesignpatterns.org/cp/owl/sequence.owl#directlyPrecedes"));
+ OWLObjectPropertyAssertionAxiom objectPropAssertion;
+
+ if(((recipeName!=null)||!recipeName.toString().isEmpty())&&((rules!=null)||!rules.isEmpty())){
+ if(!owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(ontocls, ontoind))){
+
+ //Add the rule istance
+ OWLClassAssertionAxiom classAssertion = factory.getOWLClassAssertionAxiom(ontocls,ontoind);
+ owlmanager.addAxiom(owlmodel, classAssertion);
+
+ //start and end
+ OWLNamedIndividual ind = factory.getOWLNamedIndividual(rules.firstElement());
+ if(owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(kresrule, ind))){
+ objectPropAssertion = factory.getOWLObjectPropertyAssertionAxiom(start, ontoind,ind);
+ owlmanager.addAxiom(owlmodel, objectPropAssertion);
+ ok = true;
+ }else{
+ System.err.println("The rule with IRI "+ind.getIRI()+" is not inside the ontology. Pleas check its IRI.");
+ ok = false;
+ return(ok);
+ }
+
+ ind = factory.getOWLNamedIndividual(rules.lastElement());
+ if(owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(kresrule, ind))){
+ objectPropAssertion = factory.getOWLObjectPropertyAssertionAxiom(end, ontoind,ind);
+ owlmanager.addAxiom(owlmodel, objectPropAssertion);
+ ok = true;
+ }else{
+ System.err.println("The rule with IRI "+ind.getIRI()+" is not inside the ontology. Pleas check its IRI.");
+ ok = false;
+ return(ok);
+ }
+
+ //Add the sequence string
+ OWLDataPropertyAssertionAxiom dataPropAssertion = factory.getOWLDataPropertyAssertionAxiom(sequence, ontoind,rules.toString().replace("[","").replace("]",""));
+ owlmanager.addAxiom(owlmodel, dataPropAssertion);
+
+ //Add description
+ if((recipeDescription!=null)||!recipeDescription.isEmpty()){
+ //Add the rule description
+ dataPropAssertion = factory.getOWLDataPropertyAssertionAxiom(description, ontoind, recipeDescription);
+ owlmanager.addAxiom(owlmodel, dataPropAssertion);
+ ok = true;
+ }
+
+ //Add single rule
+
+ /*
+ * BUGFIX - previously the check was done on rules.size()-1.
+ * The right code is rules.size(). Moreover is need also a control "if(r+1>(rules.size()-1)) break;" because the last rule has not successive rules.
+ *
+ */
+ for(int r=0; r<rules.size(); r++){
+ ind = factory.getOWLNamedIndividual(rules.get(r));
+ if(owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(kresrule, ind))){
+ //Add the rule to the recipes
+ objectPropAssertion = factory.getOWLObjectPropertyAssertionAxiom(hasrule, ontoind,ind);
+ owlmanager.addAxiom(owlmodel, objectPropAssertion);
+ ok = true;
+ //Add precedes
+ if(r+1>(rules.size()-1)) break;
+ OWLNamedIndividual indf = factory.getOWLNamedIndividual(rules.get(r+1));
+ if(owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(kresrule, indf))){
+ objectPropAssertion = factory.getOWLObjectPropertyAssertionAxiom(precedes,ind,indf);
+ owlmanager.addAxiom(owlmodel, objectPropAssertion);
+ ok = true;
+ }else{
+ System.err.println("The rule with IRI "+indf.getIRI()+" is not inside the ontology. Pleas check its IRI.");
+ ok = false;
+ return(ok);
+ }
+ }else{
+ System.err.println("The rule with IRI "+ind.getIRI()+" is not inside the ontology. Pleas check its IRI.");
+ ok = false;
+ return(ok);
+ }
+
+ }
+ //Add last element
+ ind = factory.getOWLNamedIndividual(rules.lastElement());
+ if(owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(kresrule, ind))){
+ //Add the rule to the recipes
+ objectPropAssertion = factory.getOWLObjectPropertyAssertionAxiom(hasrule, ontoind,ind);
+ owlmanager.addAxiom(owlmodel, objectPropAssertion);
+ ok=true;
+ }else{
+ System.err.println("The rule with IRI "+ind.getIRI()+" is not inside the ontology. Pleas check its IRI.");
+ ok = false;
+ return(ok);
+ }
+
+ }else{
+ System.err.println("The recipe with name "+recipeName+" already exists. Pleas check the name.");
+ ok = false;
+ return(ok);
+ }
+
+ }else{
+ System.err.println("The recipe with name and the set of rules cannot be empity or null.");
+ ok=false;
+ return(ok);
+ }
+
+ if(ok)
+ this.storeaux.setStore(owlmodel);
+
+ return(ok);
+ }
+
+ /**
+ * Method to add a Recipe. The inputs are two HashMap with the key the recipe name and the value is a vector of IRI contains the rule's sequence; the second map contains the description.
+ *
+ * @param recipeMap {An HashMap variable contains string recipe name as key and an IRI vector contains the rules of the sequence as value}
+ * @param recipeDescriptionMap {An HashMap variable contains string recipe name as key and the recipe's description as value}
+ * @return {A boolean that is true if the operation is ok}
+ */
+ public boolean addRecipeMap(HashMap<String,Vector<IRI>> recipeMap, HashMap<String,String> recipeDescriptionMap){
+ boolean ok = false;
+
+ OWLClass ontocls = factory.getOWLClass(IRI.create(owlIDrmi+"Recipe"));
+ OWLClass kresrule = factory.getOWLClass(IRI.create(owlIDrmi+"KReSRule"));
+ OWLDataProperty description = factory.getOWLDataProperty(IRI.create(owlIDrmi+"hasDescription"));
+ OWLDataProperty sequence = factory.getOWLDataProperty(IRI.create(owlIDrmi+"hasSequence"));
+ OWLObjectProperty hasrule = factory.getOWLObjectProperty(IRI.create(owlIDrmi+"hasRule"));
+ OWLObjectProperty start = factory.getOWLObjectProperty(IRI.create(owlIDrmi+"startWith"));
+ OWLObjectProperty end = factory.getOWLObjectProperty(IRI.create(owlIDrmi+"endWith"));
+ OWLObjectProperty precedes = factory.getOWLObjectProperty(IRI.create("http://www.ontologydesignpatterns.org/cp/owl/sequence.owl#directlyPrecedes"));
+
+ Object[] keys = recipeMap.keySet().toArray();
+
+ String recipeDescription = "";
+ OWLObjectPropertyAssertionAxiom objectPropAssertion;
+
+ for(int k = 0; k<keys.length;k++){
+ String recipeName = (String) keys[k];
+
+ OWLNamedIndividual ontoind = factory.getOWLNamedIndividual(IRI.create(owlID+recipeName));
+ Vector<IRI> rules = recipeMap.get(recipeName);
+
+ if((recipeDescriptionMap!=null))
+ if(!recipeDescriptionMap.isEmpty()){
+ recipeDescription = recipeDescriptionMap.get(recipeName);
+ }else{
+ recipeDescription ="";
+ }
+
+ if(((recipeName!=null)||!recipeName.toString().isEmpty())&&((rules!=null)||!rules.isEmpty())){
+ if(!owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(ontocls, ontoind))){
+
+ //Add the rule istance
+ OWLClassAssertionAxiom classAssertion = factory.getOWLClassAssertionAxiom(ontocls,ontoind);
+ owlmanager.addAxiom(owlmodel, classAssertion);
+
+ //start and end
+ OWLNamedIndividual ind = factory.getOWLNamedIndividual(rules.firstElement());
+ if(owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(kresrule, ind))){
+ objectPropAssertion = factory.getOWLObjectPropertyAssertionAxiom(start, ontoind,ind);
+ owlmanager.addAxiom(owlmodel, objectPropAssertion);
+ ok = true;
+ }else{
+ System.err.println("The rule with IRI "+ind.getIRI()+" is not inside the ontology. Pleas check its IRI.");
+ ok = false;
+ return(ok);
+ }
+
+ ind = factory.getOWLNamedIndividual(rules.lastElement());
+ if(owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(kresrule, ind))){
+ objectPropAssertion = factory.getOWLObjectPropertyAssertionAxiom(end, ontoind,ind);
+ owlmanager.addAxiom(owlmodel, objectPropAssertion);
+ ok = true;
+ }else{
+ System.err.println("The rule with IRI "+ind.getIRI()+" is not inside the ontology. Pleas check its IRI.");
+ ok = false;
+ return(ok);
+ }
+
+ //Add the sequence string
+ OWLDataPropertyAssertionAxiom dataPropAssertion = factory.getOWLDataPropertyAssertionAxiom(sequence, ontoind,rules.toString().replace("[","").replace("]",""));
+ owlmanager.addAxiom(owlmodel, dataPropAssertion);
+
+ //Add description
+ if((recipeDescription!=null))
+ if(!recipeDescription.isEmpty()){
+ //Add the rule description
+ dataPropAssertion = factory.getOWLDataPropertyAssertionAxiom(description, ontoind, recipeDescription);
+ owlmanager.addAxiom(owlmodel, dataPropAssertion);
+ ok = true;
+ }
+
+ //Add single rule
+ for(int r = 0; r<rules.size()-1; r++){
+ ind = factory.getOWLNamedIndividual(rules.get(r));
+ if(owlmodel.containsIndividualInSignature(ind.getIRI())){
+ //Add the rule to the recipes
+ objectPropAssertion = factory.getOWLObjectPropertyAssertionAxiom(hasrule, ontoind,ind);
+ owlmanager.addAxiom(owlmodel, objectPropAssertion);
+ ok = true;
+ //Add precedes
+ OWLNamedIndividual indf = factory.getOWLNamedIndividual(rules.get(r+1));
+ if(owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(kresrule, indf))){
+ objectPropAssertion = factory.getOWLObjectPropertyAssertionAxiom(precedes,ind,indf);
+ owlmanager.addAxiom(owlmodel, objectPropAssertion);
+ ok = true;
+ }else{
+ System.err.println("The rule with IRI "+indf.getIRI()+" is not inside the ontology. Pleas check its IRI.");
+ ok = false;
+ return(ok);
+ }
+ }else{
+ System.err.println("The rule with IRI "+ind.getIRI()+" is not inside the ontology. Pleas check its IRI.");
+ ok = false;
+ return(ok);
+ }
+
+ }
+
+ //Add last element
+ ind = factory.getOWLNamedIndividual(rules.lastElement());
+ if(owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(kresrule, ind))){
+ //Add the rule to the recipes
+ objectPropAssertion = factory.getOWLObjectPropertyAssertionAxiom(hasrule, ontoind,ind);
+ owlmanager.addAxiom(owlmodel, objectPropAssertion);
+ ok=true;
+ }else{
+ System.err.println("The rule with IRI "+ind.getIRI()+" is not inside the ontology. Pleas check its IRI.");
+ ok = false;
+ return(ok);
+ }
+
+ }else{
+ System.err.println("The recipe with name "+recipeName+" already exists. Pleas check the name.");
+ ok = false;
+ return(ok);
+ }
+
+ }else{
+ System.err.println("The recipe with name and the set of rules cannot be empity or null.");
+ ok=false;
+ return(ok);
+ }
+ }
+
+ if(ok)
+ this.storeaux.setStore(owlmodel);
+
+ return(ok);
+ }
+
+ /**
+ * Method to add a Recipe. The inputs are two HashMap with the key the recipe IRI name and the value is a vector IRI contains the rule's sequence; the second map contains the description.
+ *
+ * @param recipeMap {An HashMap variable contains the recipe IRI name as key and an IRI vector contains the rules of the sequence as value}
+ * @param recipeDescriptionMap {An HashMap variable contains the recipe IRI name as key and the recipe's description as value}
+ * @return {A boolean that is true if the operation is ok}
+ */
+ public boolean addRecipeMapIRI(HashMap<IRI,Vector<IRI>> recipeMap, HashMap<IRI,String> recipeDescriptionMap){
+ boolean ok = false;
+
+ OWLClass ontocls = factory.getOWLClass(IRI.create(owlIDrmi+"Recipe"));
+ OWLClass kresrule = factory.getOWLClass(IRI.create(owlIDrmi+"KReSRule"));
+ OWLDataProperty description = factory.getOWLDataProperty(IRI.create(owlIDrmi+"hasDescription"));
+ OWLDataProperty sequence = factory.getOWLDataProperty(IRI.create(owlIDrmi+"hasSequence"));
+ OWLObjectProperty hasrule = factory.getOWLObjectProperty(IRI.create(owlIDrmi+"hasRule"));
+ OWLObjectProperty start = factory.getOWLObjectProperty(IRI.create(owlIDrmi+"startWith"));
+ OWLObjectProperty end = factory.getOWLObjectProperty(IRI.create(owlIDrmi+"endWith"));
+ OWLObjectProperty precedes = factory.getOWLObjectProperty(IRI.create("http://www.ontologydesignpatterns.org/cp/owl/sequence.owl#directlyPrecedes"));
+
+ Object[] keys = recipeMap.keySet().toArray();
+
+ String recipeDescription = "";
+ OWLObjectPropertyAssertionAxiom objectPropAssertion;
+
+
+ for(int k = 0; k<keys.length;k++){
+ IRI recipeName = (IRI) keys[k];
+
+ OWLNamedIndividual ontoind = factory.getOWLNamedIndividual(recipeName);
+ Vector<IRI> rules = recipeMap.get(recipeName);
+
+ if((recipeDescriptionMap!=null))
+ if(!recipeDescriptionMap.isEmpty()){
+ recipeDescription = recipeDescriptionMap.get(recipeName);
+ } else {
+ recipeDescription ="";
+ }
+
+ if(((recipeName!=null)||!recipeName.toString().isEmpty())&&((rules!=null)||!rules.isEmpty())){
+ if(!owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(ontocls, ontoind))){
+
+ //Add the rule istance
+ OWLClassAssertionAxiom classAssertion = factory.getOWLClassAssertionAxiom(ontocls,ontoind);
+ owlmanager.addAxiom(owlmodel, classAssertion);
+
+ //start and end
+ OWLNamedIndividual ind = factory.getOWLNamedIndividual(rules.firstElement());
+ if(owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(kresrule, ind))){
+ objectPropAssertion = factory.getOWLObjectPropertyAssertionAxiom(start, ontoind,ind);
+ owlmanager.addAxiom(owlmodel, objectPropAssertion);
+ ok = true;
+ }else{
+ System.err.println("The rule with IRI "+ind.getIRI()+" is not inside the ontology. Pleas check its IRI.");
+ ok = false;
+ return(ok);
+ }
+
+ ind = factory.getOWLNamedIndividual(rules.lastElement());
+ if(owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(kresrule, ind))){
+ objectPropAssertion = factory.getOWLObjectPropertyAssertionAxiom(end, ontoind,ind);
+ owlmanager.addAxiom(owlmodel, objectPropAssertion);
+ ok = true;
+ }else{
+ System.err.println("The rule with IRI "+ind.getIRI()+" is not inside the ontology. Pleas check its IRI.");
+ ok = false;
+ return(ok);
+ }
+
+ //Add the sequence string
+ OWLDataPropertyAssertionAxiom dataPropAssertion = factory.getOWLDataPropertyAssertionAxiom(sequence, ontoind,rules.toString().replace("[","").replace("]",""));
+ owlmanager.addAxiom(owlmodel, dataPropAssertion);
+
+ //Add description
+ if((recipeDescription!=null))
+ if(!recipeDescription.isEmpty()){
+ //Add the rule description
+ dataPropAssertion = factory.getOWLDataPropertyAssertionAxiom(description, ontoind, recipeDescription);
+ owlmanager.addAxiom(owlmodel, dataPropAssertion);
+ ok = true;
+ }
+
+ //Add single rule
+ for(int r = 0; r<rules.size()-1; r++){
+ ind = factory.getOWLNamedIndividual(rules.get(r));
+ if(owlmodel.containsIndividualInSignature(ind.getIRI())){
+ //Add the rule to the recipes
+ objectPropAssertion = factory.getOWLObjectPropertyAssertionAxiom(hasrule, ontoind,ind);
+ owlmanager.addAxiom(owlmodel, objectPropAssertion);
+
+ //Add precedes
+ OWLNamedIndividual indf = factory.getOWLNamedIndividual(rules.get(r+1));
+ if(owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(kresrule, indf))){
+ objectPropAssertion = factory.getOWLObjectPropertyAssertionAxiom(precedes,ind,indf);
+ owlmanager.addAxiom(owlmodel, objectPropAssertion);
+ ok = true;
+ }else{
+ System.err.println("The rule with IRI "+indf.getIRI()+" is not inside the ontology. Pleas check its IRI.");
+ ok = false;
+ return(ok);
+ }
+ }else{
+ System.err.println("The rule with IRI "+ind.getIRI()+" is not inside the ontology. Pleas check its IRI.");
+ ok = false;
+ return(ok);
+ }
+
+ }
+ //Add last element
+ ind = factory.getOWLNamedIndividual(rules.lastElement());
+ if(owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(kresrule, ind))){
+ //Add the rule to the recipes
+ objectPropAssertion = factory.getOWLObjectPropertyAssertionAxiom(hasrule, ontoind,ind);
+ owlmanager.addAxiom(owlmodel, objectPropAssertion);
+ ok=true;
+ }else{
+ System.err.println("The rule with IRI "+ind.getIRI()+" is not inside the ontology. Pleas check its IRI.");
+ ok = false;
+ return(ok);
+ }
+
+ }else{
+ System.err.println("The recipe with name "+recipeName+" already exists. Pleas check the name.");
+ ok = false;
+ return(ok);
+ }
+
+ }else{
+ System.err.println("The recipe with name and the set of rules cannot be empity or null.");
+ ok=false;
+ return(ok);
+ }
+ }
+ if(ok)
+ this.storeaux.setStore(owlmodel);
+ return(ok);
+ }
+
+ /**
+ * Get the KReSRuleStore filled with rules and recipes
+ *
+ * @return {A KReSRuleStore object with the stored rules and recipes.}
+ */
+ public RuleStore getStore(){
+ return this.storeaux;
+ }
+
+}
Added: incubator/stanbol/trunk/kres/rules/manager/src/main/java/org/apache/stanbol/rules/manager/changes/KReSAddRule.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/kres/rules/manager/src/main/java/org/apache/stanbol/rules/manager/changes/KReSAddRule.java?rev=1082166&view=auto
==============================================================================
--- incubator/stanbol/trunk/kres/rules/manager/src/main/java/org/apache/stanbol/rules/manager/changes/KReSAddRule.java (added)
+++ incubator/stanbol/trunk/kres/rules/manager/src/main/java/org/apache/stanbol/rules/manager/changes/KReSAddRule.java Wed Mar 16 15:31:08 2011
@@ -0,0 +1,378 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package org.apache.stanbol.rules.manager.changes;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.stanbol.rules.base.api.RuleStore;
+import org.semanticweb.owlapi.apibinding.OWLManager;
+import org.semanticweb.owlapi.model.AddImport;
+import org.semanticweb.owlapi.model.IRI;
+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.OWLDataPropertyAssertionAxiom;
+import org.semanticweb.owlapi.model.OWLImportsDeclaration;
+import org.semanticweb.owlapi.model.OWLNamedIndividual;
+import org.semanticweb.owlapi.model.OWLOntology;
+import org.semanticweb.owlapi.model.OWLOntologyChange;
+import org.semanticweb.owlapi.model.OWLOntologyCreationException;
+import org.semanticweb.owlapi.model.OWLOntologyManager;
+
+/**
+ * This class will add new rule to the KReSRuleStore used as input.<br/>
+ * The KReSRuleStore object used as input is not changed and to get the new modified KReSRuleStore there is the method getStore().<br/>
+ * If a rule with a same name or IRI is already inside the KReSRuleStore an error is lunched and the process stopped.
+ *
+ */
+public class KReSAddRule {
+
+ private OWLOntology owlmodel;
+ private OWLOntologyManager owlmanager;
+ private OWLDataFactory factory;
+ private String owlID;
+ private String owlIDrmi;
+ private RuleStore storeaux;
+
+ /**
+ * To create a list of imported ontlogy to be added as import declarations
+ *
+ * @param inowl {Input ontology where to get the import declarations}
+ * @return {A list of declarations}
+ */
+ private List<OWLOntologyChange> createImportList(OWLOntology inowl,OWLOntology toadd){
+
+ Iterator<OWLOntology> importedonto = inowl.getDirectImports().iterator();
+ List<OWLOntologyChange> additions = new LinkedList<OWLOntologyChange>();
+ OWLDataFactory auxfactory = inowl.getOWLOntologyManager().getOWLDataFactory();
+
+ while(importedonto.hasNext()){
+ OWLOntology auxonto = importedonto.next();
+ additions.add(new AddImport(toadd,auxfactory.getOWLImportsDeclaration(auxonto.getOWLOntologyManager().getOntologyDocumentIRI(auxonto))));
+ }
+
+ if(additions.size()==0){
+ Iterator<OWLImportsDeclaration> importedontob = inowl.getImportsDeclarations().iterator();
+ additions = new LinkedList<OWLOntologyChange>();
+ auxfactory = inowl.getOWLOntologyManager().getOWLDataFactory();
+
+ while(importedontob.hasNext()){
+ OWLImportsDeclaration auxontob = importedontob.next();
+ additions.add(new AddImport(toadd,auxontob));
+ }
+ }
+
+ return additions;
+ }
+
+ /**
+ * To clone ontology with all its axioms and imports declaration
+ *
+ * @param inowl {The onotlogy to be cloned}
+ * @return {An ontology with the same characteristics}
+ */
+ private void cloneOntology(OWLOntology inowl){
+
+ //Clone the targetontology
+ try {
+ this.owlmodel = OWLManager.createOWLOntologyManager().createOntology(inowl.getOntologyID().getOntologyIRI());
+ this.owlmanager = owlmodel.getOWLOntologyManager();
+ //Add axioms
+ owlmanager.addAxioms(owlmodel,inowl.getAxioms());
+ //Add import declaration
+ List<OWLOntologyChange> additions = createImportList(inowl,owlmodel);
+ if(additions.size()>0)
+ owlmanager.applyChanges(additions);
+ } catch (OWLOntologyCreationException ex) {
+ ex.printStackTrace();
+ }
+
+ }
+
+ /**
+ * Constructor, the input is a KReSRuleStore object.<br/>
+ * N.B. To get the new KReSRuleStore object there is the method getStore().
+ * @param store {The KReSRuleStore where to add the rule.}
+ */
+ public KReSAddRule(RuleStore store){
+ this.storeaux = store;
+ cloneOntology(storeaux.getOntology());
+ this.factory = owlmanager.getOWLDataFactory();
+ this.owlIDrmi ="http://kres.iks-project.eu/ontology/meta/rmi.owl#";
+ this.owlID = owlmodel.getOntologyID().getOntologyIRI().toString()+"#";
+ }
+
+ /**
+ * Constructor, the input is a KReSRuleStore object and a string contains the base iri of the resource.<br/>
+ * N.B. To get the new KReSRuleStore object there is the method getStore().
+ * @param owlid {The base iri of resource}
+ * @param store {The KReSRuleStore where to add the rule.}
+ */
+ public KReSAddRule(RuleStore store, String owlid){
+ this.storeaux = store;
+ //cloneOntology(storeaux.getOntology());
+ this.owlmanager = OWLManager.createOWLOntologyManager();
+ this.owlmodel = storeaux.getOntology();
+ this.factory = owlmanager.getOWLDataFactory();
+ this.owlIDrmi ="http://kres.iks-project.eu/ontology/meta/rmi.owl#";
+ this.owlID = owlid;
+ }
+
+ /**
+ * Method to add a Rule. The inputs are: a rule name string that doesn't exist in the ontology, a rule body->head string and eventualy a description of the rule
+ *
+ * @param ruleName {A string variable contains a name}
+ * @param ruleBodyHead {A string variable contains the body and head of the rule}
+ * @param ruleDescription {A briefly description of the rule}
+ * @return {A boolean that is true if the operation is ok}
+ */
+ public boolean addRule(String ruleName, String ruleBodyHead, String ruleDescription){
+ boolean ok = false;
+
+ OWLClass ontocls = factory.getOWLClass(IRI.create(owlIDrmi+"KReSRule"));
+ OWLNamedIndividual ontoind = factory.getOWLNamedIndividual(IRI.create(owlID+ruleName));
+ OWLDataProperty description = factory.getOWLDataProperty(IRI.create(owlIDrmi+"hasDescription"));
+ OWLDataProperty bodyhead = factory.getOWLDataProperty(IRI.create(owlIDrmi+"hasBodyAndHead"));
+
+ if(((ruleName!=null)||!ruleName.isEmpty())&&((ruleBodyHead!=null)||!ruleBodyHead.isEmpty())){
+ if(!owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(ontocls, ontoind))){
+
+ //Add the rule istance
+ OWLClassAssertionAxiom classAssertion = factory.getOWLClassAssertionAxiom(ontocls,ontoind);
+ owlmanager.addAxiom(owlmodel, classAssertion);
+ //Add the rule Body and Head
+ OWLDataPropertyAssertionAxiom dataPropAssertion = factory.getOWLDataPropertyAssertionAxiom(bodyhead, ontoind, ruleBodyHead);
+ owlmanager.addAxiom(owlmodel, dataPropAssertion);
+
+ if((ruleDescription!=null))
+ if(!ruleDescription.isEmpty()){
+ //Add the rule description
+ dataPropAssertion = factory.getOWLDataPropertyAssertionAxiom(description, ontoind, ruleDescription);
+ owlmanager.addAxiom(owlmodel, dataPropAssertion);
+ }
+
+ ok = true;
+ }
+ else{
+ System.err.println("The rule with name "+ruleName+" already exists. Pleas check the name.");
+ ok = false;
+ return(ok);
+ }
+
+ }
+ else{
+ System.err.println("The rule with name and the body-head string cannot be empity or null.");
+ ok=false;
+ return(ok);
+ }
+ if(ok)
+ this.storeaux.setStore(owlmodel);
+
+ return(ok);
+ }
+
+ /**
+ * Method to add a Rule. The inputs are: a rule name IRI that doesn't exist in the ontology, a rule body->head string and eventualy a description of the rule
+ *
+ * @param ruleName {An IRI variable contains the complete name}
+ * @param ruleBodyHead {A string variable contains the body and head of the rule}
+ * @param ruleDescription {A briefly description of the rule}
+ * @return {A boolean that is true if the operation is ok}
+ */
+ public boolean addRule(IRI ruleName, String ruleBodyHead, String ruleDescription){
+ boolean ok = false;
+
+ OWLClass ontocls = factory.getOWLClass(IRI.create(owlIDrmi+"KReSRule"));
+ OWLNamedIndividual ontoind = factory.getOWLNamedIndividual(ruleName);
+ OWLDataProperty description = factory.getOWLDataProperty(IRI.create(owlIDrmi+"hasDescription"));
+ OWLDataProperty bodyhead = factory.getOWLDataProperty(IRI.create(owlIDrmi+"hasBodyAndHead"));
+
+ if(((ruleName!=null)&&!ruleName.toString().isEmpty())&&((ruleBodyHead!=null)&&!ruleBodyHead.isEmpty())){
+ System.out.println("entriamo "+ruleName);
+ if(!owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(ontocls, ontoind))){
+
+ //Add the rule istance
+ OWLClassAssertionAxiom classAssertion = factory.getOWLClassAssertionAxiom(ontocls,ontoind);
+ owlmanager.addAxiom(owlmodel, classAssertion);
+ //Add the rule Body and Head
+ OWLDataPropertyAssertionAxiom dataPropAssertion = factory.getOWLDataPropertyAssertionAxiom(bodyhead, ontoind, ruleBodyHead);
+ owlmanager.addAxiom(owlmodel, dataPropAssertion);
+
+ if(ruleDescription!=null)
+ if(!ruleDescription.isEmpty()){
+ //Add the rule description
+ dataPropAssertion = factory.getOWLDataPropertyAssertionAxiom(description, ontoind, ruleDescription);
+ owlmanager.addAxiom(owlmodel, dataPropAssertion);
+ }
+
+ ok = true;
+ }
+ else{
+ System.err.println("The rule with name "+ruleName+" already exists. Pleas check the name.");
+ ok = false;
+ return(ok);
+ }
+
+ }
+ else{
+ System.err.println("The rule with name and the body-head string cannot be empity or null.");
+ ok=false;
+ return(ok);
+ }
+
+ if(ok){
+ this.storeaux.setStore(owlmodel);
+ }
+
+ return(ok);
+ }
+
+ /**
+ * Method to add a Rule. The inputs are two HashMap with the key the rule name and the value are the Body -> Head string a rule description.
+ *
+ * @param ruleBodyHeadMap {An HashMap variable contains string rule name as key and the body and head as value}
+ * @param ruleDescriptionMap {An HashMap variable contains string rule name as key and the rule's description as value}
+ * @return {A boolean that is true if the operation is ok}
+ */
+ public boolean addRuleMap(HashMap<String,String> ruleBodyHeadMap, HashMap<String,String> ruleDescriptionMap){
+ boolean ok = false;
+
+ OWLClass ontocls = factory.getOWLClass(IRI.create(owlIDrmi+"KReSRule"));
+ OWLDataProperty description = factory.getOWLDataProperty(IRI.create(owlIDrmi+"hasDescription"));
+ OWLDataProperty bodyhead = factory.getOWLDataProperty(IRI.create(owlIDrmi+"hasBodyAndHead"));
+ Object[] keys = ruleBodyHeadMap.keySet().toArray();
+
+ String ruleDescription = "";
+
+ for(int k = 0; k<keys.length;k++){
+ String ruleName = (String) keys[k];
+ String ruleBodyHead = ruleBodyHeadMap.get(ruleName);
+
+ if((ruleDescriptionMap!=null)&&!ruleDescriptionMap.isEmpty()){
+ ruleDescription = ruleDescriptionMap.get(ruleName);
+ }else{
+ ruleDescription ="";
+ }
+
+ OWLNamedIndividual ontoind = factory.getOWLNamedIndividual(IRI.create(owlID+ruleName));
+ if(((ruleName!=null)||!ruleName.isEmpty())&&((ruleBodyHead!=null)||!ruleBodyHead.isEmpty())){
+ if(!owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(ontocls, ontoind))){
+
+ //Add the rule istance
+ OWLClassAssertionAxiom classAssertion = factory.getOWLClassAssertionAxiom(ontocls,ontoind);
+ owlmanager.addAxiom(owlmodel, classAssertion);
+ //Add the rule Body and Head
+ OWLDataPropertyAssertionAxiom dataPropAssertion = factory.getOWLDataPropertyAssertionAxiom(bodyhead, ontoind, ruleBodyHead);
+ owlmanager.addAxiom(owlmodel, dataPropAssertion);
+
+ if((ruleDescription!=null))
+ if(!ruleDescription.isEmpty()){
+ //Add the rule description
+ dataPropAssertion = factory.getOWLDataPropertyAssertionAxiom(description, ontoind, ruleDescription);
+ owlmanager.addAxiom(owlmodel, dataPropAssertion);
+ }
+
+ ok = true;
+ }else{
+ System.err.println("The rule with name "+ruleName+" already exists. Pleas check the name.");
+ ok = false;
+ return(ok);
+ }
+
+ }else{
+ System.err.println("The rule with name and the body-head string cannot be empity or null.");
+ ok=false;
+ return(ok);
+ }
+
+ }
+ if(ok)
+ this.storeaux.setStore(owlmodel);
+
+ return(ok);
+ }
+
+ /**
+ * Method to add a Rule. The inputs are two HashMap with the key the rule name and the value are the Body -> Head string a rule description.
+ *
+ * @param ruleBodyHeadMap {An HashMap variable contains string rule name as key and the body and head as value}
+ * @param ruleDescriptionMap {An HashMap variable contains string rule name as key and the rule's description as value}
+ * @return {A boolean that is true if the operation is ok}
+ */
+
+ public boolean addRuleMapIRI(HashMap<IRI,String> ruleBodyHeadMap, HashMap<IRI,String> ruleDescriptionMap){
+
+ boolean ok = false;
+
+ OWLClass ontocls = factory.getOWLClass(IRI.create(owlIDrmi+"KReSRule"));
+ OWLDataProperty description = factory.getOWLDataProperty(IRI.create(owlIDrmi+"hasDescription"));
+ OWLDataProperty bodyhead = factory.getOWLDataProperty(IRI.create(owlIDrmi+"hasBodyAndHead"));
+ Iterator<IRI> keys = ruleBodyHeadMap.keySet().iterator();
+ String ruleDescription = "";
+
+ while(keys.hasNext()){
+ IRI ruleName = keys.next();
+ String ruleBodyHead = ruleBodyHeadMap.get(ruleName);
+
+ if((ruleDescriptionMap!=null)&&!ruleDescriptionMap.isEmpty()){
+ ruleDescription = ruleDescriptionMap.get(ruleName);
+ }else{
+ ruleDescription ="";
+ }
+
+ OWLNamedIndividual ontoind = factory.getOWLNamedIndividual(ruleName);
+ if(((ruleName!=null)&&!ruleName.toString().isEmpty())&&((ruleBodyHead!=null)&&!ruleBodyHead.isEmpty())){
+ if(!owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(ontocls, ontoind))){
+
+ //Add the rule istance
+ OWLClassAssertionAxiom classAssertion = factory.getOWLClassAssertionAxiom(ontocls,ontoind);
+ owlmanager.addAxiom(owlmodel, classAssertion);
+ //Add the rule Body and Head
+ OWLDataPropertyAssertionAxiom dataPropAssertion = factory.getOWLDataPropertyAssertionAxiom(bodyhead, ontoind, ruleBodyHead);
+ owlmanager.addAxiom(owlmodel, dataPropAssertion);
+
+ if((ruleDescription!=null))
+ if(!ruleDescription.isEmpty()){
+ //Add the rule description
+ dataPropAssertion = factory.getOWLDataPropertyAssertionAxiom(description, ontoind, ruleDescription);
+ owlmanager.addAxiom(owlmodel, dataPropAssertion);
+ }
+
+ ok= true;
+ }else{
+ System.err.println("The rule with name "+ruleName+" already exists. Pleas check the name.");
+ ok = false;
+ return(ok);
+ }
+
+ }else{
+ System.err.println("The rule with name and the body-head string cannot be empity or null.");
+ ok=false;
+ return(ok);
+ }
+
+ }
+
+ if(ok)
+ this.storeaux.setStore(owlmodel);
+
+ return(ok);
+ }
+
+
+ /**
+ * Get the KReSRuleStore filled with rules and recipes
+ *
+ * @return {A KReSRuleStore object with the stored rules and recipes.}
+ */
+ public RuleStore getStore(){
+ return this.storeaux;
+ }
+}
Added: incubator/stanbol/trunk/kres/rules/manager/src/main/java/org/apache/stanbol/rules/manager/changes/KReSGetRecipe.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/kres/rules/manager/src/main/java/org/apache/stanbol/rules/manager/changes/KReSGetRecipe.java?rev=1082166&view=auto
==============================================================================
--- incubator/stanbol/trunk/kres/rules/manager/src/main/java/org/apache/stanbol/rules/manager/changes/KReSGetRecipe.java (added)
+++ incubator/stanbol/trunk/kres/rules/manager/src/main/java/org/apache/stanbol/rules/manager/changes/KReSGetRecipe.java Wed Mar 16 15:31:08 2011
@@ -0,0 +1,215 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package org.apache.stanbol.rules.manager.changes;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Set;
+import java.util.Vector;
+
+import org.apache.stanbol.rules.base.api.RuleStore;
+import org.semanticweb.owlapi.model.IRI;
+import org.semanticweb.owlapi.model.OWLClass;
+import org.semanticweb.owlapi.model.OWLDataFactory;
+import org.semanticweb.owlapi.model.OWLDataProperty;
+import org.semanticweb.owlapi.model.OWLIndividual;
+import org.semanticweb.owlapi.model.OWLLiteral;
+import org.semanticweb.owlapi.model.OWLNamedIndividual;
+import org.semanticweb.owlapi.model.OWLOntology;
+import org.semanticweb.owlapi.model.OWLOntologyManager;
+
+/**
+ *
+ * @author elvio
+ */
+public class KReSGetRecipe {
+
+ private OWLOntology owlmodel;
+ private String owlID;
+ private String owlIDrmi;
+ private OWLOntologyManager owlmanager;
+ private OWLDataFactory factory;
+
+ /**
+ * Constructor, the input is a KReSRuleStore object.
+ *
+ * @param store {The KReSRuleStore where there are the added rules and recipes.}
+ */
+ public KReSGetRecipe(RuleStore store){
+ this.owlmodel = store.getOntology();
+ this.owlIDrmi = "http://kres.iks-project.eu/ontology/meta/rmi.owl#";
+ this.owlID = owlmodel.getOntologyID().toString().replace("<","").replace(">","")+"#";
+ this.owlmanager = owlmodel.getOWLOntologyManager();
+ this.factory = owlmanager.getOWLDataFactory();
+ }
+
+ /**
+ * This method returns the IRI of the named recipe with its sequence string
+ *
+ * @param recipename {It is the string name of the recipe}
+ * @return {Return an HashMap with the IRI as a key and the sequence of rules as value.}
+ */
+ public HashMap<IRI,String> getRecipe(String recipename){
+
+ HashMap<IRI,String> recipe = new HashMap();
+ OWLClass ontocls = factory.getOWLClass(IRI.create(owlIDrmi+"Recipe"));
+ OWLNamedIndividual indrecipe = factory.getOWLNamedIndividual(IRI.create(owlID+recipename));
+
+ if(owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(ontocls, indrecipe))){
+
+ OWLDataProperty prop = factory.getOWLDataProperty(IRI.create(owlIDrmi + "hasSequence"));
+ Set<OWLLiteral> value = indrecipe.getDataPropertyValues(prop, owlmodel);
+ if(!value.isEmpty()){
+
+ recipe.put(indrecipe.getIRI(),value.iterator().next().getLiteral());
+ }else{
+ recipe.put(indrecipe.getIRI(),"");
+ }
+
+ }else{
+ System.err.println("The recipe with name "+recipename+" doesn't exist.");
+ return(null);
+ }
+
+ return recipe;
+ }
+
+ /**
+ * This method returns the IRI of the named recipe with its sequence string
+ *
+ * @param recipename {It is the IRI name of the recipe}
+ * @return {Return an HashMap with the IRI as a key and the sequence of rules as value.}
+ */
+ public HashMap<IRI,String> getRecipe(IRI recipename){
+
+ HashMap<IRI,String> recipe = new HashMap();
+ OWLClass ontocls = factory.getOWLClass(IRI.create(owlIDrmi+"Recipe"));
+ OWLNamedIndividual indrecipe = factory.getOWLNamedIndividual(recipename);
+
+ if(owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(ontocls, indrecipe))){
+ OWLDataProperty prop = factory.getOWLDataProperty(IRI.create(owlIDrmi + "hasSequence"));
+ Set<OWLLiteral> value = indrecipe.getDataPropertyValues(prop, owlmodel);
+ if(!value.isEmpty()){
+ recipe.put(indrecipe.getIRI(),value.iterator().next().getLiteral());
+ }else{
+ recipe.put(indrecipe.getIRI(),"");
+ }
+
+ }else{
+ System.err.println("The recipe with name "+recipename+" doesn't exist.");
+ return(null);
+ }
+
+ return recipe;
+ }
+
+ /**
+ * This methods returns all the recipes with their sequence stored inside the ontology
+ *
+ * @return {Return an HashMap with the IRI as a key and the sequence of rules value.}
+ */
+ public HashMap<IRI,String> getAllRecipes(){
+ HashMap<IRI,String> recipe = new HashMap();
+ OWLDataProperty prop = factory.getOWLDataProperty(IRI.create(owlIDrmi + "hasSequence"));
+ Iterator<OWLNamedIndividual> indaxiom = owlmodel.getIndividualsInSignature().iterator();
+
+ while(indaxiom.hasNext()){
+ OWLNamedIndividual ax = indaxiom.next();
+ Set<OWLLiteral> value = ax.getDataPropertyValues(prop, owlmodel);
+ if(!value.isEmpty())
+ recipe.put(ax.getIRI(),value.iterator().next().getLiteral());
+ }
+
+ return(recipe);
+ }
+
+ /**
+ * This methods returns all the recipes with their sequence stored inside the ontology
+ *
+ * @return {Return an HashMap with the IRI as a key and the sequence of rules value.}
+ */
+ public Vector<IRI> getGeneralRecipes(){
+ Vector<IRI> recipe = new Vector();
+ OWLClass recipeclas = factory.getOWLClass(IRI.create(owlIDrmi + "Recipe"));
+ Iterator<OWLIndividual> indaxiom = recipeclas.getIndividuals(owlmodel).iterator();
+ while(indaxiom.hasNext()){
+ OWLIndividual axind = indaxiom.next();
+ recipe.add(IRI.create(axind.toStringID()));
+ }
+
+ return(recipe);
+ }
+
+ /**
+ * This methods returns a map contains the count of the seuqnce compound of two rule
+ *
+ * @return {Return an HashMap with the IRI as a key and the sequence of rules value.}
+ */
+ public HashMap<String,Integer> getBinSequenceRecipeCount(){
+ HashMap<String,Integer> recipe = new HashMap();
+ HashMap<IRI, String> map = getAllRecipes();
+ Iterator<IRI> keys = map.keySet().iterator();
+
+ while(keys.hasNext()){
+ IRI iri = keys.next();
+ String[] sequence = map.get(iri).split(",");
+ String bin ="";
+ for(int i = 0; i<sequence.length-1;i++){
+ bin = sequence[i].replace(" ","").trim()+" precedes "+ sequence[i+1].replace(" ","").trim();
+ if(recipe.containsKey(bin)){
+ recipe.put(bin,recipe.get(bin)+1);
+ }else{
+ recipe.put(bin,1);
+ }
+ }
+ }
+
+ return(recipe);
+ }
+
+ /**
+ * To get the description of a recipe
+ * @param recipeName {A IRI contains the full recipe name}
+ * @return {A string contains the description}
+ */
+ public String getDescription(IRI recipeName){
+ OWLNamedIndividual ontoind = factory.getOWLNamedIndividual(recipeName);
+ OWLDataProperty description = factory.getOWLDataProperty(IRI.create(owlIDrmi+"hasDescription"));
+ Set<OWLLiteral> lit = ontoind.getDataPropertyValues(description, owlmodel);
+ try{
+ String string = lit.iterator().next().getLiteral();
+ if(string!=null)
+ return string;
+ else
+ return null;
+ }catch(Exception e){
+ return null;
+ }
+
+ }
+
+ /**
+ * To get the description of a recipe
+ * @param recipeName {A string contains the recipe name}
+ * @return {A string contains the description}
+ */
+ public String getDescription(String recipeName){
+ OWLNamedIndividual ontoind = factory.getOWLNamedIndividual(IRI.create(owlID+recipeName));
+ OWLDataProperty description = factory.getOWLDataProperty(IRI.create(owlIDrmi+"hasDescription"));
+ Set<OWLLiteral> lit = ontoind.getDataPropertyValues(description, owlmodel);
+ try{
+ String string = lit.iterator().next().getLiteral();
+ if(string!=null)
+ return string;
+ else
+ return null;
+ }catch(Exception e){
+ return null;
+ }
+
+ }
+
+}
Added: incubator/stanbol/trunk/kres/rules/manager/src/main/java/org/apache/stanbol/rules/manager/changes/KReSGetRule.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/kres/rules/manager/src/main/java/org/apache/stanbol/rules/manager/changes/KReSGetRule.java?rev=1082166&view=auto
==============================================================================
--- incubator/stanbol/trunk/kres/rules/manager/src/main/java/org/apache/stanbol/rules/manager/changes/KReSGetRule.java (added)
+++ incubator/stanbol/trunk/kres/rules/manager/src/main/java/org/apache/stanbol/rules/manager/changes/KReSGetRule.java Wed Mar 16 15:31:08 2011
@@ -0,0 +1,266 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package org.apache.stanbol.rules.manager.changes;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+import java.util.Vector;
+
+import org.apache.stanbol.rules.base.api.RuleStore;
+import org.semanticweb.owlapi.apibinding.OWLManager;
+import org.semanticweb.owlapi.model.IRI;
+import org.semanticweb.owlapi.model.OWLAxiom;
+import org.semanticweb.owlapi.model.OWLClass;
+import org.semanticweb.owlapi.model.OWLDataFactory;
+import org.semanticweb.owlapi.model.OWLDataProperty;
+import org.semanticweb.owlapi.model.OWLIndividual;
+import org.semanticweb.owlapi.model.OWLLiteral;
+import org.semanticweb.owlapi.model.OWLNamedIndividual;
+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.util.OWLEntityRemover;
+
+/**
+ *
+ * @author elvio
+ */
+public class KReSGetRule {
+
+ private OWLOntology owlmodel;
+ private String owlID;
+ private String owlIDrmi;
+ private OWLOntologyManager owlmanager;
+ private OWLDataFactory factory;
+
+ /**
+ * Constructor, the input is a KReSRuleStore object.
+ *
+ * @param store {The KReSRuleStore where there are the added rules and recipes.}
+ */
+ public KReSGetRule(RuleStore store){
+ this.owlmodel = store.getOntology();
+ this.owlIDrmi = "http://kres.iks-project.eu/ontology/meta/rmi.owl#";
+ this.owlID = owlmodel.getOntologyID().toString().replace("<","").replace(">","")+"#";
+ this.owlmanager = owlmodel.getOWLOntologyManager();
+ this.factory = owlmanager.getOWLDataFactory();
+ }
+
+ /**
+ * To return the IRI of the named rule with its string Body -> Head
+ *
+ * @param rulename {It is the string name of the rule}
+ * @return {Return an HashMap with the IRI as a key and the rule string as value.}
+ */
+ public HashMap<IRI,String> getRule(String rulename){
+
+ HashMap<IRI,String> rule = new HashMap();
+ OWLClass ontocls = factory.getOWLClass(IRI.create(owlIDrmi+"KReSRule"));
+ OWLNamedIndividual indrule = factory.getOWLNamedIndividual(IRI.create(owlID+rulename));
+
+ if(owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(ontocls, indrule))){
+
+ OWLDataProperty prop = factory.getOWLDataProperty(IRI.create(owlIDrmi + "hasBodyAndHead"));
+ Set<OWLLiteral> value = indrule.getDataPropertyValues(prop, owlmodel);
+
+ rule.put(indrule.getIRI(),value.iterator().next().getLiteral());
+
+ }else{
+ System.err.println("The rule with name "+rulename+" doesn't exist.");
+ return(null);
+ }
+
+ return rule;
+ }
+
+ /**
+ * To return the IRI of the named rule with its string Body -> Head
+ *
+ * @param rulename {It is the IRI name of the rule}
+ * @return {Return an HashMap with the IRI as a key and the rule string as value.}
+ */
+ public HashMap<IRI,String> getRule(IRI rulename){
+
+ HashMap<IRI,String> rule = new HashMap();
+ OWLClass ontocls = factory.getOWLClass(IRI.create(owlIDrmi+"KReSRule"));
+ OWLNamedIndividual indrule = factory.getOWLNamedIndividual(rulename);
+
+ if(owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(ontocls, indrule))){
+ OWLDataProperty prop = factory.getOWLDataProperty(IRI.create(owlIDrmi + "hasBodyAndHead"));
+ Set<OWLLiteral> value = indrule.getDataPropertyValues(prop, owlmodel);
+
+ rule.put(indrule.getIRI(),value.iterator().next().getLiteral());
+
+ }else{
+ System.err.println("The rule with name "+rulename+" doesn't exist.");
+ return(null);
+ }
+
+ return rule;
+ }
+
+ /**
+ * Return all the rules stored inside the ontology
+ *
+ * @return {Return an HashMap with the IRI as a key and the rule string as value.}
+ */
+ public HashMap<IRI,String> getAllRules(){
+ HashMap<IRI,String> rule = new HashMap();
+ OWLDataProperty prop = factory.getOWLDataProperty(IRI.create(owlIDrmi + "hasBodyAndHead"));
+ Iterator<OWLNamedIndividual> indaxiom = owlmodel.getIndividualsInSignature().iterator();
+
+ while(indaxiom.hasNext()){
+ OWLNamedIndividual ax = indaxiom.next();
+ Set<OWLLiteral> value = ax.getDataPropertyValues(prop, owlmodel);
+ if(!value.isEmpty())
+ rule.put(ax.getIRI(),value.iterator().next().getLiteral());
+ }
+
+ return(rule);
+ }
+
+ /**
+ * Return all the rules of a recipe stored inside the ontology
+ *
+ * @return {Return an HashMap with the IRI as a key and the rule string as value.}
+ */
+ public OWLOntology getAllRulesOfARecipe(IRI recipeIRI){
+ ArrayList<IRI> ruleIRIs = new ArrayList<IRI>();
+ OWLObjectProperty prop = factory.getOWLObjectProperty(IRI.create(owlIDrmi+"hasRule"));
+ Iterator<OWLNamedIndividual> indaxiom = owlmodel.getIndividualsInSignature().iterator();
+
+ OWLOntology ontlogy = null;
+ try {
+ ontlogy = OWLManager.createOWLOntologyManager().createOntology();
+ } catch (OWLOntologyCreationException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+
+
+ System.out.println("RECIPE IRI : "+recipeIRI.toString());
+
+ if(ontlogy != null){
+ OWLIndividual ind = factory.getOWLNamedIndividual(recipeIRI);
+ Set<OWLIndividual> rules = ind.getObjectPropertyValues(prop, owlmodel);
+
+ System.out.println("Rules length : "+rules.size());
+ for(OWLIndividual rule : rules){
+ OWLAxiom axiom = factory.getOWLObjectPropertyAssertionAxiom(prop, ind, rule);
+ owlmanager.addAxiom(ontlogy, axiom);
+ }
+ }
+
+ /*while(indaxiom.hasNext()){
+ OWLNamedIndividual ax = indaxiom.next();
+ Set<OWLIndividual> value = ax.getObjectPropertyValues(prop, owlmodel);
+ if(value != null){
+ for(OWLIndividual ind : value){
+ ruleIRIs.add(IRI.create(ind.toStringID()));
+ }
+ }
+
+ }*/
+
+ return ontlogy;
+ }
+
+
+
+ /**
+ * To get the Recipes where the rule is used.
+ *
+ * @param ruleName {The IRI of the rule}
+ * @return {A Vector with the IRI of the recipes.}
+ */
+ public Vector<IRI> getRuleUsage(IRI ruleName){
+ Vector<IRI> ruleusage = new Vector();
+ OWLClass ontocls = factory.getOWLClass(IRI.create(owlIDrmi+"KReSRule"));
+ OWLNamedIndividual ontoind = factory.getOWLNamedIndividual(ruleName);
+ OWLEntityRemover remover = new OWLEntityRemover(owlmanager, Collections.singleton(owlmodel));
+ OWLObjectProperty hasrule = factory.getOWLObjectProperty(IRI.create(owlIDrmi+"hasRule"));
+
+
+ if(owlmodel.containsAxiom(factory.getOWLClassAssertionAxiom(ontocls, ontoind))){
+
+ ontoind.accept(remover);
+ List<OWLOntologyChange> list = remover.getChanges();
+ Iterator<OWLOntologyChange> iter = list.iterator();
+
+ while(iter.hasNext()){
+ OWLAxiom ax =iter.next().getAxiom();
+ if(ax.getObjectPropertiesInSignature().contains(hasrule)){
+ String[] data = Arrays.toString(ax.getIndividualsInSignature().toArray()).split(",");
+ for(int s = 0; s<data.length;s++){
+ if(!data[s].contains(ontoind.getIRI().toString())){
+ String string = data[s].replace("<","").replace(">","").replace("[","").replace("]","").replace(" ","");
+ if(!string.startsWith("["))
+ ruleusage.add(IRI.create(string));
+ }
+ }
+ }
+ }
+
+ remover.reset();
+
+ return(ruleusage);
+ }else{
+ System.err.println("The rule with name "+ruleName+" is not inside the ontology. Pleas check the name.");
+ return(null);
+ }
+ }
+
+ /**
+ * To get the description of a rule
+ * @param ruleName {A IRI contains the full rule name}
+ * @return {A string contains the description}
+ */
+ public String getDescription(IRI ruleName){
+ OWLNamedIndividual ontoind = factory.getOWLNamedIndividual(ruleName);
+ OWLDataProperty description = factory.getOWLDataProperty(IRI.create(owlIDrmi+"hasDescription"));
+ Set<OWLLiteral> lit = ontoind.getDataPropertyValues(description, owlmodel);
+ try{
+ String string = lit.iterator().next().getLiteral();
+ if(string!=null)
+ return string;
+ else
+ return null;
+ }catch(Exception e){
+ return null;
+ }
+
+ }
+
+ /**
+ * To get the description of a rule
+ * @param ruleName {A string contains the rule name}
+ * @return {A string contains the description}
+ */
+ public String getDescription(String ruleName){
+ OWLNamedIndividual ontoind = factory.getOWLNamedIndividual(IRI.create(owlID+ruleName));
+ OWLDataProperty description = factory.getOWLDataProperty(IRI.create(owlIDrmi+"hasDescription"));
+ Set<OWLLiteral> lit = ontoind.getDataPropertyValues(description, owlmodel);
+ try{
+ String string = lit.iterator().next().getLiteral();
+ if(string!=null)
+ return string;
+ else
+ return null;
+ }catch(Exception e){
+ return null;
+ }
+
+ }
+
+}
Added: incubator/stanbol/trunk/kres/rules/manager/src/main/java/org/apache/stanbol/rules/manager/changes/KReSLoadRuleFile.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/kres/rules/manager/src/main/java/org/apache/stanbol/rules/manager/changes/KReSLoadRuleFile.java?rev=1082166&view=auto
==============================================================================
--- incubator/stanbol/trunk/kres/rules/manager/src/main/java/org/apache/stanbol/rules/manager/changes/KReSLoadRuleFile.java (added)
+++ incubator/stanbol/trunk/kres/rules/manager/src/main/java/org/apache/stanbol/rules/manager/changes/KReSLoadRuleFile.java Wed Mar 16 15:31:08 2011
@@ -0,0 +1,281 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package org.apache.stanbol.rules.manager.changes;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.UnsupportedEncodingException;
+import java.util.Vector;
+
+import org.apache.stanbol.rules.base.api.RuleStore;
+import org.semanticweb.owlapi.apibinding.OWLManager;
+import org.semanticweb.owlapi.model.IRI;
+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.OWLDataPropertyAssertionAxiom;
+import org.semanticweb.owlapi.model.OWLNamedIndividual;
+import org.semanticweb.owlapi.model.OWLObjectProperty;
+import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom;
+import org.semanticweb.owlapi.model.OWLOntology;
+import org.semanticweb.owlapi.model.OWLOntologyCreationException;
+import org.semanticweb.owlapi.model.OWLOntologyManager;
+
+/**
+ * This class will store a set of rules and recipes, describe in a file, to the KReSRuleStore used as input.<br/>
+ * The file contains all the rules and recipes to be added to the KReSRuleStore. The rows are the following format:<br/>
+ * - All the rows that start with # will be ignored<br/>
+ * - All the rows that start with $ identify a class either a Recipe or a KReSRule<br/>
+ * - All the rows that start with @ identify a name<br/>
+ * - All the rows that start with * identify a comment<br/>
+ * <br/>
+ * An example of file is:<br/>
+ * #This is a comment to the file<br/>
+ * #These are the rules that I want to insert in the system.<br/>
+ * $KReSRule<br/>
+ * @MyRuleA<br/>
+ * *My comment to the rule A<br/>
+ * MyRuleABody -> MyRuleAHead<br/>
+ * @MyRuleB<br/>
+ * *My comment to the rule B<br/>
+ * MyRuleBBody -> MyRuleBHead<br/>
+ * @MyRuleC<br/>
+ * *My comment to the rule C<br/>
+ * MyRuleCBody -> MyRuleCHead<br/>
+ *<br/>
+ * #This is a recipe<br/>
+ * $Recipe<br/>
+ * @MyRecipe<br/>
+ * *My comment to the recipe<br/>
+ * MyRuleC<br/>
+ * MyRuleB<br/>
+ * MyRuleA<br/>
+ *<br/>
+ * N.B. The KReSRuleStore object used as input is not changed and to get the new modified KReSRuleStore there is the method getStore().
+ *
+ */
+public class KReSLoadRuleFile {
+
+ private OWLOntology owlmodel;
+ private OWLClass ontocls;
+ private OWLNamedIndividual ontoind;
+ private OWLClassAssertionAxiom classAssertion;
+ private OWLDataPropertyAssertionAxiom dataPropAssertion;
+ private OWLObjectPropertyAssertionAxiom objectPropAssertion;
+ private RuleStore storeaux;
+ private String owlIDrmi;
+
+ /**
+ * This class reads an ad hoc file contains all the rules and recipes:<br/>
+ * - All the rows that start with # will be ignored<br/>
+ * - All the rows that start with $ identify a class or a Recipe or a KReSRule<br/>
+ * - All the rows that start with @ identify a name<br/>
+ * - All the rows that start with * identify a comment
+ *
+ * @param filepath {Path of the file contains thr rules and the recipes}
+ * @param store {The KReSRuleStore where the rules will be storage}
+ */
+ public KReSLoadRuleFile(String filepath, RuleStore store){
+ this.storeaux = store;
+ this.owlIDrmi = "http://kres.iks-project.eu/ontology/meta/rmi.owl#";
+
+ try{
+ OWLOntology owlaux = OWLManager.createOWLOntologyManager().createOntology(store.getOntology().getOntologyID());
+ this.owlmodel = owlaux;
+ owlmodel.getOWLOntologyManager().addAxioms(owlmodel, storeaux.getOntology().getAxioms());
+ }catch(OWLOntologyCreationException e){
+ e.printStackTrace();
+ }
+
+ OWLOntologyManager owlmanager = owlmodel.getOWLOntologyManager();
+ OWLDataFactory factory = owlmanager.getOWLDataFactory();
+
+ String ID = owlmodel.getOntologyID().toString().replace("<","").replace(">","")+"#";
+ String input ="";
+ String cls ="";
+ String descrp ="";
+ String name="";
+ String text="";
+ Vector<String> seq = new Vector();
+
+ try{
+ File file = new File(filepath.trim());
+ BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF8"));
+
+ while ((input = br.readLine())!=null){
+ try{
+ input = input.trim();
+
+ if((!input.startsWith("#"))&(!input.isEmpty())){
+
+ //Verify the type of owl class
+ if(input.startsWith("$")){
+ //Add the last Recipe property
+ if(seq.size()>0){
+ OWLDataProperty dataprop = factory.getOWLDataProperty(IRI.create(owlIDrmi+"hasSequence"));
+ if(owlmodel.containsIndividualInSignature(ontoind.getIRI())){
+ dataPropAssertion = factory.getOWLDataPropertyAssertionAxiom(dataprop, ontoind,seq.toString().replace("[","").replace("]",""));
+ owlmanager.addAxiom(owlmodel,dataPropAssertion);
+ }else{
+ System.err.println("There isn't the istance to which you are trying to add the property. Pleas check that "+ontoind.getIRI()+" exists.");
+ this.owlmodel = null;
+ break;
+ }
+ }
+ seq.clear();
+ ontoind = null;
+ cls = input.replace("$","").replace(" ","");
+
+ if(owlmodel.containsClassInSignature(IRI.create(owlIDrmi+cls))){
+ ontocls = factory.getOWLClass(IRI.create(owlIDrmi+cls));
+ }else{
+ System.err.println("The file contains a wrong class name. Pleas check the name: "+IRI.create(owlIDrmi+cls));
+ this.owlmodel = null;
+ break;
+ }
+ }
+
+ //Get the instance name and create the istance
+ if(input.startsWith("@")){
+ name = input.replace("@", "").replace(" ","");
+
+ if(!owlmodel.containsIndividualInSignature(IRI.create(ID+name))){
+ ontoind = factory.getOWLNamedIndividual(IRI.create(ID+name));
+ classAssertion = factory.getOWLClassAssertionAxiom(ontocls,ontoind);
+ owlmanager.addAxiom(owlmodel, classAssertion);
+ }else{
+ System.err.println("The file contains a repeated istance name. The istance is already inside the ontology. Pleas check the name: "+input);
+ this.owlmodel = null;
+ break;
+ }
+ }
+
+ //Add an eventually description of the rule or recipe
+ if((input.startsWith("*")&&(ontoind!=null))){
+ descrp = input.replace("*", "").trim();
+ OWLDataProperty dataprop = factory.getOWLDataProperty(IRI.create(owlIDrmi+"hasDescription"));
+ if(owlmodel.containsIndividualInSignature(ontoind.getIRI())){
+ dataPropAssertion = factory.getOWLDataPropertyAssertionAxiom(dataprop, ontoind, descrp);
+ owlmanager.addAxiom(owlmodel, dataPropAssertion);
+ }else{
+ System.err.println("There isn't the istance to which you are trying to add the property. Pleas check that "+ontoind.getIRI()+" exists.");
+ this.owlmodel = null;
+ break;
+ }
+ }
+
+ //Add the rule
+ if(ontocls.getIRI().toString().equals(owlIDrmi+"KReSRule")&&(ontoind!=null)&&(!input.startsWith("*")&&(!input.startsWith("@"))&&(!input.startsWith("$")))){
+ text =input.trim();
+ OWLDataProperty dataprop = factory.getOWLDataProperty(IRI.create(owlIDrmi+"hasBodyAndHead"));
+
+ if(owlmodel.containsIndividualInSignature(ontoind.getIRI())){
+ dataPropAssertion = factory.getOWLDataPropertyAssertionAxiom(dataprop, ontoind, text);
+ owlmanager.addAxiom(owlmodel,dataPropAssertion);
+ }else{
+ System.err.println("There isn't the istance to which you are trying to add the property. Pleas check that "+ontoind.getIRI()+" exists.");
+ this.owlmodel = null;
+ break;
+ }
+ }
+
+ //Add the recipe with its rule set.
+ if(ontocls.getIRI().toString().equals(owlIDrmi+"Recipe")&&(ontoind!=null)&&(!input.startsWith("*"))&&(!input.startsWith("@"))&&(!input.startsWith("$"))){
+ text =input.trim();
+ if(owlmodel.containsIndividualInSignature(IRI.create(ID+text))&&(!IRI.create(ID+text).equals(ontoind.getIRI()))){
+ seq.add(ID+text);
+ if(owlmodel.containsIndividualInSignature(ontoind.getIRI())){
+ OWLObjectProperty objprop = factory.getOWLObjectProperty(IRI.create(owlIDrmi+"hasRule"));
+ OWLNamedIndividual ruleind = factory.getOWLNamedIndividual(IRI.create(ID+text));
+ objectPropAssertion = factory.getOWLObjectPropertyAssertionAxiom(objprop,ontoind, ruleind);
+ owlmanager.addAxiom(owlmodel,objectPropAssertion);
+
+ //Add the first rule to the Recipe
+ if(seq.size()==1){
+ objprop = factory.getOWLObjectProperty(IRI.create(owlIDrmi+"startWith"));
+ ruleind = factory.getOWLNamedIndividual(IRI.create(ID+text));
+ objectPropAssertion = factory.getOWLObjectPropertyAssertionAxiom(objprop,ontoind, ruleind);
+ owlmanager.addAxiom(owlmodel,objectPropAssertion);
+
+ //Add the sequential rules
+ }else if(seq.size()>1){
+ objprop = factory.getOWLObjectProperty(IRI.create("http://www.ontologydesignpatterns.org/cp/owl/sequence.owl#directlyPrecedes"));
+ OWLNamedIndividual ruleindp = factory.getOWLNamedIndividual(IRI.create(seq.get(seq.size()-2)));
+ OWLNamedIndividual ruleindf = factory.getOWLNamedIndividual(IRI.create(seq.lastElement()));
+ objectPropAssertion = factory.getOWLObjectPropertyAssertionAxiom(objprop,ruleindp, ruleindf);
+ owlmanager.addAxiom(owlmodel,objectPropAssertion);
+
+ //Add the last rule to the recipe
+ objprop = factory.getOWLObjectProperty(IRI.create(owlIDrmi+"endWith"));
+ objectPropAssertion = factory.getOWLObjectPropertyAssertionAxiom(objprop,ontoind, ruleindf);
+ owlmanager.addAxiom(owlmodel, objectPropAssertion);
+ //Remove the previous endWith
+ if(seq.size()>2){
+ OWLObjectPropertyAssertionAxiom remove = factory.getOWLObjectPropertyAssertionAxiom(objprop,ontoind, ruleindp);
+ owlmanager.removeAxiom(owlmodel,remove);
+ }
+ }
+
+ }else{
+ System.err.println("There isn't the istance to which you are trying to add the property. Pleas check that "+ontoind.getIRI()+" and/or "+ID+text+" are already inside the ontology.");
+ this.owlmodel = null;
+ break;
+ }
+ }
+ }
+ }
+ }catch(Exception g){
+ g.printStackTrace();
+ this.owlmodel = null;
+ break;
+ }
+ }
+
+ //Add the last sequence
+ if(seq.size()>0){
+ OWLDataProperty dataprop = factory.getOWLDataProperty(IRI.create(owlIDrmi+"hasSequence"));
+ if(owlmodel.containsIndividualInSignature(ontoind.getIRI())){
+ dataPropAssertion = factory.getOWLDataPropertyAssertionAxiom(dataprop, ontoind,seq.toString().replace("[","").replace("]",""));
+ owlmanager.addAxiom(owlmodel,dataPropAssertion);
+ }else{
+ System.err.println("There isn't the istance to which you are trying to add the property. Pleas check that "+ontoind.getIRI()+" exists.");
+ this.owlmodel = null;
+ }
+ }
+
+ if(owlmodel!=null)
+ storeaux.setStore(owlmodel);
+
+ }catch (UnsupportedEncodingException uee){
+ uee.printStackTrace();
+ this.owlmodel = null;
+ }catch (FileNotFoundException fnfe){
+ fnfe.printStackTrace();
+ this.owlmodel = null;
+ }catch (IOException ioe){
+ ioe.printStackTrace();
+ this.owlmodel = null;
+ }catch (Exception e){
+ e.printStackTrace();
+ this.owlmodel = null;
+ }
+ }
+
+ /**
+ * Get the KReSRuleStore filled with rules and recipes
+ *
+ * @return {A KReSRuleStore object with the stored rules and recipes.}
+ */
+ public RuleStore getStore(){
+ return this.storeaux;
+ }
+
+}
\ No newline at end of file