Search in sources :

Example 46 with Entity

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

the class JsonMigratorTest method whenMigratorExecutedOverJsonDirectory_AllDataIsPersistedInGraph.

@Test
public void whenMigratorExecutedOverJsonDirectory_AllDataIsPersistedInGraph() {
    load(factory, getFile("json", "string-or-object/schema.gql"));
    String template = "\n" + "insert $thing isa the-thing\n" + "        has a-string if (<the-thing.a-string> != null) do {<the-thing.a-string>}\n" + "        else {<the-thing>} ;";
    declareAndLoad(template, "string-or-object/data");
    try (GraknTx graph = factory.open(GraknTxType.READ)) {
        EntityType theThing = graph.getEntityType("the-thing");
        assertEquals(2, theThing.instances().count());
        Stream<Entity> things = theThing.instances();
        boolean thingsCorrect = things.allMatch(thing -> {
            Object string = getResource(graph, thing, Label.of("a-string")).getValue();
            return string.equals("hello") || string.equals("goodbye");
        });
        assertTrue(thingsCorrect);
    }
}
Also used : EntityType(ai.grakn.concept.EntityType) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) Test(org.junit.Test)

Example 47 with Entity

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

the class GrpcServerIT method whenGettingAnEntity_TheInformationOnTheEntityIsCorrect.

@Test
public void whenGettingAnEntity_TheInformationOnTheEntityIsCorrect() {
    try (GraknTx remoteTx = remoteSession.open(GraknTxType.READ);
        GraknTx localTx = localSession.open(GraknTxType.READ)) {
        GetQuery query = remoteTx.graql().match(var("x").isa("movie")).get();
        Entity remoteConcept = query.stream().findAny().get().get("x").asEntity();
        Entity localConcept = localTx.getConcept(remoteConcept.getId()).asEntity();
        // There actually aren't any new methods on Entity, but we should still check we can get them
        assertEquals(localConcept.getId(), remoteConcept.getId());
    }
}
Also used : GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) GetQuery(ai.grakn.graql.GetQuery) Test(org.junit.Test)

Example 48 with Entity

use of ai.grakn.concept.Entity 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 49 with Entity

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

the class InsertQueryTest method whenAddingAnAttributeRelationshipWithProvenance_TheAttributeAndProvenanceAreAdded.

@Test
public void whenAddingAnAttributeRelationshipWithProvenance_TheAttributeAndProvenanceAreAdded() {
    InsertQuery query = qb.insert(y.has("provenance", z.val("Someone told me")), w.isa("movie").has(title, x.val("My Movie"), y));
    Answer answer = Iterables.getOnlyElement(query.execute());
    Entity movie = answer.get(w).asEntity();
    Attribute<String> theTitle = answer.get(x).asAttribute();
    Relationship hasTitle = answer.get(y).asRelationship();
    Attribute<String> provenance = answer.get(z).asAttribute();
    assertThat(hasTitle.rolePlayers().toArray(), arrayContainingInAnyOrder(movie, theTitle));
    assertThat(hasTitle.attributes().toArray(), arrayContaining(provenance));
}
Also used : InsertQuery(ai.grakn.graql.InsertQuery) Answer(ai.grakn.graql.admin.Answer) Entity(ai.grakn.concept.Entity) Relationship(ai.grakn.concept.Relationship) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 50 with Entity

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

the class MatchTest method whenExecutingGraqlTraversalFromGraph_ReturnExpectedResults.

@Test
public void whenExecutingGraqlTraversalFromGraph_ReturnExpectedResults() {
    EntityType type = movieKB.tx().putEntityType("Concept Type");
    Entity entity = type.addEntity();
    Collection<Concept> results = movieKB.tx().graql().match(x.isa(type.getLabel().getValue())).stream().findAny().get().concepts();
    assertThat(results, containsInAnyOrder(entity));
}
Also used : EntityType(ai.grakn.concept.EntityType) Concept(ai.grakn.concept.Concept) MatchableConcept(ai.grakn.matcher.MatchableConcept) SchemaConcept(ai.grakn.concept.SchemaConcept) Entity(ai.grakn.concept.Entity) Test(org.junit.Test)

Aggregations

Entity (ai.grakn.concept.Entity)99 Test (org.junit.Test)81 EntityType (ai.grakn.concept.EntityType)74 Role (ai.grakn.concept.Role)53 RelationshipType (ai.grakn.concept.RelationshipType)50 GraknTx (ai.grakn.GraknTx)44 Attribute (ai.grakn.concept.Attribute)18 Set (java.util.Set)16 Relationship (ai.grakn.concept.Relationship)14 Label (ai.grakn.concept.Label)11 ConceptId (ai.grakn.concept.ConceptId)10 HashSet (java.util.HashSet)10 ArrayList (java.util.ArrayList)8 AttributeType (ai.grakn.concept.AttributeType)7 List (java.util.List)7 GraknSession (ai.grakn.GraknSession)6 Before (org.junit.Before)6 GraknTxType (ai.grakn.GraknTxType)5 Concept (ai.grakn.concept.Concept)5 GraqlQueryException (ai.grakn.exception.GraqlQueryException)5