Search in sources :

Example 31 with CustomQueryOptions

use of com.inova8.intelligentgraph.context.CustomQueryOptions in project com.inova8.intelligentgraph by peterjohnlawrence.

the class PredicateElement method pathPredicatePatternQuery.

/**
 * Path predicate pattern query.
 *
 * @param sourceVariable the source variable
 * @param predicateVariable the predicate variable
 * @param targetVariable the target variable
 * @param pathIteration the path iteration
 * @param customQueryOptions the custom query options
 * @return the path tuple expr
 */
private PathTupleExpr pathPredicatePatternQuery(Variable sourceVariable, Variable predicateVariable, Variable targetVariable, Integer pathIteration, CustomQueryOptions customQueryOptions) {
    PathTupleExpr predicatePattern = null;
    if (getCardinality(pathIteration) > 0) {
        Variable intermediateSourceVariable = null;
        Variable intermediatePredicateVariable = null;
        Variable intermediateTargetVariable = null;
        Variable priorIntermediateTargetVariable = null;
        for (int iteration = 1; iteration <= getCardinality(pathIteration); iteration++) {
            if (iteration == 1) {
                intermediateSourceVariable = sourceVariable;
            }
            if (iteration < getCardinality(pathIteration)) {
                if (iteration > 1)
                    intermediateSourceVariable = priorIntermediateTargetVariable;
                intermediateTargetVariable = new Variable(targetVariable.getName() + "_i" + iteration);
                priorIntermediateTargetVariable = intermediateTargetVariable;
            }
            if (iteration == getCardinality(pathIteration)) {
                if (iteration > 1)
                    intermediateSourceVariable = priorIntermediateTargetVariable;
                intermediateTargetVariable = targetVariable;
            }
            intermediatePredicateVariable = deducePredicateVariable(sourceVariable, predicateVariable, targetVariable);
            predicatePattern = pathPredicatePatternTupleExpr(predicatePattern, intermediateSourceVariable, intermediatePredicateVariable, intermediateTargetVariable, customQueryOptions);
        }
        return predicatePattern;
    } else {
        return null;
    }
}
Also used : PathTupleExpr(com.inova8.intelligentgraph.path.PathTupleExpr)

Example 32 with CustomQueryOptions

use of com.inova8.intelligentgraph.context.CustomQueryOptions in project com.inova8.intelligentgraph by peterjohnlawrence.

the class BoundPathElement method pathPatternQuery.

/**
 * Path pattern query.
 *
 * @param sourceVariable the source variable
 * @param predicateVariable the predicate variable
 * @param targetVariable the target variable
 * @param pathIteration the path iteration
 * @param customQueryOptions the custom query options
 * @return the path tuple expr
 */
@Override
public PathTupleExpr pathPatternQuery(Variable sourceVariable, Variable predicateVariable, Variable targetVariable, Integer pathIteration, CustomQueryOptions customQueryOptions) {
    // Extend(rightPatern(?s,?p,?o),
    Variable bindSourceVariable = new Variable("bind");
    // new Variable("b0_b1");
    Variable bindPredicateVariable = null;
    Variable bindTargetVariable = new Variable("b");
    PathTupleExpr bindPattern = ((FactFilterElement) getLeftPathElement()).filterExpression(bindSourceVariable, bindPredicateVariable, bindTargetVariable, null, customQueryOptions);
    // PathTupleExpr bindPattern = getLeftPathElement().pathPatternQuery(bindSourceVariable,bindPredicateVariable,bindTargetVariable,customQueryOptions);
    PathTupleExpr rightPattern;
    if (bindPattern.getTupleExpr() == null) {
        bindSourceVariable.setName("n0");
        rightPattern = getRightPathElement().pathPatternQuery(bindSourceVariable, predicateVariable, targetVariable, customQueryOptions);
        rightPattern.setBoundVariable(bindSourceVariable);
        return rightPattern;
    } else {
        rightPattern = getRightPathElement().pathPatternQuery(sourceVariable, predicateVariable, targetVariable, customQueryOptions);
        Extension leftPattern = new Extension(bindPattern.getTupleExpr(), new ExtensionElem(bindSourceVariable, "n0"));
        Join boundPattern = new Join(leftPattern, rightPattern.getTupleExpr());
        PathTupleExpr boundPatternTupleExpr = new PathTupleExpr(boundPattern);
        boundPatternTupleExpr.setStatementBinding(rightPattern.getStatementBinding());
        boundPatternTupleExpr.setBoundVariable(bindSourceVariable);
        return boundPatternTupleExpr;
    }
}
Also used : Extension(org.eclipse.rdf4j.query.algebra.Extension) PathTupleExpr(com.inova8.intelligentgraph.path.PathTupleExpr) ExtensionElem(org.eclipse.rdf4j.query.algebra.ExtensionElem) Join(org.eclipse.rdf4j.query.algebra.Join)

Example 33 with CustomQueryOptions

use of com.inova8.intelligentgraph.context.CustomQueryOptions in project com.inova8.intelligentgraph by peterjohnlawrence.

the class FactFilterElement method filterExpression.

/**
 * Filter expression.
 *
 * @param sourceVariable the source variable
 * @param predicateVariable the predicate variable
 * @param targetVariable the target variable
 * @param filterExpression the filter expression
 * @param customQueryOptions the custom query options
 * @return the path tuple expr
 */
public PathTupleExpr filterExpression(Variable sourceVariable, Variable predicateVariable, Variable targetVariable, TupleExpr filterExpression, CustomQueryOptions customQueryOptions) {
    // QueryModelNode filterExpression = null;
    if (propertyListNotEmpty != null) {
        int verbObjectListCount = 0;
        for (VerbObjectList verbObjectList : propertyListNotEmpty) {
            verbObjectListCount++;
            Variable verbObjectListTargetVariable = null;
            if (targetVariable != null)
                verbObjectListTargetVariable = new Variable(targetVariable.getName() + verbObjectListCount);
            QueryModelNode verbObjectListExpression = verbObjectList.filterExpression(sourceVariable, predicateVariable, verbObjectListTargetVariable, customQueryOptions);
            if (filterExpression == null)
                filterExpression = (TupleExpr) verbObjectListExpression;
            else if (verbObjectListExpression == null) {
            // Ignore it for some unknown reason
            } else {
                if (filterExpression.getClass().getName().equals("org.eclipse.rdf4j.query.algebra.Compare"))
                    if (verbObjectListExpression.getClass().getName().equals("org.eclipse.rdf4j.query.algebra.Compare"))
                        filterExpression = (TupleExpr) new And((ValueExpr) filterExpression, (ValueExpr) verbObjectListExpression);
                    else
                        filterExpression = new Filter((TupleExpr) verbObjectListExpression, (ValueExpr) filterExpression);
                else if (verbObjectListExpression.getClass().getName().equals("org.eclipse.rdf4j.query.algebra.StatementPattern"))
                    filterExpression = new Join((TupleExpr) filterExpression, (TupleExpr) verbObjectListExpression);
                else if (verbObjectListExpression.getClass().getName().equals("org.eclipse.rdf4j.query.algebra.Join"))
                    filterExpression = new Join((TupleExpr) filterExpression, (TupleExpr) verbObjectListExpression);
                else if (verbObjectListExpression.getClass().getName().equals("org.eclipse.rdf4j.query.algebra.Compare"))
                    filterExpression = new Filter(filterExpression, (ValueExpr) verbObjectListExpression);
                else if (verbObjectListExpression.getClass().getName().equals("org.eclipse.rdf4j.query.algebra.Filter")) {
                    TupleExpr arg = ((Filter) verbObjectListExpression).getArg();
                    filterExpression = new Filter(new Join(filterExpression, arg), ((Filter) verbObjectListExpression).getCondition());
                } else
                    filterExpression = new Filter((TupleExpr) filterExpression, (ValueExpr) verbObjectListExpression);
            }
        }
    }
    return new PathTupleExpr(filterExpression);
}
Also used : ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) PathTupleExpr(com.inova8.intelligentgraph.path.PathTupleExpr) Filter(org.eclipse.rdf4j.query.algebra.Filter) And(org.eclipse.rdf4j.query.algebra.And) QueryModelNode(org.eclipse.rdf4j.query.algebra.QueryModelNode) Join(org.eclipse.rdf4j.query.algebra.Join) PathTupleExpr(com.inova8.intelligentgraph.path.PathTupleExpr) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr)

Example 34 with CustomQueryOptions

use of com.inova8.intelligentgraph.context.CustomQueryOptions in project com.inova8.intelligentgraph by peterjohnlawrence.

the class CustomQueryOption method getCustomQueryOptions.

/**
 * Gets the custom query options.
 *
 * @param contexts the contexts
 * @param prefixes the prefixes
 * @return the custom query options
 */
public static CustomQueryOptions getCustomQueryOptions(org.eclipse.rdf4j.model.Resource[] contexts, Prefixes prefixes) {
    CustomQueryOptions customQueryOptions = new CustomQueryOptions();
    if (contexts != null) {
        for (org.eclipse.rdf4j.model.Resource context : contexts) {
            if (context.stringValue().startsWith(IntelligentGraphConstants.URN_CUSTOM_QUERY_OPTIONS)) {
                String queryString = null;
                try {
                    queryString = URLDecoder.decode(context.stringValue(), StandardCharsets.UTF_8.toString());
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                customQueryOptions = CustomQueryOption.splitQuery(queryString, prefixes);
                return customQueryOptions;
            }
        }
        return customQueryOptions;
    } else
        return null;
}
Also used : CustomQueryOptions(com.inova8.intelligentgraph.context.CustomQueryOptions) Resource(org.eclipse.rdf4j.model.Resource) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 35 with CustomQueryOptions

use of com.inova8.intelligentgraph.context.CustomQueryOptions in project com.inova8.intelligentgraph by peterjohnlawrence.

the class IntelligentGraphConnection method prepareDataset.

/**
 * Prepare dataset.
 *
 * @param pathElement the path element
 * @param source the source
 * @param contexts the contexts
 * @return the simple dataset
 * @throws IllegalArgumentException the illegal argument exception
 */
private SimpleDataset prepareDataset(PathElement pathElement, IntelligentGraphRepository source, Resource... contexts) throws IllegalArgumentException {
    CustomQueryOptions customQueryOptions = prepareCustomQueryOptions(pathElement, source, contexts);
    SimpleDataset dataset = getDataset(contexts);
    if (customQueryOptions != null && !customQueryOptions.isEmpty()) {
        if (dataset == null)
            dataset = new SimpleDataset();
        dataset.addDefaultGraph(iri(IntelligentGraphConstants.URN_CUSTOM_QUERY_OPTIONS + "?" + customQueryOptions.toURIEncodedString()));
    }
    return dataset;
}
Also used : CustomQueryOptions(com.inova8.intelligentgraph.context.CustomQueryOptions) SimpleDataset(org.eclipse.rdf4j.query.impl.SimpleDataset)

Aggregations

CustomQueryOptions (com.inova8.intelligentgraph.context.CustomQueryOptions)27 Thing (com.inova8.intelligentgraph.model.Thing)16 IRI (org.eclipse.rdf4j.model.IRI)14 Resource (com.inova8.intelligentgraph.model.Resource)12 Value (org.eclipse.rdf4j.model.Value)11 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)10 IntelligentGraphRepository (com.inova8.intelligentgraph.intelligentGraphRepository.IntelligentGraphRepository)9 PathTupleExpr (com.inova8.intelligentgraph.path.PathTupleExpr)9 Order (org.junit.jupiter.api.Order)9 Test (org.junit.jupiter.api.Test)9 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)9 SimpleLiteral (org.eclipse.rdf4j.model.impl.SimpleLiteral)8 EvaluationContext (com.inova8.intelligentgraph.evaluator.EvaluationContext)7 Graph (com.inova8.intelligentgraph.intelligentGraphRepository.Graph)7 SimpleDataset (org.eclipse.rdf4j.query.impl.SimpleDataset)7 ResourceResults (com.inova8.intelligentgraph.results.ResourceResults)6 Statement (org.eclipse.rdf4j.model.Statement)6 Join (org.eclipse.rdf4j.query.algebra.Join)6 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)6 ValueExprEvaluationException (org.eclipse.rdf4j.query.algebra.evaluation.ValueExprEvaluationException)5