Java Code Examples for org.eclipse.rdf4j.query.algebra.Var#setAnonymous()

The following examples show how to use org.eclipse.rdf4j.query.algebra.Var#setAnonymous() . 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: EvaluationStatistics.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(ArbitraryLengthPath node) {
	final Var pathVar = new Var("_anon_" + UUID.randomUUID().toString().replaceAll("-", "_"));
	pathVar.setAnonymous(true);
	// cardinality of ALP is determined based on the cost of a
	// single ?s ?p ?o ?c pattern where ?p is unbound, compensating for the fact that
	// the length of the path is unknown but expected to be _at least_ twice that of a normal
	// statement pattern.
	cardinality = 2.0 * getCardinality(
			new StatementPattern(node.getSubjectVar(), pathVar, node.getObjectVar(), node.getContextVar()));
}
 
Example 2
Source File: TupleExprs.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Creates an (anonymous) Var representing a constant value. The variable name will be derived from the actual value
 * to guarantee uniqueness.
 *
 * @param value
 * @return an (anonymous) Var representing a constant value.
 */
public static Var createConstVar(Value value) {
	String varName = getConstVarName(value);
	Var var = new Var(varName);
	var.setConstant(true);
	var.setAnonymous(true);
	var.setValue(value);
	return var;
}
 
Example 3
Source File: SpinConstructRuleTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testAnonymousConsequent() throws Exception {
    String text = "CONSTRUCT {\n"
            + "  ?x ?p2 _:something"
            + "} WHERE {\n"
            + "  ?x ?p1 ?y .\n"
            + "  ?p1 rdfs:subPropertyOf ?p2 .\n"
            + "}";
    ParsedGraphQuery query = (ParsedGraphQuery) PARSER.parseQuery(text, null);
    SpinConstructRule rule = new SpinConstructRule(OWL.THING, RL_PRP_SPO1, query);
    Multiset<StatementPattern> expectedAntecedents = HashMultiset.create(Arrays.asList(
            new StatementPattern(new Var("p1"), ac(RDFS.SUBPROPERTYOF), new Var("p2")),
            new StatementPattern(new Var("x"), new Var("p1"), new Var("y"))));
    Assert.assertEquals(expectedAntecedents, HashMultiset.create(rule.getAntecedentPatterns()));
    // should have detected anonymous node
    Assert.assertTrue(rule.hasAnonymousConsequent());
    Var anonymousObject = new Var("object");
    anonymousObject.setAnonymous(true);
    Multiset<StatementPattern> expectedConsequents = HashMultiset.create(Arrays.asList(
            new StatementPattern(new Var("subject"), new Var("predicate"), anonymousObject)));
    Assert.assertEquals(expectedConsequents, HashMultiset.create(rule.getConsequentPatterns()));
    // Pattern matches should be unaffected by anonymous node status
    Assert.assertTrue(rule.canConclude(new StatementPattern(new Var("a"), new Var("b"), new Var("c"))));
    Assert.assertTrue(rule.canConclude(new StatementPattern(new Var("x"), c(RDFS.SUBPROPERTYOF), c(OWL.THING))));
    Assert.assertTrue(rule.canConclude(new StatementPattern(c(OWL.NOTHING), new Var("prop"), c(OWL.THING))));
    Assert.assertTrue(rule.canConclude(new StatementPattern(c(FOAF.PERSON), c(RDFS.SUBCLASSOF), new Var("x"))));
    Assert.assertTrue(rule.canConclude(new StatementPattern(c(OWL.NOTHING), c(RDFS.SUBCLASSOF), c(FOAF.PERSON))));
}
 
Example 4
Source File: TupleExprBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Creates an anonymous Var with a unique, randomly generated, variable name.
 *
 * @return an anonymous Var with a unique, randomly generated, variable name
 */
protected Var createAnonVar() {
	// dashes ('-') in the generated UUID are replaced with underscores so
	// the
	// varname
	// remains compatible with the SPARQL grammar. See SES-2310.
	final Var var = new Var("_anon_" + UUID.randomUUID().toString().replaceAll("-", "_"));
	var.setAnonymous(true);
	return var;
}
 
Example 5
Source File: ConstructConsequentVisitor.java    From rya with Apache License 2.0 5 votes vote down vote up
private void recordConsequent(ProjectionElemList variables, List<ExtensionElem> extensionElements) {
    Map<String, Value> bindings = new ConcurrentHashMap<>();
    Map<String, Value> values = new ConcurrentHashMap<>();
    Set<String> queryBnodes = new HashSet<>();
    Set<String> projectedBnodes = new HashSet<>();
    for (ExtensionElem ee : extensionElements) {
        if (ee.getExpr() instanceof ValueConstant) {
            bindings.put(ee.getName(), ((ValueConstant) ee.getExpr()).getValue());
        }
        else if (ee.getExpr() instanceof BNodeGenerator) {
            queryBnodes.add(ee.getName());
        }
    }
    for (ProjectionElem var : variables.getElements()) {
        String sourceName = var.getSourceName();
        String targetName = var.getTargetName();
        Value constValue = bindings.get(sourceName);
        if (constValue != null) {
            values.put(targetName, constValue);
        }
        else if (queryBnodes.contains(sourceName)) {
            projectedBnodes.add(targetName);
        }
    }
    Var subjVar = new Var(SUBJECT_VAR_NAME, values.get(SUBJECT_VAR_NAME));
    Var predVar = new Var(PREDICATE_VAR_NAME, values.get(PREDICATE_VAR_NAME));
    Var objVar = new Var(OBJECT_VAR_NAME, values.get(OBJECT_VAR_NAME));
    subjVar.setAnonymous(projectedBnodes.contains(SUBJECT_VAR_NAME));
    predVar.setAnonymous(projectedBnodes.contains(PREDICATE_VAR_NAME));
    objVar.setAnonymous(projectedBnodes.contains(OBJECT_VAR_NAME));
    StatementPattern sp = new StatementPattern(subjVar, predVar, objVar);
    consequentStatementPatterns.add(sp);
}
 
Example 6
Source File: SomeValuesFromVisitor.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * Checks whether the StatementPattern is a type query whose solutions could be inferred by
 * someValuesFrom inference, and if so, replaces the node with a union of itself and any
 * possible inference.
 */
@Override
protected void meetSP(StatementPattern node) throws Exception {
    final Var subjVar = node.getSubjectVar();
    final Var predVar = node.getPredicateVar();
    final Var objVar = node.getObjectVar();
    // Only applies to type queries where the type is defined
    if (predVar != null && RDF.TYPE.equals(predVar.getValue()) && objVar != null && objVar.getValue() instanceof Resource) {
        final Resource typeToInfer = (Resource) objVar.getValue();
        Map<Resource, Set<IRI>> relevantSvfRestrictions = inferenceEngine.getSomeValuesFromByRestrictionType(typeToInfer);
        if (!relevantSvfRestrictions.isEmpty()) {
            // We can infer the queried type if it is to a someValuesFrom restriction (or a
            // supertype of one), and the node in question (subjVar) is the subject of a triple
            // whose predicate is the restriction's property and whose object is an arbitrary
            // node of the restriction's value type.
            final Var valueTypeVar = new Var("t-" + UUID.randomUUID());
            final Var svfPredVar = new Var("p-" + UUID.randomUUID());
            final Var neighborVar = new Var("n-" + UUID.randomUUID());
            neighborVar.setAnonymous(true);
            final StatementPattern membershipPattern = new DoNotExpandSP(neighborVar,
                    new Var(RDF.TYPE.stringValue(), RDF.TYPE), valueTypeVar);
            final StatementPattern valuePattern = new StatementPattern(subjVar, svfPredVar, neighborVar);
            final InferJoin svfPattern = new InferJoin(membershipPattern, valuePattern);
            // Use a FixedStatementPattern to contain the appropriate (predicate, value type)
            // pairs, and check each one against the general pattern.
            final FixedStatementPattern svfPropertyTypes = new FixedStatementPattern(svfPredVar,
                    new Var(OWL.SOMEVALUESFROM.stringValue(), OWL.SOMEVALUESFROM), valueTypeVar);
            for (Resource svfValueType : relevantSvfRestrictions.keySet()) {
                for (IRI svfProperty : relevantSvfRestrictions.get(svfValueType)) {
                    svfPropertyTypes.statements.add(new NullableStatementImpl(svfProperty,
                            OWL.SOMEVALUESFROM, svfValueType));
                }
            }
            final InferJoin svfInferenceQuery = new InferJoin(svfPropertyTypes, svfPattern);
            node.replaceWith(new InferUnion(node.clone(), svfInferenceQuery));
        }
    }
}
 
Example 7
Source File: AllValuesFromVisitor.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * Checks whether the StatementPattern is a type query whose solutions could be inferred
 * by allValuesFrom inference, and if so, replaces the node with a union of itself and any
 * possible inference.
 */
@Override
protected void meetSP(StatementPattern node) throws Exception {
    final Var subjVar = node.getSubjectVar();
    final Var predVar = node.getPredicateVar();
    final Var objVar = node.getObjectVar();
    // Only applies to type queries where the type is defined
    if (predVar != null && RDF.TYPE.equals(predVar.getValue()) && objVar != null && objVar.getValue() instanceof Resource) {
        final Resource typeToInfer = (Resource) objVar.getValue();
        Map<Resource, Set<IRI>> relevantAvfRestrictions = inferenceEngine.getAllValuesFromByValueType(typeToInfer);
        if (!relevantAvfRestrictions.isEmpty()) {
            // We can infer the queried type if, for an allValuesFrom restriction type
            // associated  with the queried type, some anonymous neighboring node belongs to the
            // restriction type and has the node in question (subjVar) as a value for the
            // restriction's property.
            final Var avfTypeVar = new Var("t-" + UUID.randomUUID());
            final Var avfPredVar = new Var("p-" + UUID.randomUUID());
            final Var neighborVar = new Var("n-" + UUID.randomUUID());
            neighborVar.setAnonymous(true);
            final StatementPattern membershipPattern = new DoNotExpandSP(neighborVar,
                    new Var(RDF.TYPE.stringValue(), RDF.TYPE), avfTypeVar);
            final StatementPattern valuePattern = new StatementPattern(neighborVar, avfPredVar, subjVar);
            final InferJoin avfPattern = new InferJoin(membershipPattern, valuePattern);
            // Use a FixedStatementPattern to contain the appropriate (restriction, predicate)
            // pairs, and check each one against the general pattern.
            final FixedStatementPattern avfPropertyTypes = new FixedStatementPattern(avfTypeVar,
                    new Var(OWL.ONPROPERTY.stringValue(), OWL.ONPROPERTY), avfPredVar);
            for (Resource avfRestrictionType : relevantAvfRestrictions.keySet()) {
                for (IRI avfProperty : relevantAvfRestrictions.get(avfRestrictionType)) {
                    avfPropertyTypes.statements.add(new NullableStatementImpl(avfRestrictionType,
                            OWL.ONPROPERTY, avfProperty));
                }
            }
            final InferJoin avfInferenceQuery = new InferJoin(avfPropertyTypes, avfPattern);
            node.replaceWith(new InferUnion(node.clone(), avfInferenceQuery));
        }
    }
}
 
Example 8
Source File: QueryVariableNormalizer.java    From rya with Apache License 2.0 5 votes vote down vote up
public void meet(Var var) {
    if (!var.isConstant() && hMap.containsKey(var.getName())) {
        String val = hMap.get(var.getName());
        if (VarNameUtils.isConstant(val)) {
           var.setName(val);
           var.setValue(valMap.get(val));
           var.setAnonymous(true); //TODO this might be a hack -- when are Vars not anonymous?
        } else {
            var.setName(val);
        }
    }
}
 
Example 9
Source File: ConstructConsequentVisitorTest.java    From rya with Apache License 2.0 4 votes vote down vote up
private static Var anon(Var var) {
    var.setAnonymous(true);
    return var;
}
 
Example 10
Source File: RulesetTest.java    From rya with Apache License 2.0 4 votes vote down vote up
private static Var c(Value val) {
    final Var v = VarNameUtils.createUniqueConstVar(val);
    v.setAnonymous(true);
    return v;
}
 
Example 11
Source File: SpinConstructRuleTest.java    From rya with Apache License 2.0 4 votes vote down vote up
private static Var ac(Value val) {
    Var v = c(val);
    v.setAnonymous(true);
    return v;
}
 
Example 12
Source File: AntecedentVisitorTest.java    From rya with Apache License 2.0 4 votes vote down vote up
private static Var c(Value val) {
    final Var v = VarNameUtils.createUniqueConstVar(val);
    v.setAnonymous(true);
    return v;
}
 
Example 13
Source File: QueryModelBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Var visit(ASTVar node, Object data) throws VisitorException {
	Var var = new Var(node.getName());
	var.setAnonymous(node.isAnonymous());
	return var;
}
 
Example 14
Source File: QueryModelBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private Var createConstantVar(Value value) {
	Var var = new Var("-const-" + constantVarID++);
	var.setAnonymous(true);
	var.setValue(value);
	return var;
}
 
Example 15
Source File: TupleExprBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Var visit(ASTVar node, Object data) throws VisitorException {
	Var var = new Var(node.getName());
	var.setAnonymous(node.isAnonymous());
	return var;
}
 
Example 16
Source File: GroupBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static Var valueToVar(Value theValue) {
	Var aVar = new Var("var" + cnt++, theValue);
	aVar.setAnonymous(true);

	return aVar;
}
 
Example 17
Source File: ZeroLengthPathIteration.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public Var createAnonVar(String varName) {
	Var var = new Var(varName);
	var.setAnonymous(true);
	return var;
}
 
Example 18
Source File: PathIteration.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public Var createAnonVar(String varName) {
	Var var = new Var(varName);
	var.setAnonymous(true);
	return var;
}