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 (!NTriplesUtil.isLetter(nextId.charAt(0))) { writer.append("genid"); writer.append(Integer.toHexString(nextId.charAt(0))); } for (int i = 0; i < nextId.length(); i++) { if (NTriplesUtil.isLetterOrNumber(nextId.charAt(i))) { writer.append(nextId.charAt(i)); } else { // Append the character as its hex representation writer.append(Integer.toHexString(nextId.charAt(i))); } } } }
/** * Returns a new blank node with an AG-allocated id. * <p> * See also the javadoc for allowing external blank nodes for more discussion. * * @see AGHttpRepoClient#setAllowExternalBlankNodeIds(boolean) * @see AGRepositoryConnection#prepareHttpRepoClient() */ @Override public Resource createResource() { AGValueFactory vf = getGraph().getConnection().getValueFactory(); BNode blank = vf.createBNode(); return createResource(new AnonId(blank.stringValue())); }
@Test public void testBNodeSerialization() throws Exception { testCon.add(bob, name, nameBob); Statement st; RepositoryResult<Statement> statements = testCon.getStatements(null, null, null, false); try { st = statements.next(); } finally { statements.close(); } BNode bnode = (BNode) st.getSubject(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(baos); out.writeObject(bnode); out.close(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream in = new ObjectInputStream(bais); BNode deserializedBNode = (BNode) in.readObject(); in.close(); assertTrue(bnode.equals(deserializedBNode)); assertTrue(testCon.hasStatement(bnode, name, nameBob, true)); assertTrue(testCon.hasStatement(deserializedBNode, name, nameBob, true)); }
@Override public Set<Statement> reify(KnowledgeBase kb, KBStatement aStatement) { String statementId = aStatement.getStatementId(); KBHandle instance = aStatement.getInstance(); KBHandle property = aStatement.getProperty(); Value value = valueMapper.mapStatementValue(aStatement, vf); IRI subject = vf.createIRI(instance.getIdentifier()); IRI predicate = vf.createIRI(property.getIdentifier()); if (statementId == null) { statementId = vf.createBNode().stringValue(); } Resource id = vf.createBNode(statementId); Statement root = vf.createStatement(subject, predicate, id); IRI valuePredicate = vf.createIRI(PREDICATE_NAMESPACE, predicate.getLocalName()); Statement valueStatement = vf.createStatement(id, valuePredicate, value); Set<Statement> statements = new HashSet<>(); statements.add(root); // S P id statements.add(valueStatement); // id p_s V for (KBQualifier aQualifier : aStatement.getQualifiers()) { Set<Statement> qualifierStatement = reifyQualifier(kb, aQualifier); aQualifier.setOriginalStatements(qualifierStatement); } return statements; }
protected void endUpdateInternal(UpdateContext op) throws SailException { Collection<Statement> model; // realize DELETE synchronized (removed) { model = removed.remove(op); } if (model != null) { for (Statement st : model) { Resource ctx = st.getContext(); if (wildContext.equals(ctx)) { removeStatementsInternal(st.getSubject(), st.getPredicate(), st.getObject()); } else { removeStatementsInternal(st.getSubject(), st.getPredicate(), st.getObject(), ctx); } } } // realize INSERT synchronized (added) { model = added.remove(op); } if (model != null) { for (Statement st : model) { addStatementInternal(st.getSubject(), st.getPredicate(), st.getObject(), st.getContext()); } } }
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 (!NTriplesUtil.isLetter(nextId.charAt(0))) { writer.append("genid"); writer.append(Integer.toHexString(nextId.charAt(0))); } for (int i = 0; i < nextId.length(); i++) { if (NTriplesUtil.isLetterOrNumber(nextId.charAt(i))) { writer.append(nextId.charAt(i)); } else { // Append the character as its hex representation writer.append(Integer.toHexString(nextId.charAt(i))); } } } }
@Override public Set<Statement> reify(KnowledgeBase kb, KBStatement aStatement) { String statementId = aStatement.getStatementId(); KBHandle instance = aStatement.getInstance(); KBHandle property = aStatement.getProperty(); Value value = valueMapper.mapStatementValue(aStatement, vf); IRI subject = vf.createIRI(instance.getIdentifier()); IRI predicate = vf.createIRI(property.getIdentifier()); if (statementId == null) { statementId = vf.createBNode().stringValue(); } Resource id = vf.createBNode(statementId); Statement root = vf.createStatement(subject, predicate, id); IRI valuePredicate = vf.createIRI(PREDICATE_NAMESPACE, predicate.getLocalName()); Statement valueStatement = vf.createStatement(id, valuePredicate, value); Set<Statement> statements = new HashSet<>(); statements.add(root); // S P id statements.add(valueStatement); // id p_s V for (KBQualifier aQualifier : aStatement.getQualifiers()) { Set<Statement> qualifierStatement = reifyQualifier(kb, aQualifier); aQualifier.setOriginalStatements(qualifierStatement); } return statements; }
protected void endUpdateInternal(UpdateContext op) throws SailException { Collection<Statement> model; // realize DELETE synchronized (removed) { model = removed.remove(op); } if (model != null) { for (Statement st : model) { Resource ctx = st.getContext(); if (wildContext.equals(ctx)) { removeStatementsInternal(st.getSubject(), st.getPredicate(), st.getObject()); } else { removeStatementsInternal(st.getSubject(), st.getPredicate(), st.getObject(), ctx); } } } // realize INSERT synchronized (added) { model = added.remove(op); } if (model != null) { for (Statement st : model) { addStatementInternal(st.getSubject(), st.getPredicate(), st.getObject(), st.getContext()); } } }
public static void append(BNode bNode, Appendable appendable) throws IOException { String nextId = bNode.getID(); appendable.append("_:"); if (nextId.isEmpty()) { appendable.append("genid"); appendable.append(Integer.toHexString(bNode.hashCode())); } else { if (!isLetter(nextId.charAt(0))) { appendable.append("genid"); appendable.append(Integer.toHexString(nextId.charAt(0))); } for (int i = 0; i < nextId.length(); i++) { if (isLetterOrNumber(nextId.charAt(i))) { appendable.append(nextId.charAt(i)); } else { // Append the character as its hex representation appendable.append(Integer.toHexString(nextId.charAt(i))); } } } }
statementId = vf.createBNode().stringValue();
while (result.hasNext()) { Statement st = result.next(); assertTrue(bob.equals(st.getSubject())); assertTrue(name.equals(st.getPredicate())); assertTrue(nameBob.equals(st.getObject()));
public static void append(BNode bNode, Appendable appendable) throws IOException { String nextId = bNode.getID(); appendable.append("_:"); if (nextId.isEmpty()) { appendable.append("genid"); appendable.append(Integer.toHexString(bNode.hashCode())); } else { if (!isLetter(nextId.charAt(0))) { appendable.append("genid"); appendable.append(Integer.toHexString(nextId.charAt(0))); } for (int i = 0; i < nextId.length(); i++) { if (isLetterOrNumber(nextId.charAt(i))) { appendable.append(nextId.charAt(i)); } else { // Append the character as its hex representation appendable.append(Integer.toHexString(nextId.charAt(i))); } } } }
statementId = vf.createBNode().stringValue();
protected void writeBNode(BNode bNode) throws IOException { writer.write("_:"); writer.write(bNode.getID()); }
@Test @Category(TestSuites.Prepush.class) public void federationBNodes() throws Exception { BNode bnode = vf.createBNode(); conn.add(bnode, RDF.TYPE, vf.createIRI("http://Foo")); AGVirtualRepository fed = server.federate(repo, repo); AGRepositoryConnection conn2 = fed.getConnection(); // Should be able to create BNodes for a federation conn2.getValueFactory().createBNode(); conn2.getValueFactory().createBNode("foo"); try { conn2.add(bnode, RDF.TYPE, vf.createIRI("http://Boo")); Assert.fail("expected can't write to federation."); } catch (RepositoryException e) { //expected } AGTupleQuery q = conn2.prepareTupleQuery(QueryLanguage.SPARQL, "select ?s {?s ?p ?o}"); TupleQueryResult result = q.evaluate(); Assert.assertTrue(result.hasNext()); BindingSet bind = result.next(); Assert.assertEquals(bnode.stringValue(), bind.getValue("s").stringValue()); }
protected void writeBNode(BNode bNode) throws IOException { writer.write("_:"); writer.write(bNode.getID()); }