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

The following examples show how to use org.eclipse.rdf4j.query.algebra.Filter. 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: DisjunctiveConstraintOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void meet(Filter filter) {
	if (filter.getCondition() instanceof Or && containsSameTerm(filter.getCondition())) {
		Or orNode = (Or) filter.getCondition();
		TupleExpr filterArg = filter.getArg();

		ValueExpr leftConstraint = orNode.getLeftArg();
		ValueExpr rightConstraint = orNode.getRightArg();

		// remove filter
		filter.replaceWith(filterArg);

		// Push UNION down below other filters to avoid cloning them
		TupleExpr node = findNotFilter(filterArg);

		Filter leftFilter = new Filter(node.clone(), leftConstraint);
		Filter rightFilter = new Filter(node.clone(), rightConstraint);
		Union union = new Union(leftFilter, rightFilter);
		node.replaceWith(union);

		filter.getParentNode().visit(this);
	} else {
		super.meet(filter);
	}
}
 
Example #2
Source File: EntityOptimizer.java    From rya with Apache License 2.0 6 votes vote down vote up
protected <L extends List<TupleExpr>> L getJoinArgs(TupleExpr tupleExpr, L joinArgs) {
    if (tupleExpr instanceof Join) {
        if (!(((Join) tupleExpr).getLeftArg() instanceof FixedStatementPattern)
                && !(((Join) tupleExpr).getRightArg() instanceof DoNotExpandSP)) {
            Join join = (Join) tupleExpr;
            getJoinArgs(join.getLeftArg(), joinArgs);
            getJoinArgs(join.getRightArg(), joinArgs);
        }
    } else if(tupleExpr instanceof Filter) {
        joinArgs.add(tupleExpr);
        getJoinArgs(((Filter)tupleExpr).getArg(), joinArgs);
    } else {
        joinArgs.add(tupleExpr);
    }

    return joinArgs;
}
 
Example #3
Source File: TupleReArranger.java    From rya with Apache License 2.0 6 votes vote down vote up
private static List<TupleExpr> getFilterChain(List<Filter> filters) {
    List<TupleExpr> filterTopBottom = Lists.newArrayList();
    Filter filterChainTop = null;
    Filter filterChainBottom = null;

    for (Filter filter : filters) {
        if (filterChainTop == null) {
            filterChainTop = filter.clone();
        } else if (filterChainBottom == null) {
            filterChainBottom = filter.clone();
            filterChainTop.setArg(filterChainBottom);
        } else {
            Filter newFilter = filter.clone();
            filterChainBottom.setArg(newFilter);
            filterChainBottom = newFilter;
        }
    }
    if (filterChainTop != null) {
        filterTopBottom.add(filterChainTop);
    }
    if (filterChainBottom != null) {
        filterTopBottom.add(filterChainBottom);
    }
    return filterTopBottom;
}
 
Example #4
Source File: StatementMetadataExternalSetProviderTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void createSingleAccumuloMetadataNode() throws MalformedQueryException {

    AccumuloRdfConfiguration conf = (AccumuloRdfConfiguration) getConf(false);
    Set<RyaIRI> propertySet = new HashSet<>();
    propertySet.add(new RyaIRI("http://createdBy"));
    conf.setStatementMetadataProperties(propertySet);
    StatementMetadataExternalSetProvider metaProvider = new StatementMetadataExternalSetProvider(
            conf);
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq = parser.parseQuery(query, null);

    List<QueryModelNode> patterns = new ArrayList<>();
    List<StatementMetadataNode<?>> expected = new ArrayList<>();
    Set<StatementPattern> sp = StatementMetadataTestUtils.getMetadataStatementPatterns(pq.getTupleExpr(), propertySet);

    patterns.addAll(StatementPatternCollector.process(pq.getTupleExpr()));
    JoinSegment<StatementMetadataNode<?>> segment = new JoinSegment<>(
            new HashSet<>(patterns), patterns, new HashMap<ValueExpr, Filter>());
    List<StatementMetadataNode<?>> extSets = metaProvider.getExternalSets(segment);

    expected.add(new StatementMetadataNode<>(sp, conf));

    Assert.assertEquals(expected, extSets);

}
 
Example #5
Source File: SeparateFilterJoinsVisitor.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
public void meet(final Filter node) throws Exception {
    super.meet(node);

    final ValueExpr condition = node.getCondition();
    final TupleExpr arg = node.getArg();
    if (!(arg instanceof Join)) {
        return;
    }

    final Join join = (Join) arg;
    final TupleExpr leftArg = join.getLeftArg();
    final TupleExpr rightArg = join.getRightArg();

    if (leftArg instanceof StatementPattern && rightArg instanceof StatementPattern) {
        final Filter left = new Filter(leftArg, condition);
        final Filter right = new Filter(rightArg, condition);
        node.replaceWith(new Join(left, right));
    }

}
 
Example #6
Source File: DistanceQuerySpec.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public DistanceQuerySpec(FunctionCall distanceFunction, ValueExpr distanceExpr, String distVar, Filter filter) {
	this.distanceFunction = distanceFunction;
	this.distanceExpr = distanceExpr;
	this.distanceVar = distVar;
	this.filter = filter;
	if (distanceFunction != null) {
		List<ValueExpr> args = distanceFunction.getArgs();
		this.from = getLiteral(args.get(0));
		this.geoVar = getVarName(args.get(1));
		this.units = getURI(args.get(2));
	} else {
		this.from = null;
		this.geoVar = null;
		this.units = null;
	}
	if (distanceExpr != null) {
		Literal dist = getLiteral(distanceExpr);
		this.distance = (dist != null) ? dist.doubleValue() : Double.NaN;
	} else {
		this.distance = Double.NaN;
	}
}
 
Example #7
Source File: JoinSegment.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param tupleExpr
 *            - the query object that will be traversed by this method
 * @param joinArgs
 *            - all nodes connected by Joins and Filters
 * @return - List containing all nodes connected by Joins, LeftJoins, and
 *         Filters. This List contains the
 * @param ValueExpr
 *            in place of the Filter
 */
private List<QueryModelNode> getJoinArgs(TupleExpr tupleExpr, List<QueryModelNode> joinArgs) {

    if (tupleExpr instanceof Join) {
        if (!(((Join) tupleExpr).getLeftArg() instanceof FixedStatementPattern)
                && !(((Join) tupleExpr).getRightArg() instanceof DoNotExpandSP)) {
            Join join = (Join) tupleExpr;
            getJoinArgs(join.getRightArg(), joinArgs);
            getJoinArgs(join.getLeftArg(), joinArgs);
        }
    } else if (tupleExpr instanceof Filter) {
        Filter filter = (Filter) tupleExpr;
        joinArgs.add(filter.getCondition());
        conditionMap.put(filter.getCondition(), filter);
        getJoinArgs(filter.getArg(), joinArgs);
    } else {
        joinArgs.add(tupleExpr);
    }
    return joinArgs;
}
 
Example #8
Source File: FilterEvaluatorTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void doesNotMatch() throws Exception {
    // Read the filter object from a SPARQL query.
    final Filter filter = getFilter(
            "SELECT * " +
            "WHERE { " +
                "FILTER(?age < 10)" +
                "?person <urn:age> ?age " +
            "}");

    // Create the input binding set.
    final ValueFactory vf = SimpleValueFactory.getInstance();
    final MapBindingSet bs = new MapBindingSet();
    bs.addBinding("person", vf.createIRI("urn:Alice"));
    bs.addBinding("age", vf.createLiteral(11));
    final VisibilityBindingSet visBs = new VisibilityBindingSet(bs);

    // Test the evaluator.
    assertFalse( FilterEvaluator.make(filter).filter(visBs) );
}
 
Example #9
Source File: QueryAlgebraUtil.java    From CostFed with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Construct the statement string, i.e. "s p ?o_varID FILTER ?o_N=o ". This kind of statement
 * pattern is necessary to later on identify available results.
 * 
 * @param stmt
 * @param varID
 * @param varNames
 * @param bindings
 * @return
 */
protected static TupleExpr constructStatementCheckId(StatementPattern stmt, int varID, Set<String> varNames, BindingSet bindings) {
	
	String _varID = Integer.toString(varID);
	Var subj = appendVarId(stmt.getSubjectVar(), _varID, varNames, bindings);
	Var pred = appendVarId(stmt.getPredicateVar(), _varID, varNames, bindings);
	
	Var obj = new Var("o_" + _varID);
	varNames.add("o_" + _varID);
			
	Value objValue;
	if (stmt.getObjectVar().hasValue()) {
		objValue = stmt.getObjectVar().getValue();
	} else if (bindings.hasBinding(stmt.getObjectVar().getName())){
		objValue = bindings.getBinding(stmt.getObjectVar().getName()).getValue();
	} else {
		// just to make sure that we see an error, will be deleted soon
		throw new RuntimeException("Unexpected.");
	}
	
	Compare cmp = new Compare(obj, new ValueConstant(objValue));
	cmp.setOperator(CompareOp.EQ);
	Filter filter = new Filter( new StatementPattern(subj, pred, obj), cmp);
			
	return filter;
}
 
Example #10
Source File: QueryNodesToTupleExpr.java    From rya with Apache License 2.0 6 votes vote down vote up
private static List<Filter> getFilterChain(Set<Filter> filters) {
    final List<Filter> filterTopBottom = Lists.newArrayList();
    Filter filterChainTop = null;
    Filter filterChainBottom = null;

    for (final Filter filter : filters) {
        if (filterChainTop == null) {
            filterChainTop = filter;
            filter.setParentNode(null);
        } else if (filterChainBottom == null) {
            filterChainBottom = filter;
            filterChainTop.setArg(filterChainBottom);
        } else {
            filterChainBottom.setArg(filter);
            filterChainBottom = filter;
        }
    }
    if (filterChainTop != null) {
        filterTopBottom.add(filterChainTop);
    }
    if (filterChainBottom != null) {
        filterTopBottom.add(filterChainBottom);
    }
    return filterTopBottom;
}
 
Example #11
Source File: StatementMetadataExternalSetProviderTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void createSingleMongoMetadataNode() throws MalformedQueryException {

    MongoDBRdfConfiguration conf = (MongoDBRdfConfiguration) getConf(true);
    Set<RyaIRI> propertySet = new HashSet<>();
    propertySet.add(new RyaIRI("http://createdBy"));
    conf.setStatementMetadataProperties(propertySet);
    StatementMetadataExternalSetProvider metaProvider = new StatementMetadataExternalSetProvider(conf);
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq = parser.parseQuery(query, null);

    List<QueryModelNode> patterns = new ArrayList<>();
    List<StatementMetadataNode<?>> expected = new ArrayList<>();
    Set<StatementPattern> sp = StatementMetadataTestUtils.getMetadataStatementPatterns(pq.getTupleExpr(), propertySet);

    patterns.addAll(StatementPatternCollector.process(pq.getTupleExpr()));
    JoinSegment<StatementMetadataNode<?>> segment = new JoinSegment<>(
            new HashSet<>(patterns), patterns, new HashMap<ValueExpr, Filter>());
    List<StatementMetadataNode<?>> extSets = metaProvider.getExternalSets(segment);

    expected.add(new StatementMetadataNode<>(sp,conf));

    Assert.assertEquals(expected, extSets);

}
 
Example #12
Source File: FilterSerializer.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a {@link Filter} to a SPARQL query containing only the SPARQL representation
 * of the Filter along with a Select clause that return all variables.  The argument of the
 * Filter is replaced by a {@link SingletonSet} so that the body of the SPARQL query consists of only a
 * single Filter clause.  
 * @param filter - Filter to be serialized
 * @return - SPARQL String containing a single Filter clause that represents the serialized Filter
 * @throws FilterParseException
 */
public static String serialize(Filter filter) throws FilterParseException {
    Filter clone = filter.clone();
    clone.setArg(new SingletonSet());
    try {
        return removeAngularBracketsFromNonUriFunctions(renderer.render(new ParsedTupleQuery(clone)));
    } catch (Exception e) {
        throw new FilterParseException("Unable to parse Filter.", e);
    }
}
 
Example #13
Source File: TupleExprsTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void isFilterExistsFunctionOnNotExist() {
	Filter expr = new Filter();
	expr.setArg(new StatementPattern());
	expr.setCondition(new Not(new Exists(new StatementPattern())));

	assertThat(isFilterExistsFunction(expr)).isTrue();
}
 
Example #14
Source File: RdfTestUtil.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * Get the first {@link Filter} node from a SPARQL query.
 *
 * @param sparql - The query that contains a single Projection node.
 * @return The first {@link Filter} that is encountered.
 * @throws Exception The query could not be parsed.
 */
public static @Nullable Filter getFilter(final String sparql) throws Exception {
    requireNonNull(sparql);

    final AtomicReference<Filter> filter = new AtomicReference<>();
    final ParsedQuery parsed = new SPARQLParser().parseQuery(sparql, null);
    parsed.getTupleExpr().visit(new AbstractQueryModelVisitor<Exception>() {
        @Override
        public void meet(final Filter node) throws Exception {
            filter.set(node);
        }
    });

    return filter.get();
}
 
Example #15
Source File: GeneralizedExternalProcessor.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void meet(Filter node) {

          Set<QueryModelNode> eSet = getQNodes(node);
          Set<QueryModelNode> compSet = Sets.difference(eSet, sSet);

          // if index contains filter node and it hasn't already been moved,
          // move it down
          // query tree just above position of statement pattern nodes found
          // in both query tree
          // and index (assuming that SPBubbleDownVisitor has already been
          // called)
          if (sSet.contains(node.getCondition()) && !bubbledFilters.contains(node.getCondition())) {
              FilterBubbleDownVisitor fbdv = new FilterBubbleDownVisitor(node.clone(), compSet);
              node.visit(fbdv);
              bubbledFilters.add(node.getCondition());
              // checks if filter correctly placed, and if it has been,
              // removes old copy of filter
              if (fbdv.filterPlaced()) {

                  QueryModelNode pNode = node.getParentNode();
                  TupleExpr cNode = node.getArg();
                  pNode.replaceChildNode(node, cNode);


                  super.meetNode(pNode);
              }
              super.meet(node);

          } else {
              super.meet(node);
          }
      }
 
Example #16
Source File: SparqlFluoQueryBuilder.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void meet(final Filter node) {

    // Get or create a builder for this node populated with the known metadata.
    final String filterId = nodeIds.getOrMakeId(node);

    FilterMetadata.Builder filterBuilder = fluoQueryBuilder.getFilterBuilder(filterId).orNull();
    if(filterBuilder == null) {
        filterBuilder = FilterMetadata.builder(filterId);
        fluoQueryBuilder.addFilterMetadata(filterBuilder);
    }

    String filterString;
    try {
        filterString = FilterSerializer.serialize(node);
    } catch (final FilterParseException e) {
        throw new RuntimeException(e);
    }
    filterBuilder.setFilterSparql(filterString);

    final QueryModelNode child = node.getArg();
    if(child == null) {
        throw new IllegalArgumentException("Filter arg connot be null.");
    }

    final String childNodeId = nodeIds.getOrMakeId(child);
    filterBuilder.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, filterId);

    // Walk to the next node.
    super.meet(node);
}
 
Example #17
Source File: SparqlToPipelineTransformVisitor.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void meet(Filter filterNode) throws Exception {
    filterNode.visitChildren(this);
    if (filterNode.getArg() instanceof AggregationPipelineQueryNode && filterNode.getParentNode() != null) {
        AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) filterNode.getArg();
        if (pipelineNode.filter(filterNode.getCondition())) {
            filterNode.replaceWith(pipelineNode);
        }
    }
}
 
Example #18
Source File: HalyardEvaluationStatistics.java    From Halyard with Apache License 2.0 5 votes vote down vote up
@Override
public void meet(Filter node) throws RuntimeException {
    super.meetUnaryTupleOperator(node);
    double subCost = cardinality;
    cardinality = 1;
    node.getCondition().visit(this);
    cardinality *= subCost;
    updateMap(node);
}
 
Example #19
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 #20
Source File: PCJOptimizerUtilities.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void meet(final Intersection node) {
	if (Sets.intersection(node.getRightArg().getBindingNames(), filterVars).size() > 0) {
		relocate(filter, node.getRightArg());
	} else if (Sets.intersection(node.getLeftArg().getBindingNames(), filterVars).size() > 0) {
		final Filter clone = new Filter(filter.getArg(), filter
				.getCondition().clone());
		relocate(clone, node.getLeftArg());
	}
}
 
Example #21
Source File: TupleExprsTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void isFilterExistsFunctionOnExists() {
	Filter expr = new Filter();
	expr.setArg(new StatementPattern());
	expr.setCondition(new Exists(new StatementPattern()));

	assertThat(isFilterExistsFunction(expr)).isTrue();

}
 
Example #22
Source File: EntityToSegmentConverter.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public QuerySegment<EntityQueryNode> setToSegment(final EntityQueryNode set) {
    Preconditions.checkNotNull(set);
    final Set<QueryModelNode> matched = new HashSet<>(set.getPatterns());
    final List<QueryModelNode> unmatched = new ArrayList<>(set.getPatterns());
    return new JoinSegment<EntityQueryNode>(matched, unmatched, new HashMap<ValueExpr, Filter>());
}
 
Example #23
Source File: TupleExprsTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void isFilterExistsFunctionOnNormalFilter() {
	Filter expr = new Filter();
	expr.setArg(new StatementPattern());
	expr.setCondition(new Compare(new Var("x", f.createBNode()), new Var("y", f.createBNode())));

	assertThat(isFilterExistsFunction(expr)).isFalse();
}
 
Example #24
Source File: FedXService.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected void meetNode(QueryModelNode node)
{
	if (node instanceof StatementTupleExpr) {
		nTriples++;
	} else if (node instanceof StatementPattern) {
		nTriples++;
	} else if (node instanceof Filter) {
		simple=false;
	} else if (node instanceof Union){
		simple=false;
	}
		
	super.meetNode(node);
}
 
Example #25
Source File: FilterResultUpdater.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * Updates the results of a Filter node when one of its child has added a
 * new Binding Set to its results.
 *
 * @param tx - The transaction all Fluo queries will use. (not null)
 * @param childBindingSet - A binding set that the query's child node has emitted. (not null)
 * @param filterMetadata - The metadata of the Filter whose results will be updated. (not null)
 * @throws Exception Something caused the update to fail.
 */
public void updateFilterResults(
        final TransactionBase tx,
        final VisibilityBindingSet childBindingSet,
        final FilterMetadata filterMetadata) throws Exception {
    checkNotNull(tx);
    checkNotNull(childBindingSet);
    checkNotNull(filterMetadata);

    log.trace("Transaction ID: {}\nFilter Node ID: {}\nBinding Set:\n{}\n", tx.getStartTimestamp(), filterMetadata.getNodeId(), childBindingSet);

    // Parse the original query and find the Filter that represents filterId.
    final String sparql = filterMetadata.getFilterSparql();
    final Filter filter = FilterSerializer.deserialize(sparql);

    // Evaluate whether the child BindingSet satisfies the filter's condition.
    final ValueExpr condition = filter.getCondition();
    if (isTrue(condition, childBindingSet)) {

        // Create the Row Key for the emitted binding set. It does not contain visibilities.
        final VariableOrder filterVarOrder = filterMetadata.getVariableOrder();
        final Bytes resultRow = makeRowKey(filterMetadata.getNodeId(), filterVarOrder, childBindingSet);

        // Serialize and emit BindingSet
        final Bytes nodeValueBytes = BS_SERDE.serialize(childBindingSet);
        log.trace("Transaction ID: {}\nNew Binding Set: {}\n", tx.getStartTimestamp(), childBindingSet);

        tx.set(resultRow, FluoQueryColumns.FILTER_BINDING_SET, nodeValueBytes);
    }
}
 
Example #26
Source File: JoinSegmentTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoMatch() throws Exception {

	String query1 = ""//
			+ "SELECT ?e ?c ?l" //
			+ "{" //
			+ " Filter(?e = <uri:Bob>)" //
			+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
			+ "  ?e a ?c . "//
			+ "  ?e <uri:talksTo> ?l  . "//
			+ "}";//

	String query2 = ""//
			+ "SELECT ?e ?c ?l" //
			+ "{" //
			+ " Filter(?e = <uri:Bob>)" //
			+ " ?e a ?c . "//
			+ " ?e <uri:worksAt> ?l . "//
			+ "}";//

	SPARQLParser parser = new SPARQLParser();
	ParsedQuery pq1 = parser.parseQuery(query1, null);
	ParsedQuery pq2 = parser.parseQuery(query2, null);
	TupleExpr te1 = pq1.getTupleExpr();
	TupleExpr te2 = pq2.getTupleExpr();
	TopOfQueryFilterRelocator.moveFiltersToTop(te1);
	TopOfQueryFilterRelocator.moveFiltersToTop(te1);
	Filter filter1 = (Filter) ((Projection) te1).getArg();
	Filter filter2 = (Filter) ((Projection) te2).getArg();

	QuerySegment<ExternalTupleSet> seg1 = qFactory.getQuerySegment(filter1);
       QuerySegment<ExternalTupleSet> seg2 = qFactory.getQuerySegment(filter2);


	Assert.assertEquals(false, seg1.containsQuerySegment(seg2));

}
 
Example #27
Source File: SameTermFilterOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void renameVar(Var oldVar, Var newVar, Filter filter) {
	filter.getArg().visit(new VarRenamer(oldVar, newVar));

	// TODO: skip this step if old variable name is not used
	// Replace SameTerm-filter with an Extension, the old variable name
	// might still be relevant to nodes higher in the tree
	Extension extension = new Extension(filter.getArg());
	extension.addElement(new ExtensionElem(new Var(newVar.getName()), oldVar.getName()));
	filter.replaceWith(extension);
}
 
Example #28
Source File: FilterFunctionOptimizer.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void meet(final FunctionCall call) {
    final IRI fnUri = VF.createIRI(call.getURI());
    final Var resultVar = IndexingFunctionRegistry.getResultVarFromFunctionCall(fnUri, call.getArgs());
    if (resultVar != null && resultVar.getName().equals(matchVar)) {
        addFilter(VF.createIRI(call.getURI()), extractArguments(matchVar, call));
        if (call.getParentNode() instanceof Filter || call.getParentNode() instanceof And || call.getParentNode() instanceof LeftJoin) {
            call.replaceWith(new ValueConstant(VF.createLiteral(true)));
        } else {
            throw new IllegalArgumentException("Query error: Found " + call + " as part of an expression that is too complex");
        }
    }
}
 
Example #29
Source File: FilterOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void relocate(Filter filter, TupleExpr newFilterArg) {
	if (filter.getArg() != newFilterArg) {
		if (filter.getParentNode() != null) {
			// Remove filter from its original location
			filter.replaceWith(filter.getArg());
		}

		// Insert filter at the new location
		newFilterArg.replaceWith(filter);
		filter.setArg(newFilterArg);
	}
}
 
Example #30
Source File: GeneralizedExternalProcessor.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void meet(Join node) {

          if (!compSet.contains(node.getRightArg())) {
              // looks for placed to position filter node. if right node is
              // contained in index
              // and left node is statement pattern node contained in index or
              // is a join, place
              // filter above join.
              if (node.getLeftArg() instanceof Join || !compSet.contains(node.getLeftArg())) {

                  QueryModelNode pNode = node.getParentNode();
                  ((Filter) filter).setArg(node);
                  pNode.replaceChildNode(node, filter);
                  filterPlaced = true;

                  return;
              } // otherwise place filter below join and above right arg
              else {
                  ((Filter) filter).setArg(node.getRightArg());
                  node.replaceChildNode(node.getRightArg(), filter);
                  filterPlaced = true;
                  return;

              }
          } else if (node.getLeftArg() instanceof StatementPattern && !compSet.contains(node.getLeftArg())) {

              ((Filter) filter).setArg(node.getLeftArg());
              node.replaceChildNode(node.getLeftArg(), filter);
              filterPlaced = true;

              return;
          } else {
              super.meet(node);
          }
      }