Search in sources :

Example 91 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class GrpcServerIT method whenDefiningASchema_TheSchemaIsDefined.

@Test
public void whenDefiningASchema_TheSchemaIsDefined() {
    try (GraknTx tx = remoteSession.open(GraknTxType.WRITE)) {
        EntityType animal = tx.putEntityType("animal");
        EntityType dog = tx.putEntityType("dog").sup(animal);
        EntityType cat = tx.putEntityType("cat");
        animal.sub(cat);
        cat.setLabel(Label.of("feline"));
        dog.setAbstract(true).setAbstract(false);
        cat.setAbstract(true);
        RelationshipType chases = tx.putRelationshipType("chases");
        Role chased = tx.putRole("chased");
        Role chaser = tx.putRole("chaser");
        chases.relates(chased).relates(chaser);
        Role pointlessRole = tx.putRole("pointless-role");
        tx.putRelationshipType("pointless").relates(pointlessRole);
        chases.relates(pointlessRole).deleteRelates(pointlessRole);
        dog.plays(chaser);
        cat.plays(chased);
        AttributeType<String> name = tx.putAttributeType("name", DataType.STRING);
        AttributeType<String> id = tx.putAttributeType("id", DataType.STRING).setRegex("(good|bad)-dog");
        AttributeType<Long> age = tx.putAttributeType("age", DataType.LONG);
        animal.attribute(name);
        animal.key(id);
        dog.attribute(age).deleteAttribute(age);
        cat.key(age).deleteKey(age);
        cat.plays(chaser).deletePlays(chaser);
        Entity dunstan = dog.addEntity();
        Attribute<String> dunstanId = id.putAttribute("good-dog");
        assertNotNull(dunstan.attributeRelationship(dunstanId));
        Attribute<String> dunstanName = name.putAttribute("Dunstan");
        dunstan.attribute(dunstanName).deleteAttribute(dunstanName);
        chases.addRelationship().addRolePlayer(chaser, dunstan);
        tx.commit();
    }
    try (GraknTx tx = localSession.open(GraknTxType.READ)) {
        EntityType animal = tx.getEntityType("animal");
        EntityType dog = tx.getEntityType("dog");
        EntityType cat = tx.getEntityType("feline");
        RelationshipType chases = tx.getRelationshipType("chases");
        Role chased = tx.getRole("chased");
        Role chaser = tx.getRole("chaser");
        AttributeType<String> name = tx.getAttributeType("name");
        AttributeType<String> id = tx.getAttributeType("id");
        Entity dunstan = Iterators.getOnlyElement(dog.instances().iterator());
        Relationship aChase = Iterators.getOnlyElement(chases.instances().iterator());
        assertEquals(animal, dog.sup());
        assertEquals(animal, cat.sup());
        assertEquals(ImmutableSet.of(chased, chaser), chases.relates().collect(toSet()));
        assertEquals(ImmutableSet.of(chaser), dog.plays().filter(role -> !role.isImplicit()).collect(toSet()));
        assertEquals(ImmutableSet.of(chased), cat.plays().filter(role -> !role.isImplicit()).collect(toSet()));
        assertEquals(ImmutableSet.of(name, id), animal.attributes().collect(toSet()));
        assertEquals(ImmutableSet.of(id), animal.keys().collect(toSet()));
        assertEquals(ImmutableSet.of(name, id), dog.attributes().collect(toSet()));
        assertEquals(ImmutableSet.of(id), dog.keys().collect(toSet()));
        assertEquals(ImmutableSet.of(name, id), cat.attributes().collect(toSet()));
        assertEquals(ImmutableSet.of(id), cat.keys().collect(toSet()));
        assertEquals("good-dog", Iterables.getOnlyElement(dunstan.keys(id).collect(toSet())).getValue());
        ImmutableMap<Role, ImmutableSet<?>> expectedRolePlayers = ImmutableMap.of(chaser, ImmutableSet.of(dunstan), chased, ImmutableSet.of());
        assertEquals(expectedRolePlayers, aChase.allRolePlayers());
        assertEquals("(good|bad)-dog", id.getRegex());
        assertFalse(dog.isAbstract());
        assertTrue(cat.isAbstract());
    }
}
Also used : Entity(ai.grakn.concept.Entity) RelationshipType(ai.grakn.concept.RelationshipType) EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) ImmutableSet(com.google.common.collect.ImmutableSet) Relationship(ai.grakn.concept.Relationship) Test(org.junit.Test)

Example 92 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class GrpcServerIT method whenGettingARule_TheInformationOnTheRuleIsCorrect.

@Test
public void whenGettingARule_TheInformationOnTheRuleIsCorrect() {
    try (GraknTx remoteTx = remoteSession.open(GraknTxType.READ);
        GraknTx localTx = localSession.open(GraknTxType.READ)) {
        GetQuery query = remoteTx.graql().match(var("x").label("expectation-rule")).get();
        ai.grakn.concept.Rule remoteConcept = query.stream().findAny().get().get("x").asRule();
        ai.grakn.concept.Rule localConcept = localTx.getConcept(remoteConcept.getId()).asRule();
        assertEquals(localConcept.getWhen(), remoteConcept.getWhen());
        assertEquals(localConcept.getThen(), remoteConcept.getThen());
    }
}
Also used : GraknTx(ai.grakn.GraknTx) GetQuery(ai.grakn.graql.GetQuery) Test(org.junit.Test)

Example 93 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class GrpcServerIT method whenExecutingAndCommittingAQuery_TheQueryIsCommitted.

@Test
public void whenExecutingAndCommittingAQuery_TheQueryIsCommitted() throws InterruptedException {
    try (GraknTx tx = remoteSession.open(GraknTxType.WRITE)) {
        tx.graql().define(label("person").sub("entity")).execute();
        tx.commit();
    }
    try (GraknTx tx = localSession.open(GraknTxType.READ)) {
        assertNotNull(tx.getEntityType("person"));
    }
}
Also used : GraknTx(ai.grakn.GraknTx) Test(org.junit.Test)

Example 94 with GraknTx

use of ai.grakn.GraknTx 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 95 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class XMLMigratorTest method assertThingHasName.

private static void assertThingHasName(String name) {
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        EntityType thingType = graph.getEntityType("thingy");
        AttributeType nameType = graph.getAttributeType("name");
        assertEquals(1, thingType.instances().count());
        thingType.instances().forEach(thing -> {
            assertEquals(1, thing.attributes(nameType).count());
            assertEquals(name, thing.attributes(nameType).iterator().next().getValue());
        });
    }
}
Also used : EntityType(ai.grakn.concept.EntityType) GraknTx(ai.grakn.GraknTx) AttributeType(ai.grakn.concept.AttributeType)

Aggregations

GraknTx (ai.grakn.GraknTx)243 Test (org.junit.Test)189 EntityType (ai.grakn.concept.EntityType)54 GetQuery (ai.grakn.graql.GetQuery)52 Entity (ai.grakn.concept.Entity)51 QueryBuilder (ai.grakn.graql.QueryBuilder)49 Role (ai.grakn.concept.Role)48 RelationshipType (ai.grakn.concept.RelationshipType)46 Answer (ai.grakn.graql.admin.Answer)44 Set (java.util.Set)44 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)33 Label (ai.grakn.concept.Label)28 Attribute (ai.grakn.concept.Attribute)27 Concept (ai.grakn.concept.Concept)27 HashSet (java.util.HashSet)26 GraknSession (ai.grakn.GraknSession)22 AttributeType (ai.grakn.concept.AttributeType)22 ConceptId (ai.grakn.concept.ConceptId)22 List (java.util.List)19 GraknTxType (ai.grakn.GraknTxType)17