use of ai.grakn.concept.Attribute 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.Attribute in project grakn by graknlabs.
the class GraknTxPropertyTest method whenCallingGetResourcesByValueAfterAddingAResource_TheResultIncludesTheResource.
@Property
public void whenCallingGetResourcesByValueAfterAddingAResource_TheResultIncludesTheResource(@Open GraknTx graph, @FromTx @NonMeta @NonAbstract AttributeType attributeType, @From(ResourceValues.class) Object value) {
assumeThat(value.getClass().getName(), is(attributeType.getDataType().getName()));
Collection<Attribute<Object>> expectedAttributes = graph.getAttributesByValue(value);
Attribute attribute = attributeType.putAttribute(value);
Collection<Attribute<Object>> resourcesAfter = graph.getAttributesByValue(value);
expectedAttributes.add(attribute);
assertEquals(expectedAttributes, resourcesAfter);
}
use of ai.grakn.concept.Attribute in project grakn by graknlabs.
the class ShortestPathTest method testResourceVerticesAndEdges.
@Test
public void testResourceVerticesAndEdges() {
ConceptId idPerson1;
ConceptId idPerson2;
ConceptId idPerson3;
ConceptId idPower1;
ConceptId idPower2;
ConceptId idPower3;
ConceptId idRelationPerson1Power1;
ConceptId idRelationPerson2Power2;
ConceptId idRelationPerson1Person3;
List<ConceptId> pathPerson2Power1 = new ArrayList<>();
List<ConceptId> pathPower3Power1 = new ArrayList<>();
List<ConceptId> pathPerson3Power3 = new ArrayList<>();
try (GraknTx tx = session.open(GraknTxType.WRITE)) {
EntityType person = tx.putEntityType("person");
AttributeType<Long> power = tx.putAttributeType("power", AttributeType.DataType.LONG);
person.attribute(power);
// manually construct the attribute relation
Role resourceOwner = tx.getRole(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of("power")).getValue());
Role resourceValue = tx.getRole(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of("power")).getValue());
RelationshipType relationType = tx.getRelationshipType(Schema.ImplicitType.HAS.getLabel(Label.of("power")).getValue());
Entity person1 = person.addEntity();
idPerson1 = person1.getId();
Entity person2 = person.addEntity();
idPerson2 = person2.getId();
Entity person3 = person.addEntity();
idPerson3 = person3.getId();
Attribute power1 = power.putAttribute(1L);
idPower1 = power1.getId();
Attribute power2 = power.putAttribute(2L);
idPower2 = power2.getId();
Attribute power3 = power.putAttribute(3L);
idPower3 = power3.getId();
assert relationType != null;
idRelationPerson1Power1 = relationType.addRelationship().addRolePlayer(resourceOwner, person1).addRolePlayer(resourceValue, power1).getId();
idRelationPerson2Power2 = relationType.addRelationship().addRolePlayer(resourceOwner, person2).addRolePlayer(resourceValue, power2).getId();
// add implicit resource relationships as well
person.attribute(power);
person1.attribute(power2);
person3.attribute(power3);
// finally add a relation between persons to make it more interesting
Role role1 = tx.putRole("role1");
Role role2 = tx.putRole("role2");
person.plays(role1).plays(role2);
RelationshipType relationTypePerson = tx.putRelationshipType(related).relates(role1).relates(role2);
idRelationPerson1Person3 = relationTypePerson.addRelationship().addRolePlayer(role1, person1).addRolePlayer(role2, person3).getId();
tx.commit();
}
try (GraknTx graph = session.open(GraknTxType.READ)) {
List<List<Concept>> allPaths;
// Path from power3 to power3
pathPerson3Power3.add(idPerson3);
if (null != getResourceEdgeId(graph, idPower3, idPerson3)) {
pathPerson3Power3.add(getResourceEdgeId(graph, idPower3, idPerson3));
}
pathPerson3Power3.add(idPower3);
allPaths = graph.graql().compute().paths().from(idPerson3).to(idPower3).includeAttribute().execute();
assertEquals(1, allPaths.size());
checkPathsAreEqual(pathPerson3Power3, allPaths.get(0));
// Path from person2 to power1
pathPerson2Power1.add(idPerson2);
pathPerson2Power1.add(idRelationPerson2Power2);
pathPerson2Power1.add(idPower2);
if (null != getResourceEdgeId(graph, idPerson1, idPower2)) {
pathPerson2Power1.add(getResourceEdgeId(graph, idPerson1, idPower2));
}
pathPerson2Power1.add(idPerson1);
pathPerson2Power1.add(idRelationPerson1Power1);
pathPerson2Power1.add(idPower1);
allPaths = graph.graql().compute().paths().from(idPerson2).to(idPower1).includeAttribute().execute();
assertEquals(1, allPaths.size());
checkPathsAreEqual(pathPerson2Power1, allPaths.get(0));
// Path from power3 to power1
pathPower3Power1.add(idPower3);
if (null != getResourceEdgeId(graph, idPower3, idPerson3)) {
pathPower3Power1.add(getResourceEdgeId(graph, idPower3, idPerson3));
}
pathPower3Power1.add(idPerson3);
pathPower3Power1.add(idRelationPerson1Person3);
pathPower3Power1.add(idPerson1);
pathPower3Power1.add(idRelationPerson1Power1);
pathPower3Power1.add(idPower1);
allPaths = graph.graql().compute().paths().includeAttribute().from(idPower3).to(idPower1).execute();
assertEquals(1, allPaths.size());
checkPathsAreEqual(pathPower3Power1, allPaths.get(0));
}
}
use of ai.grakn.concept.Attribute in project grakn by graknlabs.
the class StatisticsTest method testHasResourceVerticesAndEdges.
@Test
public void testHasResourceVerticesAndEdges() {
try (GraknTx tx = session.open(GraknTxType.WRITE)) {
// manually construct the relation type and instance
AttributeType<Long> power = tx.putAttributeType("power", AttributeType.DataType.LONG);
EntityType person = tx.putEntityType("person").attribute(power);
Role resourceOwner = tx.getRole(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of("power")).getValue());
Role resourceValue = tx.getRole(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of("power")).getValue());
person.attribute(power);
Entity person1 = person.addEntity();
Entity person2 = person.addEntity();
Entity person3 = person.addEntity();
Attribute power1 = power.putAttribute(1L);
Attribute power2 = power.putAttribute(2L);
Attribute power3 = power.putAttribute(3L);
RelationshipType relationType = tx.putRelationshipType(Schema.ImplicitType.HAS.getLabel(Label.of("power"))).relates(resourceOwner).relates(resourceValue);
relationType.addRelationship().addRolePlayer(resourceOwner, person1).addRolePlayer(resourceValue, power1);
relationType.addRelationship().addRolePlayer(resourceOwner, person2).addRolePlayer(resourceValue, power2);
person1.attribute(power2);
person3.attribute(power3);
tx.commit();
}
Optional<Number> result;
try (GraknTx graph = session.open(GraknTxType.READ)) {
// No need to test all statistics as most of them share the same vertex program
result = graph.graql().compute().min().of("power").in().execute();
assertEquals(1L, result.get().longValue());
result = graph.graql().compute().max().of("power").in().execute();
assertEquals(3L, result.get().longValue());
result = graph.graql().compute().sum().of("power").in().execute();
assertEquals(8L, result.get().longValue());
result = graph.graql().compute().median().of("power").in().execute();
assertEquals(2L, result.get().longValue());
}
}
use of ai.grakn.concept.Attribute in project grakn by graknlabs.
the class ConnectedComponentTest method testConnectedComponentImplicitType.
@Test
public void testConnectedComponentImplicitType() {
String aResourceTypeLabel = "aResourceTypeLabel";
addSchemaAndEntities();
addResourceRelations();
try (GraknTx graph = session.open(GraknTxType.WRITE)) {
AttributeType<String> attributeType = graph.putAttributeType(aResourceTypeLabel, AttributeType.DataType.STRING);
graph.getEntityType(thing).attribute(attributeType);
graph.getEntityType(anotherThing).attribute(attributeType);
Attribute aAttribute = attributeType.putAttribute("blah");
graph.getEntityType(thing).instances().forEach(instance -> instance.attribute(aAttribute));
graph.getEntityType(anotherThing).instances().forEach(instance -> instance.attribute(aAttribute));
graph.commit();
}
try (GraknTx graph = session.open(GraknTxType.READ)) {
Map<String, Set<String>> result = graph.graql().compute().cluster().usingConnectedComponent().in(thing, anotherThing, aResourceTypeLabel, Schema.ImplicitType.HAS.getLabel(aResourceTypeLabel).getValue()).members().execute();
assertEquals(1, result.size());
assertEquals(5, result.values().iterator().next().size());
result = graph.graql().compute().cluster().usingConnectedComponent().in(thing, anotherThing, aResourceTypeLabel, Schema.ImplicitType.HAS.getLabel(aResourceTypeLabel).getValue()).members().of(entityId2).execute();
assertEquals(1, result.size());
assertEquals(entityId2.getValue(), result.keySet().iterator().next());
assertEquals(5, result.values().iterator().next().size());
assertEquals(1, graph.graql().compute().cluster().usingConnectedComponent().includeAttribute().in(thing, anotherThing, aResourceTypeLabel, Schema.ImplicitType.HAS.getLabel(aResourceTypeLabel).getValue()).includeAttribute().members().execute().size());
}
}
Aggregations