|
| 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 | +} |
0 commit comments