use of ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom 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.graql.internal.reasoner.atom.binary.ResourceAtom in project grakn by graknlabs.
the class AtomicTest method testUnification_ResourceWithIndirectValuePredicate.
@Test
public void testUnification_ResourceWithIndirectValuePredicate() {
EmbeddedGraknTx<?> graph = unificationTestSet.tx();
String resource = "{$x has resource $r;$r val 'f';}";
String resource2 = "{$r has resource $x;$x val 'f';}";
String resource3 = "{$r has resource 'f';}";
ReasonerAtomicQuery resourceQuery = ReasonerQueries.atomic(conjunction(resource, graph), graph);
ReasonerAtomicQuery resourceQuery2 = ReasonerQueries.atomic(conjunction(resource2, graph), graph);
ReasonerAtomicQuery resourceQuery3 = ReasonerQueries.atomic(conjunction(resource3, graph), graph);
String type = "{$x isa resource;$x id '" + resourceQuery.getQuery().execute().iterator().next().get("r").getId().getValue() + "';}";
ReasonerAtomicQuery typeQuery = ReasonerQueries.atomic(conjunction(type, graph), graph);
Atom typeAtom = typeQuery.getAtom();
Atom resourceAtom = resourceQuery.getAtom();
Atom resourceAtom2 = resourceQuery2.getAtom();
Atom resourceAtom3 = resourceQuery3.getAtom();
Unifier unifier = resourceAtom.getUnifier(typeAtom);
Unifier unifier2 = resourceAtom2.getUnifier(typeAtom);
Unifier unifier3 = resourceAtom3.getUnifier(typeAtom);
Answer typeAnswer = typeQuery.getQuery().execute().iterator().next();
Answer resourceAnswer = resourceQuery.getQuery().execute().iterator().next();
Answer resourceAnswer2 = resourceQuery2.getQuery().execute().iterator().next();
Answer resourceAnswer3 = resourceQuery3.getQuery().execute().iterator().next();
assertEquals(typeAnswer.get(var("x")), resourceAnswer.unify(unifier).get(var("x")));
assertEquals(typeAnswer.get(var("x")), resourceAnswer2.unify(unifier2).get(var("x")));
assertEquals(typeAnswer.get(var("x")), resourceAnswer3.unify(unifier3).get(var("x")));
}
Aggregations