Create Individuals using Jena - java

I created an owl file using Jena:
<rdf:RDF xmlns="file:/D:/onto/owl_ontologies/diagnostic.owl#"
xml:base="file:/D:/onto/owl_ontologies/diagnostic.owl"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:diag="file:/D:/onto/owl_ontologies/diagnostic.owl#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<owl:Ontology rdf:about="file:/D:/onto/owl_ontologies/diagnostic.owl"/>
<owl:ObjectProperty rdf:about="&diag;hasSymptom">
<rdfs:domain rdf:resource="&diag;Desease"/>
<rdfs:range rdf:resource="&diag;Symptom"/>
</owl:ObjectProperty>
<owl:DatatypeProperty rdf:about="&diag;DesId">
<rdfs:domain rdf:resource="&diag;Desease"/>
<rdfs:range rdf:resource="&xsd;string"/>
</owl:DatatypeProperty>
<owl:DatatypeProperty rdf:about="&diag;DesLabel">
<rdfs:domain rdf:resource="&diag;Desease"/>
<rdfs:range rdf:resource="&xsd;string"/>
</owl:DatatypeProperty>
<owl:DatatypeProperty rdf:about="&diag;SympId">
<rdfs:domain rdf:resource="&diag;Symptom"/>
<rdfs:range rdf:resource="&xsd;string"/>
</owl:DatatypeProperty>
<owl:DatatypeProperty rdf:about="&diag;SympLabel">
<rdfs:domain rdf:resource="&diag;Symptom"/>
<rdfs:range rdf:resource="&xsd;string"/>
</owl:DatatypeProperty>
<owl:Class rdf:about="&diag;Desease"/>
<owl:Class rdf:about="&diag;Symptom"/>
<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Individuals
//
///////////////////////////////////////////////////////////////////////////////////////
-->
I want to create individuals, this is my java code to create the ontModel:
//******************************Create new Ontology*******************************************
ontoDiag = modelDiag.createOntology(baseURI);
modelDiag.setNsPrefix("diag", ns);
//*****************Create Classes Desease && Symptoms ****************************************
desease = modelDiag.createClass(ns + "Desease");
symptom = modelDiag.createClass(ns + "Symptom");
//*********************Create DataType Property label && id for symptom class***********************
name = modelDiag.createDatatypeProperty(ns + "SympLabel");
name.setDomain(symptom);
name.setRange(XSD.xstring);
id = modelDiag.createDatatypeProperty(ns + "SympId");
id.setDomain(symptom);
id.setRange(XSD.xstring);
//*********************Create DataType Property label && id for desease class***********************
nameDes = modelDiag.createDatatypeProperty(ns + "DesLabel");
nameDes.setDomain(desease);
nameDes.setRange(XSD.xstring);
idDes = modelDiag.createDatatypeProperty(ns + "DesId");
idDes.setDomain(desease);
idDes.setRange(XSD.xstring);
//*********************Create Object Property hasSymptom *******************************************
hasSymptom = modelDiag.createObjectProperty(ns + "hasSymptom");
hasSymptom.addDomain(desease);
hasSymptom.addRange(symptom);
and this is the part where I create individuals
//***********************************Create Individual Desease****************************
Individual IndivDes = modelDiag.createIndividual(ns + desChoosen, desease);
//***************add the property name to desease **************************************
Literal desName = modelDiag.createTypedLiteral(desChoosen, XSDDatatype.XSDstring);
Statement desNameSt = modelDiag.createStatement(IndivDes, name, desName);
modelDiag.add(desNameSt);
//***************add the property id to desease **************************************
Literal desId = modelDiag.createTypedLiteral(IdDes, XSDDatatype.XSDstring);
Statement desIdSt = modelDiag.createStatement(IndivDes, id, desId);
modelDiag.add(desIdSt);
The code work perfectly, but the problem is the individuals created look like this:
<diag:Desease rdf:about="file:/D:/onto/owl_ontologies/diagnostic.owl#orbital cyst">
<diag:hasSymptom>
<diag:Symptom rdf:about="file:/D:/onto/owl_ontologies/diagnostic.owl#severe chest pain">
<diag:SympLabel>severe chest pain</diag:SympLabel>
</diag:Symptom>
</diag:hasSymptom>
<diag:SympId>DES:000001</diag:SympId>
<diag:SympLabel>orbital cyst</diag:SympLabel>
instead:
<owl:NamedIndividual rdf:about="&diag;desease2">
<rdf:type rdf:resource="&diag;Desease"/>
<hasSymptom rdf:resource="&diag;pain2"/>
</owl:NamedIndividual>
I appreciate your help thank you..

Try to add explicitly rdf:type=owl:NamedIndividual:
public static void main(String ... strings) {
OntModel m = ModelFactory.createOntologyModel();
m.setNsPrefix("test", "http://test#");
Individual i = m.createIndividual("http://test#indi", m.createResource("http://test#cl"));
i.addProperty(RDFS.comment, "something");
i.addRDFType(OWL2.NamedIndividual);
m.write(System.out);
}
This snippet will produce following rdf-xml:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:test="http://test#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#">
<owl:NamedIndividual rdf:about="http://test#indi">
<rdfs:comment>something</rdfs:comment>
<rdf:type rdf:resource="http://test#cl"/>
</owl:NamedIndividual>
</rdf:RDF>
while without "i.addRDFType(OWL.NamedIndividual)" the output would be following:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:test="http://test#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#">
<test:cl rdf:about="http://test#indi">
<rdfs:comment>something</rdfs:comment>
</test:cl>
</rdf:RDF>
Note: owl:NamedIndividual is OWL2 declaration. there is no such thing in OWL1. jena does not support OWL2, although there is a vocabulary class for it (see org.apache.jena.vocabulary.OWL2)

Related

OWLAPI : "ParserException" while converting String to Class Expression using ManchesterOWLSyntaxParser

I want to add new axiom into ontology, for that I created method which converts String [which is in Manchester OWL Syntax] into OWLClassExpression using ManchesterOWLSyntaxParser and later form new OWLAxiom and add to Ontology.
But I am getting following Exception (org.semanticweb.owlapi.manchestersyntax.renderer.ParserException) :-
Exception in thread "main" org.semanticweb.owlapi.manchestersyntax.renderer.ParserException: Encountered owl:real at line 1 column 12. Expected one of:
Datatype name
not
{
at org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntaxParserImpl$ExceptionBuilder.build(ManchesterOWLSyntaxParserImpl.java:2441)
at org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntaxParserImpl.parseDataRangePrimary(ManchesterOWLSyntaxParserImpl.java:813)
at org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntaxParserImpl.parseDataUnionOf(ManchesterOWLSyntaxParserImpl.java:756)
at org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntaxParserImpl.parseDataIntersectionOf(ManchesterOWLSyntaxParserImpl.java:737)
at org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntaxParserImpl.parseDataRange(ManchesterOWLSyntaxParserImpl.java:729)
at org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntaxParserImpl.parseDataRestriction(ManchesterOWLSyntaxParserImpl.java:695)
at org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntaxParserImpl.parseNonNaryClassExpression(ManchesterOWLSyntaxParserImpl.java:584)
at org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntaxParserImpl.parseIntersection(ManchesterOWLSyntaxParserImpl.java:488)
at org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntaxParserImpl.parseUnion(ManchesterOWLSyntaxParserImpl.java:511)
at org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntaxParserImpl.parseClassExpression(ManchesterOWLSyntaxParserImpl.java:470)
at OWLAPI.convertStringToClassExpression(OWLAPI.java:29)
Following is my Ontology :-
<?xml version="1.0"?>
<!DOCTYPE rdf:RDF [
<!ENTITY owl "http://www.w3.org/2002/07/owl#" >
<!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
<!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
<!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
]>
<rdf:RDF xmlns="http://www.semanticweb.org/empty#"
xml:base="http://www.semanticweb.org/empty"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<owl:Ontology rdf:about="http://www.semanticweb.org/empty"/>
<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Data properties
//
///////////////////////////////////////////////////////////////////////////////////////
-->
<!-- http://www.semanticweb.org/empty#name -->
<owl:DatatypeProperty rdf:about="http://www.semanticweb.org/empty#name"/>
<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Classes
//
///////////////////////////////////////////////////////////////////////////////////////
-->
<!-- http://www.semanticweb.org/empty#A -->
<owl:Class rdf:about="http://www.semanticweb.org/empty#A"/>
</rdf:RDF>
<!-- Generated by the OWL API (version 3.5.1) http://owlapi.sourceforge.net -->
My JAVA code is :-
// some code above to form OWLManager and Ontology
System.out.println(convertStringToClassExpression("name max 1 owl:real"));
private OWLClassExpression convertStringToClassExpression(String expression) {
ManchesterOWLSyntaxParser parser = OWLManager.createManchesterParser();
parser.setStringToParse(expression);
parser.setDefaultOntology(owlOntology); // my ontology
ShortFormEntityChecker checker = new ShortFormEntityChecker(getShortFormProvider());
parser.setOWLEntityChecker(checker);
return parser.parseClassExpression();
}
private BidirectionalShortFormProvider getShortFormProvider() {
Set<OWLOntology> ontologies = owlManager.getOntologies(); // my OWLOntologyManager
ShortFormProvider sfp = new ManchesterOWLSyntaxPrefixNameShortFormProvider(
owlManager.getOntologyFormat(owlOntology));
BidirectionalShortFormProvider shortFormProvider = new BidirectionalShortFormProviderAdapter(
ontologies, sfp);
return shortFormProvider;
}
But if I change my String from name max 1 owl:real to name max 1 xsd:string then there is no exception thrown from code. What's the problem ? How to avoid it ?
This is an OWLAPI bug, fixed in the current trunk versions. The fix will be released in 4.2.6 and 5.0.3, once they are completed and published on Maven Central.

How to seperate one RDF model into two models in Jena?

Now I have a RDF data, which contain two resources(I don't know whether it is correct to call the staff in rdf:description a resource), Now I want to separate the two resource into two rdf data in Jena, I do not know how to use the API to do it, The data example:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:obs="http://localhost/SensorSchema/ontology#" >
<rdf:Description rdf:about="http://localhost/SensorSchema/ontology#Observation_51709293_1_104519dd-63dc-4560-9286-8d621ce153c5">
<obs:hasLatitude rdf:datatype="http://www.w3.org/2001/XMLSchema#double">65.00999166666666</obs:hasLatitude>
<obs:hasDate rdf:datatype="http://www.w3.org/2001/XMLSchema#long">1365156000000</obs:hasDate>
<obs:hasDirection rdf:datatype="http://www.w3.org/2001/XMLSchema#int">212</obs:hasDirection>
<obs:hasVelocity rdf:datatype="http://www.w3.org/2001/XMLSchema#double">28.0</obs:hasVelocity>
<obs:hasAcceleration rdf:datatype="http://www.w3.org/2001/XMLSchema#double">0.0</obs:hasAcceleration>
<obs:hasLongitude rdf:datatype="http://www.w3.org/2001/XMLSchema#double">25.46780833333333</obs:hasLongitude>
<obs:hasArea rdf:datatype="http://www.w3.org/2001/XMLSchema#int">38</obs:hasArea>
<obs:hasDateTime>2013-04-05T13:00:00</obs:hasDateTime>
<obs:hasSender rdf:datatype="http://www.w3.org/2001/XMLSchema#int">51709293</obs:hasSender>
<rdf:type rdf:resource="http://localhost/SensorSchema/ontology#Observation"/>
<obs:hasID rdf:datatype="http://www.w3.org/2001/XMLSchema#int">1</obs:hasID>
<obs:hasDistance rdf:datatype="http://www.w3.org/2001/XMLSchema#double">0.0</obs:hasDistance>
</rdf:Description>
<rdf:Description rdf:about="http://localhost/SensorSchema/ontology#Observation_51709293_1_104519dd-63dc-4560-9286-8d621ce16666">
<obs:hasLatitude rdf:datatype="http://www.w3.org/2001/XMLSchema#double">65.00999166666666</obs:hasLatitude>
<obs:hasDate rdf:datatype="http://www.w3.org/2001/XMLSchema#long">1365156000000</obs:hasDate>
<obs:hasDirection rdf:datatype="http://www.w3.org/2001/XMLSchema#int">500</obs:hasDirection>
<obs:hasVelocity rdf:datatype="http://www.w3.org/2001/XMLSchema#double">28.0</obs:hasVelocity>
<obs:hasAcceleration rdf:datatype="http://www.w3.org/2001/XMLSchema#double">0.0</obs:hasAcceleration>
<obs:hasLongitude rdf:datatype="http://www.w3.org/2001/XMLSchema#double">25.46780833333333</obs:hasLongitude>
<obs:hasArea rdf:datatype="http://www.w3.org/2001/XMLSchema#int">38</obs:hasArea>
<obs:hasDateTime>2013-04-05T13:00:00</obs:hasDateTime>
<obs:hasSender rdf:datatype="http://www.w3.org/2001/XMLSchema#int">51709293</obs:hasSender>
<rdf:type rdf:resource="http://localhost/SensorSchema/ontology#Observation"/>
<obs:hasID rdf:datatype="http://www.w3.org/2001/XMLSchema#int">1</obs:hasID>
<obs:hasDistance rdf:datatype="http://www.w3.org/2001/XMLSchema#double">0.0</obs:hasDistance>
</rdf:Description>
</rdf:RDF>
I try to work like that:
ResIterator iter= OriginalModel.listSubjects();
int i=0;
while(iter.hasNext()) {
Resource subject = iter.next();
Model[i].?? // add the whole resource
i++;
}
but I don't know how to quickly add the resource to another model.
Once you have the subject, you can use listProperties to get a StmtIterator over the triples with that subject, and then you can use Model#add(StmtIterator) to add all those triples to a new Model.
public void splitModels() throws IOException {
// First, create a model and read the content
// into it. You probably already have this part,
// but we need it for a working example.
Model model = ModelFactory.createDefaultModel();
try (InputStream in = SplitModelExample.class.getResourceAsStream("/example.rdf")) {
RDFDataMgr.read(model, in, Lang.RDFXML);
}
// List the subjects in the model.
ResIterator subjects = model.listSubjects();
// For each subject, create another empty model that will
// contain the triples of which the subject is the subject.
// The #listProperties() method returns a StmtIterator over
// those triples, and Model#add(StmtIterator) adds all the
// triples to a model. Then we'll print out each submodel
// to make sure we're getting what we expect.
while (subjects.hasNext()) {
Resource subject = subjects.next();
Model subModel = ModelFactory.createDefaultModel();
subModel.add(subject.listProperties());
System.out.println("\n<!-- Submodel for "+subject+". -->");
RDFDataMgr.write(System.out, subModel, Lang.RDFXML);
}
}
<!-- Submodel for http://localhost/SensorSchema/ontology#Observation_51709293_1_104519dd-63dc-4560-9286-8d621ce16666. -->
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:j.0="http://localhost/SensorSchema/ontology#">
<j.0:Observation rdf:about="http://localhost/SensorSchema/ontology#Observation_51709293_1_104519dd-63dc-4560-9286-8d621ce16666">
<j.0:hasLongitude rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
>25.46780833333333</j.0:hasLongitude>
<j.0:hasLatitude rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
>65.00999166666666</j.0:hasLatitude>
<j.0:hasVelocity rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
>28.0</j.0:hasVelocity>
<j.0:hasID rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
>1</j.0:hasID>
<j.0:hasDateTime>2013-04-05T13:00:00</j.0:hasDateTime>
<j.0:hasDate rdf:datatype="http://www.w3.org/2001/XMLSchema#long"
>1365156000000</j.0:hasDate>
<j.0:hasDistance rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
>0.0</j.0:hasDistance>
<j.0:hasSender rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
>51709293</j.0:hasSender>
<j.0:hasAcceleration rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
>0.0</j.0:hasAcceleration>
<j.0:hasArea rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
>38</j.0:hasArea>
<j.0:hasDirection rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
>500</j.0:hasDirection>
</j.0:Observation>
</rdf:RDF>
<!-- Submodel for http://localhost/SensorSchema/ontology#Observation_51709293_1_104519dd-63dc-4560-9286-8d621ce153c5. -->
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:j.0="http://localhost/SensorSchema/ontology#">
<j.0:Observation rdf:about="http://localhost/SensorSchema/ontology#Observation_51709293_1_104519dd-63dc-4560-9286-8d621ce153c5">
<j.0:hasLatitude rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
>65.00999166666666</j.0:hasLatitude>
<j.0:hasDate rdf:datatype="http://www.w3.org/2001/XMLSchema#long"
>1365156000000</j.0:hasDate>
<j.0:hasDirection rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
>212</j.0:hasDirection>
<j.0:hasVelocity rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
>28.0</j.0:hasVelocity>
<j.0:hasAcceleration rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
>0.0</j.0:hasAcceleration>
<j.0:hasLongitude rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
>25.46780833333333</j.0:hasLongitude>
<j.0:hasArea rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
>38</j.0:hasArea>
<j.0:hasDateTime>2013-04-05T13:00:00</j.0:hasDateTime>
<j.0:hasSender rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
>51709293</j.0:hasSender>
<j.0:hasID rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
>1</j.0:hasID>
<j.0:hasDistance rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
>0.0</j.0:hasDistance>
</j.0:Observation>
</rdf:RDF>

Reading RDF data using Jena failing

The following code is to read a schema and a data file to find rdf.type of colin and Person. However, I am getting the error:
Exception in thread "main" org.apache.jena.riot.RiotException: [line: 1, col: 1 ] Content is not allowed in prolog. The code is given below:
public void reason(){
String NS = "urn:x-hp:eg/";
String fnameschema = "D://Work//EclipseWorkspace//Jena//data//rdfsDemoSchema.rdf";
String fnameinstance = "D://Work//EclipseWorkspace//Jena//data//rdfsDemoData.rdf";
Model schema = FileManager.get().loadModel(fnameschema);
Model data = FileManager.get().loadModel(fnameinstance);
InfModel infmodel = ModelFactory.createRDFSModel(schema, data);
Resource colin = infmodel.getResource(NS+"colin");
System.out.println("Colin has types");
for (StmtIterator i = infmodel.listStatements(colin, RDF.type, (RDFNode)null); i.hasNext(); ) {
Statement s = i.nextStatement();
System.out.println(s);
}
Resource Person = infmodel.getResource(NS+"Person");
System.out.println("\nPerson has types:");
for (StmtIterator i = infmodel.listStatements(Person, RDF.type, (RDFNode)null); i.hasNext(); ) {
Statement s = i.nextStatement();
System.out.println(s);
}
}
The file rdfsDemoData.rdf
#prefix eg: <urn:x-hp:eg/> .
<Teenager rdf:about="&eg;colin">
<mum rdf:resource="&eg;rosy" />
<age>13</age>
</Teenager>
The file rdfsDemoSchema.rdf
#prefix eg: <urn:x-hp:eg/> .
<rdf:Description rdf:about="&eg;mum">
<rdfs:subPropertyOf rdf:resource="&eg;parent"/>
</rdf:Description>
<rdf:Description rdf:about="&eg;parent">
<rdfs:range rdf:resource="&eg;Person"/>
<rdfs:domain rdf:resource="&eg;Person"/>
</rdf:Description>
<rdf:Description rdf:about="&eg;age">
<rdfs:range rdf:resource="&xsd;integer" />
</rdf:Description>
Your data is bad syntax. You are mixing Turtle and RDF/XML. RDF/XML does nto have #prefix - it uses XML's namespaces. It looks like you want an XML entity declaration like:
<?xml version="1.0"?>
<!DOCTYPE rdf:RDF [
<!ENTITY eg "urn:x-hp:eg/" >
]>
...

Writing a Jena rule to add properties to a resource with some specific property values?

I'm trying to write a Jena rule whose antecedent matches a location where the network's bandwith is 450^^xsd:float. I've tried the following two rules. The first gives me no results. The second matches for the networks labeled Gate 23 and Gate 15, but only Gate 15 should match.
[AdaptedModel:
(?d2 rdf:type perSys:NetworkCharacteristics),
(?d2 perSys:Bandwidth '450'^^xsd:float)
(?d2 perSys:SpecificLocation ?a)
->
(?d2 perSys:AdaptedSpecificLocation ?a)]
[AdaptedModel:
(?d2 rdf:type perSys:NetworkCharacteristics),
(?d2 perSys:Bandwidth '450'^^xsd:float)
(?d3 perSys:SpecificLocation ?a)
->
(?d3 perSys:AdaptedSpecificLocation ?a)]
This is my RDF data:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:perSys="http://localhost:8080/NetworkContextWS/onto/NetworkContextDescription#">
<perSys:PervasiveContext>
<perSys:HasNetworkContext>
<perSys:NetworkContext rdf:about="file:///home/taylorj/n1111">
<perSys:Rules>
<perSys:NetworkSecurity>
<perSys:NetworkSecurityState rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>Open Network</perSys:NetworkSecurityState>
<perSys:NetworkKey rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>No Key</perSys:NetworkKey>
</perSys:NetworkSecurity>
</perSys:Rules>
<perSys:Network>
<perSys:NetworkCharacteristics>
<perSys:SubNetworkType rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>HSDPA</perSys:SubNetworkType>
<perSys:NetworkState rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>CONNECTED</perSys:NetworkState>
<perSys:Bandwidth rdf:datatype="http://www.w3.org/2001/XMLSchema#float"
>450</perSys:Bandwidth>
<perSys:LinkSpeed rdf:datatype="http://www.w3.org/2001/XMLSchema#float"
>65</perSys:LinkSpeed>
<perSys:NetworkAvailability rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>Available</perSys:NetworkAvailability>
<perSys:NetworkName rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>AF23_WI-FI_9F1B</perSys:NetworkName>
</perSys:NetworkCharacteristics>
</perSys:Network>
<perSys:Network>
<perSys:NetworkTraficStats>
<perSys:NumberBytesReceived rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>15599522</perSys:NumberBytesReceived>
<perSys:NumberPacketsReceived rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
>24922</perSys:NumberPacketsReceived>
<perSys:NumberBytesTransmited rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
>4111415</perSys:NumberBytesTransmited>
<perSys:NumberPacketsTransmited rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
>26455</perSys:NumberPacketsTransmited>
<perSys:NetworkUpload rdf:datatype="http://www.w3.org/2001/XMLSchema#float"
>0</perSys:NetworkUpload>
<perSys:NetworkType rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>Connected</perSys:NetworkType>
<perSys:NetworkDownload rdf:datatype="http://www.w3.org/2001/XMLSchema#float"
>1</perSys:NetworkDownload>
<perSys:TotalData rdf:datatype="http://www.w3.org/2001/XMLSchema#float"
>1</perSys:TotalData>
</perSys:NetworkTraficStats>
</perSys:Network>
<perSys:Time>
<perSys:TimeCharacteristics>
<perSys:ConnexionTime rdf:datatype="http://www.w3.org/2001/XMLSchema#time"
>15:00:00</perSys:ConnexionTime>
<perSys:ConnexionDate rdf:datatype="http://www.w3.org/2001/XMLSchema#date"
>2015-02-06</perSys:ConnexionDate>
</perSys:TimeCharacteristics>
</perSys:Time>
<perSys:Preferences>
<perSys:PreferencesCharacteristics>
<perSys:PreferencesName rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>preferencesName</perSys:PreferencesName>
<perSys:PreferencesType rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>preferencesValue</perSys:PreferencesType>
</perSys:PreferencesCharacteristics>
</perSys:Preferences>
<perSys:Device>
<perSys:DeviceCharacteristics>
<perSys:DeviceName rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>Bekri-Laptop</perSys:DeviceName>
<perSys:DeviceType rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>Laptop</perSys:DeviceType>
</perSys:DeviceCharacteristics>
</perSys:Device>
<perSys:Location>
<perSys:LocationCharacteristics>
<perSys:SpecificLocation rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>Gate 23</perSys:SpecificLocation>
<perSys:FeatureName rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>Pittsburgh International Airport</perSys:FeatureName>
</perSys:LocationCharacteristics>
</perSys:Location>
</perSys:NetworkContext>
</perSys:HasNetworkContext>
<perSys:HasNetworkContext>
<perSys:NetworkContext rdf:about="file:///home/taylorj/lm333">
<perSys:Rules>
<perSys:NetworkSecurity>
<perSys:NetworkSecurityState rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>Open Network</perSys:NetworkSecurityState>
<perSys:NetworkKey rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>No Key</perSys:NetworkKey>
</perSys:NetworkSecurity>
</perSys:Rules>
<perSys:Network>
<perSys:NetworkCharacteristics>
<perSys:SubNetworkType rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>HSDPA</perSys:SubNetworkType>
<perSys:NetworkState rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>CONNECTED</perSys:NetworkState>
<perSys:Bandwidth rdf:datatype="http://www.w3.org/2001/XMLSchema#float"
>256</perSys:Bandwidth>
<perSys:LinkSpeed rdf:datatype="http://www.w3.org/2001/XMLSchema#float"
>65</perSys:LinkSpeed>
<perSys:NetworkAvailability rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>Available</perSys:NetworkAvailability>
<perSys:NetworkName rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>AF23_WI-FI_9F1B</perSys:NetworkName>
</perSys:NetworkCharacteristics>
</perSys:Network>
<perSys:Network>
<perSys:NetworkTraficStats>
<perSys:NumberBytesReceived rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>15599522</perSys:NumberBytesReceived>
<perSys:NumberPacketsReceived rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
>24922</perSys:NumberPacketsReceived>
<perSys:NumberBytesTransmited rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
>4111415</perSys:NumberBytesTransmited>
<perSys:NumberPacketsTransmited rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
>26455</perSys:NumberPacketsTransmited>
<perSys:NetworkUpload rdf:datatype="http://www.w3.org/2001/XMLSchema#float"
>0</perSys:NetworkUpload>
<perSys:NetworkType rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>Connected</perSys:NetworkType>
<perSys:NetworkDownload rdf:datatype="http://www.w3.org/2001/XMLSchema#float"
>1</perSys:NetworkDownload>
<perSys:TotalData rdf:datatype="http://www.w3.org/2001/XMLSchema#float"
>1</perSys:TotalData>
</perSys:NetworkTraficStats>
</perSys:Network>
<perSys:Time>
<perSys:TimeCharacteristics>
<perSys:ConnexionTime rdf:datatype="http://www.w3.org/2001/XMLSchema#time"
>15:00:00</perSys:ConnexionTime>
<perSys:ConnexionDate rdf:datatype="http://www.w3.org/2001/XMLSchema#date"
>2015-02-06</perSys:ConnexionDate>
</perSys:TimeCharacteristics>
</perSys:Time>
<perSys:Preferences>
<perSys:PreferencesCharacteristics>
<perSys:PreferencesName rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>preferencesName</perSys:PreferencesName>
<perSys:PreferencesType rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>preferencesValue</perSys:PreferencesType>
</perSys:PreferencesCharacteristics>
</perSys:Preferences>
<perSys:Device>
<perSys:DeviceCharacteristics>
<perSys:DeviceName rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>Bekri-Laptop</perSys:DeviceName>
<perSys:DeviceType rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>Laptop</perSys:DeviceType>
</perSys:DeviceCharacteristics>
</perSys:Device>
<perSys:Location>
<perSys:LocationCharacteristics>
<perSys:SpecificLocation rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>Gate 15</perSys:SpecificLocation>
<perSys:FeatureName rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>Pittsburgh International Airport</perSys:FeatureName>
</perSys:LocationCharacteristics>
</perSys:Location>
</perSys:NetworkContext>
</perSys:HasNetworkContext>
</perSys:PervasiveContext>
</rdf:RDF>
The second rule is wrong
Your second rule is broken because there's no connection between the value of the d2 and d3 variables:
[AdaptedModel:
(?d2 rdf:type perSys:NetworkCharacteristics),
(?d2 perSys:Bandwidth '450'^^xsd:float)
(?d3 perSys:SpecificLocation ?a)
->
(?d3 perSys:AdaptedSpecificLocation ?a)]
This roughly says:
If some X has type NetworkCharacterstics and has bandwidth 450, and some Y has a specific location Z, then Y has an adapted specific location Z.
There's no connection between X (which has the constraint about 450), so everything with a specific location gets connected to an adapted specific location.
Why the first rule doesn't work, and how to fix it
Let's look at the part of your data where 450 appears, and where perSys:SpecificLocations appear. It's easier to read in the N3 serialization:
<.../n1111>
a perSys:NetworkContext ;
perSys:Location [ a perSys:LocationCharacteristics ;
perSys:FeatureName "Pittsburgh International Airport"^^<http://www.w3.org/2001/XMLSchema#string> ;
perSys:SpecificLocation "Gate 23"^^<http://www.w3.org/2001/XMLSchema#string>
] ;
perSys:Network [ a perSys:NetworkCharacteristics ;
perSys:Bandwidth "450"^^<http://www.w3.org/2001/XMLSchema#float> ;
perSys:LinkSpeed "65"^^<http://www.w3.org/2001/XMLSchema#float> ;
perSys:NetworkAvailability "Available"^^<http://www.w3.org/2001/XMLSchema#string> ;
perSys:NetworkName "AF23_WI-FI_9F1B"^^<http://www.w3.org/2001/XMLSchema#string> ;
perSys:NetworkState "CONNECTED"^^<http://www.w3.org/2001/XMLSchema#string> ;
perSys:SubNetworkType "HSDPA"^^<http://www.w3.org/2001/XMLSchema#string>
] ;
The thing that has bandwidth 450 is a blank node and has type perSys:NetworkCharacteristics, so it can match the first two parts of the rule. It doesn't have a perSys:SpecificLocation, though, so you can't match the third part. The thing that has a perSys:SpecificLocation is another blank node.
I don't know where you actually want to put the perSys:AdaptedSpecificLocation triple (i.e., I don't know what the subject is supposed to be), but you'll need to match the shape of your data with something like this:
[(?context perSys:Location ?location)
(?location perSys:SpecificLocation ?specificLocation)
(?context perSys:Network ?network)
(?network perSys:Bandwidth '450'^^xsd:float)
->
(?location perSys:AdaptedSpecificLocation ?specificLocation)]
If you were to draw the graph structure, it would look like the following. The solid lines are the relationships that are in your data, and the dashed lines are the ones that will be inferred by the rule.
See that you have to match the whole structure of the data? There's a context, and it has a location and a network. The location has a specific location, and the network has a bandwidth. When all of those match, you can say that the location has an adapted specific location. This is how you have to "connect" the variables; they need to be related to each other somehow.

Get Property Value Intersection of two objects in an ontology using OWL API

I am new to OWL API and trying to experiment with it. I have created a simple ontology in protege and reciprocal application in eclipse using OWL API. Ontology structure is as follows:-
----------------------------------------------------------------------------------------
Class Object (Data Property)StringValue ObjectProperty relatedTo
----------------------------------------------------------------------------------------
WordString WS1 "One" DS1
WS2 "Two" DS2
WS3 "Three" DS3
DigiString DS1 "1"
DS2 "2"
DS3 "3"
----------------------------------------------------------------------------
Complete ontology is as follows:-
<rdf:RDF xmlns="http://localhost:3030/DigiWord.owl#"
xml:base="http://localhost:3030/DigiWord.owl"
xmlns:DigiWord="http://localhost:3030/DigiWord.owl#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<owl:Ontology rdf:about="http://localhost:3030/DigiWord.owl"/>
<!--
// Object Properties
///////////////////////////////////////////////////////////////////////////////////////
-->
<!-- http://localhost:3030/DigiWord.owl#relatedTo -->
<owl:ObjectProperty rdf:about="&DigiWord;relatedTo">
<rdfs:domain rdf:resource="&DigiWord;DigiString"/>
<rdfs:range rdf:resource="&DigiWord;WordString"/>
</owl:ObjectProperty>
<!--
//
// Data properties
///////////////////////////////////////////////////////////////////////////////////////
-->
<!-- http://localhost:3030/DigiWord.owl#stringValue -->
<owl:DatatypeProperty rdf:about="&DigiWord;stringValue">
<rdfs:domain rdf:resource="&DigiWord;DigiString"/>
<rdfs:domain rdf:resource="&DigiWord;WordString"/>
<rdfs:range rdf:resource="&xsd;string"/>
</owl:DatatypeProperty>
<!--
// Classes
///////////////////////////////////////////////////////////////////////////////////////
-->
<!-- http://localhost:3030/DigiWord.owl#DigiString -->
<owl:Class rdf:about="&DigiWord;DigiString"/>
<!-- http://localhost:3030/DigiWord.owl#WordString -->
<owl:Class rdf:about="&DigiWord;WordString"/>
<!--
// Individuals
///////////////////////////////////////////////////////////////////////////////////////
-->
<!-- http://localhost:3030/DigiWord.owl#DS1 -->
<owl:NamedIndividual rdf:about="&DigiWord;DS1">
<rdf:type rdf:resource="&DigiWord;DigiString"/>
<stringValue rdf:datatype="&xsd;string">1</stringValue>
<relatedTo rdf:resource="&DigiWord;WS1"/>
</owl:NamedIndividual>
<!-- http://localhost:3030/DigiWord.owl#DS2 -->
<owl:NamedIndividual rdf:about="&DigiWord;DS2">
<rdf:type rdf:resource="&DigiWord;DigiString"/>
<stringValue rdf:datatype="&xsd;string">2</stringValue>
<relatedTo rdf:resource="&DigiWord;WS2"/>
</owl:NamedIndividual>
<!-- http://localhost:3030/DigiWord.owl#DS3 -->
<owl:NamedIndividual rdf:about="&DigiWord;DS3">
<rdf:type rdf:resource="&DigiWord;DigiString"/>
<stringValue rdf:datatype="&xsd;string"></stringValue>
<relatedTo rdf:resource="&DigiWord;WS3"/>
</owl:NamedIndividual>
<!-- http://localhost:3030/DigiWord.owl#WS1 -->
<owl:NamedIndividual rdf:about="&DigiWord;WS1">
<rdf:type rdf:resource="&DigiWord;WordString"/>
<stringValue rdf:datatype="&xsd;string">One</stringValue>
</owl:NamedIndividual>
<!-- http://localhost:3030/DigiWord.owl#WS2 -->
<owl:NamedIndividual rdf:about="&DigiWord;WS2">
<rdf:type rdf:resource="&DigiWord;WordString"/>
<stringValue rdf:datatype="&xsd;string">Two</stringValue>
</owl:NamedIndividual>
<!-- http://localhost:3030/DigiWord.owl#WS3 -->
<owl:NamedIndividual rdf:about="&DigiWord;WS3">
<rdf:type rdf:resource="&DigiWord;WordString"/>
<stringValue rdf:datatype="&xsd;string">Three</stringValue>
</owl:NamedIndividual>
</rdf:RDF>
I want to retrieve Object WS1 for DS1, WS2 for DS2 i.e upon providing strings "1", "2" etc my code should retrieve "One" , "Two" respectively. I have not come across any relevant code of this type on net.Any help would be appreciated. Thanks in Advance.
The following example (OWLAPI 4) should help:
OWLOntology o = ... your ontology here
String ns = "http://localhost:3030/DigiWord.owl#";
OWLDataFactory df = o.getOWLOntologyManager().getOWLDataFactory();
OWLObjectProperty relatedto = df.getOWLObjectProperty(IRI.create(ns + "relatedTo"));
OWLDataProperty stringValue = df.getOWLDataProperty(IRI.create(ns + "stringValue"));
SimpleRenderer renderer = new SimpleRenderer();
for (OWLNamedIndividual i : o.getIndividualsInSignature()) {
for (OWLLiteral lit : EntitySearcher.getDataPropertyValues(i, stringValue, o)) {
System.out.println(i + " has values " + renderer.render(lit));
}
for (OWLIndividual related : EntitySearcher.getObjectPropertyValues(i, relatedto, o)) {
for (OWLLiteral lit : EntitySearcher.getDataPropertyValues(related, stringValue, o)) {
System.out.println(i + " related to " + related + " which has values " + renderer.render(lit));
}
}
}
Edit: to find matches for your query:
for (OWLNamedIndividual i : o.getIndividualsInSignature()) {
for (OWLIndividual related : EntitySearcher.getObjectPropertyValues(i, relatedto, o)) {
for (OWLLiteral lit : EntitySearcher.getDataPropertyValues(related, stringValue, o)) {
if(lit.getLexicalForm().equals("1")){
for (OWLLiteral lit : EntitySearcher.getDataPropertyValues(i, stringValue, o)) {
System.out.println(i + " has values " + renderer.render(lit));
}
}
}
}
}

Categories

Resources