Search in sources :

Example 16 with Attribute

use of ai.grakn.concept.Attribute 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 17 with Attribute

use of ai.grakn.concept.Attribute 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 18 with Attribute

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

the class KCoreTest method testImplicitTypeShouldBeIncluded.

@Test
public void testImplicitTypeShouldBeIncluded() {
    addSchemaAndEntities();
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        String aResourceTypeLabel = "aResourceTypeLabel";
        AttributeType<String> attributeType = graph.putAttributeType(aResourceTypeLabel, AttributeType.DataType.STRING);
        graph.getEntityType(thing).attribute(attributeType);
        Attribute Attribute1 = attributeType.putAttribute("blah");
        graph.getConcept(entityId1).asEntity().attribute(Attribute1);
        graph.getConcept(entityId2).asEntity().attribute(Attribute1);
        graph.getConcept(entityId3).asEntity().attribute(Attribute1);
        graph.getConcept(entityId4).asEntity().attribute(Attribute1);
        Attribute Attribute2 = attributeType.putAttribute("bah");
        graph.getConcept(entityId1).asEntity().attribute(Attribute2);
        graph.getConcept(entityId2).asEntity().attribute(Attribute2);
        graph.getConcept(entityId3).asEntity().attribute(Attribute2);
        graph.commit();
    }
    Map<String, Set<String>> result;
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        result = graph.graql().compute().cluster().usingKCore().includeAttribute().kValue(4L).execute();
        System.out.println("result = " + result);
        assertEquals(1, result.size());
        assertEquals(5, result.values().iterator().next().size());
        result = graph.graql().compute().cluster().usingKCore().kValue(3L).includeAttribute().execute();
        System.out.println("result = " + result);
        assertEquals(1, result.size());
        assertEquals(6, result.values().iterator().next().size());
    }
}
Also used : GraknTx(ai.grakn.GraknTx) Set(java.util.Set) Attribute(ai.grakn.concept.Attribute) Test(org.junit.Test)

Example 19 with Attribute

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

the class CountTest method testHasResourceEdges.

@Test
public void testHasResourceEdges() {
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        EntityType person = graph.putEntityType("person");
        AttributeType<String> name = graph.putAttributeType("name", AttributeType.DataType.STRING);
        person.attribute(name);
        Entity aPerson = person.addEntity();
        aPerson.attribute(name.putAttribute("jason"));
        graph.commit();
    }
    long count;
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        count = graph.graql().compute().count().execute();
        assertEquals(1L, count);
        count = graph.graql().compute().count().includeAttribute().execute();
        assertEquals(3L, count);
        count = graph.graql().compute().count().in("name").execute();
        assertEquals(1L, count);
        count = graph.graql().compute().count().in("@has-name").execute();
        assertEquals(1L, count);
        count = graph.graql().compute().count().in("@has-name", "thing").execute();
        assertEquals(3L, count);
        count = graph.graql().compute().count().in("@has-name", "name").execute();
        assertEquals(2L, count);
        count = graph.graql().compute().count().in("relationship").execute();
        assertEquals(0L, count);
        count = graph.graql().compute().count().in("relationship").includeAttribute().execute();
        assertEquals(1L, count);
    }
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        // manually construct the relation type and instance
        EntityType person = graph.getEntityType("person");
        Entity aPerson = person.addEntity();
        AttributeType<String> name = graph.putAttributeType("name", AttributeType.DataType.STRING);
        Attribute jason = name.putAttribute("jason");
        Role resourceOwner = graph.putRole(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of("name")));
        person.plays(resourceOwner);
        Role resourceValue = graph.putRole(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of("name")));
        name.plays(resourceValue);
        RelationshipType relationshipType = graph.putRelationshipType(Schema.ImplicitType.HAS.getLabel(Label.of("name"))).relates(resourceOwner).relates(resourceValue);
        relationshipType.addRelationship().addRolePlayer(resourceOwner, aPerson).addRolePlayer(resourceValue, jason);
        graph.commit();
    }
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        count = graph.graql().compute().count().execute();
        assertEquals(2L, count);
        count = graph.graql().compute().count().includeAttribute().execute();
        assertEquals(5L, count);
        count = graph.graql().compute().count().in("name").execute();
        assertEquals(1L, count);
        count = graph.graql().compute().count().includeAttribute().in("name").execute();
        assertEquals(1L, count);
        count = graph.graql().compute().count().in("@has-name").execute();
        assertEquals(2L, count);
        count = graph.graql().compute().count().in("@has-name", "thing").execute();
        assertEquals(5L, count);
        count = graph.graql().compute().count().in("@has-name", "name").execute();
        assertEquals(3L, count);
        count = graph.graql().compute().count().in("relationship").execute();
        assertEquals(0L, count);
        count = graph.graql().compute().count().in("relationship").includeAttribute().execute();
        assertEquals(2L, count);
    }
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) Attribute(ai.grakn.concept.Attribute) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 20 with Attribute

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

the class CountTest method testHasResourceVerticesAndEdges.

@Test
public void testHasResourceVerticesAndEdges() {
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        // manually construct the relation type and instance
        EntityType person = graph.putEntityType("person");
        Entity aPerson = person.addEntity();
        AttributeType<String> name = graph.putAttributeType("name", AttributeType.DataType.STRING);
        Attribute jason = name.putAttribute("jason");
        person.attribute(name);
        Role resourceOwner = graph.putRole(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of("name")));
        person.plays(resourceOwner);
        Role resourceValue = graph.putRole(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of("name")));
        name.plays(resourceValue);
        RelationshipType relationshipType = graph.putRelationshipType(Schema.ImplicitType.HAS.getLabel(Label.of("name"))).relates(resourceOwner).relates(resourceValue);
        // here relationship type is still implicit
        relationshipType.addRelationship().addRolePlayer(resourceOwner, aPerson).addRolePlayer(resourceValue, jason);
        graph.commit();
    }
    long count;
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        count = graph.graql().compute().count().execute();
        assertEquals(1L, count);
        count = graph.graql().compute().count().includeAttribute().execute();
        assertEquals(3L, count);
        count = graph.graql().compute().count().in("name").execute();
        assertEquals(1L, count);
        count = graph.graql().compute().count().in("@has-name").execute();
        assertEquals(1L, count);
        count = graph.graql().compute().count().in("@has-name", "name").execute();
        assertEquals(2L, count);
        count = graph.graql().compute().count().in("relationship").execute();
        assertEquals(0L, count);
        count = graph.graql().compute().count().in("relationship").includeAttribute().execute();
        assertEquals(1L, count);
    }
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        EntityType person = graph.getEntityType("person");
        AttributeType<String> name = graph.putAttributeType("name", AttributeType.DataType.STRING);
        Entity aPerson = person.addEntity();
        aPerson.attribute(name.getAttribute("jason"));
        graph.commit();
    }
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        count = graph.graql().compute().count().includeAttribute().execute();
        assertEquals(5L, count);
        count = graph.graql().compute().count().in("name").execute();
        assertEquals(1L, count);
        count = graph.graql().compute().count().in("@has-name").execute();
        assertEquals(2L, count);
        count = graph.graql().compute().count().in("@has-name", "name").execute();
        assertEquals(3L, count);
        count = graph.graql().compute().count().in("relationship").execute();
        assertEquals(0L, count);
        count = graph.graql().compute().count().in("relationship").includeAttribute().execute();
        assertEquals(2L, count);
    }
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) Attribute(ai.grakn.concept.Attribute) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Aggregations

Attribute (ai.grakn.concept.Attribute)36 Test (org.junit.Test)26 GraknTx (ai.grakn.GraknTx)15 EntityType (ai.grakn.concept.EntityType)15 Entity (ai.grakn.concept.Entity)14 RelationshipType (ai.grakn.concept.RelationshipType)10 Set (java.util.Set)10 Role (ai.grakn.concept.Role)9 AttributeType (ai.grakn.concept.AttributeType)8 Relationship (ai.grakn.concept.Relationship)8 Label (ai.grakn.concept.Label)7 Thing (ai.grakn.concept.Thing)6 Concept (ai.grakn.concept.Concept)5 HashSet (java.util.HashSet)5 ConceptId (ai.grakn.concept.ConceptId)4 Schema (ai.grakn.util.Schema)4 GraknTxOperationException (ai.grakn.exception.GraknTxOperationException)3 Sets (com.google.common.collect.Sets)3 Collection (java.util.Collection)3 Collectors (java.util.stream.Collectors)3