Search in sources :

Example 21 with Relationship

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

the class EmbeddedGraknTx method fixDuplicateResources.

/**
 * Merges the provided duplicate resources
 *
 * @param resourceVertexIds The resource vertex ids which need to be merged.
 * @return True if a commit is required.
 */
public boolean fixDuplicateResources(String index, Set<ConceptId> resourceVertexIds) {
    // This is done to ensure we merge into the indexed casting.
    Optional<AttributeImpl<?>> mainResourceOp = this.getConcept(Schema.VertexProperty.INDEX, index);
    if (!mainResourceOp.isPresent()) {
        LOG.debug(String.format("Could not post process concept with index {%s} due to not finding the concept", index));
        return false;
    }
    AttributeImpl<?> mainResource = mainResourceOp.get();
    Set<AttributeImpl> duplicates = getDuplicates(mainResource, resourceVertexIds);
    if (duplicates.size() > 0) {
        // Remove any resources associated with this index that are not the main resource
        for (Attribute otherAttribute : duplicates) {
            Stream<Relationship> otherRelations = otherAttribute.relationships();
            // Copy the actual relation
            otherRelations.forEach(otherRelation -> copyRelation(mainResource, otherAttribute, otherRelation));
            // Delete the node
            AttributeImpl.from(otherAttribute).deleteNode();
        }
        // Restore the index
        String newIndex = mainResource.getIndex();
        // NOTE: Vertex Element is used directly here otherwise property is not actually restored!
        // NOTE: Remove or change this line at your own peril!
        mainResource.vertex().element().property(Schema.VertexProperty.INDEX.name(), newIndex);
        return true;
    }
    return false;
}
Also used : Attribute(ai.grakn.concept.Attribute) AttributeImpl(ai.grakn.kb.internal.concept.AttributeImpl) Relationship(ai.grakn.concept.Relationship)

Example 22 with Relationship

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

the class InsertQueryTest method whenAddingAnAttributeRelationshipWithProvenance_TheAttributeAndProvenanceAreAdded.

@Test
public void whenAddingAnAttributeRelationshipWithProvenance_TheAttributeAndProvenanceAreAdded() {
    InsertQuery query = qb.insert(y.has("provenance", z.val("Someone told me")), w.isa("movie").has(title, x.val("My Movie"), y));
    Answer answer = Iterables.getOnlyElement(query.execute());
    Entity movie = answer.get(w).asEntity();
    Attribute<String> theTitle = answer.get(x).asAttribute();
    Relationship hasTitle = answer.get(y).asRelationship();
    Attribute<String> provenance = answer.get(z).asAttribute();
    assertThat(hasTitle.rolePlayers().toArray(), arrayContainingInAnyOrder(movie, theTitle));
    assertThat(hasTitle.attributes().toArray(), arrayContaining(provenance));
}
Also used : InsertQuery(ai.grakn.graql.InsertQuery) Answer(ai.grakn.graql.admin.Answer) Entity(ai.grakn.concept.Entity) Relationship(ai.grakn.concept.Relationship) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 23 with Relationship

use of ai.grakn.concept.Relationship 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 24 with Relationship

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

the class MatchTest method whenQueryingForHas_AllowReferringToTheImplicitRelation.

@Test
public void whenQueryingForHas_AllowReferringToTheImplicitRelation() {
    Label title = Label.of("title");
    RelationshipType hasTitle = movieKB.tx().getType(HAS.getLabel(title));
    Role titleOwner = movieKB.tx().getSchemaConcept(HAS_OWNER.getLabel(title));
    Role titleValue = movieKB.tx().getSchemaConcept(HAS_VALUE.getLabel(title));
    Relationship implicitRelation = hasTitle.instances().iterator().next();
    ConceptId owner = implicitRelation.rolePlayers(titleOwner).iterator().next().getId();
    ConceptId value = implicitRelation.rolePlayers(titleValue).iterator().next().getId();
    Match query = qb.match(x.id(owner).has(title, y.id(value), r));
    assertThat(query, variable(r, contains(MatchableConcept.of(implicitRelation))));
}
Also used : Role(ai.grakn.concept.Role) Relationship(ai.grakn.concept.Relationship) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType) ConceptId(ai.grakn.concept.ConceptId) Match(ai.grakn.graql.Match) Test(org.junit.Test)

Example 25 with Relationship

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

the class ConceptPropertyTest method assumeDeletable.

private static void assumeDeletable(GraknTx graph, Concept concept) {
    // TODO: A better way to handle these assumptions?
    if (concept.isSchemaConcept()) {
        SchemaConcept schemaConcept = concept.asSchemaConcept();
        assumeThat(schemaConcept.subs().collect(toSet()), contains(schemaConcept));
        if (schemaConcept.isType()) {
            Type type = schemaConcept.asType();
            assumeThat(type.instances().collect(toSet()), empty());
            assumeThat(type.getRulesOfHypothesis().collect(toSet()), empty());
            assumeThat(type.getRulesOfConclusion().collect(toSet()), empty());
        }
        if (schemaConcept.isRole()) {
            Role role = schemaConcept.asRole();
            assumeThat(role.playedByTypes().collect(toSet()), empty());
            assumeThat(role.relationshipTypes().collect(toSet()), empty());
            Stream<Relationship> allRelations = graph.admin().getMetaRelationType().instances();
            Set<Role> allRolesPlayed = allRelations.flatMap(relation -> relation.allRolePlayers().keySet().stream()).collect(toSet());
            assumeThat(allRolesPlayed, not(hasItem(role)));
        } else if (schemaConcept.isRelationshipType()) {
            assumeThat(schemaConcept.asRelationshipType().relates().collect(toSet()), empty());
        }
    }
}
Also used : Role(ai.grakn.concept.Role) FromTx(ai.grakn.generator.FromTxGenerator.FromTx) Assume.assumeThat(org.junit.Assume.assumeThat) Matchers.isA(org.hamcrest.Matchers.isA) Methods.mockParamsOf(ai.grakn.generator.Methods.mockParamsOf) Role(ai.grakn.concept.Role) Assume.assumeFalse(org.junit.Assume.assumeFalse) Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) MethodOf(ai.grakn.generator.Methods.MethodOf) RunWith(org.junit.runner.RunWith) Matchers.not(org.hamcrest.Matchers.not) Type(ai.grakn.concept.Type) Matchers.hasProperty(org.hamcrest.Matchers.hasProperty) Open(ai.grakn.generator.GraknTxs.Open) NonMeta(ai.grakn.generator.AbstractSchemaConceptGenerator.NonMeta) JUnitQuickcheck(com.pholser.junit.quickcheck.runner.JUnitQuickcheck) GraknTx(ai.grakn.GraknTx) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ConceptId(ai.grakn.concept.ConceptId) Relationship(ai.grakn.concept.Relationship) ExpectedException(org.junit.rules.ExpectedException) Method(java.lang.reflect.Method) Collectors.toSet(java.util.stream.Collectors.toSet) GraknTxOperationException(ai.grakn.exception.GraknTxOperationException) ErrorMessage(ai.grakn.util.ErrorMessage) Matchers.empty(org.hamcrest.Matchers.empty) Property(com.pholser.junit.quickcheck.Property) Set(java.util.Set) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) InvocationTargetException(java.lang.reflect.InvocationTargetException) Rule(org.junit.Rule) Stream(java.util.stream.Stream) Matchers.hasItem(org.hamcrest.Matchers.hasItem) Ignore(org.junit.Ignore) Matchers.contains(org.hamcrest.Matchers.contains) Assert.assertFalse(org.junit.Assert.assertFalse) GraknTxs.allConceptsFrom(ai.grakn.generator.GraknTxs.allConceptsFrom) Matchers.is(org.hamcrest.Matchers.is) Matchers.containsString(org.hamcrest.Matchers.containsString) Assert.assertEquals(org.junit.Assert.assertEquals) Type(ai.grakn.concept.Type) Relationship(ai.grakn.concept.Relationship) SchemaConcept(ai.grakn.concept.SchemaConcept)

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