OwlInputStep(StepMeta,StepDataInterface,int,TransMeta,Trans)   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 2
rs 10
cc 1
1
package br.ufrj.ppgi.greco.kettle;
2
3
import java.io.File;
4
import java.nio.file.Paths;
5
import java.util.Collection;
6
import java.util.Iterator;
7
8
import org.apache.jena.riot.Lang;
9
import org.apache.jena.riot.RDFLanguages;
10
import org.pentaho.di.core.exception.KettleException;
11
import org.pentaho.di.core.exception.KettleStepException;
12
import org.pentaho.di.core.row.RowDataUtil;
13
import org.pentaho.di.core.row.RowMeta;
14
import org.pentaho.di.core.row.RowMetaInterface;
15
import org.pentaho.di.trans.Trans;
16
import org.pentaho.di.trans.TransMeta;
17
import org.pentaho.di.trans.step.BaseStep;
18
import org.pentaho.di.trans.step.StepDataInterface;
19
import org.pentaho.di.trans.step.StepInterface;
20
import org.pentaho.di.trans.step.StepMeta;
21
import org.pentaho.di.trans.step.StepMetaInterface;
22
23
import org.apache.jena.ontology.OntClass;
24
import org.apache.jena.ontology.OntModel;
25
import org.apache.jena.ontology.OntModelSpec;
26
import org.apache.jena.ontology.OntProperty;
27
import org.apache.jena.rdf.model.ModelFactory;
28
29
import br.ufrj.ppgi.greco.kettle.plugin.tools.datatable.DataTable;
30
31
public class OwlInputStep extends BaseStep implements StepInterface {
32
	boolean repetir = true;
33
	public OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
34
35
	public OwlInputStep(StepMeta s, StepDataInterface stepDataInterface, int c, TransMeta t, Trans dis) {
36
		super(s, stepDataInterface, c, t, dis);
37
	}
38
39
	public boolean init(StepMetaInterface smi, StepDataInterface sdi) {
40
		return super.init(smi, sdi);
41
	}
42
43
	public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
44
		OwlInputStepMeta meta = (OwlInputStepMeta) smi;
45
		OwlInputStepData data = (OwlInputStepData) sdi;
46
		
47
		Object[] row = getRow();
48
		
49
		if (first) {
50
			first = false;
51
			RowMetaInterface rowMeta = getInputRowMeta(row != null);
52
			data.outputRowMeta = rowMeta.clone();
53
			meta.getFields(data.outputRowMeta, getStepname(), null, null, this);
54
		}
55
		
56
		DataTable<String> table = meta.getVocabTable();
57
		boolean isTableEmpty = table.getValue(0, OwlInputStepMeta.Field.VOCAB_TABLE_URI.name()).isEmpty();
58
		
59
		if (!isTableEmpty){
60
			for (int k = 0; k < table.size(); k++) {
61
				putOutRow(row, meta, data, 
62
						table.getValue(k, OwlInputStepMeta.Field.VOCAB_TABLE_PREFIX.name()),
63
						table.getValue(k, OwlInputStepMeta.Field.VOCAB_TABLE_URI.name()),
64
						table.getValue(k, OwlInputStepMeta.Field.VOCAB_TABLE_PROPERTY.name()),
65
						table.getValue(k, OwlInputStepMeta.Field.VOCAB_TABLE_TYPE.name()));
66
			}
67
		} else {
68
			table = meta.getMapTable();
69
			for (int k = 0; k < table.size(); k++) {
70
				String owlFile = getOwlFile(table.getValue(k, OwlInputStepMeta.Field.MAP_TABLE_ONTOLOGY_URI.name()));
71
				try {
72
					model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
73
					this.logBasic("Attempting to read " + owlFile + " as RDF/XML");
74
					model.read(owlFile);
75
					this.logBasic(owlFile + " has been read successfully");
76
				} catch (Exception eox) {
77
					this.logBasic("Error reading " + owlFile + " as RDF/XML: " + eox.getMessage());
78
					Collection<Lang> registeredLanguages = RDFLanguages.getRegisteredLanguages();
79
					for (Lang c : registeredLanguages) {
80
						try {
81
							this.logBasic("Trying to read the file as... " + c.getName());
82
							model.read(owlFile, c.getName());
83
							this.logBasic(owlFile + " has been read successfully");
84
							break;
85
						} catch (Exception e) {
86
							this.logBasic("File could not be read as " + c.getName() + ": " + e.getMessage());
87
						}
88
					}
89
				}
90
				
91
				if (!model.isEmpty()) {
92
					String ontoField = table.getValue(k, OwlInputStepMeta.Field.MAP_TABLE_ONTOLOGY_NAME.name());
93
					for (Iterator<OntClass> i = model.listClasses(); i.hasNext();) {
94
						OntClass cls = i.next();
95
						if (cls.getLocalName() != null) {
96
							putOutRow(row, meta, data, 
97
									ontoField.trim(), cls.getURI(), "rdf:type", "rdfs:class");
98
						}
99
					} 
100
101
					for (Iterator<OntProperty> j = model.listAllOntProperties(); j.hasNext();) {
102
						OntProperty proper = j.next();
103
						if (proper.getLocalName() != null) {
104
							putOutRow(row, meta, data, 
105
									ontoField.trim(), proper.getURI(), "rdf:type", "rdfs:property");
106
						}
107
					}
108
				}
109
			}
110
		}
111
		
112
		if (row == null){
113
			setOutputDone();
114
			return false;
115
		}
116
		
117
		return true;
118
	}
119
	
120
	/**
121
	 * Verifies whether src is an actual file or and URI.
122
	 * If the String src is a file, return it's valid path
123
	 * else return the URI
124
	 * 
125
	 * @param src the original file path
126
	 * @return a valid file path or an URI
127
	 */
128
	private String getOwlFile(String src) {
129
		File file = new File(src);
130
		if (!file.isDirectory()){
131
		   file = file.getParentFile();
132
		}
133
		if (file.exists()){
134
			return Paths.get(src).toUri().toString();
135
		}else {
136
			return src;
137
		}
138
	}
139
140
	public void dispose(StepMetaInterface smi, StepDataInterface sdi) {
141
		super.dispose(smi, sdi);
142
	}
143
	
144
	private void putOutRow(Object[] inputRow, OwlInputStepMeta meta, OwlInputStepData data, String... var) throws KettleStepException {
145
146
		int outputRowPos = 0;
147
		Object[] outputRow = null;
148
149
		if (meta.isKeepInputFields()) {
150
			outputRow = inputRow;
151
			outputRowPos = getInputRowMeta().size();
152
		} else {
153
			outputRow = new Object[4];
154
		}
155
		
156
		if (outputRow != null){
157
			for (String arg : var) {
158
				outputRow = RowDataUtil.addValueData(outputRow, outputRowPos++, arg);
159
			}
160
		
161
			putRow(data.outputRowMeta, outputRow);
162
		}
163
	}
164
	
165
	private RowMetaInterface getInputRowMeta(boolean hasInputRow) {
166
167
		RowMetaInterface rowMeta = null;
168
		if (hasInputRow)
169
			rowMeta = getInputRowMeta();
170
		else
171
			rowMeta = new RowMeta();
172
173
		return rowMeta;
174
	}
175
176
}
177