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())));
}
use of com.vaticle.typedb.core.traversal.common.Identifier.Variable.Retrievable in project grakn by graknlabs.
the class Unifier method cartesianUnrestrictedNamedTypes.
private static FunctionalIterator<ConceptMap> cartesianUnrestrictedNamedTypes(Map<Retrievable, Concept> initialConcepts, Requirements.Instance instanceRequirements) {
Map<Retrievable, Concept> fixedConcepts = new HashMap<>();
List<Variable.Name> namedTypeNames = new ArrayList<>();
List<FunctionalIterator<Type>> namedTypeSupers = new ArrayList<>();
initialConcepts.forEach((id, concept) -> {
if (id.isName() && concept.isType() && !instanceRequirements.hasRestriction(id)) {
namedTypeNames.add(id.asName());
namedTypeSupers.add(concept.asType().getSupertypes().map(t -> t));
} else
fixedConcepts.put(id, concept);
});
if (namedTypeNames.isEmpty()) {
return Iterators.single(new ConceptMap(fixedConcepts));
} else {
return Iterators.cartesian(namedTypeSupers).map(permutation -> {
Map<Retrievable, Concept> concepts = new HashMap<>(fixedConcepts);
for (int i = 0; i < permutation.size(); i++) {
concepts.put(namedTypeNames.get(i), permutation.get(i));
}
return new ConceptMap(concepts);
});
}
}
use of com.vaticle.typedb.core.traversal.common.Identifier.Variable.Retrievable in project grakn by graknlabs.
the class Mapping method transform.
public ConceptMap transform(ConceptMap conceptMap) {
Map<Retrievable, Concept> transformed = new HashMap<>();
for (Map.Entry<Retrievable, ? extends Concept> entry : conceptMap.concepts().entrySet()) {
Retrievable id = entry.getKey();
Retrievable mapped = mapping.get(id);
if (mapped != null) {
Concept concept = entry.getValue();
transformed.put(mapped, concept);
}
}
return new ConceptMap(transformed);
}
use of com.vaticle.typedb.core.traversal.common.Identifier.Variable.Retrievable in project grakn by graknlabs.
the class Resolver method compatibleBounds.
private Optional<ConceptMap> compatibleBounds(Conjunction conjunction, ConceptMap bounds) {
Map<Retrievable, Concept> newBounds = new HashMap<>();
for (Map.Entry<Retrievable, ? extends Concept> entry : bounds.concepts().entrySet()) {
Retrievable id = entry.getKey();
Concept bound = entry.getValue();
Variable conjVariable = conjunction.variable(id);
assert conjVariable != null;
if (conjVariable.isThing()) {
if (!conjVariable.asThing().iid().isPresent())
newBounds.put(id, bound);
else if (!conjVariable.asThing().iid().get().iid().equals(bound.asThing().getIID())) {
return Optional.empty();
}
} else if (conjVariable.isType()) {
if (!conjVariable.asType().label().isPresent())
newBounds.put(id, bound);
else if (!conjVariable.asType().label().get().properLabel().equals(bound.asType().getLabel())) {
return Optional.empty();
}
} else {
throw TypeDBException.of(ILLEGAL_STATE);
}
}
return Optional.of(new ConceptMap(newBounds));
}
Aggregations