Search in sources :

Example 1 with Rule

use of ai.grakn.concept.Rule 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 Rule

use of ai.grakn.concept.Rule in project grakn by graknlabs.

the class InferenceRule method propagateConstraints.

/**
 * @param parentAtom atom containing constraints (parent)
 * @param unifier unifier unifying parent with the rule
 * @return rule with propagated constraints from parent
 */
public InferenceRule propagateConstraints(Atom parentAtom, Unifier unifier) {
    if (!parentAtom.isRelation() && !parentAtom.isResource())
        return this;
    Atom headAtom = head.getAtom();
    Set<Atomic> bodyAtoms = new HashSet<>(body.getAtoms());
    // transfer value predicates
    parentAtom.getPredicates(ValuePredicate.class).flatMap(vp -> vp.unify(unifier).stream()).forEach(bodyAtoms::add);
    // if head is a resource merge vps into head
    if (headAtom.isResource() && ((ResourceAtom) headAtom).getMultiPredicate().isEmpty()) {
        ResourceAtom resourceHead = (ResourceAtom) headAtom;
        Set<ValuePredicate> innerVps = parentAtom.getInnerPredicates(ValuePredicate.class).flatMap(vp -> vp.unify(unifier).stream()).peek(bodyAtoms::add).collect(toSet());
        headAtom = ResourceAtom.create(headAtom.getPattern(), headAtom.getPredicateVariable(), resourceHead.getRelationVariable(), resourceHead.getTypeId(), innerVps, headAtom.getParentQuery());
    }
    Set<TypeAtom> unifiedTypes = parentAtom.getTypeConstraints().flatMap(type -> type.unify(unifier).stream()).collect(toSet());
    // set rule body types to sub types of combined query+rule types
    Set<TypeAtom> ruleTypes = body.getAtoms(TypeAtom.class).filter(t -> !t.isRelation()).collect(toSet());
    Set<TypeAtom> allTypes = Sets.union(unifiedTypes, ruleTypes);
    allTypes.stream().filter(ta -> {
        SchemaConcept schemaConcept = ta.getSchemaConcept();
        SchemaConcept subType = allTypes.stream().map(Atom::getSchemaConcept).filter(Objects::nonNull).filter(t -> ReasonerUtils.supers(t).contains(schemaConcept)).findFirst().orElse(null);
        return schemaConcept == null || subType == null;
    }).forEach(t -> bodyAtoms.add(t.copy(body)));
    return new InferenceRule(ReasonerQueries.atomic(headAtom), ReasonerQueries.create(bodyAtoms, tx), ruleId, tx);
}
Also used : PatternAdmin(ai.grakn.graql.admin.PatternAdmin) UnifierType(ai.grakn.graql.internal.reasoner.UnifierType) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) SchemaConcept(ai.grakn.concept.SchemaConcept) Answer(ai.grakn.graql.admin.Answer) QueryStateBase(ai.grakn.graql.internal.reasoner.state.QueryStateBase) Rule(ai.grakn.concept.Rule) ResolutionState(ai.grakn.graql.internal.reasoner.state.ResolutionState) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ConceptId(ai.grakn.concept.ConceptId) Collectors.toSet(java.util.stream.Collectors.toSet) QueryCache(ai.grakn.graql.internal.reasoner.cache.QueryCache) Patterns(ai.grakn.graql.internal.pattern.Patterns) ValuePredicate(ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate) Conjunction(ai.grakn.graql.admin.Conjunction) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Set(java.util.Set) ReasonerUtils(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) Atomic(ai.grakn.graql.admin.Atomic) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) List(java.util.List) ReasonerQueries(ai.grakn.graql.internal.reasoner.query.ReasonerQueries) RuleState(ai.grakn.graql.internal.reasoner.state.RuleState) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Var(ai.grakn.graql.Var) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Unifier(ai.grakn.graql.admin.Unifier) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) Atomic(ai.grakn.graql.admin.Atomic) SchemaConcept(ai.grakn.concept.SchemaConcept) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) ValuePredicate(ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate) Objects(java.util.Objects) HashSet(java.util.HashSet)

Example 3 with Rule

use of ai.grakn.concept.Rule in project grakn by graknlabs.

the class GraknTxPutPropertyTest method whenCallingPutRuleWithAnExistingRuleTypeLabel_ItReturnsThatType.

@Property
public void whenCallingPutRuleWithAnExistingRuleTypeLabel_ItReturnsThatType(@Open GraknTx tx, @FromTx Rule rule) {
    Rule newType = tx.putRule(rule.getLabel(), tx.graql().parser().parsePattern("$x"), tx.graql().parser().parsePattern("$x"));
    assertEquals(rule, newType);
}
Also used : Rule(ai.grakn.concept.Rule) Property(com.pholser.junit.quickcheck.Property)

Example 4 with Rule

use of ai.grakn.concept.Rule 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 Rule

use of ai.grakn.concept.Rule in project grakn by graknlabs.

the class RuleUtils method subGraphIsCyclical.

/**
 * @param rules set of rules of interest forming a rule subgraph
 * @param graph of interest
 * @return true if the rule subgraph formed from provided rules contains loops
 */
public static boolean subGraphIsCyclical(Set<InferenceRule> rules, GraknTx graph) {
    Iterator<Rule> ruleIterator = rules.stream().map(r -> graph.<Rule>getConcept(r.getRuleId())).iterator();
    boolean cyclical = false;
    while (ruleIterator.hasNext() && !cyclical) {
        Set<Rule> visitedRules = new HashSet<>();
        Stack<Rule> rulesToVisit = new Stack<>();
        rulesToVisit.push(ruleIterator.next());
        while (!rulesToVisit.isEmpty() && !cyclical) {
            Rule rule = rulesToVisit.pop();
            if (!visitedRules.contains(rule)) {
                rule.getConclusionTypes().flatMap(SchemaConcept::getRulesOfHypothesis).forEach(rulesToVisit::add);
                visitedRules.add(rule);
            } else {
                cyclical = true;
            }
        }
    }
    return cyclical;
}
Also used : HashSet(java.util.HashSet) Stream(java.util.stream.Stream) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) Equivalence(com.google.common.base.Equivalence) Iterator(java.util.Iterator) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) GraknTx(ai.grakn.GraknTx) SchemaConcept(ai.grakn.concept.SchemaConcept) Set(java.util.Set) Schema(ai.grakn.util.Schema) Rule(ai.grakn.concept.Rule) Stack(java.util.Stack) Rule(ai.grakn.concept.Rule) HashSet(java.util.HashSet) Stack(java.util.Stack)

Aggregations

Rule (ai.grakn.concept.Rule)18 SchemaConcept (ai.grakn.concept.SchemaConcept)7 Pattern (ai.grakn.graql.Pattern)6 HashSet (java.util.HashSet)6 Set (java.util.Set)6 Test (org.junit.Test)6 Stream (java.util.stream.Stream)5 GraknTx (ai.grakn.GraknTx)4 Label (ai.grakn.concept.Label)4 Atomic (ai.grakn.graql.admin.Atomic)4 Schema (ai.grakn.util.Schema)4 Role (ai.grakn.concept.Role)3 Conjunction (ai.grakn.graql.admin.Conjunction)3 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)3 Atom (ai.grakn.graql.internal.reasoner.atom.Atom)3 ReasonerQueryImpl (ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl)3 ErrorMessage (ai.grakn.util.ErrorMessage)3 Collectors (java.util.stream.Collectors)3 Attribute (ai.grakn.concept.Attribute)2 Relationship (ai.grakn.concept.Relationship)2