use of ai.grakn.client.BatchExecutorClient in project grakn by graknlabs.
the class BenchmarkIT method loadRuleChainData.
private void loadRuleChainData(int N) {
final GraknClient graknClient = GraknClient.of(engine.uri());
String entityLabel = "genericEntity";
String attributeLabel = "index";
String baseRelationLabel = "relation1";
String genericRelationLabel = "relation";
String fromRoleLabel = "fromRole";
String toRoleLabel = "toRole";
// load ontology
try (GraknTx tx = session.open(GraknTxType.WRITE)) {
Role fromRole = tx.putRole(fromRoleLabel);
Role toRole = tx.putRole(toRoleLabel);
AttributeType<String> index = tx.putAttributeType(attributeLabel, AttributeType.DataType.STRING);
tx.putEntityType(entityLabel).plays(fromRole).plays(toRole).attribute(index);
// define N relation types
for (int i = 1; i <= N; i++) {
tx.putRelationshipType(genericRelationLabel + i).relates(fromRole).relates(toRole);
}
// define N rules
for (int i = 2; i <= N; i++) {
Var fromVar = Graql.var().asUserDefined();
Var intermedVar = Graql.var().asUserDefined();
Var toVar = Graql.var().asUserDefined();
VarPattern rulePattern = Graql.label("rule" + i).when(Graql.and(Graql.var().rel(Graql.label(fromRole.getLabel()), fromVar).rel(Graql.label(toRole.getLabel()), intermedVar).isa(baseRelationLabel), Graql.var().rel(Graql.label(fromRole.getLabel()), intermedVar).rel(Graql.label(toRole.getLabel()), toVar).isa(genericRelationLabel + (i - 1)))).then(Graql.and(Graql.var().rel(Graql.label(fromRole.getLabel()), fromVar).rel(Graql.label(toRole.getLabel()), toVar).isa(genericRelationLabel + i)));
tx.graql().define(rulePattern).execute();
}
tx.commit();
}
// insert N + 1 entities
loadEntities(entityLabel, N + 1, graknClient, keyspace);
// load initial relation instances
try (BatchExecutorClient loader = BatchExecutorClient.newBuilder().taskClient(graknClient).build()) {
try (GraknTx tx = session.open(GraknTxType.READ)) {
Var entityVar = var().asUserDefined();
ConceptId[] instances = tx.graql().match(entityVar.isa(entityLabel)).get().execute().stream().map(ans -> ans.get(entityVar).getId()).toArray(ConceptId[]::new);
RelationshipType baseRelation = tx.getRelationshipType(baseRelationLabel);
Role fromRole = tx.getRole(fromRoleLabel);
Role toRole = tx.getRole(toRoleLabel);
loader.add(Graql.insert(Graql.var().asUserDefined().has(attributeLabel, "first").id(instances[0]).admin().varPatterns()), keyspace).subscribe();
for (int i = 1; i < instances.length; i++) {
Var fromRolePlayer = Graql.var();
Var toRolePlayer = Graql.var();
Pattern relationInsert = Graql.var().rel(Graql.label(fromRole.getLabel()), fromRolePlayer).rel(Graql.label(toRole.getLabel()), toRolePlayer).isa(Graql.label(baseRelation.getLabel())).and(fromRolePlayer.asUserDefined().id(instances[i - 1])).and(toRolePlayer.asUserDefined().id(instances[i]));
loader.add(Graql.insert(relationInsert.admin().varPatterns()), keyspace).subscribe();
Pattern resourceInsert = Graql.var().asUserDefined().has(attributeLabel, String.valueOf(i)).id(instances[i]);
loader.add(Graql.insert(resourceInsert.admin().varPatterns()), keyspace).subscribe();
}
}
}
}
use of ai.grakn.client.BatchExecutorClient in project grakn by graknlabs.
the class Migrator method load.
/**
* Migrate data constrained by this migrator using a loader configured
* by the provided parameters.
*
* @param template Template used to extract the data
* @param data Data being migrated
*/
public void load(String template, Stream<Map<String, Object>> data) {
GraknClient graknClient = GraknClient.of(uri);
AtomicInteger queriesExecuted = new AtomicInteger(0);
try (BatchExecutorClient loader = BatchExecutorClient.newBuilder().taskClient(graknClient).maxRetries(retries).maxDelay(maxDelayMs).metricRegistry(metricRegistry).build()) {
checkKeyspace(graknClient);
Stream<Query> queryStream = data.flatMap(d -> template(template, d, failFast));
if (maxLines > -1) {
queryStream = queryStream.limit(maxLines);
}
queryStream.forEach(q -> {
LOG.trace("Adding query {}", q);
totalMeter.mark();
// We add get a hot observable. It starts immediately
Observable<QueryResponse> observable = loader.add(q, keyspace, failFast);
subscribeToReportOutcome(failFast, observable, queriesExecuted);
});
}
System.out.println("Loaded " + queriesExecuted + " statements");
}
Aggregations