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;
}
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));
}
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));
}
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))));
}
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());
}
}
}
Aggregations