Esempio n. 1
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));
   }
 }
Esempio n. 3
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);
  }
Esempio 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));
 }
Esempio n. 5
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;
  }
Esempio n. 6
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();
  }
Esempio n. 8
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;
  }
Esempio n. 9
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;
   }
 }
Esempio n. 10
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);
 }
Esempio n. 11
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);
  }
Esempio n. 12
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);
  }
Esempio n. 13
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));
  }
Esempio n. 14
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));
  }
Esempio n. 15
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")));
  }
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);
  }
}
Esempio 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;
 }
Esempio n. 19
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));
 }
Esempio n. 20
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));
 }
Esempio n. 21
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));
    }
    */
  }
Esempio n. 22
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);
  }
}