Search in sources :

Example 21 with EntityType

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

the class StatisticsTest method addSchemaAndEntities.

private void addSchemaAndEntities() throws InvalidKBException {
    try (GraknTx tx = session.open(GraknTxType.WRITE)) {
        EntityType entityType1 = tx.putEntityType(thing);
        EntityType entityType2 = tx.putEntityType(anotherThing);
        Entity entity1 = entityType1.addEntity();
        Entity entity2 = entityType1.addEntity();
        Entity entity3 = entityType1.addEntity();
        Entity entity4 = entityType2.addEntity();
        entityId1 = entity1.getId();
        entityId2 = entity2.getId();
        entityId3 = entity3.getId();
        entityId4 = entity4.getId();
        Role relation1 = tx.putRole("relation1");
        Role relation2 = tx.putRole("relation2");
        entityType1.plays(relation1).plays(relation2);
        entityType2.plays(relation1).plays(relation2);
        RelationshipType related = tx.putRelationshipType("related").relates(relation1).relates(relation2);
        related.addRelationship().addRolePlayer(relation1, entity1).addRolePlayer(relation2, entity2);
        related.addRelationship().addRolePlayer(relation1, entity2).addRolePlayer(relation2, entity3);
        related.addRelationship().addRolePlayer(relation1, entity2).addRolePlayer(relation2, entity4);
        AttributeType<Double> attribute1 = tx.putAttributeType(resourceType1, AttributeType.DataType.DOUBLE);
        AttributeType<Long> attribute2 = tx.putAttributeType(resourceType2, AttributeType.DataType.LONG);
        AttributeType<Long> attribute3 = tx.putAttributeType(resourceType3, AttributeType.DataType.LONG);
        AttributeType<String> attribute4 = tx.putAttributeType(resourceType4, AttributeType.DataType.STRING);
        AttributeType<Long> attribute5 = tx.putAttributeType(resourceType5, AttributeType.DataType.LONG);
        AttributeType<Double> attribute6 = tx.putAttributeType(resourceType6, AttributeType.DataType.DOUBLE);
        AttributeType<Double> attribute7 = tx.putAttributeType(resourceType7, AttributeType.DataType.DOUBLE);
        entityType1.attribute(attribute1);
        entityType1.attribute(attribute2);
        entityType1.attribute(attribute3);
        entityType1.attribute(attribute4);
        entityType1.attribute(attribute5);
        entityType1.attribute(attribute6);
        entityType1.attribute(attribute7);
        entityType2.attribute(attribute1);
        entityType2.attribute(attribute2);
        entityType2.attribute(attribute3);
        entityType2.attribute(attribute4);
        entityType2.attribute(attribute5);
        entityType2.attribute(attribute6);
        entityType2.attribute(attribute7);
        tx.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 22 with EntityType

use of ai.grakn.concept.EntityType 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 23 with EntityType

use of ai.grakn.concept.EntityType 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 24 with EntityType

use of ai.grakn.concept.EntityType 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 25 with EntityType

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

Aggregations

EntityType (ai.grakn.concept.EntityType)168 Test (org.junit.Test)141 Role (ai.grakn.concept.Role)83 RelationshipType (ai.grakn.concept.RelationshipType)82 Entity (ai.grakn.concept.Entity)74 GraknTx (ai.grakn.GraknTx)45 Relationship (ai.grakn.concept.Relationship)21 Attribute (ai.grakn.concept.Attribute)19 AttributeType (ai.grakn.concept.AttributeType)17 ConceptId (ai.grakn.concept.ConceptId)17 Label (ai.grakn.concept.Label)17 Set (java.util.Set)16 HashSet (java.util.HashSet)15 Thing (ai.grakn.concept.Thing)14 GraknTxType (ai.grakn.GraknTxType)8 ArrayList (java.util.ArrayList)8 GraknSession (ai.grakn.GraknSession)7 Concept (ai.grakn.concept.Concept)7 HashMap (java.util.HashMap)7 Before (org.junit.Before)7