Search in sources :

Example 21 with Disjunction

use of com.vaticle.typedb.core.pattern.Disjunction in project grakn by graknlabs.

the class TypeInferenceTest method overridden_relates_are_valid.

@Test
public void overridden_relates_are_valid() {
    define_custom_schema("define" + " marriage sub relation, relates spouse;" + " hetero-marriage sub marriage," + "   relates husband as spouse, relates wife as spouse;" + " person sub entity, plays marriage:spouse, plays hetero-marriage:husband," + "   plays hetero-marriage:wife;");
    TypeInference typeInference = transaction.logic().typeInference();
    String queryString = "match $m (spouse: $x, spouse: $y) isa marriage;";
    Disjunction disjunction = createDisjunction(queryString);
    typeInference.applyCombination(disjunction);
    Map<String, Set<String>> expected = new HashMap<>() {

        {
            put("$x", set("person"));
            put("$y", set("person"));
            put("$m", set("marriage", "hetero-marriage"));
            put("$_marriage:spouse", set("marriage:spouse"));
            put("$_marriage", set("marriage"));
        }
    };
    assertEquals(expected, resolvedTypeMap(disjunction.conjunctions().get(0)));
}
Also used : TypeInference(com.vaticle.typedb.core.logic.tool.TypeInference) Disjunction(com.vaticle.typedb.core.pattern.Disjunction) Set(java.util.Set) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 22 with Disjunction

use of com.vaticle.typedb.core.pattern.Disjunction in project grakn by graknlabs.

the class TypeInferenceTest method multiple_anonymous_vars.

@Test
public void multiple_anonymous_vars() throws IOException {
    define_standard_schema("basic-schema");
    String queryString = "match $a has name 'fido'; $a has label 'poodle';";
    TypeInference typeInference = transaction.logic().typeInference();
    Disjunction disjunction = createDisjunction(queryString);
    typeInference.applyCombination(disjunction);
    Map<String, Set<String>> expected = new HashMap<>() {

        {
            put("$a", set("dog"));
            put("$_name", set("name"));
            put("$_label", set("label"));
            put("$_0", set("name"));
            put("$_1", set("label"));
        }
    };
    assertEquals(expected, resolvedTypeMap(disjunction.conjunctions().get(0)));
}
Also used : TypeInference(com.vaticle.typedb.core.logic.tool.TypeInference) Disjunction(com.vaticle.typedb.core.pattern.Disjunction) Set(java.util.Set) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 23 with Disjunction

use of com.vaticle.typedb.core.pattern.Disjunction in project grakn by graknlabs.

the class Reasoner method mayReason.

private boolean mayReason(Disjunction disjunction) {
    for (Conjunction conj : disjunction.conjunctions()) {
        Set<Variable> vars = conj.variables();
        List<Negation> negs = conj.negations();
        if (iterate(vars).flatMap(v -> iterate(v.inferredTypes())).distinct().anyMatch(this::hasRule))
            return true;
        if (!negs.isEmpty() && iterate(negs).anyMatch(n -> mayReason(n.disjunction())))
            return true;
    }
    return false;
}
Also used : ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) Context(com.vaticle.typedb.core.common.parameters.Context) TraversalEngine(com.vaticle.typedb.core.traversal.TraversalEngine) Collections.set(com.vaticle.typedb.common.collection.Collections.set) Variable(com.vaticle.typedb.core.pattern.variable.Variable) Identifier(com.vaticle.typedb.core.traversal.common.Identifier) UnboundVariable(com.vaticle.typeql.lang.pattern.variable.UnboundVariable) UNSATISFIABLE_SUB_PATTERN(com.vaticle.typedb.core.common.exception.ErrorMessage.Pattern.UNSATISFIABLE_SUB_PATTERN) LoggerFactory(org.slf4j.LoggerFactory) PARALLELISATION_FACTOR(com.vaticle.typedb.core.concurrent.executor.Executors.PARALLELISATION_FACTOR) ResolverRegistry(com.vaticle.typedb.core.reasoner.resolution.ResolverRegistry) Arguments(com.vaticle.typedb.core.common.parameters.Arguments) Producers.produce(com.vaticle.typedb.core.concurrent.producer.Producers.produce) LogicManager(com.vaticle.typedb.core.logic.LogicManager) HashSet(java.util.HashSet) Executors.async1(com.vaticle.typedb.core.concurrent.executor.Executors.async1) Either(com.vaticle.typedb.common.collection.Either) Collections.list(com.vaticle.typedb.common.collection.Collections.list) Thing(com.vaticle.typedb.core.concept.thing.Thing) Label(com.vaticle.typedb.core.common.parameters.Label) Conjunction(com.vaticle.typedb.core.pattern.Conjunction) Logger(org.slf4j.Logger) FunctionalIterator(com.vaticle.typedb.core.common.iterator.FunctionalIterator) Negation(com.vaticle.typedb.core.pattern.Negation) Options(com.vaticle.typedb.core.common.parameters.Options) Producer(com.vaticle.typedb.core.concurrent.producer.Producer) Set(java.util.Set) Iterators(com.vaticle.typedb.core.common.iterator.Iterators) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Disjunction(com.vaticle.typedb.core.pattern.Disjunction) UNSATISFIABLE_PATTERN(com.vaticle.typedb.core.common.exception.ErrorMessage.Pattern.UNSATISFIABLE_PATTERN) List(java.util.List) Executors.actor(com.vaticle.typedb.core.concurrent.executor.Executors.actor) Iterators.iterate(com.vaticle.typedb.core.common.iterator.Iterators.iterate) Explanation(com.vaticle.typedb.core.reasoner.resolution.answer.Explanation) TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) EXHAUSTIVE(com.vaticle.typedb.core.common.parameters.Arguments.Query.Producer.EXHAUSTIVE) TypeQLMatch(com.vaticle.typeql.lang.query.TypeQLMatch) Type(com.vaticle.typedb.core.concept.type.Type) Producers(com.vaticle.typedb.core.concurrent.producer.Producers) Variable(com.vaticle.typedb.core.pattern.variable.Variable) UnboundVariable(com.vaticle.typeql.lang.pattern.variable.UnboundVariable) Negation(com.vaticle.typedb.core.pattern.Negation) Conjunction(com.vaticle.typedb.core.pattern.Conjunction)

Example 24 with Disjunction

use of com.vaticle.typedb.core.pattern.Disjunction in project grakn by graknlabs.

the class TypeInferenceTest method relation_staggered_role_hierarchy.

@Test
public void relation_staggered_role_hierarchy() {
    define_custom_schema("define" + " person sub entity," + "  plays partnership:partner," + "  plays marriage:spouse;" + "" + " man sub person," + "  plays hetero-marriage:husband;" + "" + " woman sub person," + "   plays hetero-marriage:wife;" + "" + " partnership sub relation," + "  relates partner;" + "" + " marriage sub partnership," + "  relates spouse as partner;" + "" + " hetero-marriage sub marriage," + "  relates husband as spouse," + "  relates wife as spouse;");
    TypeInference typeInference = transaction.logic().typeInference();
    String queryString = "match $r (spouse: $yoko, $role: $john) isa $m; $john isa man;";
    Disjunction disjunction = createDisjunction(queryString);
    typeInference.applyCombination(disjunction);
    Map<String, Set<String>> expected = new HashMap<>() {

        {
            put("$yoko", set("person", "woman", "man"));
            put("$john", set("man"));
            put("$role", set("hetero-marriage:husband", "marriage:spouse", "partnership:partner", "relation:role"));
            put("$r", set("hetero-marriage", "marriage"));
            put("$m", set("hetero-marriage", "marriage", "partnership", "relation", "thing"));
            put("$_relation:spouse", set("marriage:spouse"));
            put("$_man", set("man"));
        }
    };
    assertEquals(expected, resolvedTypeMap(disjunction.conjunctions().get(0)));
}
Also used : TypeInference(com.vaticle.typedb.core.logic.tool.TypeInference) Disjunction(com.vaticle.typedb.core.pattern.Disjunction) Set(java.util.Set) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 25 with Disjunction

use of com.vaticle.typedb.core.pattern.Disjunction in project grakn by graknlabs.

the class TypeInferenceTest method roles_can_handle_type_constraints.

@Test
public void roles_can_handle_type_constraints() throws IOException {
    define_standard_schema("basic-schema");
    String queryString = "match ($role: $x) isa $y; $role type marriage:wife;";
    TypeInference typeInference = transaction.logic().typeInference();
    Disjunction disjunction = createDisjunction(queryString);
    typeInference.applyCombination(disjunction);
    HashMap<String, Set<String>> expected = new HashMap<>() {

        {
            put("$y", set("marriage", "relation", "thing"));
            put("$x", set("woman"));
            put("$role", set("marriage:wife"));
            put("$_0", set("marriage"));
        }
    };
    assertEquals(expected, resolvedTypeMap(disjunction.conjunctions().get(0)));
}
Also used : TypeInference(com.vaticle.typedb.core.logic.tool.TypeInference) Disjunction(com.vaticle.typedb.core.pattern.Disjunction) Set(java.util.Set) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

Disjunction (com.vaticle.typedb.core.pattern.Disjunction)57 Test (org.junit.Test)50 Set (java.util.Set)44 HashMap (java.util.HashMap)43 TypeInference (com.vaticle.typedb.core.logic.tool.TypeInference)42 Conjunction (com.vaticle.typedb.core.pattern.Conjunction)5 TypeDBException (com.vaticle.typedb.core.common.exception.TypeDBException)4 Identifier (com.vaticle.typedb.core.traversal.common.Identifier)4 Collections.set (com.vaticle.typedb.common.collection.Collections.set)3 FunctionalIterator (com.vaticle.typedb.core.common.iterator.FunctionalIterator)3 Iterators.iterate (com.vaticle.typedb.core.common.iterator.Iterators.iterate)3 ConceptMap (com.vaticle.typedb.core.concept.answer.ConceptMap)3 CoreTransaction (com.vaticle.typedb.core.database.CoreTransaction)3 Variable (com.vaticle.typedb.core.pattern.variable.Variable)3 Collections.list (com.vaticle.typedb.common.collection.Collections.list)2 Iterators (com.vaticle.typedb.core.common.iterator.Iterators)2 Arguments (com.vaticle.typedb.core.common.parameters.Arguments)2 Context (com.vaticle.typedb.core.common.parameters.Context)2 Label (com.vaticle.typedb.core.common.parameters.Label)2 ConceptManager (com.vaticle.typedb.core.concept.ConceptManager)2