From 9369b054be8784eb86496fb3052c0c3e07852fc7 Mon Sep 17 00:00:00 2001 From: Reeshabh Kumar Ranjan Date: Sat, 23 May 2020 01:48:16 +0530 Subject: [PATCH] #405 Add remaining methods (as present in RDFCollection) (documentation pending) Signed-off-by: Reeshabh Kumar Ranjan --- .../rdf4j/model/util/RDFContainers.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/core/model/src/main/java/org/eclipse/rdf4j/model/util/RDFContainers.java b/core/model/src/main/java/org/eclipse/rdf4j/model/util/RDFContainers.java index 09dcfdbe3e8..2faa77d40c7 100644 --- a/core/model/src/main/java/org/eclipse/rdf4j/model/util/RDFContainers.java +++ b/core/model/src/main/java/org/eclipse/rdf4j/model/util/RDFContainers.java @@ -33,6 +33,24 @@ public static > C asRDF(IRI containerType, Itera consumeContainer(containerType, values, container, st -> sink.add(st), contexts); return sink; } + + public static > C asRDF(IRI containerType, Iterable values, Resource container, C sink, + ValueFactory vf, Resource... contexts) { + + Objects.requireNonNull(sink); + consumeContainer(containerType, values, container, st -> sink.add(st), vf, contexts); + return sink; + } + + public static > C asValues(IRI containerType, final Model m, Resource container, C collection, + Resource... contexts) throws ModelException { + Objects.requireNonNull(collection, "collection may not be null"); + + consumeValues(m, container, containerType, v -> collection.add(v), contexts); + + return collection; + } + public static void consumeContainer(IRI containerType, Iterable values, Resource container, Consumer consumer, Resource... contexts) { consumeContainer(containerType, values, container, consumer, SimpleValueFactory.getInstance(), contexts); @@ -71,6 +89,8 @@ private static IRI getAnnotatedMemberPredicate(ValueFactory vf, int elementCount return vf.createIRI(RDF.NAMESPACE, "_" + elementCounter); } + + public static void consumeValues(final Model m, Resource container, IRI containerType, Consumer consumer, Resource... contexts) throws ModelException { Objects.requireNonNull(consumer, "consumer may not be null"); @@ -92,11 +112,49 @@ public static void consumeValues(final Model m, Resource container, IRI containe }, exceptionSupplier, contexts); } + public static > C getContainer(IRI containerType, Model sourceModel, Resource head, C sink, + Resource... contexts) { + Objects.requireNonNull(sourceModel, "input model may not be null"); + extract(containerType, sourceModel, head, st -> sink.add(st), contexts); + return sink; + } + public static void extract(IRI containerType, Model sourceModel, Resource container, Consumer consumer, Resource... contexts) { + Objects.requireNonNull(sourceModel, "source model may not be null"); + GetStatementOptional statementSupplier = (s, p, o, + c) -> ((Model) sourceModel).filter(s, p, o, c).stream().findAny(); + extract(containerType, statementSupplier, container, consumer, Models::modelException, contexts); } public static void extract(IRI containerType, GetStatementOptional statementSupplier, Resource container, Consumer collectionConsumer, Function> exceptionSupplier, Resource... contexts) throws E { + OpenRDFUtil.verifyContextNotNull(contexts); + Objects.requireNonNull(container, "list head may not be null"); + Objects.requireNonNull(collectionConsumer, "collection consumer may not be null"); + + ValueFactory vf = SimpleValueFactory.getInstance(); + + Resource current = container; + final Set encountered = new HashSet<>(); + + for (int annotatedMembershipPropertyCounter = 1; true ; annotatedMembershipPropertyCounter++) { + + + IRI annotatedMembershipPredicate = getAnnotatedMemberPredicate(vf, annotatedMembershipPropertyCounter); + if (statementSupplier.get(container, annotatedMembershipPredicate, null, contexts).equals(Optional.empty())) { + break; + } + Statement statement = statementSupplier.get(container, annotatedMembershipPredicate, null, contexts).get(); + + if (containerType.equals(RDF.ALT)) { + if (encountered.contains(statement.getObject())) { + throw exceptionSupplier.apply("rdf:alt cannot contain duplicate values").get(); + } + encountered.add(statement.getObject()); + } + + collectionConsumer.accept(statement); + } } }