Search in sources :

Example 16 with Thing

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());
}
Also used : Role(ai.grakn.concept.Role) GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) Thing(ai.grakn.concept.Thing)

Example 17 with Thing

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;
}
Also used : Role(ai.grakn.concept.Role) Relationship(ai.grakn.concept.Relationship) Thing(ai.grakn.concept.Thing) HashSet(java.util.HashSet)

Example 18 with Thing

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));
}
Also used : Role(ai.grakn.concept.Role) Answer(ai.grakn.graql.admin.Answer) Relationship(ai.grakn.concept.Relationship) Thing(ai.grakn.concept.Thing) Test(org.junit.Test)

Example 19 with Thing

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));
}
Also used : Concept(ai.grakn.concept.Concept) Map(java.util.Map) Thing(ai.grakn.concept.Thing) Test(org.junit.Test)

Example 20 with Thing

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;
    };
}
Also used : StringConverter.typeLabelToString(ai.grakn.graql.internal.util.StringConverter.typeLabelToString) ANSI(ai.grakn.graql.internal.util.ANSI) StringConverter.idToString(ai.grakn.graql.internal.util.StringConverter.idToString) Role(ai.grakn.concept.Role) Collection(java.util.Collection) Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) StringUtil(ai.grakn.util.StringUtil) Set(java.util.Set) Type(ai.grakn.concept.Type) Printer(ai.grakn.graql.Printer) Answer(ai.grakn.graql.admin.Answer) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) AttributeType(ai.grakn.concept.AttributeType) Thing(ai.grakn.concept.Thing) CommonUtil(ai.grakn.util.CommonUtil) Map(java.util.Map) Optional(java.util.Optional) Role(ai.grakn.concept.Role) CommonUtil(ai.grakn.util.CommonUtil) Type(ai.grakn.concept.Type) AttributeType(ai.grakn.concept.AttributeType) Set(java.util.Set) SchemaConcept(ai.grakn.concept.SchemaConcept) StringConverter.typeLabelToString(ai.grakn.graql.internal.util.StringConverter.typeLabelToString) StringConverter.idToString(ai.grakn.graql.internal.util.StringConverter.idToString)

Aggregations

Thing (ai.grakn.concept.Thing)47 Role (ai.grakn.concept.Role)30 Test (org.junit.Test)29 RelationshipType (ai.grakn.concept.RelationshipType)17 EntityType (ai.grakn.concept.EntityType)14 Relationship (ai.grakn.concept.Relationship)12 HashSet (java.util.HashSet)9 Set (java.util.Set)9 GraknTx (ai.grakn.GraknTx)7 Attribute (ai.grakn.concept.Attribute)7 ConceptId (ai.grakn.concept.ConceptId)7 AttributeType (ai.grakn.concept.AttributeType)6 Concept (ai.grakn.concept.Concept)6 Entity (ai.grakn.concept.Entity)6 Collectors (java.util.stream.Collectors)5 Label (ai.grakn.concept.Label)4 Schema (ai.grakn.util.Schema)4 Collection (java.util.Collection)4 Map (java.util.Map)4 GraknSession (ai.grakn.GraknSession)3