Search in sources :

Example 26 with Concept

use of ai.grakn.concept.Concept in project grakn by graknlabs.

the class ShortestPathTest method testMultiplePathsSharing1Instance.

@Test
public void testMultiplePathsSharing1Instance() throws InvalidKBException {
    ConceptId startId;
    ConceptId endId;
    Set<List<ConceptId>> correctPaths = new HashSet<>();
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        EntityType entityType = graph.putEntityType(thing);
        Role role1 = graph.putRole("role1");
        Role role2 = graph.putRole("role2");
        entityType.plays(role1).plays(role2);
        RelationshipType relationshipType1 = graph.putRelationshipType(related).relates(role1).relates(role2);
        Role role3 = graph.putRole("role3");
        Role role4 = graph.putRole("role4");
        entityType.plays(role3).plays(role4);
        RelationshipType relationshipType2 = graph.putRelationshipType(veryRelated).relates(role3).relates(role4);
        Entity start = entityType.addEntity();
        Entity end = entityType.addEntity();
        Entity middle = entityType.addEntity();
        startId = start.getId();
        endId = end.getId();
        ConceptId middleId = middle.getId();
        ConceptId assertion11 = relationshipType1.addRelationship().addRolePlayer(role1, start).addRolePlayer(role2, middle).getId();
        ConceptId assertion12 = relationshipType1.addRelationship().addRolePlayer(role1, middle).addRolePlayer(role2, end).getId();
        ConceptId assertion21 = relationshipType2.addRelationship().addRolePlayer(role3, start).addRolePlayer(role4, middle).getId();
        ConceptId assertion22 = relationshipType2.addRelationship().addRolePlayer(role3, middle).addRolePlayer(role4, end).getId();
        correctPaths.add(Lists.newArrayList(startId, assertion11, middleId, assertion12, endId));
        correctPaths.add(Lists.newArrayList(startId, assertion11, middleId, assertion22, endId));
        correctPaths.add(Lists.newArrayList(startId, assertion21, middleId, assertion12, endId));
        correctPaths.add(Lists.newArrayList(startId, assertion21, middleId, assertion22, endId));
        graph.commit();
    }
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        List<List<Concept>> allPaths = graph.graql().compute().paths().from(startId).to(endId).execute();
        assertEquals(correctPaths.size(), allPaths.size());
        Set<List<ConceptId>> computedPaths = allPaths.stream().map(path -> path.stream().map(Concept::getId).collect(Collectors.toList())).collect(Collectors.toSet());
        assertEquals(correctPaths, computedPaths);
    }
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) InvalidKBException(ai.grakn.exception.InvalidKBException) GraknTestUtil(ai.grakn.util.GraknTestUtil) Role(ai.grakn.concept.Role) Assume.assumeFalse(org.junit.Assume.assumeFalse) Concept(ai.grakn.concept.Concept) Entity(ai.grakn.concept.Entity) Graql(ai.grakn.graql.Graql) EntityType(ai.grakn.concept.EntityType) ArrayList(java.util.ArrayList) Attribute(ai.grakn.concept.Attribute) SessionContext(ai.grakn.test.rule.SessionContext) HashSet(java.util.HashSet) Assert.assertThat(org.junit.Assert.assertThat) Lists(com.google.common.collect.Lists) Label(ai.grakn.concept.Label) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) GraknTx(ai.grakn.GraknTx) ConceptId(ai.grakn.concept.ConceptId) ClassRule(org.junit.ClassRule) Before(org.junit.Before) GraknTxType(ai.grakn.GraknTxType) Matchers.empty(org.hamcrest.Matchers.empty) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Utility.getResourceEdgeId(ai.grakn.graql.internal.analytics.Utility.getResourceEdgeId) GraknSession(ai.grakn.GraknSession) Set(java.util.Set) Test(org.junit.Test) Collectors(java.util.stream.Collectors) List(java.util.List) Optional(java.util.Optional) Schema(ai.grakn.util.Schema) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) RelationshipType(ai.grakn.concept.RelationshipType) ArrayList(java.util.ArrayList) List(java.util.List) ConceptId(ai.grakn.concept.ConceptId) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 27 with Concept

use of ai.grakn.concept.Concept in project grakn by graknlabs.

the class ShortestPathTest method testMultiplePathInSubGraph.

@Test
public void testMultiplePathInSubGraph() {
    Set<List<ConceptId>> correctPaths = new HashSet<>();
    List<List<Concept>> allPaths;
    addSchemaAndEntities();
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        if (GraknTestUtil.usingJanus()) {
            // If no path is found in the vertex program, NoResultException is thrown to skip map reduce.
            // Tinker doesn't handle this well, so the next graql query would find the graph is empty.
            allPaths = graph.graql().compute().paths().in(thing, anotherThing).to(entityId1).from(entityId4).execute();
            assertEquals(0, allPaths.size());
        }
        correctPaths.add(Lists.newArrayList(entityId1, relationId12, entityId2, relationId24, entityId4));
        correctPaths.add(Lists.newArrayList(entityId1, relationId13, entityId3, relationId34, entityId4));
        allPaths = graph.graql().compute().paths().from(entityId1).to(entityId4).execute();
        assertEquals(correctPaths.size(), allPaths.size());
        Set<List<ConceptId>> computedPaths = allPaths.stream().map(path -> path.stream().map(Concept::getId).collect(Collectors.toList())).collect(Collectors.toSet());
        assertEquals(correctPaths, computedPaths);
    }
}
Also used : InvalidKBException(ai.grakn.exception.InvalidKBException) GraknTestUtil(ai.grakn.util.GraknTestUtil) Role(ai.grakn.concept.Role) Assume.assumeFalse(org.junit.Assume.assumeFalse) Concept(ai.grakn.concept.Concept) Entity(ai.grakn.concept.Entity) Graql(ai.grakn.graql.Graql) EntityType(ai.grakn.concept.EntityType) ArrayList(java.util.ArrayList) Attribute(ai.grakn.concept.Attribute) SessionContext(ai.grakn.test.rule.SessionContext) HashSet(java.util.HashSet) Assert.assertThat(org.junit.Assert.assertThat) Lists(com.google.common.collect.Lists) Label(ai.grakn.concept.Label) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) GraknTx(ai.grakn.GraknTx) ConceptId(ai.grakn.concept.ConceptId) ClassRule(org.junit.ClassRule) Before(org.junit.Before) GraknTxType(ai.grakn.GraknTxType) Matchers.empty(org.hamcrest.Matchers.empty) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Utility.getResourceEdgeId(ai.grakn.graql.internal.analytics.Utility.getResourceEdgeId) GraknSession(ai.grakn.GraknSession) Set(java.util.Set) Test(org.junit.Test) Collectors(java.util.stream.Collectors) List(java.util.List) Optional(java.util.Optional) Schema(ai.grakn.util.Schema) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) GraknTx(ai.grakn.GraknTx) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 28 with Concept

use of ai.grakn.concept.Concept in project grakn by graknlabs.

the class GrpcClient method getAttributesByValue.

public Stream<? extends Concept> getAttributesByValue(Object value) {
    communicator.send(GrpcUtil.getAttributesByValueRequest(value));
    IteratorId iteratorId = responseOrThrow().getIteratorId();
    Iterable<Concept> iterable = () -> new GraknGrpcIterator<Concept>(this, iteratorId) {

        @Override
        protected Concept getNextFromResponse(TxResponse response) {
            return conceptConverter.convert(response.getConcept());
        }
    };
    return StreamSupport.stream(iterable.spliterator(), false);
}
Also used : Concept(ai.grakn.concept.Concept) IteratorId(ai.grakn.rpc.generated.GrpcIterator.IteratorId) TxResponse(ai.grakn.rpc.generated.GrpcGrakn.TxResponse)

Example 29 with Concept

use of ai.grakn.concept.Concept in project grakn by graknlabs.

the class GeoInferenceTest method testTransitiveQuery_withSpecificResource_noRoles.

@Test
public void testTransitiveQuery_withSpecificResource_noRoles() {
    GraknTx graph = geoKB.tx();
    QueryBuilder iqb = graph.graql().infer(true);
    Concept masovia = getConcept(graph, "name", "Masovia");
    String queryString = "match " + "($x, $y) isa is-located-in;" + "$y has name 'Masovia'; get;";
    String queryString2 = "match " + "{(geo-entity: $x, entity-location: $y) isa is-located-in or " + "(geo-entity: $y, entity-location: $x) isa is-located-in;};" + "$y has name 'Masovia'; get;";
    List<Answer> answers = iqb.materialise(false).<GetQuery>parse(queryString).execute();
    answers.forEach(ans -> assertEquals(ans.size(), 2));
    answers.forEach(ans -> assertEquals(ans.get(var("y")).getId().getValue(), masovia.getId().getValue()));
    assertEquals(answers.size(), 5);
    List<Answer> answers2 = iqb.materialise(false).<GetQuery>parse(queryString2).execute();
    assertCollectionsEqual(answers, answers2);
}
Also used : Concept(ai.grakn.concept.Concept) GraknTx(ai.grakn.GraknTx) Answer(ai.grakn.graql.admin.Answer) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 30 with Concept

use of ai.grakn.concept.Concept in project grakn by graknlabs.

the class GeoInferenceTest method testTransitiveQuery_withSubstitution.

@Test
public void testTransitiveQuery_withSubstitution() {
    GraknTx graph = geoKB.tx();
    QueryBuilder iqb = graph.graql().infer(true);
    Concept poland = getConcept(graph, "name", "Poland");
    Concept europe = getConcept(graph, "name", "Europe");
    String queryString = "match " + "(geo-entity: $x, entity-location: $y) isa is-located-in;" + "$y id '" + poland.getId().getValue() + "'; get;";
    String queryString2 = "match " + "(geo-entity: $x, entity-location: $y) isa is-located-in;" + "$y id '" + europe.getId().getValue() + "'; get;";
    List<Answer> answers = iqb.materialise(false).<GetQuery>parse(queryString).execute();
    answers.forEach(ans -> assertEquals(ans.size(), 2));
    answers.forEach(ans -> assertEquals(ans.get(var("y")).getId().getValue(), poland.getId().getValue()));
    assertEquals(answers.size(), 6);
    List<Answer> answers2 = iqb.materialise(false).<GetQuery>parse(queryString2).execute();
    answers2.forEach(ans -> assertEquals(ans.size(), 2));
    answers2.forEach(ans -> assertEquals(ans.get(var("y")).getId().getValue(), europe.getId().getValue()));
    assertEquals(answers2.size(), 21);
}
Also used : Concept(ai.grakn.concept.Concept) GraknTx(ai.grakn.GraknTx) Answer(ai.grakn.graql.admin.Answer) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Aggregations

Concept (ai.grakn.concept.Concept)91 Test (org.junit.Test)56 ConceptId (ai.grakn.concept.ConceptId)26 GraknTx (ai.grakn.GraknTx)25 Answer (ai.grakn.graql.admin.Answer)25 SchemaConcept (ai.grakn.concept.SchemaConcept)19 Label (ai.grakn.concept.Label)18 GrpcConcept (ai.grakn.rpc.generated.GrpcConcept)18 QueryBuilder (ai.grakn.graql.QueryBuilder)17 Var (ai.grakn.graql.Var)15 Set (java.util.Set)15 Role (ai.grakn.concept.Role)14 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)14 HashSet (java.util.HashSet)13 List (java.util.List)12 AttributeType (ai.grakn.concept.AttributeType)11 EntityType (ai.grakn.concept.EntityType)11 Schema (ai.grakn.util.Schema)10 Collectors (java.util.stream.Collectors)10 Stream (java.util.stream.Stream)10