Search in sources :

Example 1 with Var

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

the class QueryAnswerStream method knownFilterWithInverse.

static boolean knownFilterWithInverse(Answer answer, Map<Pair<Var, Concept>, Set<Answer>> stream2InverseMap) {
    Iterator<Map.Entry<Var, Concept>> eit = answer.entrySet().iterator();
    Map.Entry<Var, Concept> entry = eit.next();
    Set<Answer> matchAnswers = findMatchingAnswers(entry.getKey(), entry.getValue(), stream2InverseMap);
    while (eit.hasNext()) {
        entry = eit.next();
        matchAnswers = Sets.intersection(matchAnswers, findMatchingAnswers(entry.getKey(), entry.getValue(), stream2InverseMap));
    }
    for (Answer knownAnswer : matchAnswers) {
        if (knownAnswer.entrySet().containsAll(answer.entrySet())) {
            return false;
        }
    }
    return true;
}
Also used : Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) Answer(ai.grakn.graql.admin.Answer) Var(ai.grakn.graql.Var) Map(java.util.Map)

Example 2 with Var

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

the class BenchmarkTests method nonRecursiveChainOfRules.

/**
 * Executes a scalability test defined in terms of the number of rules in the system. Creates a simple rule chain:
 *
 * R_i(x, y) := R_{i-1}(x, y);     i e [1, N]
 *
 * with a single initial relation instance R_0(a ,b)
 */
@Test
public void nonRecursiveChainOfRules() {
    final int N = 200;
    LOG.debug(new Object() {
    }.getClass().getEnclosingMethod().getName());
    GraknSession graknSession = sessionContext.newSession();
    // NB: loading data here as defining it as KB and using graql api leads to circular dependencies
    try (GraknTx tx = graknSession.open(GraknTxType.WRITE)) {
        Role fromRole = tx.putRole("fromRole");
        Role toRole = tx.putRole("toRole");
        RelationshipType relation0 = tx.putRelationshipType("relation0").relates(fromRole).relates(toRole);
        for (int i = 1; i <= N; i++) {
            tx.putRelationshipType("relation" + i).relates(fromRole).relates(toRole);
        }
        EntityType genericEntity = tx.putEntityType("genericEntity").plays(fromRole).plays(toRole);
        Entity fromEntity = genericEntity.addEntity();
        Entity toEntity = genericEntity.addEntity();
        relation0.addRelationship().addRolePlayer(fromRole, fromEntity).addRolePlayer(toRole, toEntity);
        for (int i = 1; i <= N; i++) {
            Var fromVar = 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()), toVar).isa("relation" + (i - 1)))).then(Graql.and(Graql.var().rel(Graql.label(fromRole.getLabel()), fromVar).rel(Graql.label(toRole.getLabel()), toVar).isa("relation" + i)));
            tx.graql().define(rulePattern).execute();
        }
        tx.commit();
    }
    try (GraknTx tx = graknSession.open(GraknTxType.READ)) {
        final long limit = 1;
        String queryPattern = "(fromRole: $x, toRole: $y) isa relation" + N + ";";
        String queryString = "match " + queryPattern + " get;";
        String limitedQueryString = "match " + queryPattern + "limit " + limit + ";" + "get;";
        assertEquals(executeQuery(queryString, tx, "full").size(), limit);
        assertEquals(executeQuery(limitedQueryString, tx, "limit").size(), limit);
    }
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) Var(ai.grakn.graql.Var) GraknSession(ai.grakn.GraknSession) RelationshipType(ai.grakn.concept.RelationshipType) VarPattern(ai.grakn.graql.VarPattern) Test(org.junit.Test)

Example 3 with Var

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

the class AtomicTest method testRoleInference_RelationHasVerticalRoleHierarchy.

// relation relates a single role so instead of assigning metarole this role should be assigned
@Test
public void testRoleInference_RelationHasVerticalRoleHierarchy() {
    EmbeddedGraknTx<?> graph = ruleApplicabilitySet.tx();
    String relationString = "{($x, $y) isa reifying-relation;}";
    RelationshipAtom relation = (RelationshipAtom) ReasonerQueries.atomic(conjunction(relationString, graph), graph).getAtom();
    ImmutableSetMultimap<Role, Var> roleMap = ImmutableSetMultimap.of(graph.getRole("role1"), var("x"), graph.getRole("role1"), var("y"));
    assertEquals(roleMap, roleSetMap(relation.getRoleVarMap()));
}
Also used : Role(ai.grakn.concept.Role) Var(ai.grakn.graql.Var) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) Test(org.junit.Test)

Example 4 with Var

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

the class AtomicTest method testRuleApplicability_AmbiguousRoleMapping.

@Test
public void testRuleApplicability_AmbiguousRoleMapping() {
    EmbeddedGraknTx<?> graph = ruleApplicabilitySet.tx();
    // although singleRoleEntity plays only one role it can also play an implicit role of the resource so mapping ambiguous
    String relationString = "{($x, $y, $z);$x isa singleRoleEntity; $y isa anotherTwoRoleEntity; $z isa twoRoleEntity;}";
    RelationshipAtom relation = (RelationshipAtom) ReasonerQueries.atomic(conjunction(relationString, graph), graph).getAtom();
    ImmutableSetMultimap<Role, Var> roleMap = ImmutableSetMultimap.of(graph.getRole("role"), var("x"), graph.getRole("role"), var("y"), graph.getRole("role"), var("z"));
    assertEquals(roleMap, roleSetMap((relation.getRoleVarMap())));
    assertEquals(3, relation.getApplicableRules().count());
}
Also used : Role(ai.grakn.concept.Role) Var(ai.grakn.graql.Var) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) Test(org.junit.Test)

Example 5 with Var

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

the class MatchModifierTest method testLimitQuery.

@Test
public void testLimitQuery() {
    Var t = var("t");
    Match match = qb.match(x.isa("movie").has("title", t)).orderBy(t, asc).offset(1).limit(3);
    assertResultsOrderedByValue(match, t, true);
    assertEquals(3, match.stream().count());
}
Also used : Var(ai.grakn.graql.Var) Match(ai.grakn.graql.Match) Test(org.junit.Test)

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