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

The following examples show how to use org.eclipse.rdf4j.query.algebra.Var#getValue() . 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: TransitivePropertyVisitor.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
protected void meetSP(final StatementPattern node) throws Exception {
    final StatementPattern sp = node.clone();
    final Var predVar = sp.getPredicateVar();

    final IRI pred = (IRI) predVar.getValue();
    final String predNamespace = pred.getNamespace();

    final Var objVar = sp.getObjectVar();
    final Var cntxtVar = sp.getContextVar();
    if (objVar != null &&
            !RDF.NAMESPACE.equals(predNamespace) &&
            !SESAME.NAMESPACE.equals(predNamespace) &&
            !RDFS.NAMESPACE.equals(predNamespace)
            && !EXPANDED.equals(cntxtVar)) {

        final IRI transPropIri = (IRI) predVar.getValue();
        if (inferenceEngine.isTransitiveProperty(transPropIri)) {
            node.replaceWith(new TransitivePropertySP(sp.getSubjectVar(), sp.getPredicateVar(), sp.getObjectVar(), sp.getContextVar()));
        }
    }
}
 
Example 2
Source File: ReflexivePropertyVisitor.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * Check whether any solution for the {@link StatementPattern} could be derived from
 * reflexive property inference, and if so, replace the pattern with a union of itself and the
 * reflexive solution.
 */
@Override
protected void meetSP(StatementPattern node) throws Exception {
    // Only applies when the predicate is defined and reflexive
    final Var predVar = node.getPredicateVar();
    if (predVar.getValue() != null && inferenceEngine.isReflexiveProperty((IRI) predVar.getValue())) {
        final StatementPattern originalSP = node.clone();
        // The reflexive solution is a ZeroLengthPath between subject and
        // object: they can be matched to one another, whether constants or
        // variables.
        final Var subjVar = node.getSubjectVar();
        final Var objVar = node.getObjectVar();
        final ZeroLengthPath reflexiveSolution = new ZeroLengthPath(subjVar, objVar);
        node.replaceWith(new InferUnion(originalSP, reflexiveSolution));
    }
}
 
Example 3
Source File: SourceSelectorWithQueryTransform.java    From semagrow with Apache License 2.0 6 votes vote down vote up
private Collection<FuzzyEntry<Var>> transformVar(Var v) {
    Set<FuzzyEntry<Var>> vars = new HashSet<FuzzyEntry<Var>>();

    Value val = v.getValue();
    if (val != null && val instanceof IRI) {
        Collection<EquivalentIRI> uris =  getEquivalentURI(v);
        for(EquivalentIRI uri : uris)
        {
            Var v1 = transformVar(v,uri);
            vars.add(new FuzzyEntry<Var>(v1, uri.getProximity()));
        }
    }
    else {
        vars.add(new FuzzyEntry<Var>(v));
    }
    return vars;
}
 
Example 4
Source File: IntersectionOfVisitor.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
protected void meetSP(final StatementPattern node) throws Exception {
    final StatementPattern currentNode = node.clone();
    final Var subVar = node.getSubjectVar();
    final Var predVar = node.getPredicateVar();
    final Var objVar = node.getObjectVar();
    final Var conVar = node.getContextVar();
    if (predVar != null && objVar != null && objVar.getValue() != null && RDF.TYPE.equals(predVar.getValue()) && !EXPANDED.equals(conVar)) {
        final List<Set<Resource>> intersections = inferenceEngine.getIntersectionsImplying((IRI) objVar.getValue());
        if (intersections != null && !intersections.isEmpty()) {
            final List<TupleExpr> joins = new ArrayList<>();
            for (final Set<Resource> intersection : intersections) {
                final Set<Resource> sortedIntersection = new TreeSet<>(new ResourceComparator());
                sortedIntersection.addAll(intersection);

                // Create a join tree of all statement patterns in the
                // current intersection.
                final TupleExpr joinTree = createJoinTree(new ArrayList<>(sortedIntersection), subVar, conVar);
                if (joinTree != null) {
                    joins.add(joinTree);
                }
            }

            if (!joins.isEmpty()) {
                // Combine all the intersection join trees for the type
                // together into a union tree.  This will be a join tree if
                // only one intersection exists.
                final TupleExpr unionTree = createUnionTree(joins);
                // Union the above union tree of intersections with the
                // original node.
                final Union union = new InferUnion(unionTree, currentNode);
                node.replaceWith(union);
                log.trace("Replacing node with inferred intersection union: " + union);
            }
        }
    }
}
 
Example 5
Source File: ContextCollector.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private boolean isSameCtx(Var v1, Var v2) {
	if ((v1 != null && v1.getValue() != null) && (v2 != null && v2.getValue() != null)) {
		return v1.getValue().equals(v2.getValue());
	} else if ((v1 != null && v1.getName() != null) && (v2 != null && v2.getName() != null)) {
		return v1.getName().equals(v2.getName());
	}

	return false;
}
 
Example 6
Source File: SourceSelectorWithQueryTransform.java    From semagrow with Apache License 2.0 5 votes vote down vote up
private Collection<EquivalentIRI> getEquivalentURI(Var v) {
    Value val = v.getValue();

    if (val != null && val instanceof IRI) {
        IRI uri = (IRI)val;
        return queryTransformation.retrieveEquivalentURIs(uri);
    }

    return Collections.emptySet();
}
 
Example 7
Source File: RdfCloudTripleStoreEvaluationStatistics.java    From rya with Apache License 2.0 5 votes vote down vote up
protected Value getConstantValue(final Var var) {
    if (var != null) {
        return var.getValue();
    } else {
        return null;
    }
}
 
Example 8
Source File: OneOfVisitor.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
protected void meetSP(final StatementPattern node) throws Exception {
    final Var subVar = node.getSubjectVar();
    final Var predVar = node.getPredicateVar();
    final Var objVar = node.getObjectVar();
    final Var conVar = node.getContextVar();
    if (predVar != null && objVar != null && objVar.getValue() != null && objVar.getValue() instanceof Resource && RDF.TYPE.equals(predVar.getValue()) && !EXPANDED.equals(conVar)) {
        final Resource object = (Resource) objVar.getValue();
        if (inferenceEngine.isEnumeratedType(object)) {
            final Set<BindingSet> solutions = new LinkedHashSet<>();
            final Set<Resource> enumeration = inferenceEngine.getEnumeration(object);
            for (final Resource enumType : enumeration) {
                final QueryBindingSet qbs = new QueryBindingSet();
                qbs.addBinding(subVar.getName(), enumType);
                solutions.add(qbs);
            }

            if (!solutions.isEmpty()) {
                final BindingSetAssignment enumNode = new BindingSetAssignment();
                enumNode.setBindingSets(solutions);

                node.replaceWith(enumNode);
                log.trace("Replacing node with inferred one of enumeration: " + enumNode);
            }
        }
    }
}
 
Example 9
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 10
Source File: ContextCollector.java    From semagrow with Apache License 2.0 5 votes vote down vote up
private boolean isSameCtx(Var v1, Var v2) {
    if ((v1 != null && v1.getValue() != null) && (v2 != null && v2.getValue() != null)) {
        return v1.getValue().equals(v2.getValue());
    }
    else if ((v1 != null && v1.getName() != null) && (v2 != null && v2.getName() != null)) {
        return v1.getName().equals(v2.getName());
    }

    return false;
}
 
Example 11
Source File: InverseOfVisitor.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
protected void meetSP(StatementPattern node) throws Exception {
    StatementPattern sp = node.clone();
    final Var predVar = sp.getPredicateVar();

    IRI pred = (IRI) predVar.getValue();
    String predNamespace = pred.getNamespace();

    final Var objVar = sp.getObjectVar();
    final Var cntxtVar = sp.getContextVar();
    if (objVar != null &&
            !RDF.NAMESPACE.equals(predNamespace) &&
            !SESAME.NAMESPACE.equals(predNamespace) &&
            !RDFS.NAMESPACE.equals(predNamespace)
            && !EXPANDED.equals(cntxtVar)) {
        /**
         *
         * { ?a ?pred ?b .}\n" +
         "       UNION " +
         "      { ?b ?pred ?a }
         */

        IRI predIri = (IRI) predVar.getValue();
        IRI invPropIri = inferenceEngine.findInverseOf(predIri);
        if (invPropIri != null) {
            Var subjVar = sp.getSubjectVar();
            Union union = new InferUnion();
            union.setLeftArg(sp);
            union.setRightArg(new StatementPattern(objVar, new Var(predVar.getName(), invPropIri), subjVar, cntxtVar));
            node.replaceWith(union);
        }
    }
}
 
Example 12
Source File: PathIteration.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Value getVarValue(Var var, boolean fixedValue, BindingSet bindingSet) {
	Value v;
	if (fixedValue) {
		v = var.getValue();
		if (v == null) {
			v = this.bindings.getValue(var.getName());
		}
	} else {
		v = bindingSet.getValue(var.getName());
	}

	return v;
}
 
Example 13
Source File: SpinRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Var node) throws RDFHandlerException {
	Value value;
	if (node.isConstant()) {
		value = node.getValue();
	} else {
		value = getVar(node.getName());
	}
	handler.handleStatement(valueFactory.createStatement(subject, predicate, value));
}
 
Example 14
Source File: HalyardStatementPatternEvaluation.java    From Halyard with Apache License 2.0 5 votes vote down vote up
/**
 * Gets a value from a {@code Var} if it has a {@code Value}. If it does not then the method will attempt to get it
 * from the bindings using the name of the Var
 * @param var
 * @param bindings
 * @return the matching {@code Value} or {@code null} if var is {@code null}
 */
private static Value getVarValue(Var var, BindingSet bindings) {
    if (var == null) {
        return null;
    } else if (var.hasValue()) {
        return var.getValue();
    } else {
        return bindings.getValue(var.getName());
    }
}
 
Example 15
Source File: StatementMetadataNode.java    From rya with Apache License 2.0 5 votes vote down vote up
private Set<String> getVariableNames() {
    final Set<String> vars = new HashSet<>();
    for (final StatementPattern pattern : patterns) {
        for (final Var var : pattern.getVarList()) {
            if (var.getValue() == null) {
                vars.add(var.getName());
            }
        }
    }
    return vars;
}
 
Example 16
Source File: StatisticsUtil.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
protected static Value toValue(Var var, BindingSet bindings) {
	if (var.hasValue())
		return var.getValue();
	
	if (bindings.hasBinding(var.getName()))	
		return bindings.getValue(var.getName());
	
	return null;			
}
 
Example 17
Source File: StatementPatternStorage.java    From rya with Apache License 2.0 4 votes vote down vote up
private Value getValue(Var subjectVar) {
    return subjectVar.hasValue() ? subjectVar.getValue() : null;
}
 
Example 18
Source File: PrepareOwnedTupleExpr.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void meet(StatementPattern node) throws RepositoryException {
	StringBuilder builder = new StringBuilder();
	Scope scope = node.getScope();
	Var subj = node.getSubjectVar();
	Var pred = node.getPredicateVar();
	Var obj = node.getObjectVar();
	Var ctx = node.getContextVar();
	boolean cokay = ctx == null && scope.equals(Scope.DEFAULT_CONTEXTS)
			|| ctx != null && scope.equals(Scope.NAMED_CONTEXTS);
	boolean sokay = !subj.hasValue() || subj.isAnonymous() || subj.getValue() instanceof IRI;
	boolean ookay = !obj.hasValue() || obj.isAnonymous() || obj.getValue() instanceof IRI
			|| obj.getValue() instanceof Literal;
	if (cokay && sokay && ookay) {
		variables.clear();
		if (ctx != null) {
			builder.append("GRAPH ");
			appendVar(builder, ctx.getName());
			builder.append(" {\n");
		}
		appendVar(builder, subj);
		appendVar(builder, pred);
		appendVar(builder, obj);
		builder.append(" .");
		appendFilter(builder, subj);
		appendFilter(builder, pred);
		appendFilter(builder, obj);
		if (ctx != null) {
			if (ctx.hasValue()) {
				builder.append("\nFILTER sameTerm(");
				appendVar(builder, ctx.getName());
				builder.append(", ");
				writeValue(builder, ctx.getValue());
				builder.append(")\n");
			}
			builder.append("}");
		}
		this.pattern = builder.toString();
		this.patternNode = node;
	} else {
		this.patternNode = null; // NOPMD
	}
}
 
Example 19
Source File: StatementMetadataNode.java    From rya with Apache License 2.0 3 votes vote down vote up
/**
 * Assigns BindingSet values for any {@link Var} whose {@link Value} is
 * null. Returns the {@link Value} associated with Var (if it has one),
 * otherwise returns the BindingSet Value corresponding to
 * {@link Var#getName()}. If no such Binding exits, this method returns
 * null.
 *
 * @param var
 * @param bindings
 * @return Value
 */
private Value getVarValue(final Var var, final BindingSet bindings) {
    if (var == null) {
        return null;
    } else if (var.hasValue()) {
        return var.getValue();
    } else {
        return bindings.getValue(var.getName());
    }
}
 
Example 20
Source File: QueryAlgebraUtil.java    From CostFed with GNU Affero General Public License v3.0 3 votes vote down vote up
/**
 * Return the {@link Value} of the variable which is either taken from
 * the variable itself (bound) or from the bindingsset (unbound).
 * 
 * @param var
 * @param bindings
 * 			the bindings, must not be null, use {@link EmptyBindingSet} instead
 * 
 * @return
 * 		the value or null
 */
public static Value getVarValue(Var var, BindingSet bindings) {
	if (var == null) {
		return null;
	} else if (var.hasValue()) {
		return var.getValue();
	} else {
		return bindings.getValue(var.getName());
	}
}