Search in sources :

Example 6 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)

Example 7 with Retrievable

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

Example 8 with Retrievable

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);
}
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 9 with Retrievable

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));
}
Also used : Concept(com.vaticle.typedb.core.concept.Concept) Variable(com.vaticle.typedb.core.pattern.variable.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