Search in sources :

Example 86 with Var

use of ai.grakn.graql.Var in project grakn by graknlabs.

the class MatchTest method testSchemaQuery.

@Test
public void testSchemaQuery() {
    Var type = var("type");
    Match query = qb.match(type.plays("character-being-played"));
    assertThat(query, variable(type, containsInAnyOrder(character, person)));
}
Also used : Var(ai.grakn.graql.Var) Match(ai.grakn.graql.Match) Test(org.junit.Test)

Example 87 with Var

use of ai.grakn.graql.Var in project grakn by graknlabs.

the class BenchmarkIT method loadRandomisedRelationInstances.

private void loadRandomisedRelationInstances(String entityLabel, String fromRoleLabel, String toRoleLabel, String relationLabel, int N, GraknSession session, GraknClient graknClient, Keyspace keyspace) {
    try (BatchExecutorClient loader = BatchExecutorClient.newBuilder().taskClient(graknClient).build()) {
        GraknTx tx = session.open(GraknTxType.READ);
        Var entityVar = var().asUserDefined();
        ConceptId[] instances = tx.graql().match(entityVar.isa(entityLabel)).get().execute().stream().map(ans -> ans.get(entityVar).getId()).toArray(ConceptId[]::new);
        assertEquals(instances.length, N);
        Role fromRole = tx.getRole(fromRoleLabel);
        Role toRole = tx.getRole(toRoleLabel);
        RelationshipType relationType = tx.getRelationshipType(relationLabel);
        Random rand = new Random();
        Multimap<Integer, Integer> assignmentMap = HashMultimap.create();
        for (int i = 0; i < N; i++) {
            int from = rand.nextInt(N - 1);
            int to = rand.nextInt(N - 1);
            while (to == from && assignmentMap.get(from).contains(to)) to = rand.nextInt(N - 1);
            Var fromRolePlayer = Graql.var();
            Var toRolePlayer = Graql.var();
            Pattern relationInsert = Graql.var().rel(Graql.label(fromRole.getLabel()), fromRolePlayer).rel(Graql.label(toRole.getLabel()), toRolePlayer).isa(Graql.label(relationType.getLabel())).and(fromRolePlayer.asUserDefined().id(instances[from])).and(toRolePlayer.asUserDefined().id(instances[to]));
            loader.add(Graql.insert(relationInsert.admin().varPatterns()), keyspace).subscribe();
        }
        tx.close();
    }
}
Also used : Iterables(com.google.common.collect.Iterables) VarPattern(ai.grakn.graql.VarPattern) Keyspace(ai.grakn.Keyspace) Role(ai.grakn.concept.Role) Assume.assumeFalse(org.junit.Assume.assumeFalse) InsertQuery(ai.grakn.graql.InsertQuery) LoggerFactory(org.slf4j.LoggerFactory) Graql(ai.grakn.graql.Graql) Random(java.util.Random) Answer(ai.grakn.graql.admin.Answer) Multimap(com.google.common.collect.Multimap) HashMultimap(com.google.common.collect.HashMultimap) Charset(java.nio.charset.Charset) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) Files(com.google.common.io.Files) GraknTx(ai.grakn.GraknTx) GraknClient(ai.grakn.client.GraknClient) ConceptId(ai.grakn.concept.ConceptId) ClassRule(org.junit.ClassRule) Grakn(ai.grakn.Grakn) GraknSystemProperty(ai.grakn.GraknSystemProperty) EngineContext(ai.grakn.test.rule.EngineContext) Before(org.junit.Before) GraknTxType(ai.grakn.GraknTxType) Graql.var(ai.grakn.graql.Graql.var) BatchExecutorClient(ai.grakn.client.BatchExecutorClient) Logger(org.slf4j.Logger) GraknSession(ai.grakn.GraknSession) Test(org.junit.Test) GetQuery(ai.grakn.graql.GetQuery) File(java.io.File) GraknTestUtil.usingTinker(ai.grakn.util.GraknTestUtil.usingTinker) List(java.util.List) SampleKBLoader.randomKeyspace(ai.grakn.util.SampleKBLoader.randomKeyspace) Var(ai.grakn.graql.Var) Pattern(ai.grakn.graql.Pattern) Assert.assertEquals(org.junit.Assert.assertEquals) VarPattern(ai.grakn.graql.VarPattern) Pattern(ai.grakn.graql.Pattern) Var(ai.grakn.graql.Var) RelationshipType(ai.grakn.concept.RelationshipType) ConceptId(ai.grakn.concept.ConceptId) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) Random(java.util.Random) BatchExecutorClient(ai.grakn.client.BatchExecutorClient)

Example 88 with Var

use of ai.grakn.graql.Var in project grakn by graknlabs.

the class BenchmarkIT method loadRuleChainData.

private void loadRuleChainData(int N) {
    final GraknClient graknClient = GraknClient.of(engine.uri());
    String entityLabel = "genericEntity";
    String attributeLabel = "index";
    String baseRelationLabel = "relation1";
    String genericRelationLabel = "relation";
    String fromRoleLabel = "fromRole";
    String toRoleLabel = "toRole";
    // load ontology
    try (GraknTx tx = session.open(GraknTxType.WRITE)) {
        Role fromRole = tx.putRole(fromRoleLabel);
        Role toRole = tx.putRole(toRoleLabel);
        AttributeType<String> index = tx.putAttributeType(attributeLabel, AttributeType.DataType.STRING);
        tx.putEntityType(entityLabel).plays(fromRole).plays(toRole).attribute(index);
        // define N relation types
        for (int i = 1; i <= N; i++) {
            tx.putRelationshipType(genericRelationLabel + i).relates(fromRole).relates(toRole);
        }
        // define N rules
        for (int i = 2; i <= N; i++) {
            Var fromVar = Graql.var().asUserDefined();
            Var intermedVar = Graql.var().asUserDefined();
            Var toVar = Graql.var().asUserDefined();
            VarPattern rulePattern = Graql.label("rule" + i).when(Graql.and(Graql.var().rel(Graql.label(fromRole.getLabel()), fromVar).rel(Graql.label(toRole.getLabel()), intermedVar).isa(baseRelationLabel), Graql.var().rel(Graql.label(fromRole.getLabel()), intermedVar).rel(Graql.label(toRole.getLabel()), toVar).isa(genericRelationLabel + (i - 1)))).then(Graql.and(Graql.var().rel(Graql.label(fromRole.getLabel()), fromVar).rel(Graql.label(toRole.getLabel()), toVar).isa(genericRelationLabel + i)));
            tx.graql().define(rulePattern).execute();
        }
        tx.commit();
    }
    // insert N + 1 entities
    loadEntities(entityLabel, N + 1, graknClient, keyspace);
    // load initial relation instances
    try (BatchExecutorClient loader = BatchExecutorClient.newBuilder().taskClient(graknClient).build()) {
        try (GraknTx tx = session.open(GraknTxType.READ)) {
            Var entityVar = var().asUserDefined();
            ConceptId[] instances = tx.graql().match(entityVar.isa(entityLabel)).get().execute().stream().map(ans -> ans.get(entityVar).getId()).toArray(ConceptId[]::new);
            RelationshipType baseRelation = tx.getRelationshipType(baseRelationLabel);
            Role fromRole = tx.getRole(fromRoleLabel);
            Role toRole = tx.getRole(toRoleLabel);
            loader.add(Graql.insert(Graql.var().asUserDefined().has(attributeLabel, "first").id(instances[0]).admin().varPatterns()), keyspace).subscribe();
            for (int i = 1; i < instances.length; i++) {
                Var fromRolePlayer = Graql.var();
                Var toRolePlayer = Graql.var();
                Pattern relationInsert = Graql.var().rel(Graql.label(fromRole.getLabel()), fromRolePlayer).rel(Graql.label(toRole.getLabel()), toRolePlayer).isa(Graql.label(baseRelation.getLabel())).and(fromRolePlayer.asUserDefined().id(instances[i - 1])).and(toRolePlayer.asUserDefined().id(instances[i]));
                loader.add(Graql.insert(relationInsert.admin().varPatterns()), keyspace).subscribe();
                Pattern resourceInsert = Graql.var().asUserDefined().has(attributeLabel, String.valueOf(i)).id(instances[i]);
                loader.add(Graql.insert(resourceInsert.admin().varPatterns()), keyspace).subscribe();
            }
        }
    }
}
Also used : Iterables(com.google.common.collect.Iterables) VarPattern(ai.grakn.graql.VarPattern) Keyspace(ai.grakn.Keyspace) Role(ai.grakn.concept.Role) Assume.assumeFalse(org.junit.Assume.assumeFalse) InsertQuery(ai.grakn.graql.InsertQuery) LoggerFactory(org.slf4j.LoggerFactory) Graql(ai.grakn.graql.Graql) Random(java.util.Random) Answer(ai.grakn.graql.admin.Answer) Multimap(com.google.common.collect.Multimap) HashMultimap(com.google.common.collect.HashMultimap) Charset(java.nio.charset.Charset) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) Files(com.google.common.io.Files) GraknTx(ai.grakn.GraknTx) GraknClient(ai.grakn.client.GraknClient) ConceptId(ai.grakn.concept.ConceptId) ClassRule(org.junit.ClassRule) Grakn(ai.grakn.Grakn) GraknSystemProperty(ai.grakn.GraknSystemProperty) EngineContext(ai.grakn.test.rule.EngineContext) Before(org.junit.Before) GraknTxType(ai.grakn.GraknTxType) Graql.var(ai.grakn.graql.Graql.var) BatchExecutorClient(ai.grakn.client.BatchExecutorClient) Logger(org.slf4j.Logger) GraknSession(ai.grakn.GraknSession) Test(org.junit.Test) GetQuery(ai.grakn.graql.GetQuery) File(java.io.File) GraknTestUtil.usingTinker(ai.grakn.util.GraknTestUtil.usingTinker) List(java.util.List) SampleKBLoader.randomKeyspace(ai.grakn.util.SampleKBLoader.randomKeyspace) Var(ai.grakn.graql.Var) Pattern(ai.grakn.graql.Pattern) Assert.assertEquals(org.junit.Assert.assertEquals) VarPattern(ai.grakn.graql.VarPattern) Pattern(ai.grakn.graql.Pattern) Var(ai.grakn.graql.Var) RelationshipType(ai.grakn.concept.RelationshipType) ConceptId(ai.grakn.concept.ConceptId) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) GraknClient(ai.grakn.client.GraknClient) VarPattern(ai.grakn.graql.VarPattern) BatchExecutorClient(ai.grakn.client.BatchExecutorClient)

Example 89 with Var

use of ai.grakn.graql.Var in project grakn by graknlabs.

the class GetQueryPropertyTest method joinAnswer.

private Optional<Answer> joinAnswer(Answer answerA, Answer answerB) {
    Map<Var, Concept> answer = Maps.newHashMap(answerA.map());
    answer.putAll(answerB.map());
    for (Var var : Sets.intersection(answerA.vars(), answerB.vars())) {
        if (!answerA.get(var).equals(answerB.get(var))) {
            return Optional.empty();
        }
    }
    return Optional.of(new QueryAnswer(answer));
}
Also used : Concept(ai.grakn.concept.Concept) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) Var(ai.grakn.graql.Var)

Example 90 with Var

use of ai.grakn.graql.Var in project grakn by graknlabs.

the class QueryAnswer method merge.

@Override
public Answer merge(Answer a2, boolean mergeExplanation) {
    if (a2.isEmpty())
        return this;
    if (this.isEmpty())
        return a2;
    Sets.SetView<Var> varUnion = Sets.union(this.vars(), a2.vars());
    Set<Var> varIntersection = Sets.intersection(this.vars(), a2.vars());
    Map<Var, Concept> entryMap = Sets.union(this.entrySet(), a2.entrySet()).stream().filter(e -> !varIntersection.contains(e.getKey())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    varIntersection.forEach(var -> {
        Concept concept = this.get(var);
        Concept otherConcept = a2.get(var);
        if (concept.equals(otherConcept))
            entryMap.put(var, concept);
        else {
            if (concept.isSchemaConcept() && otherConcept.isSchemaConcept() && !ReasonerUtils.areDisjointTypes(concept.asSchemaConcept(), otherConcept.asSchemaConcept())) {
                entryMap.put(var, Iterables.getOnlyElement(ReasonerUtils.topOrMeta(Sets.newHashSet(concept.asSchemaConcept(), otherConcept.asSchemaConcept()))));
            }
        }
    });
    if (!entryMap.keySet().equals(varUnion))
        return new QueryAnswer();
    return new QueryAnswer(entryMap, mergeExplanation ? this.mergeExplanation(a2) : this.getExplanation());
}
Also used : Concept(ai.grakn.concept.Concept) Iterables(com.google.common.collect.Iterables) Pair(ai.grakn.graql.internal.reasoner.utils.Pair) Concept(ai.grakn.concept.Concept) Graql(ai.grakn.graql.Graql) HashMap(java.util.HashMap) Answer(ai.grakn.graql.admin.Answer) JoinExplanation(ai.grakn.graql.internal.reasoner.explanation.JoinExplanation) HashSet(java.util.HashSet) AnswerExplanation(ai.grakn.graql.admin.AnswerExplanation) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) GraqlQueryException(ai.grakn.exception.GraqlQueryException) ImmutableMap(com.google.common.collect.ImmutableMap) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Collection(java.util.Collection) Set(java.util.Set) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) Collectors(java.util.stream.Collectors) ReasonerUtils(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils) Sets(com.google.common.collect.Sets) Atomic(ai.grakn.graql.admin.Atomic) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) List(java.util.List) Stream(java.util.stream.Stream) Var(ai.grakn.graql.Var) Explanation(ai.grakn.graql.internal.reasoner.explanation.Explanation) Comparator(java.util.Comparator) Unifier(ai.grakn.graql.admin.Unifier) Collections(java.util.Collections) Sets(com.google.common.collect.Sets) Var(ai.grakn.graql.Var) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

Var (ai.grakn.graql.Var)100 Test (org.junit.Test)29 Answer (ai.grakn.graql.admin.Answer)28 ConceptId (ai.grakn.concept.ConceptId)26 Concept (ai.grakn.concept.Concept)25 Role (ai.grakn.concept.Role)25 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)24 HashSet (java.util.HashSet)24 Set (java.util.Set)24 VarPattern (ai.grakn.graql.VarPattern)21 IdPredicate (ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate)21 Sets (com.google.common.collect.Sets)20 Map (java.util.Map)20 Stream (java.util.stream.Stream)20 GraqlQueryException (ai.grakn.exception.GraqlQueryException)19 ImmutableSet (com.google.common.collect.ImmutableSet)19 HashMap (java.util.HashMap)19 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)18 List (java.util.List)18 SchemaConcept (ai.grakn.concept.SchemaConcept)17