Wednesday, October 3, 2012

Neo4j + Sesame + OwlApi + Pellet


 To create inference in pellet and export this inferece to Neo4j with sail connector

function OpenSail 

        Map<String, String> config = new HashMap<String, String>();
        config.put("allow_store_upgrade", "true");
         graph = new MyNeo4jGraph("data/graph.db", 100000, config);
        graph.setCheckElementsInTransaction(true);
        sail = new GraphSail<Neo4jGraph>(graph);

        // sail.
        sail.initialize();

        SailRepository repository = new SailRepository(sail);
        connection = repository.getConnection();
        connection.setAutoCommit(true);

function import ontology
  
     connection.add(getClass().getClassLoader().getResource("Prueba.owl"),
                "etiqueta", RDFFormat.RDFXML);       
        System.out.println("Import stopped ...");
        connection.commit();

function Sparql

TupleQuery durationquery = connection
                .prepareTupleQuery(
                        QueryLanguage.SPARQL,
                        "PREFIX : <http://www.semanticweb.org/ontologies/2012/8/Prueba.owl#> "
                                + "PREFIX Prueba: <http://www.semanticweb.org/ontologies/2012/8/Prueba.owl#> "
                                + "PREFIX owl: <http://www.w3.org/2002/07/owl#>  "
                                + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
                                + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>  "
                                + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>  "
                                + "SELECT ?person " + "WHERE { " +
                                 " Prueba:Jean  Prueba:hasSibling ?person  . "+

                                "}");
        TupleQueryResult result = durationquery.evaluate();
        while (result.hasNext()) {
            BindingSet binding = result.next();
            System.out.println(binding.getBinding("person").getValue());
            Assert.assertEquals("Assert ", binding.getBinding("person").getValue().toString(),
            "http://www.semanticweb.org/ontologies/2012/8/Prueba.owl#Gemma");
        }



reasoner


         //prepare ontology and reasoner
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        //OWLOntology ontology = manager.loadOntologyFromOntologyDocument(IRI.create(BASE_URL));
        OWLOntology ontology = manager.loadOntologyFromOntologyDocument(
                IRI.create(getClass().getClassLoader().getResource("Prueba.owl")));
        System.out.println(" Load ontology" );

        OWLReasonerFactory reasonerFactory = PelletReasonerFactory.getInstance();
        System.out.println(" Load reasoner" );
        OWLReasoner reasoner = reasonerFactory.createReasoner(ontology, new SimpleConfiguration());

        OWLDataFactory factory = manager.getOWLDataFactory();
        PrefixOWLOntologyFormat pm = manager.getOntologyFormat(ontology).asPrefixOWLOntologyFormat();

        pm.setDefaultPrefix(BASE_URL + "#");
        //get class and its individuals
        OWLClass personClass = factory.getOWLClass(":Person", pm);
        pm.setDefaultPrefix("http://www.semanticweb.org/ontologies/2012/8/Prueba.owl#");
       
        OWLObjectProperty hasSibling = factory.getOWLObjectProperty(":hasSibling", pm);

//export pellet inference to sesame sail neo4j



 for (OWLNamedIndividual person : reasoner.getInstances(personClass, false).getFlattened()) {
            System.out.println("person : " + renderer.render(person));
           
            for (Node<OWLNamedIndividual> siblings : reasoner.getObjectPropertyValues(person, hasSibling)) {
                for (OWLNamedIndividual sibling : siblings.getEntities()) {
                    System.out.println("Person has sibling: " + sibling.getIRI());
                   
                    Map<OWLObjectPropertyExpression, Set<OWLIndividual>> assertedValues =  person.getObjectPropertyValues(ontology);
                    for (OWLObjectProperty objProp : ontology.getObjectPropertiesInSignature(true)) {
                        for (OWLNamedIndividual ind : reasoner.getObjectPropertyValues(person, objProp).getFlattened()) {
                            if (assertedValues.get(objProp)==null ) {//inference
                                System.out.println("inferred object property for :"
                                        + renderer.render(objProp) + " -> " + renderer.render(ind));

                           
                                ValueFactory mValueFactory = connection.getValueFactory();
                                org.openrdf.model.URI predicate = mValueFactory.createURI(hasSibling.getIRI().toURI().toString());
                                Resource subject =  mValueFactory.createURI(person.getIRI().toURI().toString()); // mValueFactory.createURI(subj.getIRI().toURI().toString());
                                Resource resource =  mValueFactory.createBNode();
                                Value object = null;
                               
                                object =  mValueFactory.createURI(ind.getIRI().toURI().toString());
                               
                                try {
                                    System.out.println("Triplet --"+subject+"--"+predicate+"--"+object+"--"+resource+"--");
                                    connection.add(subject, predicate, object,resource);
                                } catch (RepositoryException e) {
                                    System.out.println(e);
                                }
                                                               
                            }else{//asserted
                                boolean asserted = assertedValues.get(objProp).contains(ind);
                                System.out.println((asserted ? "asserted" : "inferred") + " object property for Martin: "
                                    + renderer.render(objProp) + " -> " + renderer.render(ind));
                            }
                        }
                    }
                }
            }
        }
        Assert.assertTrue(reasoner.isConsistent());

No comments:

Post a Comment