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)));
}
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);
}
}
}
}
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));
}
}
}
}
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);
}
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);
}
Aggregations