use of com.vaticle.typedb.core.concept.type.AttributeType in project grakn by graknlabs.
the class ThingTypeSteps method thing_type_set_owns_attribute_as_throws_exception.
@Then("{root_label}\\( ?{type_label} ?) set owns attribute type: {type_label} as {type_label}; throws exception")
public void thing_type_set_owns_attribute_as_throws_exception(RootLabel rootLabel, String typeLabel, String attributeLabel, String overriddenLabel) {
AttributeType attributeType = tx().concepts().getAttributeType(attributeLabel);
AttributeType overriddenType = tx().concepts().getAttributeType(overriddenLabel);
assertThrows(() -> get_thing_type(rootLabel, typeLabel).setOwns(attributeType, overriddenType));
}
use of com.vaticle.typedb.core.concept.type.AttributeType in project grakn by graknlabs.
the class ThingTypeSteps method thing_type_set_supertype.
@When("{root_label}\\( ?{type_label} ?) set supertype: {type_label}")
public void thing_type_set_supertype(RootLabel rootLabel, String typeLabel, String superLabel) {
switch(rootLabel) {
case ENTITY:
EntityType entitySuperType = tx().concepts().getEntityType(superLabel);
tx().concepts().getEntityType(typeLabel).setSupertype(entitySuperType);
break;
case ATTRIBUTE:
AttributeType attributeSuperType = tx().concepts().getAttributeType(superLabel);
tx().concepts().getAttributeType(typeLabel).setSupertype(attributeSuperType);
break;
case RELATION:
RelationType relationSuperType = tx().concepts().getRelationType(superLabel);
tx().concepts().getRelationType(typeLabel).setSupertype(relationSuperType);
break;
}
}
use of com.vaticle.typedb.core.concept.type.AttributeType 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));
}
}
}
}
use of com.vaticle.typedb.core.concept.type.AttributeType 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());
}
}
}
}
use of com.vaticle.typedb.core.concept.type.AttributeType in project grakn by graknlabs.
the class RuleTest method rule_when_that_cannot_be_satisfied_throws_an_error.
@Test
public void rule_when_that_cannot_be_satisfied_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 person = conceptMgr.putEntityType("person");
final EntityType dog = conceptMgr.putEntityType("dog");
final AttributeType name = conceptMgr.putAttributeType("name", AttributeType.ValueType.STRING);
person.setOwns(name);
// a when using an illegal comparator
assertThrowsTypeDBException(() -> txn.logic().putRule("two-unique-dogs-exist-called-fido", TypeQL.parsePattern("{$x isa dog; $y isa dog; $x != $y;}").asConjunction(), TypeQL.parseVariable("$x has name 'fido'").asThing()), RULE_WHEN_CANNOT_BE_SATISFIED.code());
}
}
}
}
Aggregations