I loaded Neo4j with Pizza.owl file using hermit reasoner and Java.
when i pass a simple query,
match (n) where n="name:Pizza" return n;
am getting the following error ,
Don't know how to compare that. Left: Node[1]{name:"owl:Thing"}
(NodeProxy); Right: "name:Pizza" (String)
Is NodeProxy is an datatype ? How can I make both of them to be compared.
Can I do casting while querying ? Any query to change datatype of the
entire graph nodes ? How to check the type of the node ?
P.S Attached my source code - owl2.java
--
You received this message because you are subscribed to the Google Groups
"Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.
package onto2Neo;
import java.io.*;
import java.util.Map;
import org.semanticweb.HermiT.Reasoner;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLClassExpression;
import org.semanticweb.owlapi.model.OWLDataPropertyExpression;
import org.semanticweb.owlapi.model.OWLLiteral;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLObjectPropertyExpression;
//import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.reasoner.NodeSet;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.DynamicRelationshipType;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.factory.*;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.index.UniqueFactory;
//import org.neo4j.index.lucene.LuceneIndexProvider;
public class Owl2{
public static void main(String[] args) throws Exception {
// First, we create an OWLOntologyManager object. The manager will load and save ontologies.
OWLOntologyManager m=OWLManager.createOWLOntologyManager();
File f=new File("/home/bigtapp/Documents/owlfiles/pizza.owl");
OWLOntology o = m.loadOntologyFromOntologyDocument(f);
File storeDir = new File("/home/bigtapp/Documents/neo4j-community-2.3.1/data/graph.db");
// Instantiate HermiT by creating an instance of the Reasoner class in the package org.semanticweb.HermiT.
Reasoner reasoner=new Reasoner(o);
System.out.println(reasoner.isConsistent());
GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabaseBuilder(storeDir).newGraphDatabase();
Transaction tx = graphDb.beginTx();
try
{
//Creating parent node
Node thingNode = getOrCreateNodeWithUniqueFactory("owl:Thing",graphDb);
//Extracting Class nodes
for (OWLClass c :o.getClassesInSignature(true))
{
String classString = c.toString();
if (classString.contains("#"))
{
classString = classString.substring(
classString.indexOf("#")+1,classString.lastIndexOf(">"));
}
Node classNode = getOrCreateNodeWithUniqueFactory(classString,graphDb);
System.out.println(classNode.getAllProperties());
NodeSet<OWLClass> superclasses = reasoner.getSuperClasses(c, true);
if (superclasses.isEmpty())
{
classNode.createRelationshipTo(thingNode,DynamicRelationshipType.withName("isA"));
}
else
{
for (org.semanticweb.owlapi.reasoner.Node<OWLClass>
parentOWLNode: superclasses)
{
OWLClassExpression parent = parentOWLNode.getRepresentativeElement();
String parentString = parent.toString();
if (parentString.contains("#"))
{
parentString = parentString.substring(
parentString.indexOf("#")+1,
parentString.lastIndexOf(">"));
}
Node parentNode =
getOrCreateNodeWithUniqueFactory(parentString,graphDb);
classNode.createRelationshipTo(parentNode,DynamicRelationshipType.withName("isA"));
}
}
//Individuals
for (org.semanticweb.owlapi.reasoner.Node<OWLNamedIndividual> in: reasoner.getInstances(c, true))
{
OWLNamedIndividual i = in.getRepresentativeElement();
String indString = i.toString();
if (indString.contains("#")) {
indString = indString.substring(
indString.indexOf("#")+1,indString.lastIndexOf(">"));
}
Node individualNode = getOrCreateNodeWithUniqueFactory(indString,graphDb);
individualNode.createRelationshipTo(classNode, DynamicRelationshipType.withName("isA"));
//object property
for (OWLObjectPropertyExpression objectProperty:o.getObjectPropertiesInSignature())
{
for
(org.semanticweb.owlapi.reasoner.Node<OWLNamedIndividual>
object: reasoner.getObjectPropertyValues(i,
objectProperty)) {
String reltype = objectProperty.toString();
reltype = reltype.substring(reltype.indexOf("#")+1,
reltype.lastIndexOf(">"));
String s =
object.getRepresentativeElement().toString();
s = s.substring(s.indexOf("#")+1,
s.lastIndexOf(">"));
Node objectNode = getOrCreateNodeWithUniqueFactory(s,graphDb);
individualNode.createRelationshipTo(objectNode,
DynamicRelationshipType.withName(reltype));
}
}
//data property
for (OWLDataPropertyExpression dataProperty:o.getDataPropertiesInSignature())
{
for (OWLLiteral object: reasoner.getDataPropertyValues(i, dataProperty.asOWLDataProperty()))
{
String reltype =
dataProperty.asOWLDataProperty().toString();
reltype = reltype.substring(reltype.indexOf("#")+1,
reltype.lastIndexOf(">"));
String s = object.toString();
individualNode.setProperty(reltype, s);
}
}
}
tx.success();
}
}
finally
{
tx.close();
graphDb.shutdown();
}
}
private static Node getOrCreateNodeWithUniqueFactory(String nodeName,GraphDatabaseService graphDb)
{
UniqueFactory<Node> factory = new UniqueFactory.UniqueNodeFactory(graphDb, "index")
{
@Override
protected void initialize(Node created,Map<String, Object> properties) {
created.setProperty("name", properties.get("name"));
}
};
return factory.getOrCreate("name", nodeName);
}
}