public static Graph getGraph() { Graph g = Factory.createGraphMem(); g.add( new Triple( NodeFactory.createURI("http://example.com/subject"), NodeFactory.createURI("http://example.com/predicate"), NodeFactory.createURI("http://example.com/object"))); g.add( new Triple( NodeFactory.createBlankNode(BlankNodeId.create("a")), NodeFactory.createURI("http://example.com/p1"), NodeFactory.createBlankNode(BlankNodeId.create("b")))); g.add( new Triple( NodeFactory.createBlankNode(BlankNodeId.create("b")), NodeFactory.createURI("http://example.com/p2"), NodeFactory.createLiteral("foo"))); g.add( new Triple( NodeFactory.createURI("http://example.com/ns/e"), NodeFactory.createURI("http://example.com/ns/p5"), NodeFactory.createLiteral("verify base works"))); return g; }
public void testRetrieveTriplesByNode() { Graph G = getGraph(); Node N = NodeFactory.createBlankNode(), M = NodeFactory.createBlankNode(); ReifierStd.reifyAs(G, N, triple("x R y")); assertEquals("gets correct triple", triple("x R y"), ReifierStd.getTriple(G, N)); ReifierStd.reifyAs(G, M, triple("p S q")); assertDiffer("the anon nodes must be distinct", N, M); assertEquals("gets correct triple", triple("p S q"), ReifierStd.getTriple(G, M)); assertTrue("node is known bound", ReifierStd.hasTriple(G, M)); assertTrue("node is known bound", ReifierStd.hasTriple(G, N)); assertFalse( "node is known unbound", ReifierStd.hasTriple(G, NodeFactory.createURI("any:thing"))); }
/** * Retrieve or create a bNode representing an inferred property value. * * @param instance the base instance node to which the property applies * @param prop the property node whose value is being inferred * @param pclass the (optional, can be null) class for the inferred value. * @return the bNode representing the property value */ public synchronized Node getTemp(Node instance, Node prop, Node pclass) { NodePair ip = new NodePair(instance, prop); Node result = null; for (Iterator<Node> i = ipMap.getAll(ip); i.hasNext(); ) { Node t = i.next(); if (pclass != null) { Object tClass = classMap.get(t); if (tClass != null && tClass.equals(pclass)) { result = t; break; } } else { result = t; break; } } if (result == null) { // No value yet, so create one result = NodeFactory.createBlankNode(); ipMap.put(ip, result); if (pclass != null) { classMap.put(result, pclass); } } return result; }
/** * Implement <_:....> as a "Node IRI" that is, use the given label as the BNode internal label. * Use with care. */ public static Node createIRIorBNode(String iri) { // Is it a bNode label? i.e. <_:xyz> if (isBNodeIRI(iri)) { String s = iri.substring(bNodeLabelStart.length()); Node n = NodeFactory.createBlankNode(s); return n; } return NodeFactory.createURI(iri); }