Search in sources :

Example 1 with Pattern

use of com.vaticle.typeql.lang.pattern.Pattern in project grakn by graknlabs.

the class BasicTest method write_and_retrieve_attribute_ownership_rule.

@Test
public void write_and_retrieve_attribute_ownership_rule() 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 txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
                ConceptManager conceptMgr = txn.concepts();
                LogicManager logicMgr = txn.logic();
                AttributeType name = conceptMgr.putAttributeType("name", STRING);
                EntityType person = conceptMgr.putEntityType("person");
                RelationType friendship = conceptMgr.putRelationType("friendship");
                friendship.setRelates("friend");
                person.setPlays(friendship.getRelates("friend"));
                person.setOwns(name);
                logicMgr.putRule("people-have-names", TypeQL.parsePattern("{$x isa person; }").asConjunction(), TypeQL.parseVariable("$x has name \"i have a name\"").asThing());
                txn.commit();
            }
            try (TypeDB.Transaction txn = session.transaction(Arguments.Transaction.Type.READ)) {
                ConceptManager conceptMgr = txn.concepts();
                LogicManager logicMgr = txn.logic();
                Rule rule = logicMgr.getRule("people-have-names");
                Pattern when = rule.getWhenPreNormalised();
                ThingVariable<?> then = rule.getThenPreNormalised();
                assertEquals(TypeQL.parsePattern("{$x isa person;}"), when);
                assertEquals(TypeQL.parseVariable("$x has name \"i have a name\""), then);
            }
        }
    }
}
Also used : ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) EntityType(com.vaticle.typedb.core.concept.type.EntityType) Pattern(com.vaticle.typeql.lang.pattern.Pattern) LogicManager(com.vaticle.typedb.core.logic.LogicManager) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) RelationType(com.vaticle.typedb.core.concept.type.RelationType) Rule(com.vaticle.typedb.core.logic.Rule) TypeDB(com.vaticle.typedb.core.TypeDB) Test(org.junit.Test)

Example 2 with Pattern

use of com.vaticle.typeql.lang.pattern.Pattern in project grakn by graknlabs.

the class Rule method pruneThenResolvedTypes.

/**
 * Remove type hints in the `then` pattern that are not valid in the `when` pattern
 */
private void pruneThenResolvedTypes() {
    then.variables().stream().filter(variable -> variable.id().isName()).forEach(thenVar -> {
        Variable whenVar = when.variable(thenVar.id());
        thenVar.retainInferredTypes(whenVar.inferredTypes());
        if (thenVar.inferredTypes().isEmpty())
            then.setCoherent(false);
    });
}
Also used : RULE_THEN_INVALID_VALUE_ASSIGNMENT(com.vaticle.typedb.core.common.exception.ErrorMessage.RuleWrite.RULE_THEN_INVALID_VALUE_ASSIGNMENT) Identifier(com.vaticle.typedb.core.traversal.common.Identifier) LoggerFactory(org.slf4j.LoggerFactory) RelationType(com.vaticle.typedb.core.concept.type.RelationType) RULE_CONCLUSION_AMBIGUOUS_LABELLED_TYPE(com.vaticle.typedb.core.common.exception.ErrorMessage.RuleWrite.RULE_CONCLUSION_AMBIGUOUS_LABELLED_TYPE) COLON(com.vaticle.typeql.lang.common.TypeQLToken.Char.COLON) Map(java.util.Map) ValueConstraint(com.vaticle.typedb.core.pattern.constraint.thing.ValueConstraint) ThingConstraint(com.vaticle.typedb.core.pattern.constraint.thing.ThingConstraint) Collections.list(com.vaticle.typedb.common.collection.Collections.list) Thing(com.vaticle.typedb.core.concept.thing.Thing) THEN(com.vaticle.typeql.lang.common.TypeQLToken.Schema.THEN) NEW_LINE(com.vaticle.typeql.lang.common.TypeQLToken.Char.NEW_LINE) FunctionalIterator(com.vaticle.typedb.core.common.iterator.FunctionalIterator) Negation(com.vaticle.typedb.core.pattern.Negation) IsaConstraint(com.vaticle.typedb.core.pattern.constraint.thing.IsaConstraint) Set(java.util.Set) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Pattern(com.vaticle.typeql.lang.pattern.Pattern) HasConstraint(com.vaticle.typedb.core.pattern.constraint.thing.HasConstraint) CURLY_OPEN(com.vaticle.typeql.lang.common.TypeQLToken.Char.CURLY_OPEN) List(java.util.List) RelationConstraint(com.vaticle.typedb.core.pattern.constraint.thing.RelationConstraint) Optional(java.util.Optional) VariableRegistry(com.vaticle.typedb.core.pattern.variable.VariableRegistry) TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) RULE(com.vaticle.typeql.lang.common.TypeQLToken.Schema.RULE) ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) ILLEGAL_STATE(com.vaticle.typedb.core.common.exception.ErrorMessage.Internal.ILLEGAL_STATE) TraversalEngine(com.vaticle.typedb.core.traversal.TraversalEngine) Collections.set(com.vaticle.typedb.common.collection.Collections.set) Variable(com.vaticle.typedb.core.pattern.variable.Variable) SEMICOLON(com.vaticle.typeql.lang.common.TypeQLToken.Char.SEMICOLON) HashMap(java.util.HashMap) Objects.className(com.vaticle.typedb.common.util.Objects.className) RULE_THEN_CANNOT_BE_SATISFIED(com.vaticle.typedb.core.common.exception.ErrorMessage.RuleWrite.RULE_THEN_CANNOT_BE_SATISFIED) SPACE(com.vaticle.typeql.lang.common.TypeQLToken.Char.SPACE) RULE_CONCLUSION_ILLEGAL_INSERT(com.vaticle.typedb.core.common.exception.ErrorMessage.RuleWrite.RULE_CONCLUSION_ILLEGAL_INSERT) Reference(com.vaticle.typeql.lang.pattern.variable.Reference) Concept(com.vaticle.typedb.core.concept.Concept) HashSet(java.util.HashSet) Concludable(com.vaticle.typedb.core.logic.resolvable.Concludable) Label(com.vaticle.typedb.core.common.parameters.Label) GraphManager(com.vaticle.typedb.core.graph.GraphManager) Conjunction(com.vaticle.typedb.core.pattern.Conjunction) RelationTraversal(com.vaticle.typedb.core.traversal.RelationTraversal) Logger(org.slf4j.Logger) ThingVariable(com.vaticle.typedb.core.pattern.variable.ThingVariable) RuleStructure(com.vaticle.typedb.core.graph.structure.RuleStructure) Iterators(com.vaticle.typedb.core.common.iterator.Iterators) RULE_WHEN_CANNOT_BE_SATISFIED(com.vaticle.typedb.core.common.exception.ErrorMessage.RuleWrite.RULE_WHEN_CANNOT_BE_SATISFIED) RoleType(com.vaticle.typedb.core.concept.type.RoleType) Disjunction(com.vaticle.typedb.core.pattern.Disjunction) CURLY_CLOSE(com.vaticle.typeql.lang.common.TypeQLToken.Char.CURLY_CLOSE) Attribute(com.vaticle.typedb.core.concept.thing.Attribute) TypeVariable(com.vaticle.typedb.core.pattern.variable.TypeVariable) INVALID_CASTING(com.vaticle.typedb.core.common.exception.ErrorMessage.Pattern.INVALID_CASTING) Iterators.iterate(com.vaticle.typedb.core.common.iterator.Iterators.iterate) INVALID_NEGATION_CONTAINS_DISJUNCTION(com.vaticle.typedb.core.common.exception.ErrorMessage.RuleWrite.INVALID_NEGATION_CONTAINS_DISJUNCTION) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) WHEN(com.vaticle.typeql.lang.common.TypeQLToken.Schema.WHEN) Variable(com.vaticle.typedb.core.pattern.variable.Variable) ThingVariable(com.vaticle.typedb.core.pattern.variable.ThingVariable) TypeVariable(com.vaticle.typedb.core.pattern.variable.TypeVariable)

Example 3 with Pattern

use of com.vaticle.typeql.lang.pattern.Pattern in project grakn by graknlabs.

the class BasicTest method write_and_retrieve_relation_rule.

@Test
public void write_and_retrieve_relation_rule() 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 txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
                ConceptManager conceptMgr = txn.concepts();
                LogicManager logicMgr = txn.logic();
                EntityType person = conceptMgr.putEntityType("person");
                RelationType friendship = conceptMgr.putRelationType("friendship");
                friendship.setRelates("friend");
                RelationType marriage = conceptMgr.putRelationType("marriage");
                marriage.setRelates("spouse");
                person.setPlays(friendship.getRelates("friend"));
                person.setPlays(marriage.getRelates("spouse"));
                logicMgr.putRule("marriage-is-friendship", TypeQL.parsePattern("{$x isa person; $y isa person; (spouse: $x, spouse: $y) isa marriage; }").asConjunction(), TypeQL.parseVariable("(friend: $x, friend: $y) isa friendship").asThing());
                txn.commit();
            }
            try (TypeDB.Transaction txn = session.transaction(Arguments.Transaction.Type.READ)) {
                ConceptManager conceptMgr = txn.concepts();
                LogicManager logicMgr = txn.logic();
                EntityType person = conceptMgr.getEntityType("person");
                RelationType friendship = conceptMgr.getRelationType("friendship");
                RoleType friend = friendship.getRelates("friend");
                RelationType marriage = conceptMgr.getRelationType("marriage");
                RoleType spouse = marriage.getRelates("spouse");
                Rule rule = logicMgr.getRule("marriage-is-friendship");
                Pattern when = rule.getWhenPreNormalised();
                ThingVariable<?> then = rule.getThenPreNormalised();
                assertEquals(TypeQL.parsePattern("{$x isa person; $y isa person; (spouse: $x, spouse: $y) isa marriage; }"), when);
                assertEquals(TypeQL.parseVariable("(friend: $x, friend: $y) isa friendship"), then);
            }
        }
    }
}
Also used : ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) EntityType(com.vaticle.typedb.core.concept.type.EntityType) Pattern(com.vaticle.typeql.lang.pattern.Pattern) LogicManager(com.vaticle.typedb.core.logic.LogicManager) RoleType(com.vaticle.typedb.core.concept.type.RoleType) RelationType(com.vaticle.typedb.core.concept.type.RelationType) Rule(com.vaticle.typedb.core.logic.Rule) TypeDB(com.vaticle.typedb.core.TypeDB) Test(org.junit.Test)

Example 4 with Pattern

use of com.vaticle.typeql.lang.pattern.Pattern in project grakn by graknlabs.

the class Rule method whenPattern.

private Conjunction whenPattern(com.vaticle.typeql.lang.pattern.Conjunction<? extends Pattern> conjunction, com.vaticle.typeql.lang.pattern.variable.ThingVariable<?> then, LogicManager logicMgr) {
    Disjunction when = Disjunction.create(conjunction.normalise(), VariableRegistry.createFromThings(list(then)));
    assert when.conjunctions().size() == 1;
    if (iterate(when.conjunctions().get(0).negations()).filter(neg -> neg.disjunction().conjunctions().size() != 1).hasNext()) {
        throw TypeDBException.of(INVALID_NEGATION_CONTAINS_DISJUNCTION, getLabel());
    }
    logicMgr.typeInference().applyCombination(when);
    return when.conjunctions().get(0);
}
Also used : RULE_THEN_INVALID_VALUE_ASSIGNMENT(com.vaticle.typedb.core.common.exception.ErrorMessage.RuleWrite.RULE_THEN_INVALID_VALUE_ASSIGNMENT) Identifier(com.vaticle.typedb.core.traversal.common.Identifier) LoggerFactory(org.slf4j.LoggerFactory) RelationType(com.vaticle.typedb.core.concept.type.RelationType) RULE_CONCLUSION_AMBIGUOUS_LABELLED_TYPE(com.vaticle.typedb.core.common.exception.ErrorMessage.RuleWrite.RULE_CONCLUSION_AMBIGUOUS_LABELLED_TYPE) COLON(com.vaticle.typeql.lang.common.TypeQLToken.Char.COLON) Map(java.util.Map) ValueConstraint(com.vaticle.typedb.core.pattern.constraint.thing.ValueConstraint) ThingConstraint(com.vaticle.typedb.core.pattern.constraint.thing.ThingConstraint) Collections.list(com.vaticle.typedb.common.collection.Collections.list) Thing(com.vaticle.typedb.core.concept.thing.Thing) THEN(com.vaticle.typeql.lang.common.TypeQLToken.Schema.THEN) NEW_LINE(com.vaticle.typeql.lang.common.TypeQLToken.Char.NEW_LINE) FunctionalIterator(com.vaticle.typedb.core.common.iterator.FunctionalIterator) Negation(com.vaticle.typedb.core.pattern.Negation) IsaConstraint(com.vaticle.typedb.core.pattern.constraint.thing.IsaConstraint) Set(java.util.Set) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Pattern(com.vaticle.typeql.lang.pattern.Pattern) HasConstraint(com.vaticle.typedb.core.pattern.constraint.thing.HasConstraint) CURLY_OPEN(com.vaticle.typeql.lang.common.TypeQLToken.Char.CURLY_OPEN) List(java.util.List) RelationConstraint(com.vaticle.typedb.core.pattern.constraint.thing.RelationConstraint) Optional(java.util.Optional) VariableRegistry(com.vaticle.typedb.core.pattern.variable.VariableRegistry) TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) RULE(com.vaticle.typeql.lang.common.TypeQLToken.Schema.RULE) ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) ILLEGAL_STATE(com.vaticle.typedb.core.common.exception.ErrorMessage.Internal.ILLEGAL_STATE) TraversalEngine(com.vaticle.typedb.core.traversal.TraversalEngine) Collections.set(com.vaticle.typedb.common.collection.Collections.set) Variable(com.vaticle.typedb.core.pattern.variable.Variable) SEMICOLON(com.vaticle.typeql.lang.common.TypeQLToken.Char.SEMICOLON) HashMap(java.util.HashMap) Objects.className(com.vaticle.typedb.common.util.Objects.className) RULE_THEN_CANNOT_BE_SATISFIED(com.vaticle.typedb.core.common.exception.ErrorMessage.RuleWrite.RULE_THEN_CANNOT_BE_SATISFIED) SPACE(com.vaticle.typeql.lang.common.TypeQLToken.Char.SPACE) RULE_CONCLUSION_ILLEGAL_INSERT(com.vaticle.typedb.core.common.exception.ErrorMessage.RuleWrite.RULE_CONCLUSION_ILLEGAL_INSERT) Reference(com.vaticle.typeql.lang.pattern.variable.Reference) Concept(com.vaticle.typedb.core.concept.Concept) HashSet(java.util.HashSet) Concludable(com.vaticle.typedb.core.logic.resolvable.Concludable) Label(com.vaticle.typedb.core.common.parameters.Label) GraphManager(com.vaticle.typedb.core.graph.GraphManager) Conjunction(com.vaticle.typedb.core.pattern.Conjunction) RelationTraversal(com.vaticle.typedb.core.traversal.RelationTraversal) Logger(org.slf4j.Logger) ThingVariable(com.vaticle.typedb.core.pattern.variable.ThingVariable) RuleStructure(com.vaticle.typedb.core.graph.structure.RuleStructure) Iterators(com.vaticle.typedb.core.common.iterator.Iterators) RULE_WHEN_CANNOT_BE_SATISFIED(com.vaticle.typedb.core.common.exception.ErrorMessage.RuleWrite.RULE_WHEN_CANNOT_BE_SATISFIED) RoleType(com.vaticle.typedb.core.concept.type.RoleType) Disjunction(com.vaticle.typedb.core.pattern.Disjunction) CURLY_CLOSE(com.vaticle.typeql.lang.common.TypeQLToken.Char.CURLY_CLOSE) Attribute(com.vaticle.typedb.core.concept.thing.Attribute) TypeVariable(com.vaticle.typedb.core.pattern.variable.TypeVariable) INVALID_CASTING(com.vaticle.typedb.core.common.exception.ErrorMessage.Pattern.INVALID_CASTING) Iterators.iterate(com.vaticle.typedb.core.common.iterator.Iterators.iterate) INVALID_NEGATION_CONTAINS_DISJUNCTION(com.vaticle.typedb.core.common.exception.ErrorMessage.RuleWrite.INVALID_NEGATION_CONTAINS_DISJUNCTION) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) WHEN(com.vaticle.typeql.lang.common.TypeQLToken.Schema.WHEN) Disjunction(com.vaticle.typedb.core.pattern.Disjunction)

Aggregations

ConceptManager (com.vaticle.typedb.core.concept.ConceptManager)4 RelationType (com.vaticle.typedb.core.concept.type.RelationType)4 Pattern (com.vaticle.typeql.lang.pattern.Pattern)4 AttributeType (com.vaticle.typedb.core.concept.type.AttributeType)3 RoleType (com.vaticle.typedb.core.concept.type.RoleType)3 Collections.list (com.vaticle.typedb.common.collection.Collections.list)2 Collections.set (com.vaticle.typedb.common.collection.Collections.set)2 Objects.className (com.vaticle.typedb.common.util.Objects.className)2 TypeDB (com.vaticle.typedb.core.TypeDB)2 ILLEGAL_STATE (com.vaticle.typedb.core.common.exception.ErrorMessage.Internal.ILLEGAL_STATE)2 INVALID_CASTING (com.vaticle.typedb.core.common.exception.ErrorMessage.Pattern.INVALID_CASTING)2 INVALID_NEGATION_CONTAINS_DISJUNCTION (com.vaticle.typedb.core.common.exception.ErrorMessage.RuleWrite.INVALID_NEGATION_CONTAINS_DISJUNCTION)2 RULE_CONCLUSION_AMBIGUOUS_LABELLED_TYPE (com.vaticle.typedb.core.common.exception.ErrorMessage.RuleWrite.RULE_CONCLUSION_AMBIGUOUS_LABELLED_TYPE)2 RULE_CONCLUSION_ILLEGAL_INSERT (com.vaticle.typedb.core.common.exception.ErrorMessage.RuleWrite.RULE_CONCLUSION_ILLEGAL_INSERT)2 RULE_THEN_CANNOT_BE_SATISFIED (com.vaticle.typedb.core.common.exception.ErrorMessage.RuleWrite.RULE_THEN_CANNOT_BE_SATISFIED)2 RULE_THEN_INVALID_VALUE_ASSIGNMENT (com.vaticle.typedb.core.common.exception.ErrorMessage.RuleWrite.RULE_THEN_INVALID_VALUE_ASSIGNMENT)2 RULE_WHEN_CANNOT_BE_SATISFIED (com.vaticle.typedb.core.common.exception.ErrorMessage.RuleWrite.RULE_WHEN_CANNOT_BE_SATISFIED)2 TypeDBException (com.vaticle.typedb.core.common.exception.TypeDBException)2 FunctionalIterator (com.vaticle.typedb.core.common.iterator.FunctionalIterator)2 Iterators (com.vaticle.typedb.core.common.iterator.Iterators)2