Search in sources :

Example 96 with EntityType

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

the class JsonMigratorTest method whenMigratorExecutedOverDataWithAllDataTypes_AllDataIsPersistedInGraph.

@Test
public void whenMigratorExecutedOverDataWithAllDataTypes_AllDataIsPersistedInGraph() throws FileNotFoundException {
    load(factory, getFile("json", "all-types/schema.gql"));
    String template = "" + "insert $x isa thingy\n" + "  has a-boolean <a-boolean>\n" + "  has a-number  <a-number>\n" + "  for (int in <array-of-ints> ) do {\n" + "  has a-int <int>\n" + "  }\n" + "  has a-string <a-string>\n" + "  if (<a-null> != null) do {has a-null <a-null>};";
    declareAndLoad(template, "all-types/data.json");
    try (GraknTx graph = factory.open(GraknTxType.READ)) {
        EntityType rootType = graph.getEntityType("thingy");
        Set<Entity> things = rootType.instances().collect(toSet());
        assertEquals(1, things.size());
        Entity thing = things.iterator().next();
        Collection<Object> integers = getResources(graph, thing, Label.of("a-int")).map(Attribute::getValue).collect(toSet());
        assertEquals(Sets.newHashSet(1L, 2L, 3L), integers);
        Attribute aBoolean = getResource(graph, thing, Label.of("a-boolean"));
        assertEquals(true, aBoolean.getValue());
        Attribute aNumber = getResource(graph, thing, Label.of("a-number"));
        assertEquals(42.1, aNumber.getValue());
        Attribute aString = getResource(graph, thing, Label.of("a-string"));
        assertEquals("hi", aString.getValue());
        assertEquals(0, graph.getAttributeType("a-null").instances().count());
    }
}
Also used : EntityType(ai.grakn.concept.EntityType) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) Attribute(ai.grakn.concept.Attribute) Test(org.junit.Test)

Example 97 with EntityType

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

the class JsonMigratorTest method whenMigratorExecutedOverSimpleJson_DataIsPersistedInGraph.

@Test
public void whenMigratorExecutedOverSimpleJson_DataIsPersistedInGraph() {
    load(factory, getFile("json", "simple-schema/schema.gql"));
    String template = "  \n" + "insert " + "$person isa person;\n" + " \n" + "$address isa address\n" + "  has city <address.city>;\n" + "\n" + "$street isa street-address\n" + "   has street <address.streetAddress.street>\n" + "   has number <address.streetAddress.number>;\n" + "\n" + "(address-with-street: $address, street-of-address: $street) isa address-has-street;\n" + "\n" + "(person-with-address: $person, address-of-person: $address) isa has-address;\n" + "\n" + "for ( <phoneNumber> ) do {\n" + "  $phone isa phone-number\n" + "    has location <location>\n" + "    has code <code>;\n" + "  \n" + "  (person-with-phone: $person, phone-of-person: $phone) isa has-phone;\n" + "  \n" + "} ";
    declareAndLoad(template, "simple-schema/data.json");
    try (GraknTx graph = factory.open(GraknTxType.READ)) {
        EntityType personType = graph.getEntityType("person");
        assertEquals(1, personType.instances().count());
        Entity person = personType.instances().iterator().next();
        Entity address = getProperty(graph, person, "has-address").asEntity();
        Entity streetAddress = getProperty(graph, address, "address-has-street").asEntity();
        Attribute number = getResource(graph, streetAddress, Label.of("number"));
        assertEquals(21L, number.getValue());
        Attribute street = getResource(graph, streetAddress, Label.of("street"));
        assertEquals("2nd Street", street.getValue());
        Attribute city = getResource(graph, address, Label.of("city")).asAttribute();
        assertEquals("New York", city.getValue());
        Collection<Thing> phoneNumbers = getProperties(graph, person, "has-phone");
        assertEquals(2, phoneNumbers.size());
        boolean phoneNumbersCorrect = phoneNumbers.stream().allMatch(phoneNumber -> {
            Object location = getResource(graph, phoneNumber, Label.of("location")).getValue();
            Object code = getResource(graph, phoneNumber, Label.of("code")).getValue();
            return ((location.equals("home") && code.equals(44L)) || (location.equals("work") && code.equals(45L)));
        });
        assertTrue(phoneNumbersCorrect);
    }
}
Also used : EntityType(ai.grakn.concept.EntityType) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) Attribute(ai.grakn.concept.Attribute) Thing(ai.grakn.concept.Thing) Test(org.junit.Test)

Example 98 with EntityType

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

the class JsonMigratorTest method whenMigratorExecutedWithConditionalTemplate_DataIsPersistedInGraph.

@Test
public void whenMigratorExecutedWithConditionalTemplate_DataIsPersistedInGraph() {
    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());
    }
}
Also used : EntityType(ai.grakn.concept.EntityType) GraknTx(ai.grakn.GraknTx) Test(org.junit.Test)

Example 99 with EntityType

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

the class CorenessTest method addSchemaAndEntities.

private void addSchemaAndEntities() throws InvalidKBException {
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        EntityType entityType1 = graph.putEntityType(thing);
        EntityType entityType2 = graph.putEntityType(anotherThing);
        Role role1 = graph.putRole("role1");
        Role role2 = graph.putRole("role2");
        RelationshipType relationshipType1 = graph.putRelationshipType(related).relates(role1).relates(role2);
        Role role3 = graph.putRole("role3");
        Role role4 = graph.putRole("role4");
        RelationshipType relationshipType2 = graph.putRelationshipType(veryRelated).relates(role3).relates(role4);
        entityType1.plays(role1).plays(role2).plays(role3).plays(role4);
        entityType2.plays(role1).plays(role2).plays(role3).plays(role4);
        Entity entity1 = entityType1.addEntity();
        Entity entity2 = entityType1.addEntity();
        Entity entity3 = entityType2.addEntity();
        Entity entity4 = entityType2.addEntity();
        relationshipType1.addRelationship().addRolePlayer(role1, entity1).addRolePlayer(role2, entity2);
        relationshipType1.addRelationship().addRolePlayer(role1, entity2).addRolePlayer(role2, entity3);
        relationshipType1.addRelationship().addRolePlayer(role1, entity3).addRolePlayer(role2, entity4);
        relationshipType1.addRelationship().addRolePlayer(role1, entity4).addRolePlayer(role2, entity1);
        relationshipType2.addRelationship().addRolePlayer(role3, entity1).addRolePlayer(role4, entity3);
        relationshipType2.addRelationship().addRolePlayer(role3, entity2).addRolePlayer(role4, entity4);
        entityId1 = entity1.getId();
        entityId2 = entity2.getId();
        entityId3 = entity3.getId();
        entityId4 = entity4.getId();
        graph.commit();
    }
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) RelationshipType(ai.grakn.concept.RelationshipType)

Example 100 with EntityType

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

the class CorenessTest method testOnGraphWithTwoEntitiesAndTwoRelationships.

@Test
public void testOnGraphWithTwoEntitiesAndTwoRelationships() {
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        EntityType entityType = graph.putEntityType(thing);
        Entity entity1 = entityType.addEntity();
        Entity entity2 = entityType.addEntity();
        Role role1 = graph.putRole("role1");
        Role role2 = graph.putRole("role2");
        entityType.plays(role1).plays(role2);
        graph.putRelationshipType(related).relates(role1).relates(role2).addRelationship().addRolePlayer(role1, entity1).addRolePlayer(role2, entity2);
        Role role3 = graph.putRole("role3");
        Role role4 = graph.putRole("role4");
        entityType.plays(role3).plays(role4);
        graph.putRelationshipType(veryRelated).relates(role3).relates(role4).addRelationship().addRolePlayer(role3, entity1).addRolePlayer(role4, entity2);
        Map<Long, Set<String>> result = graph.graql().compute().centrality().usingKCore().execute();
        assertTrue(result.isEmpty());
    }
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) Set(java.util.Set) Test(org.junit.Test)

Aggregations

EntityType (ai.grakn.concept.EntityType)168 Test (org.junit.Test)141 Role (ai.grakn.concept.Role)83 RelationshipType (ai.grakn.concept.RelationshipType)82 Entity (ai.grakn.concept.Entity)74 GraknTx (ai.grakn.GraknTx)45 Relationship (ai.grakn.concept.Relationship)21 Attribute (ai.grakn.concept.Attribute)19 AttributeType (ai.grakn.concept.AttributeType)17 ConceptId (ai.grakn.concept.ConceptId)17 Label (ai.grakn.concept.Label)17 Set (java.util.Set)16 HashSet (java.util.HashSet)15 Thing (ai.grakn.concept.Thing)14 GraknTxType (ai.grakn.GraknTxType)8 ArrayList (java.util.ArrayList)8 GraknSession (ai.grakn.GraknSession)7 Concept (ai.grakn.concept.Concept)7 HashMap (java.util.HashMap)7 Before (org.junit.Before)7