use of ai.grakn.concept.AttributeType in project grakn by graknlabs.
the class AttributeTest method whenCreatingResourceWithAnInvalidDataType_DoNotCreateTheResource.
// this is deliberately an incorrect type for the test
@SuppressWarnings("unchecked")
@Test
public void whenCreatingResourceWithAnInvalidDataType_DoNotCreateTheResource() {
AttributeType longAttributeType = tx.putAttributeType("long", AttributeType.DataType.LONG);
try {
longAttributeType.putAttribute("Invalid Thing");
fail("Expected to throw");
} catch (GraknTxOperationException e) {
// expected failure
}
Collection<Attribute> instances = (Collection<Attribute>) longAttributeType.instances().collect(toSet());
assertThat(instances, empty());
}
use of ai.grakn.concept.AttributeType in project grakn by graknlabs.
the class EntityTypeTest method whenAddingResourcesWithSubTypesToEntityTypes_EnsureImplicitStructureFollowsSubTypes.
@Test
public void whenAddingResourcesWithSubTypesToEntityTypes_EnsureImplicitStructureFollowsSubTypes() {
EntityType entityType1 = tx.putEntityType("Entity Type 1");
EntityType entityType2 = tx.putEntityType("Entity Type 2");
Label superLabel = Label.of("Super Attribute Type");
Label label = Label.of("Attribute Type");
AttributeType rtSuper = tx.putAttributeType(superLabel, AttributeType.DataType.STRING);
AttributeType rt = tx.putAttributeType(label, AttributeType.DataType.STRING).sup(rtSuper);
entityType1.attribute(rtSuper);
entityType2.attribute(rt);
// Check role types are only built explicitly
assertThat(entityType1.plays().collect(toSet()), containsInAnyOrder(tx.getRole(Schema.ImplicitType.HAS_OWNER.getLabel(superLabel).getValue())));
assertThat(entityType2.plays().collect(toSet()), containsInAnyOrder(tx.getRole(Schema.ImplicitType.HAS_OWNER.getLabel(label).getValue())));
// Check Implicit Types Follow SUB Structure
RelationshipType rtSuperRelation = tx.getSchemaConcept(Schema.ImplicitType.HAS.getLabel(rtSuper.getLabel()));
Role rtSuperRoleOwner = tx.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(rtSuper.getLabel()));
Role rtSuperRoleValue = tx.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(rtSuper.getLabel()));
RelationshipType rtRelation = tx.getSchemaConcept(Schema.ImplicitType.HAS.getLabel(rt.getLabel()));
Role reRoleOwner = tx.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(rt.getLabel()));
Role reRoleValue = tx.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(rt.getLabel()));
assertEquals(rtSuperRoleOwner, reRoleOwner.sup());
assertEquals(rtSuperRoleValue, reRoleValue.sup());
assertEquals(rtSuperRelation, rtRelation.sup());
}
use of ai.grakn.concept.AttributeType in project grakn by graknlabs.
the class EntityTypeTest method checkThatResourceTypesCanBeRetrievedFromTypes.
@Test
public void checkThatResourceTypesCanBeRetrievedFromTypes() {
EntityType e1 = tx.putEntityType("e1");
AttributeType r1 = tx.putAttributeType("r1", AttributeType.DataType.STRING);
AttributeType r2 = tx.putAttributeType("r2", AttributeType.DataType.LONG);
AttributeType r3 = tx.putAttributeType("r3", AttributeType.DataType.BOOLEAN);
assertTrue("Entity is linked to resources when it shouldn't", e1.attributes().collect(toSet()).isEmpty());
e1.attribute(r1);
e1.attribute(r2);
e1.attribute(r3);
assertThat(e1.attributes().collect(toSet()), containsInAnyOrder(r1, r2, r3));
}
use of ai.grakn.concept.AttributeType in project grakn by graknlabs.
the class AbstractThingGenerator method generateFromTx.
@Override
protected final T generateFromTx() {
T thing;
S type = genFromTx(generatorClass).makeExcludeAbstractTypes().excludeMeta().generate(random, status);
// noinspection unchecked
Collection<T> instances = (Collection<T>) type.instances().collect(toSet());
if (instances.isEmpty()) {
thing = newInstance(type);
} else {
thing = random.choose(instances);
}
if (withResource && !thing.attributes().findAny().isPresent()) {
// A new attribute type is created every time a attribute is lacking.
// Existing attribute types and resources of those types are not used because we end up mutating the
// the schema in strange ways. This approach is less complex but ensures everything has a attribute
// without conflicting with the schema
// Create a new attribute type
AttributeType.DataType<?> dataType = gen(AttributeType.DataType.class);
Label label = genFromTx(Labels.class).mustBeUnused().generate(random, status);
AttributeType attributeType = tx().putAttributeType(label, dataType);
// Create new attribute
Attribute attribute = newResource(attributeType);
// Link everything together
type.attribute(attributeType);
thing.attribute(attribute);
}
return thing;
}
use of ai.grakn.concept.AttributeType in project grakn by graknlabs.
the class PostProcessingTest method whenCreatingDuplicateResources_EnsureTheyAreMergedInPost.
@Test
public void whenCreatingDuplicateResources_EnsureTheyAreMergedInPost() throws InvalidKBException, InterruptedException, JsonProcessingException {
String value = "1";
String sample = "Sample";
// Create GraknTx With Duplicate Resources
EmbeddedGraknTx<?> tx = session.open(GraknTxType.WRITE);
AttributeType<String> attributeType = tx.putAttributeType(sample, AttributeType.DataType.STRING);
Attribute<String> attribute = attributeType.putAttribute(value);
tx.commitSubmitNoLogs();
tx = session.open(GraknTxType.WRITE);
assertEquals(1, attributeType.instances().count());
// Check duplicates have been created
Set<Vertex> resource1 = createDuplicateResource(tx, attributeType, attribute);
Set<Vertex> resource2 = createDuplicateResource(tx, attributeType, attribute);
Set<Vertex> resource3 = createDuplicateResource(tx, attributeType, attribute);
Set<Vertex> resource4 = createDuplicateResource(tx, attributeType, attribute);
assertEquals(5, attributeType.instances().count());
// Attribute vertex index
String resourceIndex = resource1.iterator().next().value(INDEX.name()).toString();
// Merge the attribute sets
Set<Vertex> merged = Sets.newHashSet();
merged.addAll(resource1);
merged.addAll(resource2);
merged.addAll(resource3);
merged.addAll(resource4);
tx.close();
// Casting sets as ConceptIds
Set<ConceptId> resourceConcepts = merged.stream().map(c -> ConceptId.of(Schema.PREFIX_VERTEX + c.id().toString())).collect(toSet());
// Create Commit Log
CommitLog commitLog = CommitLog.createDefault(tx.keyspace());
commitLog.attributes().put(resourceIndex, resourceConcepts);
// Submit it
postProcessor.submit(commitLog);
// Force running the PP job
engine.server().backgroundTaskRunner().tasks().forEach(BackgroundTask::run);
Thread.sleep(2000);
tx = session.open(GraknTxType.READ);
// Check it's fixed
assertEquals(1, tx.getAttributeType(sample).instances().count());
tx.close();
}
Aggregations