Search in sources :

Example 26 with Role

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

the class AnalyticsTest method testNullResourceDoesNotBreakAnalytics.

@Test
public void testNullResourceDoesNotBreakAnalytics() throws InvalidKBException {
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        // make slightly odd graph
        Label resourceTypeId = Label.of("degree");
        EntityType thingy = graph.putEntityType("thingy");
        AttributeType<Long> attribute = graph.putAttributeType(resourceTypeId, AttributeType.DataType.LONG);
        thingy.attribute(attribute);
        Role degreeOwner = graph.getRole(Schema.ImplicitType.HAS_OWNER.getLabel(resourceTypeId).getValue());
        Role degreeValue = graph.getRole(Schema.ImplicitType.HAS_VALUE.getLabel(resourceTypeId).getValue());
        RelationshipType relationshipType = graph.putRelationshipType(Schema.ImplicitType.HAS.getLabel(resourceTypeId)).relates(degreeOwner).relates(degreeValue);
        thingy.plays(degreeOwner);
        Entity thisThing = thingy.addEntity();
        relationshipType.addRelationship().addRolePlayer(degreeOwner, thisThing);
        graph.commit();
    }
    // the null role-player caused analytics to fail at some stage
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        graph.graql().compute().centrality().usingDegree().execute();
    } catch (RuntimeException e) {
        e.printStackTrace();
        fail();
    }
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 27 with Role

use of ai.grakn.concept.Role 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 28 with Role

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

the class DegreeTest method testDegreesSimple.

@Test
public void testDegreesSimple() {
    // create instances
    EntityType thingy = tx.putEntityType("thingy");
    EntityType anotherThing = tx.putEntityType("another");
    ConceptId entity1 = thingy.addEntity().getId();
    ConceptId entity2 = thingy.addEntity().getId();
    ConceptId entity3 = thingy.addEntity().getId();
    ConceptId entity4 = anotherThing.addEntity().getId();
    Role role1 = tx.putRole("role1");
    Role role2 = tx.putRole("role2");
    thingy.plays(role1).plays(role2);
    anotherThing.plays(role1).plays(role2);
    RelationshipType related = tx.putRelationshipType("related").relates(role1).relates(role2);
    // relate them
    related.addRelationship().addRolePlayer(role1, tx.getConcept(entity1)).addRolePlayer(role2, tx.getConcept(entity2));
    related.addRelationship().addRolePlayer(role1, tx.getConcept(entity2)).addRolePlayer(role2, tx.getConcept(entity3));
    related.addRelationship().addRolePlayer(role1, tx.getConcept(entity2)).addRolePlayer(role2, tx.getConcept(entity4));
    tx.commit();
    tx = session.open(GraknTxType.READ);
    Map<ConceptId, Long> correctDegrees = new HashMap<>();
    correctDegrees.put(entity1, 1L);
    correctDegrees.put(entity2, 3L);
    correctDegrees.put(entity3, 1L);
    correctDegrees.put(entity4, 1L);
    // compute degrees
    List<Long> list = new ArrayList<>(4);
    long workerNumber = 4L;
    if (GraknTestUtil.usingTinker())
        workerNumber = 1L;
    for (long i = 0L; i < workerNumber; i++) {
        list.add(i);
    }
    tx.close();
    Set<Map<Long, Set<String>>> result = list.parallelStream().map(i -> {
        try (GraknTx graph = session.open(GraknTxType.READ)) {
            return graph.graql().compute().centrality().usingDegree().execute();
        }
    }).collect(Collectors.toSet());
    assertEquals(1, result.size());
    Map<Long, Set<String>> degrees0 = result.iterator().next();
    assertEquals(2, degrees0.size());
    degrees0.forEach((key, value) -> value.forEach(id -> {
        assertTrue(correctDegrees.containsKey(ConceptId.of(id)));
        assertEquals(correctDegrees.get(ConceptId.of(id)), key);
    }));
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        Map<Long, Set<String>> degrees1 = graph.graql().compute().centrality().usingDegree().of("thingy").execute();
        assertEquals(2, degrees1.size());
        assertEquals(2, degrees1.get(1L).size());
        assertEquals(1, degrees1.get(3L).size());
        degrees1.forEach((key, value) -> value.forEach(id -> {
            assertTrue(correctDegrees.containsKey(ConceptId.of(id)));
            assertEquals(correctDegrees.get(ConceptId.of(id)), key);
        }));
        Map<Long, Set<String>> degrees2 = graph.graql().compute().centrality().usingDegree().of("thingy", "related").execute();
        assertEquals(degrees1, degrees2);
        degrees2 = graph.graql().compute().centrality().usingDegree().of().execute();
        assertEquals(degrees0, degrees2);
        // compute degrees on subgraph
        Map<Long, Set<String>> degrees3 = graph.graql().compute().centrality().usingDegree().in("thingy", "related").execute();
        assertEquals(degrees1, degrees3);
        degrees3 = graph.graql().compute().centrality().usingDegree().of("thingy").in("related").execute();
        assertEquals(degrees1, degrees3);
    }
}
Also used : InvalidKBException(ai.grakn.exception.InvalidKBException) GraknTestUtil(ai.grakn.util.GraknTestUtil) Role(ai.grakn.concept.Role) Entity(ai.grakn.concept.Entity) HashMap(java.util.HashMap) EntityType(ai.grakn.concept.EntityType) ArrayList(java.util.ArrayList) Attribute(ai.grakn.concept.Attribute) SessionContext(ai.grakn.test.rule.SessionContext) HashSet(java.util.HashSet) Label(ai.grakn.concept.Label) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) GraknTx(ai.grakn.GraknTx) Map(java.util.Map) ConceptId(ai.grakn.concept.ConceptId) Relationship(ai.grakn.concept.Relationship) ClassRule(org.junit.ClassRule) Before(org.junit.Before) GraknTxType(ai.grakn.GraknTxType) GraknSession(ai.grakn.GraknSession) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) List(java.util.List) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) RelationshipType(ai.grakn.concept.RelationshipType) ArrayList(java.util.ArrayList) ConceptId(ai.grakn.concept.ConceptId) EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 29 with Role

use of ai.grakn.concept.Role 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 30 with Role

use of ai.grakn.concept.Role 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)

Aggregations

Role (ai.grakn.concept.Role)189 Test (org.junit.Test)124 RelationshipType (ai.grakn.concept.RelationshipType)114 EntityType (ai.grakn.concept.EntityType)92 Entity (ai.grakn.concept.Entity)55 GraknTx (ai.grakn.GraknTx)48 Relationship (ai.grakn.concept.Relationship)37 Set (java.util.Set)36 ConceptId (ai.grakn.concept.ConceptId)33 Label (ai.grakn.concept.Label)33 Thing (ai.grakn.concept.Thing)32 HashSet (java.util.HashSet)31 Var (ai.grakn.graql.Var)25 AttributeType (ai.grakn.concept.AttributeType)21 Concept (ai.grakn.concept.Concept)20 Schema (ai.grakn.util.Schema)17 Collectors (java.util.stream.Collectors)17 Attribute (ai.grakn.concept.Attribute)16 SchemaConcept (ai.grakn.concept.SchemaConcept)16 Type (ai.grakn.concept.Type)16