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

The following examples show how to use org.eclipse.rdf4j.query.algebra.UnaryTupleOperator. 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: AbstractExternalSetMatcher.java    From rya with Apache License 2.0 6 votes vote down vote up
protected void updateTupleAndNodes() {
    segmentNodeList = segment.getOrderedNodes();
    final TupleExprAndNodes tupAndNodes = segment.getQuery();
    tuple = tupAndNodes.getTupleExpr();
    filters = tupAndNodes.getFilters();
    unmatched = new HashSet<>();
    final List<QueryModelNode> nodes = tupAndNodes.getNodes();
    for (final QueryModelNode q : nodes) {
        if (q instanceof UnaryTupleOperator || q instanceof BinaryTupleOperator) {
            unmatched.add((TupleExpr) q);
        } else if (q instanceof FlattenedOptional) {
            final FlattenedOptional opt = (FlattenedOptional) q;
            final TupleExpr rightArg = opt.getRightArg();
            if (rightArg instanceof UnaryTupleOperator || rightArg instanceof BinaryTupleOperator) {
                unmatched.add(rightArg);
            }
        }
    }
}
 
Example #2
Source File: SparqlFluoQueryBuilder.java    From rya with Apache License 2.0 6 votes vote down vote up
private static VariableOrder getConstructGraphVarOrder(final Reduced node) {

        //get child node
          final 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());
          }

          return getConstructGraphVarOrder(projections);
      }
 
Example #3
Source File: HalyardTupleExprEvaluation.java    From Halyard with Apache License 2.0 6 votes vote down vote up
/**
 * Switch logic appropriate for each type of {@link TupleExpr} query model node, sending each type to it's appropriate evaluation method. For example,
 * {@code UnaryTupleOperator} is sent to {@link evaluateUnaryTupleOperator()}.
 * @param parent
 * @param expr
 * @param bindings
 */
private void evaluateTupleExpr(BindingSetPipe parent, TupleExpr expr, BindingSet bindings) {
    if (expr instanceof StatementPattern) {
        statementEvaluation.evaluateStatementPattern(parent, (StatementPattern) expr, bindings);
    } else if (expr instanceof UnaryTupleOperator) {
        evaluateUnaryTupleOperator(parent, (UnaryTupleOperator) expr, bindings);
    } else if (expr instanceof BinaryTupleOperator) {
        evaluateBinaryTupleOperator(parent, (BinaryTupleOperator) expr, bindings);
    } else if (expr instanceof SingletonSet) {
        evaluateSingletonSet(parent, (SingletonSet) expr, bindings);
    } else if (expr instanceof EmptySet) {
        evaluateEmptySet(parent, (EmptySet) expr, bindings);
    } else if (expr instanceof ExternalSet) {
        evaluateExternalSet(parent, (ExternalSet) expr, bindings);
    } else if (expr instanceof ZeroLengthPath) {
        evaluateZeroLengthPath(parent, (ZeroLengthPath) expr, bindings);
    } else if (expr instanceof ArbitraryLengthPath) {
        evaluateArbitraryLengthPath(parent, (ArbitraryLengthPath) expr, bindings);
    } else if (expr instanceof BindingSetAssignment) {
        evaluateBindingSetAssignment(parent, (BindingSetAssignment) expr, bindings);
    } else if (expr == null) {
        parent.handleException(new IllegalArgumentException("expr must not be null"));
    } else {
        parent.handleException(new QueryEvaluationException("Unsupported tuple expr type: " + expr.getClass()));
    }
}
 
Example #4
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 #5
Source File: AbstractQueryModelVisitor.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void meetOther(QueryModelNode node) throws X {
	if (node instanceof UnaryTupleOperator) {
		meetUnaryTupleOperator((UnaryTupleOperator) node);
	} else if (node instanceof BinaryTupleOperator) {
		meetBinaryTupleOperator((BinaryTupleOperator) node);
	} else if (node instanceof CompareSubQueryValueOperator) {
		meetCompareSubQueryValueOperator((CompareSubQueryValueOperator) node);
	} else if (node instanceof SubQueryValueOperator) {
		meetSubQueryValueOperator((SubQueryValueOperator) node);
	} else if (node instanceof UnaryValueOperator) {
		meetUnaryValueOperator((UnaryValueOperator) node);
	} else if (node instanceof BinaryValueOperator) {
		meetBinaryValueOperator((BinaryValueOperator) node);
	} else if (node instanceof UpdateExpr) {
		meetUpdateExpr((UpdateExpr) node);
	} else {
		meetNode(node);
	}
}
 
Example #6
Source File: QueryJoinOptimizerTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testSES2306AggregateOrderBy() throws Exception {
	String select = "PREFIX ex: <ex:>\n" + "SELECT ((MIN(?x+1) + MAX(?y-1))/2 AS ?r) {\n"
			+ "	?this ex:name ?n . ?this ex:id ?id . ?this ex:prop1 ?x . ?this ex:prop2 ?y .\n"
			+ "} GROUP BY concat(?n, ?id) HAVING (SUM(?x) + SUM(?y) < 5) ORDER BY (COUNT(?x) + COUNT(?y))";

	SPARQLParser parser = new SPARQLParser();
	ParsedQuery q = parser.parseQuery(select, null);
	q.getTupleExpr().visit(new AbstractQueryModelVisitor<Exception>() {

		@Override
		protected void meetUnaryTupleOperator(UnaryTupleOperator node) throws Exception {
			assertNotEquals(node, node.getArg());
			super.meetUnaryTupleOperator(node);
		}
	});
}
 
Example #7
Source File: QueryModelNodeReplacer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void meetUnaryTupleOperator(UnaryTupleOperator node) {
	assert former == node.getArg();
	if (replacement == null) {
		removeNode(node);
	} else {
		node.setArg((TupleExpr) replacement);
	}
}
 
Example #8
Source File: SpinConstructRule.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void meetUnaryTupleOperator(UnaryTupleOperator node) {
    if (typeRequirement != null) {
        if (node.getArg().getBindingNames().contains(varName)) {
            node.visitChildren(this);
        }
        else {
            meetNode(node);
        }
    }
}
 
Example #9
Source File: AbstractExternalSetOptimizer.java    From rya with Apache License 2.0 5 votes vote down vote up
private Set<TupleExpr> getUnMatchedArgNodes(List<QueryModelNode> nodes) {
    Set<TupleExpr> unmatched = new HashSet<>();
    for (final QueryModelNode q : nodes) {
        if (q instanceof UnaryTupleOperator || q instanceof BinaryTupleOperator) {
            unmatched.add((TupleExpr) q);
        } else if (q instanceof FlattenedOptional) {
            FlattenedOptional opt = (FlattenedOptional) q;
            TupleExpr rightArg = opt.getRightArg();
            if (rightArg instanceof UnaryTupleOperator || rightArg instanceof BinaryTupleOperator) {
                unmatched.add(rightArg);
            }
        }
    }
    return unmatched;
}
 
Example #10
Source File: RdfCloudTripleStoreEvaluationStatistics.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
protected void meetUnaryTupleOperator(final UnaryTupleOperator node) {
    if (node instanceof Projection) {
        cardinality += -1.0;
    }
    super.meetUnaryTupleOperator(node);
}
 
Example #11
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 #12
Source File: QueryJoinOptimizerTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private TupleExpr findLeaf(TupleExpr expr) {
	if (expr instanceof UnaryTupleOperator) {
		return findLeaf(((UnaryTupleOperator) expr).getArg());
	} else if (expr instanceof BinaryTupleOperator) {
		return findLeaf(((BinaryTupleOperator) expr).getLeftArg());
	} else {
		return expr;
	}
}
 
Example #13
Source File: QueryModelNormalizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void meetUnaryTupleOperator(UnaryTupleOperator node) {
	super.meetUnaryTupleOperator(node);

	if (node.getArg() instanceof EmptySet) {
		node.replaceWith(node.getArg());
	}
}
 
Example #14
Source File: ConstantOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Applies generally applicable optimizations to the supplied query: variable assignments are inlined.
 */
@Override
public void optimize(TupleExpr tupleExpr, Dataset dataset, BindingSet bindings) {
	ConstantVisitor visitor = new ConstantVisitor();
	tupleExpr.visit(visitor);
	Set<String> varsBefore = visitor.varNames;

	VarNameCollector varCollector = new VarNameCollector();
	tupleExpr.visit(varCollector);
	Set<String> varsAfter = varCollector.varNames;

	if (varsAfter.size() < varsBefore.size()) {
		varsBefore.removeAll(varsAfter);
		for (ProjectionElemList projElems : visitor.projElemLists) {
			for (ProjectionElem projElem : projElems.getElements()) {
				String name = projElem.getSourceName();
				if (varsBefore.contains(name)) {
					UnaryTupleOperator proj = (UnaryTupleOperator) projElems.getParentNode();
					Extension ext = new Extension(proj.getArg());
					proj.setArg(ext);
					Var lostVar = new Var(name);
					Value value = bindings.getValue(name);
					if (value != null) {
						lostVar.setValue(value);
					}
					ext.addElement(new ExtensionElem(lostVar, name));
				}
			}
		}
	}
}
 
Example #15
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(UnaryTupleOperator expr,
		BindingSet bindings) throws QueryEvaluationException {
	if (expr instanceof Projection) {
		return evaluate((Projection) expr, bindings);
	} else if (expr instanceof MultiProjection) {
		return evaluate((MultiProjection) expr, bindings);
	} else if (expr instanceof Filter) {
		return evaluate((Filter) expr, bindings);
	} else if (expr instanceof Service) {
		return evaluate((Service) expr, bindings);
	} else if (expr instanceof Slice) {
		return evaluate((Slice) expr, bindings);
	} else if (expr instanceof Extension) {
		return evaluate((Extension) expr, bindings);
	} else if (expr instanceof Distinct) {
		return evaluate((Distinct) expr, bindings);
	} else if (expr instanceof Reduced) {
		return evaluate((Reduced) expr, bindings);
	} else if (expr instanceof Group) {
		return evaluate((Group) expr, bindings);
	} else if (expr instanceof Order) {
		return evaluate((Order) expr, bindings);
	} else if (expr instanceof QueryRoot) {
		// new query, reset shared return value for successive calls of
		// NOW()
		this.sharedValueOfNow = null;
		return evaluate(expr.getArg(), bindings);
	} else if (expr instanceof DescribeOperator) {
		return evaluate((DescribeOperator) expr, bindings);
	} else if (expr == null) {
		throw new IllegalArgumentException("expr must not be null");
	} else {
		throw new QueryEvaluationException("Unknown unary tuple operator type: " + expr.getClass());
	}
}
 
Example #16
Source File: FederationJoinOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void meetUnaryTupleOperator(UnaryTupleOperator node) throws RepositoryException {
	super.meetUnaryTupleOperator(node);
	RepositoryConnection owner = getSingleOwner(node.getArg());
	if (owner != null) {
		node.replaceWith(new OwnedTupleExpr(owner, node.clone()));
	}
}
 
Example #17
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;
}
 
Example #18
Source File: SpinParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void visitConstruct(Resource construct) throws RDF4JException {
	Value templates = TripleSources.singleValue(construct, SP.TEMPLATES_PROPERTY, store);
	if (!(templates instanceof Resource)) {
		throw new MalformedSpinException(String.format("Value of %s is not a resource", SP.TEMPLATES_PROPERTY));
	}

	projElems = new LinkedHashMap<>();
	UnaryTupleOperator projection = visitTemplates((Resource) templates);
	TupleExpr whereExpr = visitWhere(construct);
	projection.setArg(whereExpr);
	addSourceExpressions(projection, projElems.values());
}
 
Example #19
Source File: BasicGraphPatternExtractor.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Handles unary nodes with a potential BGP as child (e.g. projection).
 */
@Override
public void meetUnaryTupleOperator(UnaryTupleOperator node) {

	node.getArg().visit(this);
	
	if (lastBGPNode != null) {
		// child is a BGP node but this node is not
		this.bgpList.add(lastBGPNode);
		lastBGPNode = null;
	}
}
 
Example #20
Source File: GenericInfoOptimizer.java    From CostFed with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Handles unary nodes with a potential BGP as child (e.g. projection).
 */
@Override
public void meetUnaryTupleOperator(UnaryTupleOperator node) {
	node.getArg().visit(this);
	beginNewBGPGroup();
}
 
Example #21
Source File: HalyardEvaluationStatistics.java    From Halyard with Apache License 2.0 4 votes vote down vote up
@Override
protected void meetUnaryTupleOperator(UnaryTupleOperator node) {
    super.meetUnaryTupleOperator(node);
    updateMap(node);
}
 
Example #22
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(TupleExpr expr, BindingSet bindings)
		throws QueryEvaluationException {

	CloseableIteration<BindingSet, QueryEvaluationException> ret;

	if (expr instanceof StatementPattern) {
		ret = evaluate((StatementPattern) expr, bindings);
	} else if (expr instanceof UnaryTupleOperator) {
		ret = evaluate((UnaryTupleOperator) expr, bindings);
	} else if (expr instanceof BinaryTupleOperator) {
		ret = evaluate((BinaryTupleOperator) expr, bindings);
	} else if (expr instanceof SingletonSet) {
		ret = evaluate((SingletonSet) expr, bindings);
	} else if (expr instanceof EmptySet) {
		ret = evaluate((EmptySet) expr, bindings);
	} else if (expr instanceof ExternalSet) {
		ret = evaluate((ExternalSet) expr, bindings);
	} else if (expr instanceof ZeroLengthPath) {
		ret = evaluate((ZeroLengthPath) expr, bindings);
	} else if (expr instanceof ArbitraryLengthPath) {
		ret = evaluate((ArbitraryLengthPath) expr, bindings);
	} else if (expr instanceof BindingSetAssignment) {
		ret = evaluate((BindingSetAssignment) expr, bindings);
	} else if (expr instanceof TripleRef) {
		ret = evaluate((TripleRef) expr, bindings);
	} else if (expr == null) {
		throw new IllegalArgumentException("expr must not be null");
	} else {
		throw new QueryEvaluationException("Unsupported tuple expr type: " + expr.getClass());
	}

	if (trackTime) {
		// set resultsSizeActual to at least be 0 so we can track iterations that don't procude anything
		expr.setTotalTimeNanosActual(Math.max(0, expr.getTotalTimeNanosActual()));
		ret = new TimedIterator(ret, expr);
	}

	if (trackResultSize) {
		// set resultsSizeActual to at least be 0 so we can track iterations that don't procude anything
		expr.setResultSizeActual(Math.max(0, expr.getResultSizeActual()));
		ret = new ResultSizeCountingIterator(ret, expr);
	}

	return ret;
}
 
Example #23
Source File: EvaluationStatistics.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void meetUnaryTupleOperator(UnaryTupleOperator node) {
	node.getArg().visit(this);
}
 
Example #24
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);
}
 
Example #25
Source File: AbstractQueryModelVisitor.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Method called by all <tt>meet</tt> methods with a {@link UnaryTupleOperator} node as argument. Forwards the call
 * to {@link #meetNode} by default.
 *
 * @param node The node that is being visited.
 */
protected void meetUnaryTupleOperator(UnaryTupleOperator node) throws X {
	meetNode(node);
}