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

The following examples show how to use org.eclipse.rdf4j.query.algebra.Not. 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: 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 #2
Source File: QueryModelBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public List<Var> visit(ASTNode node, Object data) throws VisitorException {
	List<Var> nodeVars = new ArrayList<>();

	for (ASTNodeElem nodeElem : node.getNodeElemList()) {
		Var nodeVar = (Var) nodeElem.jjtAccept(this, null);
		nodeVars.add(nodeVar);
	}

	// Create any implicit unequalities
	for (int i = 0; i < nodeVars.size() - 1; i++) {
		Var var1 = nodeVars.get(i);

		for (int j = i + 1; j < nodeVars.size(); j++) {
			Var var2 = nodeVars.get(j);

			// At least one of the variables should be non-constant
			// for the unequality to make any sense:
			if (!var1.hasValue() || !var2.hasValue()) {
				graphPattern.addConstraint(new Not(new SameTerm(var1, var2)));
			}
		}
	}

	return nodeVars;
}
 
Example #3
Source File: FilterOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * returns true if this filter can be used for optimization. Currently no conjunctive or disjunctive expressions are
 * supported.
 *
 * @param e
 * @return whether the expression is compatible
 */
protected boolean isCompatibleExpr(ValueExpr e) {

	if (e instanceof And || e instanceof Or) {
		return false;
	}

	if (e instanceof Not) {
		return isCompatibleExpr(((Not) e).getArg());
	}

	return true;
}
 
Example #4
Source File: SPARQLValueExprRenderer.java    From semagrow with Apache License 2.0 5 votes vote down vote up
/**
 * @inheritDoc
 */
@Override
public void meet(Not theNot)
        throws Exception
{
    mBuffer.append("(");
    unaryMeet("!", theNot);
    mBuffer.append(")");
}
 
Example #5
Source File: AggregationPipelineQueryNodeTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testExtend() {
    final AggregationPipelineQueryNode base = new AggregationPipelineQueryNode(
            collection,
            new LinkedList<>(),
            Sets.newHashSet("x", "y"),
            Sets.newHashSet("x", "y", "opt"),
            HashBiMap.create());
    // Extend with a mix of variables and constants
    List<ExtensionElem> extensionElements = Arrays.asList(
            new ExtensionElem(new Var("x"), "subject"),
            new ExtensionElem(new ValueConstant(RDF.TYPE), "predicate"),
            new ExtensionElem(new Var("y"), "object"));
    AggregationPipelineQueryNode node = base.clone();
    boolean success = node.extend(extensionElements);
    Assert.assertTrue(success);
    Assert.assertEquals(1, node.getPipeline().size());
    Assert.assertEquals(Sets.newHashSet("x", "y", "subject", "predicate", "object"),
            node.getAssuredBindingNames());
    Assert.assertEquals(Sets.newHashSet("x", "y", "subject", "predicate", "object", "opt"),
            node.getBindingNames());
    // Attempt to extend with an unsupported expression
    extensionElements = Arrays.asList(
            new ExtensionElem(new Var("x"), "subject"),
            new ExtensionElem(new Not(new ValueConstant(VF.createLiteral(true))), "notTrue"));
    node = base.clone();
    success = node.extend(extensionElements);
    Assert.assertFalse(success);
    Assert.assertEquals(base, node);
}
 
Example #6
Source File: SparqlValueExprRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @inheritDoc
 */
@Override
public void meet(Not theNot) throws Exception {
	mBuffer.append("(");
	unaryMeet("!", theNot);
	mBuffer.append(")");
}
 
Example #7
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 #8
Source File: TupleExprsTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void isFilterExistsFunctionOnNormalNot() {
	Filter expr = new Filter();
	expr.setArg(new StatementPattern());
	expr.setCondition(new Not(new Compare(new Var("x", f.createBNode()), new Var("y", f.createBNode()))));

	assertThat(isFilterExistsFunction(expr)).isFalse();
}
 
Example #9
Source File: TupleExprs.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Verifies if the supplied expression is a FILTER (NOT) EXISTS operation
 *
 * @param expr a tuple expression
 * @return true if the supplied expression is a FILTER (NOT) EXISTS operation, false otherwise.
 */
public static boolean isFilterExistsFunction(TupleExpr expr) {
	if (expr instanceof Filter) {
		Filter filter = (Filter) expr;
		if (filter.getCondition() instanceof Exists) {
			return true;
		} else if (filter.getCondition() instanceof Not) {
			Not n = (Not) filter.getCondition();
			return (n.getArg() instanceof Exists);
		}
	}
	return false;
}
 
Example #10
Source File: SpinRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Exists node) throws RDFHandlerException {
	Resource currentSubj = subject;
	flushPendingStatement();
	Resource op = (node.getParentNode() instanceof Not) ? SP.NOT_EXISTS : SP.EXISTS;
	handler.handleStatement(valueFactory.createStatement(subject, RDF.TYPE, op));
	Resource elementsList = valueFactory.createBNode();
	handler.handleStatement(valueFactory.createStatement(subject, SP.ELEMENTS_PROPERTY, elementsList));
	ListContext elementsCtx = newList(elementsList);
	node.getSubQuery().visit(this);
	endList(elementsCtx);
	subject = currentSubj;
	predicate = null;
}
 
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(Not node) throws RDFHandlerException {
	if (node.getArg() instanceof Exists) {
		super.meet(node);
	} else {
		Resource currentSubj = subject;
		flushPendingStatement();
		handler.handleStatement(valueFactory.createStatement(subject, RDF.TYPE, SP.NOT));
		predicate = SP.ARG1_PROPERTY;
		node.getArg().visit(this);
		subject = currentSubj;
		predicate = null;
	}
}
 
Example #12
Source File: FilterOptimizer.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * returns true if this filter can be used for optimization. Currently no
 * conjunctive or disjunctive expressions are supported.
 * 
 * @param e
 * @return
 */
protected boolean isCompatibleExpr(ValueExpr e) {
	
	if (e instanceof And || e instanceof Or) {
		return false;
	}
	
	if (e instanceof Not) {
		return isCompatibleExpr( ((Not)e).getArg() );
	}
	
	return true;
}
 
Example #13
Source File: AbstractQueryModelVisitor.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void meet(Not node) throws X {
	meetUnaryValueOperator(node);
}
 
Example #14
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public Value evaluate(Not node, BindingSet bindings) throws QueryEvaluationException {
	Value argValue = evaluate(node.getArg(), bindings);
	boolean argBoolean = QueryEvaluationUtil.getEffectiveBooleanValue(argValue);
	return BooleanLiteral.valueOf(!argBoolean);
}
 
Example #15
Source File: SerqlValueExprRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * @inheritDoc
 */
@Override
public void meet(Not theNot) throws Exception {
	unaryMeet("not", theNot);
}
 
Example #16
Source File: FilterBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public GroupBuilder<T, E> not(ValueExpr theExpr) {
	return filter(new Not(theExpr));
}
 
Example #17
Source File: ValueExprFactory.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static Not not(ValueExpr theExpr) {
	return new Not(theExpr);
}
 
Example #18
Source File: TupleExprBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Not visit(ASTNot node, Object data) throws VisitorException {
	ValueExpr arg = (ValueExpr) super.visit(node, null);
	return new Not(arg);
}
 
Example #19
Source File: TupleExprBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Not visit(ASTNotExistsFunc node, Object data) throws VisitorException {

	GraphPattern parentGP = graphPattern;
	graphPattern = new GraphPattern(parentGP);

	Exists e = new Exists();
	node.jjtGetChild(0).jjtAccept(this, e);

	TupleExpr te = graphPattern.buildTupleExpr();

	e.setSubQuery(te);

	graphPattern = parentGP;

	return new Not(e);
}
 
Example #20
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Value evaluate(ValueExpr expr, BindingSet bindings)
		throws QueryEvaluationException {
	if (expr instanceof Var) {
		return evaluate((Var) expr, bindings);
	} else if (expr instanceof ValueConstant) {
		return evaluate((ValueConstant) expr, bindings);
	} else if (expr instanceof BNodeGenerator) {
		return evaluate((BNodeGenerator) expr, bindings);
	} else if (expr instanceof Bound) {
		return evaluate((Bound) expr, bindings);
	} else if (expr instanceof Str) {
		return evaluate((Str) expr, bindings);
	} else if (expr instanceof Label) {
		return evaluate((Label) expr, bindings);
	} else if (expr instanceof Lang) {
		return evaluate((Lang) expr, bindings);
	} else if (expr instanceof LangMatches) {
		return evaluate((LangMatches) expr, bindings);
	} else if (expr instanceof Datatype) {
		return evaluate((Datatype) expr, bindings);
	} else if (expr instanceof Namespace) {
		return evaluate((Namespace) expr, bindings);
	} else if (expr instanceof LocalName) {
		return evaluate((LocalName) expr, bindings);
	} else if (expr instanceof IsResource) {
		return evaluate((IsResource) expr, bindings);
	} else if (expr instanceof IsURI) {
		return evaluate((IsURI) expr, bindings);
	} else if (expr instanceof IsBNode) {
		return evaluate((IsBNode) expr, bindings);
	} else if (expr instanceof IsLiteral) {
		return evaluate((IsLiteral) expr, bindings);
	} else if (expr instanceof IsNumeric) {
		return evaluate((IsNumeric) expr, bindings);
	} else if (expr instanceof IRIFunction) {
		return evaluate((IRIFunction) expr, bindings);
	} else if (expr instanceof Regex) {
		return evaluate((Regex) expr, bindings);
	} else if (expr instanceof Coalesce) {
		return evaluate((Coalesce) expr, bindings);
	} else if (expr instanceof Like) {
		return evaluate((Like) expr, bindings);
	} else if (expr instanceof FunctionCall) {
		return evaluate((FunctionCall) expr, bindings);
	} else if (expr instanceof And) {
		return evaluate((And) expr, bindings);
	} else if (expr instanceof Or) {
		return evaluate((Or) expr, bindings);
	} else if (expr instanceof Not) {
		return evaluate((Not) expr, bindings);
	} else if (expr instanceof SameTerm) {
		return evaluate((SameTerm) expr, bindings);
	} else if (expr instanceof Compare) {
		return evaluate((Compare) expr, bindings);
	} else if (expr instanceof MathExpr) {
		return evaluate((MathExpr) expr, bindings);
	} else if (expr instanceof In) {
		return evaluate((In) expr, bindings);
	} else if (expr instanceof CompareAny) {
		return evaluate((CompareAny) expr, bindings);
	} else if (expr instanceof CompareAll) {
		return evaluate((CompareAll) expr, bindings);
	} else if (expr instanceof Exists) {
		return evaluate((Exists) expr, bindings);
	} else if (expr instanceof If) {
		return evaluate((If) expr, bindings);
	} else if (expr instanceof ListMemberOperator) {
		return evaluate((ListMemberOperator) expr, bindings);
	} else if (expr instanceof ValueExprTripleRef) {
		return evaluate((ValueExprTripleRef) expr, bindings);
	} else if (expr == null) {
		throw new IllegalArgumentException("expr must not be null");
	} else {
		throw new QueryEvaluationException("Unsupported value expr type: " + expr.getClass());
	}
}
 
Example #21
Source File: QueryModelBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Not visit(ASTNot node, Object data) throws VisitorException {
	return new Not((ValueExpr) super.visit(node, data));
}
 
Example #22
Source File: HalyardValueExprEvaluation.java    From Halyard with Apache License 2.0 4 votes vote down vote up
/**
 * Determines which evaluate method to call based on the type of {@link ValueExpr}
 * @param expr the expression to evaluate
 * @param bindings the set of named value bindings the set of named value bindings
 * @return the {@link Value} resulting from the evaluation
 * @throws ValueExprEvaluationException
 * @throws QueryEvaluationException
 */
Value evaluate(ValueExpr expr, BindingSet bindings) throws ValueExprEvaluationException, QueryEvaluationException {
    if (expr instanceof Var) {
        return evaluate((Var) expr, bindings);
    } else if (expr instanceof ValueConstant) {
        return evaluate((ValueConstant) expr, bindings);
    } else if (expr instanceof BNodeGenerator) {
        return evaluate((BNodeGenerator) expr, bindings);
    } else if (expr instanceof Bound) {
        return evaluate((Bound) expr, bindings);
    } else if (expr instanceof Str) {
        return evaluate((Str) expr, bindings);
    } else if (expr instanceof Label) {
        return evaluate((Label) expr, bindings);
    } else if (expr instanceof Lang) {
        return evaluate((Lang) expr, bindings);
    } else if (expr instanceof LangMatches) {
        return evaluate((LangMatches) expr, bindings);
    } else if (expr instanceof Datatype) {
        return evaluate((Datatype) expr, bindings);
    } else if (expr instanceof Namespace) {
        return evaluate((Namespace) expr, bindings);
    } else if (expr instanceof LocalName) {
        return evaluate((LocalName) expr, bindings);
    } else if (expr instanceof IsResource) {
        return evaluate((IsResource) expr, bindings);
    } else if (expr instanceof IsURI) {
        return evaluate((IsURI) expr, bindings);
    } else if (expr instanceof IsBNode) {
        return evaluate((IsBNode) expr, bindings);
    } else if (expr instanceof IsLiteral) {
        return evaluate((IsLiteral) expr, bindings);
    } else if (expr instanceof IsNumeric) {
        return evaluate((IsNumeric) expr, bindings);
    } else if (expr instanceof IRIFunction) {
        return evaluate((IRIFunction) expr, bindings);
    } else if (expr instanceof Regex) {
        return evaluate((Regex) expr, bindings);
    } else if (expr instanceof Coalesce) {
        return evaluate((Coalesce) expr, bindings);
    } else if (expr instanceof Like) {
        return evaluate((Like) expr, bindings);
    } else if (expr instanceof FunctionCall) {
        return evaluate((FunctionCall) expr, bindings);
    } else if (expr instanceof And) {
        return evaluate((And) expr, bindings);
    } else if (expr instanceof Or) {
        return evaluate((Or) expr, bindings);
    } else if (expr instanceof Not) {
        return evaluate((Not) expr, bindings);
    } else if (expr instanceof SameTerm) {
        return evaluate((SameTerm) expr, bindings);
    } else if (expr instanceof Compare) {
        return evaluate((Compare) expr, bindings);
    } else if (expr instanceof MathExpr) {
        return evaluate((MathExpr) expr, bindings);
    } else if (expr instanceof In) {
        return evaluate((In) expr, bindings);
    } else if (expr instanceof CompareAny) {
        return evaluate((CompareAny) expr, bindings);
    } else if (expr instanceof CompareAll) {
        return evaluate((CompareAll) expr, bindings);
    } else if (expr instanceof Exists) {
        return evaluate((Exists) expr, bindings);
    } else if (expr instanceof If) {
        return evaluate((If) expr, bindings);
    } else if (expr instanceof ListMemberOperator) {
        return evaluate((ListMemberOperator) expr, bindings);
    } else if (expr == null) {
        throw new IllegalArgumentException("expr must not be null");
    } else {
        throw new QueryEvaluationException("Unsupported value expr type: " + expr.getClass());
    }
}
 
Example #23
Source File: HalyardValueExprEvaluation.java    From Halyard with Apache License 2.0 2 votes vote down vote up
/**
 * Evaluate a {@link Not} node
 * @param node the node to evaluate
 * @param bindings the set of named value bindings
 * @return
 * @throws ValueExprEvaluationException
 * @throws QueryEvaluationException
 */
private Value evaluate(Not node, BindingSet bindings) throws ValueExprEvaluationException, QueryEvaluationException {
    Value argValue = evaluate(node.getArg(), bindings);
    boolean argBoolean = QueryEvaluationUtil.getEffectiveBooleanValue(argValue);
    return BooleanLiteral.valueOf(!argBoolean);
}