Search in sources :

Example 1 with Retrievable

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;
}
Also used : Pair(com.vaticle.typedb.common.collection.Pair) Variable(com.vaticle.typedb.core.traversal.common.Identifier.Variable) Collections.set(com.vaticle.typedb.common.collection.Collections.set) Identifier(com.vaticle.typedb.core.traversal.common.Identifier) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Concept(com.vaticle.typedb.core.concept.Concept) HashSet(java.util.HashSet) REVERSE_UNIFICATION_MISSING_CONCEPT(com.vaticle.typedb.core.common.exception.ErrorMessage.Reasoner.REVERSE_UNIFICATION_MISSING_CONCEPT) ThingType(com.vaticle.typedb.core.concept.type.ThingType) Map(java.util.Map) Label(com.vaticle.typedb.core.common.parameters.Label) FunctionalIterator(com.vaticle.typedb.core.common.iterator.FunctionalIterator) Set(java.util.Set) Iterators(com.vaticle.typedb.core.common.iterator.Iterators) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Retrievable(com.vaticle.typedb.core.traversal.common.Identifier.Variable.Retrievable) Attribute(com.vaticle.typedb.core.concept.thing.Attribute) Optional(java.util.Optional) TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) Collections(java.util.Collections) Type(com.vaticle.typedb.core.concept.type.Type) Variable(com.vaticle.typedb.core.traversal.common.Identifier.Variable) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 2 with Retrievable

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);
}
Also used : Concept(com.vaticle.typedb.core.concept.Concept) Retrievable(com.vaticle.typedb.core.traversal.common.Identifier.Variable.Retrievable) HashMap(java.util.HashMap) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap)

Example 3 with Retrievable

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);
}
Also used : Concept(com.vaticle.typedb.core.concept.Concept) Retrievable(com.vaticle.typedb.core.traversal.common.Identifier.Variable.Retrievable) HashMap(java.util.HashMap) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Map(java.util.Map) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) HashMap(java.util.HashMap)

Example 4 with Retrievable

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();
}
Also used : Concept(com.vaticle.typedb.core.concept.Concept) Variable(com.vaticle.typedb.core.traversal.common.Identifier.Variable) HashSet(java.util.HashSet) Set(java.util.Set) Retrievable(com.vaticle.typedb.core.traversal.common.Identifier.Variable.Retrievable) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap)

Example 5 with Retrievable

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())));
}
Also used : Concept(com.vaticle.typedb.core.concept.Concept) HashSet(java.util.HashSet) Set(java.util.Set) Variable(com.vaticle.typedb.core.traversal.common.Identifier.Variable) Retrievable(com.vaticle.typedb.core.traversal.common.Identifier.Variable.Retrievable) HashMap(java.util.HashMap) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) HashMap(java.util.HashMap) Map(java.util.Map) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap)

Aggregations

ConceptMap (com.vaticle.typedb.core.concept.answer.ConceptMap)9 Retrievable (com.vaticle.typedb.core.traversal.common.Identifier.Variable.Retrievable)9 Concept (com.vaticle.typedb.core.concept.Concept)8 HashMap (java.util.HashMap)8 Map (java.util.Map)7 Set (java.util.Set)5 Variable (com.vaticle.typedb.core.traversal.common.Identifier.Variable)4 HashSet (java.util.HashSet)4 Identifier (com.vaticle.typedb.core.traversal.common.Identifier)3 Collections.set (com.vaticle.typedb.common.collection.Collections.set)2 Pair (com.vaticle.typedb.common.collection.Pair)2 REVERSE_UNIFICATION_MISSING_CONCEPT (com.vaticle.typedb.core.common.exception.ErrorMessage.Reasoner.REVERSE_UNIFICATION_MISSING_CONCEPT)2 TypeDBException (com.vaticle.typedb.core.common.exception.TypeDBException)2 FunctionalIterator (com.vaticle.typedb.core.common.iterator.FunctionalIterator)2 Iterators (com.vaticle.typedb.core.common.iterator.Iterators)2 Label (com.vaticle.typedb.core.common.parameters.Label)2 Attribute (com.vaticle.typedb.core.concept.thing.Attribute)2 ThingType (com.vaticle.typedb.core.concept.type.ThingType)2 Type (com.vaticle.typedb.core.concept.type.Type)2 ArrayList (java.util.ArrayList)2