Skip to content

Commit 81b0e4e

Browse files
authored
Merge pull request #24 from hkuich/attributes_in_relation
moved to core 2.0.1 and client 2.0.0
2 parents 46fc679 + 8478839 commit 81b0e4e

24 files changed

+548
-190
lines changed

build.gradle

Lines changed: 2 additions & 2 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.1.0-alpha-12'
8+
version '0.1.0'
99

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

1717
dependencies {
18-
compile group: 'io.grakn.client', name: 'grakn-client', version: '2.0.0-alpha-12'
18+
compile group: 'io.grakn.client', name: 'grakn-client', version: '2.0.0'
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'

src/main/java/cli/GramiCLI.java

Lines changed: 1 addition & 1 deletion
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.1.0-alpha-9", mixinStandardHelpOptions = true)
11+
@CommandLine.Command(description="Welcome to the CLI of GraMi - your grakn data migration tool", name = "grami", version = "0.1.0-alpha-12", mixinStandardHelpOptions = true)
1212
public class GramiCLI {
1313

1414
public static void main(String[] args) {
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package generator;
2+
3+
import configuration.DataConfigEntry;
4+
import configuration.ProcessorConfigEntry;
5+
import graql.lang.Graql;
6+
import graql.lang.pattern.Pattern;
7+
import graql.lang.pattern.variable.ThingVariable;
8+
import graql.lang.pattern.variable.ThingVariable.Attribute;
9+
import graql.lang.pattern.variable.UnboundVariable;
10+
import org.apache.logging.log4j.LogManager;
11+
import org.apache.logging.log4j.Logger;
12+
13+
import java.util.ArrayList;
14+
import java.util.Arrays;
15+
16+
import static generator.GeneratorUtil.addValue;
17+
import static generator.GeneratorUtil.malformedRow;
18+
19+
public class AttributeInsertGenerator extends InsertGenerator {
20+
21+
private static final Logger appLogger = LogManager.getLogger("com.bayer.dt.grami");
22+
private static final Logger dataLogger = LogManager.getLogger("com.bayer.dt.grami.data");
23+
private final DataConfigEntry dce;
24+
private final ProcessorConfigEntry pce;
25+
26+
public AttributeInsertGenerator(DataConfigEntry dataConfigEntry, ProcessorConfigEntry processorConfigEntry) {
27+
super();
28+
this.dce = dataConfigEntry;
29+
this.pce = processorConfigEntry;
30+
appLogger.debug("Creating AttributeInsertGenerator for processor " + processorConfigEntry.getProcessor() + " of type " + processorConfigEntry.getProcessorType());
31+
}
32+
33+
public ArrayList<ThingVariable<?>> graknAttributeInsert(ArrayList<String> rows,
34+
String header) throws IllegalArgumentException {
35+
ArrayList<ThingVariable<?>> patterns = new ArrayList<>();
36+
for (String row : rows) {
37+
try {
38+
ThingVariable<?> temp = graknAttributeQueryFromRow(row, header);
39+
if (temp != null) {
40+
patterns.add(temp);
41+
}
42+
} catch (Exception e) {
43+
e.printStackTrace();
44+
}
45+
}
46+
return patterns;
47+
}
48+
49+
public ThingVariable<Attribute> graknAttributeQueryFromRow(String row,
50+
String header) throws Exception {
51+
String fileSeparator = dce.getSeparator();
52+
String[] rowTokens = row.split(fileSeparator);
53+
String[] columnNames = header.split(fileSeparator);
54+
appLogger.debug("processing tokenized row: " + Arrays.toString(rowTokens));
55+
malformedRow(row, rowTokens, columnNames.length);
56+
57+
UnboundVariable attributeInitialStatement = addAttributeToStatement();
58+
Attribute attributeInsertStatement = null;
59+
60+
for (DataConfigEntry.DataConfigGeneratorMapping generatorMappingForAttribute : dce.getAttributes()) {
61+
attributeInsertStatement = addValue(rowTokens, attributeInitialStatement, columnNames, generatorMappingForAttribute, pce, generatorMappingForAttribute.getPreprocessor());
62+
}
63+
64+
if (attributeInsertStatement != null) {
65+
attributeInsertStatement = attributeInsertStatement.isa(pce.getSchemaType());
66+
67+
if (isValid(attributeInsertStatement)) {
68+
appLogger.debug("valid query: <insert " + attributeInsertStatement.toString() + ";>");
69+
return attributeInsertStatement;
70+
} else {
71+
dataLogger.warn("in datapath <" + dce.getDataPath() + ">: skipped row b/c does not have a proper <isa> statement or is missing required attributes. Faulty tokenized row: " + Arrays.toString(rowTokens));
72+
return null;
73+
}
74+
} else {
75+
return null;
76+
}
77+
}
78+
79+
private UnboundVariable addAttributeToStatement() {
80+
if (pce.getSchemaType() != null) {
81+
return Graql.var("a");
82+
} else {
83+
throw new IllegalArgumentException("Required field <schemaType> not set in processor " + pce.getProcessor());
84+
}
85+
}
86+
87+
private boolean isValid(Pattern pa) {
88+
String patternAsString = pa.toString();
89+
return patternAsString.contains("isa " + pce.getSchemaType());
90+
}
91+
}

src/main/java/generator/EntityInsertGenerator.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,12 @@ public EntityInsertGenerator(DataConfigEntry dataConfigEntry, ProcessorConfigEnt
3333
public ArrayList<ThingVariable<?>> graknEntityInsert(ArrayList<String> rows,
3434
String header) throws IllegalArgumentException {
3535
ArrayList<ThingVariable<?>> patterns = new ArrayList<>();
36-
int insertCounter = 0;
3736
for (String row : rows) {
3837
try {
39-
ThingVariable temp = graknEntityQueryFromRow(row, header, insertCounter);
38+
ThingVariable temp = graknEntityQueryFromRow(row, header);
4039
if (temp != null) {
4140
patterns.add(temp);
4241
}
43-
insertCounter++;
4442
} catch (Exception e) {
4543
e.printStackTrace();
4644
}
@@ -49,15 +47,14 @@ public ArrayList<ThingVariable<?>> graknEntityInsert(ArrayList<String> rows,
4947
}
5048

5149
public ThingVariable graknEntityQueryFromRow(String row,
52-
String header,
53-
int insertCounter) throws Exception {
50+
String header) throws Exception {
5451
String fileSeparator = dce.getSeparator();
5552
String[] rowTokens = row.split(fileSeparator);
5653
String[] columnNames = header.split(fileSeparator);
5754
appLogger.debug("processing tokenized row: " + Arrays.toString(rowTokens));
5855
malformedRow(row, rowTokens, columnNames.length);
5956

60-
Thing entityInsertStatement = addEntityToStatement(insertCounter);
57+
Thing entityInsertStatement = addEntityToStatement();
6158

6259
for (DataConfigEntry.DataConfigGeneratorMapping generatorMappingForAttribute : dce.getAttributes()) {
6360
entityInsertStatement = addAttribute(rowTokens, entityInsertStatement, columnNames, generatorMappingForAttribute, pce, generatorMappingForAttribute.getPreprocessor());
@@ -72,9 +69,9 @@ public ThingVariable graknEntityQueryFromRow(String row,
7269
}
7370
}
7471

75-
private Thing addEntityToStatement(int insertCounter) {
72+
private Thing addEntityToStatement() {
7673
if (pce.getSchemaType() != null) {
77-
return Graql.var("e-" + insertCounter).isa(pce.getSchemaType());
74+
return Graql.var("e").isa(pce.getSchemaType());
7875
} else {
7976
throw new IllegalArgumentException("Required field <schemaType> not set in processor " + pce.getProcessor());
8077
}

0 commit comments

Comments
 (0)