1
|
|
|
package br.ufrj.ppgi.greco.kettle; |
2
|
|
|
|
3
|
|
|
import org.pentaho.di.core.exception.KettleException; |
4
|
|
|
import org.pentaho.di.core.exception.KettleStepException; |
5
|
|
|
import org.pentaho.di.core.row.RowDataUtil; |
6
|
|
|
import org.pentaho.di.core.row.RowMetaInterface; |
7
|
|
|
import org.pentaho.di.trans.Trans; |
8
|
|
|
import org.pentaho.di.trans.TransMeta; |
9
|
|
|
import org.pentaho.di.trans.step.BaseStep; |
10
|
|
|
import org.pentaho.di.trans.step.StepDataInterface; |
11
|
|
|
import org.pentaho.di.trans.step.StepInterface; |
12
|
|
|
import org.pentaho.di.trans.step.StepMeta; |
13
|
|
|
import org.pentaho.di.trans.step.StepMetaInterface; |
14
|
|
|
|
15
|
|
|
import br.ufrj.ppgi.greco.kettle.plugin.tools.datatable.DataTable; |
16
|
|
|
|
17
|
|
|
public class TurtleGeneratorStep extends BaseStep implements StepInterface { |
18
|
|
|
|
19
|
|
|
int j = 0; |
20
|
|
|
|
21
|
|
|
public TurtleGeneratorStep(StepMeta stepMeta, StepDataInterface stepDataInterface, int copyNr, TransMeta transMeta, |
22
|
|
|
Trans trans) { |
23
|
|
|
super(stepMeta, stepDataInterface, copyNr, transMeta, trans); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
@Override |
27
|
|
|
public boolean init(StepMetaInterface smi, StepDataInterface sdi) { |
28
|
|
|
if (super.init(smi, sdi)) { |
29
|
|
|
return true; |
30
|
|
|
} else { |
31
|
|
|
return false; |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
@Override |
36
|
|
|
public void dispose(StepMetaInterface smi, StepDataInterface sdi) { |
37
|
|
|
super.dispose(smi, sdi); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Metodo chamado para cada linha que entra no step |
42
|
|
|
*/ |
43
|
|
|
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException { |
44
|
|
|
|
45
|
|
|
TurtleGeneratorStepMeta meta = (TurtleGeneratorStepMeta) smi; |
46
|
|
|
TurtleGeneratorStepData data = (TurtleGeneratorStepData) sdi; |
47
|
|
|
|
48
|
|
|
// Obtem linha do fluxo de entrada e termina caso nao haja mais entrada |
49
|
|
|
Object[] row = getRow(); |
50
|
|
|
if (row == null) { |
51
|
|
|
setOutputDone(); |
52
|
|
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (first) { |
56
|
|
|
first = false; |
57
|
|
|
|
58
|
|
|
RowMetaInterface rowMeta = getInputRowMeta(); |
59
|
|
|
data.outputRowMeta = rowMeta.clone(); |
60
|
|
|
|
61
|
|
|
// Adiciona os metadados do step atual |
62
|
|
|
meta.getFields(data.outputRowMeta, getStepname(), null, null, this); |
63
|
|
|
|
64
|
|
|
// Prefixos |
65
|
|
|
String prefix = meta.getPrefixes().toString(); |
66
|
|
|
prefix = prefix.replace("[", ""); |
67
|
|
|
prefix = prefix.replace(", ", " <"); |
68
|
|
|
prefix = prefix.replace("]", ">. "); |
69
|
|
|
prefix = prefix.replace("<@", "@"); |
70
|
|
|
prefix = prefix.replace(">. >.", ">."); |
71
|
|
|
prefix = prefix.replace(" <>. ", ""); |
72
|
|
|
prefix = prefix.replace(". ", "." + System.getProperty("line.separator")); |
73
|
|
|
|
74
|
|
|
putOutRow(row, meta, data, prefix); |
75
|
|
|
putOutRow(row, meta, data, ""); |
76
|
|
|
putOutRow(row, meta, data, "#"); |
77
|
|
|
putOutRow(row, meta, data, "# PROPERTIES DEFINITION"); |
78
|
|
|
putOutRow(row, meta, data, "#"); |
79
|
|
|
putOutRow(row, meta, data, ""); |
80
|
|
|
|
81
|
|
|
DataTable<String> table = meta.getMapTable(); |
82
|
|
|
for (int i = 0; i < table.size(); i++) { |
83
|
|
|
String dimensionField = table.getValue(i, |
84
|
|
|
TurtleGeneratorStepMeta.Field.MAP_TABLE_DIMENSIONS_FIELD_NAME.name()); |
85
|
|
|
String labelDimensao = table.getValue(i, TurtleGeneratorStepMeta.Field.MAP_TABLE_LABELS_FIELD_NAME.name()); |
86
|
|
|
String dimensionURIType = table.getValue(i, |
87
|
|
|
TurtleGeneratorStepMeta.Field.MAP_TABLE_URI_TYPE_FIELD_NAME.name()); |
88
|
|
|
if (dimensionField != null) { |
89
|
|
|
putOutRow(row, meta, data, "exProp:" + removeSignals(dimensionField).toLowerCase() + " owl:sameAs <" + dimensionURIType + ">;"); |
90
|
|
|
putOutRow(row, meta, data, " rdfs:label \"" + labelDimensao + "\"@en ."); |
91
|
|
|
putOutRow(row, meta, data, ""); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (TurtleGeneratorStepMeta.Field.MAP_TABLE_HIERARCHY_FIELD_NAME.name() != null) { |
96
|
|
|
|
97
|
|
|
putOutRow(row, meta, data, "#"); |
98
|
|
|
putOutRow(row, meta, data, "# HIERARCHIES"); |
99
|
|
|
putOutRow(row, meta, data, "#"); |
100
|
|
|
putOutRow(row, meta, data, ""); |
101
|
|
|
|
102
|
|
|
table = meta.getMapTable2(); |
103
|
|
|
for (int i = 0; i < table.size(); i++) { |
104
|
|
|
String hierarquia = table.getValue(i, TurtleGeneratorStepMeta.Field.MAP_TABLE_HIERARCHY_FIELD_NAME.name()); |
105
|
|
|
String hierarquiaDe = table.getValue(i, |
106
|
|
|
TurtleGeneratorStepMeta.Field.MAP_TABLE_HIERARCHY_DE_FIELD_NAME.name()); |
107
|
|
|
String hierarquiaLabel = table.getValue(i, |
108
|
|
|
TurtleGeneratorStepMeta.Field.MAP_TABLE_HIERARCHY_LABEL_FIELD_NAME.name()); |
109
|
|
|
String hierarquiaPara = table.getValue(i, |
110
|
|
|
TurtleGeneratorStepMeta.Field.MAP_TABLE_HIERARCHY_PARA_FIELD_NAME.name()); |
111
|
|
|
if (hierarquia != null && hierarquiaDe != null && hierarquiaPara != null) { |
112
|
|
|
putOutRow(row, meta, data, "ex:" + removeSignals(hierarquiaDe).toLowerCase() + " a ex:" |
113
|
|
|
+ removeSignals(hierarquia).toLowerCase() + " ;"); |
114
|
|
|
putOutRow(row, meta, data, " obo:part_of ex:" + removeSignals(hierarquiaPara) + ";"); |
115
|
|
|
putOutRow(row, meta, data, " rdfs:label \"" + hierarquiaLabel + "\"@en ."); |
116
|
|
|
putOutRow(row, meta, data, ""); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
DataTable<String> table = meta.getMapTable(); |
123
|
|
|
String line = ""; |
124
|
|
|
|
125
|
|
|
for (int i = 0; i < table.size(); i++) { |
126
|
|
|
String dimensionField = table.getValue(i, TurtleGeneratorStepMeta.Field.MAP_TABLE_DIMENSIONS_FIELD_NAME.name()); |
127
|
|
|
String dimension = getInputRowMeta().getString(row, dimensionField, ""); |
128
|
|
|
if (!dimension.startsWith("http")) { |
129
|
|
|
if (i == 0) { |
130
|
|
|
line = " exProp:" + removeSignals(dimensionField).toLowerCase() + " " + "\"" + dimension + "\";"; |
131
|
|
|
} else |
132
|
|
|
line = " exProp:" + removeSignals(dimensionField).toLowerCase() + " " + "\"" + dimension + "\";"; |
133
|
|
|
} else { |
134
|
|
|
line = " exProp:" + removeSignals(dimensionField).toLowerCase() + " " + "<" + dimension + ">;"; |
135
|
|
|
} |
136
|
|
|
if (i == 0) { |
137
|
|
|
line = "ex:obj" + j + line; |
138
|
|
|
} |
139
|
|
|
putOutRow(row, meta, data, line); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
putOutRow(row, meta, data, " rdfs:label \"" + meta.getunity() + "\" ."); |
143
|
|
|
putOutRow(row, meta, data, ""); |
144
|
|
|
|
145
|
|
|
j++; |
146
|
|
|
|
147
|
|
|
return true; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
private void putOutRow(Object[] inputRow, TurtleGeneratorStepMeta meta, TurtleGeneratorStepData data, String turtle) |
151
|
|
|
throws KettleStepException { |
152
|
|
|
|
153
|
|
|
Object[] outputRow = null; |
154
|
|
|
|
155
|
|
|
outputRow = new Object[0]; |
156
|
|
|
|
157
|
|
|
outputRow = RowDataUtil.addValueData(outputRow, outputRow.length, turtle); |
158
|
|
|
// Coloca linha no fluxo |
159
|
|
|
putRow(data.outputRowMeta, outputRow); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
private static String removeSignals(String value) { |
163
|
|
|
if (value != null) { |
164
|
|
|
return value.replaceAll("á", "a").replaceAll("à", "a").replaceAll("ä", "a").replaceAll("ã", "a") |
165
|
|
|
.replaceAll("ú", "u").replaceAll("ù", "u").replaceAll("é", "e").replaceAll("è", "e") |
166
|
|
|
.replaceAll("ó", "o").replaceAll("ò", "o").replaceAll("ú", "u").replaceAll("ç", "c") |
167
|
|
|
.replaceAll("í", "i").replaceAll("ì", "i").replaceAll(" ", "").trim(); |
168
|
|
|
} else { |
169
|
|
|
return ""; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
} |
174
|
|
|
|