Search in sources :

Example 1 with Pattern

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

the class ReasonerTest method testTwoRulesOnlyDifferingByVarNamesAreEquivalent.

@Test
public void testTwoRulesOnlyDifferingByVarNamesAreEquivalent() {
    EmbeddedGraknTx<?> tx = testGeoKB.tx();
    Rule rule1 = tx.getRule("Geo Rule");
    Pattern body2 = Graql.and(tx.graql().parser().parsePatterns("(geo-entity: $l1, entity-location: $l2) isa is-located-in;" + "(geo-entity: $l2, entity-location: $l3) isa is-located-in;"));
    Pattern head2 = Graql.and(tx.graql().parser().parsePatterns("(geo-entity: $l1, entity-location: $l3) isa is-located-in;"));
    Rule rule2 = tx.putRule("Rule 2", body2, head2);
    InferenceRule R1 = new InferenceRule(rule1, tx);
    InferenceRule R2 = new InferenceRule(rule2, tx);
    assertEquals(R1, R2);
}
Also used : Pattern(ai.grakn.graql.Pattern) Rule(ai.grakn.concept.Rule) ClassRule(org.junit.ClassRule) InferenceRule(ai.grakn.graql.internal.reasoner.rule.InferenceRule) InferenceRule(ai.grakn.graql.internal.reasoner.rule.InferenceRule) Test(org.junit.Test)

Example 2 with Pattern

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

the class PatternTest method testSimpleDisjunction.

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

Example 3 with Pattern

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

the class CWKB method buildRules.

@Override
protected void buildRules(GraknTx tx) {
    // R1: "It is a crime for an American to sell weapons to hostile nations"
    Pattern R1_LHS = tx.graql().parser().parsePattern("{" + "$x isa person;$x has nationality 'American';" + "$y isa weapon;" + "$z isa country;$z has alignment 'hostile';" + "(seller: $x, transaction-item: $y, buyer: $z) isa transaction;}");
    Pattern R1_RHS = tx.graql().parser().parsePattern("{$x isa criminal;}");
    tx.putRule("R1: It is a crime for an American to sell weapons to hostile nations", R1_LHS, R1_RHS);
    // R2: "Missiles are a kind of a weapon"
    Pattern R2_LHS = tx.graql().parser().parsePattern("{$x isa missile;}");
    Pattern R2_RHS = tx.graql().parser().parsePattern("{$x isa weapon;}");
    tx.putRule("R2: Missiles are a kind of a weapon\"", R2_LHS, R2_RHS);
    // R3: "If a country is an enemy of America then it is hostile"
    Pattern R3_LHS = tx.graql().parser().parsePattern("{$x isa country;" + "($x, $y) isa is-enemy-of;" + "$y isa country;$y has name 'America';}");
    Pattern R3_RHS = tx.graql().parser().parsePattern("{$x has alignment 'hostile';}");
    tx.putRule("R3: If a country is an enemy of America then it is hostile", R3_LHS, R3_RHS);
    // R4: "If a rocket is self-propelled and guided, it is a missile"
    Pattern R4_LHS = tx.graql().parser().parsePattern("{$x isa rocket;$x has propulsion 'gsp';}");
    Pattern R4_RHS = tx.graql().parser().parsePattern("{$x isa missile;}");
    tx.putRule("R4: If a rocket is self-propelled and guided, it is a missile", R4_LHS, R4_RHS);
    Pattern R5_LHS = tx.graql().parser().parsePattern("{$x isa person;" + "$y isa country;" + "$z isa weapon;" + "($x, $y) isa is-paid-by;" + "($y, $z) isa owns;}");
    Pattern R5_RHS = tx.graql().parser().parsePattern("{(seller: $x, buyer: $y, transaction-item: $z) isa transaction;}");
    tx.putRule("R5: If a country pays a person and that country now owns a weapon then the person has sold the country a weapon", R5_LHS, R5_RHS);
}
Also used : Pattern(ai.grakn.graql.Pattern)

Example 4 with Pattern

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

the class TxObserver method putRule.

private void putRule(PutRule putRule) {
    Label label = GrpcUtil.convert(putRule.getLabel());
    Pattern when = GrpcUtil.convert(putRule.getWhen());
    Pattern then = GrpcUtil.convert(putRule.getThen());
    Rule rule = tx().putRule(label, when, then);
    responseObserver.onNext(GrpcUtil.conceptResponse(rule));
}
Also used : Pattern(ai.grakn.graql.Pattern) Label(ai.grakn.concept.Label) Rule(ai.grakn.concept.Rule) PutRule(ai.grakn.rpc.generated.GrpcGrakn.PutRule)

Example 5 with Pattern

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

the class RemoteGraknTxTest method whenPuttingRule_EnsureCorrectRequestIsSent.

@Test
public void whenPuttingRule_EnsureCorrectRequestIsSent() {
    ConceptId id = ConceptId.of(V123.getValue());
    Label label = Label.of("foo");
    Pattern when = var("x").isa("person");
    Pattern then = var("y").isa("person");
    try (RemoteGraknTx tx = RemoteGraknTx.create(session, GrpcUtil.openRequest(KEYSPACE, GraknTxType.READ))) {
        // The open request
        verify(server.requests()).onNext(any());
        Concept concept = RemoteConcepts.createRule(tx, id);
        server.setResponse(GrpcUtil.putRuleRequest(label, when, then), GrpcUtil.conceptResponse(concept));
        assertEquals(concept, tx.putRule(label, when, then));
    }
}
Also used : GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) Concept(ai.grakn.concept.Concept) Pattern(ai.grakn.graql.Pattern) Label(ai.grakn.concept.Label) ConceptId(ai.grakn.concept.ConceptId) 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