Search in sources :

Example 61 with Entity

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

the class DegreeTest method testDegreeTernaryRelationships.

@Test
public void testDegreeTernaryRelationships() throws InvalidKBException {
    // make relationship
    Role productionWithCast = tx.putRole("production-with-cast");
    Role actor = tx.putRole("actor");
    Role characterBeingPlayed = tx.putRole("character-being-played");
    RelationshipType hasCast = tx.putRelationshipType("has-cast").relates(productionWithCast).relates(actor).relates(characterBeingPlayed);
    EntityType movie = tx.putEntityType("movie").plays(productionWithCast);
    EntityType person = tx.putEntityType("person").plays(actor);
    EntityType character = tx.putEntityType("character").plays(characterBeingPlayed);
    Entity godfather = movie.addEntity();
    Entity marlonBrando = person.addEntity();
    Entity donVitoCorleone = character.addEntity();
    hasCast.addRelationship().addRolePlayer(productionWithCast, godfather).addRolePlayer(actor, marlonBrando).addRolePlayer(characterBeingPlayed, donVitoCorleone);
    Map<Long, Set<String>> referenceDegrees = new HashMap<>();
    referenceDegrees.put(1L, Sets.newHashSet(godfather.getId().getValue(), marlonBrando.getId().getValue(), donVitoCorleone.getId().getValue()));
    tx.commit();
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        Map<Long, Set<String>> degrees = graph.graql().compute().centrality().usingDegree().execute();
        assertEquals(referenceDegrees, 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) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 62 with Entity

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

the class DegreeTest method testOneRolePlayerMultipleRoles.

@Test
public void testOneRolePlayerMultipleRoles() throws InvalidKBException {
    Role pet = tx.putRole("pet");
    Role owner = tx.putRole("owner");
    Role breeder = tx.putRole("breeder");
    RelationshipType mansBestFriend = tx.putRelationshipType("mans-best-friend").relates(pet).relates(owner).relates(breeder);
    EntityType person = tx.putEntityType("person").plays(owner).plays(breeder);
    EntityType animal = tx.putEntityType("animal").plays(pet);
    // make one person breeder and owner
    Entity coco = animal.addEntity();
    Entity dave = person.addEntity();
    mansBestFriend.addRelationship().addRolePlayer(pet, coco).addRolePlayer(owner, dave).addRolePlayer(breeder, dave);
    Map<Long, Set<String>> referenceDegrees = new HashMap<>();
    referenceDegrees.put(1L, Sets.newHashSet(coco.getId().getValue()));
    referenceDegrees.put(2L, Collections.singleton(dave.getId().getValue()));
    tx.commit();
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        Map<Long, Set<String>> degrees = graph.graql().compute().centrality().usingDegree().execute();
        assertEquals(referenceDegrees, 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) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 63 with Entity

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

the class DegreeTest method testDegreeTwoAttributes.

@Test
public void testDegreeTwoAttributes() throws InvalidKBException {
    // create a simple graph
    Role pet = tx.putRole("pet");
    Role owner = tx.putRole("owner");
    RelationshipType mansBestFriend = tx.putRelationshipType("mans-best-friend").relates(pet).relates(owner);
    EntityType person = tx.putEntityType("person").plays(owner);
    EntityType animal = tx.putEntityType("animal").plays(pet);
    AttributeType<String> name = tx.putAttributeType("name", AttributeType.DataType.STRING);
    AttributeType<String> altName = tx.putAttributeType("alternate-name", AttributeType.DataType.STRING);
    animal.attribute(name).attribute(altName);
    // add data to the graph
    Entity coco = animal.addEntity();
    Entity dave = person.addEntity();
    Attribute coconut = name.putAttribute("coconut");
    Attribute stinky = altName.putAttribute("stinky");
    mansBestFriend.addRelationship().addRolePlayer(owner, dave).addRolePlayer(pet, coco);
    coco.attribute(coconut).attribute(stinky);
    // manually compute the degree for small graph
    Map<Long, Set<String>> subgraphReferenceDegrees = new HashMap<>();
    subgraphReferenceDegrees.put(1L, Sets.newHashSet(coco.getId().getValue(), dave.getId().getValue()));
    // manually compute degree for almost full graph
    Map<Long, Set<String>> almostFullReferenceDegrees = new HashMap<>();
    almostFullReferenceDegrees.put(2L, Sets.newHashSet(coco.getId().getValue()));
    almostFullReferenceDegrees.put(1L, Sets.newHashSet(dave.getId().getValue(), coconut.getId().getValue()));
    // manually compute degrees
    Map<Long, Set<String>> fullReferenceDegrees = new HashMap<>();
    fullReferenceDegrees.put(3L, Sets.newHashSet(coco.getId().getValue()));
    fullReferenceDegrees.put(1L, Sets.newHashSet(dave.getId().getValue(), coconut.getId().getValue(), stinky.getId().getValue()));
    tx.commit();
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        // create a subgraph excluding attributes and their relationship
        HashSet<Label> subGraphTypes = Sets.newHashSet(Label.of("animal"), Label.of("person"), Label.of("mans-best-friend"));
        Map<Long, Set<String>> degrees = graph.graql().compute().centrality().usingDegree().in(subGraphTypes).execute();
        assertEquals(subgraphReferenceDegrees, degrees);
        // create a subgraph excluding one attribute type only
        HashSet<Label> almostFullTypes = Sets.newHashSet(Label.of("animal"), Label.of("person"), Label.of("mans-best-friend"), Label.of("@has-name"), Label.of("name"));
        degrees = graph.graql().compute().centrality().usingDegree().in(almostFullTypes).execute();
        assertEquals(almostFullReferenceDegrees, degrees);
        // full graph
        degrees = graph.graql().compute().centrality().usingDegree().of().execute();
        assertEquals(fullReferenceDegrees, degrees);
    }
}
Also used : Entity(ai.grakn.concept.Entity) HashSet(java.util.HashSet) Set(java.util.Set) Attribute(ai.grakn.concept.Attribute) HashMap(java.util.HashMap) RelationshipType(ai.grakn.concept.RelationshipType) Label(ai.grakn.concept.Label) Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) GraknTx(ai.grakn.GraknTx) Test(org.junit.Test)

Example 64 with Entity

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

the class DegreeTest method testDegreeMissingRolePlayer.

@Test
public void testDegreeMissingRolePlayer() {
    Role pet = tx.putRole("pet");
    Role owner = tx.putRole("owner");
    Role breeder = tx.putRole("breeder");
    RelationshipType mansBestFriend = tx.putRelationshipType("mans-best-friend").relates(pet).relates(owner).relates(breeder);
    EntityType person = tx.putEntityType("person").plays(owner).plays(breeder);
    EntityType animal = tx.putEntityType("animal").plays(pet);
    // make one person breeder and owner
    Entity coco = animal.addEntity();
    Entity dave = person.addEntity();
    mansBestFriend.addRelationship().addRolePlayer(pet, coco).addRolePlayer(owner, dave);
    // manual degrees
    Map<Long, Set<String>> referenceDegrees = new HashMap<>();
    referenceDegrees.put(1L, Sets.newHashSet(coco.getId().getValue(), dave.getId().getValue()));
    tx.commit();
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        Map<Long, Set<String>> degrees = graph.graql().compute().centrality().usingDegree().execute();
        assertEquals(referenceDegrees, 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) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 65 with Entity

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

the class KCoreTest 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<String, Set<String>> result = graph.graql().compute().cluster().usingKCore().kValue(2L).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

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