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

The following examples show how to use org.eclipse.rdf4j.query.algebra.SingletonSet. 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: SpinParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void visitDeleteData(Resource query) throws RDF4JException {
	SingletonSet stub = new SingletonSet();
	tupleRoot = new QueryRoot(stub);
	tupleNode = stub;
	TupleExpr deleteExpr;
	Value delete = TripleSources.singleValue(query, SP.DATA_PROPERTY, store);
	if (!(delete instanceof Resource)) {
		throw new MalformedSpinException(String.format("Value of %s is not a resource", SP.DATA_PROPERTY));
	}
	visitDelete((Resource) delete);
	deleteExpr = tupleNode;
	deleteExpr.setParentNode(null);

	DataVisitor visitor = new DataVisitor();
	deleteExpr.visit(visitor);
	updateRoot = new DeleteData(visitor.getData());
}
 
Example #2
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 #3
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 #4
Source File: QuerySpec.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public QueryModelNode removeQueryPatterns() {
	final Supplier<? extends QueryModelNode> replacement = SingletonSet::new;

	replace(getQueryPattern(), replacement);
	replace(getScorePattern(), replacement);
	replace(getPropertyPattern(), replacement);
	replace(getSnippetPattern(), replacement);
	replace(getTypePattern(), replacement);

	final QueryModelNode placeholder = new SingletonSet();

	getMatchesPattern().replaceWith(placeholder);

	return placeholder;
}
 
Example #5
Source File: SpinParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void visitInsertData(Resource query) throws RDF4JException {
	SingletonSet stub = new SingletonSet();
	tupleRoot = new QueryRoot(stub);
	tupleNode = stub;
	TupleExpr insertExpr;
	Value insert = TripleSources.singleValue(query, SP.DATA_PROPERTY, store);
	if (!(insert instanceof Resource)) {
		throw new MalformedSpinException(String.format("Value of %s is not a resource", SP.DATA_PROPERTY));
	}
	visitInsert((Resource) insert);
	insertExpr = tupleNode;
	insertExpr.setParentNode(null);

	DataVisitor visitor = new DataVisitor();
	insertExpr.visit(visitor);
	updateRoot = new InsertData(visitor.getData());
}
 
Example #6
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 #7
Source File: TupleExprBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Object visit(ASTServiceGraphPattern node, Object data) throws VisitorException {
	GraphPattern parentGP = graphPattern;

	ValueExpr serviceRef = (ValueExpr) node.jjtGetChild(0).jjtAccept(this, null);

	graphPattern = new GraphPattern(parentGP);
	node.jjtGetChild(1).jjtAccept(this, null);
	TupleExpr serviceExpr = graphPattern.buildTupleExpr();

	if (serviceExpr instanceof SingletonSet) {
		return null; // do not add an empty service block
	}

	String serviceExpressionString = node.getPatternString();

	parentGP.addRequiredTE(new Service(mapValueExprToVar(serviceRef), serviceExpr, serviceExpressionString,
			node.getPrefixDeclarations(), node.getBaseURI(), node.isSilent()));

	graphPattern = parentGP;

	return null;
}
 
Example #8
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 #9
Source File: QueryModelNormalizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Difference difference) {
	super.meet(difference);

	TupleExpr leftArg = difference.getLeftArg();
	TupleExpr rightArg = difference.getRightArg();

	if (leftArg instanceof EmptySet) {
		difference.replaceWith(leftArg);
	} else if (rightArg instanceof EmptySet) {
		difference.replaceWith(leftArg);
	} else if (leftArg instanceof SingletonSet && rightArg instanceof SingletonSet) {
		difference.replaceWith(new EmptySet());
	}
}
 
Example #10
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 #11
Source File: QueryModelNormalizerTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testNormalizeUnionWithEmptyLeft() {
	Projection p = new Projection();
	Union union = new Union();
	SingletonSet s = new SingletonSet();
	union.setLeftArg(new EmptySet());
	union.setRightArg(s);
	p.setArg(union);

	subject.meet(union);

	assertThat(p.getArg()).isEqualTo(s);
}
 
Example #12
Source File: QueryModelNormalizerTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testNormalizeUnionWithEmptyRight() {
	Projection p = new Projection();
	Union union = new Union();
	SingletonSet s = new SingletonSet();
	union.setRightArg(new EmptySet());
	union.setLeftArg(s);
	p.setArg(union);

	subject.meet(union);

	assertThat(p.getArg()).isEqualTo(s);
}
 
Example #13
Source File: QueryModelNormalizerTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @see https://github.com/eclipse/rdf4j/issues/1404
 */
@Test
public void testNormalizeUnionWithTwoSingletons() {
	Projection p = new Projection();
	Union union = new Union();
	union.setRightArg(new SingletonSet());
	union.setLeftArg(new SingletonSet());
	p.setArg(union);

	subject.meet(union);

	assertThat(p.getArg()).isEqualTo(union);
}
 
Example #14
Source File: HalyardTupleExprEvaluation.java    From Halyard with Apache License 2.0 5 votes vote down vote up
/**
 * Evaluate {@link SingletonSet} query model nodes
 * @param parent
 * @param singletonSet
 * @param bindings
 */
private void evaluateSingletonSet(BindingSetPipe parent, SingletonSet singletonSet, BindingSet bindings) {
    try {
        if (parent.push(bindings)) {
            parent.push(null);
        }
    } catch (InterruptedException e) {
        parent.handleException(e);
    }
}
 
Example #15
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 #16
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 #17
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 #18
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 #19
Source File: QueryModelPruner.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void meetMultiJoin(NaryJoin join) {
	super.meetOther(join);
	for (TupleExpr arg : join.getArgs()) {
		if (arg instanceof SingletonSet) {
			join.removeArg(arg);
		} else if (arg instanceof EmptySet) {
			join.replaceWith(new EmptySet()); // NOPMD
			return;
		}
	}
	if (join.getNumberOfArguments() == 1) {
		join.replaceWith(join.getArg(0));
	}
}
 
Example #20
Source File: QueryModelPruner.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Join join) {
	super.meet(join);

	TupleExpr leftArg = join.getLeftArg();
	TupleExpr rightArg = join.getRightArg();

	if (leftArg instanceof EmptySet || rightArg instanceof EmptySet) {
		join.replaceWith(new EmptySet());
	} else if (leftArg instanceof SingletonSet) {
		join.replaceWith(rightArg);
	} else if (rightArg instanceof SingletonSet) {
		join.replaceWith(leftArg);
	}
}
 
Example #21
Source File: QueryModelPruner.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(LeftJoin leftJoin) {
	super.meet(leftJoin);
	TupleExpr leftArg = leftJoin.getLeftArg();
	TupleExpr rightArg = leftJoin.getRightArg();
	ValueExpr condition = leftJoin.getCondition();
	if (leftArg instanceof EmptySet) {
		leftJoin.replaceWith(leftArg);
	} else if (rightArg instanceof EmptySet) {
		leftJoin.replaceWith(leftArg);
	} else if (rightArg instanceof SingletonSet) {
		leftJoin.replaceWith(leftArg);
	} else if (condition instanceof ValueConstant) {
		boolean conditionValue;
		try {
			conditionValue = QueryEvaluationUtil
					.getEffectiveBooleanValue(((ValueConstant) condition).getValue());
		} catch (ValueExprEvaluationException e) {
			conditionValue = false;
		}
		if (conditionValue) {
			leftJoin.setCondition(null);
		} else {
			// Constraint is always false
			leftJoin.replaceWith(leftArg);
		}
	}
}
 
Example #22
Source File: QueryModelPruner.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Difference difference) {
	super.meet(difference);

	TupleExpr leftArg = difference.getLeftArg();
	TupleExpr rightArg = difference.getRightArg();

	if (leftArg instanceof EmptySet) {
		difference.replaceWith(leftArg);
	} else if (rightArg instanceof EmptySet) {
		difference.replaceWith(leftArg);
	} else if (leftArg instanceof SingletonSet && rightArg instanceof SingletonSet) {
		difference.replaceWith(new EmptySet());
	}
}
 
Example #23
Source File: QueryModelPruner.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Union union) {
	super.meet(union);
	TupleExpr leftArg = union.getLeftArg();
	TupleExpr rightArg = union.getRightArg();
	if (leftArg instanceof EmptySet) {
		union.replaceWith(rightArg);
	} else if (rightArg instanceof EmptySet) {
		union.replaceWith(leftArg);
	} else if (leftArg instanceof SingletonSet && rightArg instanceof SingletonSet) {
		union.replaceWith(leftArg);
	}
}
 
Example #24
Source File: QueryModelPruner.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Difference difference) {
	super.meet(difference);
	TupleExpr leftArg = difference.getLeftArg();
	TupleExpr rightArg = difference.getRightArg();
	if (leftArg instanceof EmptySet) {
		difference.replaceWith(leftArg);
	} else if (rightArg instanceof EmptySet) {
		difference.replaceWith(leftArg);
	} else if (leftArg instanceof SingletonSet && rightArg instanceof SingletonSet) {
		difference.replaceWith(new EmptySet());
	}
}
 
Example #25
Source File: QueryModelPruner.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Union union) {
	super.meet(union);

	TupleExpr leftArg = union.getLeftArg();
	TupleExpr rightArg = union.getRightArg();

	if (leftArg instanceof EmptySet) {
		union.replaceWith(rightArg);
	} else if (rightArg instanceof EmptySet) {
		union.replaceWith(leftArg);
	} else if (leftArg instanceof SingletonSet && rightArg instanceof SingletonSet) {
		union.replaceWith(leftArg);
	}
}
 
Example #26
Source File: QueryModelNormalizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(LeftJoin leftJoin) {
	super.meet(leftJoin);

	TupleExpr leftArg = leftJoin.getLeftArg();
	TupleExpr rightArg = leftJoin.getRightArg();
	ValueExpr condition = leftJoin.getCondition();

	if (leftArg instanceof EmptySet) {
		leftJoin.replaceWith(leftArg);
	} else if (rightArg instanceof EmptySet) {
		leftJoin.replaceWith(leftArg);
	} else if (rightArg instanceof SingletonSet) {
		leftJoin.replaceWith(leftArg);
	} else if (condition instanceof ValueConstant) {
		boolean conditionValue;
		try {
			conditionValue = QueryEvaluationUtil.getEffectiveBooleanValue(((ValueConstant) condition).getValue());
		} catch (ValueExprEvaluationException e) {
			conditionValue = false;
		}

		if (conditionValue == false) {
			// Constraint is always false
			leftJoin.replaceWith(leftArg);
		} else {
			leftJoin.setCondition(null);
		}
	}
}
 
Example #27
Source File: QueryModelPruner.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(LeftJoin leftJoin) {
	super.meet(leftJoin);

	TupleExpr leftArg = leftJoin.getLeftArg();
	TupleExpr rightArg = leftJoin.getRightArg();
	ValueExpr condition = leftJoin.getCondition();

	if (leftArg instanceof EmptySet) {
		leftJoin.replaceWith(leftArg);
	} else if (rightArg instanceof EmptySet) {
		leftJoin.replaceWith(leftArg);
	} else if (rightArg instanceof SingletonSet) {
		leftJoin.replaceWith(leftArg);
	} else if (condition instanceof ValueConstant) {
		boolean conditionValue;
		try {
			conditionValue = QueryEvaluationUtil
					.getEffectiveBooleanValue(((ValueConstant) condition).getValue());
		} catch (ValueExprEvaluationException e) {
			conditionValue = false;
		}

		if (conditionValue == false) {
			// Constraint is always false
			leftJoin.replaceWith(leftArg);
		} else {
			leftJoin.setCondition(null);
		}
	}
}
 
Example #28
Source File: QueryModelPruner.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Join join) {
	super.meet(join);

	TupleExpr leftArg = join.getLeftArg();
	TupleExpr rightArg = join.getRightArg();

	if (leftArg instanceof EmptySet || rightArg instanceof EmptySet) {
		join.replaceWith(new EmptySet());
	} else if (leftArg instanceof SingletonSet) {
		join.replaceWith(rightArg);
	} else if (rightArg instanceof SingletonSet) {
		join.replaceWith(leftArg);
	}
}
 
Example #29
Source File: HalyardEvaluationStatistics.java    From Halyard with Apache License 2.0 4 votes vote down vote up
@Override
public void meet(SingletonSet node) {
    super.meet(node);
    updateMap(node);
}
 
Example #30
Source File: SpinConstructRule.java    From rya with Apache License 2.0 4 votes vote down vote up
@Override
public void meet(SingletonSet node) {
    if (typeRequirement != null) {
        node.replaceWith(typeRequirement);
    }
}