use of ai.grakn.concept.Thing in project grakn by graknlabs.
the class GrpcUtil method convert.
public static Map<Role, Set<Thing>> convert(GrpcConceptConverter converter, RolePlayers allRolePlayers) {
ImmutableSetMultimap.Builder<Role, Thing> map = ImmutableSetMultimap.builder();
for (GrpcConcept.RolePlayer rolePlayer : allRolePlayers.getRolePlayerList()) {
Role role = converter.convert(rolePlayer.getRole()).asRole();
Thing player = converter.convert(rolePlayer.getPlayer()).asThing();
map.put(role, player);
}
return Multimaps.asMap(map.build());
}
use of ai.grakn.concept.Thing in project grakn by graknlabs.
the class ValidateGlobalRules method validatePlaysAndRelatesStructure.
/**
* This method checks if the plays edge has been added between the roleplayer's {@link Type} and
* the {@link Role} being played.
*
* It also checks if the {@link Role} of the {@link Casting} has been linked to the {@link RelationshipType} of the
* {@link Relationship} which the {@link Casting} connects to.
*
* @return Specific errors if any are found
*/
static Set<String> validatePlaysAndRelatesStructure(Casting casting) {
Set<String> errors = new HashSet<>();
// Gets here to make sure we traverse/read only once
Thing thing = casting.getRolePlayer();
Role role = casting.getRole();
Relationship relationship = casting.getRelationship();
// Actual checks
roleNotAllowedToBePlayed(role, thing).ifPresent(errors::add);
roleNotLinkedToRelationShip(role, relationship.type(), relationship).ifPresent(errors::add);
return errors;
}
use of ai.grakn.concept.Thing in project grakn by graknlabs.
the class InsertQueryTest method whenInsertingMultipleRolePlayers_BothRolePlayersAreAdded.
@Test
public void whenInsertingMultipleRolePlayers_BothRolePlayersAreAdded() {
List<Answer> results = qb.match(var("g").has("title", "Godfather"), var("m").has("title", "The Muppets")).insert(var("c").isa("cluster").has("name", "2"), var("r").rel("cluster-of-production", "c").rel("production-with-cluster", "g").rel("production-with-cluster", "m").isa("has-cluster")).execute();
Thing cluster = results.get(0).get("c").asThing();
Thing godfather = results.get(0).get("g").asThing();
Thing muppets = results.get(0).get("m").asThing();
Relationship relationship = results.get(0).get("r").asRelationship();
Role clusterOfProduction = movieKB.tx().getRole("cluster-of-production");
Role productionWithCluster = movieKB.tx().getRole("production-with-cluster");
assertEquals(relationship.rolePlayers().collect(toSet()), ImmutableSet.of(cluster, godfather, muppets));
assertEquals(relationship.rolePlayers(clusterOfProduction).collect(toSet()), ImmutableSet.of(cluster));
assertEquals(relationship.rolePlayers(productionWithCluster).collect(toSet()), ImmutableSet.of(godfather, muppets));
}
use of ai.grakn.concept.Thing in project grakn by graknlabs.
the class AggregateTest method testGroupCount.
@Test
public void testGroupCount() {
AggregateQuery<Map<Concept, Long>> groupCountQuery = qb.match(var("x").isa("movie"), var("r").rel("x")).aggregate(group("x", count()));
Map<Concept, Long> groupCount = groupCountQuery.execute();
Thing godfather = rule.tx().getAttributeType("title").getAttribute("Godfather").owner();
assertEquals(new Long(9), groupCount.get(godfather));
}
use of ai.grakn.concept.Thing in project grakn by graknlabs.
the class GraqlPrinter method build.
@Override
public Function<StringBuilder, StringBuilder> build(Concept concept) {
return sb -> {
// Display values for resources and ids for everything else
if (concept.isAttribute()) {
sb.append(colorKeyword("val ")).append(StringUtil.valueToString(concept.asAttribute().getValue()));
} else if (concept.isSchemaConcept()) {
SchemaConcept ontoConcept = concept.asSchemaConcept();
sb.append(colorKeyword("label ")).append(colorType(ontoConcept));
SchemaConcept superConcept = ontoConcept.sup();
if (superConcept != null) {
sb.append(colorKeyword(" sub ")).append(colorType(superConcept));
}
} else {
sb.append(colorKeyword("id ")).append(idToString(concept.getId()));
}
if (concept.isRelationship()) {
String relationString = concept.asRelationship().allRolePlayers().entrySet().stream().flatMap(entry -> {
Role role = entry.getKey();
Set<Thing> things = entry.getValue();
return things.stream().map(instance -> Optional.of(colorType(role) + ": id " + idToString(instance.getId())));
}).flatMap(CommonUtil::optionalToStream).collect(Collectors.joining(", "));
sb.append(" (").append(relationString).append(")");
}
// Display type of each instance
if (concept.isThing()) {
Type type = concept.asThing().type();
sb.append(colorKeyword(" isa ")).append(colorType(type));
}
// Display when and then for rules
if (concept.isRule()) {
sb.append(colorKeyword(" when ")).append("{ ").append(concept.asRule().getWhen()).append(" }");
sb.append(colorKeyword(" then ")).append("{ ").append(concept.asRule().getThen()).append(" }");
}
// Display any requested resources
if (concept.isThing() && attributeTypes.length > 0) {
concept.asThing().attributes(attributeTypes).forEach(resource -> {
String resourceType = colorType(resource.type());
String value = StringUtil.valueToString(resource.getValue());
sb.append(colorKeyword(" has ")).append(resourceType).append(" ").append(value);
});
}
return sb;
};
}
Aggregations