Search in sources :

Example 6 with Thing

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

the class ThingService method execute.

public void execute(Transaction.Req req) {
    ConceptProto.Thing.Req thingReq = req.getThingReq();
    assert thingReq != null;
    Thing thing = notNull(conceptMgr.getThing(ByteArray.of(thingReq.getIid().toByteArray())));
    UUID reqID = byteStringAsUUID(req.getReqId());
    switch(thingReq.getReqCase()) {
        case THING_DELETE_REQ:
            delete(thing, reqID);
            return;
        case THING_GET_TYPE_REQ:
            getType(thing, reqID);
            return;
        case THING_SET_HAS_REQ:
            setHas(thing, thingReq.getThingSetHasReq().getAttribute(), reqID);
            return;
        case THING_UNSET_HAS_REQ:
            unsetHas(thing, thingReq.getThingUnsetHasReq().getAttribute(), reqID);
            return;
        case THING_GET_HAS_REQ:
            getHas(thing, thingReq.getThingGetHasReq(), reqID);
            return;
        case THING_GET_RELATIONS_REQ:
            getRelations(thing, thingReq.getThingGetRelationsReq().getRoleTypesList(), reqID);
            return;
        case THING_GET_PLAYING_REQ:
            getPlaying(thing, reqID);
            return;
        case RELATION_ADD_PLAYER_REQ:
            addPlayer(thing.asRelation(), thingReq.getRelationAddPlayerReq(), reqID);
            return;
        case RELATION_REMOVE_PLAYER_REQ:
            removePlayer(thing.asRelation(), thingReq.getRelationRemovePlayerReq(), reqID);
            return;
        case RELATION_GET_PLAYERS_REQ:
            getPlayers(thing.asRelation(), thingReq.getRelationGetPlayersReq().getRoleTypesList(), reqID);
            return;
        case RELATION_GET_PLAYERS_BY_ROLE_TYPE_REQ:
            getPlayersByRoleType(thing.asRelation(), reqID);
            return;
        case RELATION_GET_RELATING_REQ:
            getRelating(thing.asRelation(), reqID);
            break;
        case ATTRIBUTE_GET_OWNERS_REQ:
            getOwners(thing.asAttribute(), thingReq.getAttributeGetOwnersReq(), reqID);
            return;
        case REQ_NOT_SET:
        default:
            throw TypeDBException.of(UNKNOWN_REQUEST_TYPE);
    }
}
Also used : RequestReader.byteStringAsUUID(com.vaticle.typedb.core.server.common.RequestReader.byteStringAsUUID) UUID(java.util.UUID) Thing(com.vaticle.typedb.core.concept.thing.Thing)

Example 7 with Thing

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

the class ThingService method getPlayersByRoleType.

private void getPlayersByRoleType(Relation relation, UUID reqID) {
    // TODO: this should be optimised to actually iterate over role players by role type lazily
    Map<? extends RoleType, ? extends List<? extends Thing>> playersByRole = relation.getPlayersByRoleType();
    Stream.Builder<Pair<RoleType, Thing>> responses = Stream.builder();
    for (Map.Entry<? extends RoleType, ? extends List<? extends Thing>> players : playersByRole.entrySet()) {
        for (Thing player : players.getValue()) {
            responses.add(pair(players.getKey(), player));
        }
    }
    transactionSvc.stream(responses.build().iterator(), reqID, players -> getPlayersByRoleTypeResPart(reqID, players));
}
Also used : Stream(java.util.stream.Stream) Map(java.util.Map) Thing(com.vaticle.typedb.core.concept.thing.Thing) Pair(com.vaticle.typedb.common.collection.Pair)

Example 8 with Thing

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

the class UnifyRelationConcludableTest method relation_role_unifies_rule_relation_exact.

@Test
public void relation_role_unifies_rule_relation_exact() {
    Unifier unifier = uniqueUnifier("{ ($role: $y) isa employment; }", rule(" (employee: $x) isa employment ", "{ $x isa person; }"));
    Map<String, Set<String>> result = getStringMapping(unifier.mapping());
    Map<String, Set<String>> expected = new HashMap<String, Set<String>>() {

        {
            put("$y", set("$x"));
            put("$role", set("$_employment:employee"));
            put("$_0", set("$_0"));
        }
    };
    assertEquals(expected, result);
    // test requirement
    assertEquals(typeHierarchy("employment"), unifier.requirements().isaExplicit().get(Variable.anon(0)));
    assertEquals(2, unifier.requirements().isaExplicit().size());
    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);
    Map<Variable, Concept> concepts = map(pair(Variable.anon(0), employment), pair(Variable.name("x"), person), pair(Variable.label("employment"), employment.getType()), pair(Variable.label("employment:employee"), employment.getType().getRelates("employee")));
    FunctionalIterator<ConceptMap> unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
    assertTrue(unified.hasNext());
    ConceptMap unifiedAnswer = unified.first().get();
    assertEquals(3, unified.next().concepts().size());
    assertEquals(employment.getType().getRelates("employee"), unifiedAnswer.get("role"));
    assertEquals(person, unifiedAnswer.get("y"));
    assertEquals(employment, unifiedAnswer.get(Variable.anon(0)));
    // filter out invalid types
    Relation friendship = instanceOf("friendship").asRelation();
    person = instanceOf("person");
    addRolePlayer(friendship, "friend", person);
    concepts = map(pair(Variable.anon(0), friendship), pair(Variable.name("x"), person), pair(Variable.label("employment"), friendship.getType()), pair(Variable.label("employment:employee"), friendship.getType().getRelates("friend")));
    unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
    assertFalse(unified.hasNext());
}
Also used : Concept(com.vaticle.typedb.core.concept.Concept) Set(java.util.Set) HashSet(java.util.HashSet) Variable(com.vaticle.typedb.core.traversal.common.Identifier.Variable) HashMap(java.util.HashMap) Relation(com.vaticle.typedb.core.concept.thing.Relation) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Thing(com.vaticle.typedb.core.concept.thing.Thing) Test(org.junit.Test)

Example 9 with Thing

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

the class UnifyRelationConcludableTest method unUnify_produces_cartesian_named_types.

@Test
public void unUnify_produces_cartesian_named_types() {
    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())).toList();
    assertEquals(6, 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");
        }
    }, new HashMap<String, String>() {

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

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

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

        {
            put("$rel", "thing");
            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 10 with Thing

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

the class UnifyRelationConcludableTest method relation_type_and_player_unifies_rule_relation_exact.

@Test
public void relation_type_and_player_unifies_rule_relation_exact() {
    Unifier unifier = uniqueUnifier("{ (employee: $y) isa $rel; }", rule("(employee: $x) isa employment", "{ $x isa person; }"));
    Map<String, Set<String>> result = getStringMapping(unifier.mapping());
    Map<String, Set<String>> expected = new HashMap<String, Set<String>>() {

        {
            put("$y", set("$x"));
            put("$rel", set("$_employment"));
            put("$_0", set("$_0"));
        }
    };
    assertEquals(expected, result);
    // test requirements
    assertEquals(roleHierarchy("employee", "employment"), unifier.requirements().types().get(Variable.label("relation:employee")));
    assertEquals(2, unifier.requirements().types().size());
    assertEquals(2, unifier.requirements().isaExplicit().size());
    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);
    Map<Variable, Concept> concepts = map(pair(Variable.anon(0), employment), pair(Variable.name("x"), person), pair(Variable.label("employment"), employment.getType()), pair(Variable.label("employment:employee"), employment.getType().getRelates("employee")));
    FunctionalIterator<ConceptMap> unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
    assertTrue(unified.hasNext());
    ConceptMap unifiedAnswer = unified.first().get();
    assertEquals(3, unifiedAnswer.concepts().size());
    assertEquals(employment.getType(), unifiedAnswer.get("rel"));
    assertEquals(person, unifiedAnswer.get("y"));
    assertEquals(employment, unifiedAnswer.get(Variable.anon(0)));
    // filter out invalid types
    Relation friendship = instanceOf("friendship").asRelation();
    person = instanceOf("person");
    addRolePlayer(friendship, "friend", person);
    concepts = map(pair(Variable.anon(0), friendship), pair(Variable.name("x"), person), pair(Variable.label("employment"), friendship.getType()), pair(Variable.label("employment:employee"), friendship.getType().getRelates("friend")));
    unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
    assertFalse(unified.hasNext());
}
Also used : Concept(com.vaticle.typedb.core.concept.Concept) Set(java.util.Set) HashSet(java.util.HashSet) Variable(com.vaticle.typedb.core.traversal.common.Identifier.Variable) HashMap(java.util.HashMap) Relation(com.vaticle.typedb.core.concept.thing.Relation) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Thing(com.vaticle.typedb.core.concept.thing.Thing) Test(org.junit.Test)

Aggregations

Thing (com.vaticle.typedb.core.concept.thing.Thing)11 Relation (com.vaticle.typedb.core.concept.thing.Relation)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 Map (java.util.Map)6 RoleType (com.vaticle.typedb.core.concept.type.RoleType)4 Rule (com.vaticle.typedb.core.logic.Rule)4 Util.createRule (com.vaticle.typedb.core.logic.resolvable.Util.createRule)4 RelationType (com.vaticle.typedb.core.concept.type.RelationType)3 Set (java.util.Set)3 UUID (java.util.UUID)3 Lists (com.google.common.collect.Lists)2 Collections.list (com.vaticle.typedb.common.collection.Collections.list)2 Collections.map (com.vaticle.typedb.common.collection.Collections.map)2 Collections.pair (com.vaticle.typedb.common.collection.Collections.pair)2 Collections.set (com.vaticle.typedb.common.collection.Collections.set)2