use of ai.grakn.concept.Entity in project grakn by graknlabs.
the class TxCacheTest method whenClosingTransaction_EnsureTransactionCacheIsEmpty.
@Test
public void whenClosingTransaction_EnsureTransactionCacheIsEmpty() {
TxCache cache = tx.txCache();
// Load some sample data
AttributeType<String> attributeType = tx.putAttributeType("Attribute Type", AttributeType.DataType.STRING);
Role role1 = tx.putRole("role 1");
Role role2 = tx.putRole("role 2");
EntityType entityType = tx.putEntityType("My Type").plays(role1).plays(role2).attribute(attributeType);
RelationshipType relationshipType = tx.putRelationshipType("My Relationship Type").relates(role1).relates(role2);
Entity e1 = entityType.addEntity();
Entity e2 = entityType.addEntity();
Attribute<String> r1 = attributeType.putAttribute("test");
e1.attribute(r1);
relationshipType.addRelationship().addRolePlayer(role1, e1).addRolePlayer(role2, e2);
// Check the caches are not empty
assertThat(cache.getConceptCache().keySet(), not(empty()));
assertThat(cache.getSchemaConceptCache().keySet(), not(empty()));
assertThat(cache.getLabelCache().keySet(), not(empty()));
assertThat(cache.getShardingCount().keySet(), not(empty()));
assertThat(cache.getModifiedCastings(), not(empty()));
// Close the transaction
tx.commit();
// Check the caches are empty
assertThat(cache.getConceptCache().keySet(), empty());
assertThat(cache.getSchemaConceptCache().keySet(), empty());
assertThat(cache.getLabelCache().keySet(), empty());
assertThat(cache.getShardingCount().keySet(), empty());
assertThat(cache.getModifiedThings(), empty());
assertThat(cache.getModifiedRoles(), empty());
assertThat(cache.getModifiedRelationshipTypes(), empty());
assertThat(cache.getModifiedRules(), empty());
assertThat(cache.getModifiedCastings(), empty());
}
use of ai.grakn.concept.Entity in project grakn by graknlabs.
the class TxCacheTest method whenDeletingAnInstanceWithNoRelations_EnsureLogIsEmpty.
@Test
public void whenDeletingAnInstanceWithNoRelations_EnsureLogIsEmpty() {
EntityType t1 = tx.putEntityType("1");
Entity i1 = t1.addEntity();
tx.commit();
tx = EmbeddedGraknSession.create(tx.keyspace(), Grakn.IN_MEMORY).open(GraknTxType.WRITE);
assertThat(tx.txCache().getModifiedThings(), is(empty()));
i1.delete();
assertThat(tx.txCache().getModifiedThings(), is(empty()));
}
use of ai.grakn.concept.Entity in project grakn by graknlabs.
the class EntityTest method whenCreatingAnInferredEntity_EnsureMarkedAsInferred.
@Test
public void whenCreatingAnInferredEntity_EnsureMarkedAsInferred() {
EntityTypeImpl et = EntityTypeImpl.from(tx.putEntityType("et"));
Entity entity = et.addEntity();
Entity entityInferred = et.addEntityInferred();
assertFalse(entity.isInferred());
assertTrue(entityInferred.isInferred());
}
use of ai.grakn.concept.Entity in project grakn by graknlabs.
the class StatisticsTest method addSchemaAndEntities.
private void addSchemaAndEntities() throws InvalidKBException {
try (GraknTx tx = session.open(GraknTxType.WRITE)) {
EntityType entityType1 = tx.putEntityType(thing);
EntityType entityType2 = tx.putEntityType(anotherThing);
Entity entity1 = entityType1.addEntity();
Entity entity2 = entityType1.addEntity();
Entity entity3 = entityType1.addEntity();
Entity entity4 = entityType2.addEntity();
entityId1 = entity1.getId();
entityId2 = entity2.getId();
entityId3 = entity3.getId();
entityId4 = entity4.getId();
Role relation1 = tx.putRole("relation1");
Role relation2 = tx.putRole("relation2");
entityType1.plays(relation1).plays(relation2);
entityType2.plays(relation1).plays(relation2);
RelationshipType related = tx.putRelationshipType("related").relates(relation1).relates(relation2);
related.addRelationship().addRolePlayer(relation1, entity1).addRolePlayer(relation2, entity2);
related.addRelationship().addRolePlayer(relation1, entity2).addRolePlayer(relation2, entity3);
related.addRelationship().addRolePlayer(relation1, entity2).addRolePlayer(relation2, entity4);
AttributeType<Double> attribute1 = tx.putAttributeType(resourceType1, AttributeType.DataType.DOUBLE);
AttributeType<Long> attribute2 = tx.putAttributeType(resourceType2, AttributeType.DataType.LONG);
AttributeType<Long> attribute3 = tx.putAttributeType(resourceType3, AttributeType.DataType.LONG);
AttributeType<String> attribute4 = tx.putAttributeType(resourceType4, AttributeType.DataType.STRING);
AttributeType<Long> attribute5 = tx.putAttributeType(resourceType5, AttributeType.DataType.LONG);
AttributeType<Double> attribute6 = tx.putAttributeType(resourceType6, AttributeType.DataType.DOUBLE);
AttributeType<Double> attribute7 = tx.putAttributeType(resourceType7, AttributeType.DataType.DOUBLE);
entityType1.attribute(attribute1);
entityType1.attribute(attribute2);
entityType1.attribute(attribute3);
entityType1.attribute(attribute4);
entityType1.attribute(attribute5);
entityType1.attribute(attribute6);
entityType1.attribute(attribute7);
entityType2.attribute(attribute1);
entityType2.attribute(attribute2);
entityType2.attribute(attribute3);
entityType2.attribute(attribute4);
entityType2.attribute(attribute5);
entityType2.attribute(attribute6);
entityType2.attribute(attribute7);
tx.commit();
}
}
use of ai.grakn.concept.Entity in project grakn by graknlabs.
the class StatisticsTest method testStd.
@Test
public void testStd() throws Exception {
Optional<Double> result;
// resource-type has no instance
addSchemaAndEntities();
try (GraknTx graph = session.open(GraknTxType.READ)) {
result = Graql.compute().std().of(resourceType1).in(Collections.emptyList()).withTx(graph).execute();
assertFalse(result.isPresent());
result = Graql.compute().std().of(resourceType1).withTx(graph).execute();
assertFalse(result.isPresent());
result = Graql.compute().withTx(graph).std().of(resourceType1).execute();
assertFalse(result.isPresent());
result = Graql.compute().std().withTx(graph).of(resourceType1).execute();
assertFalse(result.isPresent());
result = graph.graql().compute().std().of(resourceType2).execute();
assertFalse(result.isPresent());
result = graph.graql().compute().std().of(resourceType2, resourceType5).execute();
assertFalse(result.isPresent());
result = graph.graql().compute().std().of(resourceType2).withTx(graph).execute();
assertFalse(result.isPresent());
result = graph.graql().compute().withTx(graph).std().of(resourceType2).execute();
assertFalse(result.isPresent());
}
// add resources, but resources are not connected to any entities
addResourcesInstances();
try (GraknTx graph = session.open(GraknTxType.READ)) {
result = Graql.compute().std().of(resourceType1).withTx(graph).execute();
assertFalse(result.isPresent());
result = Graql.compute().std().of(resourceType1).in().withTx(graph).execute();
assertFalse(result.isPresent());
result = graph.graql().compute().std().of(resourceType2).in(thing, anotherThing).execute();
assertFalse(result.isPresent());
result = Graql.compute().std().of(resourceType2).withTx(graph).in(anotherThing).execute();
assertFalse(result.isPresent());
}
// connect entity and resources
addResourceRelations();
try (GraknTx graph = session.open(GraknTxType.READ)) {
result = Graql.compute().std().of(resourceType1).withTx(graph).execute();
assertEquals(Math.sqrt(0.18 / 3), result.get(), delta);
result = Graql.compute().std().of(resourceType2).withTx(graph).in(anotherThing).execute();
assertEquals(Math.sqrt(0D), result.get(), delta);
result = graph.graql().compute().std().of(resourceType1, resourceType6).execute();
assertEquals(Math.sqrt(54.18 / 6), result.get(), delta);
result = graph.graql().compute().std().of(resourceType2, resourceType5).in(thing, anotherThing).execute();
assertEquals(Math.sqrt(110.0 / 6), result.get(), delta);
result = graph.graql().compute().std().of(resourceType2).in(thing).execute();
assertEquals(2.5, result.get(), delta);
}
List<Long> list = new ArrayList<>();
long workerNumber = 3L;
if (GraknTestUtil.usingTinker())
workerNumber = 1;
for (long i = 0L; i < workerNumber; i++) {
list.add(i);
}
List<Double> numberList = list.parallelStream().map(i -> {
try (GraknTx graph = session.open(GraknTxType.READ)) {
return graph.graql().compute().std().of(resourceType2).in(thing).execute().get();
}
}).collect(Collectors.toList());
numberList.forEach(value -> assertEquals(2.5D, value.doubleValue(), delta));
}
Aggregations