Search in sources :

Example 21 with SchemaConcept

use of ai.grakn.concept.SchemaConcept in project grakn by graknlabs.

the class RuleUtils method subGraphIsCyclical.

/**
 * @param rules set of rules of interest forming a rule subgraph
 * @param graph of interest
 * @return true if the rule subgraph formed from provided rules contains loops
 */
public static boolean subGraphIsCyclical(Set<InferenceRule> rules, GraknTx graph) {
    Iterator<Rule> ruleIterator = rules.stream().map(r -> graph.<Rule>getConcept(r.getRuleId())).iterator();
    boolean cyclical = false;
    while (ruleIterator.hasNext() && !cyclical) {
        Set<Rule> visitedRules = new HashSet<>();
        Stack<Rule> rulesToVisit = new Stack<>();
        rulesToVisit.push(ruleIterator.next());
        while (!rulesToVisit.isEmpty() && !cyclical) {
            Rule rule = rulesToVisit.pop();
            if (!visitedRules.contains(rule)) {
                rule.getConclusionTypes().flatMap(SchemaConcept::getRulesOfHypothesis).forEach(rulesToVisit::add);
                visitedRules.add(rule);
            } else {
                cyclical = true;
            }
        }
    }
    return cyclical;
}
Also used : HashSet(java.util.HashSet) Stream(java.util.stream.Stream) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) Equivalence(com.google.common.base.Equivalence) Iterator(java.util.Iterator) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) GraknTx(ai.grakn.GraknTx) SchemaConcept(ai.grakn.concept.SchemaConcept) Set(java.util.Set) Schema(ai.grakn.util.Schema) Rule(ai.grakn.concept.Rule) Stack(java.util.Stack) Rule(ai.grakn.concept.Rule) HashSet(java.util.HashSet) Stack(java.util.Stack)

Example 22 with SchemaConcept

use of ai.grakn.concept.SchemaConcept in project grakn by graknlabs.

the class AdminTest method testGetTypesInQuery.

@Test
public void testGetTypesInQuery() {
    Match match = qb.match(var("x").isa(label("movie").sub("production")).has("tmdb-vote-count", 400), var("y").isa("character"), var().rel("production-with-cast", "x").rel("y").isa("has-cast"));
    Set<SchemaConcept> types = Stream.of("movie", "production", "tmdb-vote-count", "character", "production-with-cast", "has-cast").map(t -> rule.tx().<SchemaConcept>getSchemaConcept(Label.of(t))).collect(toSet());
    assertEquals(types, match.admin().getSchemaConcepts());
}
Also used : PatternAdmin(ai.grakn.graql.admin.PatternAdmin) DeleteQuery(ai.grakn.graql.DeleteQuery) SchemaConcept(ai.grakn.concept.SchemaConcept) InsertQuery(ai.grakn.graql.InsertQuery) Graql(ai.grakn.graql.Graql) TestCase.assertNotNull(junit.framework.TestCase.assertNotNull) Label(ai.grakn.concept.Label) ConceptId(ai.grakn.concept.ConceptId) ClassRule(org.junit.ClassRule) Collectors.toSet(java.util.stream.Collectors.toSet) Before(org.junit.Before) QueryBuilder(ai.grakn.graql.QueryBuilder) Graql.var(ai.grakn.graql.Graql.var) Conjunction(ai.grakn.graql.admin.Conjunction) Set(java.util.Set) Test(org.junit.Test) Graql.label(ai.grakn.graql.Graql.label) Sets(com.google.common.collect.Sets) MovieKB(ai.grakn.test.kbs.MovieKB) Match(ai.grakn.graql.Match) Stream(java.util.stream.Stream) Assert.assertFalse(org.junit.Assert.assertFalse) SampleKBContext(ai.grakn.test.rule.SampleKBContext) Optional(java.util.Optional) Assert.assertEquals(org.junit.Assert.assertEquals) SchemaConcept(ai.grakn.concept.SchemaConcept) Match(ai.grakn.graql.Match) Test(org.junit.Test)

Example 23 with SchemaConcept

use of ai.grakn.concept.SchemaConcept in project grakn by graknlabs.

the class AdminTest method testMatchInsertQueryGetTypes.

@Test
public void testMatchInsertQueryGetTypes() {
    InsertQuery query = qb.match(var("y").isa("movie")).insert(var("x").isa("person").has("name", var("z")), var().rel("actor", "x").isa("has-cast"));
    Set<SchemaConcept> types = Stream.of("movie", "person", "name", "actor", "has-cast").map(t -> rule.tx().<SchemaConcept>getSchemaConcept(Label.of(t))).collect(toSet());
    assertEquals(types, query.admin().getSchemaConcepts());
}
Also used : PatternAdmin(ai.grakn.graql.admin.PatternAdmin) DeleteQuery(ai.grakn.graql.DeleteQuery) SchemaConcept(ai.grakn.concept.SchemaConcept) InsertQuery(ai.grakn.graql.InsertQuery) Graql(ai.grakn.graql.Graql) TestCase.assertNotNull(junit.framework.TestCase.assertNotNull) Label(ai.grakn.concept.Label) ConceptId(ai.grakn.concept.ConceptId) ClassRule(org.junit.ClassRule) Collectors.toSet(java.util.stream.Collectors.toSet) Before(org.junit.Before) QueryBuilder(ai.grakn.graql.QueryBuilder) Graql.var(ai.grakn.graql.Graql.var) Conjunction(ai.grakn.graql.admin.Conjunction) Set(java.util.Set) Test(org.junit.Test) Graql.label(ai.grakn.graql.Graql.label) Sets(com.google.common.collect.Sets) MovieKB(ai.grakn.test.kbs.MovieKB) Match(ai.grakn.graql.Match) Stream(java.util.stream.Stream) Assert.assertFalse(org.junit.Assert.assertFalse) SampleKBContext(ai.grakn.test.rule.SampleKBContext) Optional(java.util.Optional) Assert.assertEquals(org.junit.Assert.assertEquals) InsertQuery(ai.grakn.graql.InsertQuery) SchemaConcept(ai.grakn.concept.SchemaConcept) Test(org.junit.Test)

Example 24 with SchemaConcept

use of ai.grakn.concept.SchemaConcept in project grakn by graknlabs.

the class AdminTest method testInsertQueryGetTypes.

@Test
public void testInsertQueryGetTypes() {
    InsertQuery query = qb.insert(var("x").isa("person").has("name", var("y")), var().rel("actor", "x").isa("has-cast"));
    Set<SchemaConcept> types = Stream.of("person", "name", "actor", "has-cast").map(t -> rule.tx().<SchemaConcept>getSchemaConcept(Label.of(t))).collect(toSet());
    assertEquals(types, query.admin().getSchemaConcepts());
}
Also used : PatternAdmin(ai.grakn.graql.admin.PatternAdmin) DeleteQuery(ai.grakn.graql.DeleteQuery) SchemaConcept(ai.grakn.concept.SchemaConcept) InsertQuery(ai.grakn.graql.InsertQuery) Graql(ai.grakn.graql.Graql) TestCase.assertNotNull(junit.framework.TestCase.assertNotNull) Label(ai.grakn.concept.Label) ConceptId(ai.grakn.concept.ConceptId) ClassRule(org.junit.ClassRule) Collectors.toSet(java.util.stream.Collectors.toSet) Before(org.junit.Before) QueryBuilder(ai.grakn.graql.QueryBuilder) Graql.var(ai.grakn.graql.Graql.var) Conjunction(ai.grakn.graql.admin.Conjunction) Set(java.util.Set) Test(org.junit.Test) Graql.label(ai.grakn.graql.Graql.label) Sets(com.google.common.collect.Sets) MovieKB(ai.grakn.test.kbs.MovieKB) Match(ai.grakn.graql.Match) Stream(java.util.stream.Stream) Assert.assertFalse(org.junit.Assert.assertFalse) SampleKBContext(ai.grakn.test.rule.SampleKBContext) Optional(java.util.Optional) Assert.assertEquals(org.junit.Assert.assertEquals) InsertQuery(ai.grakn.graql.InsertQuery) SchemaConcept(ai.grakn.concept.SchemaConcept) Test(org.junit.Test)

Example 25 with SchemaConcept

use of ai.grakn.concept.SchemaConcept in project grakn by graknlabs.

the class ValidateGlobalRules method validateRelationTypesToRolesSchema.

/**
 * @param relationshipType the {@link RelationshipType} to be validated
 * @return Error messages if the role type sub structure does not match the {@link RelationshipType} sub structure
 */
static Set<String> validateRelationTypesToRolesSchema(RelationshipType relationshipType) {
    RelationshipTypeImpl superRelationType = (RelationshipTypeImpl) relationshipType.sup();
    if (Schema.MetaSchema.isMetaLabel(superRelationType.getLabel()) || superRelationType.isAbstract()) {
        // If super type is a meta type no validation needed
        return Collections.emptySet();
    }
    Set<String> errorMessages = new HashSet<>();
    Collection<Role> superRelates = superRelationType.relates().collect(Collectors.toSet());
    Collection<Role> relates = relationshipType.relates().collect(Collectors.toSet());
    Set<Label> relatesLabels = relates.stream().map(SchemaConcept::getLabel).collect(Collectors.toSet());
    // Check 1) Every role of relationTypes is the sub of a role which is in the relates of it's supers
    if (!superRelationType.isAbstract()) {
        Set<Label> allSuperRolesPlayed = new HashSet<>();
        superRelationType.sups().forEach(rel -> rel.relates().forEach(roleType -> allSuperRolesPlayed.add(roleType.getLabel())));
        for (Role relate : relates) {
            boolean validRoleTypeFound = SchemaConceptImpl.from(relate).sups().anyMatch(superRole -> allSuperRolesPlayed.contains(superRole.getLabel()));
            if (!validRoleTypeFound) {
                errorMessages.add(VALIDATION_RELATION_TYPES_ROLES_SCHEMA.getMessage(relate.getLabel(), relationshipType.getLabel(), "super", "super", superRelationType.getLabel()));
            }
        }
    }
    // Check 2) Every role of superRelationType has a sub role which is in the relates of relationTypes
    for (Role superRelate : superRelates) {
        boolean subRoleNotFoundInRelates = superRelate.subs().noneMatch(sub -> relatesLabels.contains(sub.getLabel()));
        if (subRoleNotFoundInRelates) {
            errorMessages.add(VALIDATION_RELATION_TYPES_ROLES_SCHEMA.getMessage(superRelate.getLabel(), superRelationType.getLabel(), "sub", "sub", relationshipType.getLabel()));
        }
    }
    return errorMessages;
}
Also used : Role(ai.grakn.concept.Role) RuleImpl(ai.grakn.kb.internal.concept.RuleImpl) Iterables(com.google.common.collect.Iterables) VALIDATION_CASTING(ai.grakn.util.ErrorMessage.VALIDATION_CASTING) VALIDATION_REQUIRED_RELATION(ai.grakn.util.ErrorMessage.VALIDATION_REQUIRED_RELATION) Role(ai.grakn.concept.Role) SchemaConcept(ai.grakn.concept.SchemaConcept) RelationshipTypeImpl(ai.grakn.kb.internal.concept.RelationshipTypeImpl) Type(ai.grakn.concept.Type) Rule(ai.grakn.concept.Rule) Attribute(ai.grakn.concept.Attribute) TypeImpl(ai.grakn.kb.internal.concept.TypeImpl) HashSet(java.util.HashSet) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType) GraknTx(ai.grakn.GraknTx) CommonUtil(ai.grakn.util.CommonUtil) Map(java.util.Map) Relationship(ai.grakn.concept.Relationship) GraknTxOperationException(ai.grakn.exception.GraknTxOperationException) ErrorMessage(ai.grakn.util.ErrorMessage) VALIDATION_RELATION_TYPES_ROLES_SCHEMA(ai.grakn.util.ErrorMessage.VALIDATION_RELATION_TYPES_ROLES_SCHEMA) SchemaConceptImpl(ai.grakn.kb.internal.concept.SchemaConceptImpl) Conjunction(ai.grakn.graql.admin.Conjunction) VALIDATION_RELATION_TYPE(ai.grakn.util.ErrorMessage.VALIDATION_RELATION_TYPE) Collection(java.util.Collection) Set(java.util.Set) VALIDATION_ROLE_TYPE_MISSING_RELATION_TYPE(ai.grakn.util.ErrorMessage.VALIDATION_ROLE_TYPE_MISSING_RELATION_TYPE) Collectors(java.util.stream.Collectors) Atomic(ai.grakn.graql.admin.Atomic) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) Stream(java.util.stream.Stream) Thing(ai.grakn.concept.Thing) VALIDATION_RELATION_CASTING_LOOP_FAIL(ai.grakn.util.ErrorMessage.VALIDATION_RELATION_CASTING_LOOP_FAIL) Casting(ai.grakn.kb.internal.structure.Casting) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Optional(java.util.Optional) Schema(ai.grakn.util.Schema) Pattern(ai.grakn.graql.Pattern) Collections(java.util.Collections) VALIDATION_NOT_EXACTLY_ONE_KEY(ai.grakn.util.ErrorMessage.VALIDATION_NOT_EXACTLY_ONE_KEY) RelationshipTypeImpl(ai.grakn.kb.internal.concept.RelationshipTypeImpl) Label(ai.grakn.concept.Label) HashSet(java.util.HashSet)

Aggregations

SchemaConcept (ai.grakn.concept.SchemaConcept)51 Label (ai.grakn.concept.Label)19 Set (java.util.Set)15 Type (ai.grakn.concept.Type)14 GraknTx (ai.grakn.GraknTx)12 HashSet (java.util.HashSet)12 ConceptId (ai.grakn.concept.ConceptId)11 Stream (java.util.stream.Stream)11 Test (org.junit.Test)11 AttributeType (ai.grakn.concept.AttributeType)10 Property (com.pholser.junit.quickcheck.Property)10 Role (ai.grakn.concept.Role)9 Concept (ai.grakn.concept.Concept)8 Sets (com.google.common.collect.Sets)8 Optional (java.util.Optional)8 RelationshipType (ai.grakn.concept.RelationshipType)7 Rule (ai.grakn.concept.Rule)7 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)7 ErrorMessage (ai.grakn.util.ErrorMessage)7 Var (ai.grakn.graql.Var)6