Search in sources :

Example 6 with Disjunction

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

the class TypeInferenceTest method converts_root_types.

@Test
public void converts_root_types() throws IOException {
    define_standard_schema("test-type-inference");
    TypeInference typeInference = transaction.logic().typeInference();
    String relationString = "match $x isa relation;";
    Disjunction relationDisjunction = createDisjunction(relationString);
    typeInference.applyCombination(relationDisjunction);
    Map<String, Set<String>> relationExpected = new HashMap<>() {

        {
            put("$x", set("friendship", "employment"));
            put("$_relation", set("relation"));
        }
    };
    assertEquals(relationExpected, resolvedTypeMap(relationDisjunction.conjunctions().get(0)));
    String attributeString = "match $x isa attribute;";
    Disjunction attributeDisjunction = createDisjunction(attributeString);
    typeInference.applyCombination(attributeDisjunction);
    Map<String, Set<String>> attributeExpected = new HashMap<>() {

        {
            put("$x", set("name", "age", "ref"));
            put("$_attribute", set("attribute"));
        }
    };
    assertEquals(attributeExpected, resolvedTypeMap(attributeDisjunction.conjunctions().get(0)));
    String entityString = "match $x isa entity;";
    Disjunction entityDisjunction = createDisjunction(entityString);
    typeInference.applyCombination(entityDisjunction);
    Map<String, Set<String>> entityExpected = new HashMap<>() {

        {
            put("$x", set("person", "company"));
            put("$_entity", set("entity"));
        }
    };
    assertEquals(entityExpected, resolvedTypeMap(entityDisjunction.conjunctions().get(0)));
    String roleString = "match ($role: $x) isa relation;";
    Disjunction roleDisjunction = createDisjunction(roleString);
    typeInference.applyCombination(roleDisjunction);
    Map<String, Set<String>> roleExpected = new HashMap<>() {

        {
            put("$role", set("friendship:friend", "employment:employer", "employment:employee", "relation:role"));
            put("$x", set("person", "company"));
            put("$_0", set("friendship", "employment"));
            put("$_relation", set("relation"));
        }
    };
    assertEquals(roleExpected, resolvedTypeMap(roleDisjunction.conjunctions().get(0)));
    String thingString = "match $x isa thing;";
    Disjunction thingDisjunction = createDisjunction(thingString);
    typeInference.applyCombination(thingDisjunction);
    Map<String, Set<String>> thingExpected = new HashMap<>() {

        {
            put("$x", set("person", "company", "friendship", "employment", "name", "age", "ref"));
            put("$_thing", set("thing"));
        }
    };
    assertEquals(thingExpected, resolvedTypeMap(thingDisjunction.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 7 with Disjunction

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

the class Materialiser method query.

public Map<Conjunction, FunctionalIterator<ConceptMap>> query(TypeQLMatch inferenceQuery) {
    // TODO: How do we handle disjunctions inside negations and negations in general?
    Disjunction disjunction = Disjunction.create(inferenceQuery.conjunction().normalise());
    tx.logic().typeInference().applyCombination(disjunction);
    HashMap<Conjunction, FunctionalIterator<ConceptMap>> conjunctionAnswers = new HashMap<>();
    disjunction.conjunctions().forEach(conjunction -> conjunctionAnswers.put(conjunction, traverse(conjunction)));
    return conjunctionAnswers;
}
Also used : Disjunction(com.vaticle.typedb.core.pattern.Disjunction) HashMap(java.util.HashMap) Conjunction(com.vaticle.typedb.core.pattern.Conjunction) FunctionalIterator(com.vaticle.typedb.core.common.iterator.FunctionalIterator)

Example 8 with Disjunction

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

the class TypeInferenceTest method cannot_insert_if_relation_type_too_general.

@Test
public void cannot_insert_if_relation_type_too_general() {
    define_custom_schema("define" + " person sub entity, plays marriage:husband, plays marriage:wife;" + " partnership sub relation, relates partner;" + " marriage sub partnership, relates husband as partner, relates wife as partner;");
    String queryString = "match $x isa person; (wife: $x) isa partnership;";
    Disjunction disjunction = createDisjunction(queryString);
    transaction.logic().typeInference().applyCombination(disjunction);
    assertThrows(() -> transaction.logic().putRule("marriage-rule", TypeQL.parsePattern("{$x isa person;}").asConjunction(), TypeQL.parseVariable("(wife: $x) isa partnership").asThing()));
}
Also used : Disjunction(com.vaticle.typedb.core.pattern.Disjunction) Test(org.junit.Test)

Example 9 with Disjunction

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

the class TypeInferenceTest method up_down_hierarchy_isa.

@Test
public void up_down_hierarchy_isa() {
    define_custom_schema("define" + "  animal sub entity;" + "  person sub animal;" + "  man sub person;" + "  greek sub man;" + "  socrates sub greek;");
    TypeInference typeInference = transaction.logic().typeInference();
    String queryString = "match" + "  $p isa man;" + "  man sub $q;";
    Disjunction disjunction = createDisjunction(queryString);
    typeInference.applyCombination(disjunction);
    Map<String, Set<String>> expected = new HashMap<>() {

        {
            put("$p", set("man", "greek", "socrates"));
            put("$q", set("thing", "entity", "animal", "person", "man"));
            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 10 with Disjunction

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

the class TypeInferenceTest method cannot_insert_abstract_attributes.

@Test
public void cannot_insert_abstract_attributes() {
    define_custom_schema("define " + " person sub entity, owns name;" + " woman sub person, owns maiden-name as name;" + " name sub attribute, value string, abstract;" + " maiden-name sub name, value string;");
    String queryString = "match $x isa woman, has name 'smith';";
    Disjunction disjunction = createDisjunction(queryString);
    transaction.logic().typeInference().applyCombination(disjunction);
    assertThrows(() -> transaction.logic().putRule("women-called-smith", TypeQL.parsePattern("{$x isa woman;}").asConjunction(), TypeQL.parseVariable("$x has name 'smith'").asThing()));
}
Also used : Disjunction(com.vaticle.typedb.core.pattern.Disjunction) 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