Search in sources :

Example 11 with ConceptId

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;
                }
            });
        });
    });
}
Also used : GenealogyKB(ai.grakn.test.kbs.GenealogyKB) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) GetQuery(ai.grakn.graql.GetQuery) List(java.util.List) Label(ai.grakn.concept.Label) SampleKBContext(ai.grakn.test.rule.SampleKBContext) Schema(ai.grakn.util.Schema) ConceptId(ai.grakn.concept.ConceptId) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) ClassRule(org.junit.ClassRule) Assert.assertEquals(org.junit.Assert.assertEquals) Schema(ai.grakn.util.Schema) Label(ai.grakn.concept.Label) ConceptId(ai.grakn.concept.ConceptId) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) GetQuery(ai.grakn.graql.GetQuery) Test(org.junit.Test)

Example 12 with ConceptId

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);
}
Also used : ReasonerUtils.getIdPredicate(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.getIdPredicate) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Var(ai.grakn.graql.Var) ValuePredicate(ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate) ConceptId(ai.grakn.concept.ConceptId)

Example 13 with ConceptId

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);
}
Also used : IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) ReasonerUtils.getIdPredicate(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.getIdPredicate) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Var(ai.grakn.graql.Var) ConceptId(ai.grakn.concept.ConceptId)

Example 14 with ConceptId

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);
}
Also used : Set(java.util.Set) HashMap(java.util.HashMap) Keyspace(ai.grakn.Keyspace) CommitLog(ai.grakn.kb.log.CommitLog) ConceptId(ai.grakn.concept.ConceptId) Test(org.junit.Test)

Example 15 with ConceptId

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)));
}
Also used : Keyspace(ai.grakn.Keyspace) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ConceptId(ai.grakn.concept.ConceptId) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ConceptId (ai.grakn.concept.ConceptId)80 Test (org.junit.Test)55 Concept (ai.grakn.concept.Concept)23 Role (ai.grakn.concept.Role)22 RelationshipType (ai.grakn.concept.RelationshipType)19 GraknTx (ai.grakn.GraknTx)18 EntityType (ai.grakn.concept.EntityType)18 Label (ai.grakn.concept.Label)16 GrpcConcept (ai.grakn.rpc.generated.GrpcConcept)14 Var (ai.grakn.graql.Var)12 List (java.util.List)12 Entity (ai.grakn.concept.Entity)10 AttributeType (ai.grakn.concept.AttributeType)9 HashSet (java.util.HashSet)9 Set (java.util.Set)9 Assert.assertEquals (org.junit.Assert.assertEquals)9 ClassRule (org.junit.ClassRule)9 GraknTxType (ai.grakn.GraknTxType)8 Keyspace (ai.grakn.Keyspace)8 IdPredicate (ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate)8