use of com.vaticle.typedb.core.traversal.common.Identifier.Variable.Retrievable in project grakn by graknlabs.
the class Unifier method reverse.
private Map<Variable, Set<Retrievable>> reverse(Map<Retrievable, Set<Variable>> unifier) {
Map<Variable, Set<Retrievable>> reverse = new HashMap<>();
unifier.forEach((unify, unifieds) -> {
for (Variable unified : unifieds) {
reverse.computeIfAbsent(unified, (u) -> new HashSet<>()).add(unify);
}
});
return reverse;
}
use of com.vaticle.typedb.core.traversal.common.Identifier.Variable.Retrievable in project grakn by graknlabs.
the class ExplanationTest method applyMapping.
private ConceptMap applyMapping(Map<Retrievable, Set<Retrievable>> mapping, ConceptMap completeMap) {
Map<Retrievable, Concept> concepts = new HashMap<>();
mapping.forEach((from, tos) -> {
assertTrue(completeMap.contains(from));
Concept concept = completeMap.get(from);
tos.forEach(mapped -> {
assertTrue(!concepts.containsKey(mapped) || concepts.get(mapped).equals(concept));
concepts.put(mapped, concept);
});
});
return new ConceptMap(concepts);
}
use of com.vaticle.typedb.core.traversal.common.Identifier.Variable.Retrievable in project grakn by graknlabs.
the class Mapping method unTransform.
public ConceptMap unTransform(ConceptMap conceptMap) {
assert reverseMapping.size() == conceptMap.concepts().size();
Map<Retrievable, Concept> transformed = new HashMap<>();
for (Map.Entry<Retrievable, ? extends Concept> entry : conceptMap.concepts().entrySet()) {
Retrievable id = entry.getKey();
assert reverseMapping.containsKey(id);
Concept concept = entry.getValue();
transformed.put(reverseMapping.get(id), concept);
}
// we ignore explainables because they can't be mapped here
return new ConceptMap(transformed);
}
use of com.vaticle.typedb.core.traversal.common.Identifier.Variable.Retrievable in project grakn by graknlabs.
the class Unifier method unUnify.
/**
* Un-unify a map of concepts, with given identifiers. These must include anonymous and labelled concepts,
* as they may be mapped to from a named variable, and may have requirements that need to be met.
*/
public FunctionalIterator<ConceptMap> unUnify(Map<Variable, Concept> concepts, Requirements.Instance instanceRequirements) {
if (!unifiedRequirements.exactlySatisfiedBy(concepts))
return Iterators.empty();
Map<Retrievable, Concept> reversedConcepts = new HashMap<>();
for (Map.Entry<Variable, Set<Retrievable>> entry : reverseUnifier.entrySet()) {
Variable toReverse = entry.getKey();
Set<Retrievable> reversed = entry.getValue();
if (!concepts.containsKey(toReverse)) {
throw TypeDBException.of(REVERSE_UNIFICATION_MISSING_CONCEPT, toReverse, concepts);
}
Concept concept = concepts.get(toReverse);
for (Retrievable r : reversed) {
if (!reversedConcepts.containsKey(r))
reversedConcepts.put(r, concept);
if (!reversedConcepts.get(r).equals(concept))
return Iterators.empty();
}
}
if (instanceRequirements.satisfiedBy(reversedConcepts))
return cartesianUnrestrictedNamedTypes(reversedConcepts, instanceRequirements);
else
return Iterators.empty();
}
use of com.vaticle.typedb.core.traversal.common.Identifier.Variable.Retrievable in project grakn by graknlabs.
the class Unifier method unify.
/*
Returns a best-effort forward unification. It may produce an empty concept map, or a not-present Optional.
An empty concept map means that none of the concepts were required by this unifier.
Eg. unifier { b -> x, c -> y}.unify({a = Attr(0x10}) -> Optional.of({})
However, a not-present Optional may be produced:
Eg. unifier { b -> x, c -> x}.unify({b = Attr(0x10), c = Attr(0x20)}) -> Optional.empty()
the latter will never be valid as it is a contradiction, the former empty map is the result of the unifier's filtering
*/
public Optional<Pair<ConceptMap, Requirements.Instance>> unify(ConceptMap conceptMap) {
Map<Retrievable, Concept> unifiedMap = new HashMap<>();
if (requirements.contradicts(conceptMap))
return Optional.empty();
for (Map.Entry<Retrievable, Set<Variable>> entry : unifier.entrySet()) {
Retrievable toUnify = entry.getKey();
Concept concept = conceptMap.get(toUnify.asRetrievable());
if (concept == null)
continue;
for (Variable unified : entry.getValue()) {
if (unified.isRetrievable()) {
if (!unifiedMap.containsKey(unified.asRetrievable())) {
unifiedMap.put(unified.asRetrievable(), concept);
}
if (!unifiedMap.get(unified.asRetrievable()).equals(concept))
return Optional.empty();
}
}
}
return Optional.of(new Pair<>(new ConceptMap(unifiedMap), new Requirements.Instance(conceptMap.concepts())));
}
Aggregations