Search in sources :

Example 66 with RelationshipType

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

the class DegreeTest method testRelationshipPlaysARole.

@Test
public void testRelationshipPlaysARole() throws InvalidKBException {
    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);
    Role ownership = tx.putRole("ownership");
    Role ownershipResource = tx.putRole("ownership-resource");
    RelationshipType hasOwnershipResource = tx.putRelationshipType("has-ownership-resource").relates(ownership).relates(ownershipResource);
    AttributeType<String> startDate = tx.putAttributeType("start-date", AttributeType.DataType.STRING);
    startDate.plays(ownershipResource);
    mansBestFriend.plays(ownership);
    // add instances
    Entity coco = animal.addEntity();
    Entity dave = person.addEntity();
    Relationship daveOwnsCoco = mansBestFriend.addRelationship().addRolePlayer(owner, dave).addRolePlayer(pet, coco);
    Attribute aStartDate = startDate.putAttribute("01/01/01");
    hasOwnershipResource.addRelationship().addRolePlayer(ownershipResource, aStartDate).addRolePlayer(ownership, daveOwnsCoco);
    Map<Long, Set<String>> referenceDegrees = new HashMap<>();
    referenceDegrees.put(1L, Sets.newHashSet(coco.getId().getValue(), dave.getId().getValue(), aStartDate.getId().getValue(), daveOwnsCoco.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 : 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) Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) GraknTx(ai.grakn.GraknTx) Relationship(ai.grakn.concept.Relationship) Test(org.junit.Test)

Example 67 with RelationshipType

use of ai.grakn.concept.RelationshipType 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 68 with RelationshipType

use of ai.grakn.concept.RelationshipType 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 69 with RelationshipType

use of ai.grakn.concept.RelationshipType 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 70 with RelationshipType

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

Aggregations

RelationshipType (ai.grakn.concept.RelationshipType)127 Role (ai.grakn.concept.Role)105 Test (org.junit.Test)91 EntityType (ai.grakn.concept.EntityType)80 Entity (ai.grakn.concept.Entity)52 GraknTx (ai.grakn.GraknTx)39 Relationship (ai.grakn.concept.Relationship)25 ConceptId (ai.grakn.concept.ConceptId)23 Label (ai.grakn.concept.Label)21 HashSet (java.util.HashSet)20 Set (java.util.Set)20 AttributeType (ai.grakn.concept.AttributeType)17 Thing (ai.grakn.concept.Thing)17 Attribute (ai.grakn.concept.Attribute)16 Schema (ai.grakn.util.Schema)12 Collectors (java.util.stream.Collectors)12 List (java.util.List)11 GraknSession (ai.grakn.GraknSession)10 GraknTxType (ai.grakn.GraknTxType)10 Concept (ai.grakn.concept.Concept)10