org.apache.jena.sparql.engine.QueryIterator Java Examples

The following examples show how to use org.apache.jena.sparql.engine.QueryIterator. 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: QueryIterTripleStarPatternTest.java    From RDFstarTools with Apache License 2.0 6 votes vote down vote up
@Test
public void matchSubjectWithGivenObjectRequiresRedundancyAugmentation()
{
	// ?V1 ex:p ?V3
	final Triple tp = new Triple( $V1(), $p(), $V3() );

	// ?V3 --> ex:x1
	final Binding inputBinding = BindingFactory.binding( $V3(), $o() );
	final ExecutionContext execCxt = createTestExecCxt();
	final QueryIterator input = QueryIterSingleton.create(inputBinding, execCxt);

	final QueryIterator it = QueryIterTripleStarPattern.create( input, tp, execCxt );
	assertTrue( it.hasNext() );

	final Binding outputBinding = it.nextBinding();
	assertEquals( 2, outputBinding.size() );

	assertEquals( $s(), outputBinding.get($V1()) );
	assertEquals( $o(), outputBinding.get($V3()) );

	assertFalse( it.hasNext() ); 
	it.close();
}
 
Example #2
Source File: CSVTableTest.java    From tarql with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void testMultipleParallelIterators() throws IOException {
	CSVOptions options = new CSVOptions();
	options.setColumnNamesInFirstRow(false);
	CSVTable table = new CSVTable(InputStreamSource.fromString("Alice,Smith\nBob,Cook"), options);
	List<Var> vars = vars("a", "b", "ROWNUM");
	Binding row1 = binding(vars, "\"Alice\"", "\"Smith\"", "1");
	Binding row2 = binding(vars, "\"Bob\"", "\"Cook\"", "2");
	QueryIterator it1 = table.iterator(null);
	assertEquals(row1, it1.next());
	QueryIterator it2 = table.iterator(null);
	QueryIterator it3 = table.iterator(null);
	assertEquals(row1, it2.next());
	assertEquals(row2, it1.next());
	it2.close();
	assertEquals(row1, it3.next());
	assertEquals(row2, it3.next());
}
 
Example #3
Source File: StageGeneratorAlt.java    From xcurator with Apache License 2.0 6 votes vote down vote up
@Override
public QueryIterator execute(BasicPattern pattern, 
                             QueryIterator input,
                             ExecutionContext execCxt)
{
    // Just want to pick out some BGPs (e.g. on a particualr graph)
    // Test ::  execCxt.getActiveGraph() 
    if ( ! ( execCxt.getActiveGraph() instanceof GraphBase ) )
        // Example: pass on up to the original StageGenerator if
        // not based on GraphBase (which most Graph implementations are). 
        return other.execute(pattern, input, execCxt) ;
    
    System.err.println("MyStageGenerator.compile:: triple patterns = "+pattern.size()) ;

    // Stream the triple matches together, one triple matcher at a time. 
    QueryIterator qIter = input ;
    for (Triple triple : pattern.getList())
        qIter = new QueryIterTriplePattern(qIter, triple, execCxt) ;
    return qIter ;
}
 
Example #4
Source File: AlgebraEx.java    From xcurator with Apache License 2.0 6 votes vote down vote up
public static void main(String []args)
{
    String s = "SELECT DISTINCT ?s { ?s ?p ?o }";
    
    // Parse
    Query query = QueryFactory.create(s) ;
    System.out.println(query) ;
    
    // Generate algebra
    Op op = Algebra.compile(query) ;
    op = Algebra.optimize(op) ;
    System.out.println(op) ;
    
    // Execute it.
    QueryIterator qIter = Algebra.exec(op, ExQuerySelect1.createModel()) ;
    
    // Results
    for ( ; qIter.hasNext() ; )
    {
        Binding b = qIter.nextBinding() ;
        System.out.println(b) ;
    }
    qIter.close() ;
}
 
Example #5
Source File: localname.java    From xcurator with Apache License 2.0 6 votes vote down vote up
private QueryIterator execAllNodes(Var subjVar, Node nodeLocalname,  Binding input, ExecutionContext execCxt)
{
    if ( ! nodeLocalname.isVariable() )
    {
        if ( ! nodeLocalname.isLiteral() )
            // Not a variable, not a literal=> can't match
            return QueryIterNullIterator.create(execCxt) ;
    
        if( ! NodeUtils.isSimpleString(nodeLocalname) )
            return QueryIterNullIterator.create(execCxt) ;
    }
    
    //Set bindings = new HashSet() ;    // Use a Set if you want unique results. 
    List<Binding> bindings = new ArrayList<Binding>() ;   // Use a list if you want counting results. 
    Graph graph = execCxt.getActiveGraph() ;
    
    ExtendedIterator<Triple>iter = graph.find(Node.ANY, Node.ANY, Node.ANY) ;
    for ( ; iter.hasNext() ; )
    {
        Triple t = iter.next() ;
        slot(bindings, input, t.getSubject(),   subjVar, nodeLocalname) ;
        slot(bindings, input, t.getPredicate(), subjVar, nodeLocalname) ;
        slot(bindings, input, t.getObject(),    subjVar, nodeLocalname) ;
    }
    return new QueryIterPlainWrapper(bindings.iterator(), execCxt) ;
}
 
Example #6
Source File: localname.java    From xcurator with Apache License 2.0 6 votes vote down vote up
private QueryIterator execFixedSubject(Node nodeURI, Node nodeLocalname, Binding binding, ExecutionContext execCxt)
{
    if ( ! nodeURI.isURI() )
        // Subject bound but not a URI
        return QueryIterNullIterator.create(execCxt) ;

    // Subject is bound and a URI - get the localname as a Node 
    Node localname = NodeFactory.createLiteral(nodeURI.getLocalName()) ;
    
    // Object - unbound variable or a value? 
    if ( ! nodeLocalname.isVariable() )
    {
        // Object bound or a query constant.  Is it the same as the calculated value?
        if ( nodeLocalname.equals(localname) )
            // Same
            return QueryIterSingleton.create(binding, execCxt) ;
        // No - different - no match.
        return QueryIterNullIterator.create(execCxt) ;
    }
    
    // Object unbound variable - assign the localname to it.
    return QueryIterSingleton.create(binding, Var.alloc(nodeLocalname), localname, execCxt) ;
}
 
Example #7
Source File: labelSearch.java    From xcurator with Apache License 2.0 6 votes vote down vote up
private QueryIterator buildSyntax(QueryIterator input, Node nodeVar, String pattern, ExecutionContext execCxt)
{
    Var var2 = createNewVar() ; 
    // Triple patterns for   ?x rdfs:label ?hiddenVar
    ElementTriplesBlock elementBGP = new ElementTriplesBlock();
    Triple t = new Triple(nodeVar, RDFS.label.asNode(), var2) ;
    elementBGP.addTriple(t) ;
    
    // Regular expression for  regex(?hiddenVar, "pattern", "i") 
    Expr regex = new E_Regex(new ExprVar(var2.getName()), pattern, "i") ;
    
    ElementGroup elementGroup = new ElementGroup() ;
    elementGroup.addElement(elementBGP) ;
    elementGroup.addElement(new ElementFilter(regex)) ;
    // Compile it.
    // The better design is to build the Op structure programmatically,
    Op op = Algebra.compile(elementGroup) ;
    op = Algebra.optimize(op, execCxt.getContext()) ;
    return QC.execute(op, input, execCxt) ;
}
 
Example #8
Source File: TargetContainsPFunction.java    From shacl with Apache License 2.0 6 votes vote down vote up
@Override
public QueryIterator exec(Binding binding, PropFuncArg argSubject,
		Node predicate, PropFuncArg argObject, ExecutionContext execCxt) {

	argSubject = Substitute.substitute(argSubject, binding);
	argObject = Substitute.substitute(argObject, binding);
	
	if(!argObject.getArg().isVariable()) {
		throw new ExprEvalException("Right hand side of tosh:targetContains must be a variable");
	}
	
	Node targetNode = argSubject.getArgList().get(0);
	Node shapesGraphNode = argSubject.getArgList().get(1);
	
	Model currentModel = ModelFactory.createModelForGraph(execCxt.getActiveGraph());
	Dataset dataset = new DatasetWithDifferentDefaultModel(currentModel, DatasetImpl.wrap(execCxt.getDataset()));

	Model model = dataset.getNamedModel(shapesGraphNode.getURI());
	Resource target = (Resource) model.asRDFNode(targetNode);

	Set<Node> focusNodes = new HashSet<Node>();
	SHACLUtil.addNodesInTarget(target, dataset, focusNodes);
	return new QueryIterExtendByVar(binding, (Var) argObject.getArg(), focusNodes.iterator(), execCxt);
}
 
Example #9
Source File: QueryIterTripleStarPatternTest.java    From RDFstarTools with Apache License 2.0 6 votes vote down vote up
@Test
public void matchObjectRequiresRedundancyAugmentation()
{
	// ex:s ex:p ?V3
	final Triple tp = new Triple( $s(), $p(), $V3() );

	final Binding inputBinding = BindingFactory.binding();
	final ExecutionContext execCxt = createTestExecCxt();
	final QueryIterator input = QueryIterSingleton.create(inputBinding, execCxt);

	final QueryIterator it = QueryIterTripleStarPattern.create( input, tp, execCxt );
	assertTrue( it.hasNext() );

	final Binding outputBinding = it.nextBinding();
	assertEquals( 1, outputBinding.size() );

	assertEquals( $o(), outputBinding.get($V3()) );

	assertFalse( it.hasNext() ); 
	it.close();
}
 
Example #10
Source File: QueryIterTripleStarPatternTest.java    From RDFstarTools with Apache License 2.0 6 votes vote down vote up
@Test
public void matchNothingWithGivenObjectInObjectTriple()
{
	// ?V1 ?V2 << ex:s ex:p ?V3 >>
	final Triple tp1 = new Triple( $s(), $p(), $V3() );
	final Node nTP1 = new Node_TripleStarPattern( tp1 );
	final Triple tp2 = new Triple( $V1(), $V2(), nTP1 );

	// ?V3 --> ex:x1
	final Binding inputBinding = BindingFactory.binding( $V3(), $x1() );
	final ExecutionContext execCxt = createTestExecCxt();
	final QueryIterator input = QueryIterSingleton.create(inputBinding, execCxt);

	final QueryIterator it = QueryIterTripleStarPattern.create( input, tp2, execCxt );
	assertFalse( it.hasNext() ); 
	it.close();
}
 
Example #11
Source File: QueryIterTripleStarPatternTest.java    From RDFstarTools with Apache License 2.0 6 votes vote down vote up
@Test
public void matchSubjectInSubjectTripleWithGivenObjectInMetaTriple()
{
	// << ?V1 ex:p ex:o >> ex:m1 ?V2
	final Triple tp1 = new Triple( $V1(), $p(), $o() );
	final Node nTP1 = new Node_TripleStarPattern( tp1 );
	final Triple tp2 = new Triple( nTP1, $m1(), $V2() );

	// ?V2 --> ex:x1
	final Binding inputBinding = BindingFactory.binding( $V2(), $x1() );
	final ExecutionContext execCxt = createTestExecCxt();
	final QueryIterator input = QueryIterSingleton.create(inputBinding, execCxt);

	final QueryIterator it = QueryIterTripleStarPattern.create( input, tp2, execCxt );
	assertTrue( it.hasNext() );

	final Binding outputBinding = it.nextBinding();
	assertEquals( 2, outputBinding.size() );

	assertEquals( $s(), outputBinding.get($V1()) );
	assertEquals( $x1(), outputBinding.get($V2()) );

	assertFalse( it.hasNext() ); 
	it.close();
}
 
Example #12
Source File: QueryIterTripleStarPattern.java    From RDFstarTools with Apache License 2.0 6 votes vote down vote up
static public QueryIterator create( QueryIterator input,
                                       Triple tp,
                                       ExecutionContext cxt )
{
	if (   tp.getSubject() instanceof Node_Triple
	    || tp.getObject()  instanceof Node_Triple
		|| tp.getSubject().isVariable()
		|| tp.getObject().isVariable() )
	{
		return new QueryIterTripleStarPattern(input, tp, cxt);
	}
	else
	{
		return new QueryIterTriplePattern(input, tp, cxt);
	}
}
 
Example #13
Source File: QueryIterTripleStarPatternTest.java    From RDFstarTools with Apache License 2.0 6 votes vote down vote up
@Test
public void matchSubjectInSubjectTriple()
{
	// << ?V1 ex:p ex:o >> ex:m1 ex:x1
	final Triple tp1 = new Triple( $V1(), $p(), $o() );
	final Node nTP1 = new Node_TripleStarPattern( tp1 );
	final Triple tp2 = new Triple( nTP1, $m1(), $x1() );

	final Binding inputBinding = BindingFactory.binding();
	final ExecutionContext execCxt = createTestExecCxt();
	final QueryIterator input = QueryIterSingleton.create(inputBinding, execCxt);

	final QueryIterator it = QueryIterTripleStarPattern.create( input, tp2, execCxt );
	assertTrue( it.hasNext() );

	final Binding outputBinding = it.nextBinding();
	assertEquals( 1, outputBinding.size() );
	assertEquals( $V1(), outputBinding.vars().next() );

	assertEquals( $s(), outputBinding.get($V1()) );

	assertFalse( it.hasNext() ); 
	it.close();
}
 
Example #14
Source File: QueryIterTripleStarPatternTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@Test
public void createWithNestedTP2()
{
	final Node u = NodeFactory.createURI("http://example.com/i");
	final Node n1 = new Node_TripleStarPattern( new Triple(u,u,u) );
	final Triple tp = new Triple(n1, u, u);
	final QueryIterator input = new QueryIterNullIterator(null);

	final QueryIterator it = QueryIterTripleStarPattern.create(input, tp, null);
	assertTrue( it instanceof QueryIterTripleStarPattern );
	it.close();
}
 
Example #15
Source File: CSVTable.java    From tarql with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public QueryIterator iterator(ExecutionContext ctxt) {
	// QueryIteratorPlainWrapper doesn't close wrapped 
	// ClosableIterators, so we do that ourselves.
	final ClosableIterator<Binding> wrapped = rows();
	return new QueryIterPlainWrapper(wrapped, ctxt) {
		@Override
		protected void closeIterator() {
			super.closeIterator();
			wrapped.close();
		}
	};
}
 
Example #16
Source File: QueryIterTripleStarPattern.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
protected QueryIterTripleStarPattern( QueryIterator input,
                                      Triple tp,
                                      ExecutionContext cxt )
{
    super(input, cxt);
    this.tp = Node_TripleStarPattern.asTripleWithNode_TripleStarPatterns(tp);
}
 
Example #17
Source File: MyQueryEngine.java    From xcurator with Apache License 2.0 5 votes vote down vote up
@Override
public QueryIterator eval(Op op, DatasetGraph dsg, Binding initial, Context context)
{
    // Extension point: access possible to all the parameters for execution.
    // Be careful to deal with initial bindings.
    Transform transform = new MyTransform() ;
    op = Transformer.transform(transform, op) ;
    return super.eval(op, dsg, initial, context) ;
}
 
Example #18
Source File: QueryIterTripleStarPatternTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@Test
public void createWithoutNestedTP()
{
	final Node u = NodeFactory.createURI("http://example.com/i");
	final Triple tp = new Triple( u, u, u );
	final QueryIterator input = new QueryIterNullIterator(null);

	final QueryIterator it = QueryIterTripleStarPattern.create(input, tp, null);
	assertTrue( it instanceof QueryIterTriplePattern );
	it.close();
}
 
Example #19
Source File: QueryIterTripleStarPatternTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@Test
public void createWithNestedTP1()
{
	final Node u = NodeFactory.createURI("http://example.com/i");
	final Node n1 = new Node_TripleStarPattern( new Triple(u,u,u) );
	final Triple tp = new Triple(n1, u, u);
	final QueryIterator input = new QueryIterNullIterator(null);

	final QueryIterator it = QueryIterTripleStarPattern.create(input, tp, null);
	assertTrue( it instanceof QueryIterTripleStarPattern );
	it.close();
}
 
Example #20
Source File: localname.java    From xcurator with Apache License 2.0 5 votes vote down vote up
@Override
public QueryIterator execEvaluated(Binding binding, Node nodeURI, Node predicate, Node nodeLocalname, ExecutionContext execCxt)
{
    if ( ! nodeURI.isVariable() )
        return execFixedSubject(nodeURI, nodeLocalname, binding, execCxt) ;
    else
        return execAllNodes(Var.alloc(nodeURI), nodeLocalname, binding, execCxt) ;
}
 
Example #21
Source File: QueryIterTripleStarPatternTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@Test
public void matchSubjectInSubjectTripleAndObjectInMetaTriple()
{
	// << ?V1 ex:p ex:o >> ex:m1 ?V2
	final Triple tp1 = new Triple( $V1(), $p(), $o() );
	final Node nTP1 = new Node_TripleStarPattern( tp1 );
	final Triple tp2 = new Triple( nTP1, $m1(), $V2() );

	final Binding inputBinding = BindingFactory.binding();
	final ExecutionContext execCxt = createTestExecCxt();
	final QueryIterator input = QueryIterSingleton.create(inputBinding, execCxt);

	final QueryIterator it = QueryIterTripleStarPattern.create( input, tp2, execCxt );
	assertTrue( it.hasNext() );

	Binding outputBinding = it.nextBinding();
	assertEquals( 2, outputBinding.size() );

	assertEquals( $s(), outputBinding.get($V1()) );
	assertEquals( $x2(), outputBinding.get($V2()) );

	outputBinding = it.nextBinding();
	assertEquals( 2, outputBinding.size() );

	assertEquals( $s(), outputBinding.get($V1()) );
	assertEquals( $x1(), outputBinding.get($V2()) );

	assertFalse( it.hasNext() ); 
	it.close();
}
 
Example #22
Source File: QueryIterTripleStarPatternTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@Test
public void matchWholeSubjectTriple()
{
	// ?V1 ex:m1 ex:x1
	final Triple tp = new Triple( $V1(), $m1(), $x1() );

	final ExecutionContext execCxt = createTestExecCxt();
	final Binding inputBinding = BindingFactory.binding();
	final QueryIterator input = QueryIterSingleton.create(inputBinding, execCxt);

	final QueryIterator it = QueryIterTripleStarPattern.create( input, tp, execCxt );
	assertTrue( it.hasNext() );

	final Binding outputBinding = it.nextBinding();
	assertEquals( 1, outputBinding.size() );
	assertEquals( $V1(), outputBinding.vars().next() );

	final Node outputValue = outputBinding.get( $V1() );  
	assertTrue( outputValue instanceof Node_Triple );

	final Triple outputTriple = ( (Node_Triple) outputValue ).get();
	assertEquals( $s(), outputTriple.getSubject() );
	assertEquals( $p(), outputTriple.getPredicate() );
	assertEquals( $o(), outputTriple.getObject() );

	assertFalse( it.hasNext() ); 
	it.close();
}
 
Example #23
Source File: StageGeneratorSPARQLStar.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@Override
protected QueryIterator execute(BasicPattern pattern,
                                ReorderTransformation reorder,
                                StageGenerator execution,
                                QueryIterator input,
                                ExecutionContext execCxt)
{
	// The implementation of this method is a copy of the
	// superclass method that is modified to use SPARQL*-aware
	// versions of Substitute and QueryIterBlockTriples.

       Explain.explain(pattern, execCxt.getContext());

       if ( ! input.hasNext() )
           return input;

       if ( reorder != null && pattern.size() >= 2 ) {
           // If pattern size is 0 or 1, nothing to do.
           BasicPattern bgp2 = pattern;

           // Try to ground the pattern
           if ( ! input.isJoinIdentity() ) {
               QueryIterPeek peek = QueryIterPeek.create(input, execCxt);
               // And now use this one
               input = peek;
               Binding b = peek.peek();
               bgp2 = ExtendedSubstitute.substitute(pattern, b);
           }
           ReorderProc reorderProc = reorder.reorderIndexes(bgp2);
           pattern = reorderProc.reorder(pattern);
       }
       Explain.explain("Reorder/generic", pattern, execCxt.getContext());

       QueryIterator chain = input;
       for ( final Triple triple : pattern )
           chain = QueryIterTripleStarPattern.create(chain, triple, execCxt);
       return chain;
}
 
Example #24
Source File: QueryIterTripleStarPatternTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@Test
public void matchWholeObjectTriple()
{
	// ex:x1 ex:m1 ?V1
	final Triple tp = new Triple( $x1(), $m1(), $V1() );

	final ExecutionContext execCxt = createTestExecCxt();
	final Binding inputBinding = BindingFactory.binding();
	final QueryIterator input = QueryIterSingleton.create(inputBinding, execCxt);

	final QueryIterator it = QueryIterTripleStarPattern.create( input, tp, execCxt );
	assertTrue( it.hasNext() );

	final Binding outputBinding = it.nextBinding();
	assertEquals( 1, outputBinding.size() );
	assertEquals( $V1(), outputBinding.vars().next() );

	final Node outputValue = outputBinding.get( $V1() );  
	assertTrue( outputValue instanceof Node_Triple );

	final Triple outputTriple = ( (Node_Triple) outputValue ).get();
	assertEquals( $s(), outputTriple.getSubject() );
	assertEquals( $p(), outputTriple.getPredicate() );
	assertEquals( $o(), outputTriple.getObject() );

	assertFalse( it.hasNext() ); 
	it.close();
}
 
Example #25
Source File: QueryIterTripleStarPatternTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@Test
public void matchSubjectInMetaTripleWithGivenSubjectInObjectTriple()
{
	// ?V1 ?V2 << ?V3 ex:p ex:o >>
	final Triple tp1 = new Triple( $s(), $p(), $V3() );
	final Node nTP1 = new Node_TripleStarPattern( tp1 );
	final Triple tp2 = new Triple( $V1(), $V2(), nTP1 );

	// ?V2 --> ex:m1, ?V3 --> ex:s
	final Binding inputBindingX = BindingFactory.binding( $V2(), $m1() );
	final Binding inputBinding = BindingFactory.binding( inputBindingX, $V3(), $o() );
	final ExecutionContext execCxt = createTestExecCxt();
	final QueryIterator input = QueryIterSingleton.create(inputBinding, execCxt);

	final QueryIterator it = QueryIterTripleStarPattern.create( input, tp2, execCxt );
	assertTrue( it.hasNext() );

	final Binding outputBinding = it.nextBinding();
	assertEquals( 3, outputBinding.size() );

	assertEquals( $x1(), outputBinding.get($V1()) );
	assertEquals( $m1(), outputBinding.get($V2()) );
	assertEquals( $o(), outputBinding.get($V3()) );

	assertFalse( it.hasNext() ); 
	it.close();
}
 
Example #26
Source File: QueryIterTripleStarPatternTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@Test
public void matchSubjectInMetaTripleWithGivenObjectInObjectTriple()
{
	// ?V1 ?V2 << ex:s ex:p ?V3 >>
	final Triple tp1 = new Triple( $s(), $p(), $V3() );
	final Node nTP1 = new Node_TripleStarPattern( tp1 );
	final Triple tp2 = new Triple( $V1(), $V2(), nTP1 );

	// ?V3 --> ex:o
	final Binding inputBinding = BindingFactory.binding( $V3(), $o() );
	final ExecutionContext execCxt = createTestExecCxt();
	final QueryIterator input = QueryIterSingleton.create(inputBinding, execCxt);

	final QueryIterator it = QueryIterTripleStarPattern.create( input, tp2, execCxt );
	assertTrue( it.hasNext() );

	Binding outputBinding = it.nextBinding();
	assertEquals( 3, outputBinding.size() );

	assertEquals( $x2(), outputBinding.get($V1()) );
	assertEquals( $m2(), outputBinding.get($V2()) );
	assertEquals( $o(), outputBinding.get($V3()) );

	outputBinding = it.nextBinding();
	assertEquals( 3, outputBinding.size() );

	assertEquals( $x1(), outputBinding.get($V1()) );
	assertEquals( $m1(), outputBinding.get($V2()) );
	assertEquals( $o(), outputBinding.get($V3()) );

	assertFalse( it.hasNext() ); 
	it.close();
}
 
Example #27
Source File: QueryIterTripleStarPatternTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@Test
public void matchMetaTriplesBasedOnWholeSubjectTriple()
{
	//  << ex:s ex:p ex:o >> ?V1 ?V2
	final Triple tp1 = new Triple( $s(), $p(), $o() );
	final Node nTP1 = new Node_TripleStarPattern( tp1 );
	final Triple tp2 = new Triple( nTP1, $V1(), $V2() );

	final Binding inputBinding = BindingFactory.binding();
	final ExecutionContext execCxt = createTestExecCxt();
	final QueryIterator input = QueryIterSingleton.create(inputBinding, execCxt);

	final QueryIterator it = QueryIterTripleStarPattern.create( input, tp2, execCxt );
	assertTrue( it.hasNext() );

	Binding outputBinding = null;

	outputBinding = it.nextBinding();
	assertEquals( 2, outputBinding.size() );

	assertEquals( $m1(), outputBinding.get($V1()) );
	assertEquals( $x2(), outputBinding.get($V2()) );

	outputBinding = it.nextBinding();
	assertEquals( 2, outputBinding.size() );

	assertEquals( $m1(), outputBinding.get($V1()) );
	assertEquals( $x1(), outputBinding.get($V2()) );

	assertFalse( it.hasNext() ); 
	it.close();
}
 
Example #28
Source File: labelSearch.java    From xcurator with Apache License 2.0 4 votes vote down vote up
@Override
public QueryIterator exec(QueryIterator input, PropFuncArg argSubject, Node predicate, PropFuncArg argObject, ExecutionContext execCxt)
{
    // No real need to check the pattern arguments because
    // the replacement triple pattern and regex will cope
    // but we illustrate testing here.

    Node nodeVar = argSubject.getArg() ;
    String pattern = NodeUtils.stringLiteral(argObject.getArg()) ;
    if ( pattern == null )
    {
        Log.warn(this, "Pattern must be a plain literal or xsd:string: "+argObject.getArg()) ;
        return QueryIterNullIterator.create(execCxt) ;
    }

    if ( false )
        // Old (ARQ 1) way - not recommended.
        return buildSyntax(input, nodeVar, pattern, execCxt) ;
    
    // Better 
    // Build a SPARQL algebra expression
    Var var2 = createNewVar() ;                     // Hidden variable
    
    BasicPattern bp = new BasicPattern() ;
    Triple t = new Triple(nodeVar, RDFS.label.asNode(), var2) ;
    bp.add(t) ;
    OpBGP op = new OpBGP(bp) ;
    
    Expr regex = new E_Regex(new ExprVar(var2.getName()), pattern, "i") ;
    Op filter = OpFilter.filter(regex, op) ;

    // ---- Evaluation
    if ( true )
    {
        // Use the reference query engine
        // Create a table for the input stream (so it uses working memory at this point, 
        // which is why this is not the preferred way).  
        // Then join to expression for this stage.
        Table table = TableFactory.create(input) ;
        Op op2 = OpJoin.create(OpTable.create(table), filter) ;
        return Algebra.exec(op2, execCxt.getDataset()) ;
    }        
    
    // Use the default, optimizing query engine.
    return QC.execute(filter, input, execCxt) ;
}
 
Example #29
Source File: AlgebraExec.java    From xcurator with Apache License 2.0 4 votes vote down vote up
public static void main (String[] argv)
{
    String BASE = "http://example/" ; 
    BasicPattern bp = new BasicPattern() ;
    Var var_x = Var.alloc("x") ;
    Var var_z = Var.alloc("z") ;
    
    // ---- Build expression
    bp.add(new Triple(var_x, NodeFactory.createURI(BASE+"p"), var_z)) ;
    Op op = new OpBGP(bp) ;
    //Expr expr = ExprUtils.parse("?z < 2 ") ;
    Expr expr = new E_LessThan(new ExprVar(var_z), NodeValue.makeNodeInteger(2)) ;
    op = OpFilter.filter(expr, op) ;

    // ---- Example setup
    Model m = makeModel() ;
    m.write(System.out, "TTL") ;
    System.out.println("--------------") ;
    System.out.print(op) ;
    System.out.println("--------------") ;

    // ---- Execute expression
    QueryIterator qIter = Algebra.exec(op, m.getGraph()) ;
    
    // -------- Either read the query iterator directly ...
    if ( false )
    {
        for ( ; qIter.hasNext() ; )
        {
            Binding b = qIter.nextBinding() ;
            Node n = b.get(var_x) ;
            System.out.println(NodeFmtLib.displayStr(n)) ;
            System.out.println(b) ; 
        }
        qIter.close() ;
    }
    else
    {
        // -------- Or make ResultSet from it (but not both - reading an
        //          iterator consumes the current solution)
        List<String> varNames = new ArrayList<String>() ;
        varNames.add("x") ;
        varNames.add("z") ;
        ResultSet rs = new ResultSetStream(varNames, m, qIter);
        ResultSetFormatter.out(rs) ;
        qIter.close() ;
    }
    System.exit(0) ;
}
 
Example #30
Source File: EvalExprPFunction.java    From shacl with Apache License 2.0 4 votes vote down vote up
@Override
public QueryIterator exec(Binding binding, PropFuncArg argSubject,
		Node predicate, PropFuncArg argObject, ExecutionContext execCxt) {

	argSubject = Substitute.substitute(argSubject, binding);
	argObject = Substitute.substitute(argObject, binding);
	
	if(!argObject.getArg().isVariable()) {
		throw new ExprEvalException("Right hand side of tosh:exprEval must be a variable");
	}
	
	Node exprNode = argSubject.getArgList().get(0);
	Node focusNode = argSubject.getArgList().get(1);
	
	Model model = ModelFactory.createModelForGraph(execCxt.getActiveGraph());
	Dataset dataset = ARQFactory.get().getDataset(model);
	URI shapesGraphURI = URI.create("urn:x-topbraid:dummyShapesGraph");
	dataset.addNamedModel(shapesGraphURI.toString(), model);
	
	ShapesGraph[] shapesGraph = new ShapesGraph[1];
	
	NodeExpression n = NodeExpressionFactory.get().create(model.asRDFNode(exprNode));
	ExtendedIterator<RDFNode> it = n.eval(model.asRDFNode(focusNode), new NodeExpressionContext() {
		
		@Override
		public URI getShapesGraphURI() {
			return shapesGraphURI;
		}
		
		@Override
		public ShapesGraph getShapesGraph() {
			if(shapesGraph[0] == null) {
				shapesGraph[0] = new ShapesGraph(model);
			}
			return shapesGraph[0];
		}
		
		@Override
		public Dataset getDataset() {
			return dataset;
		}
	});
	
	Iterator<Node> nit = it.mapWith(rdfNode -> rdfNode.asNode());

	return new QueryIterExtendByVar(binding, (Var) argObject.getArg(), nit, execCxt);
}