Search in sources :

Example 11 with Property

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);
}
Also used : Dataset(org.apache.jena.query.Dataset) Statement(org.apache.jena.rdf.model.Statement) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) Property(org.apache.jena.rdf.model.Property) Atom(won.protocol.model.Atom)

Example 12 with Property

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;
}
Also used : Statement(org.apache.jena.rdf.model.Statement) ArrayList(java.util.ArrayList) StmtIterator(org.apache.jena.rdf.model.StmtIterator) Iterator(java.util.Iterator) Property(org.apache.jena.rdf.model.Property)

Example 13 with Property

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;
}
Also used : StmtIterator(org.apache.jena.rdf.model.StmtIterator) Statement(org.apache.jena.rdf.model.Statement) Resource(org.apache.jena.rdf.model.Resource) ArrayList(java.util.ArrayList) Property(org.apache.jena.rdf.model.Property) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 14 with Property

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;
}
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 15 with Property

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;
}
Also used : StmtIterator(org.apache.jena.rdf.model.StmtIterator) Statement(org.apache.jena.rdf.model.Statement) OntModel(org.apache.jena.ontology.OntModel) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) Property(org.apache.jena.rdf.model.Property) RDFNode(org.apache.jena.rdf.model.RDFNode)

Aggregations

Property (org.apache.jena.rdf.model.Property)89 Resource (org.apache.jena.rdf.model.Resource)58 Model (org.apache.jena.rdf.model.Model)45 RDFNode (org.apache.jena.rdf.model.RDFNode)18 Test (org.junit.Test)18 Statement (org.apache.jena.rdf.model.Statement)17 StmtIterator (org.apache.jena.rdf.model.StmtIterator)14 ArrayList (java.util.ArrayList)13 Literal (org.apache.jena.rdf.model.Literal)11 Node (org.apache.jena.graph.Node)6 Triple (org.apache.jena.graph.Triple)6 Iterator (java.util.Iterator)5 OntModel (org.apache.jena.ontology.OntModel)5 Dataset (org.apache.jena.query.Dataset)5 QueryExecution (org.apache.jena.query.QueryExecution)5 InfModel (org.apache.jena.rdf.model.InfModel)4 Store (org.apache.jena.sdb.Store)4 HashSet (java.util.HashSet)3 LEGATO (legato.LEGATO)3 RDFDatatype (org.apache.jena.datatypes.RDFDatatype)3