org.eclipse.rdf4j.model.BNode Java Examples
The following examples show how to use
org.eclipse.rdf4j.model.BNode.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ElasticsearchStoreTransactionsTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void testClear() { SailRepository elasticsearchStore = new SailRepository(this.elasticsearchStore); try (SailRepositoryConnection connection = elasticsearchStore.getConnection()) { BNode context = vf.createBNode(); connection.begin(IsolationLevels.READ_COMMITTED); connection.add(RDF.TYPE, RDF.TYPE, RDFS.RESOURCE); connection.add(RDF.TYPE, RDF.TYPE, RDF.PROPERTY, context); connection.commit(); connection.begin(IsolationLevels.NONE); connection.clear(); connection.commit(); } }
Example #2
Source File: SPARQLQueryStringUtil.java From semagrow with Apache License 2.0 | 6 votes |
public static StringBuilder toSPARQL(Value value, StringBuilder builder) { if(value instanceof IRI) { IRI aLit = (IRI)value; builder.append("<").append(aLit.toString()).append(">"); } else if(value instanceof BNode) { builder.append("_:").append(((BNode)value).getID()); } else if(value instanceof Literal) { Literal aLit1 = (Literal)value; builder.append("\"\"\"").append(RenderUtils.escape(aLit1.getLabel())).append("\"\"\""); if(Literals.isLanguageLiteral(aLit1)) { builder.append("@").append(aLit1.getLanguage()); } else if (value instanceof PlainLiteral) { // do not print type } else { builder.append("^^<").append(aLit1.getDatatype().toString()).append(">"); } } return builder; }
Example #3
Source File: AbstractNQuadsParserUnitTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Tests basic N-Quads parsing with literal. */ @Test public void testParseBasicLiteral() throws RDFHandlerException, IOException, RDFParseException { final ByteArrayInputStream bais = new ByteArrayInputStream( "_:a123456768 <http://www.w3.org/20/ica#dtend> \"2010-05-02\" <http://sin.siteserv.org/def/>." .getBytes()); final TestRDFHandler rdfHandler = new TestRDFHandler(); parser.setRDFHandler(rdfHandler); parser.parse(bais, "http://test.base.uri"); assertThat(rdfHandler.getStatements()).hasSize(1); final Statement statement = rdfHandler.getStatements().iterator().next(); Assert.assertTrue(statement.getSubject() instanceof BNode); Assert.assertEquals("http://www.w3.org/20/ica#dtend", statement.getPredicate().stringValue()); Assert.assertTrue(statement.getObject() instanceof Literal); Assert.assertEquals("2010-05-02", statement.getObject().stringValue()); Assert.assertEquals("http://sin.siteserv.org/def/", statement.getContext().stringValue()); }
Example #4
Source File: RenderUtils.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Append the SPARQL query string rendering of the {@link org.eclipse.rdf4j.model.Value} to the supplied * {@link StringBuilder}. * * @param value the value to render * @param builder the {@link StringBuilder} to append to * @return the original {@link StringBuilder} with the value appended. */ public static StringBuilder toSPARQL(Value value, StringBuilder builder) { if (value instanceof IRI) { IRI aURI = (IRI) value; builder.append("<").append(aURI.toString()).append(">"); } else if (value instanceof BNode) { builder.append("_:").append(((BNode) value).getID()); } else if (value instanceof Literal) { Literal aLit = (Literal) value; builder.append("\"\"\"").append(escape(aLit.getLabel())).append("\"\"\""); if (Literals.isLanguageLiteral(aLit)) { aLit.getLanguage().ifPresent(lang -> builder.append("@").append(lang)); } else { builder.append("^^<").append(aLit.getDatatype().toString()).append(">"); } } return builder; }
Example #5
Source File: RDFWriterTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void testWriteTwoStatementsSubjectBNodeSinglePredicateSingleContextBNodeWithNamespace() throws Exception { Model input = new LinkedHashModel(); input.setNamespace("ex", exNs); input.add(vf.createStatement(bnodeSingleUseSubject, uri1, uri1, bnode)); input.add(vf.createStatement(bnodeSingleUseSubject, uri1, uri2, bnode)); ByteArrayOutputStream outputWriter = new ByteArrayOutputStream(); write(input, outputWriter); ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray()); Model parsedOutput = parse(inputReader, ""); assertEquals(2, parsedOutput.size()); assertEquals(1, parsedOutput.filter(null, uri1, uri1).size()); assertEquals(1, parsedOutput.filter(null, uri1, uri2).size()); assertEquals(1, parsedOutput.contexts().size()); if (rdfWriterFactory.getRDFFormat().supportsContexts()) { assertTrue(parsedOutput.contexts().iterator().next() instanceof BNode); } assertEquals(1, parsedOutput.subjects().size()); assertTrue(parsedOutput.subjects().iterator().next() instanceof BNode); }
Example #6
Source File: RDFWriterTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void testWriteSingleStatementSubjectBNodeSingleContextIRI() throws Exception { Model input = new LinkedHashModel(); input.add(vf.createStatement(bnodeSingleUseSubject, uri1, uri1, uri1)); ByteArrayOutputStream outputWriter = new ByteArrayOutputStream(); write(input, outputWriter); ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray()); Model parsedOutput = parse(inputReader, ""); assertEquals(1, parsedOutput.size()); if (rdfWriterFactory.getRDFFormat().supportsContexts()) { assertEquals(1, parsedOutput.filter(null, uri1, uri1, uri1).size()); } else { assertEquals(1, parsedOutput.filter(null, uri1, uri1).size()); } assertEquals(1, parsedOutput.subjects().size()); assertTrue(parsedOutput.subjects().iterator().next() instanceof BNode); }
Example #7
Source File: RDFWriterTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void testWriteOneStatementsObjectBNodeSinglePredicateSingleContextBNodeReusedWithNamespace() throws Exception { Model input = new LinkedHashModel(); input.setNamespace("ex", exNs); input.add(vf.createStatement(uri1, uri1, bnode, bnode)); ByteArrayOutputStream outputWriter = new ByteArrayOutputStream(); write(input, outputWriter); ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray()); Model parsedOutput = parse(inputReader, ""); assertEquals(1, parsedOutput.size()); Model doubleBNodeStatement = parsedOutput.filter(uri1, uri1, null); assertEquals(1, doubleBNodeStatement.size()); assertEquals(1, parsedOutput.contexts().size()); if (rdfWriterFactory.getRDFFormat().supportsContexts()) { assertTrue(parsedOutput.contexts().iterator().next() instanceof BNode); } assertEquals(1, parsedOutput.objects().size()); assertTrue(parsedOutput.objects().iterator().next() instanceof BNode); assertTrue(doubleBNodeStatement.objects().iterator().next() instanceof BNode); if (rdfWriterFactory.getRDFFormat().supportsContexts()) { assertEquals(doubleBNodeStatement.objects().iterator().next(), doubleBNodeStatement.contexts().iterator().next()); } }
Example #8
Source File: ComplexSPARQLQueryTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void testDescribeAWhere() throws Exception { loadTestData("/testdata-query/dataset-describe.trig"); StringBuilder query = new StringBuilder(); query.append(getNamespaceDeclarations()); query.append("DESCRIBE ?x WHERE {?x rdfs:label \"a\". } "); GraphQuery gq = conn.prepareGraphQuery(QueryLanguage.SPARQL, query.toString()); ValueFactory f = conn.getValueFactory(); IRI a = f.createIRI("http://example.org/a"); IRI p = f.createIRI("http://example.org/p"); try (GraphQueryResult evaluate = gq.evaluate();) { Model result = QueryResults.asModel(evaluate); Set<Value> objects = result.filter(a, p, null).objects(); assertNotNull(objects); for (Value object : objects) { if (object instanceof BNode) { assertTrue(result.contains((Resource) object, null, null)); assertEquals(2, result.filter((Resource) object, null, null).size()); } } } }
Example #9
Source File: RDFXMLPrettyWriter.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Write out the opening tag of the subject or object of a statement up to (but not including) the end of the tag. * Used both in writeStartSubject and writeEmptySubject. */ private void writeNodeStartOfStartTag(Node node) throws IOException, RDFHandlerException { Value value = node.getValue(); if (node.hasType()) { // We can use abbreviated syntax writeStartOfStartTag(node.getType().getNamespace(), node.getType().getLocalName()); } else { // We cannot use abbreviated syntax writeStartOfStartTag(RDF.NAMESPACE, "Description"); } if (value instanceof IRI) { IRI uri = (IRI) value; writeAttribute(RDF.NAMESPACE, "about", uri.toString()); } else { BNode bNode = (BNode) value; writeAttribute(RDF.NAMESPACE, "nodeID", getValidNodeId(bNode)); } }
Example #10
Source File: ProtocolTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void testEncodeValueRoundtrip() { final ValueFactory vf = SimpleValueFactory.getInstance(); IRI uri = vf.createIRI("http://example.org/foo-bar"); String encodedUri = Protocol.encodeValue(uri); IRI decodedUri = (IRI) Protocol.decodeValue(encodedUri, vf); assertEquals(uri, decodedUri); BNode bnode = vf.createBNode("foo-bar-1"); String encodedBnode = Protocol.encodeValue(bnode); BNode decodedNode = (BNode) Protocol.decodeValue(encodedBnode, vf); assertEquals(bnode, decodedNode); Triple triple1 = vf.createTriple(bnode, uri, vf.createLiteral(16)); String encodedTriple1 = Protocol.encodeValue(triple1); Triple decodedTriple1 = (Triple) Protocol.decodeValue(encodedTriple1, vf); assertEquals(triple1, decodedTriple1); Triple triple2 = vf.createTriple(bnode, uri, triple1); String encodedTriple2 = Protocol.encodeValue(triple2); Triple decodedTriple2 = (Triple) Protocol.decodeValue(encodedTriple2, vf); assertEquals(triple2, decodedTriple2); }
Example #11
Source File: SpinRenderer.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
public void end() throws RDFHandlerException { if (list != null) { endList(null); } // end any named graph lists for (ListContext elementsCtx : namedGraphLists.values()) { restore(elementsCtx); endList(null); } // output all the var BNodes together to give a more friendlier RDF // structure for (Map.Entry<String, BNode> entry : varBNodes.entrySet()) { handler.handleStatement(valueFactory.createStatement(entry.getValue(), SP.VAR_NAME_PROPERTY, valueFactory.createLiteral(entry.getKey()))); } }
Example #12
Source File: ValueRdfConverterTest.java From Wikidata-Toolkit with Apache License 2.0 | 6 votes |
@Test public void testWriteUnsupportedEntityIdValue() throws RDFHandlerException, RDFParseException, IOException { AnyValueConverter valueConverter = new AnyValueConverter( this.rdfWriter, this.rdfConversionBuffer, this.propertyRegister); UnsupportedEntityIdValue value = mapper.readValue( "{\"type\":\"wikibase-entityid\",\"value\":{\"entity-type\":\"funky\",\"id\":\"Z343\"}}", UnsupportedEntityIdValueImpl.class); PropertyIdValue propertyIdValue = objectFactory.getPropertyIdValue( "P569", "http://www.wikidata.org/entity/"); Value valueURI = valueConverter.getRdfValue(value, propertyIdValue, false); this.rdfWriter.finish(); assertTrue(valueURI instanceof BNode); }
Example #13
Source File: AbstractNQuadsParserUnitTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Tests basic N-Quads parsing with blank node. */ @Test public void testParseBasicBNode() throws RDFHandlerException, IOException, RDFParseException { final ByteArrayInputStream bais = new ByteArrayInputStream( "_:a123456768 <http://www.w3.org/20/ica#dtend> <http://sin/value/2> <http://sin.siteserv.org/def/>." .getBytes()); final TestRDFHandler rdfHandler = new TestRDFHandler(); parser.setRDFHandler(rdfHandler); parser.parse(bais, "http://test.base.uri"); assertThat(rdfHandler.getStatements()).hasSize(1); final Statement statement = rdfHandler.getStatements().iterator().next(); Assert.assertTrue(statement.getSubject() instanceof BNode); Assert.assertEquals("http://www.w3.org/20/ica#dtend", statement.getPredicate().stringValue()); Assert.assertTrue(statement.getObject() instanceof IRI); Assert.assertEquals("http://sin/value/2", statement.getObject().stringValue()); Assert.assertEquals("http://sin.siteserv.org/def/", statement.getContext().stringValue()); }
Example #14
Source File: NTriplesWriter.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
private void writeBNode(BNode bNode) throws IOException { String nextId = bNode.getID(); writer.append("_:"); if (nextId.isEmpty()) { writer.append("genid"); writer.append(Integer.toHexString(bNode.hashCode())); } else { if (!ASCIIUtil.isLetter(nextId.charAt(0))) { writer.append("genid"); writer.append(Integer.toHexString(nextId.charAt(0))); } for (int i = 0; i < nextId.length(); i++) { if (ASCIIUtil.isLetterOrNumber(nextId.charAt(i))) { writer.append(nextId.charAt(i)); } else { // Append the character as its hex representation writer.append(Integer.toHexString(nextId.charAt(i))); } } } }
Example #15
Source File: ElasticsearchStoreTransactionsTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void testRollbackClearSimple() { SailRepository elasticsearchStore = new SailRepository(this.elasticsearchStore); try (SailRepositoryConnection connection = elasticsearchStore.getConnection()) { BNode context = vf.createBNode(); connection.begin(IsolationLevels.READ_COMMITTED); connection.add(RDF.TYPE, RDF.TYPE, RDFS.RESOURCE); connection.add(RDF.TYPE, RDF.TYPE, RDF.PROPERTY, context); connection.commit(); connection.begin(); connection.clear(); assertEquals(0, connection.size()); connection.rollback(); assertEquals(2, connection.size()); } }
Example #16
Source File: RDFWriterTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void testWriteSingleStatementSubjectBNodeSingleContextBNode() throws Exception { Model input = new LinkedHashModel(); input.add(vf.createStatement(bnodeSingleUseSubject, uri1, uri1, bnode)); ByteArrayOutputStream outputWriter = new ByteArrayOutputStream(); write(input, outputWriter); ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray()); Model parsedOutput = parse(inputReader, ""); assertEquals(1, parsedOutput.size()); assertEquals(1, parsedOutput.filter(null, uri1, uri1).size()); assertEquals(1, parsedOutput.contexts().size()); if (rdfWriterFactory.getRDFFormat().supportsContexts()) { assertTrue(parsedOutput.contexts().iterator().next() instanceof BNode); } assertEquals(1, parsedOutput.subjects().size()); assertTrue(parsedOutput.subjects().iterator().next() instanceof BNode); }
Example #17
Source File: VisulizerTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void maxCount() throws Exception { ShaclSail shaclSail = Utils.getInitializedShaclSail("shaclMax.ttl"); try (NotifyingSailConnection connection = shaclSail.getConnection()) { SimpleValueFactory vf = SimpleValueFactory.getInstance(); connection.begin(); BNode bNode = vf.createBNode(); connection.addStatement(bNode, RDF.TYPE, RDFS.RESOURCE); connection.addStatement(bNode, RDFS.LABEL, vf.createLiteral("")); connection.commit(); shaclSail.setLogValidationPlans(true); connection.begin(); BNode bNode2 = vf.createBNode(); connection.addStatement(bNode2, RDF.TYPE, RDFS.RESOURCE); connection.removeStatement(null, bNode, RDFS.LABEL, vf.createLiteral("")); connection.commit(); } }
Example #18
Source File: VisulizerTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test(expected = SailException.class) public void minCount() throws Exception { ShaclSail shaclSail = Utils.getInitializedShaclSail("shacl.ttl"); try (NotifyingSailConnection connection = shaclSail.getConnection()) { SimpleValueFactory vf = SimpleValueFactory.getInstance(); connection.begin(); BNode bNode = vf.createBNode(); connection.addStatement(bNode, RDF.TYPE, RDFS.RESOURCE); connection.addStatement(bNode, RDFS.LABEL, vf.createLiteral("")); connection.commit(); shaclSail.setLogValidationPlans(true); connection.begin(); BNode bNode2 = vf.createBNode(); connection.addStatement(bNode2, RDF.TYPE, RDFS.RESOURCE); connection.removeStatement(null, bNode, RDFS.LABEL, vf.createLiteral("")); connection.commit(); } }
Example #19
Source File: RDFWriterTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
private void assertSameModel(Model expected, Model actual) { assertEquals(expected.size(), actual.size()); assertEquals(expected.subjects().size(), actual.subjects().size()); assertEquals(expected.predicates().size(), actual.predicates().size()); assertEquals(expected.objects().size(), actual.objects().size()); Set<Value> inputNodes = new HashSet<>(expected.subjects()); inputNodes.addAll(expected.objects()); Set<Value> outputNodes = new HashSet<>(actual.subjects()); outputNodes.addAll(actual.objects()); assertEquals(inputNodes.size(), outputNodes.size()); for (Statement st : expected) { Resource subj = st.getSubject() instanceof IRI ? st.getSubject() : null; IRI pred = st.getPredicate(); Value obj = st.getObject() instanceof BNode ? null : st.getObject(); assertTrue("Missing " + st, actual.contains(subj, pred, obj)); assertEquals(actual.filter(subj, pred, obj).size(), actual.filter(subj, pred, obj).size()); } }
Example #20
Source File: RDFWriterTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void testWriteTwoStatementsSubjectBNodeSinglePredicateSingleContextIRIWithNamespace() throws Exception { Model input = new LinkedHashModel(); input.setNamespace("ex", exNs); input.add(vf.createStatement(bnodeSingleUseSubject, uri1, uri1, uri1)); input.add(vf.createStatement(bnodeSingleUseSubject, uri1, uri2, uri1)); ByteArrayOutputStream outputWriter = new ByteArrayOutputStream(); write(input, outputWriter); ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray()); Model parsedOutput = parse(inputReader, ""); assertEquals(2, parsedOutput.size()); if (rdfWriterFactory.getRDFFormat().supportsContexts()) { assertEquals(1, parsedOutput.filter(null, uri1, uri1, uri1).size()); assertEquals(1, parsedOutput.filter(null, uri1, uri2, uri1).size()); } else { assertEquals(1, parsedOutput.filter(null, uri1, uri1).size()); assertEquals(1, parsedOutput.filter(null, uri1, uri2).size()); } assertEquals(1, parsedOutput.subjects().size()); assertTrue(parsedOutput.subjects().iterator().next() instanceof BNode); }
Example #21
Source File: MemInferencingTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void testBlankNodePredicateInference() { Repository sailRepository = new SailRepository(createSail()); ValueFactory vf = sailRepository.getValueFactory(); try (RepositoryConnection connection = sailRepository.getConnection()) { BNode bNode = vf.createBNode(); connection.add(vf.createStatement(vf.createIRI("http://a"), RDFS.SUBPROPERTYOF, bNode)); // 1 connection.add(vf.createStatement(bNode, RDFS.DOMAIN, vf.createIRI("http://c"))); // 2 connection.add( vf.createStatement(vf.createIRI("http://d"), vf.createIRI("http://a"), vf.createIRI("http://e"))); // 3 } try (RepositoryConnection connection = sailRepository.getConnection()) { boolean correctInference = connection.hasStatement(vf.createIRI("http://d"), RDF.TYPE, vf.createIRI("http://c"), true); assertTrue("d should be type c, because 3 and 1 entail 'd _:bNode e' with 2 entail 'd type c'", correctInference); } }
Example #22
Source File: RepositoryManagerFederator.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Adds a new repository to the {@link org.eclipse.rdf4j.repository.manager.RepositoryManager}, which is a * federation of the given repository id's, which must also refer to repositories already managed by the * {@link org.eclipse.rdf4j.repository.manager.RepositoryManager}. * * @param fedID the desired identifier for the new federation repository * @param description the desired description for the new federation repository * @param members the identifiers of the repositories to federate, which must already exist and be managed by * the {@link org.eclipse.rdf4j.repository.manager.RepositoryManager} * @param readonly whether the federation is read-only * @param distinct whether the federation enforces distinct results from its members * @throws MalformedURLException if the {@link org.eclipse.rdf4j.repository.manager.RepositoryManager} has a * malformed location * @throws RDF4JException if a problem otherwise occurs while creating the federation */ public void addFed(String fedID, String description, Collection<String> members, boolean readonly, boolean distinct) throws MalformedURLException, RDF4JException { if (members.contains(fedID)) { throw new RepositoryConfigException("A federation member may not have the same ID as the federation."); } Model graph = new LinkedHashModel(); BNode fedRepoNode = valueFactory.createBNode(); LOGGER.debug("Federation repository root node: {}", fedRepoNode); addToGraph(graph, fedRepoNode, RDF.TYPE, RepositoryConfigSchema.REPOSITORY); addToGraph(graph, fedRepoNode, RepositoryConfigSchema.REPOSITORYID, valueFactory.createLiteral(fedID)); addToGraph(graph, fedRepoNode, RDFS.LABEL, valueFactory.createLiteral(description)); addImplementation(members, graph, fedRepoNode, readonly, distinct); RepositoryConfig fedConfig = RepositoryConfig.create(graph, fedRepoNode); fedConfig.validate(); manager.addRepositoryConfig(fedConfig); }
Example #23
Source File: RDFWriterTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void testWriteSingleStatementSubjectBNodeSingleContextBNodeWithNamespace() throws Exception { Model input = new LinkedHashModel(); input.setNamespace("ex", exNs); input.add(vf.createStatement(bnodeSingleUseSubject, uri1, uri1, bnode)); ByteArrayOutputStream outputWriter = new ByteArrayOutputStream(); write(input, outputWriter); ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray()); Model parsedOutput = parse(inputReader, ""); assertEquals(1, parsedOutput.size()); assertEquals(1, parsedOutput.filter(null, uri1, uri1).size()); assertEquals(1, parsedOutput.contexts().size()); if (rdfWriterFactory.getRDFFormat().supportsContexts()) { assertTrue(parsedOutput.contexts().iterator().next() instanceof BNode); } assertEquals(1, parsedOutput.subjects().size()); assertTrue(parsedOutput.subjects().iterator().next() instanceof BNode); }
Example #24
Source File: RenderUtils.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Return the query string rendering of the {@link Value} * * @param theValue the value to render * @return the value rendered in its query string representation */ public static String toSeRQL(Value theValue) { StringBuilder aBuffer = new StringBuilder(); if (theValue instanceof IRI) { IRI aURI = (IRI) theValue; aBuffer.append("<").append(aURI.toString()).append(">"); } else if (theValue instanceof BNode) { aBuffer.append("_:").append(((BNode) theValue).getID()); } else if (theValue instanceof Literal) { Literal aLit = (Literal) theValue; aBuffer.append("\"").append(escape(aLit.getLabel())).append("\""); if (Literals.isLanguageLiteral(aLit)) { aBuffer.append("@").append(aLit.getLanguage()); } else { aBuffer.append("^^<").append(aLit.getDatatype().toString()).append(">"); } } return aBuffer.toString(); }
Example #25
Source File: NodeKindFilter.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override boolean checkTuple(Tuple t) { Value value = t.line.get(1); /* * BlankNode(SHACL.BLANK_NODE), IRI(SHACL.IRI), Literal(SHACL.LITERAL), BlankNodeOrIRI(SHACL.BLANK_NODE_OR_IRI), * BlankNodeOrLiteral(SHACL.BLANK_NODE_OR_LITERAL), IRIOrLiteral(SHACL.IRI_OR_LITERAL), */ switch (nodeKind) { case IRI: return value instanceof IRI; case Literal: return value instanceof Literal; case BlankNode: return value instanceof BNode; case IRIOrLiteral: return value instanceof IRI || value instanceof Literal; case BlankNodeOrIRI: return value instanceof BNode || value instanceof IRI; case BlankNodeOrLiteral: return value instanceof BNode || value instanceof Literal; } throw new IllegalStateException("Unknown nodeKind"); }
Example #26
Source File: TupleExprs.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
public static String getConstVarName(Value value) { if (value == null) { throw new IllegalArgumentException("value can not be null"); } // We use toHexString to get a more compact stringrep. String uniqueStringForValue = Integer.toHexString(value.stringValue().hashCode()); if (value instanceof Literal) { uniqueStringForValue += "_lit"; // we need to append datatype and/or language tag to ensure a unique // var name (see SES-1927) Literal lit = (Literal) value; if (lit.getDatatype() != null) { uniqueStringForValue += "_" + Integer.toHexString(lit.getDatatype().hashCode()); } if (lit.getLanguage() != null) { uniqueStringForValue += "_" + Integer.toHexString(lit.getLanguage().hashCode()); } } else if (value instanceof BNode) { uniqueStringForValue += "_node"; } else { uniqueStringForValue += "_uri"; } return "_const_" + uniqueStringForValue; }
Example #27
Source File: AbstractValueFactory.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public synchronized BNode createBNode() { int id = nextBNodeID++; BNode result = createBNode(bnodePrefix + id); if (id == Integer.MAX_VALUE) { // Start with a new bnode prefix initBNodeParams(); } return result; }
Example #28
Source File: ConstructProjection.java From rya with Apache License 2.0 | 5 votes |
/** * Projects a given BindingSet onto a RyaStatement. The subject, predicate, * and object are extracted from the input VisibilityBindingSet (if the * getSubjectSourceName, getPredicateSourceName, getObjectSourceName is resp. * non-constant) and from the Var Value itself (if getSubjectSourceName, * predicateSource, getObjectSourceName is resp. constant). * * * @param vBs * - Visibility BindingSet that gets projected onto an RDF * Statement BindingSet with Binding names subject, predicate and * object * @param bNodes - Optional Map used to pass {@link BNode}s for given variable names into * multiple {@link ConstructProjection}s. This allows a ConstructGraph to create * RyaStatements with the same BNode for a given variable name across multiple ConstructProjections. * @return - RyaStatement whose values are determined by * {@link ConstructProjection#getSubjectSourceName()}, * {@link ConstructProjection#getPredicateSourceName()}, * {@link ConstructProjection#getObjectSourceName()}. * */ public RyaStatement projectBindingSet(VisibilityBindingSet vBs, Map<String, BNode> bNodes) { Preconditions.checkNotNull(vBs); Preconditions.checkNotNull(bNodes); Value subj = getValue(subjName, subjValue, vBs, bNodes); Value pred = getValue(predName, predValue, vBs, bNodes); Value obj = getValue(objName, objValue, vBs, bNodes); Preconditions.checkNotNull(subj); Preconditions.checkNotNull(pred); Preconditions.checkNotNull(obj); Preconditions.checkArgument(subj instanceof Resource); Preconditions.checkArgument(pred instanceof IRI); RyaIRI subjType = RdfToRyaConversions.convertResource((Resource) subj); RyaIRI predType = RdfToRyaConversions.convertIRI((IRI) pred); RyaType objectType = RdfToRyaConversions.convertValue(obj); RyaStatement statement = new RyaStatement(subjType, predType, objectType); try { statement.setColumnVisibility(vBs.getVisibility().getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { log.trace("Unable to decode column visibility. RyaStatement being created without column visibility."); } return statement; }
Example #29
Source File: StatementFunctionTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testEvaluateBNodeAndIRI() { BNode subj = f.createBNode(); IRI pred = f.createIRI("urn:b"); IRI obj = f.createIRI("urn:c"); Value value = function.evaluate(f, subj, pred, obj); assertNotNull(value); assertTrue("expect Triple", value instanceof Triple); Triple other = f.createTriple(subj, pred, obj); assertEquals("expect to be the same", value, other); }
Example #30
Source File: RDFWriterTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testWriteSingleStatementSubjectBNodeNoContext() throws Exception { Model input = new LinkedHashModel(); input.add(vf.createStatement(bnodeSingleUseSubject, uri1, uri1)); ByteArrayOutputStream outputWriter = new ByteArrayOutputStream(); write(input, outputWriter); ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray()); Model parsedOutput = parse(inputReader, ""); assertEquals(1, parsedOutput.size()); assertEquals(1, parsedOutput.filter(null, uri1, uri1).size()); assertEquals(1, parsedOutput.subjects().size()); assertTrue(parsedOutput.subjects().iterator().next() instanceof BNode); }