Search in sources :

Example 41 with GraknTx

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

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

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

the class GraqlTest method testPaths.

@Test
public void testPaths() throws InvalidKBException {
    addSchemaAndEntities();
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        PathsQuery query = graph.graql().parse("compute paths from '" + entityId1 + "' to '" + entityId2 + "';");
        List<List<Concept>> path = query.execute();
        assertEquals(1, path.size());
        List<String> result = path.get(0).stream().map(Concept::getId).map(ConceptId::getValue).collect(Collectors.toList());
        List<String> expected = Lists.newArrayList(entityId1, relationId12, entityId2);
        assertEquals(expected, result);
    }
}
Also used : Concept(ai.grakn.concept.Concept) GraknTx(ai.grakn.GraknTx) PathsQuery(ai.grakn.graql.analytics.PathsQuery) List(java.util.List) Test(org.junit.Test)

Example 44 with GraknTx

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

the class GraqlTest method testNonResourceTypeAsSubgraphForAnalytics.

@Test(expected = GraqlQueryException.class)
public void testNonResourceTypeAsSubgraphForAnalytics() throws InvalidKBException {
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        graph.putEntityType(thingy);
        graph.commit();
    }
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        graph.graql().parse("compute sum of thingy;").execute();
    }
}
Also used : GraknTx(ai.grakn.GraknTx) Test(org.junit.Test)

Example 45 with GraknTx

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

the class GraqlTest method testPath.

@Test
public void testPath() throws InvalidKBException {
    addSchemaAndEntities();
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        PathQuery query = graph.graql().parse("compute path from '" + entityId1 + "' to '" + entityId2 + "';");
        Optional<List<Concept>> path = query.execute();
        List<String> result = path.get().stream().map(Concept::getId).map(ConceptId::getValue).collect(Collectors.toList());
        List<String> expected = Lists.newArrayList(entityId1, relationId12, entityId2);
        assertEquals(expected, result);
    }
}
Also used : Concept(ai.grakn.concept.Concept) GraknTx(ai.grakn.GraknTx) PathQuery(ai.grakn.graql.analytics.PathQuery) List(java.util.List) Test(org.junit.Test)

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