Get DISTINCT Statements #4518
-
Hi there, Ho can i get the DISTINCT (by subject & predicate) statements? Thanks Fredy |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
The main thing to understand is that if you ask for statements from more than one context, then you get statements from more than one context. In RDF4J, the context is part of the identity of a statement, so the same (subject, predicate, object) in multiple contexts are not considered identical to each other. Assuming your intent is to get a Model that doesn't have contexts (and therefore no 'duplicated' triples), then one way to fix is by using a SPARQL query instead of a simple getStatement operation, to control the exact shape of the returned data. Something like this: CONSTRUCT { ?s ?p ?o }
FROM <context1>
FROM <context2>
...
WHERE { ?s ?p ?o } Another way is to convert statements on the fly, as you're retrieving, before sticking them into your model. Something like this: Model resultModel = DynamicModelFactory.createEmptyModel();
con.getStatements(subject, null, null, contexts.toArray(new Resource[] {}))
.forEach(st -> resultModel.add(st.getSubject(), st.getPredicate(), st.getObject())); You can also do a similar trick with your original Model resultModel = QueryResults.asModel(con.getStatements(subject, null, null, contexts.toArray(new Resource[] {})));
Model distinctTriples = resultModel.stream()
.map(st -> statement(st.getSubject(), st.getPredicate(), st.getObject(), null))
.collect(ModelCollector.toModel()); |
Beta Was this translation helpful? Give feedback.
The main thing to understand is that if you ask for statements from more than one context, then you get statements from more than one context. In RDF4J, the context is part of the identity of a statement, so the same (subject, predicate, object) in multiple contexts are not considered identical to each other.
Assuming your intent is to get a Model that doesn't have contexts (and therefore no 'duplicated' triples), then one way to fix is by using a SPARQL query instead of a simple getStatement operation, to control the exact shape of the returned data. Something like this:
Another way is to convert statements on…