Search in sources :

Example 46 with Predicate

use of com.inova8.intelligentgraph.model.Predicate 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 47 with Predicate

use of com.inova8.intelligentgraph.model.Predicate in project com.inova8.intelligentgraph by peterjohnlawrence.

the class IntelligentGraphConnection method addStatement.

/**
 * Adds the statement.
 *
 * @param subject the subject
 * @param predicate the predicate
 * @param object the object
 * @param contexts the contexts
 * @throws SailException the sail exception
 */
@Override
public void addStatement(Resource subject, IRI predicate, Value object, Resource... contexts) throws SailException {
    try {
        String[] predicateParts;
        if (predicate != null) {
            // predicateParts= predicate.stringValue().split(IntelligentGraphConstants.PATH_QL_REGEX);
            predicateParts = decodePredicate(predicate);
            switch(predicateParts[0]) {
                case PATHQL.addFact:
                    addFact(subject, predicateParts[1], object, contexts);
                    break;
                default:
                    super.addStatement(subject, predicate, object, contexts);
                    checkReificationsChanged(predicate);
            }
        } else
            super.addStatement(subject, predicate, object, contexts);
        this.intelligentGraphSail.clearCache();
    } catch (Exception e) {
        throw new SailException(e);
    }
}
Also used : SailException(org.eclipse.rdf4j.sail.SailException) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) SailException(org.eclipse.rdf4j.sail.SailException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PathPatternException(com.inova8.pathql.processor.PathPatternException) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Example 48 with Predicate

use of com.inova8.intelligentgraph.model.Predicate in project com.inova8.intelligentgraph by peterjohnlawrence.

the class IntelligentGraphRepository method getFacts.

public ResourceResults getFacts(String boundPredicatePattern, CustomQueryOptions customQueryOptions) {
    logger.debug("getFacts{}\n", boundPredicatePattern);
    // this.getEvaluationContext().getTracer().traceFacts(this, queryPattern);
    SimpleDataset dataset = getDataset(customQueryOptions);
    // dataset.addDefaultGraph(this.graphName);
    org.eclipse.rdf4j.model.Resource[] contextArray = dataset.getDefaultGraphs().toArray(new org.eclipse.rdf4j.model.Resource[0]);
    ResourceStatementResults results = null;
    IRI predicate = preparePredicate(PATHQL.getFacts, boundPredicatePattern);
    if (this.getRepository() == null) {
        CloseableIteration<? extends Statement, QueryEvaluationException> localStatementIterator = this.getTripleSource().getStatements(null, predicate, null);
        results = new ResourceStatementResults(localStatementIterator, this, null, customQueryOptions);
    } else {
        CloseableIteration<Statement, RepositoryException> statementIterator = this.getRepository().getConnection().getStatements(null, predicate, null, contextArray);
        results = new ResourceStatementResults(statementIterator, this, null, customQueryOptions);
    }
    return results;
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) Statement(org.eclipse.rdf4j.model.Statement) Resource(org.eclipse.rdf4j.model.Resource) ResourceStatementResults(com.inova8.intelligentgraph.results.ResourceStatementResults) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) SimpleDataset(org.eclipse.rdf4j.query.impl.SimpleDataset) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException)

Example 49 with Predicate

use of com.inova8.intelligentgraph.model.Predicate in project com.inova8.intelligentgraph by peterjohnlawrence.

the class Thing method getFact.

// public  Resource getFact(IRI predicate, SimpleLiteral scriptString, CustomQueryOptions customQueryOptions, org.eclipse.rdf4j.model.Resource ... contexts) throws HandledException {
// Resource fact = processFactObjectValue(predicate,scriptString,  customQueryOptions ,contexts);
// return fact;
/**
 * Gets the fact.
 *
 * @param predicatePattern the predicate pattern
 * @param customQueryOptions the custom query options
 * @param bindValues the bind values
 * @return the fact
 */
// }
public Resource getFact(String predicatePattern, CustomQueryOptions customQueryOptions, Value... bindValues) {
    logger.debug("getFact{}\n", predicatePattern);
    ResourceResults factValues = getFacts(predicatePattern, customQueryOptions, bindValues);
    if (factValues == null) {
        throw new NullValueReturnedException(String.format("No values found for pattern %s with subject %s, customQueryOptions %s, and bind variables %s", predicatePattern, this, customQueryOptions, bindValues));
    } else if (factValues.hasNext()) {
        return factValues.next();
    } else {
        factValues.close();
        throw new NullValueReturnedException(String.format("No values found for pattern %s with subject %s, customQueryOptions %s, and bind variables %s", predicatePattern, this, customQueryOptions, bindValues));
    }
}
Also used : NullValueReturnedException(com.inova8.intelligentgraph.exceptions.NullValueReturnedException) ResourceResults(com.inova8.intelligentgraph.results.ResourceResults)

Example 50 with Predicate

use of com.inova8.intelligentgraph.model.Predicate in project com.inova8.intelligentgraph by peterjohnlawrence.

the class Thing method getPath.

/**
 * Gets the path.
 *
 * @param predicatePattern the predicate pattern
 * @return the path
 */
public Path getPath(String predicatePattern) {
    logger.debug("getPath{}\n", predicatePattern);
    PathResults pathValues = getPaths(predicatePattern, null);
    if (pathValues == null) {
        this.getEvaluationContext().getTracer().tracePathReturnNull(this, predicatePattern);
        return new NullPath();
    } else if (pathValues.hasNext()) {
        Path path = (Path) pathValues.next();
        this.getEvaluationContext().getTracer().tracePathReturn(this, predicatePattern, path);
        return path;
    } else {
        this.getEvaluationContext().getTracer().tracePathEmpty(this, predicatePattern);
        pathValues.close();
        return new NullPath();
    }
}
Also used : Path(com.inova8.intelligentgraph.path.Path) NullPath(com.inova8.intelligentgraph.path.NullPath) NullPath(com.inova8.intelligentgraph.path.NullPath) PathResults(com.inova8.intelligentgraph.results.PathResults)

Aggregations

Order (org.junit.jupiter.api.Order)26 Test (org.junit.jupiter.api.Test)26 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)26 PathElement (com.inova8.pathql.element.PathElement)22 IRI (org.eclipse.rdf4j.model.IRI)15 Thing (com.inova8.intelligentgraph.model.Thing)12 PathTupleExpr (com.inova8.intelligentgraph.path.PathTupleExpr)10 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)10 RecognitionException (org.antlr.v4.runtime.RecognitionException)9 IntelligentGraphRepository (com.inova8.intelligentgraph.intelligentGraphRepository.IntelligentGraphRepository)8 CharStream (org.antlr.v4.runtime.CharStream)8 Statement (org.eclipse.rdf4j.model.Statement)8 Value (org.eclipse.rdf4j.model.Value)8 EvaluationContext (com.inova8.intelligentgraph.evaluator.EvaluationContext)7 SimpleLiteral (org.eclipse.rdf4j.model.impl.SimpleLiteral)7 CustomQueryOptions (com.inova8.intelligentgraph.context.CustomQueryOptions)6 Resource (com.inova8.intelligentgraph.model.Resource)6 Join (org.eclipse.rdf4j.query.algebra.Join)6 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)6 PathPatternException (com.inova8.pathql.processor.PathPatternException)5