use of org.apache.jena.rdf.model.Property in project webofneeds by researchstudio-sat.
the class ConnectFromNodeReviewSocketImpl method addReviewToAtom.
private void addReviewToAtom(Map<Property, String> reviewData, URI connectionUri) throws IllegalArgumentException {
String aboutAtomURI = reviewData.get(SCHEMA.ABOUT);
Double rating = Double.parseDouble(reviewData.get(SCHEMA.RATING_VALUE)) > 0.0 ? Double.parseDouble(reviewData.get(SCHEMA.RATING_VALUE)) : 0.0;
Atom aboutAtom = atomService.getAtomRequired(URI.create(aboutAtomURI));
Dataset atomDataset = aboutAtom.getDatatsetHolder().getDataset();
Model derivationModel = atomDataset.getNamedModel(aboutAtom.getAtomURI() + "#derivedData");
Resource aboutAtomResource = derivationModel.getResource(aboutAtomURI);
Resource conRes = derivationModel.getResource(connectionUri.toString());
Statement reviewdConnectionsProperty = derivationModel.getProperty(aboutAtomResource, WXREVIEW.reviewedConnection);
if (reviewdConnectionsProperty == null) {
derivationModel.add(aboutAtomResource, WXREVIEW.reviewedConnection, conRes);
reviewdConnectionsProperty = derivationModel.getProperty(aboutAtomResource, WXREVIEW.reviewedConnection);
} else {
Property ratedConnections = derivationModel.getProperty(WXREVIEW.reviewedConnection.toString());
if (derivationModel.contains(aboutAtomResource, ratedConnections, conRes)) {
logger.debug("Connection already reviewed {}", connectionUri.toString());
throw new IllegalArgumentException("Connection already reviewed");
} else {
derivationModel.add(aboutAtomResource, ratedConnections, conRes);
}
}
Statement aggregateRatingProperty = derivationModel.getProperty(aboutAtomResource, SCHEMA.AGGREGATE_RATING);
if (aggregateRatingProperty == null) {
derivationModel.addLiteral(aboutAtomResource, SCHEMA.AGGREGATE_RATING, rating);
aggregateRatingProperty = derivationModel.getProperty(aboutAtomResource, SCHEMA.AGGREGATE_RATING);
}
int ratingCount = 0;
Statement reviewCountProperty = derivationModel.getProperty(aboutAtomResource, SCHEMA.REVIEW_COUNT);
if (reviewCountProperty != null) {
ratingCount = reviewCountProperty.getInt();
} else {
derivationModel.addLiteral(aboutAtomResource, SCHEMA.REVIEW_COUNT, 0.0);
reviewCountProperty = derivationModel.getProperty(aboutAtomResource, SCHEMA.REVIEW_COUNT);
}
Double aggregateRating = aggregateRatingProperty.getDouble();
Double ratingSum = 0.0;
if (ratingCount < 1) {
ratingSum = aggregateRating;
} else {
ratingSum = (aggregateRating * ratingCount) + rating;
}
ratingCount++;
Double newAggregateRating = ratingSum / ratingCount;
aggregateRatingProperty.changeLiteralObject(newAggregateRating);
reviewCountProperty.changeLiteralObject(ratingCount);
aboutAtom.getDatatsetHolder().setDataset(atomDataset);
atomRepository.save(aboutAtom);
}
use of org.apache.jena.rdf.model.Property in project legato by DOREMUS-ANR.
the class ModelManager method getPropFromPath.
/**
******
* Get all triples of a resource from an RDF model
*******
*/
/* public static Model getAllTriples (Model model, Resource resource)
{
Model newModel = ModelFactory.createDefaultModel();
StmtIterator iter = model.listStatements();
while (iter.hasNext()){
Statement stmt = iter.nextStatement();
Resource subject = stmt.getSubject();
if (subject.toString().equals(resource.toString())){
newModel.add(stmt);
}
}
return model;
} */
/**
******
* List all the properties of a path
*******
*/
public static List<Property> getPropFromPath(Path propPath) {
List<Property> path = new ArrayList<Property>();
Iterator iterPath = propPath.iterator();
while (iterPath.hasNext()) {
Statement stmtPath = (Statement) iterPath.next();
path.add((Property) stmtPath.getPredicate());
}
return path;
}
use of org.apache.jena.rdf.model.Property in project legato by DOREMUS-ANR.
the class ModelManager method getAllClassesFromModels.
/**
*************************************
***Get all classes from two models
**************************************
*/
public static List<Resource> getAllClassesFromModels(Model srcModel, Model tgtModel) {
List<Resource> classList = new ArrayList<Resource>();
StmtIterator iter1 = srcModel.listStatements();
while (iter1.hasNext()) {
Statement stmt = iter1.nextStatement();
Property prop = stmt.getPredicate();
RDFNode object = stmt.getObject();
if (prop.equals(RDF.type))
if (!classList.contains(object))
classList.add(object.asResource());
}
StmtIterator iter2 = tgtModel.listStatements();
while (iter2.hasNext()) {
Statement stmt = iter2.nextStatement();
Property prop = stmt.getPredicate();
RDFNode object = stmt.getObject();
if (prop.equals(RDF.type))
if (!classList.contains(object))
classList.add(object.asResource());
}
return classList;
}
use of org.apache.jena.rdf.model.Property 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.rdf.model.Property in project legato by DOREMUS-ANR.
the class ModelManager method parseCBD.
/**
***************************************************
* Parse a CBD to get the prefLabels from vocabularies
****************************************************
*/
public static Model parseCBD(Model model) throws IOException {
Model m = ModelFactory.createDefaultModel();
StmtIterator iter = model.listStatements();
while (iter.hasNext()) {
Statement stmt = iter.nextStatement();
Resource subject = stmt.getSubject();
Property prop = stmt.getPredicate();
RDFNode object = stmt.getObject();
// if (!object.isLiteral()){ //
// Resource uriConcept = (Resource) object;
// if (uriConcept.toString().contains("iaml/genre")){
// for (String label : ConceptFinder.getLabel(ConceptFinder.genreVocabulary, uriConcept.toString())){
// m.add(subject, prop, label);
// }
// }
// else if (uriConcept.toString().contains("diabolo/genre")){
// for (String label : ConceptFinder.getLabel(ConceptFinder.genreVocabularyDiabolo, uriConcept.toString())){
// m.add(subject, prop, label);
// }
// }
// } //
}
return m;
}
Aggregations