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);
}
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);
}
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);
}
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));
}
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;
}
Aggregations