Skip to content

Commit

Permalink
Merge pull request #24 from hkuich/attributes_in_relation
Browse files Browse the repository at this point in the history
moved to core 2.0.1 and client 2.0.0
  • Loading branch information
hkuich authored Apr 8, 2021
2 parents 46fc679 + 8478839 commit 81b0e4e
Show file tree
Hide file tree
Showing 24 changed files with 548 additions and 190 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group 'com.github.bayer-science-for-a-better-life'
version '0.1.0-alpha-12'
version '0.1.0'

repositories {
mavenCentral()
Expand All @@ -15,7 +15,7 @@ repositories {
}

dependencies {
compile group: 'io.grakn.client', name: 'grakn-client', version: '2.0.0-alpha-12'
compile group: 'io.grakn.client', name: 'grakn-client', version: '2.0.0'
testCompile group: 'junit', name: 'junit', version: '4.12'
compile 'com.google.code.gson:gson:2.8.6'
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cli/GramiCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import java.io.IOException;

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

public static void main(String[] args) {
Expand Down
91 changes: 91 additions & 0 deletions src/main/java/generator/AttributeInsertGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package generator;

import configuration.DataConfigEntry;
import configuration.ProcessorConfigEntry;
import graql.lang.Graql;
import graql.lang.pattern.Pattern;
import graql.lang.pattern.variable.ThingVariable;
import graql.lang.pattern.variable.ThingVariable.Attribute;
import graql.lang.pattern.variable.UnboundVariable;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.Arrays;

import static generator.GeneratorUtil.addValue;
import static generator.GeneratorUtil.malformedRow;

public class AttributeInsertGenerator extends InsertGenerator {

private static final Logger appLogger = LogManager.getLogger("com.bayer.dt.grami");
private static final Logger dataLogger = LogManager.getLogger("com.bayer.dt.grami.data");
private final DataConfigEntry dce;
private final ProcessorConfigEntry pce;

public AttributeInsertGenerator(DataConfigEntry dataConfigEntry, ProcessorConfigEntry processorConfigEntry) {
super();
this.dce = dataConfigEntry;
this.pce = processorConfigEntry;
appLogger.debug("Creating AttributeInsertGenerator for processor " + processorConfigEntry.getProcessor() + " of type " + processorConfigEntry.getProcessorType());
}

public ArrayList<ThingVariable<?>> graknAttributeInsert(ArrayList<String> rows,
String header) throws IllegalArgumentException {
ArrayList<ThingVariable<?>> patterns = new ArrayList<>();
for (String row : rows) {
try {
ThingVariable<?> temp = graknAttributeQueryFromRow(row, header);
if (temp != null) {
patterns.add(temp);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return patterns;
}

public ThingVariable<Attribute> graknAttributeQueryFromRow(String row,
String header) throws Exception {
String fileSeparator = dce.getSeparator();
String[] rowTokens = row.split(fileSeparator);
String[] columnNames = header.split(fileSeparator);
appLogger.debug("processing tokenized row: " + Arrays.toString(rowTokens));
malformedRow(row, rowTokens, columnNames.length);

UnboundVariable attributeInitialStatement = addAttributeToStatement();
Attribute attributeInsertStatement = null;

for (DataConfigEntry.DataConfigGeneratorMapping generatorMappingForAttribute : dce.getAttributes()) {
attributeInsertStatement = addValue(rowTokens, attributeInitialStatement, columnNames, generatorMappingForAttribute, pce, generatorMappingForAttribute.getPreprocessor());
}

if (attributeInsertStatement != null) {
attributeInsertStatement = attributeInsertStatement.isa(pce.getSchemaType());

if (isValid(attributeInsertStatement)) {
appLogger.debug("valid query: <insert " + attributeInsertStatement.toString() + ";>");
return attributeInsertStatement;
} else {
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));
return null;
}
} else {
return null;
}
}

private UnboundVariable addAttributeToStatement() {
if (pce.getSchemaType() != null) {
return Graql.var("a");
} else {
throw new IllegalArgumentException("Required field <schemaType> not set in processor " + pce.getProcessor());
}
}

private boolean isValid(Pattern pa) {
String patternAsString = pa.toString();
return patternAsString.contains("isa " + pce.getSchemaType());
}
}
13 changes: 5 additions & 8 deletions src/main/java/generator/EntityInsertGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ public EntityInsertGenerator(DataConfigEntry dataConfigEntry, ProcessorConfigEnt
public ArrayList<ThingVariable<?>> graknEntityInsert(ArrayList<String> rows,
String header) throws IllegalArgumentException {
ArrayList<ThingVariable<?>> patterns = new ArrayList<>();
int insertCounter = 0;
for (String row : rows) {
try {
ThingVariable temp = graknEntityQueryFromRow(row, header, insertCounter);
ThingVariable temp = graknEntityQueryFromRow(row, header);
if (temp != null) {
patterns.add(temp);
}
insertCounter++;
} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -49,15 +47,14 @@ public ArrayList<ThingVariable<?>> graknEntityInsert(ArrayList<String> rows,
}

public ThingVariable graknEntityQueryFromRow(String row,
String header,
int insertCounter) throws Exception {
String header) throws Exception {
String fileSeparator = dce.getSeparator();
String[] rowTokens = row.split(fileSeparator);
String[] columnNames = header.split(fileSeparator);
appLogger.debug("processing tokenized row: " + Arrays.toString(rowTokens));
malformedRow(row, rowTokens, columnNames.length);

Thing entityInsertStatement = addEntityToStatement(insertCounter);
Thing entityInsertStatement = addEntityToStatement();

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

private Thing addEntityToStatement(int insertCounter) {
private Thing addEntityToStatement() {
if (pce.getSchemaType() != null) {
return Graql.var("e-" + insertCounter).isa(pce.getSchemaType());
return Graql.var("e").isa(pce.getSchemaType());
} else {
throw new IllegalArgumentException("Required field <schemaType> not set in processor " + pce.getProcessor());
}
Expand Down
Loading

0 comments on commit 81b0e4e

Please sign in to comment.