Search in sources :

Example 1 with VarPattern

use of ai.grakn.graql.VarPattern 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 2 with VarPattern

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

the class PatternTest method testVarEquals.

@Test
public void testVarEquals() {
    VarPattern var1;
    VarPattern var2;
    var1 = var();
    var2 = var();
    assertTrue(var1.equals(var1));
    assertFalse(var1.equals(var2));
    var1 = var("x");
    var2 = var("y");
    assertTrue(var1.equals(var1));
    assertFalse(var1.equals(var2));
    var1 = var("x").isa("movie");
    var2 = var("x").isa("movie");
    assertTrue(var1.equals(var2));
    var1 = var("x").isa("movie").has("title", "abc");
    var2 = var("x").has("title", "abc").isa("movie");
    assertTrue(var1.equals(var2));
}
Also used : VarPattern(ai.grakn.graql.VarPattern) Test(org.junit.Test)

Example 3 with VarPattern

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

the class InstanceMapper method map.

/**
 * Map a {@link Attribute} to a var IF it is not attached in a has relation to another instance
 * @param attribute {@link Attribute} to be mapped
 * @return var patterns representing the given instance
 */
private static VarPattern map(Attribute attribute) {
    if (isHasResourceResource(attribute)) {
        return var();
    }
    VarPattern var = base(attribute);
    var = var.val(attribute.getValue());
    return var;
}
Also used : VarPattern(ai.grakn.graql.VarPattern)

Example 4 with VarPattern

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

the class SchemaConceptMapper method formatBase.

/**
 * Create a var with the information underlying all Types
 * @param schemaConcept type to be mapped
 * @return {@link VarPattern} containing basic information about the given type
 */
private static VarPattern formatBase(SchemaConcept schemaConcept) {
    VarPattern var = var().label(schemaConcept.getLabel());
    SchemaConcept superType = schemaConcept.sup();
    if (schemaConcept.sup() != null) {
        var = var.sub(Graql.label(superType.getLabel()));
    }
    if (schemaConcept.isType()) {
        Type type = schemaConcept.asType();
        var = plays(var, type);
        var = isAbstract(var, type);
    }
    return var;
}
Also used : AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) Type(ai.grakn.concept.Type) VarPattern(ai.grakn.graql.VarPattern) SchemaConcept(ai.grakn.concept.SchemaConcept)

Example 5 with VarPattern

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

the class QueryVisitor method visitPropHas.

@Override
public UnaryOperator<VarPattern> visitPropHas(GraqlParser.PropHasContext ctx) {
    Label type = visitLabel(ctx.label());
    VarPattern relation = Optional.ofNullable(ctx.relation).map(this::getVariable).orElseGet(Graql::var);
    VarPattern resource = Optional.ofNullable(ctx.resource).map(this::getVariable).orElseGet(Graql::var);
    if (ctx.predicate() != null) {
        resource = resource.val(visitPredicate(ctx.predicate()));
    }
    VarPattern finalResource = resource;
    return var -> var.has(type, finalResource, relation);
}
Also used : VarPattern(ai.grakn.graql.VarPattern) Token(org.antlr.v4.runtime.Token) Graql(ai.grakn.graql.Graql) ConnectedComponentQuery(ai.grakn.graql.analytics.ConnectedComponentQuery) UnaryOperator(java.util.function.UnaryOperator) Label(ai.grakn.concept.Label) Map(java.util.Map) ComputeQuery(ai.grakn.graql.ComputeQuery) ConceptId(ai.grakn.concept.ConceptId) Aggregate(ai.grakn.graql.Aggregate) MinQuery(ai.grakn.graql.analytics.MinQuery) Collectors.toSet(java.util.stream.Collectors.toSet) QueryBuilder(ai.grakn.graql.QueryBuilder) MaxQuery(ai.grakn.graql.analytics.MaxQuery) ImmutableSet(com.google.common.collect.ImmutableSet) AggregateQuery(ai.grakn.graql.AggregateQuery) ImmutableMap(com.google.common.collect.ImmutableMap) Graql.eq(ai.grakn.graql.Graql.eq) Collection(java.util.Collection) StringUtil(ai.grakn.util.StringUtil) Set(java.util.Set) ValuePredicate(ai.grakn.graql.ValuePredicate) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) Match(ai.grakn.graql.Match) List(java.util.List) GraqlParser(ai.grakn.graql.internal.antlr.GraqlParser) Stream(java.util.stream.Stream) LocalDate(java.time.LocalDate) Var(ai.grakn.graql.Var) DefineQuery(ai.grakn.graql.DefineQuery) Optional(java.util.Optional) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) DegreeQuery(ai.grakn.graql.analytics.DegreeQuery) DeleteQuery(ai.grakn.graql.DeleteQuery) GraqlBaseVisitor(ai.grakn.graql.internal.antlr.GraqlBaseVisitor) StdQuery(ai.grakn.graql.analytics.StdQuery) InsertQuery(ai.grakn.graql.InsertQuery) LocalDateTime(java.time.LocalDateTime) Function(java.util.function.Function) PathQuery(ai.grakn.graql.analytics.PathQuery) AttributeType(ai.grakn.concept.AttributeType) MeanQuery(ai.grakn.graql.analytics.MeanQuery) CommonUtil(ai.grakn.util.CommonUtil) SumQuery(ai.grakn.graql.analytics.SumQuery) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Graql.label(ai.grakn.graql.Graql.label) GetQuery(ai.grakn.graql.GetQuery) Query(ai.grakn.graql.Query) MedianQuery(ai.grakn.graql.analytics.MedianQuery) Graql.and(ai.grakn.graql.Graql.and) Collectors.toList(java.util.stream.Collectors.toList) Order(ai.grakn.graql.Order) NamedAggregate(ai.grakn.graql.NamedAggregate) DateTimeFormatter(java.time.format.DateTimeFormatter) CountQuery(ai.grakn.graql.analytics.CountQuery) PathsQuery(ai.grakn.graql.analytics.PathsQuery) Pattern(ai.grakn.graql.Pattern) Label(ai.grakn.concept.Label) VarPattern(ai.grakn.graql.VarPattern) Graql(ai.grakn.graql.Graql)

Aggregations

VarPattern (ai.grakn.graql.VarPattern)46 Test (org.junit.Test)28 Var (ai.grakn.graql.Var)12 Pattern (ai.grakn.graql.Pattern)11 Answer (ai.grakn.graql.admin.Answer)9 GraknTx (ai.grakn.GraknTx)8 Role (ai.grakn.concept.Role)8 Label (ai.grakn.concept.Label)7 RelationshipType (ai.grakn.concept.RelationshipType)7 Graql (ai.grakn.graql.Graql)7 Set (java.util.Set)7 ConceptId (ai.grakn.concept.ConceptId)6 EntityType (ai.grakn.concept.EntityType)6 List (java.util.List)6 Collectors.toSet (java.util.stream.Collectors.toSet)6 SchemaConcept (ai.grakn.concept.SchemaConcept)5 GraqlQueryException (ai.grakn.exception.GraqlQueryException)5 QueryBuilder (ai.grakn.graql.QueryBuilder)5 RelationPlayer (ai.grakn.graql.admin.RelationPlayer)5 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)5