Search in sources :

Example 1 with QueryException

use of com.inova8.intelligentgraph.exceptions.QueryException in project com.inova8.intelligentgraph by peterjohnlawrence.

the class PredicateElement method pathReifiedPredicatePatternTupleExpr.

/**
 * Path reified predicate pattern tuple expr.
 *
 * @param predicatePattern the predicate pattern
 * @param sourceVariable the source variable
 * @param intermediatePredicateVariable the intermediate predicate variable
 * @param targetVariable the target variable
 * @param reificationVariable the reification variable
 * @param customQueryOptions the custom query options
 * @return the path tuple expr
 */
private PathTupleExpr pathReifiedPredicatePatternTupleExpr(PathTupleExpr predicatePattern, Variable sourceVariable, Variable intermediatePredicateVariable, Variable targetVariable, Variable reificationVariable, CustomQueryOptions customQueryOptions) {
    ArrayList<Variable> targetVariables = new ArrayList<Variable>();
    if (objectFilterElement != null)
        targetVariables = objectFilterElement.bindTargetVariable(targetVariable, customQueryOptions);
    if (targetVariables.size() == 0)
        targetVariables.add(targetVariable);
    TupleExpr reifiedPredicatePattern = null;
    intermediatePredicateVariable.setValue(getPredicate());
    // getPredicateVariable();
    Variable predicateVariable = intermediatePredicateVariable;
    IRI subject = getReifications().getReificationSubject(reification);
    IRI isSubjectOf = getReifications().getReificationIsSubjectOf(reification);
    if (subject == null && isSubjectOf == null)
        throw new QueryException("Undefined Reification", "No subject/isSubjectOf for reification " + reification.stringValue());
    IRI property = getReifications().getReificationPredicate(reification);
    IRI isPropertyOf = getReifications().getReificationIsPredicateOf(reification);
    if (property == null && isPropertyOf == null)
        throw new QueryException("Undefined Reification", "No property/isPropertyOf for reification " + reification.stringValue());
    IRI object = getReifications().getReificationObject(reification);
    IRI isObjectOf = getReifications().getReificationIsObjectOf(reification);
    if (object == null && isObjectOf == null)
        throw new QueryException("Undefined Reification", "No object/isObjectOf for reification " + reification.stringValue());
    Variable subjectVariable = new Variable("subject" + getExitIndex(), subject);
    Variable isSubjectOfVariable = new Variable("isSubjectOf" + getExitIndex(), isSubjectOf);
    Variable propertyVariable = new Variable("property" + getExitIndex(), property);
    Variable isPropertyOfVariable = new Variable("isPropertyOf" + getExitIndex(), isPropertyOf);
    Variable objectVariable = new Variable("object" + getExitIndex(), object);
    Variable isObjectOfVariable = new Variable("isObjectOf" + getExitIndex(), isObjectOf);
    // Part1
    TupleExpr part1Pattern = null;
    if (isInverseOf) {
        if (object != null && isObjectOf != null) {
            StatementPattern objectPattern = new StatementPattern(reificationVariable, objectVariable, sourceVariable);
            StatementPattern isObjectOfPattern = new StatementPattern(sourceVariable, isObjectOfVariable, reificationVariable);
            part1Pattern = new Union(objectPattern, isObjectOfPattern);
        } else if (object != null) {
            StatementPattern objectPattern = new StatementPattern(reificationVariable, objectVariable, sourceVariable);
            part1Pattern = objectPattern;
        } else if (isObjectOf != null) {
            StatementPattern isObjectOfPattern = new StatementPattern(targetVariable, isObjectOfVariable, sourceVariable);
            part1Pattern = isObjectOfPattern;
        } else {
        }
    } else {
        if (subject != null && isSubjectOf != null) {
            StatementPattern subjectPattern = new StatementPattern(reificationVariable, subjectVariable, sourceVariable);
            StatementPattern isSubjectOfPattern = new StatementPattern(sourceVariable, isSubjectOfVariable, reificationVariable);
            part1Pattern = new Union(subjectPattern, isSubjectOfPattern);
        } else if (subject != null) {
            StatementPattern subjectPattern = new StatementPattern(reificationVariable, subjectVariable, sourceVariable);
            part1Pattern = subjectPattern;
        } else if (isSubjectOf != null) {
            StatementPattern isSubjectOfPattern = new StatementPattern(sourceVariable, isSubjectOfVariable, reificationVariable);
            part1Pattern = isSubjectOfPattern;
        } else {
        }
    }
    // Part2
    TupleExpr part2Pattern = null;
    if (property != null && isPropertyOf != null) {
        StatementPattern propertyPattern = new StatementPattern(reificationVariable, propertyVariable, predicateVariable);
        StatementPattern isPropertyOfPattern = new StatementPattern(predicateVariable, isPropertyOfVariable, reificationVariable);
        part2Pattern = new Union(propertyPattern, isPropertyOfPattern);
    } else if (property != null) {
        StatementPattern propertyPattern = new StatementPattern(reificationVariable, propertyVariable, predicateVariable);
        part2Pattern = propertyPattern;
    } else if (isPropertyOf != null) {
        StatementPattern isPropertyOfPattern = new StatementPattern(predicateVariable, isPropertyOfVariable, reificationVariable);
        part2Pattern = isPropertyOfPattern;
    } else {
    }
    TupleExpr part12Pattern = null;
    if (part1Pattern != null && part2Pattern != null)
        part12Pattern = new Join(part1Pattern, part2Pattern);
    else if (part1Pattern != null)
        part12Pattern = part1Pattern;
    else if (part2Pattern != null)
        part12Pattern = part2Pattern;
    // Part3
    TupleExpr part3Pattern = null;
    for (Variable theTargetVariable : targetVariables) {
        Variable aTargetVariable = new Variable(theTargetVariable.getName(), theTargetVariable.getValue());
        TupleExpr aTargetPattern = null;
        if (isInverseOf) {
            if (subject != null && isSubjectOf != null) {
                StatementPattern subjectPattern = new StatementPattern(reificationVariable, subjectVariable, aTargetVariable);
                StatementPattern isSubjectOfPattern = new StatementPattern(aTargetVariable, isSubjectOfVariable, reificationVariable);
                aTargetPattern = new Union(subjectPattern, isSubjectOfPattern);
            } else if (subject != null) {
                StatementPattern subjectPattern = new StatementPattern(reificationVariable, subjectVariable, aTargetVariable);
                aTargetPattern = subjectPattern;
            } else if (isSubjectOf != null) {
                StatementPattern isSubjectOfPattern = new StatementPattern(aTargetVariable, isSubjectOfVariable, reificationVariable);
                aTargetPattern = isSubjectOfPattern;
            } else {
            }
        } else {
            if (object != null && isObjectOf != null) {
                StatementPattern objectPattern = new StatementPattern(reificationVariable, objectVariable, aTargetVariable);
                StatementPattern isObjectOfPattern = new StatementPattern(aTargetVariable, isObjectOfVariable, reificationVariable);
                aTargetPattern = new Union(objectPattern, isObjectOfPattern);
            } else if (object != null) {
                StatementPattern objectPattern = new StatementPattern(reificationVariable, objectVariable, aTargetVariable);
                aTargetPattern = objectPattern;
            } else if (isObjectOf != null) {
                StatementPattern isObjectOfPattern = new StatementPattern(aTargetVariable, isObjectOfVariable, reificationVariable);
                aTargetPattern = isObjectOfPattern;
            } else {
            }
        }
        if (part3Pattern != null) {
            Join newPart3Pattern = new Join(part3Pattern, aTargetPattern);
            part3Pattern = newPart3Pattern;
        } else {
            part3Pattern = aTargetPattern;
        }
    }
    if (part12Pattern != null && part3Pattern != null)
        reifiedPredicatePattern = new Join(part12Pattern, part3Pattern);
    else if (part12Pattern != null)
        reifiedPredicatePattern = part12Pattern;
    else if (part3Pattern != null)
        reifiedPredicatePattern = part3Pattern;
    Variable objectFilterTargetVariable;
    if (objectFilterElement != null) {
        objectFilterTargetVariable = new Variable(targetVariable.getName(), targetVariable.getValue());
        reifiedPredicatePattern = objectFilterElement.filterExpression(objectFilterTargetVariable, intermediatePredicateVariable, null, reifiedPredicatePattern, customQueryOptions).getTupleExpr();
    }
    if (statementFilterElement != null) {
        reifiedPredicatePattern = statementFilterElement.filterExpression(reificationVariable, intermediatePredicateVariable, null, reifiedPredicatePattern, customQueryOptions).getTupleExpr();
    }
    StatementBinding statementBinding = new StatementBinding(sourceVariable, getReification(), reificationVariable, predicateVariable, getIsDereified() ? reificationVariable : targetVariable, getIsInverseOf(), getIsDereified());
    if (predicatePattern == null) {
        predicatePattern = new PathTupleExpr(reifiedPredicatePattern);
    } else {
        predicatePattern.setTupleExpr(new Join(predicatePattern.getTupleExpr(), reifiedPredicatePattern));
    }
    predicatePattern.setStatementBinding(statementBinding);
    predicatePattern.setBoundVariable(predicatePattern.getStatementBinding().getSourceVariable());
    predicatePattern.getPath().add(statementBinding);
    return predicatePattern;
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) QueryException(com.inova8.intelligentgraph.exceptions.QueryException) StatementBinding(com.inova8.intelligentgraph.path.StatementBinding) PathTupleExpr(com.inova8.intelligentgraph.path.PathTupleExpr) ArrayList(java.util.ArrayList) Join(org.eclipse.rdf4j.query.algebra.Join) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) PathTupleExpr(com.inova8.intelligentgraph.path.PathTupleExpr) Union(org.eclipse.rdf4j.query.algebra.Union)

Aggregations

QueryException (com.inova8.intelligentgraph.exceptions.QueryException)1 PathTupleExpr (com.inova8.intelligentgraph.path.PathTupleExpr)1 StatementBinding (com.inova8.intelligentgraph.path.StatementBinding)1 ArrayList (java.util.ArrayList)1 IRI (org.eclipse.rdf4j.model.IRI)1 Join (org.eclipse.rdf4j.query.algebra.Join)1 StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)1 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)1 Union (org.eclipse.rdf4j.query.algebra.Union)1