Skip to content

Commit bdf52db

Browse files
authored
Merge pull request #18 from hkuich/0.1.0
0.1.0
2 parents b3d6d27 + e5bb062 commit bdf52db

24 files changed

+1043
-1447
lines changed

README.md

Lines changed: 43 additions & 709 deletions
Large diffs are not rendered by default.

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
}
66

77
group 'com.github.bayer-science-for-a-better-life'
8-
version '0.0.3'
8+
version '0.1.0-alpha-9'
99

1010
repositories {
1111
mavenCentral()
@@ -15,7 +15,7 @@ repositories {
1515
}
1616

1717
dependencies {
18-
compile group: 'io.grakn.client', name: 'grakn-client', version: '1.8.3'
18+
compile group: 'io.grakn.client', name: 'grakn-client', version: '2.0.0-alpha-9'
1919
testCompile group: 'junit', name: 'junit', version: '4.12'
2020
compile 'com.google.code.gson:gson:2.8.6'
2121
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
@@ -25,7 +25,7 @@ dependencies {
2525
compile 'info.picocli:picocli:4.5.1'
2626
}
2727

28-
mainClassName = 'cli.Cli'
28+
mainClassName = 'cli.GramiCLI'
2929

3030
publishing {
3131
publications {

src/main/java/cli/GramiCLI.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import java.io.IOException;
1010

11-
@CommandLine.Command(description="Welcome to the CLI of GraMi - your grakn data migration tool", name = "grami", version = "0.0.3", mixinStandardHelpOptions = true)
11+
@CommandLine.Command(description="Welcome to the CLI of GraMi - your grakn data migration tool", name = "grami", version = "0.1.0-alpha-9", mixinStandardHelpOptions = true)
1212
public class GramiCLI {
1313

1414
public static void main(String[] args) {
@@ -25,22 +25,22 @@ public static void main(String[] args) {
2525
class MigrateCommand implements Runnable {
2626
@CommandLine.Spec CommandLine.Model.CommandSpec spec;
2727

28-
@CommandLine.Option(names = {"-d", "--dataConfigFile"}, description = "data config file in JSON format", required = true)
28+
@CommandLine.Option(names = {"-dc", "--dataConfigFile"}, description = "data config file in JSON format", required = true)
2929
private String dataConfigFilePath;
3030

31-
@CommandLine.Option(names = {"-p", "--processorConfigFile"}, description = "processor config file in JSON format", required = true)
31+
@CommandLine.Option(names = {"-pc", "--processorConfigFile"}, description = "processor config file in JSON format", required = true)
3232
private String processorConfigFilePath;
3333

34-
@CommandLine.Option(names = {"-m", "--migrationStatusFile"}, description = "file to track migration status in", required = true)
34+
@CommandLine.Option(names = {"-ms", "--migrationStatusFile"}, description = "file to track migration status in", required = true)
3535
private String migrationStatusFilePath;
3636

3737
@CommandLine.Option(names = {"-s", "--schemaFile"}, description = "your schema file as .gql", required = true)
3838
private String schemaFilePath;
3939

40-
@CommandLine.Option(names = {"-k", "--keyspace"}, description = "target keyspace in your grakn instance", required = true)
41-
private String keyspaceName;
40+
@CommandLine.Option(names = {"-db", "--database"}, description = "target database in your grakn instance", required = true)
41+
private String databaseName;
4242

43-
@CommandLine.Option(names = {"-g", "--grakn"}, description = "optional - grakn DB in format: server:port (default: localhost:48555)", defaultValue = "localhost:48555")
43+
@CommandLine.Option(names = {"-g", "--grakn"}, description = "optional - grakn DB in format: server:port (default: localhost:1729)", defaultValue = "localhost:1729")
4444
private String graknURI;
4545

4646
@CommandLine.Option(names = {"-cm", "--cleanMigration"}, description = "optional - delete old schema and data and restart migration from scratch - default: continue previous migration, if exists")
@@ -57,12 +57,12 @@ public void run() {
5757
spec.commandLine().getOut().println("\tprocessor configuration: " + processorConfigFilePath);
5858
spec.commandLine().getOut().println("\ttracking migration status in: " + migrationStatusFilePath);
5959
spec.commandLine().getOut().println("\tschema: " + schemaFilePath);
60-
spec.commandLine().getOut().println("\tkeyspace: " + keyspaceName);
60+
spec.commandLine().getOut().println("\tdatabase: " + databaseName);
6161
spec.commandLine().getOut().println("\tgrakn server: " + graknURI);
62-
spec.commandLine().getOut().println("\tdelete keyspace and all data in it for a clean new migration?: " + cleanMigration);
62+
spec.commandLine().getOut().println("\tdelete database and all data in it for a clean new migration?: " + cleanMigration);
6363
spec.commandLine().getOut().println("\tmigration scope: " + scope);
6464

65-
final MigrationConfig migrationConfig = new MigrationConfig(graknURI, keyspaceName, schemaFilePath, dataConfigFilePath, processorConfigFilePath);
65+
final MigrationConfig migrationConfig = new MigrationConfig(graknURI, databaseName, schemaFilePath, dataConfigFilePath, processorConfigFilePath);
6666

6767
try {
6868
GraknMigrator mig = new GraknMigrator(migrationConfig, migrationStatusFilePath, cleanMigration);
@@ -91,21 +91,21 @@ class SchemaUpdateCommand implements Runnable {
9191
@CommandLine.Option(names = {"-s", "--schemaFile"}, description = "your schema file as .gql", required = true)
9292
private String schemaFilePath;
9393

94-
@CommandLine.Option(names = {"-k", "--keyspace"}, description = "target keyspace in your grakn instance", required = true)
95-
private String keyspaceName;
94+
@CommandLine.Option(names = {"-db", "--database"}, description = "target database in your grakn instance", required = true)
95+
private String databaseName;
9696

97-
@CommandLine.Option(names = {"-g", "--grakn"}, description = "optional - grakn DB in format: server:port (default: localhost:48555)", defaultValue = "localhost:48555")
97+
@CommandLine.Option(names = {"-g", "--grakn"}, description = "optional - grakn DB in format: server:port (default: localhost:1729)", defaultValue = "localhost:1729")
9898
private String graknURI;
9999

100100
@Override
101101
public void run() {
102102
spec.commandLine().getOut().println("############## GraMi schema-update ###############");
103103
spec.commandLine().getOut().println("schema-update started with parameters:");
104104
spec.commandLine().getOut().println("\tschema: " + schemaFilePath);
105-
spec.commandLine().getOut().println("\tkeyspace: " + keyspaceName);
105+
spec.commandLine().getOut().println("\tkeyspace: " + databaseName);
106106
spec.commandLine().getOut().println("\tgrakn server: " + graknURI);
107107

108-
SchemaUpdateConfig suConfig = new SchemaUpdateConfig(graknURI, keyspaceName, schemaFilePath);
108+
SchemaUpdateConfig suConfig = new SchemaUpdateConfig(graknURI, databaseName, schemaFilePath);
109109
SchemaUpdater su = new SchemaUpdater(suConfig);
110110
su.updateSchema();
111111
}

src/main/java/generator/AppendAttributeGenerator.java

Lines changed: 65 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
import configuration.DataConfigEntry;
44
import configuration.ProcessorConfigEntry;
55
import graql.lang.Graql;
6-
import graql.lang.statement.Statement;
7-
import graql.lang.statement.StatementInstance;
6+
import graql.lang.pattern.Pattern;
7+
import graql.lang.pattern.variable.ThingVariable;
8+
import graql.lang.pattern.variable.ThingVariable.Thing;
9+
import graql.lang.pattern.variable.UnboundVariable;
810
import org.apache.logging.log4j.LogManager;
911
import org.apache.logging.log4j.Logger;
1012

1113
import java.util.ArrayList;
1214
import java.util.Arrays;
15+
import java.util.HashMap;
1316
import java.util.Map;
1417

1518
import static generator.GeneratorUtil.*;
@@ -21,38 +24,42 @@ public class AppendAttributeGenerator extends InsertGenerator {
2124
private static final Logger appLogger = LogManager.getLogger("com.bayer.dt.grami");
2225
private static final Logger dataLogger = LogManager.getLogger("com.bayer.dt.grami.data");
2326

24-
public AppendAttributeGenerator(DataConfigEntry dataConfigEntry, ProcessorConfigEntry processorConfigEntry) {
27+
public AppendAttributeGenerator(DataConfigEntry dataConfigEntry,
28+
ProcessorConfigEntry processorConfigEntry) {
2529
super();
2630
this.dce = dataConfigEntry;
2731
this.pce = processorConfigEntry;
2832
appLogger.debug("Creating AppendAttribute for processor " + processorConfigEntry.getProcessor() + " of type " + processorConfigEntry.getProcessorType());
2933
}
3034

31-
public ArrayList<ArrayList<ArrayList<Statement>>> graknAppendAttributeInsert(ArrayList<String> rows, String header) throws Exception {
32-
ArrayList<ArrayList<ArrayList<Statement>>> matchInsertStatements = new ArrayList<>();
35+
public HashMap<String, ArrayList<ArrayList<ThingVariable<?>>>> graknAppendAttributeInsert(ArrayList<String> rows,
36+
String header) throws Exception {
37+
HashMap<String, ArrayList<ArrayList<ThingVariable<?>>>> matchInsertPatterns = new HashMap<>();
3338

34-
ArrayList<ArrayList<Statement>> matchStatements = new ArrayList<>();
35-
ArrayList<ArrayList<Statement>> insertStatements = new ArrayList<>();
39+
ArrayList<ArrayList<ThingVariable<?>>> matchPatterns = new ArrayList<>();
40+
ArrayList<ArrayList<ThingVariable<?>>> insertPatterns = new ArrayList<>();
3641

3742
int insertCounter = 0;
3843

3944
for (String row : rows) {
40-
ArrayList<ArrayList<Statement>> tmp = graknAppendAttributeQueryFromRow(row, header, insertCounter);
45+
ArrayList<ArrayList<ThingVariable<?>>> tmp = graknAppendAttributeQueryFromRow(row, header, insertCounter);
4146
if (tmp != null) {
4247
if (tmp.get(0) != null && tmp.get(1) != null) {
43-
matchStatements.add(tmp.get(0));
44-
insertStatements.add(tmp.get(1));
48+
matchPatterns.add(tmp.get(0));
49+
insertPatterns.add(tmp.get(1));
4550
insertCounter++;
4651
}
4752
}
4853

4954
}
50-
matchInsertStatements.add(matchStatements);
51-
matchInsertStatements.add(insertStatements);
52-
return matchInsertStatements;
55+
matchInsertPatterns.put("match", matchPatterns);
56+
matchInsertPatterns.put("insert", insertPatterns);
57+
return matchInsertPatterns;
5358
}
5459

55-
public ArrayList<ArrayList<Statement>> graknAppendAttributeQueryFromRow(String row, String header, int insertCounter) throws Exception {
60+
public ArrayList<ArrayList<ThingVariable<?>>> graknAppendAttributeQueryFromRow(String row,
61+
String header,
62+
int insertCounter) throws Exception {
5663
String fileSeparator = dce.getSeparator();
5764
String[] rowTokens = row.split(fileSeparator);
5865
String[] columnNames = header.split(fileSeparator);
@@ -63,35 +70,38 @@ public ArrayList<ArrayList<Statement>> graknAppendAttributeQueryFromRow(String r
6370
throw new IllegalArgumentException("data config entry for " + dce.getDataPath() + " is incomplete - it needs at least one attribute used for matching (\"match\": true) and at least one attribute to be appended (\"match\": false or not set at all");
6471
}
6572

66-
ArrayList<Statement> matchStatements = new ArrayList<>();
67-
ArrayList<Statement> insertStatements = new ArrayList<>();
73+
ArrayList<ThingVariable<?>> matchPatterns = new ArrayList<>();
74+
ArrayList<ThingVariable<?>> insertPatterns = new ArrayList<>();
6875

6976
// get all attributes that are isMatch() --> construct match clause
70-
StatementInstance appendAttributeMatchStatement = addEntityToMatchStatement(insertCounter);
77+
Thing appendAttributeMatchPattern = addEntityToMatchPattern(insertCounter);
7178
for (DataConfigEntry.DataConfigGeneratorMapping generatorMappingForMatchAttribute : dce.getAttributes()) {
72-
if (generatorMappingForMatchAttribute.isMatch()){
73-
appendAttributeMatchStatement = addAttribute(rowTokens, appendAttributeMatchStatement, columnNames, generatorMappingForMatchAttribute, pce, generatorMappingForMatchAttribute.getPreprocessor());
79+
if (generatorMappingForMatchAttribute.isMatch()) {
80+
appendAttributeMatchPattern = addAttribute(rowTokens, appendAttributeMatchPattern, columnNames, generatorMappingForMatchAttribute, pce, generatorMappingForMatchAttribute.getPreprocessor());
7481
}
7582
}
76-
matchStatements.add(appendAttributeMatchStatement);
83+
matchPatterns.add(appendAttributeMatchPattern);
7784

7885
// get all attributes that are !isMatch() --> construct insert clause
79-
Statement appendAttributeInsertStatement = addEntityToInsertStatement(insertCounter);
86+
UnboundVariable thingVar = addEntityToInsertPattern(insertCounter);
87+
Thing appendAttributeInsertPattern = null;
8088
for (DataConfigEntry.DataConfigGeneratorMapping generatorMappingForAppendAttribute : dce.getAttributes()) {
81-
if (!generatorMappingForAppendAttribute.isMatch()){
82-
appendAttributeInsertStatement = addAttribute(rowTokens, appendAttributeMatchStatement, columnNames, generatorMappingForAppendAttribute, pce, generatorMappingForAppendAttribute.getPreprocessor());
89+
if (!generatorMappingForAppendAttribute.isMatch()) {
90+
appendAttributeInsertPattern = addAttribute(rowTokens, thingVar, columnNames, generatorMappingForAppendAttribute, pce, generatorMappingForAppendAttribute.getPreprocessor());
8391
}
8492
}
85-
insertStatements.add((StatementInstance) appendAttributeInsertStatement);
93+
if (appendAttributeInsertPattern != null) {
94+
insertPatterns.add(appendAttributeInsertPattern);
95+
}
8696

87-
ArrayList<ArrayList<Statement>> assembledStatements = new ArrayList<>();
88-
assembledStatements.add(matchStatements);
89-
assembledStatements.add(insertStatements);
97+
ArrayList<ArrayList<ThingVariable<?>>> assembledPatterns = new ArrayList<>();
98+
assembledPatterns.add(matchPatterns);
99+
assembledPatterns.add(insertPatterns);
90100

91101

92-
if (isValid(assembledStatements)) {
93-
appLogger.debug("valid query: <" + assembleQuery(assembledStatements).toString() + ">");
94-
return assembledStatements;
102+
if (isValid(assembledPatterns)) {
103+
appLogger.debug("valid query: <" + assembleQuery(assembledPatterns).toString() + ">");
104+
return assembledPatterns;
95105
} else {
96106
dataLogger.warn("in datapath <" + dce.getDataPath() + ">: skipped row b/c does not contain at least one match attribute and one insert attribute. Faulty tokenized row: " + Arrays.toString(rowTokens));
97107
return null;
@@ -102,7 +112,7 @@ private boolean validateDataConfigEntry() {
102112
boolean containsMatchAttribute = false;
103113
boolean containsAppendAttribute = false;
104114
for (DataConfigEntry.DataConfigGeneratorMapping attributeMapping : dce.getAttributes()) {
105-
if (attributeMapping.isMatch()){
115+
if (attributeMapping.isMatch()) {
106116
containsMatchAttribute = true;
107117
}
108118
if (!attributeMapping.isMatch()) {
@@ -112,51 +122,58 @@ private boolean validateDataConfigEntry() {
112122
return containsMatchAttribute && containsAppendAttribute;
113123
}
114124

115-
private StatementInstance addEntityToMatchStatement(int insertCounter) {
125+
private Thing addEntityToMatchPattern(int insertCounter) {
116126
if (pce.getSchemaType() != null) {
117-
return Graql.var("e-" + insertCounter).isa(pce.getSchemaType());
127+
// return Graql.var("e-" + insertCounter).isa(pce.getSchemaType());
128+
return Graql.var("e").isa(pce.getSchemaType());
118129
} else {
119130
throw new IllegalArgumentException("Required field <schemaType> not set in processor " + pce.getProcessor());
120131
}
121132
}
122133

123-
private Statement addEntityToInsertStatement(int insertCounter) {
134+
private UnboundVariable addEntityToInsertPattern(int insertCounter) {
124135
if (pce.getSchemaType() != null) {
125-
return Graql.var("e-" + insertCounter);
136+
// return Graql.var("e-" + insertCounter);
137+
return Graql.var("e");
126138
} else {
127139
throw new IllegalArgumentException("Required field <schemaType> not set in processor " + pce.getProcessor());
128140
}
129141
}
130142

131-
private String assembleQuery(ArrayList<ArrayList<Statement>> queries) {
143+
private String assembleQuery(ArrayList<ArrayList<ThingVariable<?>>> queries) {
132144
StringBuilder ret = new StringBuilder();
133-
for (Statement st : queries.get(0)) {
145+
for (ThingVariable st : queries.get(0)) {
134146
ret.append(st.toString());
135147
}
136148
ret.append(queries.get(1).get(0).toString());
137149
return ret.toString();
138150
}
139151

140-
private boolean isValid(ArrayList<ArrayList<Statement>> si) {
141-
ArrayList<Statement> matchStatements = si.get(0);
142-
ArrayList<Statement> insertStatements = si.get(1);
143-
StringBuilder matchStatement = new StringBuilder();
144-
for (Statement st:matchStatements) {
145-
matchStatement.append(st.toString());
152+
private boolean isValid(ArrayList<ArrayList<ThingVariable<?>>> si) {
153+
ArrayList<ThingVariable<?>> matchPatterns = si.get(0);
154+
ArrayList<ThingVariable<?>> insertPatterns = si.get(1);
155+
156+
if (insertPatterns.size() < 1) {
157+
return false;
158+
}
159+
160+
StringBuilder matchPattern = new StringBuilder();
161+
for (Pattern st : matchPatterns) {
162+
matchPattern.append(st.toString());
146163
}
147-
String insertStatement = insertStatements.get(0).toString();
164+
String insertPattern = insertPatterns.get(0).toString();
148165

149166
// missing match attribute
150-
for (DataConfigEntry.DataConfigGeneratorMapping attributeMapping: dce.getMatchAttributes()) {
167+
for (DataConfigEntry.DataConfigGeneratorMapping attributeMapping : dce.getMatchAttributes()) {
151168
String generatorKey = attributeMapping.getGenerator();
152169
ProcessorConfigEntry.ConceptGenerator generatorEntry = pce.getAttributeGenerator(generatorKey);
153-
if (!matchStatement.toString().contains("has " + generatorEntry.getAttributeType())) {
170+
if (!matchPattern.toString().contains("has " + generatorEntry.getAttributeType())) {
154171
return false;
155172
}
156173
}
157174
// missing required insert attribute
158-
for (Map.Entry<String, ProcessorConfigEntry.ConceptGenerator> generatorEntry: pce.getRequiredAttributes().entrySet()) {
159-
if (!insertStatement.contains("has " + generatorEntry.getValue().getAttributeType())) {
175+
for (Map.Entry<String, ProcessorConfigEntry.ConceptGenerator> generatorEntry : pce.getRequiredAttributes().entrySet()) {
176+
if (!insertPattern.contains("has " + generatorEntry.getValue().getAttributeType())) {
160177
return false;
161178
}
162179
}

0 commit comments

Comments
 (0)