Java Code Examples for org.eclipse.rdf4j.query.algebra.StatementPattern#getSubjectVar()
The following examples show how to use
org.eclipse.rdf4j.query.algebra.StatementPattern#getSubjectVar() .
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: ReflexivePropertyVisitor.java From rya with Apache License 2.0 | 6 votes |
/** * 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 2
Source File: SourceSelectorWithQueryTransform.java From semagrow with Apache License 2.0 | 6 votes |
private Collection<FuzzyEntry<StatementPattern>> transformPattern(StatementPattern pattern) { Set<FuzzyEntry<StatementPattern>> transformedPatterns = new HashSet<FuzzyEntry<StatementPattern>>(); //queryTransformation.retrieveEquivalentURIs(pattern) Var sVar = pattern.getSubjectVar(); Var pVar = pattern.getPredicateVar(); Var oVar = pattern.getObjectVar(); double patternProximity = 1.0; for (FuzzyEntry<Var> sVar1 : transformVar(sVar)) { for (FuzzyEntry<Var> pVar1 : transformVar(pVar)) { for (FuzzyEntry<Var> oVar1 : transformVar(oVar)) { StatementPattern p = new StatementPattern(sVar1.getElem(), pVar1.getElem(), oVar1.getElem()); if (!p.equals(pattern)) { double prox = Math.min(Math.min(sVar1.getProximity(), pVar1.getProximity()), oVar1.getProximity()); transformedPatterns.add(new FuzzyEntry<StatementPattern>(p, prox)); } } } } return transformedPatterns; }
Example 3
Source File: TupleExprBuilder.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
private List<StatementPattern> replaceSameTermVars(List<StatementPattern> statementPatterns, Set<SameTerm> sameTermConstraints) { if (sameTermConstraints != null) { for (SameTerm st : sameTermConstraints) { Var left = (Var) st.getLeftArg(); Var right = (Var) st.getRightArg(); for (StatementPattern sp : statementPatterns) { Var subj = sp.getSubjectVar(); Var obj = sp.getObjectVar(); if (subj.equals(left) || subj.equals(right)) { if (obj.equals(left) || obj.equals(right)) { sp.setObjectVar(subj); } } } } } return statementPatterns; }
Example 4
Source File: FluoStringConverter.java From rya with Apache License 2.0 | 5 votes |
/** * Provides a string representation of an SP which contains info about * whether each component (subj, pred, obj) is constant and its data and * data type if it is constant. * * @param sp - The statement pattern to convert. (not null) * @return A String representation of the statement pattern that may be * used to do triple matching. */ public static String toStatementPatternString(final StatementPattern sp) { checkNotNull(sp); final Var subjVar = sp.getSubjectVar(); String subj = subjVar.getName(); if(subjVar.getValue() != null) { final Value subjValue = subjVar.getValue(); subj = VarNameUtils.createSimpleConstVarName(subjVar); if (subjValue instanceof BNode ) { subj = subj + TYPE_DELIM + RyaSchema.BNODE_NAMESPACE + TYPE_DELIM + ((BNode) subjValue).getID(); } else { subj = subj + TYPE_DELIM + URI_TYPE; } } final Var predVar = sp.getPredicateVar(); String pred = predVar.getName(); if(predVar.getValue() != null) { pred = VarNameUtils.createSimpleConstVarName(predVar); pred = pred + TYPE_DELIM + URI_TYPE; } final Var objVar = sp.getObjectVar(); String obj = objVar.getName(); if (objVar.getValue() != null) { final Value objValue = objVar.getValue(); obj = VarNameUtils.createSimpleConstVarName(objVar); final RyaType rt = RdfToRyaConversions.convertValue(objValue); obj = obj + TYPE_DELIM + rt.getDataType().stringValue(); } return subj + DELIM + pred + DELIM + obj; }
Example 5
Source File: OneOfVisitor.java From rya with Apache License 2.0 | 5 votes |
@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 6
Source File: FilterFunctionOptimizer.java From rya with Apache License 2.0 | 5 votes |
private void processPropertySearches(final TupleExpr tupleExpr, final Collection<Var> searchProperties) { final MatchStatementVisitor matchStatements = new MatchStatementVisitor(searchProperties); tupleExpr.visit(matchStatements); for (final StatementPattern matchStatement: matchStatements.matchStatements) { final Var subject = matchStatement.getSubjectVar(); if (subject.hasValue() && !(subject.getValue() instanceof Resource)) { throw new IllegalArgumentException("Query error: Found " + subject.getValue() + ", expected an IRI or BNode"); } Validate.isTrue(subject.hasValue() || subject.getName() != null); Validate.isTrue(!matchStatement.getObjectVar().hasValue() && matchStatement.getObjectVar().getName() != null); buildQuery(tupleExpr, matchStatement); } }
Example 7
Source File: GeoTemporalIndexSetProvider.java From rya with Apache License 2.0 | 5 votes |
private void discoverPatterns(final StatementPattern pattern, final List<QueryModelNode> unmatched) { final Var subj = pattern.getSubjectVar(); final Var objVar = pattern.getObjectVar(); patternMap.put(subj, pattern); objectPatterns.put(objVar, pattern); //check for existing filters. if(unmatchedFilters.containsKey(objVar)) { final Collection<FunctionCall> calls = unmatchedFilters.removeAll(objVar); for(final FunctionCall call : calls) { addFilter(call); matchedFilters.put(objVar, call); } } }
Example 8
Source File: SomeValuesFromVisitor.java From rya with Apache License 2.0 | 5 votes |
/** * 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 9
Source File: InverseOfVisitor.java From rya with Apache License 2.0 | 5 votes |
@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 10
Source File: GeoEnabledFilterFunctionOptimizer.java From rya with Apache License 2.0 | 5 votes |
private void processPropertySearches(final TupleExpr tupleExpr, final Collection<Var> searchProperties) { final MatchStatementVisitor matchStatements = new MatchStatementVisitor(searchProperties); tupleExpr.visit(matchStatements); for (final StatementPattern matchStatement: matchStatements.matchStatements) { final Var subject = matchStatement.getSubjectVar(); if (subject.hasValue() && !(subject.getValue() instanceof Resource)) { throw new IllegalArgumentException("Query error: Found " + subject.getValue() + ", expected an IRI or BNode"); } Validate.isTrue(subject.hasValue() || subject.getName() != null); Validate.isTrue(!matchStatement.getObjectVar().hasValue() && matchStatement.getObjectVar().getName() != null); buildQuery(tupleExpr, matchStatement); } }
Example 11
Source File: FedXStatementPattern.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
public FedXStatementPattern(StatementPattern node, QueryInfo queryInfo) { super(node.getSubjectVar(), node.getPredicateVar(), node.getObjectVar(), node.getContextVar()); setScope(node.getScope()); this.id = NodeFactory.getNextId(); this.queryInfo = queryInfo; initFreeVars(); }
Example 12
Source File: SubPropertyOfVisitor.java From rya with Apache License 2.0 | 4 votes |
@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)) { /** * * { ?subProp rdfs:subPropertyOf ub:worksFor . ?y ?subProp <http://www.Department0.University0.edu> }\n" + " UNION " + " { ?y ub:worksFor <http://www.Department0.University0.edu> } */ // String s = UUID.randomUUID().toString(); // Var subPropVar = new Var(s); // StatementPattern subPropOf = new StatementPattern(subPropVar, new Var("c-" + s, SESAME.DIRECTSUBPROPERTYOF), predVar, EXPANDED); // StatementPattern subPropOf2 = new StatementPattern(sp.getSubjectVar(), subPropVar, objVar, EXPANDED); // InferJoin join = new InferJoin(subPropOf, subPropOf2); // join.getProperties().put(InferConstants.INFERRED, InferConstants.TRUE); // node.replaceWith(join); // Collection<URI> parents = inferenceEngine.findParents(inferenceEngine.subPropertyOfGraph, (URI) predVar.getValue()); // if (parents != null && parents.size() > 0) { // StatementPatterns statementPatterns = new StatementPatterns(); // statementPatterns.patterns.add(node); // Var subjVar = node.getSubjectVar(); // for (IRI iri : parents) { // statementPatterns.patterns.add(new StatementPattern(subjVar, new Var(predVar.getName(), iri), objVar)); // } // node.replaceWith(statementPatterns); // } // if (parents != null && parents.size() > 0) { // VarCollection vc = new VarCollection(); // vc.setName(predVar.getName()); // vc.values.add(predVar); // for (IRI iri : parents) { // vc.values.add(new Var(predVar.getName(), iri)); // } // Var subjVar = node.getSubjectVar(); // node.replaceWith(new StatementPattern(subjVar, vc, objVar, node.getContextVar())); // } final IRI subprop_iri = (IRI) predVar.getValue(); final Set<IRI> parents = InferenceEngine.findParents(inferenceEngine.getSubPropertyOfGraph(), subprop_iri); if (parents != null && parents.size() > 0) { final String s = UUID.randomUUID().toString(); final Var typeVar = new Var(s); final FixedStatementPattern fsp = new FixedStatementPattern(typeVar, new Var("c-" + s, RDFS.SUBPROPERTYOF), predVar, cntxtVar); // fsp.statements.add(new NullableStatementImpl(subprop_uri, RDFS.SUBPROPERTYOF, subprop_uri)); //add self parents.add(subprop_iri); for (final IRI u : parents) { fsp.statements.add(new NullableStatementImpl(u, RDFS.SUBPROPERTYOF, subprop_iri)); } final StatementPattern rdfType = new DoNotExpandSP(sp.getSubjectVar(), typeVar, sp.getObjectVar(), cntxtVar); final InferJoin join = new InferJoin(fsp, rdfType); join.getProperties().put(InferConstants.INFERRED, InferConstants.TRUE); node.replaceWith(join); } } }
Example 13
Source File: TrueStatementPattern.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
public TrueStatementPattern(StatementPattern node) { super(node.getSubjectVar(), node.getPredicateVar(), node.getObjectVar(), node.getContextVar()); }
Example 14
Source File: EmptyStatementPattern.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
public EmptyStatementPattern(StatementPattern node) { super(node.getSubjectVar(), node.getPredicateVar(), node.getObjectVar(), node.getContextVar()); }
Example 15
Source File: HasValueVisitor.java From rya with Apache License 2.0 | 4 votes |
/** * Checks whether facts matching the StatementPattern could be derived using * has-value inference, and if so, replaces the StatementPattern node with a * union of itself and any such possible derivations. */ @Override protected void meetSP(StatementPattern node) throws Exception { final Var subjVar = node.getSubjectVar(); final Var predVar = node.getPredicateVar(); final Var objVar = node.getObjectVar(); // We can reason over two types of statement patterns: // { ?var rdf:type :Restriction } and { ?var :property ?value } // Both require defined predicate if (predVar != null && predVar.getValue() != null) { final IRI predIRI = (IRI) predVar.getValue(); if (RDF.TYPE.equals(predIRI) && objVar != null && objVar.getValue() != null && objVar.getValue() instanceof Resource) { // If the predicate is rdf:type and the type is specified, check whether it can be // inferred using any hasValue restriction(s) final Resource objType = (Resource) objVar.getValue(); final Map<IRI, Set<Value>> sufficientValues = inferenceEngine.getHasValueByType(objType); if (sufficientValues.size() > 0) { final Var valueVar = new Var("v-" + UUID.randomUUID()); TupleExpr currentNode = node.clone(); for (IRI property : sufficientValues.keySet()) { final Var propVar = new Var(property.toString(), property); final TupleExpr valueSP = new DoNotExpandSP(subjVar, propVar, valueVar); final FixedStatementPattern relevantValues = new FixedStatementPattern(objVar, propVar, valueVar); for (Value value : sufficientValues.get(property)) { relevantValues.statements.add(new NullableStatementImpl(objType, property, value)); } currentNode = new InferUnion(currentNode, new InferJoin(relevantValues, valueSP)); } node.replaceWith(currentNode); } } else { // If the predicate has some hasValue restriction associated with it, then finding // that the object belongs to the appropriate type implies a value. final Map<Resource, Set<Value>> impliedValues = inferenceEngine.getHasValueByProperty(predIRI); if (impliedValues.size() > 0) { final Var rdfTypeVar = new Var(RDF.TYPE.stringValue(), RDF.TYPE); final Var typeVar = new Var("t-" + UUID.randomUUID()); final Var hasValueVar = new Var(OWL.HASVALUE.stringValue(), OWL.HASVALUE); final TupleExpr typeSP = new DoNotExpandSP(subjVar, rdfTypeVar, typeVar); final FixedStatementPattern typeToValue = new FixedStatementPattern(typeVar, hasValueVar, objVar); final TupleExpr directValueSP = node.clone(); for (Resource type : impliedValues.keySet()) { // { ?var rdf:type :type } implies { ?var :property :val } for certain (:type, :val) pairs for (Value impliedValue : impliedValues.get(type)) { typeToValue.statements.add(new NullableStatementImpl(type, OWL.HASVALUE, impliedValue)); } } node.replaceWith(new InferUnion(new InferJoin(typeToValue, typeSP), directValueSP)); } } } }
Example 16
Source File: SubClassOfVisitor.java From rya with Apache License 2.0 | 4 votes |
@Override protected void meetSP(final StatementPattern node) throws Exception { final StatementPattern sp = node.clone(); final Var predVar = sp.getPredicateVar(); final Var objVar = sp.getObjectVar(); final Var conVar = sp.getContextVar(); if (predVar != null && objVar != null && objVar.getValue() != null && RDF.TYPE.equals(predVar.getValue()) && !EXPANDED.equals(conVar)) { /** * ?type sesame:directSubClassOf ub:Student . ?student rdf:type ?type + */ // String s = UUID.randomUUID().toString(); // Var typeVar = new Var(s); // StatementPattern subClassOf = new StatementPattern(typeVar, new Var("c-" + s, SESAME.DIRECTSUBCLASSOF), objVar, SUBCLASS_EXPANDED); // StatementPattern rdfType = new StatementPattern(sp.getSubjectVar(), sp.getPredicateVar(), typeVar, SUBCLASS_EXPANDED); // InferJoin join = new InferJoin(subClassOf, rdfType); // join.getProperties().put(InferConstants.INFERRED, InferConstants.TRUE); // node.replaceWith(join); final IRI subclassof_iri = (IRI) objVar.getValue(); final Collection<IRI> parents = InferenceEngine.findParents(inferenceEngine.getSubClassOfGraph(), subclassof_iri); if (parents != null && parents.size() > 0) { final String s = UUID.randomUUID().toString(); final Var typeVar = new Var(s); final FixedStatementPattern fsp = new FixedStatementPattern(typeVar, new Var("c-" + s, RDFS.SUBCLASSOF), objVar, conVar); parents.add(subclassof_iri); for (final IRI iri : parents) { fsp.statements.add(new NullableStatementImpl(iri, RDFS.SUBCLASSOF, subclassof_iri)); } final StatementPattern rdfType = new DoNotExpandSP(sp.getSubjectVar(), sp.getPredicateVar(), typeVar, conVar); final InferJoin join = new InferJoin(fsp, rdfType); join.getProperties().put(InferConstants.INFERRED, InferConstants.TRUE); node.replaceWith(join); } // if (parents != null && parents.size() > 0) { // StatementPatterns statementPatterns = new StatementPatterns(); // statementPatterns.patterns.add(node); // Var subjVar = node.getSubjectVar(); // for (IRI iri : parents) { // statementPatterns.patterns.add(new StatementPattern(subjVar, predVar, new Var(objVar.getName(), iri))); // } // node.replaceWith(statementPatterns); // } // if (parents != null && parents.size() > 0) { // VarCollection vc = new VarCollection(); // vc.setName(objVar.getName()); // vc.values.add(objVar); // for (IRI iri : parents) { // vc.values.add(new Var(objVar.getName(), iri)); // } // Var subjVar = node.getSubjectVar(); // node.replaceWith(new StatementPattern(subjVar, predVar, vc, node.getContextVar())); // } } }
Example 17
Source File: FedXStatementPattern.java From CostFed with GNU Affero General Public License v3.0 | 4 votes |
public FedXStatementPattern(StatementPattern node, QueryInfo queryInfo) { super(node.getSubjectVar(), node.getPredicateVar(), node.getObjectVar(), node.getContextVar()); this.id = NodeFactory.getNextId(); this.queryInfo=queryInfo; initFreeVars(); }
Example 18
Source File: TrueStatementPattern.java From CostFed with GNU Affero General Public License v3.0 | 4 votes |
public TrueStatementPattern(StatementPattern node) { super(node.getSubjectVar(), node.getPredicateVar(), node.getObjectVar(), node.getContextVar()); }
Example 19
Source File: EntityIndexSetProvider.java From rya with Apache License 2.0 | 4 votes |
private Var getTypeSubject(final Type type) { //we just need the first pattern since all the patterns in this type map are the same subject final StatementPattern pattern = typeMap.get(type).iterator().next(); return pattern.getSubjectVar(); }
Example 20
Source File: EmptyStatementPattern.java From CostFed with GNU Affero General Public License v3.0 | 4 votes |
public EmptyStatementPattern(StatementPattern node) { super(node.getSubjectVar(), node.getPredicateVar(), node.getObjectVar(), node.getContextVar()); }