use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.
the class ValidatorTest method whenCommittingRelationWithoutSpecifyingSchema_ThrowOnCommit.
@Test
public void whenCommittingRelationWithoutSpecifyingSchema_ThrowOnCommit() {
EntityType fakeType = tx.putEntityType("Fake Concept");
RelationshipType relationshipType = tx.putRelationshipType("kicks");
Role kicker = tx.putRole("kicker");
Role kickee = tx.putRole("kickee");
Thing kyle = fakeType.addEntity();
Thing icke = fakeType.addEntity();
relationshipType.addRelationship().addRolePlayer(kicker, kyle).addRolePlayer(kickee, icke);
String error1 = ErrorMessage.VALIDATION_CASTING.getMessage(kyle.type().getLabel(), kyle.getId(), kicker.getLabel());
String error2 = ErrorMessage.VALIDATION_CASTING.getMessage(icke.type().getLabel(), icke.getId(), kickee.getLabel());
expectedException.expect(InvalidKBException.class);
expectedException.expectMessage(allOf(containsString(error1), containsString(error2)));
tx.commit();
}
use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.
the class ValidatorTest method whenCommittingNonAbstractRelationTypeNotLinkedToAnyRoleType_Throw.
@Test
public void whenCommittingNonAbstractRelationTypeNotLinkedToAnyRoleType_Throw() {
RelationshipType alone = tx.putRelationshipType("alone");
expectedException.expect(InvalidKBException.class);
expectedException.expectMessage(containsString(ErrorMessage.VALIDATION_RELATION_TYPE.getMessage(alone.getLabel())));
tx.commit();
}
use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.
the class RemoteConceptsTest method whenCallingSubs_GetTheExpectedResult.
@Test
public void whenCallingSubs_GetTheExpectedResult() {
Type me = relationshipType;
Type mySub = RemoteConcepts.createRelationshipType(tx, A);
Type mySubsSub = RemoteConcepts.createRelationshipType(tx, B);
mockConceptMethod(ConceptMethods.GET_SUB_CONCEPTS, Stream.of(me, mySub, mySubsSub));
assertThat(relationshipType.subs().collect(toSet()), containsInAnyOrder(me, mySub, mySubsSub));
}
use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.
the class TypeImpl method has.
/**
* Creates a relation type which allows this type and a {@link ai.grakn.concept.Attribute} type to be linked.
* @param attributeType The {@link AttributeType} which instances of this type should be allowed to play.
* @param has the implicit relation type to build
* @param hasValue the implicit role type to build for the {@link AttributeType}
* @param hasOwner the implicit role type to build for the type
* @param required Indicates if the {@link ai.grakn.concept.Attribute} is required on the entity
* @return The {@link Type} itself
*/
private T has(AttributeType attributeType, Schema.ImplicitType has, Schema.ImplicitType hasValue, Schema.ImplicitType hasOwner, boolean required) {
// Check if this is a met type
checkSchemaMutationAllowed();
// Check if attribute type is the meta
if (Schema.MetaSchema.ATTRIBUTE.getLabel().equals(attributeType.getLabel())) {
throw GraknTxOperationException.metaTypeImmutable(attributeType.getLabel());
}
Label attributeLabel = attributeType.getLabel();
Role ownerRole = vertex().tx().putRoleTypeImplicit(hasOwner.getLabel(attributeLabel));
Role valueRole = vertex().tx().putRoleTypeImplicit(hasValue.getLabel(attributeLabel));
RelationshipType relationshipType = vertex().tx().putRelationTypeImplicit(has.getLabel(attributeLabel)).relates(ownerRole).relates(valueRole);
// Linking with ako structure if present
AttributeType attributeTypeSuper = attributeType.sup();
Label superLabel = attributeTypeSuper.getLabel();
if (!Schema.MetaSchema.ATTRIBUTE.getLabel().equals(superLabel)) {
// Check to make sure we dont add plays edges to meta types accidentally
Role ownerRoleSuper = vertex().tx().putRoleTypeImplicit(hasOwner.getLabel(superLabel));
Role valueRoleSuper = vertex().tx().putRoleTypeImplicit(hasValue.getLabel(superLabel));
RelationshipType relationshipTypeSuper = vertex().tx().putRelationTypeImplicit(has.getLabel(superLabel)).relates(ownerRoleSuper).relates(valueRoleSuper);
// Create the super type edges from sub role/relations to super roles/relation
ownerRole.sup(ownerRoleSuper);
valueRole.sup(valueRoleSuper);
relationshipType.sup(relationshipTypeSuper);
// Make sure the supertype attribute is linked with the role as well
((AttributeTypeImpl) attributeTypeSuper).plays(valueRoleSuper);
}
this.plays(ownerRole, required);
// TODO: Use explicit cardinality of 0-1 rather than just false
((AttributeTypeImpl) attributeType).plays(valueRole, false);
return getThis();
}
use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.
the class GraknTxTest method checkThatMainCentralCacheIsNotAffectedByTransactionModifications.
@Test
public void checkThatMainCentralCacheIsNotAffectedByTransactionModifications() throws InvalidKBException, ExecutionException, InterruptedException {
// Check Central cache is empty
assertCacheOnlyContainsMetaTypes();
Role r1 = tx.putRole("r1");
Role r2 = tx.putRole("r2");
EntityType e1 = tx.putEntityType("e1").plays(r1).plays(r2);
RelationshipType rel1 = tx.putRelationshipType("rel1").relates(r1).relates(r2);
// Purge the above concepts into the main cache
tx.commit();
tx = EmbeddedGraknSession.create(tx.keyspace(), Grakn.IN_MEMORY).open(GraknTxType.WRITE);
// Check cache is in good order
Collection<SchemaConcept> cachedValues = tx.getGlobalCache().getCachedTypes().values();
assertTrue("Type [" + r1 + "] was not cached", cachedValues.contains(r1));
assertTrue("Type [" + r2 + "] was not cached", cachedValues.contains(r2));
assertTrue("Type [" + e1 + "] was not cached", cachedValues.contains(e1));
assertTrue("Type [" + rel1 + "] was not cached", cachedValues.contains(rel1));
assertThat(e1.plays().collect(toSet()), containsInAnyOrder(r1, r2));
ExecutorService pool = Executors.newSingleThreadExecutor();
// Mutate Schema in a separate thread
pool.submit(() -> {
GraknTx innerGraph = Grakn.session(Grakn.IN_MEMORY, tx.keyspace()).open(GraknTxType.WRITE);
EntityType entityType = innerGraph.getEntityType("e1");
Role role = innerGraph.getRole("r1");
entityType.deletePlays(role);
}).get();
// Check the above mutation did not affect central repo
SchemaConcept foundE1 = tx.getGlobalCache().getCachedTypes().get(e1.getLabel());
assertTrue("Main cache was affected by transaction", foundE1.asType().plays().anyMatch(role -> role.equals(r1)));
}
Aggregations