Search in sources :

Example 1 with Relation

use of com.vaticle.typedb.core.concept.thing.Relation in project grakn by graknlabs.

the class DataExporter method relation.

private DataProto.Item relation(Relation relation) {
    status.relationCount.incrementAndGet();
    DataProto.Item.Relation.Builder relationBuilder = DataProto.Item.Relation.newBuilder().setId(relation.getIID().toBase64String()).setLabel(relation.getType().getLabel().name());
    Map<? extends RoleType, ? extends List<? extends Thing>> playersByRole = relation.getPlayersByRoleType();
    for (Map.Entry<? extends RoleType, ? extends List<? extends Thing>> rolePlayers : playersByRole.entrySet()) {
        RoleType role = rolePlayers.getKey();
        DataProto.Item.Relation.Role.Builder roleBuilder = DataProto.Item.Relation.Role.newBuilder().setLabel(role.getLabel().scopedName());
        for (Thing player : rolePlayers.getValue()) {
            status.roleCount.incrementAndGet();
            roleBuilder.addPlayer(DataProto.Item.Relation.Role.Player.newBuilder().setId(player.getIID().toBase64String()));
        }
        relationBuilder.addRole(roleBuilder);
    }
    readOwnerships(relation).forEachRemaining(a -> {
        status.ownershipCount.incrementAndGet();
        relationBuilder.addAttribute(a);
    });
    return DataProto.Item.newBuilder().setRelation(relationBuilder).build();
}
Also used : Relation(com.vaticle.typedb.core.concept.thing.Relation) RoleType(com.vaticle.typedb.core.concept.type.RoleType) Map(java.util.Map) Thing(com.vaticle.typedb.core.concept.thing.Thing)

Example 2 with Relation

use of com.vaticle.typedb.core.concept.thing.Relation in project grakn by graknlabs.

the class DataImporter method importSkippedRelations.

private void importSkippedRelations() {
    try (TypeDB.Transaction transaction = session.transaction(Arguments.Transaction.Type.WRITE)) {
        skippedRelations.forEach(msg -> {
            RelationType relType = transaction.concepts().getRelationType(msg.getLabel());
            if (relType == null)
                throw TypeDBException.of(TYPE_NOT_FOUND, msg.getLabel());
            Relation importedRelation = relType.create();
            idMapper.put(msg.getId(), importedRelation.getIID());
            int ownershipCount = 0;
            for (DataProto.Item.OwnedAttribute ownership : msg.getAttributeList()) {
                Thing attribute = transaction.concepts().getThing(idMapper.get(ownership.getId()));
                assert attribute != null;
                importedRelation.setHas(attribute.asAttribute());
                ownershipCount++;
            }
            status.ownershipCount.addAndGet(ownershipCount);
        });
        skippedRelations.forEach(msg -> {
            RelationType relType = transaction.concepts().getRelationType(msg.getLabel());
            Relation relation = transaction.concepts().getThing(idMapper.get(msg.getId())).asRelation();
            msg.getRoleList().forEach(roleMsg -> {
                RoleType roleType = getRoleType(relType, roleMsg);
                for (DataProto.Item.Relation.Role.Player playerMessage : roleMsg.getPlayerList()) {
                    Thing player = transaction.concepts().getThing(idMapper.get(playerMessage.getId()));
                    if (player == null)
                        throw TypeDBException.of(PLAYER_NOT_FOUND, relType.getLabel());
                    else
                        relation.addPlayer(roleType, player);
                }
            });
        });
        transaction.commit();
    }
}
Also used : Relation(com.vaticle.typedb.core.concept.thing.Relation) RoleType(com.vaticle.typedb.core.concept.type.RoleType) RelationType(com.vaticle.typedb.core.concept.type.RelationType) TypeDB(com.vaticle.typedb.core.TypeDB) Thing(com.vaticle.typedb.core.concept.thing.Thing)

Example 3 with Relation

use of com.vaticle.typedb.core.concept.thing.Relation in project grakn by graknlabs.

the class RelationSteps method relation_type_create_new_instance_with_key.

@When("{var} = relation\\( ?{type_label} ?) create new instance with key\\( ?{type_label} ?): {datetime}")
public void relation_type_create_new_instance_with_key(String var, String type, String keyType, LocalDateTime keyValue) {
    Attribute.DateTime key = tx().concepts().getAttributeType(keyType).asDateTime().put(keyValue);
    Relation relation = tx().concepts().getRelationType(type).create();
    relation.setHas(key);
    put(var, relation);
}
Also used : Relation(com.vaticle.typedb.core.concept.thing.Relation) Attribute(com.vaticle.typedb.core.concept.thing.Attribute) When(io.cucumber.java.en.When)

Example 4 with Relation

use of com.vaticle.typedb.core.concept.thing.Relation in project grakn by graknlabs.

the class UnifyRelationConcludableTest method unUnify_produces_cartesian_named_types_only_for_unbound_vars.

@Test
public void unUnify_produces_cartesian_named_types_only_for_unbound_vars() {
    String conjunction = "{$r ($role: $x) isa $rel;}";
    Set<Concludable> concludables = Concludable.create(resolvedConjunction(conjunction, logicMgr));
    Concludable.Relation queryConcludable = concludables.iterator().next().asRelation();
    Rule rule = createRule("people-are-self-friends", "{ $x isa person; }", " (friend: $x) isa friendship ", logicMgr);
    List<Unifier> unifiers = queryConcludable.unify(rule.conclusion(), conceptMgr).toList();
    assertEquals(1, unifiers.size());
    Unifier unifier = unifiers.get(0);
    // test filter allows a valid answer
    Relation friendship = instanceOf("friendship").asRelation();
    Thing person = instanceOf("person");
    addRolePlayer(friendship, "friend", person);
    Map<Variable, Concept> concepts = map(pair(Variable.anon(0), friendship), pair(Variable.name("x"), person), pair(Variable.label("friendship"), friendship.getType()), pair(Variable.label("friendship:friend"), friendship.getType().getRelates("friend")));
    List<ConceptMap> unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map(pair(Variable.name("rel"), friendship.getType())))).toList();
    assertEquals(2, unified.size());
    Set<Map<String, String>> expected = set(new HashMap<String, String>() {

        {
            put("$rel", "friendship");
            put("$role", "friendship:friend");
        }
    }, new HashMap<String, String>() {

        {
            put("$rel", "friendship");
            put("$role", "relation:role");
        }
    });
    Set<Map<String, String>> actual = new HashSet<>();
    iterate(unified).forEachRemaining(answer -> {
        actual.add(new HashMap<String, String>() {

            {
                put("$rel", answer.get("rel").asType().getLabel().name());
                put("$role", answer.get("role").asType().getLabel().scopedName());
            }
        });
    });
    assertEquals(expected, actual);
}
Also used : Concept(com.vaticle.typedb.core.concept.Concept) Variable(com.vaticle.typedb.core.traversal.common.Identifier.Variable) Relation(com.vaticle.typedb.core.concept.thing.Relation) Rule(com.vaticle.typedb.core.logic.Rule) Util.createRule(com.vaticle.typedb.core.logic.resolvable.Util.createRule) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Map(java.util.Map) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) HashMap(java.util.HashMap) Thing(com.vaticle.typedb.core.concept.thing.Thing) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with Relation

use of com.vaticle.typedb.core.concept.thing.Relation in project grakn by graknlabs.

the class UnifyRelationConcludableTest method relation_duplicate_players_unifies_rule_relation_distinct_players.

@Test
public void relation_duplicate_players_unifies_rule_relation_distinct_players() {
    List<Unifier> unifiers = unifiers("{ (employee: $p, employee: $p) isa employment; }", rule("($employee: $x, $employee: $y) isa $employment", "{ $x isa person; $y isa person; $employment type employment;$employee type employment:employee; }")).toList();
    Set<Map<String, Set<String>>> result = iterate(unifiers).map(u -> getStringMapping(u.mapping())).toSet();
    Set<Map<String, Set<String>>> expected = set(new HashMap<String, Set<String>>() {

        {
            put("$p", set("$x", "$y"));
            put("$_0", set("$_0"));
        }
    });
    assertEquals(expected, result);
    Unifier unifier = unifiers.get(0);
    // test requirements
    assertEquals(typeHierarchy("employment"), unifier.requirements().isaExplicit().get(Variable.anon(0)));
    assertEquals(2, unifier.requirements().isaExplicit().size());
    assertEquals(2, unifier.requirements().types().size());
    assertEquals(roleHierarchy("employee", "employment"), unifier.requirements().types().get(Variable.label("employment:employee")));
    assertEquals(0, unifier.requirements().predicates().size());
    // test filter allows a valid answer
    Relation employment = instanceOf("employment").asRelation();
    Thing person = instanceOf("person");
    addRolePlayer(employment, "employee", person);
    addRolePlayer(employment, "employee", person);
    Map<Variable, Concept> identifiedConcepts = map(pair(Variable.anon(0), employment), pair(Variable.name("x"), person), pair(Variable.name("y"), person), pair(Variable.name("employment"), employment.getType()), pair(Variable.name("employee"), employment.getType().getRelates("employee")));
    FunctionalIterator<ConceptMap> unified = unifier.unUnify(identifiedConcepts, new Unifier.Requirements.Instance(map()));
    assertTrue(unified.hasNext());
    ConceptMap unifiedAnswer = unified.first().get();
    assertEquals(2, unifiedAnswer.concepts().size());
    assertEquals(person, unifiedAnswer.get("p"));
    // filter out answers with differing role players that must be the same
    employment = instanceOf("employment").asRelation();
    person = instanceOf("person");
    Thing differentPerson = instanceOf("person");
    addRolePlayer(employment, "employee", person);
    addRolePlayer(employment, "employee", differentPerson);
    identifiedConcepts = map(pair(Variable.anon(0), employment), pair(Variable.name("x"), person), pair(Variable.name("y"), differentPerson), pair(Variable.name("employment"), employment.getType()), pair(Variable.name("employee"), employment.getType().getRelates("employee")));
    unified = unifier.unUnify(identifiedConcepts, new Unifier.Requirements.Instance(map()));
    assertFalse(unified.hasNext());
}
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) Concept(com.vaticle.typedb.core.concept.Concept) Set(java.util.Set) HashSet(java.util.HashSet) Variable(com.vaticle.typedb.core.traversal.common.Identifier.Variable) Relation(com.vaticle.typedb.core.concept.thing.Relation) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Map(java.util.Map) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) HashMap(java.util.HashMap) Thing(com.vaticle.typedb.core.concept.thing.Thing) Test(org.junit.Test)

Aggregations

Relation (com.vaticle.typedb.core.concept.thing.Relation)15 Thing (com.vaticle.typedb.core.concept.thing.Thing)9 Concept (com.vaticle.typedb.core.concept.Concept)7 ConceptMap (com.vaticle.typedb.core.concept.answer.ConceptMap)7 Variable (com.vaticle.typedb.core.traversal.common.Identifier.Variable)7 HashMap (java.util.HashMap)7 HashSet (java.util.HashSet)7 Test (org.junit.Test)7 RoleType (com.vaticle.typedb.core.concept.type.RoleType)5 RelationType (com.vaticle.typedb.core.concept.type.RelationType)4 Rule (com.vaticle.typedb.core.logic.Rule)4 Util.createRule (com.vaticle.typedb.core.logic.resolvable.Util.createRule)4 Map (java.util.Map)4 TypeDBException (com.vaticle.typedb.core.common.exception.TypeDBException)3 FunctionalIterator (com.vaticle.typedb.core.common.iterator.FunctionalIterator)3 AttributeType (com.vaticle.typedb.core.concept.type.AttributeType)3 Type (com.vaticle.typedb.core.concept.type.Type)3 Set (java.util.Set)3 Lists (com.google.common.collect.Lists)2 Collections.list (com.vaticle.typedb.common.collection.Collections.list)2