-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from hkuich/attributes_in_relation
moved to core 2.0.1 and client 2.0.0
- Loading branch information
Showing
24 changed files
with
548 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.