Wednesday, July 22, 2009

Java Code Sinppet: OWL Demo with SWRL

In the classpath :
protege.jar
and every jar in the
Protege_3.4\plugins\edu.stanford.smi.protegex.owl
directory
public class OWLDemo {

public static void main(String[] args) {
try {
String uri = "file:///C:/MSC/project/old/travel.owl";
// String uri = "http://localhost:8080/owl/travel.owl";
OWLModel owlModel = ProtegeOWL.createJenaOWLModelFromURI(uri);
OWLNamedClass classs= owlModel.getOWLNamedClass("Trade_Association");

Collection c = classs.getNamedSubclasses();
Iterator itr = c.iterator();

while(itr.hasNext()){
OWLNamedClass cc = (OWLNamedClass) itr.next();
System.out.println(cc.getNestedBrowserText());
System.out.println(cc.getName());
}

SWRLFactory factory = new SWRLFactory(owlModel);
// factory.replaceImps(owlModel); // to run the same query again uncomment
SWRLImp imp = factory.createImp("Query-2", "Use_Inconjunction_With(?x, ?y) -> sqwrl:select(?x, ?y)");

SWRLRuleEngineBridge bridge = BridgeFactory.createBridge("SWRLJessBridge", owlModel);
bridge.infer();
SQWRLResult result = bridge.getSQWRLResult("Query-2");
while (result.hasNext()) {
System.out.println("X: " + result.getObjectValue("?y").getIndividualName());
System.out.println("Y: " + result.getObjectValue("?x").getIndividualName());

result.next();
}

} catch (Exception ex) {

}

}

}


A Useful Guide