Java Code Examples for org.eclipse.rdf4j.query.algebra.Extension#getArg()

The following examples show how to use org.eclipse.rdf4j.query.algebra.Extension#getArg() . 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: SparqlToPipelineTransformVisitor.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void meet(Extension extensionNode) throws Exception {
    extensionNode.visitChildren(this);
    if (extensionNode.getArg() instanceof AggregationPipelineQueryNode && extensionNode.getParentNode() != null) {
        AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) extensionNode.getArg();
        if (pipelineNode.extend(extensionNode.getElements())) {
            extensionNode.replaceWith(pipelineNode);
        }
    }
}
 
Example 2
Source File: TestSparqlStarParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testUseInValues() throws Exception {
	String simpleSparqlQuery = "SELECT ?ref WHERE { values ?ref {<<<urn:A> <urn:B> 1>>} }";

	ParsedQuery q = parser.parseQuery(simpleSparqlQuery, null);

	assertNotNull(q);
	assertTrue("expect projection", q.getTupleExpr() instanceof Projection);
	Projection proj = (Projection) q.getTupleExpr();

	assertTrue("expect extension", proj.getArg() instanceof Extension);
	Extension ext = (Extension) proj.getArg();

	assertTrue("single extention elemrnt", ext.getElements().size() == 1);
	ExtensionElem elem = ext.getElements().get(0);

	assertEquals("name should match", elem.getName(), "ref");
	assertTrue("expect Var in extention element", elem.getExpr() instanceof Var);
	assertEquals("names should match", elem.getName(), ((Var) elem.getExpr()).getName());

	assertTrue("expect BindingSetAssignment as arg", ext.getArg() instanceof BindingSetAssignment);
	BindingSetAssignment values = (BindingSetAssignment) ext.getArg();
	boolean oneValue[] = new boolean[] { false };
	values.getBindingSets().forEach(bs -> {
		Value v = bs.getValue("ref");
		assertTrue("expect binding for ref", v != null);
		assertTrue("expect Triple ", v instanceof Triple);
		Triple triple = (Triple) v;
		assertTrue("subject should be IRI", triple.getSubject() instanceof IRI);
		assertEquals("subject should match", "urn:A", triple.getSubject().toString());

		assertTrue("predicate should be IRI", triple.getPredicate() instanceof IRI);
		assertEquals("predicate should match", "urn:B", triple.getPredicate().toString());

		assertTrue("object should be Literal", triple.getObject() instanceof Literal);
		assertEquals("object should match", 1, ((Literal) triple.getObject()).intValue());

		assertTrue("expect one value", oneValue[0] == false);
		oneValue[0] = true;
	});
	assertTrue("expect one value", oneValue[0]);
}
 
Example 3
Source File: TestSparqlStarParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testUseInConstructFromStatementPattern() throws Exception {
	String simpleSparqlQuery = "CONSTRUCT {<<?s ?p ?o>> <urn:pred> <urn:value>} WHERE {?s ?p ?o}";

	ParsedQuery q = parser.parseQuery(simpleSparqlQuery, null);

	assertNotNull(q);
	assertTrue("expect Reduced", q.getTupleExpr() instanceof Reduced);
	assertTrue("expect projection", ((Reduced) q.getTupleExpr()).getArg() instanceof Projection);
	Projection proj = (Projection) ((Reduced) q.getTupleExpr()).getArg();

	List<ProjectionElem> list = proj.getProjectionElemList().getElements();
	final ArrayList<String> listTargetNames = new ArrayList<>();
	list.forEach(el -> {
		listTargetNames.add(el.getTargetName());
	});
	assertEquals("expect all bindings", 3, list.size());
	assertTrue("expect target subject", listTargetNames.contains("subject"));
	assertTrue("expect target predicate", listTargetNames.contains("predicate"));
	assertTrue("expect target oobject", listTargetNames.contains("object"));

	final ArrayList<String> listSourceNames = new ArrayList<>();
	list.forEach(el -> {
		listSourceNames.add(el.getSourceName());
	});

	assertTrue("expect extension", proj.getArg() instanceof Extension);
	Extension ext = (Extension) proj.getArg();
	assertTrue("three extention elements", ext.getElements().size() == 3);
	ExtensionElem elem = ext.getElements().get(0);

	assertEquals("anon name should match first", elem.getName(), listSourceNames.get(0));

	assertTrue("expect ValueExprTripleRef in extention element", elem.getExpr() instanceof ValueExprTripleRef);
	ValueExprTripleRef ref = (ValueExprTripleRef) elem.getExpr();
	assertEquals("subject var name", "s", ref.getSubjectVar().getName());
	assertEquals("predicate var name", "p", ref.getPredicateVar().getName());
	assertEquals("object var name", "o", ref.getObjectVar().getName());

	elem = ext.getElements().get(1);
	assertEquals("names should match", elem.getName(), listSourceNames.get(1));
	assertEquals("value should match", "urn:pred", ((ValueConstant) elem.getExpr()).getValue().toString());

	elem = ext.getElements().get(2);
	assertEquals("names should match", elem.getName(), listSourceNames.get(2));
	assertEquals("value should match", "urn:value", ((ValueConstant) elem.getExpr()).getValue().toString());

	assertTrue("expect StatementPattern", ext.getArg() instanceof StatementPattern);
	StatementPattern pattern = (StatementPattern) ext.getArg();

	assertEquals("subj var name should match", "s", pattern.getSubjectVar().getName());
	assertEquals("pred var name should match", "p", pattern.getPredicateVar().getName());
	assertEquals("obj var name should match", "o", pattern.getObjectVar().getName());
}
 
Example 4
Source File: TestSparqlStarParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testUseInInsertFromStatementPattern() throws Exception {
	String simpleSparqlQuery = "Insert {<<?s ?p ?o>> <urn:pred> <urn:value>} WHERE {?s ?p ?o}";

	ParsedUpdate q = parser.parseUpdate(simpleSparqlQuery, null);

	assertNotNull(q);
	assertTrue("expect single UpdateExpr", q.getUpdateExprs().size() == 1);
	UpdateExpr updateExpr = q.getUpdateExprs().get(0);
	assertTrue("expect Modify UpdateExpr", updateExpr instanceof Modify);
	Modify modify = (Modify) updateExpr;
	assertTrue("expect no DELETE", modify.getDeleteExpr() == null);

	assertTrue("expect INSERT", modify.getInsertExpr() != null);
	assertTrue("expect INSERT as statamentPattern", modify.getInsertExpr() instanceof StatementPattern);
	StatementPattern insert = (StatementPattern) modify.getInsertExpr();
	String anonVar = insert.getSubjectVar().getName();
	assertEquals("expect predicate", "urn:pred", insert.getPredicateVar().getValue().toString());
	assertEquals("expect object", "urn:value", insert.getObjectVar().getValue().toString());

	assertTrue("expect WHERE", modify.getWhereExpr() != null);
	assertTrue("expect WHERE as extension", modify.getWhereExpr() instanceof Extension);

	Extension where = (Extension) modify.getWhereExpr();

	Extension ext = (Extension) where;
	assertTrue("one extention element", ext.getElements().size() == 1);
	ExtensionElem elem = ext.getElements().get(0);

	assertEquals("anon name should match first", elem.getName(), anonVar);

	assertTrue("expect ValueExprTripleRef in extention element", elem.getExpr() instanceof ValueExprTripleRef);
	ValueExprTripleRef ref = (ValueExprTripleRef) elem.getExpr();
	assertEquals("subject var name", "s", ref.getSubjectVar().getName());
	assertEquals("predicate var name", "p", ref.getPredicateVar().getName());
	assertEquals("object var name", "o", ref.getObjectVar().getName());

	assertTrue("expect StatementPattern as extension argument", ext.getArg() instanceof StatementPattern);
	StatementPattern pattern = (StatementPattern) ext.getArg();
	assertEquals("subject var name should match", pattern.getSubjectVar().getName(), ref.getSubjectVar().getName());
	assertEquals("predicate var name should match", pattern.getPredicateVar().getName(),
			ref.getPredicateVar().getName());
	assertEquals("object var name should match", pattern.getObjectVar().getName(), ref.getObjectVar().getName());

}
 
Example 5
Source File: TestSparqlStarParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testUseInDeleteFromStatementPattern() throws Exception {
	String simpleSparqlQuery = "DELETE {<<?s ?p ?o>> <urn:pred> <urn:value>} WHERE {?s ?p ?o}";

	ParsedUpdate q = parser.parseUpdate(simpleSparqlQuery, null);

	assertNotNull(q);
	assertTrue("expect single UpdateExpr", q.getUpdateExprs().size() == 1);
	UpdateExpr updateExpr = q.getUpdateExprs().get(0);
	assertTrue("expect Modify UpdateExpr", updateExpr instanceof Modify);
	Modify modify = (Modify) updateExpr;
	assertTrue("expect no INSERT", modify.getInsertExpr() == null);

	assertTrue("expect DELETE", modify.getDeleteExpr() != null);
	assertTrue("expect DETELE as statamentPattern", modify.getDeleteExpr() instanceof StatementPattern);
	StatementPattern insert = (StatementPattern) modify.getDeleteExpr();
	String anonVar = insert.getSubjectVar().getName();
	assertEquals("expect predicate", "urn:pred", insert.getPredicateVar().getValue().toString());
	assertEquals("expect object", "urn:value", insert.getObjectVar().getValue().toString());

	assertTrue("expect WHERE", modify.getWhereExpr() != null);
	assertTrue("expect WHERE as extension", modify.getWhereExpr() instanceof Extension);

	Extension where = (Extension) modify.getWhereExpr();

	Extension ext = (Extension) where;
	assertTrue("one extention element", ext.getElements().size() == 1);
	ExtensionElem elem = ext.getElements().get(0);

	assertEquals("anon name should match first", elem.getName(), anonVar);

	assertTrue("expect ValueExprTripleRef in extention element", elem.getExpr() instanceof ValueExprTripleRef);
	ValueExprTripleRef ref = (ValueExprTripleRef) elem.getExpr();
	assertEquals("subject var name", "s", ref.getSubjectVar().getName());
	assertEquals("predicate var name", "p", ref.getPredicateVar().getName());
	assertEquals("object var name", "o", ref.getObjectVar().getName());

	assertTrue("expect StatementPattern as extension argument", ext.getArg() instanceof StatementPattern);
	StatementPattern pattern = (StatementPattern) ext.getArg();
	assertEquals("subject var name should match", pattern.getSubjectVar().getName(), ref.getSubjectVar().getName());
	assertEquals("predicate var name should match", pattern.getPredicateVar().getName(),
			ref.getPredicateVar().getName());
	assertEquals("object var name should match", pattern.getObjectVar().getName(), ref.getObjectVar().getName());

}
 
Example 6
Source File: SparqlFluoQueryBuilder.java    From rya with Apache License 2.0 4 votes vote down vote up
@Override
public void meet(final Reduced node) {
    //create id, initialize ConstructQueryMetadata builder, register ConstructQueryMetadata
    //builder with FluoQueryBuilder, and add metadata that we currently have
    final String constructId = nodeIds.getOrMakeId(node);

    ConstructQueryMetadata.Builder constructBuilder = fluoQueryBuilder.getConstructQueryBuilder().orNull();
    if(constructBuilder == null) {
        constructBuilder = ConstructQueryMetadata.builder();
        constructBuilder.setNodeId(constructId);
        fluoQueryBuilder.setConstructQueryMetadata(constructBuilder);
    }

    //get child node
    QueryModelNode child = node.getArg();
    Preconditions.checkArgument(child instanceof Projection || child instanceof MultiProjection);
    final UnaryTupleOperator unary = (UnaryTupleOperator) child;

    //get ProjectionElemList to build ConstructGraph
    final List<ProjectionElemList> projections = new ArrayList<>();
    if(unary instanceof Projection) {
        projections.add(((Projection) unary).getProjectionElemList());
    } else {
        projections.addAll(((MultiProjection)unary).getProjections());
    }

    //get ExtensionElems to build ConstructGraph
    final QueryModelNode grandChild = unary.getArg();
    Preconditions.checkArgument(grandChild instanceof Extension);
    final Extension extension = (Extension) grandChild;
    final List<ExtensionElem> extensionElems = extension.getElements();
    final ConstructGraph graph = getConstructGraph(projections, extensionElems);
    constructBuilder.setConstructGraph(graph);

    //set child to the next node we care about in Fluo
    //if Extension's arg is a Group node, then it is an Aggregation, so set child to Extension
    //otherwise set child to Extension's child (only care about Extensions if they are Aggregations)
    if(extension.getArg() instanceof Group) {
        child = extension;
    } else {
        child = extension.getArg();
    }

    //Set the child node in the ConstructQueryMetadataBuilder
    final String childNodeId = nodeIds.getOrMakeId(child);
    constructBuilder.setChildNodeId(childNodeId);

    // Update the child node's metadata.
    final Set<String> childVars = getVars((TupleExpr)child);
    final VariableOrder childVarOrder = new VariableOrder(childVars);
    setChildMetadata(fluoQueryBuilder, childNodeId, childVarOrder, constructId);

    //fast forward visitor to next node we care about
    child.visit(this);
}