Search in sources :

Example 6 with CoreDatabaseManager

use of com.vaticle.typedb.core.database.CoreDatabaseManager in project grakn by graknlabs.

the class RuleTest method rule_with_ambiguous_then_throws.

@Test
public void rule_with_ambiguous_then_throws() 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)) {
                txn.query().define(TypeQL.parseQuery("define " + "person sub entity, plays marriage:husband, plays marriage:wife;" + "marriage sub relation, relates husband, relates wife;"));
                assertThrows(() -> txn.logic().putRule("invalid-marriage-insertion", TypeQL.parsePattern("{$x isa person;}").asConjunction(), TypeQL.parseVariable("(role: $x) isa marriage;").asThing()));
            }
        }
    }
}
Also used : CoreDatabaseManager(com.vaticle.typedb.core.database.CoreDatabaseManager) CoreSession(com.vaticle.typedb.core.database.CoreSession) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) Test(org.junit.Test)

Example 7 with CoreDatabaseManager

use of com.vaticle.typedb.core.database.CoreDatabaseManager in project grakn by graknlabs.

the class RuleTest method rule_that_cannot_be_inserted_throws_an_error.

@Test
public void rule_that_cannot_be_inserted_throws_an_error() 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)) {
                final ConceptManager conceptMgr = txn.concepts();
                final EntityType animal = conceptMgr.putEntityType("animal");
                final EntityType person = conceptMgr.putEntityType("person");
                final EntityType dog = conceptMgr.putEntityType("dog");
                final AttributeType name = conceptMgr.putAttributeType("name", AttributeType.ValueType.STRING);
                person.setOwns(name);
                person.setSupertype(animal);
                dog.setSupertype(animal);
                assertThrows(() -> txn.logic().putRule("animals-are-named-fido", TypeQL.parsePattern("{$x isa animal;}").asConjunction(), TypeQL.parseVariable("$x has name 'fido'").asThing()));
            }
        }
    }
}
Also used : ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) EntityType(com.vaticle.typedb.core.concept.type.EntityType) CoreDatabaseManager(com.vaticle.typedb.core.database.CoreDatabaseManager) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) CoreSession(com.vaticle.typedb.core.database.CoreSession) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) Test(org.junit.Test)

Example 8 with CoreDatabaseManager

use of com.vaticle.typedb.core.database.CoreDatabaseManager in project grakn by graknlabs.

the class MigratorTest method test_import_export_data.

@Test
public void test_import_export_data() throws IOException {
    Util.resetDirectory(dataDir);
    try (CoreDatabaseManager databaseMgr = CoreDatabaseManager.open(options)) {
        databaseMgr.create(database);
        String schema = new String(Files.readAllBytes(schemaPath), UTF_8);
        runSchema(databaseMgr, schema);
        new DataImporter(databaseMgr, database, dataPath, Version.VERSION).run();
        new DataExporter(databaseMgr, database, exportDataPath, Version.VERSION).run();
        assertEquals(getChecksums(dataPath), getChecksums(exportDataPath));
    }
}
Also used : DataExporter(com.vaticle.typedb.core.migrator.data.DataExporter) CoreDatabaseManager(com.vaticle.typedb.core.database.CoreDatabaseManager) DataImporter(com.vaticle.typedb.core.migrator.data.DataImporter) Test(org.junit.Test)

Example 9 with CoreDatabaseManager

use of com.vaticle.typedb.core.database.CoreDatabaseManager in project grakn by graknlabs.

the class RuleTest method rule_has_variable_materialises_when_missing.

@Test
public void rule_has_variable_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; $a 10 isa age-in-days; }").asConjunction(), TypeQL.parseVariable("$x has $a").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();
                Attribute.Long ageInDays10 = ageInDays.asLong().put(10L);
                Rule rule = txn.logic().getRule("old-milk-is-not-good");
                ConceptMap whenAnswer = new ConceptMap(map(pair(Identifier.Variable.name("x"), milkInst), pair(Identifier.Variable.name("a"), ageInDays10)));
                List<Map<Identifier.Variable, Concept>> materialisations = rule.conclusion().materialise(whenAnswer, txn.traversal(), conceptMgr).toList();
                assertEquals(1, materialisations.size());
                assertEquals(2, materialisations.get(0).size());
                List<? extends Attribute> ageInDaysOwned = milkInst.getHas(ageInDays).toList();
                assertEquals(1, ageInDaysOwned.size());
                assertEquals(ageInDays10, ageInDaysOwned.get(0));
            }
        }
    }
}
Also used : Entity(com.vaticle.typedb.core.concept.thing.Entity) Attribute(com.vaticle.typedb.core.concept.thing.Attribute) 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 10 with CoreDatabaseManager

use of com.vaticle.typedb.core.database.CoreDatabaseManager in project grakn by graknlabs.

the class RuleTest method new_type_updates_rule_conclusion_index.

@Test
public void new_type_updates_rule_conclusion_index() 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();
                LogicManager logicMgr = txn.logic();
                EntityType person = conceptMgr.putEntityType("person");
                RelationType marriage = conceptMgr.putRelationType("marriage");
                AttributeType name = conceptMgr.putAttributeType("name", AttributeType.ValueType.STRING);
                name.setAbstract();
                AttributeType firstName = conceptMgr.putAttributeType("first-name", AttributeType.ValueType.STRING);
                firstName.setSupertype(name);
                marriage.setRelates("spouse");
                person.setPlays(marriage.getRelates("spouse"));
                person.setOwns(firstName);
                Rule marriageSameName = logicMgr.putRule("marriage-same-name", TypeQL.parsePattern("{ $x isa person, has name $a; $y isa person; (spouse:$x, spouse: $y) isa marriage; }").asConjunction(), TypeQL.parseVariable("$y has $a").asThing());
                Conjunction sameName = marriageSameName.then();
                Variable nameAttr = getVariable(sameName.variables(), Identifier.Variable.name("a"));
                assertEquals(set(Label.of("first-name")), nameAttr.inferredTypes());
                txn.commit();
            }
            // add a new subtype of an attribute in a rule
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
                ConceptManager conceptMgr = txn.concepts();
                AttributeType lastName = conceptMgr.putAttributeType("last-name", AttributeType.ValueType.STRING);
                lastName.setSupertype(conceptMgr.getAttributeType("name"));
                conceptMgr.getEntityType("person").setOwns(lastName);
                txn.commit();
            }
            // check the new attribute type is re-indexed in the conclusions index
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.READ)) {
                LogicManager logicMgr = txn.logic();
                Rule marriageSameName = logicMgr.getRule("marriage-same-name");
                assertEquals(set(marriageSameName), logicMgr.rulesConcludingHas(Label.of("last-name")).toSet());
            }
        }
    }
}
Also used : ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) EntityType(com.vaticle.typedb.core.concept.type.EntityType) CoreDatabaseManager(com.vaticle.typedb.core.database.CoreDatabaseManager) Variable(com.vaticle.typedb.core.pattern.variable.Variable) ThingVariable(com.vaticle.typeql.lang.pattern.variable.ThingVariable) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) RelationType(com.vaticle.typedb.core.concept.type.RelationType) Conjunction(com.vaticle.typedb.core.pattern.Conjunction) CoreSession(com.vaticle.typedb.core.database.CoreSession) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) Test(org.junit.Test)

Aggregations

CoreDatabaseManager (com.vaticle.typedb.core.database.CoreDatabaseManager)15 Test (org.junit.Test)15 CoreSession (com.vaticle.typedb.core.database.CoreSession)14 CoreTransaction (com.vaticle.typedb.core.database.CoreTransaction)14 ConceptManager (com.vaticle.typedb.core.concept.ConceptManager)12 EntityType (com.vaticle.typedb.core.concept.type.EntityType)12 AttributeType (com.vaticle.typedb.core.concept.type.AttributeType)10 RelationType (com.vaticle.typedb.core.concept.type.RelationType)7 ConceptMap (com.vaticle.typedb.core.concept.answer.ConceptMap)4 Conjunction (com.vaticle.typedb.core.pattern.Conjunction)4 Variable (com.vaticle.typedb.core.pattern.variable.Variable)4 Identifier (com.vaticle.typedb.core.traversal.common.Identifier)4 ThingVariable (com.vaticle.typeql.lang.pattern.variable.ThingVariable)4 Map (java.util.Map)4 Entity (com.vaticle.typedb.core.concept.thing.Entity)2 Attribute (com.vaticle.typedb.core.concept.thing.Attribute)1 GraphManager (com.vaticle.typedb.core.graph.GraphManager)1 DataExporter (com.vaticle.typedb.core.migrator.data.DataExporter)1 DataImporter (com.vaticle.typedb.core.migrator.data.DataImporter)1