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;
}
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;
}
Aggregations