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;
}
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());
}
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());
}
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());
}
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;
}
Aggregations