Search in sources :

Example 36 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class StatisticsTest method addResourcesInstances.

private void addResourcesInstances() throws InvalidKBException {
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        graph.<Double>getAttributeType(resourceType1).putAttribute(1.2);
        graph.<Double>getAttributeType(resourceType1).putAttribute(1.5);
        graph.<Double>getAttributeType(resourceType1).putAttribute(1.8);
        graph.<Long>getAttributeType(resourceType2).putAttribute(4L);
        graph.<Long>getAttributeType(resourceType2).putAttribute(-1L);
        graph.<Long>getAttributeType(resourceType2).putAttribute(0L);
        graph.<Long>getAttributeType(resourceType5).putAttribute(6L);
        graph.<Long>getAttributeType(resourceType5).putAttribute(7L);
        graph.<Long>getAttributeType(resourceType5).putAttribute(8L);
        graph.<Double>getAttributeType(resourceType6).putAttribute(7.2);
        graph.<Double>getAttributeType(resourceType6).putAttribute(7.5);
        graph.<Double>getAttributeType(resourceType6).putAttribute(7.8);
        graph.<String>getAttributeType(resourceType4).putAttribute("a");
        graph.<String>getAttributeType(resourceType4).putAttribute("b");
        graph.<String>getAttributeType(resourceType4).putAttribute("c");
        graph.commit();
    }
}
Also used : GraknTx(ai.grakn.GraknTx)

Example 37 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class StatisticsTest method testMean.

@Test
public void testMean() throws Exception {
    Optional<Double> result;
    // resource-type has no instance
    addSchemaAndEntities();
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        result = Graql.compute().mean().of(resourceType1).in(Collections.emptyList()).withTx(graph).execute();
        assertFalse(result.isPresent());
        result = Graql.compute().mean().of(resourceType1).withTx(graph).execute();
        assertFalse(result.isPresent());
        result = Graql.compute().withTx(graph).mean().of(resourceType1).execute();
        assertFalse(result.isPresent());
        result = Graql.compute().mean().withTx(graph).of(resourceType1).execute();
        assertFalse(result.isPresent());
        result = graph.graql().compute().mean().of(resourceType2).execute();
        assertFalse(result.isPresent());
        result = graph.graql().compute().mean().of(resourceType2, resourceType5).execute();
        assertFalse(result.isPresent());
        result = graph.graql().compute().mean().of(resourceType2).withTx(graph).execute();
        assertFalse(result.isPresent());
        result = graph.graql().compute().withTx(graph).mean().of(resourceType2).execute();
        assertFalse(result.isPresent());
    }
    // add resources, but resources are not connected to any entities
    addResourcesInstances();
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        result = Graql.compute().mean().of(resourceType1).withTx(graph).execute();
        assertFalse(result.isPresent());
        result = Graql.compute().mean().of(resourceType1).in().withTx(graph).execute();
        assertFalse(result.isPresent());
        result = graph.graql().compute().mean().of(resourceType2).in(thing, anotherThing).execute();
        assertFalse(result.isPresent());
        result = Graql.compute().mean().of(resourceType2).withTx(graph).in(anotherThing).execute();
        assertFalse(result.isPresent());
    }
    // connect entity and resources
    addResourceRelations();
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        result = Graql.compute().withTx(graph).mean().of(resourceType1).execute();
        assertEquals(1.5, result.get(), delta);
        result = Graql.compute().mean().of(resourceType2).withTx(graph).execute();
        assertEquals(1D, result.get(), delta);
        result = graph.graql().compute().mean().of(resourceType1, resourceType6).execute();
        assertEquals(4.5, result.get(), delta);
        result = graph.graql().compute().mean().in(thing, anotherThing).of(resourceType2, resourceType5).execute();
        assertEquals(-3D, result.get(), delta);
        result = graph.graql().compute().mean().in(thing).of(resourceType1, resourceType6).execute();
        assertEquals(3.9, result.get(), delta);
    }
}
Also used : GraknTx(ai.grakn.GraknTx) Test(org.junit.Test)

Example 38 with GraknTx

use of ai.grakn.GraknTx 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 39 with GraknTx

use of ai.grakn.GraknTx 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 40 with GraknTx

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

Aggregations

GraknTx (ai.grakn.GraknTx)243 Test (org.junit.Test)189 EntityType (ai.grakn.concept.EntityType)54 GetQuery (ai.grakn.graql.GetQuery)52 Entity (ai.grakn.concept.Entity)51 QueryBuilder (ai.grakn.graql.QueryBuilder)49 Role (ai.grakn.concept.Role)48 RelationshipType (ai.grakn.concept.RelationshipType)46 Answer (ai.grakn.graql.admin.Answer)44 Set (java.util.Set)44 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)33 Label (ai.grakn.concept.Label)28 Attribute (ai.grakn.concept.Attribute)27 Concept (ai.grakn.concept.Concept)27 HashSet (java.util.HashSet)26 GraknSession (ai.grakn.GraknSession)22 AttributeType (ai.grakn.concept.AttributeType)22 ConceptId (ai.grakn.concept.ConceptId)22 List (java.util.List)19 GraknTxType (ai.grakn.GraknTxType)17