Search in sources :

Example 16 with Disjunction

use of com.vaticle.typedb.core.pattern.Disjunction 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)));
}
Also used : Entity(com.vaticle.typedb.core.concept.thing.Entity) 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 17 with Disjunction

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

the class TypeInferenceTest method nested_negation_is_satisfiable.

/**
 * If the type resolver conflates anonymous or generated variables between the inner or outer nestings,
 * we will see it here
 */
@Test
public void nested_negation_is_satisfiable() {
    define_custom_schema("define session sub entity,\n" + "          plays reported-fault:parent-session,\n" + "          plays unanswered-question:parent-session;\n" + "      fault sub entity,\n" + "          plays reported-fault:relevant-fault,\n" + "          plays fault-identification:identified-fault;\n" + "      question sub entity,\n" + "          plays fault-identification:identifying-question,\n" + "          plays unanswered-question:question-not-answered;\n" + "      reported-fault sub relation,\n" + "          relates relevant-fault,\n" + "          relates parent-session;\n" + "      unanswered-question sub relation,\n" + "          relates question-not-answered,\n" + "          relates parent-session;\n" + "      fault-identification sub relation,\n" + "          relates identifying-question,\n" + "          relates identified-fault;\n");
    String query = "match (relevant-fault: $flt, parent-session: $ts) isa reported-fault;\n" + "          not {\n" + "              (question-not-answered: $ques, parent-session: $ts) isa unanswered-question;\n" + "              ($flt, $ques) isa fault-identification;" + "          };";
    Disjunction disjunction = createDisjunction(query);
    transaction.logic().typeInference().applyCombination(disjunction);
    assertTrue(disjunction.conjunctions().get(0).negations().iterator().next().disjunction().conjunctions().get(0).isCoherent());
}
Also used : Disjunction(com.vaticle.typedb.core.pattern.Disjunction) Test(org.junit.Test)

Example 18 with Disjunction

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

the class TypeInferenceTest method infer_from_value_type.

@Test
public void infer_from_value_type() {
    define_custom_schema("define" + "  dog sub entity, owns weight;" + "  person sub entity, owns name;" + "  weight sub attribute, value double;" + "  name sub attribute, value string;");
    TypeInference typeInference = transaction.logic().typeInference();
    String queryString = "match" + "  $p has $a;" + "  $a = 'bob';";
    Disjunction disjunction = createDisjunction(queryString);
    typeInference.applyCombination(disjunction);
    Map<String, Set<String>> expected = new HashMap<>() {

        {
            put("$p", set("person"));
            put("$a", set("name"));
        }
    };
    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 19 with Disjunction

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

the class TypeInferenceTest method nested_negation_outer_scope_correctly_reduces_resolved_types.

@Test
public void nested_negation_outer_scope_correctly_reduces_resolved_types() {
    define_custom_schema("define " + "person sub entity, plays marriage:spouse;" + "woman sub person;" + "marriage sub relation, relates spouse;");
    String minimallyRestricted = "match $x isa person; not { ($x) isa marriage; };";
    Disjunction disjunction = createDisjunction(minimallyRestricted);
    transaction.logic().typeInference().applyCombination(disjunction);
    HashMap<String, Set<String>> expected = new HashMap<>() {

        {
            put("$x", set("person", "woman"));
            put("$_0", set("marriage"));
            put("$_marriage", set("marriage"));
        }
    };
    // test the inner negation
    assertEquals(expected, resolvedTypeMap(disjunction.conjunctions().get(0).negations().iterator().next().disjunction().conjunctions().get(0)));
    String restricted = "match $x isa woman; not { ($x) isa marriage; };";
    Disjunction restrictedDisjunction = createDisjunction(restricted);
    transaction.logic().typeInference().applyCombination(restrictedDisjunction);
    expected = new HashMap<>() {

        {
            put("$x", set("woman"));
            put("$_0", set("marriage"));
            put("$_marriage", set("marriage"));
        }
    };
    // test the inner negation
    assertEquals(expected, resolvedTypeMap(restrictedDisjunction.conjunctions().get(0).negations().iterator().next().disjunction().conjunctions().get(0)));
}
Also used : Disjunction(com.vaticle.typedb.core.pattern.Disjunction) Set(java.util.Set) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 20 with Disjunction

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

the class TypeInferenceTest method relation_variable_role_variable_relation_named_variable.

@Test
public void relation_variable_role_variable_relation_named_variable() throws IOException {
    define_standard_schema("basic-schema");
    TypeInference typeInference = transaction.logic().typeInference();
    String queryString = "match $r (wife: $yoko) isa $m;";
    Disjunction disjunction = createDisjunction(queryString);
    typeInference.applyCombination(disjunction);
    Map<String, Set<String>> expected = new HashMap<>() {

        {
            put("$yoko", set("woman"));
            put("$r", set("marriage"));
            put("$m", set("marriage", "relation", "thing"));
            put("$_relation:wife", set("marriage:wife"));
        }
    };
    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