Search in sources :

Example 11 with RelationType

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

the class ConceptService method putRelationType.

private void putRelationType(String label, UUID reqID) {
    RelationType relationType = conceptMgr.putRelationType(label);
    transactionSvc.respond(putRelationTypeRes(reqID, relationType));
}
Also used : RelationType(com.vaticle.typedb.core.concept.type.RelationType)

Example 12 with RelationType

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

the class ThingTypeSteps method thing_type_set_supertype_throws_exception.

@Then("{root_label}\\( ?{type_label} ?) set supertype: {type_label}; throws exception")
public void thing_type_set_supertype_throws_exception(RootLabel rootLabel, String typeLabel, String superLabel) {
    switch(rootLabel) {
        case ENTITY:
            EntityType entitySuperType = tx().concepts().getEntityType(superLabel);
            assertThrows(() -> tx().concepts().getEntityType(typeLabel).setSupertype(entitySuperType));
            break;
        case ATTRIBUTE:
            AttributeType attributeSuperType = tx().concepts().getAttributeType(superLabel);
            assertThrows(() -> tx().concepts().getAttributeType(typeLabel).setSupertype(attributeSuperType));
            break;
        case RELATION:
            RelationType relationSuperType = tx().concepts().getRelationType(superLabel);
            assertThrows(() -> tx().concepts().getRelationType(typeLabel).setSupertype(relationSuperType));
            break;
    }
}
Also used : EntityType(com.vaticle.typedb.core.concept.type.EntityType) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) RelationType(com.vaticle.typedb.core.concept.type.RelationType) Then(io.cucumber.java.en.Then)

Example 13 with RelationType

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

the class UnifyRelationConcludableTest method addRolePlayer.

private void addRolePlayer(Relation relation, String role, Thing player) {
    RelationType relationType = relation.getType();
    RoleType roleType = relationType.getRelates(role);
    assert roleType != null : "Role type " + role + " does not exist in relation type " + relation.getType().getLabel();
    relation.addPlayer(roleType, player);
}
Also used : RoleType(com.vaticle.typedb.core.concept.type.RoleType) RelationType(com.vaticle.typedb.core.concept.type.RelationType)

Example 14 with RelationType

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

the class ReasonerTest method test_relation_rule.

@Test
public void test_relation_rule() {
    try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.SCHEMA)) {
        try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.WRITE)) {
            ConceptManager conceptMgr = txn.concepts();
            LogicManager logicMgr = txn.logic();
            EntityType person = conceptMgr.putEntityType("person");
            AttributeType name = conceptMgr.putAttributeType("name", AttributeType.ValueType.STRING);
            person.setOwns(name);
            RelationType friendship = conceptMgr.putRelationType("friendship");
            friendship.setRelates("friend");
            RelationType marriage = conceptMgr.putRelationType("marriage");
            marriage.setRelates("husband");
            marriage.setRelates("wife");
            person.setPlays(friendship.getRelates("friend"));
            person.setPlays(marriage.getRelates("husband"));
            person.setPlays(marriage.getRelates("wife"));
            logicMgr.putRule("marriage-is-friendship", TypeQL.parsePattern("{ $x isa person; $y isa person; (husband: $x, wife: $y) isa marriage; }").asConjunction(), TypeQL.parseVariable("(friend: $x, friend: $y) isa friendship").asThing());
            txn.commit();
        }
    }
    try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.DATA)) {
        try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.WRITE)) {
            txn.query().insert(TypeQL.parseQuery("insert $x isa person, has name 'Zack'; $y isa person, has name 'Yasmin'; (husband: $x, wife: $y) isa marriage;").asInsert());
            txn.commit();
        }
        try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.READ)) {
            List<ConceptMap> ans = txn.query().match(TypeQL.parseQuery("match $f (friend: $p1, friend: $p2) isa friendship; $p1 has name $na;").asMatch()).toList();
            ans.iterator().forEachRemaining(a -> {
                assertEquals("friendship", a.get("f").asThing().getType().getLabel().scopedName());
                assertEquals("person", a.get("p1").asThing().getType().getLabel().scopedName());
                assertEquals("person", a.get("p2").asThing().getType().getLabel().scopedName());
                assertEquals("name", a.get("na").asAttribute().getType().getLabel().scopedName());
            });
            assertEquals(2, ans.size());
        }
    }
}
Also used : ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) EntityType(com.vaticle.typedb.core.concept.type.EntityType) LogicManager(com.vaticle.typedb.core.logic.LogicManager) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) RelationType(com.vaticle.typedb.core.concept.type.RelationType) CoreSession(com.vaticle.typedb.core.database.CoreSession) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) Test(org.junit.Test)

Example 15 with RelationType

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

the class ExplanationTest method test_nested_explanations.

@Test
public void test_nested_explanations() {
    try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.SCHEMA)) {
        try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.WRITE)) {
            ConceptManager conceptMgr = txn.concepts();
            LogicManager logicMgr = txn.logic();
            EntityType person = conceptMgr.putEntityType("person");
            AttributeType name = conceptMgr.putAttributeType("name", AttributeType.ValueType.STRING);
            AttributeType gender = conceptMgr.putAttributeType("gender", AttributeType.ValueType.STRING);
            EntityType city = conceptMgr.putEntityType("city");
            person.setOwns(name);
            person.setOwns(gender);
            RelationType friendship = conceptMgr.putRelationType("friendship");
            friendship.setRelates("friend");
            RelationType marriage = conceptMgr.putRelationType("marriage");
            marriage.setRelates("husband");
            marriage.setRelates("wife");
            RelationType wedding = conceptMgr.putRelationType("wedding");
            wedding.setRelates("male");
            wedding.setRelates("female");
            wedding.setRelates("location");
            person.setPlays(friendship.getRelates("friend"));
            person.setPlays(marriage.getRelates("husband"));
            person.setPlays(marriage.getRelates("wife"));
            person.setPlays(wedding.getRelates("male"));
            person.setPlays(wedding.getRelates("female"));
            city.setPlays(wedding.getRelates("location"));
            logicMgr.putRule("wedding-implies-marriage", TypeQL.parsePattern("{ $x isa person, has gender \"male\"; $y isa person, has gender \"female\"; " + "$l isa city; (male: $x, female: $y, location: $l) isa wedding; }").asConjunction(), TypeQL.parseVariable("(husband: $x, wife: $y) isa marriage").asThing());
            logicMgr.putRule("marriage-is-friendship", TypeQL.parsePattern("{ $a isa person; $b isa person; (husband: $a, wife: $b) isa marriage; }").asConjunction(), TypeQL.parseVariable("(friend: $a, friend: $b) isa friendship").asThing());
            txn.commit();
        }
    }
    try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.DATA)) {
        try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.WRITE)) {
            txn.query().insert(TypeQL.parseQuery("insert " + "(male: $x, female: $y, location: $l) isa wedding;" + "$x isa person, has gender \"male\";" + "$y isa person, has gender \"female\";" + "$l isa city;").asInsert());
            txn.commit();
        }
        try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.READ, (new Options.Transaction().explain(true)))) {
            List<ConceptMap> ans = txn.query().match(TypeQL.parseQuery("match ($x) isa friendship;").asMatch()).toList();
            assertEquals(2, ans.size());
            assertFalse(ans.get(0).explainables().isEmpty());
            List<Explanation> explanations = assertSingleExplainableExplanations(ans.get(0), 1, 1, 1, txn);
            Explanation explanation = explanations.get(0);
            assertEquals(txn.logic().getRule("marriage-is-friendship"), explanation.rule());
            assertEquals(2, explanation.variableMapping().size());
            assertEquals(3, explanation.conclusionAnswer().concepts().size());
            ConceptMap marriageIsFriendshipAnswer = explanation.conditionAnswer();
            assertEquals(1, marriageIsFriendshipAnswer.explainables().iterator().count());
            assertSingleExplainableExplanations(marriageIsFriendshipAnswer, 1, 1, 1, txn);
        }
    }
}
Also used : Options(com.vaticle.typedb.core.common.parameters.Options) Explanation(com.vaticle.typedb.core.reasoner.resolution.answer.Explanation) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) EntityType(com.vaticle.typedb.core.concept.type.EntityType) LogicManager(com.vaticle.typedb.core.logic.LogicManager) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) RelationType(com.vaticle.typedb.core.concept.type.RelationType) CoreSession(com.vaticle.typedb.core.database.CoreSession) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Test(org.junit.Test)

Aggregations

RelationType (com.vaticle.typedb.core.concept.type.RelationType)25 EntityType (com.vaticle.typedb.core.concept.type.EntityType)20 AttributeType (com.vaticle.typedb.core.concept.type.AttributeType)18 Test (org.junit.Test)17 ConceptManager (com.vaticle.typedb.core.concept.ConceptManager)14 CoreSession (com.vaticle.typedb.core.database.CoreSession)12 CoreTransaction (com.vaticle.typedb.core.database.CoreTransaction)12 ConceptMap (com.vaticle.typedb.core.concept.answer.ConceptMap)9 RoleType (com.vaticle.typedb.core.concept.type.RoleType)9 CoreDatabaseManager (com.vaticle.typedb.core.database.CoreDatabaseManager)9 TypeDB (com.vaticle.typedb.core.TypeDB)7 LogicManager (com.vaticle.typedb.core.logic.LogicManager)7 Options (com.vaticle.typedb.core.common.parameters.Options)4 Conjunction (com.vaticle.typedb.core.pattern.Conjunction)4 Variable (com.vaticle.typedb.core.pattern.variable.Variable)4 ThingVariable (com.vaticle.typeql.lang.pattern.variable.ThingVariable)4 FunctionalIterator (com.vaticle.typedb.core.common.iterator.FunctionalIterator)3 MB (com.vaticle.typedb.core.common.collection.Bytes.MB)2 Arguments (com.vaticle.typedb.core.common.parameters.Arguments)2 Database (com.vaticle.typedb.core.common.parameters.Options.Database)2