Java Code Examples for org.eclipse.rdf4j.query.algebra.StatementPattern#getPredicateVar()

The following examples show how to use org.eclipse.rdf4j.query.algebra.StatementPattern#getPredicateVar() . 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: SourceSelectorWithQueryTransform.java    From semagrow with Apache License 2.0 6 votes vote down vote up
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 2
Source File: FilterRangeVisitor.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
public void meet(final StatementPattern node) throws Exception {
    super.meet(node);

    final Var subjectVar = node.getSubjectVar();
    final RangeValue subjRange = rangeValues.get(subjectVar);
    final Var predVar = node.getPredicateVar();
    final RangeValue predRange = rangeValues.get(predVar);
    final Var objVar = node.getObjectVar();
    final RangeValue objRange = rangeValues.get(objVar);
    if(subjRange != null) {
        subjectVar.setValue(new RangeIRI(subjRange));//Assumes no blank nodes can be ranges
    }
    if(predRange != null) {
        predVar.setValue(new RangeIRI(predRange));
    }
    if(objRange != null) {
        objVar.setValue(objRange);
    }
}
 
Example 3
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 4
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 5
Source File: AbstractInferVisitor.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
    public void meet(StatementPattern sp) throws Exception {
        if (!include) {
            return;
        }
        if (sp instanceof FixedStatementPattern || sp instanceof TransitivePropertySP || sp instanceof DoNotExpandSP) {
            return;   //already inferred somewhere else
        }
        final Var predVar = sp.getPredicateVar();
        //we do not let timeRange preds be inferred, not good
        if (predVar == null || predVar.getValue() == null
//                || RdfCloudTripleStoreUtils.getTtlValueConverter(conf, (URI) predVar.getValue()) != null
                ) {
            return;
        }
        meetSP(sp);
    }
 
Example 6
Source File: FluoStringConverter.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * 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 7
Source File: SymmetricPropertyVisitor.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 symmPropIri = (IRI) predVar.getValue();
        if(inferenceEngine.isSymmetricProperty(symmPropIri)) {
            Var subjVar = sp.getSubjectVar();
            Union union = new InferUnion();
            union.setLeftArg(sp);
            union.setRightArg(new StatementPattern(objVar, predVar, subjVar, cntxtVar));
            node.replaceWith(union);
        }
    }
}
 
Example 8
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 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: 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 11
Source File: StatementMetadataExternalSetProvider.java    From rya with Apache License 2.0 5 votes vote down vote up
private Set<StatementPattern> removeInvalidProperties(Collection<StatementPattern> patterns) {

        Set<StatementPattern> finalPatterns = new HashSet<>();
        
        for (StatementPattern pattern : patterns) {
            Var var = pattern.getPredicateVar();
            if (var.getValue() != null && var.getValue() instanceof IRI) {
                RyaIRI uri = RdfToRyaConversions.convertIRI((IRI) var.getValue());
                if(expectedURI.contains(uri) || metadataProperties.contains(uri)) {
                    finalPatterns.add(pattern);
                }
            }
        }
        return finalPatterns;
    }
 
Example 12
Source File: ConstructProjection.java    From rya with Apache License 2.0 4 votes vote down vote up
public ConstructProjection(StatementPattern pattern) {
    this(pattern.getSubjectVar(), pattern.getPredicateVar(), pattern.getObjectVar());
}
 
Example 13
Source File: SubPropertyOfVisitor.java    From rya with Apache License 2.0 4 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)) {
            /**
             *
             * { ?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 14
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 15
Source File: TrueStatementPattern.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public TrueStatementPattern(StatementPattern node) {
	super(node.getSubjectVar(), node.getPredicateVar(), node.getObjectVar(), node.getContextVar());
}
 
Example 16
Source File: EntityQueryNode.java    From rya with Apache License 2.0 4 votes vote down vote up
/**
 * Constructs an instance of {@link EntityQueryNode}.
 *
 * @param type - The type of {@link Entity} this node matches. (not null)
 * @param patterns - The query StatementPatterns that are solved using an
 *   Entity of the Type. (not null)
 * @param entities - The {@link EntityStorage} that will be searched to match
 *   {@link BindingSet}s when evaluating a query. (not null)
 */
public EntityQueryNode(final Type type, final Collection<StatementPattern> patterns, final EntityStorage entities) throws IllegalStateException {
    this.type = requireNonNull(type);
    this.patterns = requireNonNull(patterns);
    this.entities = requireNonNull(entities);

    bindingNames = new HashSet<>();
    properties = new HashSet<>();
    // Subject based preconditions.
    verifySameSubjects(patterns);

    // Predicate based preconditions.
    verifyAllPredicatesAreConstants(patterns);
    verifyHasCorrectTypePattern(type, patterns);
    verifyAllPredicatesPartOfType(type, patterns);

    // The Subject may either be constant or a variable.
    final Var subject = patterns.iterator().next().getSubjectVar();
    subjectIsConstant = subject.isConstant();
    if(subjectIsConstant) {
        subjectConstant = Optional.of( subject.getValue().toString() );
        subjectVar = Optional.empty();
    } else {
        subjectConstant = Optional.empty();
        subjectVar = Optional.of( subject.getName() );
    }

    // Any constant that appears in the Object portion of the SP will be used to make sure they match.
    final Builder<RyaIRI, Var> builder = ImmutableMap.builder();
    for(final StatementPattern sp : patterns) {
        final Var object = sp.getObjectVar();
        final Var pred = sp.getPredicateVar();
        final RyaIRI predIRI = new RyaIRI(pred.getValue().stringValue());
        bindingNames.addAll(sp.getBindingNames());
        if(object.isConstant() && !pred.getValue().equals(RDF.TYPE)) {
            final RyaType propertyType = RdfToRyaConversions.convertValue(object.getValue());
            properties.add(new Property(predIRI, propertyType));
        }
        builder.put(predIRI, object);
    }
    objectVariables = builder.build();
}
 
Example 17
Source File: EntityIndexSetProvider.java    From rya with Apache License 2.0 4 votes vote down vote up
private RyaIRI getPredIRI(final StatementPattern pattern) {
    final Var pred = pattern.getPredicateVar();
    return new RyaIRI(pred.getValue().stringValue());
}
 
Example 18
Source File: SubClassOfVisitor.java    From rya with Apache License 2.0 4 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 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 19
Source File: StarQuery.java    From rya with Apache License 2.0 4 votes vote down vote up
public static boolean isValidStarQuery(final Collection<StatementPattern> nodes) {

        Set<String> bindings = null;
        boolean contextSet = false;
        Var context = null;

        if(nodes.size() < 2) {
            return false;
        }

        for(final StatementPattern sp: nodes) {

            final Var tempContext = sp.getContextVar();
            final Var predVar = sp.getPredicateVar();

            //does not support variable context
            if(tempContext != null && !tempContext.isConstant()) {
               return false;
            }
            if(!contextSet) {
                context = tempContext;
                contextSet = true;
            } else {

                if(context == null && tempContext != null) {
                    return false;
                } else if (context != null && !context.equals(tempContext)) {
                    return false;
                }
            }

            if(!predVar.isConstant()) {
                return false;
            }

            if(bindings == null ) {
                bindings = sp.getBindingNames();
                if(bindings.size() == 0) {
                    return false;
                }
            } else {
                bindings = Sets.intersection(bindings, sp.getBindingNames());
                if(bindings.size() == 0) {
                    return false;
                }
            }

        }


        return isBindingsetValid(bindings);
    }
 
Example 20
Source File: EmptyStatementPattern.java    From CostFed with GNU Affero General Public License v3.0 4 votes vote down vote up
public EmptyStatementPattern(StatementPattern node) {
	super(node.getSubjectVar(), node.getPredicateVar(), node.getObjectVar(), node.getContextVar());
}