Search in sources :

Example 11 with Thing

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

the class RemoteConceptsTest method whenCallingRolePlayersWithNoArguments_GetTheExpectedResult.

@Test
public void whenCallingRolePlayersWithNoArguments_GetTheExpectedResult() {
    Role foo = RemoteConcepts.createRole(tx, ConceptId.of("foo"));
    Thing a = RemoteConcepts.createEntity(tx, A);
    Thing b = RemoteConcepts.createRelationship(tx, B);
    Thing c = RemoteConcepts.createAttribute(tx, C);
    Stream<RolePlayer> expected = Stream.of(RolePlayer.create(foo, a), RolePlayer.create(foo, b), RolePlayer.create(foo, c));
    mockConceptMethod(ConceptMethods.GET_ROLE_PLAYERS, expected);
    assertThat(relationship.rolePlayers().collect(toSet()), containsInAnyOrder(a, b, c));
}
Also used : Role(ai.grakn.concept.Role) RolePlayer(ai.grakn.grpc.RolePlayer) Thing(ai.grakn.concept.Thing) Test(org.junit.Test)

Example 12 with Thing

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

the class RelationshipReified method innerToString.

@Override
public String innerToString() {
    StringBuilder description = new StringBuilder();
    description.append("ID [").append(getId()).append("] Type [").append(type().getLabel()).append("] Roles and Role Players: \n");
    for (Map.Entry<Role, Set<Thing>> entry : allRolePlayers().entrySet()) {
        if (entry.getValue().isEmpty()) {
            description.append("    Role [").append(entry.getKey().getLabel()).append("] not played by any instance \n");
        } else {
            StringBuilder instancesString = new StringBuilder();
            for (Thing thing : entry.getValue()) {
                instancesString.append(thing.getId()).append(",");
            }
            description.append("    Role [").append(entry.getKey().getLabel()).append("] played by [").append(instancesString.toString()).append("] \n");
        }
    }
    return description.toString();
}
Also used : Role(ai.grakn.concept.Role) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Map(java.util.Map) Thing(ai.grakn.concept.Thing)

Example 13 with Thing

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

the class ValidateGlobalRulesTest method testValidatePlaysStructure.

@Test
public void testValidatePlaysStructure() throws Exception {
    EntityTypeImpl wolf = (EntityTypeImpl) tx.putEntityType("wolf");
    EntityTypeImpl creature = (EntityTypeImpl) tx.putEntityType("creature");
    EntityTypeImpl hunter = (EntityTypeImpl) tx.putEntityType("hunter");
    RelationshipType hunts = tx.putRelationshipType("hunts");
    RoleImpl witcher = (RoleImpl) tx.putRole("witcher");
    RoleImpl monster = (RoleImpl) tx.putRole("monster");
    Thing geralt = hunter.addEntity();
    ThingImpl werewolf = (ThingImpl) wolf.addEntity();
    RelationshipImpl assertion = (RelationshipImpl) hunts.addRelationship().addRolePlayer(witcher, geralt).addRolePlayer(monster, werewolf);
    assertion.reified().get().castingsRelation().forEach(rolePlayer -> assertFalse(ValidateGlobalRules.validatePlaysAndRelatesStructure(rolePlayer).isEmpty()));
    hunter.plays(witcher);
    boolean[] flags = { false, false };
    int count = 0;
    for (Casting casting : assertion.reified().get().castingsRelation().collect(Collectors.toSet())) {
        flags[count] = !ValidateGlobalRules.validatePlaysAndRelatesStructure(casting).isEmpty();
        count++;
    }
    assertTrue(flags[0] && flags[1]);
    wolf.sup(creature);
    creature.plays(monster);
    for (Casting casting : assertion.reified().get().castingsRelation().collect(Collectors.toSet())) {
        assertFalse(ValidateGlobalRules.validatePlaysAndRelatesStructure(casting).isEmpty());
    }
}
Also used : Casting(ai.grakn.kb.internal.structure.Casting) RoleImpl(ai.grakn.kb.internal.concept.RoleImpl) EntityTypeImpl(ai.grakn.kb.internal.concept.EntityTypeImpl) RelationshipType(ai.grakn.concept.RelationshipType) RelationshipImpl(ai.grakn.kb.internal.concept.RelationshipImpl) Thing(ai.grakn.concept.Thing) ThingImpl(ai.grakn.kb.internal.concept.ThingImpl) Test(org.junit.Test)

Example 14 with Thing

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

the class TestKB method putEntityWithResource.

public static Thing putEntityWithResource(GraknTx tx, String id, EntityType type, Label key) {
    Thing inst = type.addEntity();
    putResource(inst, tx.getSchemaConcept(key), id);
    return inst;
}
Also used : Thing(ai.grakn.concept.Thing)

Example 15 with Thing

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

the class GrpcServerIT method whenGettingARelationship_TheInformationOnTheRelationshipIsCorrect.

@Test
public void whenGettingARelationship_TheInformationOnTheRelationshipIsCorrect() {
    try (GraknTx remoteTx = remoteSession.open(GraknTxType.READ);
        GraknTx localTx = localSession.open(GraknTxType.READ)) {
        GetQuery query = remoteTx.graql().match(var("x").isa("has-cast")).get();
        Relationship remoteConcept = query.stream().findAny().get().get("x").asRelationship();
        Relationship localConcept = localTx.getConcept(remoteConcept.getId()).asRelationship();
        assertEqualConcepts(localConcept, remoteConcept, Relationship::rolePlayers);
        ImmutableMultimap.Builder<ConceptId, ConceptId> localRolePlayers = ImmutableMultimap.builder();
        localConcept.allRolePlayers().forEach((role, players) -> {
            for (Thing player : players) {
                localRolePlayers.put(role.getId(), player.getId());
            }
        });
        ImmutableMultimap.Builder<ConceptId, ConceptId> remoteRolePlayers = ImmutableMultimap.builder();
        remoteConcept.allRolePlayers().forEach((role, players) -> {
            for (Thing player : players) {
                remoteRolePlayers.put(role.getId(), player.getId());
            }
        });
        assertEquals(localRolePlayers.build(), remoteRolePlayers.build());
    }
}
Also used : GraknTx(ai.grakn.GraknTx) GetQuery(ai.grakn.graql.GetQuery) Relationship(ai.grakn.concept.Relationship) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) Thing(ai.grakn.concept.Thing) ConceptId(ai.grakn.concept.ConceptId) Test(org.junit.Test)

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