br.ufrj.ppgi.greco.kettle.DataCubeStep   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 22
eloc 105
c 1
b 0
f 1
dl 0
loc 152
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A DataCubeStep(StepMeta,StepDataInterface,int,TransMeta,Trans) 0 3 1
A putOutRow(Object[],DataCubeStepMeta,DataCubeStepData,String) 0 7 3
A formatPrefix(String) 0 5 2
A formatUri(String) 0 2 1
D processRow(StepMetaInterface,StepDataInterface) 0 105 12
A init(StepMetaInterface,StepDataInterface) 0 6 2
A dispose(StepMetaInterface,StepDataInterface) 0 3 1
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 DataCubeStep extends BaseStep implements StepInterface {
18
19
	int i = 0;
20
	public static final String OBJ = "exProp:%s ex:%s;";
21
	
22
	public DataCubeStep(StepMeta stepMeta, StepDataInterface stepDataInterface, int copyNr, TransMeta transMeta,
23
			Trans trans) {
24
		super(stepMeta, stepDataInterface, copyNr, transMeta, trans);
25
	}
26
27
	@Override
28
	public boolean init(StepMetaInterface smi, StepDataInterface sdi) {
29
		if (super.init(smi, sdi)) {
30
			return true;
31
		} else
32
			return false;
33
	}
34
35
	@Override
36
	public void dispose(StepMetaInterface smi, StepDataInterface sdi) {
37
		super.dispose(smi, sdi);
38
	}
39
40
	private String formatUri(String uri) {
41
		return "<" + uri + ">";
42
	}
43
44
	private String formatPrefix(String prefix) {
45
		if (!prefix.equals("@base")){
46
			return "@prefix " + prefix + ":";
47
		}else {
48
			return prefix;
49
		}
50
	}
51
52
	/**
53
	 * Método chamado para cada linha que entra no step
54
	 */
55
	public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
56
57
		DataCubeStepMeta meta = (DataCubeStepMeta) smi;
58
		DataCubeStepData data = (DataCubeStepData) sdi;
59
				
60
		Object[] row = getRow();
61
		if (row == null) {
62
			setOutputDone();
63
			return false;
64
		}
65
		
66
		if (first) {
67
			first = false;
68
69
			RowMetaInterface rowMeta = getInputRowMeta();
70
			data.outputRowMeta = rowMeta.clone();
71
72
			// Adiciona os metadados do step atual
73
			meta.getFields(data.outputRowMeta, getStepname(), null, null, this);
74
75
			DataTable<String> vocabularyTable = meta.getVocabularyTable();
76
			for (int k = 0; k < vocabularyTable.size(); k++) {
77
				String prefix = vocabularyTable.getValue(k, DataCubeStepMeta.Field.VOCABULARY_TABLE_PREFIX.name());
78
				String uri = vocabularyTable.getValue(k, DataCubeStepMeta.Field.VOCABULARY_TABLE_URI.name());
79
				putOutRow(row, meta, data, this.formatPrefix(prefix) + " " + this.formatUri(uri) + " .");
80
			}
81
82
			putOutRow(row, meta, data, "", 
83
					"<http://purl.org/linked-data/cube> a owl:Ontology ;", 
84
					"rdfs:label \"Example DataCube Knowledge Base\" ;",
85
					"dc:description \"This knowledgebase contains one Data Structure Definition with one Data Set. This Data Set has a couple of Components and Observations.\" .",
86
					"", 
87
					"# Data Structure Definitions", 
88
					"", 
89
					"ex:dsd a cube:DataStructureDefinition ;",
90
					"    rdfs:label \"A Data Structure Definition\"@en ;",
91
					"    rdfs:comment \"Defines the structure of a DataSet or slice.\" ;");
92
93
			DataTable<String> dimensionTable = meta.getDimensionTable();
94
			for (int k = 0; k < dimensionTable.size(); k++) {
95
				String dimURI = dimensionTable.getValue(k, DataCubeStepMeta.Field.DIMENSION_TABLE_URI.name());
96
				if (k == 0) {
97
					putOutRow(row, meta, data, "    cube:component  " + this.formatUri(dimURI) + ",");
98
				} else if (k == dimensionTable.size() - 1) {
99
					putOutRow(row, meta, data, "      " + this.formatUri(dimURI) + " .");
100
				} else {
101
					putOutRow(row, meta, data, "      " + this.formatUri(dimURI) + ",");
102
				}
103
			}
104
105
			putOutRow(row, meta, data, "", "# Component Specifications", "");
106
107
			for (int k = 0; k < dimensionTable.size(); k++) {
108
				String label = dimensionTable.getValue(k, DataCubeStepMeta.Field.DIMENSION_TABLE_LABEL.name());
109
				String dimURI = dimensionTable.getValue(k, DataCubeStepMeta.Field.DIMENSION_TABLE_URI.name());
110
				String dimName = dimensionTable.getValue(k, DataCubeStepMeta.Field.DIMENSION_TABLE_NAME.name());
111
				putOutRow(row, meta, data, 
112
						this.formatUri(dimURI) + " a cube:ComponentSpecification ;",
113
						"    rdfs:label \"" + label + "\" ;", 
114
						"    cube: cube:dimension exProp:" + dimName + " .", "");
115
			}
116
117
			putOutRow(row, meta, data, "# Data Set",
118
					"rdfs:label \"A DataSet\"^^<http://www.w3.org/2001/XMLSchema#string> ;",
119
					"rdfs:comment \"Represents a collection of observations and conforming to some common dimensional structure.\" ;",
120
					"cube:structure ex:dsd .", "", "# Dimensions, Unit and Measure");
121
122
			for (int k = 0; k < dimensionTable.size(); k++) {
123
				String type = dimensionTable.getValue(k, DataCubeStepMeta.Field.DIMENSION_TABLE_TYPE.name());
124
				String label = dimensionTable.getValue(k, DataCubeStepMeta.Field.DIMENSION_TABLE_LABEL.name());
125
				String dimName = dimensionTable.getValue(k, DataCubeStepMeta.Field.DIMENSION_TABLE_NAME.name());
126
				if (type.trim().equals("")) { // it's not a measure
127
					putOutRow(row, meta, data, 
128
							"exProp:" + dimName + " a cube:DimensionProperty ;",
129
							"    rdfs:label \"" + label + "\"@en .", "");
130
				} else {
131
					putOutRow(row, meta, data, "exProp:unit a cube:AttributeProperty ;",
132
							"    exProp:" + dimName + " a cube:MeasureProperty ;", 
133
							"    rdfs:label \"" + label + "\"@en .", "");
134
				}
135
			}
136
137
		}
138
139
		putOutRow(row, meta, data, "ex:ob" + i + " a cube:Observation;", 
140
				                   "    cube:dataSet ex:dataset;");
141
142
		DataTable<String> dimensionTable = meta.getDimensionTable();
143
		for (int k = 0; k < dimensionTable.size(); k++) {
144
			String dimName = dimensionTable.getValue(k, DataCubeStepMeta.Field.DIMENSION_TABLE_NAME.name());
145
			String type = dimensionTable.getValue(k, DataCubeStepMeta.Field.DIMENSION_TABLE_TYPE.name());
146
			String label = dimensionTable.getValue(k, DataCubeStepMeta.Field.DIMENSION_TABLE_LABEL.name());
147
			if (type.trim().equals("")) {
148
				putOutRow(row, meta, data, "    " + String.format(OBJ, dimName, getInputRowMeta().getString(row, dimName, "")));
149
			} else {
150
				putOutRow(row, meta, data, 
151
						"    exProp:unit \"" + label + "\";",
152
						"    exProp:value \"" + getInputRowMeta().getString(row, dimName, "") + "\"^^" + type + ";");
153
			}
154
		}
155
156
		putOutRow(row, meta, data, "    rdfs:label \"\".", "");
157
158
		i++;
159
		return true;
160
	}
161
162
	private void putOutRow(Object[] inputRow, DataCubeStepMeta meta, DataCubeStepData data, String... lines)
163
			throws KettleStepException {
164
		for (String arg : lines) {
165
			Object[] outputRow = null;
166
			outputRow = meta.isKeepInputFields() ? inputRow : new Object[0];
167
			outputRow = RowDataUtil.addValueData(outputRow, outputRow.length, arg);
168
			putRow(data.outputRowMeta, outputRow);
169
		}
170
	}
171
172
}