Hi all, I have been trying in vain to get pellet to work in Eclipse in a
dynamic web application. Pellet infers SWRL rules and works well in Protege and
as a 'stand alone' java class in Eclipse. Apparently, OntModel model =
ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC) requires HP
libs to work and the servlet has apache libs. The problem is that they don't
play nice.
My data goes from a JSP to the servlet which (should) pass it onto the class.
The servlet sees the HP libs in the class and throws exceptions about wanting
HP model and RDF libs. When I put them in it chages the nature of some
statement methods which apparently are different between HP and Apache.
I have tried other ways of reasoning with pellet such as: Reasoner reasoner =
ReasonerRegistry.getOWLReasoner();
reasoner = reasoner.bindSchema(model1);
// Obtain standard OWL-DL spec and attach the Pellet reasoner
OntModelSpec ontModelSpec = OntModelSpec.OWL_DL_MEM_RULE_INF;
//OntModelSpec ontModelSpec =
OntModelSpec.PelletReasonerFactory.THE_SPEC;
ontModelSpec.setReasoner(reasoner);
// Create ontology model with reasoner support
OntModel model = ModelFactory.createOntologyModel(ontModelSpec, model1);
to no avail.
Is there any way I can reason with pellet for an OntModel in an apache web
application in Tomcat 7?
Follows is the class that changes statements in the ontology and passes the
changed ontology onto the helper class to do the reasoning. See below, I tried
casting the OntModel from apache to HP and that didn't work.
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.ontology.OntModelSpec;
import org.apache.jena.rdf.model.Literal;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.ResourceFactory;
import org.apache.jena.rdf.model.Statement;
import org.apache.jena.rdf.model.StmtIterator;
import org.apache.jena.util.FileManager;
public class ModifyStatements {
public static void main(String[] args) {
//test data
String patientsName= "Patient_Doug";
int ptAge=62;
int iabc=5;
int iimpact=9;
int isupervision=3;
int ifeeding=11;
int itoilet=4;
int imobility=11;
deleteAndWriteStatements(patientsName, ptAge, iabc, iimpact,
isupervision, ifeeding, itoilet, imobility);
}
static List<String> thePropertyBeingChangedArray = new
ArrayList<String>();
static List<Integer> theNewLiteralsArray = new ArrayList<Integer>();
static List<Statement> statementsToRemoveArray = new
ArrayList<Statement>();
public static void deleteAndWriteStatements(String patientsName, int age,
int abc, int impact, int supervision, int feeding, int toilet, int mobility) {
Inf inf = new Inf();
theNewLiteralsArray.clear();
statementsToRemoveArray.clear();
thePropertyBeingChangedArray.clear();
Resource subject=null;
Property predicate=null;
OntModel model1 =
ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM);
String inputFile = "c:\\jena\\acuity.owl";
InputStream in = FileManager.get().open(inputFile);
if (in == null) {
throw new IllegalArgumentException("File: " + inputFile + " not
found");
}
model1.read(in,"");
String ns = "http://philshields.altervista.org/owl/unit.owl#";
String hq="http://example.org/";
//incoming integer array
theNewLiteralsArray.add(0,abc);
theNewLiteralsArray.add(1,impact);
theNewLiteralsArray.add(2,supervision);
theNewLiteralsArray.add(3,feeding);
theNewLiteralsArray.add(4,toilet);
theNewLiteralsArray.add(5,mobility);
theNewLiteralsArray.add(6,age);
System.out.println("Lereral array feeding=
"+theNewLiteralsArray.get(3));
//store the properties to be deleted then reinstated in an array
thePropertyBeingChangedArray.add(0,hq+"hasABCValue");
thePropertyBeingChangedArray.add(1,hq+"hasImpactOfSymptomsValue");
thePropertyBeingChangedArray.add(2,hq+"hasSupervisionValue");
thePropertyBeingChangedArray.add(3,hq+"hasFeedingValue");
thePropertyBeingChangedArray.add(4,hq+"hasHygieneToiletingValue");
thePropertyBeingChangedArray.add(5,hq+"hasMobilityValue");
thePropertyBeingChangedArray.add(6,ns+"hasAge");
inf.inferenceResults((com.hp.hpl.jena.ontology.OntModel)
model1,patientsName);
StmtIterator iter = model1.listStatements();
while (iter.hasNext())
{
Statement stmt = iter.nextStatement();
subject = stmt.getSubject();
predicate = stmt.getPredicate();
//put statements belonging to the properties for this patient in
an array for deletion
for (int i = 0; i < thePropertyBeingChangedArray.size(); i++) {
if(subject.toString().equals(ns+patientsName)&&
predicate.toString().equals(thePropertyBeingChangedArray.get(i))) {
statementsToRemoveArray.add(stmt);
}
}
}
Resource s = ResourceFactory.createResource(ns+patientsName);
for (int i = 0; i < statementsToRemoveArray.size(); i++) {
System.out.println("Statements in array "+i+"=
"+thePropertyBeingChangedArray.get(i)+" The value "+theNewLiteralsArray.get(i));
model1.remove(statementsToRemoveArray.get(i));
Property p =
ResourceFactory.createProperty(thePropertyBeingChangedArray.get(i));
Literal o = model1.createTypedLiteral(theNewLiteralsArray.get(i));
Statement newStatement = ResourceFactory.createStatement(s, p, o );
model1.add(newStatement);
}
}
}
The helper class that does the reasoning.
import java.util.ArrayList;
import org.mindswap.pellet.jena.PelletReasonerFactory;
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntProperty;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
public class Inf {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
static ArrayList<String> nurses=new ArrayList<String>();
static ArrayList<String> nursesAndQualifications=new ArrayList<String>();
static ArrayList<String> alliedHealth=new ArrayList<String>();
static ArrayList<String> hospitalUnit=new ArrayList<String>();
static ArrayList<String> aSuggestion=new ArrayList<String>();
static ArrayList<String> aRisk=new ArrayList<String>();
static int ptTotal;
public void inferenceResults(OntModel model1, String patientsName) {
String hq="http://example.org/";
String ns = "http://philshields.altervista.org/owl/unit.owl#";
System.out.println("Patients name= "+patientsName);
OntModel model =
ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC );
// OntModel ontModel = ModelFactory.createOntologyModel();
model.addSubModel(model1);
Individual ind = model.getIndividual(ns+patientsName);
//model.prepare();
//asserted data properties
OntProperty abcValue = model.getOntProperty(hq+"hasABCValue");
OntProperty impact =
model.getOntProperty(hq+"hasImpactOfSymptomsValue");
OntProperty supervision =
model.getOntProperty(hq+"hasSupervisionValue");
OntProperty feeding = model.getOntProperty(hq+"hasFeedingValue");
OntProperty toilet =
model.getOntProperty(hq+"hasHygieneToiletingValue");
OntProperty mobility = model.getOntProperty(hq+"hasMobilityValue");
OntProperty age = model.getOntProperty(ns+"hasAge");
//inferred data properties
OntProperty total = model.getOntProperty(ns+"hasTotalScore");
OntProperty risk = model.getOntProperty(ns+"hasPossibleRisk");
OntProperty allied = model.getOntProperty(ns+"hasAlliedHealth");
OntProperty nurse = model.getOntProperty(ns+"hasShiftNurse");
OntProperty qualification = model.getOntProperty(ns+"hasQualification");
OntProperty suggestion = model.getOntProperty(ns+"hasSuggestion");
OntProperty unit = model.getOntProperty(ns+"hasPossibleUnit");
//print asserted data properties
System.out.println("Properties for "+ind.getLocalName().toString());
System.out.println( abcValue.getLocalName()+"=
"+(ind.getPropertyValue(abcValue)).asLiteral().getInt());
System.out.println( impact.getLocalName()+"=
"+ind.getPropertyValue(impact).asLiteral().getInt());
System.out.println( supervision.getLocalName()+"=
"+ind.getPropertyValue(supervision).asLiteral().getInt());
System.out.println( feeding.getLocalName()+"=
"+ind.getPropertyValue(feeding).asLiteral().getInt());
System.out.println( toilet.getLocalName()+"=
"+ind.getPropertyValue(toilet).asLiteral().getInt());
System.out.println( mobility.getLocalName()+"=
"+ind.getPropertyValue(mobility).asLiteral().getInt());
System.out.println( age.getLocalName()+"=
"+ind.getPropertyValue(age).asLiteral().getInt());
//print inferred data properties
//System.out.println( total.getLocalName()+"=
"+ind.getPropertyValue(total).asLiteral().getInt());
//ptTotal= ind.getPropertyValue(total).isLiteral().getInt();
aRisk.clear();
for (StmtIterator j = ind.listProperties(risk); j.hasNext(); ) {
Statement s = j.next();
//System.out.println( " " + s.getPredicate().getLocalName() + "
-> " );
System.out.println( "A possible risk... " +
s.getLiteral().getLexicalForm());
aRisk.add("A possible risk... " + s.getLiteral().getLexicalForm());
}
//an example of getting a data property
aSuggestion.clear();
for (StmtIterator j = ind.listProperties(suggestion); j.hasNext();
) {
Statement s = j.next();
//System.out.println( " " + s.getPredicate().getLocalName()
+ " -> " );
System.out.println( "A possible suggestion... " +
s.getLiteral().getLexicalForm());
aSuggestion.add("A possible suggestion... " +
s.getLiteral().getLexicalForm());
}
hospitalUnit.clear();
for (StmtIterator j = ind.listProperties(unit); j.hasNext(); ) {
Statement s = j.next();
//System.out.println( " " + s.getPredicate().getLocalName()
+ " -> " );
System.out.println( "A possible unit... " +
s.getLiteral().getLexicalForm());
hospitalUnit.add("A possible unit... " +
s.getLiteral().getLexicalForm());
}
// an example of getting a class's individual
alliedHealth.clear();
for (StmtIterator j = ind.listProperties(allied); j.hasNext(); ) {
Statement s = j.next();
//System.out.println( " " + s.getPredicate().getLocalName()
+ " -> " );
System.out.println( "Possible allied health... " +
s.getLiteral().getLexicalForm());
alliedHealth.add("Possible allied health... " +
s.getLiteral().getLexicalForm());
}
//System.out.println("A possible nurse= "+((Resource)
ind.getPropertyValue(nurse)).getLocalName());
//put multiple possible nurses in array that fit the qualification
criteria
nursesAndQualifications.clear();
for (StmtIterator j = ind.listProperties(nurse); j.hasNext(); ) {
int x=0;
Statement s = j.next();
nurses.add(x,s.getLiteral().getLexicalForm());
//System.out.println( " " + s.getPredicate().getLocalName()
+ " -> " );
//System.out.println( "A possible nurse... " + ((Resource)
s.getObject()).getLocalName());
// System.out.println("a nurse from individual= "+nurses.get(x));
//System.out.println("a nurse from individual= "+nurses.get(x));
//System.out.println("Some nurse
"+model.getIndividual(ns+nurses.get(x)).listProperties(qualification));
Individual ind1 = model.getIndividual(ns+nurses.get(x));
//System.out.println("a nurse from individual= "+ind1);
for (StmtIterator ji = ind1.listProperties(qualification);
ji.hasNext(); ) {
Statement si = ji.next();
//System.out.println( " " +
s.getPredicate().getLocalName() + " -> " );
System.out.println( nurses.get(x)+" has an appropriate
qualification... " + si.getLiteral().getLexicalForm());
nursesAndQualifications.add(nurses.get(x)+" has an
appropriate qualification... " + si.getLiteral().getLexicalForm());
}nurses.clear();
}
}
Many thanks,Phil