@Override public Set<Triple> edgesOf(Node vertex) { Set<Triple> result = new HashSet<>(); find(vertex, confinementPredicate, Node.ANY).forEachRemaining(result::add); find(Node.ANY, confinementPredicate, vertex).forEachRemaining(result::add); return result; }
@Override public Set<Node> vertexSet() { Set<Node> result = new HashSet<>(); find(Node.ANY, confinementPredicate, Node.ANY).forEachRemaining(triple -> { result.add(triple.getSubject()); result.add(triple.getObject()); }); return result; }
g.find(ANY).forEachRemaining(x);
/** * Tries to return the items of a list or empty if th elist is empty or not an rdf:List */ public static Collection<RDFNode> getListItemsOrEmpty(RDFNode node) { ImmutableList.Builder<RDFNode> items = ImmutableList.builder(); if (isList(node)) { RDFList rdfList = node.as(RDFList.class); rdfList.iterator().forEachRemaining(items::add); } return items.build(); }
/** * Tries to return the items of a list or empty if th elist is empty or not an rdf:List */ public static Collection<RDFNode> getListItemsOrEmpty(RDFNode node) { ImmutableList.Builder<RDFNode> items = ImmutableList.builder(); if (isList(node)) { RDFList rdfList = node.as(RDFList.class); rdfList.iterator().forEachRemaining(items::add); } return items.build(); }
@Override public void addGraph(Node graphName, Graph graph) { graph.find().forEachRemaining(t -> add(new Quad(graphName, t))); }
private Consumer<Graph> addGraph(final Node name) { return g -> g.find().mapWith(t -> new Quad(name, t)).forEachRemaining(this::add); }
g.find(ANY).forEachRemaining(x);
@Override public void setDefaultGraph(final Graph g) { mutate(graph -> { defaultGraph().clear(); graph.find().forEachRemaining(defaultGraph()::add); }, g); }
@Override public InputStream getEntailedTriples(final String subject, final InputStream input, final String contentType, final String acceptType) { final Model model = createDefaultModel(); read(model, input, contentTypeToLang(contentType.split(";")[0])); final Model entailed = createDefaultModel(); entailed.setNsPrefixes(model); createRDFSModel(schema, model).listStatements() // We don't care about rdfs:Class and rdfs:Resource types .filterDrop(s -> isRdfsClass(s) || isRdfsResource(s)) .filterDrop(t -> schema.contains(t.getSubject(), null)) .forEachRemaining(entailed::add); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); write(baos, entailed, getRdfLanguage(acceptType.split(";")[0]).orElse(DEFAULT_LANG)); return new ByteArrayInputStream(baos.toByteArray()); }
/** Format:: access:entry [ :user "user2" ; :graphs (<http://host/graphname3> ) ] */ private void parseStruct(Multimap<String, Node> map, Resource root, Resource r) { if ( ! GraphUtils.exactlyOneProperty(r, VocabSecurity.pUser) ) throw new AssemblerException(root, "Expected exactly one access:user property for "+r); if ( ! GraphUtils.exactlyOneProperty(r, VocabSecurity.pGraphs) ) throw new AssemblerException(root, "Expected exactly one access:graphs property for "+r); String user = GraphUtils.getStringValue(r, VocabSecurity.pUser); r.listProperties(VocabSecurity.pGraphs).mapWith(s->s.getObject()).forEachRemaining(x->{ List<Node> graphs = new ArrayList<>(); if ( x.isURIResource() ) { //System.out.printf("S: user %s : access : %s\n", user, x.asNode()); graphs.add(x.asNode()); } else { // List? RDFList list = x.as(RDFList.class); list.iterator().forEachRemaining(rn->{ graphs.add(rn.asNode()); }); } accessEntries(root, map, user, graphs); }); }
@Test public void testVocabularyRev() { final Graph graph = getVocabulary(namespace()); final Set<String> subjects = fields().map(namespace()::concat).collect(toSet()); assertTrue(subjects.size() > 0, "Unable to extract field definitions!"); graph.find(ANY, ANY, ANY).mapWith(Triple::getSubject).filterKeep(Node::isURI).mapWith(Node::getURI) .filterKeep(Objects::nonNull) .filterKeep(uri -> uri.startsWith(namespace())).filterDrop(namespace()::equals) .filterDrop(subjects::contains).forEachRemaining(uri -> { LOGGER.debug("{} not defined in {} class", uri, vocabulary().getName()); }); }
@Test public void directDataQuadsURI() throws IOException { // first make a file of quads to load later final Model model = createDefaultModel(); final Path quads = createTempFile("quadExample", ".nq"); final Resource quadsURI = model.createResource(quads.toFile().toURI().toString()); final Resource simpleExample = model.createResource("test:simpleExample"); simpleExample.addProperty(type, DatasetAssemblerVocab.tDatasetTxnMem); simpleExample.addProperty(data, quadsURI); final DatasetGraph dsg = DatasetGraphFactory.createTxnMem(); model.listStatements().mapWith(Statement::asTriple).mapWith(t -> new Quad(quadsURI.asNode(), t)) .forEachRemaining(dsg::add); try (OutputStream out = new FileOutputStream(quads.toFile())) { write(out, dsg, NQUADS); } final Dataset dataset = assemble(simpleExample); final Model assembledDefaultModel = dataset.getDefaultModel(); final Model assembledNamedModel = dataset.getNamedModel(quadsURI.getURI()); assertTrue(assembledDefaultModel.isEmpty()); assertTrue(assembledNamedModel.contains(assembledNamedModel.createStatement(simpleExample, data, quadsURI))); }
@Test public void testUnion() { final Graph g1 = parseGraph("(graph (triple <s1> <p1> <o1> ))"); final DatasetGraph dsg1 = DatasetGraphFactory.create(g1); final Node graphName1 = NodeFactory.createBlankNode(); dsg1.addGraph(graphName1, g1); final Graph g2 = parseGraph("(graph (triple <s2> <p2> <o2> ))"); final DatasetGraph dsg2 = DatasetGraphFactory.create(g2); final Node graphName2 = NodeFactory.createBlankNode(); dsg2.addGraph(graphName2, g2); DatasetGraph dsg = testInstance(dsg1, dsg2, Context.emptyContext); assertEquals(2, dsg.size()); assertTrue(g1.isIsomorphicWith(dsg.getGraph(graphName1))); assertTrue(g2.isIsomorphicWith(dsg.getGraph(graphName2))); g1.find().mapWith(dsg.getDefaultGraph()::contains).forEachRemaining(Assert::assertTrue); g2.find().mapWith(dsg.getDefaultGraph()::contains).forEachRemaining(Assert::assertTrue); } }
@Test public void testUnion() { final Graph g1 = parseGraph("(graph (triple <s1> <p1> <o1> ))"); final Model m1 = createModelForGraph(g1); final Dataset dsg1 = DatasetFactory.create(m1); final String graphName1 = NodeFactory.createBlankNode().toString(); dsg1.addNamedModel(graphName1, m1); final Graph g2 = parseGraph("(graph (triple <s2> <p2> <o2> ))"); final Dataset dsg2 = DatasetFactory.create(createModelForGraph(g2)); final Model m2 = createModelForGraph(g2); final String graphName2 = NodeFactory.createBlankNode().toString(); dsg2.addNamedModel(graphName2, m2); final Stream<Dataset> stream = Stream.<Dataset>builder().add(dsg1).add(dsg2).build(); Dataset dataset = stream.collect(testInstance()); assertEquals(2, Iter.count(dataset.listNames())); assertTrue(m1.isIsomorphicWith(dataset.getNamedModel(graphName1))); assertTrue(m2.isIsomorphicWith(dataset.getNamedModel(graphName2))); // all statements in any input should be present in the union m1.listStatements().mapWith(dataset.getDefaultModel()::contains).forEachRemaining(Assert::assertTrue); m2.listStatements().mapWith(dataset.getDefaultModel()::contains).forEachRemaining(Assert::assertTrue); // all statements in the union should be present in an input List<Statement> leftovers = dataset.getDefaultModel().listStatements() .filterDrop(m1::contains) .filterDrop(m2::contains).toList(); assertTrue(leftovers.isEmpty()); } }