Search in sources :

Example 11 with Relationship

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

the class RelationshipAtom method materialise.

@Override
public Stream<Answer> materialise() {
    RelationshipType relationType = getSchemaConcept().asRelationshipType();
    Multimap<Role, Var> roleVarMap = getRoleVarMap();
    Answer substitution = getParentQuery().getSubstitution();
    Relationship relationship = RelationshipTypeImpl.from(relationType).addRelationshipInferred();
    roleVarMap.asMap().forEach((key, value) -> value.forEach(var -> relationship.addRolePlayer(key, substitution.get(var).asThing())));
    Answer relationSub = getRoleSubstitution().merge(getVarName().isUserDefinedName() ? new QueryAnswer(ImmutableMap.of(getVarName(), relationship)) : new QueryAnswer());
    return Stream.of(substitution.merge(relationSub));
}
Also used : Role(ai.grakn.concept.Role) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Pair(ai.grakn.graql.internal.reasoner.utils.Pair) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) VarPattern(ai.grakn.graql.VarPattern) RelationshipProperty(ai.grakn.graql.internal.pattern.property.RelationshipProperty) RelationshipTypeImpl(ai.grakn.kb.internal.concept.RelationshipTypeImpl) Graql(ai.grakn.graql.Graql) ReasonerUtils.top(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.top) Type(ai.grakn.concept.Type) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) EntityType(ai.grakn.concept.EntityType) HashMultimap(com.google.common.collect.HashMultimap) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType) GraknTx(ai.grakn.GraknTx) Map(java.util.Map) ReasonerUtils.supers(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.supers) RoleConverter(ai.grakn.graql.internal.reasoner.utils.conversion.RoleConverter) ConceptId(ai.grakn.concept.ConceptId) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSet(com.google.common.collect.ImmutableSet) ValuePredicate(ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Set(java.util.Set) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) Atomic(ai.grakn.graql.admin.Atomic) List(java.util.List) Stream(java.util.stream.Stream) Var(ai.grakn.graql.Var) AutoValue(com.google.auto.value.AutoValue) ReasonerUtils.compatibleRoles(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.compatibleRoles) UnifierType(ai.grakn.graql.internal.reasoner.UnifierType) Iterables(com.google.common.collect.Iterables) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) Role(ai.grakn.concept.Role) Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) ReasonerUtils.compatibleRelationTypesWithRoles(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.compatibleRelationTypesWithRoles) HashMap(java.util.HashMap) Answer(ai.grakn.graql.admin.Answer) Multimap(com.google.common.collect.Multimap) Rule(ai.grakn.concept.Rule) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ReasonerUtils.multimapIntersection(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.multimapIntersection) ImmutableList(com.google.common.collect.ImmutableList) CommonUtil(ai.grakn.util.CommonUtil) Predicate(ai.grakn.graql.internal.reasoner.atom.predicate.Predicate) Relationship(ai.grakn.concept.Relationship) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) TypeConverter(ai.grakn.graql.internal.reasoner.utils.conversion.TypeConverter) Nullable(javax.annotation.Nullable) ErrorMessage(ai.grakn.util.ErrorMessage) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Iterator(java.util.Iterator) Memoized(com.google.auto.value.extension.memoized.Memoized) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) UnifierImpl(ai.grakn.graql.internal.reasoner.UnifierImpl) ReasonerUtils(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils) VarProperty(ai.grakn.graql.admin.VarProperty) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) IsaProperty(ai.grakn.graql.internal.pattern.property.IsaProperty) UnifierComparison(ai.grakn.graql.admin.UnifierComparison) RelationPlayer(ai.grakn.graql.admin.RelationPlayer) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Schema(ai.grakn.util.Schema) Pattern(ai.grakn.graql.Pattern) Comparator(java.util.Comparator) Unifier(ai.grakn.graql.admin.Unifier) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) Var(ai.grakn.graql.Var) Relationship(ai.grakn.concept.Relationship) RelationshipType(ai.grakn.concept.RelationshipType)

Example 12 with Relationship

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

the class ValidatorTest method whenCreatingRelationWithoutLinkingRelates_Throw.

@Test
public void whenCreatingRelationWithoutLinkingRelates_Throw() {
    Role hunter = tx.putRole("hunter");
    Role monster = tx.putRole("monster");
    EntityType stuff = tx.putEntityType("Stuff").plays(hunter).plays(monster);
    RelationshipType kills = tx.putRelationshipType("kills").relates(hunter);
    Entity myHunter = stuff.addEntity();
    Entity myMonster = stuff.addEntity();
    Relationship relation = kills.addRelationship().addRolePlayer(hunter, myHunter).addRolePlayer(monster, myMonster);
    expectedException.expect(InvalidKBException.class);
    expectedException.expectMessage(containsString(ErrorMessage.VALIDATION_RELATION_CASTING_LOOP_FAIL.getMessage(relation.getId(), monster.getLabel(), kills.getLabel())));
    tx.commit();
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) Entity(ai.grakn.concept.Entity) Relationship(ai.grakn.concept.Relationship) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 13 with Relationship

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

the class ValidatorTest method whenARoleInARelationIsPlayedAZillionTimes_TheGraphIsValid.

@Test
public void whenARoleInARelationIsPlayedAZillionTimes_TheGraphIsValid() {
    Role role1 = tx.putRole("role-1");
    Role role2 = tx.putRole("role-2");
    RelationshipType relationshipType = tx.putRelationshipType("my-relationship").relates(role1).relates(role2);
    EntityType entityType = tx.putEntityType("my-entity").plays(role1);
    Relationship relationship = relationshipType.addRelationship();
    Set<Thing> things = new HashSet<>();
    int oneZillion = 100;
    for (int i = 0; i < oneZillion; i++) {
        Thing thing = entityType.addEntity();
        things.add(thing);
        relationship.addRolePlayer(role1, thing);
    }
    assertEquals(things, relationship.rolePlayers(role1).collect(toSet()));
    tx.commit();
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) Relationship(ai.grakn.concept.Relationship) RelationshipType(ai.grakn.concept.RelationshipType) Thing(ai.grakn.concept.Thing) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 14 with Relationship

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

the class ValidatorTest method whenRemovingInvalidRolePlayers_EnsureValidationPasses.

@Test
public void whenRemovingInvalidRolePlayers_EnsureValidationPasses() {
    Role chased = tx.putRole("chased");
    Role chaser = tx.putRole("chaser");
    RelationshipType chases = tx.putRelationshipType("chases").relates(chased).relates(chaser);
    EntityType puppy = tx.putEntityType("puppy").plays(chaser);
    Entity dunstan = puppy.addEntity();
    Relationship rel = chases.addRelationship();
    rel.addRolePlayer(chaser, dunstan);
    rel.addRolePlayer(chased, dunstan).removeRolePlayer(chased, dunstan);
    tx.commit();
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) Entity(ai.grakn.concept.Entity) Relationship(ai.grakn.concept.Relationship) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 15 with Relationship

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

the class RemoteConceptsTest method whenCallingRelationshipsWithNoArguments_GetTheExpectedResult.

@Test
public void whenCallingRelationshipsWithNoArguments_GetTheExpectedResult() {
    Relationship a = RemoteConcepts.createRelationship(tx, A);
    Relationship b = RemoteConcepts.createRelationship(tx, B);
    Relationship c = RemoteConcepts.createRelationship(tx, C);
    mockConceptMethod(ConceptMethods.GET_RELATIONSHIPS, Stream.of(a, b, c));
    assertThat(thing.relationships().collect(toSet()), containsInAnyOrder(a, b, c));
}
Also used : Relationship(ai.grakn.concept.Relationship) Test(org.junit.Test)

Aggregations

Relationship (ai.grakn.concept.Relationship)50 Test (org.junit.Test)36 Role (ai.grakn.concept.Role)27 EntityType (ai.grakn.concept.EntityType)22 RelationshipType (ai.grakn.concept.RelationshipType)22 Entity (ai.grakn.concept.Entity)15 Thing (ai.grakn.concept.Thing)11 Label (ai.grakn.concept.Label)9 HashSet (java.util.HashSet)9 Attribute (ai.grakn.concept.Attribute)8 ConceptId (ai.grakn.concept.ConceptId)8 GraknTx (ai.grakn.GraknTx)7 Set (java.util.Set)7 Concept (ai.grakn.concept.Concept)6 Stream (java.util.stream.Stream)6 Type (ai.grakn.concept.Type)5 Answer (ai.grakn.graql.admin.Answer)5 Schema (ai.grakn.util.Schema)5 Property (com.pholser.junit.quickcheck.Property)5 Collectors (java.util.stream.Collectors)5