use of ai.grakn.graql.internal.reasoner.UnifierImpl in project grakn by graknlabs.
the class ReasonerAtomicQuery method getMultiUnifier.
/**
* @throws IllegalArgumentException if passed a {@link ReasonerQuery} that is not a {@link ReasonerAtomicQuery}.
*/
@Override
public MultiUnifier getMultiUnifier(ReasonerQuery p, UnifierComparison unifierType) {
if (p == this)
return new MultiUnifierImpl();
Preconditions.checkArgument(p instanceof ReasonerAtomicQuery);
ReasonerAtomicQuery parent = (ReasonerAtomicQuery) p;
MultiUnifier multiUnifier = this.getAtom().getMultiUnifier(parent.getAtom(), unifierType);
Set<TypeAtom> childTypes = this.getAtom().getTypeConstraints().collect(Collectors.toSet());
if (childTypes.isEmpty())
return multiUnifier;
// get corresponding type unifiers
Set<TypeAtom> parentTypes = parent.getAtom().getTypeConstraints().collect(Collectors.toSet());
if (multiUnifier.isEmpty())
return new MultiUnifierImpl(typeUnifier(childTypes, parentTypes, new UnifierImpl()));
Set<Unifier> unifiers = multiUnifier.unifiers().stream().map(unifier -> typeUnifier(childTypes, parentTypes, unifier)).collect(Collectors.toSet());
return new MultiUnifierImpl(unifiers);
}
use of ai.grakn.graql.internal.reasoner.UnifierImpl in project grakn by graknlabs.
the class SNBInferenceTest method testQueryConsistency.
/**
* Tests transitivity and Bug #7416
*/
@Test
public void testQueryConsistency() {
QueryBuilder iqb = snbGraph.tx().graql().infer(true);
String queryString = "match $x isa person; $y isa place; ($x, $y) isa resides;" + "$z isa person;$z has name 'Miguel Gonzalez'; ($x, $z) isa knows; get $x, $y;";
String queryString2 = "match $x isa person; $y isa person;$y has name 'Miguel Gonzalez';" + "$z isa place; ($x, $y) isa knows; ($x, $z) isa resides; get $x, $z;";
Unifier unifier = new UnifierImpl(ImmutableMap.of(Graql.var("z"), Graql.var("y")));
List<Answer> answers = iqb.materialise(false).<GetQuery>parse(queryString).execute();
List<Answer> answers2 = iqb.materialise(false).<GetQuery>parse(queryString2).execute().stream().map(a -> a.unify(unifier)).collect(Collectors.toList());
assertCollectionsEqual(answers, answers2);
}
use of ai.grakn.graql.internal.reasoner.UnifierImpl in project grakn by graknlabs.
the class Binary method getUnifier.
@Override
public Unifier getUnifier(Atom parentAtom) {
if (!(parentAtom instanceof Binary)) {
throw GraqlQueryException.unificationAtomIncompatibility();
}
Multimap<Var, Var> varMappings = HashMultimap.create();
Var childVarName = this.getVarName();
Var parentVarName = parentAtom.getVarName();
Var childPredicateVarName = this.getPredicateVariable();
Var parentPredicateVarName = parentAtom.getPredicateVariable();
if (parentVarName.isUserDefinedName() && childVarName.isUserDefinedName() && !childVarName.equals(parentVarName)) {
varMappings.put(childVarName, parentVarName);
}
if (parentPredicateVarName.isUserDefinedName() && childPredicateVarName.isUserDefinedName() && !childPredicateVarName.equals(parentPredicateVarName)) {
varMappings.put(childPredicateVarName, parentPredicateVarName);
}
return new UnifierImpl(varMappings);
}
use of ai.grakn.graql.internal.reasoner.UnifierImpl in project grakn by graknlabs.
the class RelationshipAtom method getMultiUnifier.
@Override
public MultiUnifier getMultiUnifier(Atom pAtom, UnifierComparison unifierType) {
Unifier baseUnifier = super.getUnifier(pAtom);
Set<Unifier> unifiers = new HashSet<>();
if (pAtom.isRelation()) {
// This is safe due to the check above
assert (pAtom instanceof RelationshipAtom);
RelationshipAtom parentAtom = (RelationshipAtom) pAtom;
// this is important for cases like unifying ($r1: $x, $r2: $y) with itself
if (this.equals(parentAtom) && this.getPartialSubstitutions().collect(toSet()).equals(parentAtom.getPartialSubstitutions().collect(toSet())) && this.getTypeConstraints().collect(toSet()).equals(parentAtom.getTypeConstraints().collect(toSet()))) {
return new MultiUnifierImpl();
}
boolean unifyRoleVariables = parentAtom.getRelationPlayers().stream().map(RelationPlayer::getRole).flatMap(CommonUtil::optionalToStream).anyMatch(rp -> rp.var().isUserDefinedName());
getRelationPlayerMappings(parentAtom, unifierType).forEach(mappingList -> {
Multimap<Var, Var> varMappings = HashMultimap.create();
mappingList.forEach(rpm -> {
// add role player mapping
varMappings.put(rpm.getKey().getRolePlayer().var(), rpm.getValue().getRolePlayer().var());
// add role var mapping if needed
VarPattern childRolePattern = rpm.getKey().getRole().orElse(null);
VarPattern parentRolePattern = rpm.getValue().getRole().orElse(null);
if (parentRolePattern != null && childRolePattern != null && unifyRoleVariables) {
varMappings.put(childRolePattern.admin().var(), parentRolePattern.admin().var());
}
});
unifiers.add(baseUnifier.merge(new UnifierImpl(varMappings)));
});
} else {
unifiers.add(baseUnifier);
}
return new MultiUnifierImpl(unifiers);
}
use of ai.grakn.graql.internal.reasoner.UnifierImpl in project grakn by graknlabs.
the class ResourceAtom method getUnifier.
@Override
public Unifier getUnifier(Atom parentAtom) {
if (!(parentAtom instanceof ResourceAtom)) {
return new UnifierImpl(ImmutableMap.of(this.getPredicateVariable(), parentAtom.getVarName()));
}
Unifier unifier = super.getUnifier(parentAtom);
ResourceAtom parent = (ResourceAtom) parentAtom;
// unify relation vars
Var childRelationVarName = this.getRelationVariable();
Var parentRelationVarName = parent.getRelationVariable();
if (parentRelationVarName.isUserDefinedName() && !childRelationVarName.equals(parentRelationVarName)) {
unifier = unifier.merge(new UnifierImpl(ImmutableMap.of(childRelationVarName, parentRelationVarName)));
}
return unifier;
}
Aggregations