Search in sources :

Example 6 with Type

use of com.vaticle.typedb.core.concept.type.Type 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 7 with Type

use of com.vaticle.typedb.core.concept.type.Type in project grakn by graknlabs.

the class RelationTypeImpl method getRelates.

/**
 * Get the role type with a given {@code roleLabel} related by this relation type.
 *
 * First, look up the role type by the given label and it's scope: the relation label.
 * If the role type vertex do not exist, then call {@code role()} to get the inherited role types,
 * and see if the any of them has the {@code roleLabel} of interest.
 *
 * @param roleLabel the label of the role
 * @return the role type related in this relation
 */
@Override
public RoleTypeImpl getRelates(String roleLabel) {
    Optional<RoleTypeImpl> roleType;
    TypeVertex roleTypeVertex = graphMgr.schema().getType(roleLabel, vertex.label());
    if (roleTypeVertex != null) {
        return RoleTypeImpl.of(graphMgr, roleTypeVertex);
    } else if ((roleType = getRelates().filter(role -> role.getLabel().name().equals(roleLabel)).first()).isPresent()) {
        return roleType.get();
    } else
        return null;
}
Also used : TYPE_ROOT_MISMATCH(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeRead.TYPE_ROOT_MISMATCH) TypeEdge(com.vaticle.typedb.core.graph.edge.TypeEdge) RELATION_RELATES_ROLE_FROM_SUPERTYPE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.RELATION_RELATES_ROLE_FROM_SUPERTYPE) Order(com.vaticle.typedb.core.common.iterator.sorted.SortedIterator.Order) RelationImpl(com.vaticle.typedb.core.concept.thing.impl.RelationImpl) RelationType(com.vaticle.typedb.core.concept.type.RelationType) ASC(com.vaticle.typedb.core.common.iterator.sorted.SortedIterator.ASC) Forwardable.iterateSorted(com.vaticle.typedb.core.common.iterator.sorted.SortedIterators.Forwardable.iterateSorted) RELATION_NO_ROLE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.RELATION_NO_ROLE) RELATION_ABSTRACT_ROLE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.RELATION_ABSTRACT_ROLE) RELATES(com.vaticle.typedb.core.graph.common.Encoding.Edge.Type.RELATES) RELATION(com.vaticle.typedb.core.graph.common.Encoding.Vertex.Type.Root.RELATION) ROLE(com.vaticle.typedb.core.graph.common.Encoding.Vertex.Type.Root.ROLE) RELATION_RELATES_ROLE_NOT_AVAILABLE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.RELATION_RELATES_ROLE_NOT_AVAILABLE) RELATION_TYPE(com.vaticle.typedb.core.graph.common.Encoding.Vertex.Type.RELATION_TYPE) GraphManager(com.vaticle.typedb.core.graph.GraphManager) FunctionalIterator(com.vaticle.typedb.core.common.iterator.FunctionalIterator) Forwardable(com.vaticle.typedb.core.common.iterator.sorted.SortedIterator.Forwardable) RoleType(com.vaticle.typedb.core.concept.type.RoleType) ThingVertex(com.vaticle.typedb.core.graph.vertex.ThingVertex) Objects(java.util.Objects) List(java.util.List) Relation(com.vaticle.typedb.core.concept.thing.Relation) ROOT_TYPE_MUTATION(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.ROOT_TYPE_MUTATION) Optional(java.util.Optional) TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) TypeVertex(com.vaticle.typedb.core.graph.vertex.TypeVertex) TYPE_HAS_INSTANCES_SET_ABSTRACT(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.TYPE_HAS_INSTANCES_SET_ABSTRACT) Type(com.vaticle.typedb.core.concept.type.Type) TypeVertex(com.vaticle.typedb.core.graph.vertex.TypeVertex)

Example 8 with Type

use of com.vaticle.typedb.core.concept.type.Type in project grakn by graknlabs.

the class UnifyRelationConcludableTest method relation_duplicate_roles_unifies_rule_relation_duplicate_roles.

// [reflexive parent, child]
@Test
public void relation_duplicate_roles_unifies_rule_relation_duplicate_roles() {
    String conjunction = "{ (employee: $p, employee: $p) isa employment; }";
    Set<Concludable> concludables = Concludable.create(resolvedConjunction(conjunction, logicMgr));
    Concludable.Relation queryConcludable = concludables.iterator().next().asRelation();
    Rule rule = createRule("a-person-is-employed-twice", "{ $x isa person; $employment type employment; $employee type employment:employee; }", "($employee: $x, $employee: $x) isa $employment", logicMgr);
    FunctionalIterator<Unifier> unifier = queryConcludable.unify(rule.conclusion(), conceptMgr);
    Set<Map<String, Set<String>>> result = unifier.map(u -> getStringMapping(u.mapping())).toSet();
    Set<Map<String, Set<String>>> expected = set(new HashMap<String, Set<String>>() {

        {
            put("$p", set("$x"));
            put("$_0", set("$_0"));
        }
    });
    assertEquals(expected, result);
}
Also used : Variable(com.vaticle.typedb.core.traversal.common.Identifier.Variable) MB(com.vaticle.typedb.core.common.collection.Bytes.MB) RelationType(com.vaticle.typedb.core.concept.type.RelationType) Util.getStringMapping(com.vaticle.typedb.core.logic.resolvable.Util.getStringMapping) Arguments(com.vaticle.typedb.core.common.parameters.Arguments) LogicManager(com.vaticle.typedb.core.logic.LogicManager) ThingType(com.vaticle.typedb.core.concept.type.ThingType) After(org.junit.After) Map(java.util.Map) Collections.list(com.vaticle.typedb.common.collection.Collections.list) Thing(com.vaticle.typedb.core.concept.thing.Thing) Path(java.nio.file.Path) TypeQL(com.vaticle.typeql.lang.TypeQL) AfterClass(org.junit.AfterClass) FunctionalIterator(com.vaticle.typedb.core.common.iterator.FunctionalIterator) CoreDatabaseManager(com.vaticle.typedb.core.database.CoreDatabaseManager) Options(com.vaticle.typedb.core.common.parameters.Options) Set(java.util.Set) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) Collections.map(com.vaticle.typedb.common.collection.Collections.map) ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) ILLEGAL_STATE(com.vaticle.typedb.core.common.exception.ErrorMessage.Internal.ILLEGAL_STATE) Util.resolvedConjunction(com.vaticle.typedb.core.logic.resolvable.Util.resolvedConjunction) BeforeClass(org.junit.BeforeClass) Collections.set(com.vaticle.typedb.common.collection.Collections.set) HashMap(java.util.HashMap) Concept(com.vaticle.typedb.core.concept.Concept) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) Rule(com.vaticle.typedb.core.logic.Rule) Collections.pair(com.vaticle.typedb.common.collection.Collections.pair) Util.createRule(com.vaticle.typedb.core.logic.resolvable.Util.createRule) Label(com.vaticle.typedb.core.common.parameters.Label) Before(org.junit.Before) Conjunction(com.vaticle.typedb.core.pattern.Conjunction) Iterator(java.util.Iterator) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) RoleType(com.vaticle.typedb.core.concept.type.RoleType) CoreSession(com.vaticle.typedb.core.database.CoreSession) Relation(com.vaticle.typedb.core.concept.thing.Relation) Paths(java.nio.file.Paths) Iterators.iterate(com.vaticle.typedb.core.common.iterator.Iterators.iterate) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Type(com.vaticle.typedb.core.concept.type.Type) Util(com.vaticle.typedb.core.test.integration.util.Util) Set(java.util.Set) HashSet(java.util.HashSet) Rule(com.vaticle.typedb.core.logic.Rule) Util.createRule(com.vaticle.typedb.core.logic.resolvable.Util.createRule) Map(java.util.Map) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 9 with Type

use of com.vaticle.typedb.core.concept.type.Type in project grakn by graknlabs.

the class UnifyRelationConcludableTest method relation_player_role_unifies_rule_relation_repeated_variable_role.

// [many2many]
@Test
public void relation_player_role_unifies_rule_relation_repeated_variable_role() {
    String conjunction = "{ ($role: $p) isa employment; }";
    Set<Concludable> concludables = Concludable.create(resolvedConjunction(conjunction, logicMgr));
    Concludable.Relation queryConcludable = concludables.iterator().next().asRelation();
    Rule rule = createRule("two-people-are-employed", "{ $x isa person; $y isa person; $employment type employment; " + "$employee type employment:employee; $employer type employment:employer; }", "($employee: $x, $employee: $y) isa $employment", logicMgr);
    FunctionalIterator<Unifier> unifier = queryConcludable.unify(rule.conclusion(), conceptMgr);
    Set<Map<String, Set<String>>> result = unifier.map(u -> getStringMapping(u.mapping())).toSet();
    Set<Map<String, Set<String>>> expected = set(new HashMap<String, Set<String>>() {

        {
            put("$p", set("$x"));
            put("$role", set("$employee"));
            put("$_0", set("$_0"));
        }
    }, new HashMap<String, Set<String>>() {

        {
            put("$p", set("$y"));
            put("$role", set("$employee"));
            put("$_0", set("$_0"));
        }
    });
    assertEquals(expected, result);
}
Also used : Variable(com.vaticle.typedb.core.traversal.common.Identifier.Variable) MB(com.vaticle.typedb.core.common.collection.Bytes.MB) RelationType(com.vaticle.typedb.core.concept.type.RelationType) Util.getStringMapping(com.vaticle.typedb.core.logic.resolvable.Util.getStringMapping) Arguments(com.vaticle.typedb.core.common.parameters.Arguments) LogicManager(com.vaticle.typedb.core.logic.LogicManager) ThingType(com.vaticle.typedb.core.concept.type.ThingType) After(org.junit.After) Map(java.util.Map) Collections.list(com.vaticle.typedb.common.collection.Collections.list) Thing(com.vaticle.typedb.core.concept.thing.Thing) Path(java.nio.file.Path) TypeQL(com.vaticle.typeql.lang.TypeQL) AfterClass(org.junit.AfterClass) FunctionalIterator(com.vaticle.typedb.core.common.iterator.FunctionalIterator) CoreDatabaseManager(com.vaticle.typedb.core.database.CoreDatabaseManager) Options(com.vaticle.typedb.core.common.parameters.Options) Set(java.util.Set) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) Collections.map(com.vaticle.typedb.common.collection.Collections.map) ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) ILLEGAL_STATE(com.vaticle.typedb.core.common.exception.ErrorMessage.Internal.ILLEGAL_STATE) Util.resolvedConjunction(com.vaticle.typedb.core.logic.resolvable.Util.resolvedConjunction) BeforeClass(org.junit.BeforeClass) Collections.set(com.vaticle.typedb.common.collection.Collections.set) HashMap(java.util.HashMap) Concept(com.vaticle.typedb.core.concept.Concept) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) Rule(com.vaticle.typedb.core.logic.Rule) Collections.pair(com.vaticle.typedb.common.collection.Collections.pair) Util.createRule(com.vaticle.typedb.core.logic.resolvable.Util.createRule) Label(com.vaticle.typedb.core.common.parameters.Label) Before(org.junit.Before) Conjunction(com.vaticle.typedb.core.pattern.Conjunction) Iterator(java.util.Iterator) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) RoleType(com.vaticle.typedb.core.concept.type.RoleType) CoreSession(com.vaticle.typedb.core.database.CoreSession) Relation(com.vaticle.typedb.core.concept.thing.Relation) Paths(java.nio.file.Paths) Iterators.iterate(com.vaticle.typedb.core.common.iterator.Iterators.iterate) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Type(com.vaticle.typedb.core.concept.type.Type) Util(com.vaticle.typedb.core.test.integration.util.Util) Set(java.util.Set) HashSet(java.util.HashSet) Rule(com.vaticle.typedb.core.logic.Rule) Util.createRule(com.vaticle.typedb.core.logic.resolvable.Util.createRule) Map(java.util.Map) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 10 with Type

use of com.vaticle.typedb.core.concept.type.Type in project grakn by graknlabs.

the class UnifyRelationConcludableTest method relation_more_players_than_rule_relation_fails_unify.

@Test
public void relation_more_players_than_rule_relation_fails_unify() {
    String relationQuery = "{ (part-time-employee: $r, part-time-employer: $p, restriction: $q) isa part-time-employment; }";
    Set<Concludable> concludables = Concludable.create(resolvedConjunction(relationQuery, logicMgr));
    Concludable.Relation queryConcludable = concludables.iterator().next().asRelation();
    Rule rule = createRule("one-employee-one-employer", "{ $x isa person; $y isa organisation; " + "$employee type employment:employee; $employer type employment:employer; }", "($employee: $x, $employer: $y) isa employment", logicMgr);
    FunctionalIterator<Unifier> unifier = queryConcludable.unify(rule.conclusion(), conceptMgr);
    Set<Map<String, Set<String>>> result = unifier.map(u -> getStringMapping(u.mapping())).toSet();
    Set<Map<String, Set<String>>> expected = Collections.emptySet();
    assertEquals(expected, result);
}
Also used : Variable(com.vaticle.typedb.core.traversal.common.Identifier.Variable) MB(com.vaticle.typedb.core.common.collection.Bytes.MB) RelationType(com.vaticle.typedb.core.concept.type.RelationType) Util.getStringMapping(com.vaticle.typedb.core.logic.resolvable.Util.getStringMapping) Arguments(com.vaticle.typedb.core.common.parameters.Arguments) LogicManager(com.vaticle.typedb.core.logic.LogicManager) ThingType(com.vaticle.typedb.core.concept.type.ThingType) After(org.junit.After) Map(java.util.Map) Collections.list(com.vaticle.typedb.common.collection.Collections.list) Thing(com.vaticle.typedb.core.concept.thing.Thing) Path(java.nio.file.Path) TypeQL(com.vaticle.typeql.lang.TypeQL) AfterClass(org.junit.AfterClass) FunctionalIterator(com.vaticle.typedb.core.common.iterator.FunctionalIterator) CoreDatabaseManager(com.vaticle.typedb.core.database.CoreDatabaseManager) Options(com.vaticle.typedb.core.common.parameters.Options) Set(java.util.Set) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) Collections.map(com.vaticle.typedb.common.collection.Collections.map) ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) ILLEGAL_STATE(com.vaticle.typedb.core.common.exception.ErrorMessage.Internal.ILLEGAL_STATE) Util.resolvedConjunction(com.vaticle.typedb.core.logic.resolvable.Util.resolvedConjunction) BeforeClass(org.junit.BeforeClass) Collections.set(com.vaticle.typedb.common.collection.Collections.set) HashMap(java.util.HashMap) Concept(com.vaticle.typedb.core.concept.Concept) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) Rule(com.vaticle.typedb.core.logic.Rule) Collections.pair(com.vaticle.typedb.common.collection.Collections.pair) Util.createRule(com.vaticle.typedb.core.logic.resolvable.Util.createRule) Label(com.vaticle.typedb.core.common.parameters.Label) Before(org.junit.Before) Conjunction(com.vaticle.typedb.core.pattern.Conjunction) Iterator(java.util.Iterator) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) RoleType(com.vaticle.typedb.core.concept.type.RoleType) CoreSession(com.vaticle.typedb.core.database.CoreSession) Relation(com.vaticle.typedb.core.concept.thing.Relation) Paths(java.nio.file.Paths) Iterators.iterate(com.vaticle.typedb.core.common.iterator.Iterators.iterate) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Type(com.vaticle.typedb.core.concept.type.Type) Util(com.vaticle.typedb.core.test.integration.util.Util) Rule(com.vaticle.typedb.core.logic.Rule) Util.createRule(com.vaticle.typedb.core.logic.resolvable.Util.createRule) Map(java.util.Map) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

Type (com.vaticle.typedb.core.concept.type.Type)11 AttributeType (com.vaticle.typedb.core.concept.type.AttributeType)10 RelationType (com.vaticle.typedb.core.concept.type.RelationType)10 RoleType (com.vaticle.typedb.core.concept.type.RoleType)10 ThingType (com.vaticle.typedb.core.concept.type.ThingType)10 TypeDBException (com.vaticle.typedb.core.common.exception.TypeDBException)9 FunctionalIterator (com.vaticle.typedb.core.common.iterator.FunctionalIterator)9 Collections.set (com.vaticle.typedb.common.collection.Collections.set)8 Label (com.vaticle.typedb.core.common.parameters.Label)8 Concept (com.vaticle.typedb.core.concept.Concept)8 ConceptMap (com.vaticle.typedb.core.concept.answer.ConceptMap)8 Relation (com.vaticle.typedb.core.concept.thing.Relation)8 Variable (com.vaticle.typedb.core.traversal.common.Identifier.Variable)8 Collections (java.util.Collections)8 Lists (com.google.common.collect.Lists)7 Collections.list (com.vaticle.typedb.common.collection.Collections.list)7 Collections.map (com.vaticle.typedb.common.collection.Collections.map)7 Collections.pair (com.vaticle.typedb.common.collection.Collections.pair)7 MB (com.vaticle.typedb.core.common.collection.Bytes.MB)7 ILLEGAL_STATE (com.vaticle.typedb.core.common.exception.ErrorMessage.Internal.ILLEGAL_STATE)7