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

The following examples show how to use org.eclipse.rdf4j.query.algebra.Reduced. 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: OrderLimitOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void meet(Order node) {
	for (OrderElem e : node.getElements()) {
		e.visit(this);
	}
	if (variablesProjected) {
		QueryModelNode parent = node.getParentNode();
		if (projection == parent) {
			node.replaceWith(node.getArg().clone());
			node.setArg(projection.clone());
			Order replacement = node.clone();
			projection.replaceWith(replacement);
			QueryModelNode distinct = replacement.getParentNode();
			if (distinct instanceof Distinct) {
				distinct.replaceWith(new Reduced(replacement.clone()));
			}
		}
	}
}
 
Example #2
Source File: HalyardTupleExprEvaluation.java    From Halyard with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the limit of the current variable bindings before any further projection.
 */
private static long getLimit(QueryModelNode node) {
    long offset = 0;
    if (node instanceof Slice) {
        Slice slice = (Slice) node;
        if (slice.hasOffset() && slice.hasLimit()) {
            return slice.getOffset() + slice.getLimit();
        } else if (slice.hasLimit()) {
            return slice.getLimit();
        } else if (slice.hasOffset()) {
            offset = slice.getOffset();
        }
    }
    QueryModelNode parent = node.getParentNode();
    if (parent instanceof Distinct || parent instanceof Reduced || parent instanceof Slice) {
        long limit = getLimit(parent);
        if (offset > 0L && limit < Long.MAX_VALUE) {
            return offset + limit;
        } else {
            return limit;
        }
    }
    return Long.MAX_VALUE;
}
 
Example #3
Source File: HalyardTupleExprEvaluation.java    From Halyard with Apache License 2.0 6 votes vote down vote up
/**
 * Evaluate {@link Reduced} query model nodes
 * @param parent
 * @param reduced
 * @param bindings
 */
private void evaluateReduced(BindingSetPipe parent, Reduced reduced, BindingSet bindings) {
    evaluateTupleExpr(new BindingSetPipe(parent) {
        private BindingSet previous = null;

        @Override
        public boolean push(BindingSet bs) throws InterruptedException {
            synchronized (this) {
                if (bs != null && bs.equals(previous)) {
                    return true;
                }
                previous = bs;
            }
            return parent.push(bs);
        }
    }, reduced.getArg(), bindings);
}
 
Example #4
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Returns the limit of the current variable bindings before any further projection.
 */
protected long getLimit(QueryModelNode node) {
	long offset = 0;
	if (node instanceof Slice) {
		Slice slice = (Slice) node;
		if (slice.hasOffset() && slice.hasLimit()) {
			return slice.getOffset() + slice.getLimit();
		} else if (slice.hasLimit()) {
			return slice.getLimit();
		} else if (slice.hasOffset()) {
			offset = slice.getOffset();
		}
	}
	QueryModelNode parent = node.getParentNode();
	if (parent instanceof Distinct || parent instanceof Reduced || parent instanceof Slice) {
		long limit = getLimit(parent);
		if (offset > 0L && limit < Long.MAX_VALUE) {
			return offset + limit;
		} else {
			return limit;
		}
	}
	return Long.MAX_VALUE;
}
 
Example #5
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 #6
Source File: SparqlFluoQueryBuilder.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void meet(final Reduced node) throws Exception {
    if(varOrder == null) {
        varOrder = getConstructGraphVarOrder(node);
    }

    if(queryType == null) {
        queryType = QueryType.CONSTRUCT;
    }
    super.meet(node);
}
 
Example #7
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 #8
Source File: SparqlToPipelineTransformVisitor.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void meet(Reduced reducedNode) throws Exception {
    reducedNode.visitChildren(this);
    if (reducedNode.getArg() instanceof AggregationPipelineQueryNode && reducedNode.getParentNode() != null) {
        reducedNode.replaceWith(reducedNode.getArg());
    }
}
 
Example #9
Source File: HalyardTupleExprEvaluation.java    From Halyard with Apache License 2.0 5 votes vote down vote up
/**
 * Determines if the parent of the node is an instance of {@link Distinct} or {@link Reduced}.
 * @param node the {@link QueryModelNode} to test
 * @return {@code true} if the parent is and instance of {@link Distinct} or {@link Reduced} and {@code false} otherwise. If the parent is
 * an instance of {@link Slice} then the parent is considered to be the first non-{@code Slice} node up the tree.
 */
private static boolean isReducedOrDistinct(QueryModelNode node) {
    QueryModelNode parent = node.getParentNode();
    if (parent instanceof Slice) {
        return isReducedOrDistinct(parent);
    }
    return parent instanceof Distinct || parent instanceof Reduced;
}
 
Example #10
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 #11
Source File: SpinRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Reduced node) throws RDFHandlerException {
	if (!isSubQuery) { // ignore root reduced
		node.getArg().visit(this);
	} else {
		super.meet(node);
	}
}
 
Example #12
Source File: BaseTupleExprRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @inheritDoc
 */
@Override
public void meet(final Reduced theReduced) throws Exception {
	mReduced = true;

	theReduced.visitChildren(this);
}
 
Example #13
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected boolean isReducedOrDistinct(QueryModelNode node) {
	QueryModelNode parent = node.getParentNode();
	if (parent instanceof Slice) {
		return isReducedOrDistinct(parent);
	}
	return parent instanceof Distinct || parent instanceof Reduced;
}
 
Example #14
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 #15
Source File: PrepareOwnedTupleExpr.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Reduced node) throws RepositoryException {
	boolean before = reduce;
	try {
		reduce = true;
		node.getArg().visit(this);
	} finally {
		reduce = before;
	}
	if (patternNode == null) {
		return;
	}
	this.reduced = true;
	this.patternNode = node;
}
 
Example #16
Source File: TopologyFactory.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void meet(final Reduced node) throws TopologyBuilderException {
    // This indicates we're outputting VisibilityStatements.
    sinkEntry = new SinkEntry<>(
            new StatementOutputFormatterSupplier(),
            new StringSerializer(),
            new VisibilityStatementSerializer());
    super.meet(node);
}
 
Example #17
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 #18
Source File: PeriodicQueryUtil.java    From rya with Apache License 2.0 4 votes vote down vote up
public void meet(Reduced node) {
    relocationParent = node;
    super.meet(node);
}
 
Example #19
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 #20
Source File: AbstractQueryModelVisitor.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void meet(Reduced node) throws X {
	meetUnaryTupleOperator(node);
}
 
Example #21
Source File: FilterOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void meet(Reduced node) {
	node.getArg().visit(this);
}
 
Example #22
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(Reduced reduced, BindingSet bindings)
		throws QueryEvaluationException {
	return new ReducedIteration<>(evaluate(reduced.getArg(), bindings));
}
 
Example #23
Source File: SpinRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void meet(Reduced node) throws RDFHandlerException {
	node.getArg().visit(this);
	handler.handleStatement(valueFactory.createStatement(subject, SP.REDUCED_PROPERTY, BooleanLiteral.TRUE));
}