Search in sources :

Example 1 with Entity

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

the class TypeInferenceTest method iid_inference.

@Test
public void iid_inference() throws IOException {
    define_standard_schema("basic-schema");
    transaction.commit();
    session.close();
    session = databaseMgr.session(database, DATA);
    transaction = session.transaction(WRITE);
    Entity person = transaction.concepts().getEntityType("person").create();
    TypeInference typeInference = transaction.logic().typeInference();
    // using a person IID, the attribute can only be a name or email, but not a dog label
    String queryString = "match $p iid " + person.getIID().toHexString() + "; $p has $a;";
    Disjunction disjunction = createDisjunction(queryString);
    typeInference.applyCombination(disjunction);
    Map<String, Set<String>> expected = new HashMap<>() {

        {
            put("$p", set("person"));
            put("$a", set("name", "email"));
        }
    };
    assertEquals(expected, resolvedTypeMap(disjunction.conjunctions().get(0)));
}
Also used : Entity(com.vaticle.typedb.core.concept.thing.Entity) TypeInference(com.vaticle.typedb.core.logic.tool.TypeInference) Disjunction(com.vaticle.typedb.core.pattern.Disjunction) Set(java.util.Set) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 2 with Entity

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

the class QueryTest method test_query_insert.

@Test
public void test_query_insert() throws IOException {
    Util.resetDirectory(dataDir);
    try (TypeDB.DatabaseManager typedb = CoreDatabaseManager.open(options)) {
        typedb.create(database);
        try (TypeDB.Session session = typedb.session(database, Arguments.Session.Type.SCHEMA)) {
            try (TypeDB.Transaction transaction = session.transaction(Arguments.Transaction.Type.WRITE)) {
                TypeQLDefine query = TypeQL.parseQuery(new String(Files.readAllBytes(Paths.get("test/integration/schema.tql")), UTF_8));
                transaction.query().define(query);
                transaction.commit();
            }
        }
        try (TypeDB.Session session = typedb.session(database, Arguments.Session.Type.DATA)) {
            try (TypeDB.Transaction transaction = session.transaction(Arguments.Transaction.Type.WRITE)) {
                String queryString = "insert " + "$n 'vaticle' isa name; " + "$o isa organisation, has name $n; " + "$t isa team, has name 'engineers', has symbol 'vaticle/engineers'; " + "$u isa user, has name 'butler', has email 'butler@vaticle.com'; " + "($o, $t) isa org-team; " + "($o, $u) isa org-member; " + "($t, $u) isa team-member;";
                TypeQLInsert query = TypeQL.parseQuery(queryString);
                transaction.query().insert(query);
                transaction.commit();
            }
            try (TypeDB.Transaction transaction = session.transaction(Arguments.Transaction.Type.READ)) {
                Attribute.String name_vaticle = transaction.concepts().getAttributeType("name").asString().get("vaticle");
                Attribute.String symbol_engineers = transaction.concepts().getAttributeType("symbol").asString().get("vaticle/engineers");
                Attribute.String email_butler = transaction.concepts().getAttributeType("email").asString().get("butler@vaticle.com");
                assertNotNulls(name_vaticle, symbol_engineers, email_butler);
                Entity organisation_vaticle = name_vaticle.getOwners().first().get().asEntity();
                Entity team_engineers = symbol_engineers.getOwners().first().get().asEntity();
                Entity user_butler = email_butler.getOwners().first().get().asEntity();
                assertNotNulls(organisation_vaticle, team_engineers, user_butler);
                assertEquals(organisation_vaticle.getRelations("org-team:org").first().get().getPlayers("team").first().get(), team_engineers);
                assertEquals(organisation_vaticle.getRelations("org-member:org").first().get().getPlayers("member").first().get(), user_butler);
                assertEquals(team_engineers.getRelations("team-member:team").first().get().getPlayers("member").first().get(), user_butler);
            }
        }
    }
}
Also used : Entity(com.vaticle.typedb.core.concept.thing.Entity) TypeQLDefine(com.vaticle.typeql.lang.query.TypeQLDefine) Attribute(com.vaticle.typedb.core.concept.thing.Attribute) TypeDB(com.vaticle.typedb.core.TypeDB) TypeQLInsert(com.vaticle.typeql.lang.query.TypeQLInsert) Test(org.junit.Test)

Example 3 with Entity

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

the class RuleTest method rule_has_explicit_materialises_when_missing.

@Test
public void rule_has_explicit_materialises_when_missing() throws IOException {
    Util.resetDirectory(dataDir);
    try (CoreDatabaseManager databaseMgr = CoreDatabaseManager.open(options)) {
        databaseMgr.create(database);
        try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.SCHEMA)) {
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
                ConceptManager conceptMgr = txn.concepts();
                EntityType milk = conceptMgr.putEntityType("milk");
                AttributeType ageInDays = conceptMgr.putAttributeType("age-in-days", AttributeType.ValueType.LONG);
                AttributeType isStillGood = conceptMgr.putAttributeType("is-still-good", AttributeType.ValueType.BOOLEAN);
                milk.setOwns(ageInDays);
                milk.setOwns(isStillGood);
                txn.logic().putRule("old-milk-is-not-good", TypeQL.parsePattern("{ $x isa milk, has age-in-days $a; $a >= 10; }").asConjunction(), TypeQL.parseVariable("$x has is-still-good false").asThing());
                txn.commit();
            }
        }
        try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.DATA)) {
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
                ConceptManager conceptMgr = txn.concepts();
                EntityType milk = conceptMgr.getEntityType("milk");
                AttributeType ageInDays = conceptMgr.getAttributeType("age-in-days");
                Entity milkInst = milk.create();
                milkInst.setHas(ageInDays.asLong().put(20L));
                Rule rule = txn.logic().getRule("old-milk-is-not-good");
                ConceptMap whenAnswer = new ConceptMap(map(pair(Identifier.Variable.name("x"), milkInst)));
                List<Map<Identifier.Variable, Concept>> materialisations = rule.conclusion().materialise(whenAnswer, txn.traversal(), conceptMgr).toList();
                assertEquals(1, materialisations.size());
                assertEquals(3, materialisations.get(0).size());
                AttributeType isStillGood = conceptMgr.getAttributeType("is-still-good");
                List<? extends Attribute> isStillGoodOwned = milkInst.getHas(isStillGood).toList();
                assertEquals(1, isStillGoodOwned.size());
                assertEquals(isStillGood.asBoolean().getInstances().first().get(), isStillGoodOwned.get(0));
            }
        }
    }
}
Also used : Entity(com.vaticle.typedb.core.concept.thing.Entity) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) EntityType(com.vaticle.typedb.core.concept.type.EntityType) CoreDatabaseManager(com.vaticle.typedb.core.database.CoreDatabaseManager) Identifier(com.vaticle.typedb.core.traversal.common.Identifier) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) CoreSession(com.vaticle.typedb.core.database.CoreSession) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Map(java.util.Map) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Test(org.junit.Test)

Example 4 with Entity

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

the class EntitySteps method entity_type_create_new_instance_with_key.

@When("{var} = entity\\( ?{type_label} ?) create new instance with key\\( ?{type_label} ?): {word}")
public void entity_type_create_new_instance_with_key(String var, String type, String keyType, String keyValue) {
    Attribute.String key = tx().concepts().getAttributeType(keyType).asString().put(keyValue);
    Entity entity = tx().concepts().getEntityType(type).create();
    entity.setHas(key);
    put(var, entity);
}
Also used : Entity(com.vaticle.typedb.core.concept.thing.Entity) Attribute(com.vaticle.typedb.core.concept.thing.Attribute) When(io.cucumber.java.en.When)

Example 5 with Entity

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

the class EntitySteps method entity_type_create_new_instance_with_key.

@When("{var} = entity\\( ?{type_label} ?) create new instance with key\\( ?{type_label} ?): {int}")
public void entity_type_create_new_instance_with_key(String var, String type, String keyType, int keyValue) {
    Attribute.Long key = tx().concepts().getAttributeType(keyType).asLong().put(keyValue);
    Entity entity = tx().concepts().getEntityType(type).create();
    entity.setHas(key);
    put(var, entity);
}
Also used : Entity(com.vaticle.typedb.core.concept.thing.Entity) Attribute(com.vaticle.typedb.core.concept.thing.Attribute) When(io.cucumber.java.en.When)

Aggregations

Entity (com.vaticle.typedb.core.concept.thing.Entity)7 Attribute (com.vaticle.typedb.core.concept.thing.Attribute)5 Test (org.junit.Test)4 When (io.cucumber.java.en.When)3 ConceptManager (com.vaticle.typedb.core.concept.ConceptManager)2 ConceptMap (com.vaticle.typedb.core.concept.answer.ConceptMap)2 AttributeType (com.vaticle.typedb.core.concept.type.AttributeType)2 EntityType (com.vaticle.typedb.core.concept.type.EntityType)2 CoreDatabaseManager (com.vaticle.typedb.core.database.CoreDatabaseManager)2 CoreSession (com.vaticle.typedb.core.database.CoreSession)2 CoreTransaction (com.vaticle.typedb.core.database.CoreTransaction)2 Identifier (com.vaticle.typedb.core.traversal.common.Identifier)2 Map (java.util.Map)2 TypeDB (com.vaticle.typedb.core.TypeDB)1 TypeInference (com.vaticle.typedb.core.logic.tool.TypeInference)1 Disjunction (com.vaticle.typedb.core.pattern.Disjunction)1 TypeQLDefine (com.vaticle.typeql.lang.query.TypeQLDefine)1 TypeQLInsert (com.vaticle.typeql.lang.query.TypeQLInsert)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1