use of ai.grakn.concept.Attribute in project grakn by graknlabs.
the class TestKB method putResource.
public static <T> void putResource(Thing thing, AttributeType<T> attributeType, T resource) {
Attribute attributeInstance = attributeType.putAttribute(resource);
thing.attribute(attributeInstance);
}
use of ai.grakn.concept.Attribute 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.Attribute in project grakn by graknlabs.
the class ThingImpl method deleteAttribute.
@Override
public T deleteAttribute(Attribute attribute) {
Role roleHasOwner = vertex().tx().getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(attribute.type().getLabel()));
Role roleKeyOwner = vertex().tx().getSchemaConcept(Schema.ImplicitType.KEY_OWNER.getLabel(attribute.type().getLabel()));
Role roleHasValue = vertex().tx().getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(attribute.type().getLabel()));
Role roleKeyValue = vertex().tx().getSchemaConcept(Schema.ImplicitType.KEY_VALUE.getLabel(attribute.type().getLabel()));
Stream<Relationship> relationships = relationships(filterNulls(roleHasOwner, roleKeyOwner));
relationships.filter(relationship -> {
Stream<Thing> rolePlayers = relationship.rolePlayers(filterNulls(roleHasValue, roleKeyValue));
return rolePlayers.anyMatch(rolePlayer -> rolePlayer.equals(attribute));
}).forEach(Concept::delete);
return getThis();
}
use of ai.grakn.concept.Attribute in project grakn by graknlabs.
the class ThingImpl method attributeRelationship.
private Relationship attributeRelationship(Attribute attribute, boolean isInferred) {
Schema.ImplicitType has = Schema.ImplicitType.HAS;
Schema.ImplicitType hasValue = Schema.ImplicitType.HAS_VALUE;
Schema.ImplicitType hasOwner = Schema.ImplicitType.HAS_OWNER;
// Is this attribute a key to me?
if (type().keys().anyMatch(rt -> rt.equals(attribute.type()))) {
has = Schema.ImplicitType.KEY;
hasValue = Schema.ImplicitType.KEY_VALUE;
hasOwner = Schema.ImplicitType.KEY_OWNER;
}
Label label = attribute.type().getLabel();
RelationshipType hasAttribute = vertex().tx().getSchemaConcept(has.getLabel(label));
Role hasAttributeOwner = vertex().tx().getSchemaConcept(hasOwner.getLabel(label));
Role hasAttributeValue = vertex().tx().getSchemaConcept(hasValue.getLabel(label));
if (hasAttribute == null || hasAttributeOwner == null || hasAttributeValue == null || type().plays().noneMatch(play -> play.equals(hasAttributeOwner))) {
throw GraknTxOperationException.hasNotAllowed(this, attribute);
}
EdgeElement attributeEdge = addEdge(AttributeImpl.from(attribute), Schema.EdgeLabel.ATTRIBUTE);
if (isInferred)
attributeEdge.property(Schema.EdgeProperty.IS_INFERRED, true);
return vertex().tx().factory().buildRelation(attributeEdge, hasAttribute, hasAttributeOwner, hasAttributeValue);
}
use of ai.grakn.concept.Attribute 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