Search in sources :

Example 51 with Predicate

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

the class Path method create.

public static Path create(Value boundValue, Value pathValue) throws RDFParseException, UnsupportedRDFormatException, IOException {
    String pathString = pathValue.stringValue();
    Model pathModel = Rio.parse(new ByteArrayInputStream(pathString.getBytes(StandardCharsets.UTF_8)), PATHQL.NAMESPACE, RDFFormat.TURTLE);
    // = new Path(boundValue);
    Path path = null;
    for (Statement pathStatement : pathModel.filter(null, PATHQL.PATH_EDGE, null)) {
        org.eclipse.rdf4j.model.Resource pathId = pathStatement.getSubject();
        path = new Path(pathId);
        break;
    }
    if (path != null) {
        for (org.eclipse.rdf4j.model.Resource edge : pathModel.filter(null, RDF.TYPE, PATHQL.EDGE).subjects()) {
            Edge pathEdge;
            Optional<org.eclipse.rdf4j.model.Resource> source = Models.getPropertyResource(pathModel, edge, PATHQL.EDGE_SOURCE);
            Optional<org.eclipse.rdf4j.model.Resource> predicate = Models.getPropertyResource(pathModel, edge, PATHQL.EDGE_PREDICATE);
            Optional<org.eclipse.rdf4j.model.Value> target = Models.getProperty(pathModel, edge, PATHQL.EDGE_TARGET);
            Optional<org.eclipse.rdf4j.model.Literal> direction = Models.getPropertyLiteral(pathModel, edge, PATHQL.EDGE_DIRECTION);
            boolean isDirect = direction.get().getLabel().equals(Direction.DIRECT.toString()) ? false : true;
            Optional<IRI> reification = Models.getPropertyIRI(pathModel, edge, PATHQL.EDGE_REIFICATION);
            if (reification.isPresent()) {
                Optional<org.eclipse.rdf4j.model.Literal> dereified = Models.getPropertyLiteral(pathModel, edge, PATHQL.EDGE_DEREIFIED);
                boolean isDereified = dereified.get().booleanValue();
                Optional<org.eclipse.rdf4j.model.IRI> reified = Models.getPropertyIRI(pathModel, edge, PATHQL.EDGE_REIFIED);
                pathEdge = new Edge(source.get(), reified.get(), reification.get(), predicate.get(), target.get(), isDirect, isDereified);
            } else {
                pathEdge = new Edge(source.get(), predicate.get(), target.get(), isDirect);
            }
            path.add(pathEdge);
        }
    }
    return path;
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) Statement(org.eclipse.rdf4j.model.Statement) Resource(com.inova8.intelligentgraph.model.Resource) ByteArrayInputStream(java.io.ByteArrayInputStream) Model(org.eclipse.rdf4j.model.Model) Value(org.eclipse.rdf4j.model.Value)

Example 52 with Predicate

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

the class IntelligentEvaluator method processFactObjectValue.

/**
 * Process fact object value.
 *
 * @param thing the thing
 * @param predicate the predicate
 * @param objectValue the object value
 * @param customQueryOptions the custom query options
 * @param contexts the contexts
 * @return the resource
 * @throws QueryEvaluationException the query evaluation exception
 */
public static Resource processFactObjectValue(Thing thing, IRI predicate, Value objectValue, CustomQueryOptions customQueryOptions, org.eclipse.rdf4j.model.Resource... contexts) throws QueryEvaluationException {
    Resource returnResult = null;
    SimpleLiteral literalValue;
    {
        literalValue = (SimpleLiteral) objectValue;
        if (Evaluator.isScriptEngine(literalValue.getDatatype())) {
            org.eclipse.rdf4j.model.Resource customQueryOptionsContext;
            if (customQueryOptions != null)
                customQueryOptionsContext = customQueryOptions.getContext();
            else
                customQueryOptionsContext = CustomQueryOptions.getEmptyContext();
            FactCache factCache = thing.getSource().getIntelligentGraphConnection().getIntelligentGraphSail().getFactCache();
            if (/*notTracing() && */
            factCache.contains(thing.getIRI(), predicate, customQueryOptionsContext)) {
                Value resultValue = factCache.getFacts(thing.getIRI(), predicate, customQueryOptionsContext);
                logger.debug("Retrieved cache {} of {} = {}", predicate.stringValue(), thing.getSuperValue().stringValue(), resultValue.stringValue());
                thing.getEvaluationContext().getTracer().traceRetrievedCache(thing, predicate, resultValue);
                returnResult = Resource.create(thing.getSource(), resultValue, thing.getEvaluationContext());
            } else {
                Resource result = handleScript(thing, literalValue, predicate, customQueryOptions, contexts);
                if (result != null) {
                    // TODO validate caching
                    factCache.addFact(thing.getIRI(), predicate, result.getSuperValue(), customQueryOptionsContext);
                    thing.getEvaluationContext().getTracer().traceCalculated(thing, predicate, result);
                }
                returnResult = result;
            }
        } else {
            thing.getEvaluationContext().getTracer().traceRetrievedLiteral(thing, predicate, objectValue);
            returnResult = Resource.create(thing.getSource(), objectValue, thing.getEvaluationContext());
        }
    }
    return returnResult;
}
Also used : Resource(com.inova8.intelligentgraph.model.Resource) Value(org.eclipse.rdf4j.model.Value) SimpleLiteral(org.eclipse.rdf4j.model.impl.SimpleLiteral)

Example 53 with Predicate

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

the class Thing method getPaths.

/**
 * Gets the paths.
 *
 * @param predicatePattern the predicate pattern
 * @param customQueryOptions the custom query options
 * @return the paths
 */
public PathResults getPaths(String predicatePattern, CustomQueryOptions customQueryOptions) {
    logger.debug("getPaths{}\n", predicatePattern);
    this.getEvaluationContext().getTracer().tracePaths(this, predicatePattern);
    SimpleDataset dataset = IntelligentGraphRepository.getDataset(customQueryOptions);
    dataset.addDefaultGraph(this.graphName);
    org.eclipse.rdf4j.model.Resource[] contextArray = dataset.getDefaultGraphs().toArray(new org.eclipse.rdf4j.model.Resource[0]);
    PathResults results = null;
    IRI predicate = IntelligentGraphRepository.preparePredicate(PATHQL.getPaths, predicatePattern);
    if (this.getSource().getRepository() == null) {
        CloseableIteration<? extends Statement, QueryEvaluationException> localPathIterator = this.getSource().getTripleSource().getStatements(this.getIRI(), predicate, null, contextArray);
        results = new PathResults(localPathIterator, this, null);
    } else {
        CloseableIteration<Statement, RepositoryException> pathIterator = this.getSource().getRepository().getConnection().getStatements(this.getIRI(), predicate, null, contextArray);
        results = new PathResults(pathIterator, this, null, customQueryOptions);
    }
    return results;
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) Statement(org.eclipse.rdf4j.model.Statement) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) SimpleDataset(org.eclipse.rdf4j.query.impl.SimpleDataset) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) PathResults(com.inova8.intelligentgraph.results.PathResults)

Example 54 with Predicate

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

the class Local_MultiGraphAddGetFact_Tests method test_25.

/**
 * Test 25.
 */
@Test
@Order(25)
void test_25() {
    try {
        // Thing myCountry = addGraph2();
        Graph graph = source.addGraph("<http://inova8.com/calc2graph/testGraph2>");
        Thing myCountry = graph.getThing(":Country");
        myCountry.addFact(":sales", "1");
        myCountry.addFact(":sales", "2");
        myCountry.addFact(":sales", "3");
        myCountry.addFact(":sales", "4");
        myCountry.addFact(":sales", "5");
        String averageSalesScript = "totalSales=0; count=0;for(sales in _this.getFacts(\"<http://inova8.com/calc2graph/def/sales>\")){totalSales +=  sales.doubleValue();count++}; return totalSales/count;";
        myCountry.addFact(":averageSales", averageSalesScript, SCRIPT.GROOVY);
        CustomQueryOptions customQueryOptions = new CustomQueryOptions();
        customQueryOptions.add("time", 42);
        customQueryOptions.add("name", "Peter");
        Trace averageCountrySalesTrace = myCountry.traceFact(":averageSales", customQueryOptions);
        source.removeGraph("<http://inova8.com/calc2graph/testGraph2>");
        // Query.assertEqualsWOSpaces
        assertEquals("<ol style='list-style-type:none;'><li>Getting facts  ':averageSales' of <a href='http://inova8.com/calc2graph/def/Country' target='_blank'>Country</a></li></li><li>...using options: [name=&quot;Peter&quot;&amp;time=&quot;42&quot;^^&lt;http://www.w3.org/2001/XMLSchema#int&gt;]</li></li><li>...within contexts: [file://src/test/resources/calc2graph.def.ttl, http://inova8.com/calc2graph/testGraph2]</li></li><ol style='list-style-type:none;'><li>Evaluating predicate <a href='http://inova8.com/calc2graph/def/averageSales' target='_blank'>averageSales</a> of <a href='http://inova8.com/calc2graph/def/Country' target='_blank'>Country</a>, by invoking <b>groovy</b> script\n" + "</li></li><li><div  style='border: 1px solid black;'> <pre><code >totalSales=0; count=0;for(sales in _this.getFacts(&quot;&lt;http://inova8.com/calc2graph/def/sales&gt;&quot;)){totalSales += &nbsp;sales.doubleValue();count++}; return totalSales/count;</code></pre></div></li><ol style='list-style-type:none;'><li>Getting facts '&lt;http://inova8.com/calc2graph/def/sales&gt;' of <a href='http://inova8.com/calc2graph/def/Country' target='_blank'>Country</a> </li></li><li>Next fact 'http://inova8.com/calc2graph/def/sales' of <a href='http://inova8.com/calc2graph/def/Country' target='_blank'>Country</a> = 1</li></li><li>Next fact 'http://inova8.com/calc2graph/def/sales' of <a href='http://inova8.com/calc2graph/def/Country' target='_blank'>Country</a> = 2</li></li><li>Next fact 'http://inova8.com/calc2graph/def/sales' of <a href='http://inova8.com/calc2graph/def/Country' target='_blank'>Country</a> = 3</li></li><li>Next fact 'http://inova8.com/calc2graph/def/sales' of <a href='http://inova8.com/calc2graph/def/Country' target='_blank'>Country</a> = 4</li></li><li>Next fact 'http://inova8.com/calc2graph/def/sales' of <a href='http://inova8.com/calc2graph/def/Country' target='_blank'>Country</a> = 5</li></li></ol><li>Evaluated <a href='http://inova8.com/calc2graph/def/averageSales' target='_blank'>averageSales</a> of <a href='http://inova8.com/calc2graph/def/Country' target='_blank'>Country</a> =  3.0^^<a href='http://www.w3.org/2001/XMLSchema#double' target='_blank'>double</a></li></li></ol><li>Calculated <a href='http://inova8.com/calc2graph/def/averageSales' target='_blank'>averageSales</a> of <a href='http://inova8.com/calc2graph/def/Country' target='_blank'>Country</a> = 3.0^^<a href='http://www.w3.org/2001/XMLSchema#double' target='_blank'>double</a></li></li><li>Retrieved cached value <a href='http://inova8.com/calc2graph/def/averageSales' target='_blank'>averageSales</a> of <a href='http://inova8.com/calc2graph/def/Country' target='_blank'>Country</a> = 3.0^^<a href='http://www.w3.org/2001/XMLSchema#double' target='_blank'>double</a></li></li><li>Returned fact 'http://inova8.com/calc2graph/def/averageSales' of <a href='http://inova8.com/calc2graph/def/Country' target='_blank'>Country</a> = 3.0^^<a href='http://www.w3.org/2001/XMLSchema#double' target='_blank'>double</a></li></li><p></ol>", averageCountrySalesTrace.asHTML());
    } catch (Exception e) {
        assertEquals("", e.getMessage());
    }
}
Also used : Trace(com.inova8.intelligentgraph.evaluator.Trace) Graph(com.inova8.intelligentgraph.intelligentGraphRepository.Graph) CustomQueryOptions(com.inova8.intelligentgraph.context.CustomQueryOptions) Thing(com.inova8.intelligentgraph.model.Thing) PathPatternException(com.inova8.pathql.processor.PathPatternException) RecognitionException(org.antlr.v4.runtime.RecognitionException) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Order(org.junit.jupiter.api.Order) Test(org.junit.jupiter.api.Test)

Example 55 with Predicate

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

the class PathPatternQueryExpressionTests method test_6.

/**
 * Test 6.
 */
@Test
@Order(6)
void test_6() {
    CharStream input = CharStreams.fromString(":Location@:appearsOn[ eq [ rdfs:label \"Calc2Graph1\"] ]#/^:lat/:long/^:left/:right");
    PathElement element = prepareElement(input);
    // Query.assertEqualsWOSpaces
    assertEquals("Join\r\n" + "   Join\r\n" + "      Join\r\n" + "         Join\r\n" + "            Join\r\n" + "               Join\r\n" + "                  StatementPattern\r\n" + "                     Variable (name=r1)\r\n" + "                     Variable (name=subject1, value=http://www.w3.org/1999/02/22-rdf-syntax-ns#subject)\r\n" + "                     Variable (name=n0)\r\n" + "                  StatementPattern\r\n" + "                     Variable (name=r1)\r\n" + "                     Variable (name=property1, value=http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate)\r\n" + "                     Variable (name=p_n0_n1, value=http://default/appearsOn)\r\n" + "               StatementPattern\r\n" + "                  Variable (name=r1)\r\n" + "                  Variable (name=object1, value=http://www.w3.org/1999/02/22-rdf-syntax-ns#object)\r\n" + "                  Variable (name=n1)\r\n" + "            StatementPattern\r\n" + "               Variable (name=n2)\r\n" + "               Variable (name=p_r1_n2, value=http://default/lat)\r\n" + "               Variable (name=r1)\r\n" + "         StatementPattern\r\n" + "            Variable (name=n2)\r\n" + "            Variable (name=p_n2_n3, value=http://default/long)\r\n" + "            Variable (name=n3)\r\n" + "      StatementPattern\r\n" + "         Variable (name=n4)\r\n" + "         Variable (name=p_n3_n4, value=http://default/left)\r\n" + "         Variable (name=n3)\r\n" + "   StatementPattern\r\n" + "      Variable (name=n4)\r\n" + "      Variable (name=p_n4_n5, value=http://default/right)\r\n" + "      Variable (name=n5)\r\n" + "", element.pathPatternQuery().toString());
}
Also used : PathElement(com.inova8.pathql.element.PathElement) CharStream(org.antlr.v4.runtime.CharStream) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Order(org.junit.jupiter.api.Order) Test(org.junit.jupiter.api.Test)

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