Search in sources :

Example 26 with Entity

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

the class CorenessTest method testDisconnectedCores.

@Test
public void testDisconnectedCores() {
    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 entity0 = entityType1.addEntity();
        Entity entity1 = entityType1.addEntity();
        Entity entity2 = entityType1.addEntity();
        Entity entity3 = entityType1.addEntity();
        Entity entity4 = entityType1.addEntity();
        Entity entity5 = entityType1.addEntity();
        Entity entity6 = entityType1.addEntity();
        Entity entity7 = entityType1.addEntity();
        Entity entity8 = entityType1.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, entity1).addRolePlayer(role2, entity3);
        relationshipType1.addRelationship().addRolePlayer(role1, entity1).addRolePlayer(role2, entity4);
        relationshipType1.addRelationship().addRolePlayer(role1, entity2).addRolePlayer(role2, entity4);
        relationshipType1.addRelationship().addRolePlayer(role1, entity5).addRolePlayer(role2, entity6);
        relationshipType2.addRelationship().addRolePlayer(role3, entity5).addRolePlayer(role4, entity7);
        relationshipType2.addRelationship().addRolePlayer(role3, entity5).addRolePlayer(role4, entity8);
        relationshipType2.addRelationship().addRolePlayer(role3, entity6).addRolePlayer(role4, entity7);
        relationshipType2.addRelationship().addRolePlayer(role3, entity6).addRolePlayer(role4, entity8);
        relationshipType2.addRelationship().addRolePlayer(role3, entity7).addRolePlayer(role4, entity8);
        relationshipType1.addRelationship().addRolePlayer(role1, entity0).addRolePlayer(role2, entity1);
        relationshipType1.addRelationship().addRolePlayer(role1, entity0).addRolePlayer(role2, entity8);
        graph.commit();
    }
    Map<Long, Set<String>> result;
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        result = graph.graql().compute().centrality().usingKCore().execute();
        assertEquals(2, result.size());
        assertEquals(8, result.get(3L).size());
        assertEquals(1, result.get(2L).size());
        result = graph.graql().compute().centrality().usingKCore().minK(3L).execute();
        assertEquals(1, result.size());
        assertEquals(8, result.get(3L).size());
    }
}
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) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 27 with Entity

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

the class DegreeTest method testSubIsAccountedForInSubgraph.

@Test
public void testSubIsAccountedForInSubgraph() {
    Role pet = tx.putRole("pet");
    Role owner = tx.putRole("owner");
    Entity person = tx.putEntityType("person").plays(owner).addEntity();
    EntityType animal = tx.putEntityType("animal").plays(pet);
    Entity dog = tx.putEntityType("dog").sup(animal).addEntity();
    tx.putRelationshipType("mans-best-friend").relates(pet).relates(owner).addRelationship().addRolePlayer(pet, dog).addRolePlayer(owner, person);
    Map<Long, Set<String>> correctDegrees = new HashMap<>();
    correctDegrees.put(1L, Sets.newHashSet(person.getId().getValue(), dog.getId().getValue()));
    tx.commit();
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        // set subgraph, use animal instead of dog
        Set<Label> ct = Sets.newHashSet(Label.of("person"), Label.of("animal"), Label.of("mans-best-friend"));
        Map<Long, Set<String>> degrees = graph.graql().compute().centrality().usingDegree().in(ct).execute();
        // check that dog has a degree to confirm sub has been inferred
        assertEquals(correctDegrees, degrees);
    }
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) Entity(ai.grakn.concept.Entity) GraknTx(ai.grakn.GraknTx) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Label(ai.grakn.concept.Label) Test(org.junit.Test)

Example 28 with Entity

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

the class KCoreTest method testDisconnectedCores.

@Test
public void testDisconnectedCores() {
    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 entity0 = entityType1.addEntity();
        Entity entity1 = entityType1.addEntity();
        Entity entity2 = entityType1.addEntity();
        Entity entity3 = entityType1.addEntity();
        Entity entity4 = entityType1.addEntity();
        Entity entity5 = entityType1.addEntity();
        Entity entity6 = entityType1.addEntity();
        Entity entity7 = entityType1.addEntity();
        Entity entity8 = entityType1.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, entity1).addRolePlayer(role2, entity3);
        relationshipType1.addRelationship().addRolePlayer(role1, entity1).addRolePlayer(role2, entity4);
        relationshipType1.addRelationship().addRolePlayer(role1, entity2).addRolePlayer(role2, entity4);
        relationshipType1.addRelationship().addRolePlayer(role1, entity5).addRolePlayer(role2, entity6);
        relationshipType2.addRelationship().addRolePlayer(role3, entity5).addRolePlayer(role4, entity7);
        relationshipType2.addRelationship().addRolePlayer(role3, entity5).addRolePlayer(role4, entity8);
        relationshipType2.addRelationship().addRolePlayer(role3, entity6).addRolePlayer(role4, entity7);
        relationshipType2.addRelationship().addRolePlayer(role3, entity6).addRolePlayer(role4, entity8);
        relationshipType2.addRelationship().addRolePlayer(role3, entity7).addRolePlayer(role4, entity8);
        relationshipType1.addRelationship().addRolePlayer(role1, entity0).addRolePlayer(role2, entity1);
        relationshipType1.addRelationship().addRolePlayer(role1, entity0).addRolePlayer(role2, entity8);
        graph.commit();
    }
    Map<String, Set<String>> result;
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        result = graph.graql().compute().cluster().usingKCore().kValue(3L).execute();
        assertEquals(2, result.size());
        assertEquals(4, result.values().iterator().next().size());
        System.out.println("result = " + result);
        result = graph.graql().compute().cluster().usingKCore().kValue(2L).execute();
        assertEquals(1, result.size());
        assertEquals(9, result.values().iterator().next().size());
    }
}
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) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 29 with Entity

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

the class CSVMigratorTest method whenLinesIsSetToN_OnlyFirstNLinesAreProcessed.

@Test
public void whenLinesIsSetToN_OnlyFirstNLinesAreProcessed() {
    load(factory, getFile("csv", "pets/schema.gql"));
    String template = getFileAsString("csv", "pets/template.gql");
    declareAndLoad(template, "pets/data/pets.quotes", new MigratorBuilder().setUri(engine.uri()).setKeyspace(keyspace).setRetries(0).setFailFast(false).setLines(3).build());
    try (GraknTx graph = factory.open(GraknTxType.WRITE)) {
        // Re Open Transaction
        Collection<Entity> pets = graph.getEntityType("pet").instances().collect(Collectors.toSet());
        TestCase.assertEquals(3, pets.size());
        Collection<Entity> cats = graph.getEntityType("cat").instances().collect(Collectors.toSet());
        TestCase.assertEquals(2, cats.size());
        Collection<Entity> dogs = graph.getEntityType("dog").instances().collect(Collectors.toSet());
        TestCase.assertEquals(0, dogs.size());
    }
}
Also used : GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) MigratorBuilder(ai.grakn.migration.base.MigratorBuilder) MigratorTestUtils.getFileAsString(ai.grakn.test.migration.MigratorTestUtils.getFileAsString) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 30 with Entity

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

the class JsonMigratorMainTest method runAndAssertDataCorrect.

private void runAndAssertDataCorrect(String... args) {
    run(args);
    try (GraknTx graph = session.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());
        Collection<Thing> phoneNumbers = getProperties(graph, person, "has-phone");
        assertEquals(2, phoneNumbers.size());
    }
}
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)

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