use of ai.grakn.concept.ConceptId in project grakn by graknlabs.
the class ExplanationBuilderTest method whenExplainInferred_returnsLinkedExplanation.
// NOTE: This test ix expected to be slower than average.
@Test
public void whenExplainInferred_returnsLinkedExplanation() {
Label person = Label.of("person");
Label siblings = Label.of("siblings");
Label parentship = Label.of("parentship");
String mainQuery = "match ($x, $y) isa cousins; limit 15; get;";
genealogyKB.tx().graql().infer(true).parser().<GetQuery>parseQuery(mainQuery).forEach(answer -> {
String cousin1 = answer.get("x").getId().getValue();
String cousin2 = answer.get("y").getId().getValue();
String specificQuery = "match " + "$x id " + cousin1 + ";" + "$y id " + cousin2 + ";" + "(cousin: $x, cousin: $y) isa cousins; limit 1;get;";
GetQuery query = genealogyKB.tx().graql().infer(true).parse(specificQuery);
ai.grakn.graql.admin.Answer specificAnswer = query.execute().stream().findFirst().orElse(new QueryAnswer());
Set<ConceptId> originalEntityIds = specificAnswer.getExplanation().getAnswers().stream().flatMap(ans -> ans.concepts().stream()).map(ai.grakn.concept.Concept::getId).collect(Collectors.toSet());
List<Answer> explanation = ExplanationBuilder.buildExplanation(specificAnswer);
Set<ConceptId> entityIds = explanation.stream().flatMap(exp -> exp.conceptMap().values().stream()).filter(c -> c.baseType().equals("ENTITY")).map(Concept::id).collect(Collectors.toSet());
// ensure we deal with the same entities
assertEquals(originalEntityIds, entityIds);
assertEquals(3, explanation.size());
explanation.forEach(explanationAnswer -> {
explanationAnswer.conceptMap().values().forEach(concept -> {
Schema.BaseType baseType = Schema.BaseType.valueOf(concept.baseType());
Label typeLabel = ((Thing) concept).type().label();
switch(baseType) {
case ENTITY:
assertEquals(person, typeLabel);
break;
case RELATIONSHIP:
assertTrue(typeLabel.equals(siblings) || typeLabel.equals(parentship));
break;
}
});
});
});
}
use of ai.grakn.concept.ConceptId in project grakn by graknlabs.
the class HasAttributeProperty method mapToAtom.
@Override
public Atomic mapToAtom(VarPatternAdmin var, Set<VarPatternAdmin> vars, ReasonerQuery parent) {
// NB: HasAttributeProperty always has (type) label specified
Var varName = var.var().asUserDefined();
Var relationVariable = relationship().var();
Var attributeVariable = attribute().var().asUserDefined();
Set<ValuePredicate> predicates = getValuePredicates(attributeVariable, attribute(), vars, parent);
IsaProperty isaProp = attribute().getProperties(IsaProperty.class).findFirst().orElse(null);
VarPatternAdmin typeVar = isaProp != null ? isaProp.type() : null;
IdPredicate predicate = typeVar != null ? getIdPredicate(attributeVariable, typeVar, vars, parent) : null;
ConceptId predicateId = predicate != null ? predicate.getPredicate() : null;
// add resource atom
VarPatternAdmin resVar = relationVariable.isUserDefinedName() ? varName.has(type(), attributeVariable, relationVariable).admin() : varName.has(type(), attributeVariable).admin();
return ResourceAtom.create(resVar, attributeVariable, relationVariable, predicateId, predicates, parent);
}
use of ai.grakn.concept.ConceptId in project grakn by graknlabs.
the class PlaysProperty method mapToAtom.
@Override
public Atomic mapToAtom(VarPatternAdmin var, Set<VarPatternAdmin> vars, ReasonerQuery parent) {
Var varName = var.var().asUserDefined();
VarPatternAdmin typeVar = this.role();
Var typeVariable = typeVar.var().asUserDefined();
IdPredicate predicate = getIdPredicate(typeVariable, typeVar, vars, parent);
ConceptId predicateId = predicate == null ? null : predicate.getPredicate();
return PlaysAtom.create(varName, typeVariable, predicateId, parent);
}
use of ai.grakn.concept.ConceptId in project grakn by graknlabs.
the class IndexPostProcessorTest method whenAddingCommitLogToPostProcessor_EnsureIndexStorageIsUpdated.
@Test
public void whenAddingCommitLogToPostProcessor_EnsureIndexStorageIsUpdated() {
// Create Sample Data For CommitLog
String index1 = "index1";
String index2 = "index2";
Set<ConceptId> ids1 = Arrays.asList("a", "b", "c").stream().map(ConceptId::of).collect(Collectors.toSet());
Set<ConceptId> ids2 = Arrays.asList("1", "2", "3").stream().map(ConceptId::of).collect(Collectors.toSet());
HashMap<String, Set<ConceptId>> attributes = new HashMap<>();
attributes.put(index1, ids1);
attributes.put(index2, ids2);
// Create Commit Log
Keyspace keyspace = Keyspace.of("whatakeyspace");
CommitLog commitLog = CommitLog.create(keyspace, Collections.emptyMap(), attributes);
// Call the post processor
indexPostProcessor.updateIndices(commitLog);
// Check index storage is updated
verify(indexStorage, Mockito.times(1)).addIndex(keyspace, index1, ids1);
verify(indexStorage, Mockito.times(1)).addIndex(keyspace, index2, ids2);
}
use of ai.grakn.concept.ConceptId in project grakn by graknlabs.
the class RedisCountStorageTest method whenIncreasingCountOnRedisConcurrently_EnsureAllThreadCountsArePersisted.
@Test
public void whenIncreasingCountOnRedisConcurrently_EnsureAllThreadCountsArePersisted() throws ExecutionException, InterruptedException {
Keyspace keyspace = SampleKBLoader.randomKeyspace();
ConceptId conceptId = ConceptId.of("Roach");
int[] counts = { 5, 5, 10, 10, -8, -2, 5, 5, -7 };
ExecutorService pool = Executors.newCachedThreadPool();
Set<Future> futures = new HashSet<>();
assertEquals(0, redis.getCount(RedisCountStorage.getKeyNumInstances(keyspace, conceptId)));
for (int i = 0; i < counts.length; i++) {
int finalI = i;
futures.add(pool.submit(() -> redis.incrementCount(RedisCountStorage.getKeyNumInstances(keyspace, conceptId), counts[finalI])));
}
for (Future future : futures) {
future.get();
}
assertEquals(23, redis.getCount(RedisCountStorage.getKeyNumInstances(keyspace, conceptId)));
}
Aggregations