Search in sources :

Example 1 with Database

use of com.vaticle.typedb.core.common.parameters.Options.Database in project grakn by graknlabs.

the class QueryTest method test_query_undefine.

@Test
public void test_query_undefine() 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.Transaction transaction = session.transaction(Arguments.Transaction.Type.WRITE)) {
                String queryString = "undefine analysis abstract, owns created, plays commit-analysis:analysis;";
                TypeQLUndefine query = TypeQL.parseQuery(queryString);
                transaction.query().undefine(query);
                queryString = "undefine rule performance-tracker-rule;";
                query = TypeQL.parseQuery(queryString);
                transaction.query().undefine(query);
                queryString = "undefine performance-tracker relates tracker;";
                query = TypeQL.parseQuery(queryString);
                transaction.query().undefine(query);
                queryString = "undefine email regex '.+\\@.+\\..+';";
                query = TypeQL.parseQuery(queryString);
                transaction.query().undefine(query);
                queryString = "undefine index sub attribute, value long;";
                query = TypeQL.parseQuery(queryString);
                transaction.query().undefine(query);
                // undefine first 4 rules
                queryString = "undefine rule repo-fork-rule;";
                query = TypeQL.parseQuery(queryString);
                transaction.query().undefine(query);
                queryString = "undefine rule repo-dependency-transitive-rule;";
                query = TypeQL.parseQuery(queryString);
                transaction.query().undefine(query);
                queryString = "undefine rule repo-dependency-transitive-type-rule;";
                query = TypeQL.parseQuery(queryString);
                transaction.query().undefine(query);
                queryString = "undefine rule repo-collaborator-org-rule;";
                query = TypeQL.parseQuery(queryString);
                transaction.query().undefine(query);
                transaction.commit();
            }
            try (TypeDB.Transaction tx = session.transaction(Arguments.Transaction.Type.READ)) {
                EntityType analysis = tx.concepts().getEntityType("analysis");
                RelationType performanceTracker = tx.concepts().getRelationType("performance-tracker");
                RoleType commitAnalysisAnalysis = tx.concepts().getRelationType("commit-analysis").getRelates("analysis");
                AttributeType.DateTime created = tx.concepts().getAttributeType("created").asDateTime();
                AttributeType.String email = tx.concepts().getAttributeType("email").asString();
                assertNotNulls(analysis, performanceTracker, commitAnalysisAnalysis, created, email);
                assertFalse(analysis.isAbstract());
                assertTrue(analysis.getOwns().noneMatch(att -> att.equals(created)));
                assertTrue(analysis.getPlays().noneMatch(rol -> rol.equals(commitAnalysisAnalysis)));
                assertTrue(performanceTracker.getRelates().noneMatch(rol -> rol.getLabel().name().equals("tracker")));
                assertNull(email.getRegex());
                AttributeType index = tx.concepts().getAttributeType("index");
                assertNull(index);
                assertNull(tx.logic().getRule("repo-fork-rule"));
                assertNull(tx.logic().getRule("repo-dependency-transitive-rule"));
                assertNull(tx.logic().getRule("repo-dependency-transitive-type-rule"));
                assertNull(tx.logic().getRule("repo-collaborator-org-rule"));
                // check total count
                assertEquals(15 - 5, tx.logic().rules().toList().size());
                // a query that used to trigger a rule should not cause an error
                List<ConceptMap> answers = tx.query().match(TypeQL.parseQuery("match $x isa repo-fork;").asMatch()).toList();
            }
        }
    }
}
Also used : TypeQLDelete(com.vaticle.typeql.lang.query.TypeQLDelete) TypeQLInsert(com.vaticle.typeql.lang.query.TypeQLInsert) MB(com.vaticle.typedb.core.common.collection.Bytes.MB) TypeQLUndefine(com.vaticle.typeql.lang.query.TypeQLUndefine) Util.assertNotNulls(com.vaticle.typedb.core.test.integration.util.Util.assertNotNulls) RelationType(com.vaticle.typedb.core.concept.type.RelationType) Arguments(com.vaticle.typedb.core.common.parameters.Arguments) Path(java.nio.file.Path) TypeQLDefine(com.vaticle.typeql.lang.query.TypeQLDefine) TypeQL(com.vaticle.typeql.lang.TypeQL) FunctionalIterator(com.vaticle.typedb.core.common.iterator.FunctionalIterator) CoreDatabaseManager(com.vaticle.typedb.core.database.CoreDatabaseManager) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Test(org.junit.Test) IOException(java.io.IOException) RoleType(com.vaticle.typedb.core.concept.type.RoleType) TypeDB(com.vaticle.typedb.core.TypeDB) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Paths(java.nio.file.Paths) Assert.assertFalse(org.junit.Assert.assertFalse) Attribute(com.vaticle.typedb.core.concept.thing.Attribute) Database(com.vaticle.typedb.core.common.parameters.Options.Database) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) EntityType(com.vaticle.typedb.core.concept.type.EntityType) TypeQLMatch(com.vaticle.typeql.lang.query.TypeQLMatch) Entity(com.vaticle.typedb.core.concept.thing.Entity) Assert.assertEquals(org.junit.Assert.assertEquals) Util(com.vaticle.typedb.core.test.integration.util.Util) TypeQLUndefine(com.vaticle.typeql.lang.query.TypeQLUndefine) RoleType(com.vaticle.typedb.core.concept.type.RoleType) TypeDB(com.vaticle.typedb.core.TypeDB) EntityType(com.vaticle.typedb.core.concept.type.EntityType) TypeQLDefine(com.vaticle.typeql.lang.query.TypeQLDefine) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) RelationType(com.vaticle.typedb.core.concept.type.RelationType) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Test(org.junit.Test)

Example 2 with Database

use of com.vaticle.typedb.core.common.parameters.Options.Database in project grakn by graknlabs.

the class QueryTest method test_query_define.

@Test
public void test_query_define() 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.Transaction tx = session.transaction(Arguments.Transaction.Type.READ)) {
                AttributeType.String name = tx.concepts().getAttributeType("name").asString();
                AttributeType.String symbol = tx.concepts().getAttributeType("symbol").asString();
                AttributeType.Boolean active = tx.concepts().getAttributeType("active").asBoolean();
                AttributeType.Long priority = tx.concepts().getAttributeType("priority").asLong();
                assertNotNulls(name, symbol, active, priority);
                EntityType organisation = tx.concepts().getEntityType("organisation");
                EntityType team = tx.concepts().getEntityType("team");
                EntityType user = tx.concepts().getEntityType("user");
                EntityType repository = tx.concepts().getEntityType("repository");
                EntityType branchRule = tx.concepts().getEntityType("branch-rule");
                EntityType commit = tx.concepts().getEntityType("commit");
                assertNotNulls(organisation, team, user, repository, branchRule, commit);
                assertTrue(organisation.getOwns().anyMatch(a -> a.equals(name)));
                assertTrue(team.getOwns().anyMatch(a -> a.equals(symbol)));
                assertTrue(user.getOwns().anyMatch(a -> a.equals(name)));
                assertTrue(repository.getOwns().anyMatch(a -> a.equals(active)));
                assertTrue(branchRule.getOwns().anyMatch(a -> a.equals(priority)));
                assertTrue(commit.getOwns().anyMatch(a -> a.equals(symbol)));
                RelationType orgTeam = tx.concepts().getRelationType("org-team");
                RelationType teamMember = tx.concepts().getRelationType("team-member");
                RelationType repoDependency = tx.concepts().getRelationType("repo-dependency");
                assertNotNulls(orgTeam, teamMember, repoDependency);
                RoleType orgTeam_org = orgTeam.getRelates("org");
                RoleType orgTeam_team = orgTeam.getRelates("team");
                RoleType teamMember_team = teamMember.getRelates("team");
                RoleType teamMember_member = teamMember.getRelates("member");
                assertNotNulls(orgTeam_org, orgTeam_team, teamMember_team, teamMember_member);
                assertTrue(organisation.getPlays().anyMatch(r -> r.equals(orgTeam_org)));
                assertTrue(team.getPlays().anyMatch(r -> r.equals(orgTeam_team)));
                assertTrue(team.getPlays().anyMatch(r -> r.equals(teamMember_team)));
                assertTrue(user.getPlays().anyMatch(r -> r.equals(teamMember_member)));
                // check first 4 rules
                assertNotNull(tx.logic().getRule("repo-fork-rule"));
                assertNotNull(tx.logic().getRule("repo-dependency-transitive-rule"));
                assertNotNull(tx.logic().getRule("repo-dependency-transitive-type-rule"));
                assertNotNull(tx.logic().getRule("repo-collaborator-org-rule"));
                // check total count
                assertEquals(15, tx.logic().rules().toList().size());
            }
        }
    }
}
Also used : TypeQLDelete(com.vaticle.typeql.lang.query.TypeQLDelete) TypeQLInsert(com.vaticle.typeql.lang.query.TypeQLInsert) MB(com.vaticle.typedb.core.common.collection.Bytes.MB) TypeQLUndefine(com.vaticle.typeql.lang.query.TypeQLUndefine) Util.assertNotNulls(com.vaticle.typedb.core.test.integration.util.Util.assertNotNulls) RelationType(com.vaticle.typedb.core.concept.type.RelationType) Arguments(com.vaticle.typedb.core.common.parameters.Arguments) Path(java.nio.file.Path) TypeQLDefine(com.vaticle.typeql.lang.query.TypeQLDefine) TypeQL(com.vaticle.typeql.lang.TypeQL) FunctionalIterator(com.vaticle.typedb.core.common.iterator.FunctionalIterator) CoreDatabaseManager(com.vaticle.typedb.core.database.CoreDatabaseManager) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Test(org.junit.Test) IOException(java.io.IOException) RoleType(com.vaticle.typedb.core.concept.type.RoleType) TypeDB(com.vaticle.typedb.core.TypeDB) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Paths(java.nio.file.Paths) Assert.assertFalse(org.junit.Assert.assertFalse) Attribute(com.vaticle.typedb.core.concept.thing.Attribute) Database(com.vaticle.typedb.core.common.parameters.Options.Database) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) EntityType(com.vaticle.typedb.core.concept.type.EntityType) TypeQLMatch(com.vaticle.typeql.lang.query.TypeQLMatch) Entity(com.vaticle.typedb.core.concept.thing.Entity) Assert.assertEquals(org.junit.Assert.assertEquals) Util(com.vaticle.typedb.core.test.integration.util.Util) RoleType(com.vaticle.typedb.core.concept.type.RoleType) TypeDB(com.vaticle.typedb.core.TypeDB) EntityType(com.vaticle.typedb.core.concept.type.EntityType) TypeQLDefine(com.vaticle.typeql.lang.query.TypeQLDefine) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) RelationType(com.vaticle.typedb.core.concept.type.RelationType) Test(org.junit.Test)

Aggregations

TypeDB (com.vaticle.typedb.core.TypeDB)2 MB (com.vaticle.typedb.core.common.collection.Bytes.MB)2 FunctionalIterator (com.vaticle.typedb.core.common.iterator.FunctionalIterator)2 Arguments (com.vaticle.typedb.core.common.parameters.Arguments)2 Database (com.vaticle.typedb.core.common.parameters.Options.Database)2 ConceptMap (com.vaticle.typedb.core.concept.answer.ConceptMap)2 Attribute (com.vaticle.typedb.core.concept.thing.Attribute)2 Entity (com.vaticle.typedb.core.concept.thing.Entity)2 AttributeType (com.vaticle.typedb.core.concept.type.AttributeType)2 EntityType (com.vaticle.typedb.core.concept.type.EntityType)2 RelationType (com.vaticle.typedb.core.concept.type.RelationType)2 RoleType (com.vaticle.typedb.core.concept.type.RoleType)2 CoreDatabaseManager (com.vaticle.typedb.core.database.CoreDatabaseManager)2 Util (com.vaticle.typedb.core.test.integration.util.Util)2 Util.assertNotNulls (com.vaticle.typedb.core.test.integration.util.Util.assertNotNulls)2 TypeQL (com.vaticle.typeql.lang.TypeQL)2 TypeQLDefine (com.vaticle.typeql.lang.query.TypeQLDefine)2 TypeQLDelete (com.vaticle.typeql.lang.query.TypeQLDelete)2 TypeQLInsert (com.vaticle.typeql.lang.query.TypeQLInsert)2 TypeQLMatch (com.vaticle.typeql.lang.query.TypeQLMatch)2