Search in sources :

Example 16 with Pattern

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

the class RuleTest method whenCreatingDistinctRulesWithSimilarStringHashes_EnsureRulesDoNotClash.

@Test
public void whenCreatingDistinctRulesWithSimilarStringHashes_EnsureRulesDoNotClash() {
    String productRefused = "productRefused";
    Pattern when1 = graknTx.graql().parser().parsePattern("{$step has step-id 9; $e (process-case: $case) isa process-record; $case has consent false;}");
    Pattern then1 = graknTx.graql().parser().parsePattern("{(record: $e, step: $step) isa record-step;}");
    Rule rule1 = graknTx.putRule(productRefused, when1, then1);
    String productAccepted = "productAccepted";
    Pattern when2 = graknTx.graql().parser().parsePattern("{$step has step-id 7; $e (process-case: $case) isa process-record; $case has consent true;}");
    Pattern then2 = graknTx.graql().parser().parsePattern("{(record: $e, step: $step) isa record-step;}");
    Rule rule2 = graknTx.putRule(productAccepted, when2, then2);
    assertEquals(rule1, graknTx.getRule(productRefused));
    assertEquals(rule2, graknTx.getRule(productAccepted));
}
Also used : Pattern(ai.grakn.graql.Pattern) Rule(ai.grakn.concept.Rule) Test(org.junit.Test)

Example 17 with Pattern

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

the class CWInferenceTest method testGraphCase.

@Test
public void testGraphCase() {
    EmbeddedGraknTx<?> tx = cwKB2.tx();
    QueryBuilder lqb = tx.graql().infer(false);
    QueryBuilder ilqb = tx.graql().infer(true);
    tx.putEntityType("region");
    Pattern R6_LHS = and(tx.graql().parser().parsePatterns("$x isa region;"));
    Pattern R6_RHS = and(tx.graql().parser().parsePatterns("$x isa country;"));
    tx.putRule("R6: If something is a region it is a country", R6_LHS, R6_RHS);
    tx.commitSubmitNoLogs();
    String queryString = "match $p isa criminal; get;";
    String explicitQuery = "match $p isa person has name 'colonelWest'; get;";
    // Reopen transaction
    cwKB2.tx();
    assertQueriesEqual(ilqb.parse(queryString), lqb.parse(explicitQuery));
}
Also used : Pattern(ai.grakn.graql.Pattern) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 18 with Pattern

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

the class ConceptBuilder method tryPutConcept.

private Concept tryPutConcept() {
    usedParams.clear();
    Concept concept;
    if (has(IS_ROLE)) {
        use(IS_ROLE);
        Label label = use(LABEL);
        Role role = executor.tx().putRole(label);
        if (has(SUPER_CONCEPT)) {
            setSuper(role, use(SUPER_CONCEPT));
        }
        concept = role;
    } else if (has(IS_RULE)) {
        use(IS_RULE);
        Label label = use(LABEL);
        Pattern when = use(WHEN);
        Pattern then = use(THEN);
        Rule rule = executor.tx().putRule(label, when, then);
        if (has(SUPER_CONCEPT)) {
            setSuper(rule, use(SUPER_CONCEPT));
        }
        concept = rule;
    } else if (has(SUPER_CONCEPT)) {
        concept = putSchemaConcept();
    } else if (has(TYPE)) {
        concept = putInstance();
    } else {
        throw GraqlQueryException.insertUndefinedVariable(executor.printableRepresentation(var));
    }
    // Check for any unexpected parameters
    preProvidedParams.forEach((param, value) -> {
        if (!usedParams.contains(param)) {
            throw GraqlQueryException.insertUnexpectedProperty(param.name(), value, concept);
        }
    });
    return concept;
}
Also used : Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) Role(ai.grakn.concept.Role) Pattern(ai.grakn.graql.Pattern) Label(ai.grakn.concept.Label) Rule(ai.grakn.concept.Rule)

Example 19 with Pattern

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

the class RelationshipConverter method pattern.

public Pattern pattern(Relationship concept) {
    VarPattern relationPattern = Graql.var();
    Set<Pattern> idPatterns = new HashSet<>();
    for (Map.Entry<Role, Set<Thing>> entry : concept.allRolePlayers().entrySet()) {
        for (Thing var : entry.getValue()) {
            Var rolePlayer = Graql.var();
            relationPattern = relationPattern.rel(Graql.label(entry.getKey().getLabel()), rolePlayer);
            idPatterns.add(rolePlayer.asUserDefined().id(var.getId()));
        }
    }
    relationPattern = relationPattern.isa(Graql.label(concept.type().getLabel()));
    Pattern pattern = relationPattern;
    for (Pattern idPattern : idPatterns) {
        pattern = pattern.and(idPattern);
    }
    return pattern;
}
Also used : Role(ai.grakn.concept.Role) VarPattern(ai.grakn.graql.VarPattern) Pattern(ai.grakn.graql.Pattern) HashSet(java.util.HashSet) Set(java.util.Set) Var(ai.grakn.graql.Var) VarPattern(ai.grakn.graql.VarPattern) Map(java.util.Map) Thing(ai.grakn.concept.Thing) HashSet(java.util.HashSet)

Example 20 with Pattern

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

the class PatternTest method testSimpleConjunction.

@Test
public void testSimpleConjunction() {
    Pattern conjunction = and();
    assertFalse(conjunction.admin().isVarPattern());
    assertFalse(conjunction.admin().isDisjunction());
    assertTrue(conjunction.admin().isConjunction());
    // noinspection AssertEqualsBetweenInconvertibleTypes
    assertEquals(conjunction.admin(), conjunction.admin().asConjunction());
}
Also used : VarPattern(ai.grakn.graql.VarPattern) Pattern(ai.grakn.graql.Pattern) Test(org.junit.Test)

Aggregations

Pattern (ai.grakn.graql.Pattern)35 Test (org.junit.Test)24 VarPattern (ai.grakn.graql.VarPattern)12 Fragment (ai.grakn.graql.internal.gremlin.fragment.Fragment)9 InIsaFragment (ai.grakn.graql.internal.gremlin.fragment.InIsaFragment)9 LabelFragment (ai.grakn.graql.internal.gremlin.fragment.LabelFragment)9 NeqFragment (ai.grakn.graql.internal.gremlin.fragment.NeqFragment)9 OutIsaFragment (ai.grakn.graql.internal.gremlin.fragment.OutIsaFragment)9 Role (ai.grakn.concept.Role)5 Rule (ai.grakn.concept.Rule)5 GraknTx (ai.grakn.GraknTx)4 Label (ai.grakn.concept.Label)4 ConceptId (ai.grakn.concept.ConceptId)3 RelationshipType (ai.grakn.concept.RelationshipType)3 SchemaConcept (ai.grakn.concept.SchemaConcept)3 InsertQuery (ai.grakn.graql.InsertQuery)3 Var (ai.grakn.graql.Var)3 Iterables (com.google.common.collect.Iterables)3 Grakn (ai.grakn.Grakn)2 GraknSession (ai.grakn.GraknSession)2