org.eclipse.rdf4j.query.algebra.Extension Java Examples

The following examples show how to use org.eclipse.rdf4j.query.algebra.Extension. 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: TopologyFactory.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
public void meet(final MultiProjection node) throws TopologyBuilderException {
    final String id = PROJECTION_PREFIX + UUID.randomUUID();
    final Optional<Side> side = getSide(node);

    final MultiProjectionProcessorSupplier supplier = new MultiProjectionProcessorSupplier(
            MultiProjectionEvaluator.make(node, bNodeIdFactory),
            result -> getResult(side, result));

    // If the arg is an Extension, then this node's grandchild is the next processing node.
    TupleExpr downstreamNode = node.getArg();
    if (downstreamNode instanceof Extension) {
        downstreamNode = ((Extension) downstreamNode).getArg();
    }

    entries.add(new ProcessorEntry(node, id, side, supplier, Lists.newArrayList(downstreamNode)));
    idMap.put(node, id);
    super.meet(node);
}
 
Example #2
Source File: MultiProjectionEvaluator.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * Make a {@link MultiProjectionEvaluator} that processes the logic of a {@link MultiProjection}.
 *
 * @param multiProjection - Defines the projections that will be processed. (not null)
 * @param bNodeIdFactory - Creates the IDs for Blank Nodes. (not null)
 * @return A {@link MultiProjectionEvaluator} for the provided {@link MultiProjection}.
 */
public static MultiProjectionEvaluator make(final MultiProjection multiProjection, final BNodeIdFactory bNodeIdFactory) {
    requireNonNull(multiProjection);

    // Figure out if there are extensions.
    final TupleExpr arg = multiProjection.getArg();
    final Optional<Extension> extension = (arg instanceof Extension) ? Optional.of((Extension)arg): Optional.empty();

    // If there are, iterate through them and find any blank node source names.
    final Set<String> blankNodeSourceNames = new HashSet<>();
    if(extension.isPresent()) {
        for(final ExtensionElem elem : extension.get().getElements()) {
            if(elem.getExpr() instanceof BNodeGenerator) {
                blankNodeSourceNames.add( elem.getName() );
            }
        }
    }

    // Create a ProjectionEvaluator for each projection that is part of the multi.
    final Set<ProjectionEvaluator> projections = new HashSet<>();
    for(final ProjectionElemList projectionElemList : multiProjection.getProjections()) {
        projections.add( new ProjectionEvaluator(projectionElemList, extension) );
    }

    return new MultiProjectionEvaluator(projections, blankNodeSourceNames, bNodeIdFactory);
}
 
Example #3
Source File: QueryJoinOptimizerTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void bindSubselectJoinOrder() throws Exception {
	String query = "SELECT * WHERE {\n" + "    BIND (bnode() as ?ct01) \n" + "    { SELECT ?s WHERE {\n"
			+ "            ?s ?p ?o .\n" + "      }\n" + "      LIMIT 10\n" + "    }\n" + "}";

	SPARQLParser parser = new SPARQLParser();
	ParsedQuery q = parser.parseQuery(query, null);
	QueryJoinOptimizer opt = new QueryJoinOptimizer();
	QueryRoot optRoot = new QueryRoot(q.getTupleExpr());
	opt.optimize(optRoot, null, null);

	JoinFinder joinFinder = new JoinFinder();
	optRoot.visit(joinFinder);
	Join join = joinFinder.getJoin();

	assertThat(join.getLeftArg()).as("BIND clause should be left-most argument of join")
			.isInstanceOf(Extension.class);
}
 
Example #4
Source File: QueryJoinOptimizerTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testSES2116JoinBind() throws Exception {

	StringBuilder qb = new StringBuilder();
	qb.append("SELECT ?subject ?name ?row {\n" + "  ?subject <http://localhost/table_1> ?uri .\n"
			+ "  BIND(STR(?uri) AS ?name)\n"
			+ "  ?table <http://linked.opendata.cz/ontology/odcs/tabular/hasRow> ?row .\n"
			+ "  ?table <http://linked.opendata.cz/ontology/odcs/tabular/symbolicName> ?name .\n" + "}");

	SPARQLParser parser = new SPARQLParser();
	ParsedQuery q = parser.parseQuery(qb.toString(), null);
	QueryJoinOptimizer opt = new QueryJoinOptimizer();
	QueryRoot optRoot = new QueryRoot(q.getTupleExpr());
	opt.optimize(optRoot, null, null);
	TupleExpr leaf = findLeaf(optRoot);
	Assert.assertTrue("Extension must be evaluated before StatementPattern",
			leaf.getParentNode() instanceof Extension);
}
 
Example #5
Source File: AbstractQueryBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private UnaryTupleOperator projection() {
	if (!mProjectionPatterns.isEmpty()) {
		return multiProjection();
	} else {
		Extension aExt = null;

		ProjectionElemList aList = new ProjectionElemList();

		for (String aVar : mProjectionVars) {
			aList.addElement(new ProjectionElem(aVar));
		}

		Projection aProjection = new Projection();
		aProjection.setProjectionElemList(aList);

		if (aExt != null) {
			aProjection.setArg(aExt);
		}

		return aProjection;
	}
}
 
Example #6
Source File: SparqlToPipelineTransformVisitorTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnsupportedExtension() throws Exception {
    StatementPattern sp = new StatementPattern(new Var("x"), constant(TAKES), new Var("c"));
    List<ExtensionElem> elements = Arrays.asList(new ExtensionElem(new Var("x"), "renamed"),
            new ExtensionElem(new Not(new ValueConstant(VF.createLiteral(true))), "notTrue"),
            new ExtensionElem(new ValueConstant(TAKES), "constant"));
    Extension extensionNode = new Extension(sp, elements);
    QueryRoot queryTree = new QueryRoot(extensionNode);
    SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(collection);
    queryTree.visit(visitor);
    Assert.assertTrue(queryTree.getArg() instanceof Extension);
    Assert.assertEquals(elements, ((Extension) queryTree.getArg()).getElements());
    TupleExpr innerQuery = ((Extension) queryTree.getArg()).getArg();
    Assert.assertTrue(innerQuery instanceof AggregationPipelineQueryNode);
    AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) innerQuery;
    Assert.assertEquals(Sets.newHashSet("x", "c"), pipelineNode.getAssuredBindingNames());
}
 
Example #7
Source File: SpinConstructRule.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
public void meet(Extension node) {
    Set<String> argBindings = node.getArg().getBindingNames();
    if (typeRequirement != null) {
        node.getElements().removeIf(elem -> {
            if (varName.equals(elem.getName())) {
                ValueExpr expr = elem.getExpr();
                if (expr == null) {
                    return true;
                }
                else if (expr instanceof Var) {
                    String fromName = ((Var) expr).getName();
                    if (getVarValue((Var) expr) == null && !argBindings.contains(fromName)) {
                        return true;
                    }
                }
            }
            return false;
        });
        meetUnaryTupleOperator(node);
    }
}
 
Example #8
Source File: ConstructConsequentVisitorTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void testConcreteSP() {
    Extension extension = new Extension(new SingletonSet(),
            new ExtensionElem(new ValueConstant(FOAF.PERSON), "x"),
            new ExtensionElem(new ValueConstant(RDF.TYPE), "y"),
            new ExtensionElem(new ValueConstant(OWL.CLASS), "z"));
    Projection projection = new Projection(extension, new ProjectionElemList(
            new ProjectionElem("x", "subject"),
            new ProjectionElem("y", "predicate"),
            new ProjectionElem("z", "object")));
    ConstructConsequentVisitor visitor = new ConstructConsequentVisitor();
    projection.visit(visitor);
    Set<StatementPattern> expected = Sets.newHashSet(
            new StatementPattern(s(FOAF.PERSON), p(RDF.TYPE), o(OWL.CLASS)));
    Assert.assertEquals(expected, visitor.getConsequents());
}
 
Example #9
Source File: GeoRelationQuerySpec.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public QueryModelNode removeQueryPatterns() {
	final QueryModelNode placeholder = new SingletonSet();

	filter.replaceWith(filter.getArg());

	geoStatement.replaceWith(placeholder);

	if (functionParent instanceof ExtensionElem) {
		Extension extension = (Extension) functionParent.getParentNode();
		List<ExtensionElem> elements = extension.getElements();
		if (elements.size() > 1) {
			elements.remove(functionParent);
		} else {
			extension.replaceWith(extension.getArg());
		}
	}

	return placeholder;
}
 
Example #10
Source File: DistanceQuerySpec.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public QueryModelNode removeQueryPatterns() {
	final QueryModelNode placeholder = new SingletonSet();

	filter.replaceWith(filter.getArg());

	geoStatement.replaceWith(placeholder);

	QueryModelNode functionParent = distanceFunction.getParentNode();
	if (functionParent instanceof ExtensionElem) {
		Extension extension = (Extension) functionParent.getParentNode();
		List<ExtensionElem> elements = extension.getElements();
		if (elements.size() > 1) {
			elements.remove(functionParent);
		} else {
			extension.replaceWith(extension.getArg());
		}
	}

	return placeholder;
}
 
Example #11
Source File: TopologyFactory.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
public void meet(final Projection node) throws TopologyBuilderException {
    final String id = PROJECTION_PREFIX + UUID.randomUUID();
    final Optional<Side> side = getSide(node);

    // If the arg is an Extension, there are rebindings that need to be
    // ignored since they do not have a processor node.
    TupleExpr downstreamNode = node.getArg();
    if (downstreamNode instanceof Extension) {
        downstreamNode = ((Extension) downstreamNode).getArg();
    }

    final ProjectionProcessorSupplier supplier = new ProjectionProcessorSupplier(
            ProjectionEvaluator.make(node),
            result -> getResult(side, result));

    entries.add(new ProcessorEntry(node, id, side, supplier, Lists.newArrayList(downstreamNode)));
    idMap.put(node, id);
    super.meet(node);
}
 
Example #12
Source File: HasSelfVisitorTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testPropertyPattern_constantObj() throws Exception {
    final InferenceEngine inferenceEngine = mock(InferenceEngine.class);
    final Set<Resource> loveTypes = new HashSet<>();
    loveTypes.add(narcissist);
    when(inferenceEngine.getHasSelfImplyingProperty(love)).thenReturn(loveTypes);
    final Var subj = new Var("s");
    final Var obj = new Var("o", self);
    obj.setConstant(true);
    final Var pred = new Var("p", love);
    pred.setConstant(true);

    final Projection query = new Projection(new StatementPattern(subj, pred, obj),
            new ProjectionElemList(new ProjectionElem("s", "subject")));
    query.visit(new HasSelfVisitor(conf, inferenceEngine));

    Assert.assertTrue(query.getArg() instanceof Union);
    final Union union = (Union) query.getArg();
    Assert.assertTrue(union.getRightArg() instanceof StatementPattern);
    Assert.assertTrue(union.getLeftArg() instanceof Extension);
    final StatementPattern expectedRight = new StatementPattern(subj, pred, obj);
    final Extension expectedLeft = new Extension(
            new StatementPattern(obj, new Var(RDF.TYPE.stringValue(), RDF.TYPE), new Var("urn:Narcissist", narcissist)),
            new ExtensionElem(obj, "s"));
    Assert.assertEquals(expectedLeft, union.getLeftArg());
    Assert.assertEquals(expectedRight, union.getRightArg());
}
 
Example #13
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 #14
Source File: SparqlToPipelineTransformVisitorTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testExtension() throws Exception {
    QueryRoot queryTree = new QueryRoot(new Extension(
            new StatementPattern(new Var("x"), constant(TAKES), new Var("c")),
            new ExtensionElem(new Var("x"), "renamed"),
            new ExtensionElem(new ValueConstant(TAKES), "constant")));
    SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(collection);
    queryTree.visit(visitor);
    Assert.assertTrue(queryTree.getArg() instanceof AggregationPipelineQueryNode);
    AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) queryTree.getArg();
    Assert.assertEquals(Sets.newHashSet("x", "c", "renamed", "constant"), pipelineNode.getAssuredBindingNames());
}
 
Example #15
Source File: HasSelfVisitorTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testPropertyPattern_constantSubj() throws Exception {
    final InferenceEngine inferenceEngine = mock(InferenceEngine.class);
    final Set<Resource> loveTypes = new HashSet<>();
    loveTypes.add(narcissist);
    when(inferenceEngine.getHasSelfImplyingProperty(love)).thenReturn(loveTypes);
    final Var subj = new Var("s", self);
    subj.setConstant(true);
    final Var obj = new Var("o");
    final Var pred = new Var("p", love);
    pred.setConstant(true);

    final Projection query = new Projection(new StatementPattern(subj, pred, obj),
            new ProjectionElemList(new ProjectionElem("s", "subject")));
    query.visit(new HasSelfVisitor(conf, inferenceEngine));

    Assert.assertTrue(query.getArg() instanceof Union);
    final Union union = (Union) query.getArg();
    Assert.assertTrue(union.getRightArg() instanceof StatementPattern);
    Assert.assertTrue(union.getLeftArg() instanceof Extension);
    final StatementPattern expectedRight = new StatementPattern(subj, pred, obj);
    final Extension expectedLeft = new Extension(
            new StatementPattern(subj, new Var(RDF.TYPE.stringValue(), RDF.TYPE), new Var("urn:Narcissist", narcissist)),
            new ExtensionElem(subj, "o"));
    Assert.assertEquals(expectedLeft, union.getLeftArg());
    Assert.assertEquals(expectedRight, union.getRightArg());
}
 
Example #16
Source File: HalyardTupleExprEvaluation.java    From Halyard with Apache License 2.0 5 votes vote down vote up
/**
 * Switch logic for evaluation of any instance of a {@link UnaryTupleOperator} query model node
 * @param parent
 * @param expr
 * @param bindings
 */
private void evaluateUnaryTupleOperator(BindingSetPipe parent, UnaryTupleOperator expr, BindingSet bindings) {
    if (expr instanceof Projection) {
        evaluateProjection(parent, (Projection) expr, bindings);
    } else if (expr instanceof MultiProjection) {
        evaluateMultiProjection(parent, (MultiProjection) expr, bindings);
    } else if (expr instanceof Filter) {
        evaluateFilter(parent, (Filter) expr, bindings);
    } else if (expr instanceof Service) {
        evaluateService(parent, (Service) expr, bindings);
    } else if (expr instanceof Slice) {
        evaluateSlice(parent, (Slice) expr, bindings);
    } else if (expr instanceof Extension) {
        evaluateExtension(parent, (Extension) expr, bindings);
    } else if (expr instanceof Distinct) {
        evaluateDistinct(parent, (Distinct) expr, bindings);
    } else if (expr instanceof Reduced) {
        evaluateReduced(parent, (Reduced) expr, bindings);
    } else if (expr instanceof Group) {
        evaluateGroup(parent, (Group) expr, bindings);
    } else if (expr instanceof Order) {
        evaluateOrder(parent, (Order) expr, bindings);
    } else if (expr instanceof QueryRoot) {
        parentStrategy.sharedValueOfNow = null;
        evaluateTupleExpr(parent, ((QueryRoot) expr).getArg(), bindings);
    } else if (expr instanceof DescribeOperator) {
        evaluateDescribeOperator(parent, (DescribeOperator) expr, bindings);
    } else if (expr == null) {
        parent.handleException(new IllegalArgumentException("expr must not be null"));
    } else {
        parent.handleException(new QueryEvaluationException("Unknown unary tuple operator type: " + expr.getClass()));
    }
}
 
Example #17
Source File: SpinRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Extension node) throws RDFHandlerException {
	if (inlineBindings != null && inlineBindings.extension == node) {
		// this is the first Extension node and has already been handled
		// by meetExtension()
		// to produce inline bindings in SELECT so we can skip it here
		node.getArg().visit(this);
	} else {
		// any further Extension nodes produce BIND() clauses
		node.getArg().visit(this);
		for (ExtensionElem elem : node.getElements()) {
			elem.visit(this);
		}
	}
}
 
Example #18
Source File: TestSparqlStarParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testUseInSTR() throws Exception {
	String simpleSparqlQuery = "SELECT (str(<<<urn:a> <urn:b> <urn:c>>>) as ?str) WHERE { } ";

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

	assertNotNull(q);
	assertTrue("expect projection", q.getTupleExpr() instanceof Projection);
	Projection proj = (Projection) q.getTupleExpr();
	List<ProjectionElem> list = proj.getProjectionElemList().getElements();
	final ArrayList<String> listNames = new ArrayList<>();
	list.forEach(el -> {
		listNames.add(el.getTargetName());
	});
	assertEquals("expect one binding", 1, list.size());
	assertTrue("expect str", listNames.contains("str"));

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

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

	assertEquals("name should match", "str", elem.getName());
	assertTrue("expect Str in extention element", elem.getExpr() instanceof Str);

	assertTrue("expect ValueExprTripleRef in extention element",
			((Str) elem.getExpr()).getArg() instanceof ValueExprTripleRef);
	ValueExprTripleRef ref = (ValueExprTripleRef) ((Str) elem.getExpr()).getArg();
	assertEquals("subject var value", "urn:a", ref.getSubjectVar().getValue().toString());
	assertEquals("predicate var name", "urn:b", ref.getPredicateVar().getValue().toString());
	assertEquals("object var name", "urn:c", ref.getObjectVar().getValue().toString());
}
 
Example #19
Source File: ProjectionEvaluator.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * Make a {@link ProjectionEvaluator} that processes the logic of a {@link Projection}.
 *
 * @param projection - Defines the projection that will be processed. (not null)
 * @return A {@link ProjectionEvaluator} for the provided {@link Projection}.
 */
public static ProjectionEvaluator make(final Projection projection) {
    requireNonNull(projection);

    final ProjectionElemList projectionElems = projection.getProjectionElemList();

    final TupleExpr arg = projection.getArg();
    final Optional<Extension> extension = arg instanceof Extension ? Optional.of((Extension)arg) : Optional.empty();

    return new ProjectionEvaluator(projectionElems, extension);
}
 
Example #20
Source File: SparqlFluoQueryBuilder.java    From rya with Apache License 2.0 5 votes vote down vote up
private String makeId(final QueryModelNode node) {
    checkNotNull(node);

    // Create the prefix of the id. This makes it a little bit more human readable.
    String prefix;
    if (node instanceof StatementPattern) {
        prefix = SP_PREFIX;
    } else if (node instanceof Filter) {
        prefix = FILTER_PREFIX;
    } else if (node instanceof Join || node instanceof LeftJoin) {
        prefix = JOIN_PREFIX;
    } else if (node instanceof Projection) {
        prefix = PROJECTION_PREFIX;
    } else if(node instanceof Extension) {
        prefix = AGGREGATION_PREFIX;
    }  else if (node instanceof Reduced) {
        prefix = CONSTRUCT_PREFIX;
    } else if(node instanceof PeriodicQueryNode) {
        prefix = PERIODIC_QUERY_PREFIX;
    } else {
        throw new IllegalArgumentException("Node must be of type {StatementPattern, Join, Filter, Extension, Projection} but was " + node.getClass());
    }

    final String unique = UUID.randomUUID().toString().replaceAll("-", "");
    // Put them together to create the Node ID.
    return prefix + "_" + unique;
}
 
Example #21
Source File: ConstructConsequentVisitor.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void meet(Projection projection) {
    if (projection.getArg() instanceof Extension) {
        recordConsequent(projection.getProjectionElemList(),
                ((Extension) projection.getArg()).getElements());
    }
    else {
        recordConsequent(projection.getProjectionElemList(), Arrays.asList());
    }
}
 
Example #22
Source File: ConstructConsequentVisitor.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void meet(MultiProjection projection) {
    List<ExtensionElem> bindings;
    if (projection.getArg() instanceof Extension) {
        bindings = ((Extension) projection.getArg()).getElements();
    }
    else {
        bindings = Arrays.asList();
    }
    for (ProjectionElemList template : projection.getProjections()) {
        recordConsequent(template, bindings);
    }
}
 
Example #23
Source File: ConstructConsequentVisitorTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenericSP() {
    Extension extension = new Extension(new SingletonSet(),
            new ExtensionElem(new Var("z"), "z"));
    Projection projection = new Projection(extension, new ProjectionElemList(
            new ProjectionElem("x", "subject"),
            new ProjectionElem("y", "predicate"),
            new ProjectionElem("z", "object")));
    ConstructConsequentVisitor visitor = new ConstructConsequentVisitor();
    projection.visit(visitor);
    Set<StatementPattern> expected = Sets.newHashSet(
            new StatementPattern(s(null), p(null), o(null)));
    Assert.assertEquals(expected, visitor.getConsequents());
}
 
Example #24
Source File: ConstructConsequentVisitorTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testMissingVariables() {
    Extension extension = new Extension(new SingletonSet(),
            new ExtensionElem(new ValueConstant(FOAF.PERSON), "x"),
            new ExtensionElem(new ValueConstant(RDF.TYPE), "y"));
    Projection projection = new Projection(extension, new ProjectionElemList(
            new ProjectionElem("x", "s"),
            new ProjectionElem("y", "predicate"),
            new ProjectionElem("z", "object")));
    ConstructConsequentVisitor visitor = new ConstructConsequentVisitor();
    projection.visit(visitor);
    Set<StatementPattern> expected = Sets.newHashSet(
            new StatementPattern(s(null), p(RDF.TYPE), o(null)));
    Assert.assertEquals(expected, visitor.getConsequents());
}
 
Example #25
Source File: ConstructConsequentVisitorTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiProjection() {
    Extension extension = new Extension(new SingletonSet(),
            new ExtensionElem(new ValueConstant(RDF.TYPE), "rdftype"),
            new ExtensionElem(new ValueConstant(OWL.OBJECTPROPERTY), "owlprop"),
            new ExtensionElem(new ValueConstant(OWL.EQUIVALENTCLASS), "owleqcls"),
            new ExtensionElem(new ValueConstant(OWL.CLASS), "owlclass"));
    MultiProjection projection = new MultiProjection(extension, Arrays.asList(
            new ProjectionElemList(
                    new ProjectionElem("cls", "subject"),
                    new ProjectionElem("rdftype", "predicate"),
                    new ProjectionElem("owlclass", "object")),
            new ProjectionElemList(
                    new ProjectionElem("prop", "subject"),
                    new ProjectionElem("rdftype", "predicate"),
                    new ProjectionElem("owlprop", "object")),
            new ProjectionElemList(
                    new ProjectionElem("owleqcls", "predicate"),
                    new ProjectionElem("cls", "object"))));
    ConstructConsequentVisitor visitor = new ConstructConsequentVisitor();
    projection.visit(visitor);
    Set<StatementPattern> expected = Sets.newHashSet(
            new StatementPattern(s(null), p(RDF.TYPE), o(OWL.CLASS)),
            new StatementPattern(s(null), p(RDF.TYPE), o(OWL.OBJECTPROPERTY)),
            new StatementPattern(s(null), p(OWL.EQUIVALENTCLASS), o(null)));
    Assert.assertEquals(expected, visitor.getConsequents());
}
 
Example #26
Source File: ConstructConsequentVisitorTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testBNode() {
    Extension extension = new Extension(new SingletonSet(),
            new ExtensionElem(new Var("x"), "x"),
            new ExtensionElem(new BNodeGenerator(), "z"));
    Projection projection = new Projection(extension, new ProjectionElemList(
            new ProjectionElem("x", "subject"),
            new ProjectionElem("y", "predicate"),
            new ProjectionElem("z", "object")));
    ConstructConsequentVisitor visitor = new ConstructConsequentVisitor();
    projection.visit(visitor);
    Set<StatementPattern> expected = Sets.newHashSet(
            new StatementPattern(s(null), p(null), anon(o(null))));
    Assert.assertEquals(expected, visitor.getConsequents());
}
 
Example #27
Source File: QueryJoinOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected List<Extension> getExtensions(List<TupleExpr> expressions) {
	List<Extension> extensions = new ArrayList<>();
	for (TupleExpr expr : expressions) {
		if (expr instanceof Extension) {
			extensions.add((Extension) expr);
		}
	}
	return extensions;
}
 
Example #28
Source File: DefaultFedXCostModel.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public double estimateCost(TupleExpr tupleExpr, Set<String> joinVars) {

	if (tupleExpr instanceof StatementSourcePattern) {
		return estimateCost((StatementSourcePattern) tupleExpr, joinVars);
	}
	if (tupleExpr instanceof ExclusiveStatement) {
		return estimateCost((ExclusiveStatement) tupleExpr, joinVars);
	}
	if (tupleExpr instanceof ExclusiveGroup) {
		return estimateCost((ExclusiveGroup) tupleExpr, joinVars);
	}
	if (tupleExpr instanceof NJoin) {
		return estimateCost((NJoin) tupleExpr, joinVars);
	}
	if (tupleExpr instanceof NUnion) {
		return estimateCost((NUnion) tupleExpr, joinVars);
	}
	if (tupleExpr instanceof FedXService) {
		return estimateCost((FedXService) tupleExpr, joinVars);
	}
	if (tupleExpr instanceof Projection) {
		return estimateCost((Projection) tupleExpr, joinVars);
	}
	if (tupleExpr instanceof BindingSetAssignment) {
		return 0;
	}
	if (tupleExpr instanceof Extension) {
		return 0;
	}
	if (tupleExpr instanceof ArbitraryLengthPath) {
		return estimateCost((ArbitraryLengthPath) tupleExpr, joinVars);
	}

	log.debug("No cost estimation for " + tupleExpr.getClass().getSimpleName() + " available.");

	return 1000d;
}
 
Example #29
Source File: SpinRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Extension node) {
	extension = node;
	List<ExtensionElem> elements = node.getElements();
	// NB: preserve ExtensionElem order
	extensionExprs = new LinkedHashMap<>(elements.size());
	for (ExtensionElem elem : elements) {
		extensionExprs.put(elem.getName(), elem.getExpr());
	}
}
 
Example #30
Source File: SpinParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private TupleExpr visitHaving(Resource having) throws RDF4JException {
	UnaryTupleOperator op = (UnaryTupleOperator) group.getParentNode();
	op.setArg(new Extension(group));
	Iteration<Resource, QueryEvaluationException> iter = TripleSources.listResources(having, store);
	while (iter.hasNext()) {
		Resource r = iter.next();
		ValueExpr havingExpr = visitExpression(r);
		Filter filter = new Filter(op.getArg(), havingExpr);
		op.setArg(filter);
		op = filter;
	}
	return op;
}