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());
}
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());
}
}
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);
}
}
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();
}
}
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);
}
}
Aggregations