use of ai.grakn.GraknTx in project grakn by graknlabs.
the class EngineContext method clearGraphs.
private static void clearGraphs(EngineGraknTxFactory factory) {
// Drop all keyspaces
final Set<String> keyspaceNames = new HashSet<String>();
try (GraknTx systemGraph = factory.tx(GraknKeyspaceStore.SYSTEM_KB_KEYSPACE, GraknTxType.WRITE)) {
systemGraph.graql().match(var("x").isa("keyspace-name")).forEach(x -> x.concepts().forEach(y -> {
keyspaceNames.add(y.asAttribute().getValue().toString());
}));
}
keyspaceNames.forEach(name -> {
GraknTx graph = factory.tx(Keyspace.of(name), GraknTxType.WRITE);
graph.admin().delete();
});
factory.refreshConnections();
}
use of ai.grakn.GraknTx in project grakn by graknlabs.
the class MigratorTestUtils method load.
public static void load(GraknSession factory, File schema) {
try (GraknTx graph = factory.open(GraknTxType.WRITE)) {
graph.graql().parse(Files.readLines(schema, StandardCharsets.UTF_8).stream().collect(joining("\n"))).execute();
graph.commit();
} catch (IOException | InvalidKBException e) {
throw new RuntimeException(e);
}
}
use of ai.grakn.GraknTx in project grakn by graknlabs.
the class CSVMigratorTest method whenLinesIsSetToN_OnlyFirstNLinesAreProcessed.
@Test
public void whenLinesIsSetToN_OnlyFirstNLinesAreProcessed() {
load(factory, getFile("csv", "pets/schema.gql"));
String template = getFileAsString("csv", "pets/template.gql");
declareAndLoad(template, "pets/data/pets.quotes", new MigratorBuilder().setUri(engine.uri()).setKeyspace(keyspace).setRetries(0).setFailFast(false).setLines(3).build());
try (GraknTx graph = factory.open(GraknTxType.WRITE)) {
// Re Open Transaction
Collection<Entity> pets = graph.getEntityType("pet").instances().collect(Collectors.toSet());
TestCase.assertEquals(3, pets.size());
Collection<Entity> cats = graph.getEntityType("cat").instances().collect(Collectors.toSet());
TestCase.assertEquals(2, cats.size());
Collection<Entity> dogs = graph.getEntityType("dog").instances().collect(Collectors.toSet());
TestCase.assertEquals(0, dogs.size());
}
}
use of ai.grakn.GraknTx in project grakn by graknlabs.
the class CSVMigratorTest method whenParsedLineIsEmpty_ErrorIsNotThrownAndThoseLinesAreSkipped.
@Test
public void whenParsedLineIsEmpty_ErrorIsNotThrownAndThoseLinesAreSkipped() {
load(factory, getFile("csv", "pets/schema.gql"));
// Only dont insert Puffball
String template = "if (<name> != \"Puffball\") do { insert $x isa cat; }";
declareAndLoad(template, "pets/data/pets.quotes", defaultMigrator);
// Re Open Transaction
GraknTx graph = factory.open(GraknTxType.WRITE);
assertEquals(8, graph.getEntityType("pet").instances().count());
}
use of ai.grakn.GraknTx in project grakn by graknlabs.
the class JsonMigratorMainTest method runAndAssertDataCorrect.
private void runAndAssertDataCorrect(String... args) {
run(args);
try (GraknTx graph = session.open(GraknTxType.READ)) {
EntityType personType = graph.getEntityType("person");
assertEquals(1, personType.instances().count());
Entity person = personType.instances().iterator().next();
Entity address = getProperty(graph, person, "has-address").asEntity();
Entity streetAddress = getProperty(graph, address, "address-has-street").asEntity();
Attribute number = getResource(graph, streetAddress, Label.of("number"));
assertEquals(21L, number.getValue());
Collection<Thing> phoneNumbers = getProperties(graph, person, "has-phone");
assertEquals(2, phoneNumbers.size());
}
}
Aggregations