Ejemplo n.º 1
0
 /**
  * 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);
 }
Ejemplo n.º 2
0
  /**
   * Tests characteristic properties
   *
   * @throws IOException
   */
  @Test
  public void characteristic_writable_03() throws IOException {
    CharacteristicWritable cw1 =
        new CharacteristicWritable(NodeFactory.createURI("http://example.org"));
    CharacteristicWritable cw2 =
        new CharacteristicWritable(NodeFactory.createURI("http://example.org/other"));
    this.checkRoundTrip(cw1);
    this.checkRoundTrip(cw2);

    // Should not be equal as different nodes
    Assert.assertNotEquals(cw1, cw2);
  }
 protected void generateData(
     MapDriver<LongWritable, TripleWritable, LongWritable, QuadWritable> driver, int num) {
   for (int i = 0; i < num; i++) {
     Triple t =
         new Triple(
             NodeFactory.createURI("http://subjects/" + i),
             NodeFactory.createURI("http://predicate"),
             NodeFactory.createLiteral(Integer.toString(i), XSDDatatype.XSDinteger));
     Quad q = new Quad(t.getSubject(), t);
     driver.addInput(new LongWritable(i), new TripleWritable(t));
     driver.addOutput(new LongWritable(i), new QuadWritable(q));
   }
 }
Ejemplo n.º 4
0
 @Test
 public void testAddConstruct() {
   Triple t =
       new Triple(
           NodeFactory.createURI("one"),
           NodeFactory.createURI("two"),
           NodeFactory.createURI("three"));
   handler.addConstruct(t);
   Template template = query.getConstructTemplate();
   assertNotNull(template);
   List<Triple> lst = template.getTriples();
   assertEquals(1, lst.size());
   assertEquals(t, lst.get(0));
 }
Ejemplo n.º 5
0
  /**
   * Tests characteristic sets
   *
   * @throws IOException
   */
  @Test
  public void characteristic_set_writable_02() throws IOException {
    CharacteristicSetWritable set = new CharacteristicSetWritable();

    // Add some characteristics
    CharacteristicWritable cw1 =
        new CharacteristicWritable(NodeFactory.createURI("http://example.org"));
    CharacteristicWritable cw2 =
        new CharacteristicWritable(NodeFactory.createURI("http://example.org"), 2);
    set.add(cw1);
    set.add(cw2);
    this.checkCharacteristicSet(set, 1, new long[] {3});
    this.checkRoundTrip(set);
  }
Ejemplo n.º 6
0
  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")));
  }
Ejemplo n.º 7
0
  @Override
  public QueryIterator execEvaluated(
      final Binding binding,
      PropFuncArg argSubject,
      Node predicate,
      PropFuncArg argObject,
      ExecutionContext execCxt) {
    // check subject is a variable.
    if (!argSubject.getArg().isVariable()) throw new QueryExecException("Subject not a variable");

    final Var var = Var.alloc(argSubject.getArg());

    if (!argObject.getArg().isLiteral()) throw new QueryExecException("Subject not a literal");

    String searchTerm = argObject.getArg().getLiteralLexicalForm();
    Search search = searchEngine();
    Iterator<String> x = search.search(searchTerm);
    Iter<String> iter = Iter.iter(x);
    QueryIterator qIter =
        new QueryIterPlainWrapper(
            iter.map(
                (item) -> {
                  return BindingFactory.binding(binding, var, NodeFactory.createURI(item));
                }));
    return qIter;
  }
Ejemplo n.º 8
0
 /**
  * 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;
 }
Ejemplo n.º 9
0
  /**
   * Tests characteristic round tripping
   *
   * @throws IOException
   */
  @Test
  public void characteristic_writable_01() throws IOException {
    Node n = NodeFactory.createURI("http://example.org");
    CharacteristicWritable expected = new CharacteristicWritable(n);
    Assert.assertEquals(1, expected.getCount().get());

    this.checkRoundTrip(expected);
  }
  /**
   * Tests quads to triples conversion
   *
   * @throws IOException
   */
  @Test
  public void triples_to_quads_mapper_01() throws IOException {
    MapDriver<LongWritable, TripleWritable, LongWritable, QuadWritable> driver =
        this.getMapDriver();

    Triple t =
        new Triple(
            NodeFactory.createURI("http://s"),
            NodeFactory.createURI("http://p"),
            NodeFactory.createLiteral("test"));
    Quad q = new Quad(t.getSubject(), t);
    driver
        .withInput(
            new Pair<LongWritable, TripleWritable>(new LongWritable(1), new TripleWritable(t)))
        .withOutput(new Pair<LongWritable, QuadWritable>(new LongWritable(1), new QuadWritable(q)));
    driver.runTest();
  }
Ejemplo n.º 11
0
  @BeforeClass
  public static void setup() {
    // Dataset for implicit join tests
    implJoin = DatasetFactory.createTxnMem();

    Node a = NodeFactory.createURI("http://a");
    Node b = NodeFactory.createURI("http://b");
    Node c = NodeFactory.createURI("http://c");
    Node p1 = NodeFactory.createURI("http://p1");
    Node p2 = NodeFactory.createURI("http://p2");
    Node pSelf = NodeFactory.createURI("http://self");
    Node o = NodeFactory.createLiteral("object");

    DatasetGraph dsg = implJoin.asDatasetGraph();
    dsg.add(Quad.defaultGraphNodeGenerated, a, p1, o);
    dsg.add(Quad.defaultGraphNodeGenerated, a, p2, o);
    dsg.add(Quad.defaultGraphNodeGenerated, b, p1, o);
    dsg.add(Quad.defaultGraphNodeGenerated, b, p2, o);
    dsg.add(Quad.defaultGraphNodeGenerated, c, p1, o);
    // dsg.add(Quad.defaultGraphNodeGenerated, a, pSelf, a);

    // Currently these optimizations are off by default
    Assert.assertFalse(ARQ.isFalse(ARQ.optFilterImplicitJoin));
    Assert.assertFalse(ARQ.isFalse(ARQ.optImplicitLeftJoin));
  }
Ejemplo n.º 12
0
 private static Node createNode(String x) {
   try {
     IRI iri = resolver.resolve(x);
     return NodeFactory.createURI(iri.toString());
   } catch (Exception ex) {
     ServletOps.errorBadRequest("SPARQL Update: bad IRI: " + x);
     return null;
   }
 }
Ejemplo n.º 13
0
  /**
   * Tests characteristic sets
   *
   * @throws IOException
   */
  @Test
  public void characteristic_set_writable_03() throws IOException {
    CharacteristicSetWritable set1 = new CharacteristicSetWritable();
    CharacteristicSetWritable set2 = new CharacteristicSetWritable();

    // Add some characteristics
    CharacteristicWritable cw1 =
        new CharacteristicWritable(NodeFactory.createURI("http://example.org"));
    CharacteristicWritable cw2 =
        new CharacteristicWritable(NodeFactory.createURI("http://example.org/other"));
    set1.add(cw1);
    set2.add(cw2);
    this.checkCharacteristicSet(set1, 1, new long[] {1});
    this.checkCharacteristicSet(set2, 1, new long[] {1});
    this.checkRoundTrip(set1);
    this.checkRoundTrip(set2);

    Assert.assertNotEquals(set1, set2);
  }
Ejemplo n.º 14
0
  /**
   * Tests characteristic properties
   *
   * @throws IOException
   */
  @Test
  public void characteristic_writable_02() throws IOException {
    Node n = NodeFactory.createURI("http://example.org");
    CharacteristicWritable cw1 = new CharacteristicWritable(n);
    CharacteristicWritable cw2 = new CharacteristicWritable(n, 100);
    this.checkRoundTrip(cw1);
    this.checkRoundTrip(cw2);

    // Should still be equal since equality is only on the node not the
    // count
    Assert.assertEquals(cw1, cw2);
  }
Ejemplo n.º 15
0
  @Test
  public void testSetVars() {
    Var v = Var.alloc("v");
    Triple t = new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), v);
    handler.addConstruct(t);
    Template template = query.getConstructTemplate();
    assertNotNull(template);
    List<Triple> lst = template.getTriples();
    assertEquals(1, lst.size());
    assertEquals(t, lst.get(0));

    Map<Var, Node> values = new HashMap<Var, Node>();
    values.put(v, NodeFactory.createURI("three"));
    handler.setVars(values);

    template = query.getConstructTemplate();
    assertNotNull(template);
    lst = template.getTriples();
    assertEquals(1, lst.size());
    t =
        new Triple(
            NodeFactory.createURI("one"),
            NodeFactory.createURI("two"),
            NodeFactory.createURI("three"));
    assertEquals(t, lst.get(0));
  }
public class TestPrefixMappingOverDatasetPrefixes2 extends AbstractTestPrefixMapping2 {
  static Node gn = NodeFactory.createURI("http://test/graphName");
  DatasetPrefixesStorage2 dsgprefixes;

  @Override
  protected PrefixMapping create() {
    dsgprefixes = PrefixesFactory.newDatasetPrefixesMem();
    return view();
  }

  @Override
  protected PrefixMapping view() {
    PrefixMapStorage view = PrefixMapStorageView.viewGraph(dsgprefixes, gn);
    PrefixMapI pmap = PrefixesFactory.newPrefixMap(view);
    return PrefixesFactory.newPrefixMappingOverPrefixMapI(pmap);
  }
}
Ejemplo n.º 17
0
  @Override
  public void emitTriples(
      RdfEmitterContext emitterContext,
      Object entity,
      Node subject,
      Graph shapeGraph,
      Consumer<Triple> sink) {

    @SuppressWarnings("unchecked")
    Map<? super Object, ? super Object> map = createMapView.apply(entity);

    int i = 1;
    for (Entry<?, ?> e : map.entrySet()) {
      Object k = e.getKey();
      Object v = e.getValue();

      Node eNode = NodeFactory.createURI(subject.getURI() + "-" + i);

      // persistenceContext.
      // persistenceContext.entityFor(new TypedNode(rdfType, node));

      //            Node kNode = null; // emitterContext.getValueNode(entity,
      // propertyName)//RdfPersistenceContextImpl.getOrCreateRootNode(persistenceContext,
      // typeFactory, k);
      //            Node vNode = null;
      // //RdfPersistenceContextImpl.getOrCreateRootNode(persistenceContext, typeFactory, v);

      //            emitterContext.add(k, entity, "key" + i);
      //            emitterContext.add(v, entity, "value" + i);

      // Node keyNode = emitterContext.

      // Node kNode = emitterContext.getValueNode(entity, "key" + i, v);
      // Node vNode = emitterContext.getValueNode(entity, "value" + i, v);

      Node kNode = emitterContext.requestResolution(k);
      Node vNode = emitterContext.requestResolution(v);

      sink.accept(new Triple(subject, entry.asNode(), eNode));
      sink.accept(new Triple(eNode, key.asNode(), kNode));
      sink.accept(new Triple(eNode, value.asNode(), vNode));

      ++i;
    }
  }
 /**
  * Creates a set consisting of the given predicates
  *
  * @param predicates Predicates
  * @return Set
  */
 protected CharacteristicSetWritable createSet(
     MapReduceDriver<
             CharacteristicSetWritable,
             CharacteristicSetWritable,
             CharacteristicSetWritable,
             CharacteristicSetWritable,
             CharacteristicSetWritable,
             NullWritable>
         driver,
     int inputOccurrences,
     int outputOccurrences,
     String... predicates) {
   CharacteristicSetWritable set = new CharacteristicSetWritable();
   for (String predicateUri : predicates) {
     set.add(new CharacteristicWritable(NodeFactory.createURI(predicateUri)));
   }
   for (int i = 1; i <= inputOccurrences; i++) {
     driver.addInput(set, set);
   }
   for (int i = 1; i <= outputOccurrences; i++) {
     driver.addOutput(set, NullWritable.get());
   }
   return set;
 }
Ejemplo n.º 19
0
public class TestNodeAllocator extends BaseTest {
  static Node gragh1 = NodeFactory.createURI("g1");
  static Node gragh2 = NodeFactory.createURI("g2");

  // ---- Glaobl default policy needed for RDF parsing
  @Test
  public void allocOneScope1() {
    LabelToNode alloc = SyntaxLabels.createLabelToNode();
    Node b1 = alloc.get(gragh1, "xyz");
    Node b2 = alloc.get(gragh1, "xyz");
    // SAME
    assertEquals(b1, b2);
  }

  @Test
  public void allocOneScope2() {
    LabelToNode alloc = SyntaxLabels.createLabelToNode();
    Node b1 = alloc.get(gragh1, "xyz");
    Node b2 = alloc.get(gragh1, "123");
    // DIFFERENT
    assertNotEquals(b1, b2);
  }

  @Test
  public void allocOneScope3() {
    LabelToNode alloc = SyntaxLabels.createLabelToNode();
    Node b1 = alloc.get(gragh1, "xyz");
    Node b2 = alloc.get(gragh2, "xyz");
    // SAME
    assertEquals(b1, b2);
  }

  @Test
  public void allocOneScope4() {
    LabelToNode alloc = SyntaxLabels.createLabelToNode();
    Node b1 = alloc.get(null, "xyz");
    Node b2 = alloc.get(gragh2, "xyz");
    // SAME
    assertEquals(b1, b2);
  }

  @Test
  public void allocOneScope5() {
    LabelToNode alloc = SyntaxLabels.createLabelToNode();
    Node b1 = alloc.get(null, "xyz");
    Node b2 = alloc.get(null, "xyz");
    // SAME
    assertEquals(b1, b2);
  }

  // ---- Graph Scope
  @Test
  public void allocGraphScope1() {
    LabelToNode alloc = LabelToNode.createScopeByGraph();
    Node b1 = alloc.get(gragh1, "xyz");
    Node b2 = alloc.get(gragh1, "xyz");
    // SAME
    assertEquals(b1, b2);
    assertSame(b1, b2);
  }

  @Test
  public void allocGraphScope2() {
    LabelToNode alloc = LabelToNode.createScopeByGraph();
    Node b1 = alloc.get(gragh1, "xyz");
    Node b2 = alloc.get(gragh1, "123");
    // DIFFERENT
    assertNotEquals(b1, b2);
  }

  @Test
  public void allocGraphScope3() {
    LabelToNode alloc = LabelToNode.createScopeByGraph();
    Node b1 = alloc.get(gragh1, "xyz");
    Node b2 = alloc.get(gragh2, "xyz");
    // DIFFERENT
    assertNotEquals(b1, b2);
  }

  @Test
  public void allocGraphScope4() {
    LabelToNode alloc = SyntaxLabels.createLabelToNode();
    Node b1 = alloc.get(null, "xyz");
    Node b2 = alloc.get(gragh2, "xyz");
    // DIFFERENT
    assertEquals(b1, b2);
  }

  @Test
  public void allocGraphScope5() {
    LabelToNode alloc = SyntaxLabels.createLabelToNode();
    Node b1 = alloc.get(null, "xyz");
    Node b2 = alloc.get(null, "xyz");
    // SAME
    assertEquals(b1, b2);
  }
}
Ejemplo n.º 20
0
  /**
   * Instantiates a new sparql construct function.
   *
   * @param conf the graph with dataflow definition
   * @param confRoot the specific node in the graph representing the producer configuration
   * @param map the map to access the other defined producers
   * @see RDFProducer
   */
  public SparqlConstructFunction(Graph conf, Node confRoot, final ProducerMap map) {
    URI baseURI = URI.create(confRoot.getURI());

    Node inputNode = GraphUtils.getSingleValueOptProperty(conf, confRoot, DF.input.asNode());
    inputProducer =
        (inputNode != null) ? map.getProducer(inputNode) : EmptyGraphProducer.getInstance();

    Node queryAsInputNode = NodeFactory.createURI(baseURI.resolve("#query").toString());

    Node queryNode = GraphUtils.getSingleValueOptProperty(conf, confRoot, DF.configTxt.asNode());
    String queryTxt = (queryNode != null) ? queryNode.getLiteralLexicalForm() : null;

    Node configNode = GraphUtils.getSingleValueOptProperty(conf, confRoot, DF.config.asNode());
    RDFProducer configProducer = (configNode != null) ? map.getProducer(configNode) : null;

    Node configRootNode =
        GraphUtils.getSingleValueOptProperty(conf, confRoot, DF.configRoot.asNode());
    if (configRootNode == null) configRootNode = configNode;
    if (configRootNode == null && inputNode != null) {
      Iterator<Node> namedInputNodes =
          GraphUtils.getPropertyValues(conf, inputNode, DF.namedInput.asNode());
      while (namedInputNodes.hasNext()) {
        Node namedInputNode = namedInputNodes.next();
        if (conf.contains(namedInputNode, DF.id.asNode(), queryAsInputNode)) {
          configRootNode =
              GraphUtils.getSingleValueProperty(conf, namedInputNode, DF.input.asNode());
          break;
        }
      }
    }

    //		String configRootURI = (configRootNode != null && configRootNode.isURI()) ?
    // configRootNode.getURI() : null;

    queryProducer =
        new CoalesceQueryProducer(
            new GraphQueryProducer(
                new CoalesceGraphProducer(
                    configProducer, new SelectGraphProducer(inputProducer, queryAsInputNode)),
                configRootNode /*NodeFactory.createURI(baseURI.resolve("").toString())*/),
            new StringQueryProducer(queryTxt, baseURI.toString()));
    //		final Model confModel = ModelFactory.createModelForGraph(conf);
    //		Resource constructResource =
    //			confModel
    //					.getRDFNode(confRoot)
    //					.asResource()
    //					.getPropertyResourceValue(SPINX.config);
    //		Iterator<Producer> fromGraphProds =
    //			confModel
    //					.getRDFNode(confRoot)
    //					.asResource()
    //					.listProperties(SPINX.from)
    //					.mapWith(new Map1<Statement, Producer>() {
    //						@Override
    //						public Producer map1(Statement stmt) {
    //							return map.getProducer(stmt.getObject().asNode());
    //						}
    //					});
    // defaultProd = null;
    //		if (!fromGraphProds.hasNext()) {
    //			defaultProd = EmptyGraphProducer.getInstance();
    //		} else {
    //			defaultProd = fromGraphProds.next();
    //			if (fromGraphProds.hasNext()) {
    //				defaultProd =
    //					new UnionFunction(
    //							new ConcatenatedIterator<Producer>(
    //									new SingletonIterator<Producer>(defaultProd),
    //									fromGraphProds));
    //			}
    //		}
    //		Iterator<Node> fromNamedGraphNames =
    //			confModel
    //					.getRDFNode(confRoot)
    //					.asResource()
    //					.listProperties(SPINX.fromNamed)
    //					.mapWith(new Map1<Statement, Node>() {
    //						@Override
    //						public Node map1(Statement stmt) {
    //							return stmt.getObject().asNode();
    //						}
    //					});
    //		while (fromNamedGraphNames.hasNext()) {
    //			Node graphNames = fromNamedGraphNames.next();
    //			prodMap.put(graphNames, map.getProducer(graphNames));
    //		}

    //		query = QueryFactory.toQuery(conf, constructResource.asNode());
    //		if (query == null)
    //			throw new RuntimeException("Parsing Error");

    /*
    // debug
    Graph revEnginedGraph = SpinxFactory.fromQuery(query);
    System.out.println();
    System.out.println("**********************************");
    System.out.println("*** Reverse Engine query graph ***");
    System.out.println("**********************************");
    ModelFactory.createModelForGraph(revEnginedGraph).write(System.out,"N3");
    System.out.println("**********************************");
    System.out.println();
    */

    /*
    SPINFactory.asExpression(constructResource);
    query = ARQFactory.get().createQuery(SPINFactory.asQuery(constructResource));
    */
    /*
    Iterator<String> graphUris =
    	new ConcatenatedIterator<String>(
    			query.getGraphURIs().iterator(),
    			query.getNamedGraphURIs().iterator());
    while (graphUris.hasNext()) {
    	Node node = confModel.getResource(graphUris.next()).asNode();
    	prodMap.put(node, (GraphProducer) map.getProducer(node));
    }
    */
  }
Ejemplo n.º 21
0
 /**
  * Constructor
  *
  * @param securityEvaluator The security evaluator to use
  * @param graphIRI The IRI for the default graph.
  */
 public OpRewriter(final SecurityEvaluator securityEvaluator, final String graphIRI) {
   this(securityEvaluator, NodeFactory.createURI(graphIRI));
 }
Ejemplo n.º 22
0
  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;
  }
Ejemplo n.º 23
0
 public void testReifyAs() {
   Graph G = getGraph();
   Node X = NodeFactory.createURI("some:uri");
   assertEquals("node used", X, ReifierStd.reifyAs(G, X, triple("x R y")));
   assertEquals("retrieves correctly", triple("x R y"), ReifierStd.getTriple(G, X));
 }