Search in sources :

Example 6 with Label

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

the class StatisticsTest method testStatisticsExceptions.

@Test
public void testStatisticsExceptions() throws Exception {
    addSchemaAndEntities();
    addResourceRelations();
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        // resources-type is not set
        assertGraqlQueryExceptionThrown(graph.graql().compute().max().in(thing)::execute);
        assertGraqlQueryExceptionThrown(graph.graql().compute().min().in(thing)::execute);
        assertGraqlQueryExceptionThrown(graph.graql().compute().mean().in(thing)::execute);
        assertGraqlQueryExceptionThrown(graph.graql().compute().sum().in(thing)::execute);
        assertGraqlQueryExceptionThrown(graph.graql().compute().std().in(thing)::execute);
        assertGraqlQueryExceptionThrown(graph.graql().compute().median().in(thing)::execute);
        // if it's not a resource-type
        assertGraqlQueryExceptionThrown(graph.graql().compute().max().of(thing)::execute);
        assertGraqlQueryExceptionThrown(graph.graql().compute().min().of(thing)::execute);
        assertGraqlQueryExceptionThrown(graph.graql().compute().mean().of(thing)::execute);
        assertGraqlQueryExceptionThrown(graph.graql().compute().sum().of(thing)::execute);
        assertGraqlQueryExceptionThrown(graph.graql().compute().std().of(thing)::execute);
        assertGraqlQueryExceptionThrown(graph.graql().compute().median().of(thing)::execute);
        // resource-type has no instance
        assertFalse(graph.graql().compute().max().of(resourceType7).execute().isPresent());
        assertFalse(graph.graql().compute().min().of(resourceType7).execute().isPresent());
        assertFalse(graph.graql().compute().sum().of(resourceType7).execute().isPresent());
        assertFalse(graph.graql().compute().std().of(resourceType7).execute().isPresent());
        assertFalse(graph.graql().compute().median().of(resourceType7).execute().isPresent());
        assertFalse(graph.graql().compute().mean().of(resourceType7).execute().isPresent());
        // resources are not connected to any entities
        assertFalse(graph.graql().compute().max().of(resourceType3).execute().isPresent());
        assertFalse(graph.graql().compute().min().of(resourceType3).execute().isPresent());
        assertFalse(graph.graql().compute().sum().of(resourceType3).execute().isPresent());
        assertFalse(graph.graql().compute().std().of(resourceType3).execute().isPresent());
        assertFalse(graph.graql().compute().median().of(resourceType3).execute().isPresent());
        assertFalse(graph.graql().compute().mean().of(resourceType3).execute().isPresent());
        // resource-type has incorrect data type
        assertGraqlQueryExceptionThrown(graph.graql().compute().max().of(resourceType4)::execute);
        assertGraqlQueryExceptionThrown(graph.graql().compute().min().of(resourceType4)::execute);
        assertGraqlQueryExceptionThrown(graph.graql().compute().mean().of(resourceType4)::execute);
        assertGraqlQueryExceptionThrown(graph.graql().compute().sum().of(resourceType4)::execute);
        assertGraqlQueryExceptionThrown(graph.graql().compute().std().of(resourceType4)::execute);
        assertGraqlQueryExceptionThrown(graph.graql().compute().median().of(resourceType4)::execute);
        // resource-types have different data types
        Set<Label> resourceTypes = Sets.newHashSet(Label.of(resourceType1), Label.of(resourceType2));
        assertGraqlQueryExceptionThrown(graph.graql().compute().max().of(resourceTypes)::execute);
        assertGraqlQueryExceptionThrown(graph.graql().compute().min().of(resourceTypes)::execute);
        assertGraqlQueryExceptionThrown(graph.graql().compute().mean().of(resourceTypes)::execute);
        assertGraqlQueryExceptionThrown(graph.graql().compute().sum().of(resourceTypes)::execute);
        assertGraqlQueryExceptionThrown(graph.graql().compute().std().of(resourceTypes)::execute);
        assertGraqlQueryExceptionThrown(graph.graql().compute().median().of(resourceTypes)::execute);
    }
}
Also used : GraknTx(ai.grakn.GraknTx) Label(ai.grakn.concept.Label) Test(org.junit.Test)

Example 7 with Label

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

the class ResourceAtom method toRelationshipAtom.

@Override
public RelationshipAtom toRelationshipAtom() {
    SchemaConcept type = getSchemaConcept();
    if (type == null)
        throw GraqlQueryException.illegalAtomConversion(this);
    GraknTx tx = getParentQuery().tx();
    Label typeLabel = Schema.ImplicitType.HAS.getLabel(type.getLabel());
    return RelationshipAtom.create(Graql.var().rel(Schema.ImplicitType.HAS_OWNER.getLabel(type.getLabel()).getValue(), getVarName()).rel(Schema.ImplicitType.HAS_VALUE.getLabel(type.getLabel()).getValue(), getPredicateVariable()).isa(typeLabel.getValue()).admin(), getPredicateVariable(), tx.getSchemaConcept(typeLabel).getId(), getParentQuery());
}
Also used : GraknTx(ai.grakn.GraknTx) Label(ai.grakn.concept.Label) SchemaConcept(ai.grakn.concept.SchemaConcept)

Example 8 with Label

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

the class ConnectedComponentTest method testConnectedComponent.

@Test
public void testConnectedComponent() {
    Map<String, Long> sizeMap;
    Map<String, Set<String>> memberMap;
    addSchemaAndEntities();
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        sizeMap = Graql.compute().withTx(graph).cluster().usingConnectedComponent().includeAttribute().execute();
        assertEquals(1, sizeMap.size());
        // 4 entities, 3 assertions
        assertEquals(7L, sizeMap.values().iterator().next().longValue());
        sizeMap = Graql.compute().withTx(graph).cluster().usingConnectedComponent().of(entityId1).includeAttribute().execute();
        assertEquals(1, sizeMap.size());
        assertEquals(7L, sizeMap.values().iterator().next().longValue());
        assertEquals(entityId1.getValue(), sizeMap.keySet().iterator().next());
        memberMap = Graql.compute().withTx(graph).cluster().usingConnectedComponent().in().members().execute();
        assertEquals(1, memberMap.size());
        assertEquals(7, memberMap.values().iterator().next().size());
        memberMap = Graql.compute().withTx(graph).cluster().usingConnectedComponent().of(entityId4).in().members().execute();
        assertEquals(1, memberMap.size());
        assertEquals(7, memberMap.values().iterator().next().size());
        assertEquals(entityId1.getValue(), sizeMap.keySet().iterator().next());
    }
    // add different resources. This may change existing cluster labels.
    addResourceRelations();
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        sizeMap = graph.graql().compute().cluster().usingConnectedComponent().includeAttribute().execute();
        Map<Long, Integer> populationCount00 = new HashMap<>();
        sizeMap.values().forEach(value -> populationCount00.put(value, populationCount00.containsKey(value) ? populationCount00.get(value) + 1 : 1));
        // 5 resources are not connected to anything
        assertEquals(5, populationCount00.get(1L).intValue());
        assertEquals(1, populationCount00.get(15L).intValue());
        sizeMap = graph.graql().compute().cluster().usingConnectedComponent().of(aDisconnectedAttribute).includeAttribute().execute();
        assertEquals(1, sizeMap.size());
        memberMap = graph.graql().compute().cluster().usingConnectedComponent().members().execute();
        assertEquals(1, memberMap.size());
        Map<Integer, Integer> populationCount1 = new HashMap<>();
        memberMap.values().forEach(value -> populationCount1.put(value.size(), populationCount1.containsKey(value.size()) ? populationCount1.get(value.size()) + 1 : 1));
        assertEquals(1, populationCount1.get(7).intValue());
        // test on subtypes. This will change existing cluster labels.
        Set<Label> subTypes = Sets.newHashSet(thing, anotherThing, resourceType1, resourceType2, resourceType3, resourceType4, resourceType5, resourceType6).stream().map(Label::of).collect(Collectors.toSet());
        sizeMap = graph.graql().compute().cluster().usingConnectedComponent().in(subTypes).execute();
        // No relationships, so this is the entity count;
        assertEquals(17, sizeMap.size());
        memberMap = graph.graql().compute().cluster().usingConnectedComponent().members().in(subTypes).execute();
        assertEquals(17, memberMap.size());
    }
}
Also used : GraknTx(ai.grakn.GraknTx) Set(java.util.Set) HashMap(java.util.HashMap) Label(ai.grakn.concept.Label) Test(org.junit.Test)

Example 9 with Label

use of ai.grakn.concept.Label 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 10 with Label

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

Label (ai.grakn.concept.Label)87 Test (org.junit.Test)41 Role (ai.grakn.concept.Role)25 GraknTx (ai.grakn.GraknTx)22 ConceptId (ai.grakn.concept.ConceptId)21 Concept (ai.grakn.concept.Concept)20 Set (java.util.Set)20 RelationshipType (ai.grakn.concept.RelationshipType)19 SchemaConcept (ai.grakn.concept.SchemaConcept)19 AttributeType (ai.grakn.concept.AttributeType)17 EntityType (ai.grakn.concept.EntityType)17 Relationship (ai.grakn.concept.Relationship)14 HashSet (java.util.HashSet)14 Collectors (java.util.stream.Collectors)13 Type (ai.grakn.concept.Type)12 Stream (java.util.stream.Stream)12 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)11 Schema (ai.grakn.util.Schema)11 Collection (java.util.Collection)11 Pattern (ai.grakn.graql.Pattern)10