Search in sources :

Example 1 with Path

use of org.apache.jena.ontology.OntTools.Path in project legato by DOREMUS-ANR.

the class ModelManager method rewrite.

/**
 ********
 ** Place all Literals (in resources CBD) to a distance = 1
 *********
 */
public static Model rewrite(Model model, boolean ok) throws IOException {
    LEGATO legato = LEGATO.getInstance();
    Model finalModel = ModelFactory.createDefaultModel();
    model.listSubjects().toSet().forEach((resource) -> {
        // Parse all resources
        if (// If the current resource belongs to a given "type"
        legato.hasType(resource) == true) {
            Model m = CBDBuilder.getCBD(model, resource);
            if (ok == true) {
                m.add(CBDBuilder.getCBDDirectPredecessors(model, resource));
                m.add(CBDBuilder.getCBDDirectSuccessors(model, resource));
            }
            try {
                m.add(ModelManager.parseCBD(m));
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            m.listStatements().toSet().forEach((stmt) -> {
                Resource sub = stmt.getSubject();
                Property prop = stmt.getPredicate();
                RDFNode object = stmt.getObject();
                if (// Parse all literals
                object.isLiteral() == true) {
                    // A filter which accepts statements whose predicate matches one of a collection of predicates held by the filter object.
                    Path path = OntTools.findShortestPath(m, resource, object, Filter.any);
                    if (!(path == null)) {
                        // Get the successive properties from the path
                        List<Property> properties = getPropFromPath(path);
                        if (legato.getPropList().existProperty(properties) == false) {
                            int indice = legato.getPropList().size();
                            finalModel.createResource(resource.toString()).addProperty(finalModel.createProperty("http://model.org/property" + indice), object);
                            try {
                                legato.addToPropList("http://model.org/property" + indice, properties);
                            } catch (IOException e) {
                            }
                        } else {
                            finalModel.createResource(resource.toString()).addProperty(finalModel.createProperty(legato.getPropList().getPropertyName(properties)), object);
                        }
                    } else {
                        String sparqlQueryString = "select ?predec where {" + "?predec ?prop <" + resource + ">." + "}";
                        Query query = QueryFactory.create(sparqlQueryString);
                        QueryExecution qexec = QueryExecutionFactory.create(query, model);
                        ResultSet queryResults = qexec.execSelect();
                        while (queryResults.hasNext()) {
                            QuerySolution qs = queryResults.nextSolution();
                            final PathManager.Path path2 = PathManager.findShortestPath(model, qs.getResource("?predec"), object, prop);
                            if (!(path2 == null)) {
                                // Get the successive properties from the path
                                List<Property> properties = getPropFromPath(path2);
                                if (legato.getPropList().existProperty(properties) == false) {
                                    int indice = legato.getPropList().size();
                                    finalModel.createResource(resource.toString()).addProperty(finalModel.createProperty("http://model.org/property" + indice), object);
                                    try {
                                        legato.addToPropList("http://model.org/property" + indice, properties);
                                    } catch (IOException e) {
                                    }
                                } else {
                                    finalModel.createResource(resource.toString()).addProperty(finalModel.createProperty(legato.getPropList().getPropertyName(properties)), object);
                                }
                            }
                        }
                        qexec.close();
                    }
                } else if (prop.equals(RDF.type) && (legato.hasType(sub))) {
                    finalModel.createResource(resource.toString()).addProperty(RDF.type, object);
                }
            // else
            // finalModel.createResource(resource.toString()).addProperty(prop, object);
            });
        }
    });
    return finalModel;
}
Also used : Path(org.apache.jena.ontology.OntTools.Path) Query(org.apache.jena.query.Query) Resource(org.apache.jena.rdf.model.Resource) IOException(java.io.IOException) QueryExecution(org.apache.jena.query.QueryExecution) LEGATO(legato.LEGATO) QuerySolution(org.apache.jena.query.QuerySolution) OntModel(org.apache.jena.ontology.OntModel) Model(org.apache.jena.rdf.model.Model) ResultSet(org.apache.jena.query.ResultSet) Property(org.apache.jena.rdf.model.Property) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 2 with Path

use of org.apache.jena.ontology.OntTools.Path in project legato by DOREMUS-ANR.

the class ModelManager method getID.

/**
 *************************************
 ***Get a value of a given property for a resource
 **************************************
 */
public static String getID(Model model, Resource rsrc, String prop) {
    String id = null;
    StmtIterator iter = model.listStatements();
    while (iter.hasNext()) {
        Statement stmt = iter.nextStatement();
        Property property = stmt.getPredicate();
        if (prop.equals(property)) {
            RDFNode object = stmt.getObject();
            // A filter which accepts statements whose predicate matches one of a collection of predicates held by the filter object.
            Path path = OntTools.findShortestPath(model, rsrc, object, Filter.any);
            if (!(path == null)) {
                id = object.toString();
            }
        }
    }
    return id;
}
Also used : Path(org.apache.jena.ontology.OntTools.Path) StmtIterator(org.apache.jena.rdf.model.StmtIterator) Statement(org.apache.jena.rdf.model.Statement) Property(org.apache.jena.rdf.model.Property) RDFNode(org.apache.jena.rdf.model.RDFNode)

Aggregations

Path (org.apache.jena.ontology.OntTools.Path)2 Property (org.apache.jena.rdf.model.Property)2 RDFNode (org.apache.jena.rdf.model.RDFNode)2 IOException (java.io.IOException)1 LEGATO (legato.LEGATO)1 OntModel (org.apache.jena.ontology.OntModel)1 Query (org.apache.jena.query.Query)1 QueryExecution (org.apache.jena.query.QueryExecution)1 QuerySolution (org.apache.jena.query.QuerySolution)1 ResultSet (org.apache.jena.query.ResultSet)1 Model (org.apache.jena.rdf.model.Model)1 Resource (org.apache.jena.rdf.model.Resource)1 Statement (org.apache.jena.rdf.model.Statement)1 StmtIterator (org.apache.jena.rdf.model.StmtIterator)1