Search in sources :

Example 1 with Edge

use of com.inova8.intelligentgraph.path.Edge in project com.inova8.intelligentgraph by peterjohnlawrence.

the class Local_TutorialTests method test_7.

/**
 * Test 7.
 */
@SuppressWarnings("resource")
@Test
@Order(7)
void test_7() {
    try {
        Repository workingRep = Query.createNativeLuceneIntelligentGraphRepository("src/test/resources/datadir/Local_TutorialTests7/");
        RepositoryConnection conn = workingRep.getConnection();
        conn.setNamespace("", "http://inova8.com/intelligentgraph/example7/");
        conn.setNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
        conn.setNamespace("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
        IntelligentGraphRepository source = IntelligentGraphRepository.create(workingRep);
        source.removeGraph("<http://inova8.com/intelligentgraph/example7>");
        Graph graph = source.addGraph("<http://inova8.com/intelligentgraph/example7>");
        Thing aPerson = graph.getThing(":aPerson");
        Thing Another1 = graph.getThing(":Another1");
        Thing Another2 = graph.getThing(":Another2");
        Thing Another3 = graph.getThing(":Another3");
        Thing Another4 = graph.getThing(":Another4");
        Thing Another5 = graph.getThing(":Another5");
        Thing Another6 = graph.getThing(":Another6");
        Thing Another7 = graph.getThing(":Another7");
        Thing Another8 = graph.getThing(":Another8");
        Thing Another9 = graph.getThing(":Another9");
        Thing Another10 = graph.getThing(":Another10");
        Thing Another11 = graph.getThing(":Another11");
        Thing Another12 = graph.getThing(":Another12");
        Thing Location = graph.getThing(":Location");
        Thing Maidstone = graph.getThing(":Maidstone").addFact(RDF.TYPE, Location);
        Thing Tideswell = graph.getThing(":Tideswell").addFact(RDF.TYPE, Location);
        Thing Gender = graph.getThing(":Gender");
        Thing Male = graph.getThing(":Male").addFact(RDF.TYPE, Gender);
        Thing Female = graph.getThing(":Female").addFact(RDF.TYPE, Gender);
        Thing Person = graph.getThing(":Person");
        aPerson.addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.7", XSD.DOUBLE).addFact(":hasWeight", "62", XSD.DOUBLE).addFact(":hasLocation", Tideswell).addFact(":hasGender", Male).addFact(":hasParent", Another1).addFact(":hasParent", Another3);
        Another1.addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.9", XSD.DOUBLE).addFact(":hasWeight", "72", XSD.DOUBLE).addFact(":hasLocation", Tideswell).addFact(":hasGender", Male).addFact(":hasParent", Another2);
        Another2.addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.7", XSD.DOUBLE).addFact(":hasWeight", "65", XSD.DOUBLE).addFact(":hasLocation", Maidstone).addFact(":hasGender", Male);
        Another3.addFact(RDF.TYPE, Person).addFact(":hasHeight", "2", XSD.DOUBLE).addFact(":hasWeight", "59", XSD.DOUBLE).addFact(":hasLocation", Maidstone).addFact(":hasGender", Female).addFact(":hasParent", Another4);
        Another4.addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.8", XSD.DOUBLE).addFact(":hasWeight", "47", XSD.DOUBLE).addFact(":hasLocation", Maidstone).addFact(":hasGender", Male).addFact(":hasParent", Another5).addFact(":hasParent", Another6);
        Another5.addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.5", XSD.DOUBLE).addFact(":hasWeight", "70", XSD.DOUBLE).addFact(":hasLocation", Maidstone).addFact(":hasGender", Female);
        Another6.addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.5", XSD.DOUBLE).addFact(":hasWeight", "56", XSD.DOUBLE).addFact(":hasLocation", Maidstone).addFact(":hasGender", Male);
        Another7.addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.7", XSD.DOUBLE).addFact(":hasWeight", "63", XSD.DOUBLE).addFact(":hasLocation", Maidstone).addFact(":hasGender", Female).addFact(":hasParent", Another5).addFact(":hasParent", Another8).addFact(":hasParent", Another9);
        Another8.addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.6", XSD.DOUBLE).addFact(":hasWeight", "66", XSD.DOUBLE).addFact(":hasLocation", Maidstone).addFact(":hasGender", Male);
        Another9.addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.7", XSD.DOUBLE).addFact(":hasWeight", "46", XSD.DOUBLE).addFact(":hasLocation", Maidstone).addFact(":hasGender", Female).addFact(":hasParent", Another10).addFact(":hasParent", Another11).addFact(":hasParent", Another12);
        Another10.addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.9", XSD.DOUBLE).addFact(":hasWeight", "61", XSD.DOUBLE).addFact(":hasLocation", Tideswell).addFact(":hasGender", Female);
        Another11.addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.5", XSD.DOUBLE).addFact(":hasWeight", "61", XSD.DOUBLE).addFact(":hasLocation", Tideswell).addFact(":hasGender", Male);
        Another11.addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.8", XSD.DOUBLE).addFact(":hasWeight", "75", XSD.DOUBLE).addFact(":hasLocation", Tideswell).addFact(":hasGender", Female);
        assertEquals("http://inova8.com/intelligentgraph/example7/aPerson", aPerson.stringValue());
        assertEquals("http://inova8.com/intelligentgraph/example7/Another3", aPerson.getFact(":hasParent[:hasGender :Female]").stringValue());
        assertEquals("http://inova8.com/intelligentgraph/example7/Another3", aPerson.getFact(":hasParent[:hasLocation :Maidstone]").stringValue());
        assertEquals("http://inova8.com/intelligentgraph/example7/Another3", aPerson.getFact(":hasParent{0,4}/:hasParent[:hasGender :Female]").stringValue());
        assertEquals("[ {s=http://inova8.com/intelligentgraph/example7/aPerson, p=http://inova8.com/intelligentgraph/example7/hasParent, o=http://inova8.com/intelligentgraph/example7/Another3}; {s=http://inova8.com/intelligentgraph/example7/Another4, p=http://inova8.com/intelligentgraph/example7/hasParent, o=http://inova8.com/intelligentgraph/example7/Another5};]", aPerson.getFacts(":hasParent{0,4}/:hasParent[:hasGender :Female]").toString());
        assertEquals("[ {s=http://inova8.com/intelligentgraph/example7/aPerson, p=http://inova8.com/intelligentgraph/example7/hasParent, o=http://inova8.com/intelligentgraph/example7/Another3}; {s=http://inova8.com/intelligentgraph/example7/Another1, p=http://inova8.com/intelligentgraph/example7/hasParent, o=http://inova8.com/intelligentgraph/example7/Another2}; {s=http://inova8.com/intelligentgraph/example7/Another3, p=http://inova8.com/intelligentgraph/example7/hasParent, o=http://inova8.com/intelligentgraph/example7/Another4}; {s=http://inova8.com/intelligentgraph/example7/Another4, p=http://inova8.com/intelligentgraph/example7/hasParent, o=http://inova8.com/intelligentgraph/example7/Another5}; {s=http://inova8.com/intelligentgraph/example7/Another4, p=http://inova8.com/intelligentgraph/example7/hasParent, o=http://inova8.com/intelligentgraph/example7/Another6};]", aPerson.getFacts(":hasParent{0,4}/:hasParent[:hasLocation :Maidstone]").toString());
        PathResults paths = aPerson.getPaths(":hasParent{0,4}/:hasParent[:hasGender :Female]");
        assertEquals("Path=[[http://inova8.com/intelligentgraph/example7/aPerson,http://inova8.com/intelligentgraph/example7/hasParent,http://inova8.com/intelligentgraph/example7/Another3,DIRECT]\r\n" + "]\r\n" + "Path=[[http://inova8.com/intelligentgraph/example7/aPerson,http://inova8.com/intelligentgraph/example7/hasParent,http://inova8.com/intelligentgraph/example7/Another3,DIRECT]\r\n" + "[http://inova8.com/intelligentgraph/example7/Another3,http://inova8.com/intelligentgraph/example7/hasParent,http://inova8.com/intelligentgraph/example7/Another4,DIRECT]\r\n" + "[http://inova8.com/intelligentgraph/example7/Another4,http://inova8.com/intelligentgraph/example7/hasParent,http://inova8.com/intelligentgraph/example7/Another5,DIRECT]\r\n" + "]\r\n" + "", paths.toString());
        paths = aPerson.getPaths(":hasParent{0,4}/:hasParent[:hasLocation :Maidstone]");
        String pathString = "";
        for (Path path : paths) {
            pathString += "Path" + "\n";
            for (Edge edge : path) {
                pathString += edge.toString() + "\n";
            }
        }
        assertEquals("Path\n" + "[http://inova8.com/intelligentgraph/example7/aPerson,http://inova8.com/intelligentgraph/example7/hasParent,http://inova8.com/intelligentgraph/example7/Another3,DIRECT]\n" + "Path\n" + "[http://inova8.com/intelligentgraph/example7/aPerson,http://inova8.com/intelligentgraph/example7/hasParent,http://inova8.com/intelligentgraph/example7/Another1,DIRECT]\n" + "[http://inova8.com/intelligentgraph/example7/Another1,http://inova8.com/intelligentgraph/example7/hasParent,http://inova8.com/intelligentgraph/example7/Another2,DIRECT]\n" + "Path\n" + "[http://inova8.com/intelligentgraph/example7/aPerson,http://inova8.com/intelligentgraph/example7/hasParent,http://inova8.com/intelligentgraph/example7/Another3,DIRECT]\n" + "[http://inova8.com/intelligentgraph/example7/Another3,http://inova8.com/intelligentgraph/example7/hasParent,http://inova8.com/intelligentgraph/example7/Another4,DIRECT]\n" + "Path\n" + "[http://inova8.com/intelligentgraph/example7/aPerson,http://inova8.com/intelligentgraph/example7/hasParent,http://inova8.com/intelligentgraph/example7/Another3,DIRECT]\n" + "[http://inova8.com/intelligentgraph/example7/Another3,http://inova8.com/intelligentgraph/example7/hasParent,http://inova8.com/intelligentgraph/example7/Another4,DIRECT]\n" + "[http://inova8.com/intelligentgraph/example7/Another4,http://inova8.com/intelligentgraph/example7/hasParent,http://inova8.com/intelligentgraph/example7/Another5,DIRECT]\n" + "Path\n" + "[http://inova8.com/intelligentgraph/example7/aPerson,http://inova8.com/intelligentgraph/example7/hasParent,http://inova8.com/intelligentgraph/example7/Another3,DIRECT]\n" + "[http://inova8.com/intelligentgraph/example7/Another3,http://inova8.com/intelligentgraph/example7/hasParent,http://inova8.com/intelligentgraph/example7/Another4,DIRECT]\n" + "[http://inova8.com/intelligentgraph/example7/Another4,http://inova8.com/intelligentgraph/example7/hasParent,http://inova8.com/intelligentgraph/example7/Another6,DIRECT]\n" + "", pathString);
        @SuppressWarnings("unused") Thing BMI = graph.getThing(":BMI").addFact(SCRIPT.SCRIPTCODE, "height=_this.getFact(':hasHeight').doubleValue(); _this.getFact(':hasWeight').doubleValue()/(height*height)", SCRIPT.GROOVY);
        @SuppressWarnings("unused") Thing familialRelativeBMI = graph.getThing(":FamilialRelativeBMI").addFact(SCRIPT.SCRIPTCODE, "_this.getFact(':hasBMI').doubleValue()/_this.getFacts(':hasParent{0,4}/:hasBMI').average();", SCRIPT.GROOVY);
        for (Resource person : graph.getThing(":Person").getFacts("^rdf:type")) {
            ((Thing) person).addFact(":hasBMI", "<:BMI>", SCRIPT.GROOVY).addFact(":hasFamilialRelativeBMI", "<:FamilialRelativeBMI>", SCRIPT.GROOVY);
        }
        assertEquals(1.006889937409004, aPerson.getFact(":hasFamilialRelativeBMI").doubleValue());
    } catch (Exception e) {
        assertEquals("", e.getCause().getMessage());
    }
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) Path(com.inova8.intelligentgraph.path.Path) Repository(org.eclipse.rdf4j.repository.Repository) IntelligentGraphRepository(com.inova8.intelligentgraph.intelligentGraphRepository.IntelligentGraphRepository) IntelligentGraphRepository(com.inova8.intelligentgraph.intelligentGraphRepository.IntelligentGraphRepository) Graph(com.inova8.intelligentgraph.intelligentGraphRepository.Graph) Resource(com.inova8.intelligentgraph.model.Resource) PathResults(com.inova8.intelligentgraph.results.PathResults) Edge(com.inova8.intelligentgraph.path.Edge) Thing(com.inova8.intelligentgraph.model.Thing) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Order(org.junit.jupiter.api.Order) Test(org.junit.jupiter.api.Test)

Example 2 with Edge

use of com.inova8.intelligentgraph.path.Edge in project com.inova8.intelligentgraph by peterjohnlawrence.

the class IntelligentStatementPaths method pathToLiteral.

/**
 * Path to literal.
 *
 * @param bindingset the bindingset
 * @return the literal
 */
private Literal pathToLiteral(BindingSet bindingset) {
    ModelBuilder builder = new ModelBuilder();
    builder.setNamespace("", PATHQL.NAMESPACE);
    PathBinding thisPathBinding = pathTupleExpr.getPath();
    for (EdgeBinding edge : thisPathBinding) {
        edge.addEdgeToPathModel(builder, bindingset);
    }
    Model pathModel = builder.build();
    OutputStream pathJSON = new ByteArrayOutputStream();
    Rio.write(pathModel, pathJSON, RDFFormat.TURTLE);
    return literal(pathJSON.toString());
}
Also used : ModelBuilder(org.eclipse.rdf4j.model.util.ModelBuilder) EdgeBinding(com.inova8.intelligentgraph.path.EdgeBinding) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) Model(org.eclipse.rdf4j.model.Model) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PathBinding(com.inova8.intelligentgraph.path.PathBinding)

Example 3 with Edge

use of com.inova8.intelligentgraph.path.Edge 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 4 with Edge

use of com.inova8.intelligentgraph.path.Edge in project com.inova8.intelligentgraph by peterjohnlawrence.

the class FactResults method nextResource.

/**
 * Next resource.
 *
 * @return the resource
 * @throws QueryEvaluationException the query evaluation exception
 */
@Override
public Resource nextResource() throws QueryEvaluationException {
    BindingSet next = getResourceSet().next();
    PathBinding path = new PathBinding();
    path = pathElement.visitPathBinding(path, 0);
    // for(Edge edge:path) {
    // TODO
    // Value sourceValue = next.getBinding(edge.getSourceVariable().getName()).getValue();
    // Value predicateValue = next.getBinding(edge.getPredicateVariable().getName()).getValue();
    // Value reificationValue = edge.getReification();
    // Value targetValue = next.getBinding(edge.getTargetVariable().getName()).getValue();
    // Boolean direction = edge.isInverse();
    // Boolean isDereified = edge.getIsDereified();
    // Fact edgeFact = new Fact(sourceValue, predicateValue, targetValue);
    // }
    Resource objectValue = calculateValue(next);
    return new Fact(next.getBinding(getPathElement().getTargetSubject().getName()).getValue(), next.getBinding(getPathElement().getTargetPredicate().getName()).getValue(), objectValue.getValue());
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) Resource(com.inova8.intelligentgraph.model.Resource) PathBinding(com.inova8.intelligentgraph.path.PathBinding) Fact(com.inova8.intelligentgraph.model.Fact)

Aggregations

Resource (com.inova8.intelligentgraph.model.Resource)3 PathBinding (com.inova8.intelligentgraph.path.PathBinding)2 Model (org.eclipse.rdf4j.model.Model)2 Graph (com.inova8.intelligentgraph.intelligentGraphRepository.Graph)1 IntelligentGraphRepository (com.inova8.intelligentgraph.intelligentGraphRepository.IntelligentGraphRepository)1 Fact (com.inova8.intelligentgraph.model.Fact)1 Thing (com.inova8.intelligentgraph.model.Thing)1 Edge (com.inova8.intelligentgraph.path.Edge)1 EdgeBinding (com.inova8.intelligentgraph.path.EdgeBinding)1 Path (com.inova8.intelligentgraph.path.Path)1 PathResults (com.inova8.intelligentgraph.results.PathResults)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 IRI (org.eclipse.rdf4j.model.IRI)1 Statement (org.eclipse.rdf4j.model.Statement)1 Value (org.eclipse.rdf4j.model.Value)1 ModelBuilder (org.eclipse.rdf4j.model.util.ModelBuilder)1 BindingSet (org.eclipse.rdf4j.query.BindingSet)1 Repository (org.eclipse.rdf4j.repository.Repository)1