Search in sources :

Example 21 with Entity

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

the class StatisticsTest method addResourceRelations.

private void addResourceRelations() throws InvalidKBException {
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        Entity entity1 = graph.getConcept(entityId1);
        Entity entity2 = graph.getConcept(entityId2);
        Entity entity3 = graph.getConcept(entityId3);
        Entity entity4 = graph.getConcept(entityId4);
        Role resourceOwner1 = graph.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of(resourceType1)));
        Role resourceOwner2 = graph.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of(resourceType2)));
        Role resourceOwner3 = graph.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of(resourceType3)));
        Role resourceOwner4 = graph.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of(resourceType4)));
        Role resourceOwner5 = graph.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of(resourceType5)));
        Role resourceOwner6 = graph.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of(resourceType6)));
        Role resourceValue1 = graph.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of(resourceType1)));
        Role resourceValue2 = graph.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of(resourceType2)));
        Role resourceValue3 = graph.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of(resourceType3)));
        Role resourceValue4 = graph.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of(resourceType4)));
        Role resourceValue5 = graph.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of(resourceType5)));
        Role resourceValue6 = graph.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of(resourceType6)));
        RelationshipType relationshipType1 = graph.getSchemaConcept(Schema.ImplicitType.HAS.getLabel(Label.of(resourceType1)));
        relationshipType1.addRelationship().addRolePlayer(resourceOwner1, entity1).addRolePlayer(resourceValue1, graph.<Double>getAttributeType(resourceType1).putAttribute(1.2));
        relationshipType1.addRelationship().addRolePlayer(resourceOwner1, entity1).addRolePlayer(resourceValue1, graph.<Double>getAttributeType(resourceType1).putAttribute(1.5));
        relationshipType1.addRelationship().addRolePlayer(resourceOwner1, entity3).addRolePlayer(resourceValue1, graph.<Double>getAttributeType(resourceType1).putAttribute(1.8));
        RelationshipType relationshipType2 = graph.getSchemaConcept(Schema.ImplicitType.HAS.getLabel(Label.of(resourceType2)));
        relationshipType2.addRelationship().addRolePlayer(resourceOwner2, entity1).addRolePlayer(resourceValue2, graph.<Long>getAttributeType(resourceType2).putAttribute(4L));
        relationshipType2.addRelationship().addRolePlayer(resourceOwner2, entity1).addRolePlayer(resourceValue2, graph.<Long>getAttributeType(resourceType2).putAttribute(-1L));
        relationshipType2.addRelationship().addRolePlayer(resourceOwner2, entity4).addRolePlayer(resourceValue2, graph.<Long>getAttributeType(resourceType2).putAttribute(0L));
        graph.<Long>getAttributeType(resourceType3).putAttribute(100L);
        RelationshipType relationshipType5 = graph.getSchemaConcept(Schema.ImplicitType.HAS.getLabel(Label.of(resourceType5)));
        relationshipType5.addRelationship().addRolePlayer(resourceOwner5, entity1).addRolePlayer(resourceValue5, graph.<Long>getAttributeType(resourceType5).putAttribute(-7L));
        relationshipType5.addRelationship().addRolePlayer(resourceOwner5, entity2).addRolePlayer(resourceValue5, graph.<Long>getAttributeType(resourceType5).putAttribute(-7L));
        relationshipType5.addRelationship().addRolePlayer(resourceOwner5, entity4).addRolePlayer(resourceValue5, graph.<Long>getAttributeType(resourceType5).putAttribute(-7L));
        RelationshipType relationshipType6 = graph.getSchemaConcept(Schema.ImplicitType.HAS.getLabel(Label.of(resourceType6)));
        relationshipType6.addRelationship().addRolePlayer(resourceOwner6, entity1).addRolePlayer(resourceValue6, graph.<Double>getAttributeType(resourceType6).putAttribute(7.5));
        relationshipType6.addRelationship().addRolePlayer(resourceOwner6, entity2).addRolePlayer(resourceValue6, graph.<Double>getAttributeType(resourceType6).putAttribute(7.5));
        relationshipType6.addRelationship().addRolePlayer(resourceOwner6, entity4).addRolePlayer(resourceValue6, graph.<Double>getAttributeType(resourceType6).putAttribute(7.5));
        // some resources in, but not connect them to any instances
        graph.<Double>getAttributeType(resourceType1).putAttribute(2.8);
        graph.<Long>getAttributeType(resourceType2).putAttribute(-5L);
        graph.<Long>getAttributeType(resourceType5).putAttribute(10L);
        graph.<Double>getAttributeType(resourceType6).putAttribute(0.8);
        graph.commit();
    }
}
Also used : Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) RelationshipType(ai.grakn.concept.RelationshipType)

Example 22 with Entity

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

the class ConnectedComponentTest method addResourceRelations.

private void addResourceRelations() throws InvalidKBException {
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        Entity entity1 = graph.getConcept(entityId1);
        Entity entity2 = graph.getConcept(entityId2);
        Entity entity3 = graph.getConcept(entityId3);
        Entity entity4 = graph.getConcept(entityId4);
        entity1.attribute(graph.getAttributeType(resourceType1).putAttribute(1.2)).attribute(graph.getAttributeType(resourceType1).putAttribute(1.5)).attribute(graph.getAttributeType(resourceType2).putAttribute(4L)).attribute(graph.getAttributeType(resourceType2).putAttribute(-1L)).attribute(graph.getAttributeType(resourceType5).putAttribute(-7L)).attribute(graph.getAttributeType(resourceType6).putAttribute(7.5));
        entity2.attribute(graph.getAttributeType(resourceType5).putAttribute(-7L)).attribute(graph.getAttributeType(resourceType6).putAttribute(7.5));
        entity3.attribute(graph.getAttributeType(resourceType1).putAttribute(1.8));
        entity4.attribute(graph.getAttributeType(resourceType2).putAttribute(0L)).attribute(graph.getAttributeType(resourceType5).putAttribute(-7L)).attribute(graph.getAttributeType(resourceType6).putAttribute(7.5));
        // some resources in, but not connect them to any instances
        aDisconnectedAttribute = graph.getAttributeType(resourceType1).putAttribute(2.8).getId();
        graph.getAttributeType(resourceType2).putAttribute(-5L);
        graph.getAttributeType(resourceType3).putAttribute(100L);
        graph.getAttributeType(resourceType5).putAttribute(10L);
        graph.getAttributeType(resourceType6).putAttribute(0.8);
        graph.commit();
    }
}
Also used : GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity)

Example 23 with Entity

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

the class GraqlTest method testStatisticsMethods.

@Test
public void testStatisticsMethods() throws InvalidKBException {
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        Label resourceTypeId = Label.of("my-resource");
        AttributeType<Long> resource = graph.putAttributeType(resourceTypeId, AttributeType.DataType.LONG);
        EntityType thingy = graph.putEntityType("thingy");
        thingy.attribute(resource);
        Entity theResourceOwner = thingy.addEntity();
        Role resourceOwner = graph.getRole(Schema.ImplicitType.HAS_OWNER.getLabel(resourceTypeId).getValue());
        Role resourceValue = graph.getRole(Schema.ImplicitType.HAS_VALUE.getLabel(resourceTypeId).getValue());
        RelationshipType relationshipType = graph.getRelationshipType(Schema.ImplicitType.HAS.getLabel(resourceTypeId).getValue());
        relationshipType.addRelationship().addRolePlayer(resourceOwner, theResourceOwner).addRolePlayer(resourceValue, resource.putAttribute(1L));
        relationshipType.addRelationship().addRolePlayer(resourceOwner, theResourceOwner).addRolePlayer(resourceValue, resource.putAttribute(2L));
        relationshipType.addRelationship().addRolePlayer(resourceOwner, theResourceOwner).addRolePlayer(resourceValue, resource.putAttribute(3L));
        graph.commit();
    }
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        // use graql to compute various statistics
        Optional<? extends Number> result = graph.graql().<SumQuery>parse("compute sum of my-resource;").execute();
        assertEquals(Optional.of(6L), result);
        result = graph.graql().<MinQuery>parse("compute min of my-resource;").execute();
        assertEquals(Optional.of(1L), result);
        result = graph.graql().<MaxQuery>parse("compute max of my-resource;").execute();
        assertEquals(Optional.of(3L), result);
        result = graph.graql().<MeanQuery>parse("compute mean of my-resource;").execute();
        assert result.isPresent();
        assertEquals(2.0, (Double) result.get(), 0.1);
        result = graph.graql().<MedianQuery>parse("compute median of my-resource;").execute();
        assertEquals(Optional.of(2L), result);
    }
}
Also used : Entity(ai.grakn.concept.Entity) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType) MedianQuery(ai.grakn.graql.analytics.MedianQuery) MeanQuery(ai.grakn.graql.analytics.MeanQuery) MaxQuery(ai.grakn.graql.analytics.MaxQuery) EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) MinQuery(ai.grakn.graql.analytics.MinQuery) Test(org.junit.Test)

Example 24 with Entity

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

the class AnalyticsTest method addSchemaAndEntities.

private void addSchemaAndEntities() throws InvalidKBException {
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        EntityType entityType1 = graph.putEntityType(thingy);
        EntityType entityType2 = graph.putEntityType(anotherThing);
        Entity entity1 = entityType1.addEntity();
        Entity entity2 = entityType1.addEntity();
        Entity entity3 = entityType1.addEntity();
        Entity entity4 = entityType2.addEntity();
        entityId1 = entity1.getId().getValue();
        entityId2 = entity2.getId().getValue();
        entityId3 = entity3.getId().getValue();
        entityId4 = entity4.getId().getValue();
        Role role1 = graph.putRole("role1");
        Role role2 = graph.putRole("role2");
        entityType1.plays(role1).plays(role2);
        entityType2.plays(role1).plays(role2);
        RelationshipType relationshipType = graph.putRelationshipType(related).relates(role1).relates(role2);
        relationId12 = relationshipType.addRelationship().addRolePlayer(role1, entity1).addRolePlayer(role2, entity2).getId().getValue();
        relationId24 = relationshipType.addRelationship().addRolePlayer(role1, entity2).addRolePlayer(role2, entity4).getId().getValue();
        graph.commit();
    }
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) RelationshipType(ai.grakn.concept.RelationshipType)

Example 25 with Entity

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

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