XD02 Scripted Reader Example: Text file with register, analog, tasks and logs
Outline
This scripted reader a text file with analog data , tasks and logs. The content type check is performed by checking for a keyword.
Platforms |
|
|||
Requirements |
|
|||
Content of example record files |
|
Output
Usage
To create a Scripted Reader, open the preferences and go to "impulse->Serializer-> Scripted Reader", open the configuration dialog and add a new Scripted Reader configuration.
A scripted reader calls a user script to perform the actual parsing and generation of the signals.
To enable the use of a scripted read for workspace resources, a user need to add a content type association and script code to verify the validity of the content.
R005 Scripted Reader 10 Signal Script
Script Examples impulse JDK Open JavaDoc
Data input
The data to be parsed is quite similar to a numeric CSV file:
# jet 1.0;A;1;1;2;3 2.0;A;2;3;4;6; 2.0;T;task1; 2.0;L;started tesk 1;Bad;345 2.1;T;task2; 3.0;A;1;1;2;3 4.0;A;2;3;4;5 4.0;T;task3; 4.1;T;task4; 4.5;T;taskx; 4.7;L;huhu my text;Bad;345 5.0;A;1;1;2;3 6.0;A;2;3;4;5
Content type check
To enable the reader for the use with resources, we need to add a content type association and script code to verify the validity of the content. The script just checks if the keyword 'jet' is available.
var StringType = Java.type("java.lang.String"); var s = new StringType(buffer); if (s.contains('jet')) IRecordReader.APPLICABLE;
Reader Script
The script read the input line by line, splits the input lines and checks the type of line line by the 2nd arguments (A for analog data, T for tasks and L for logs).
// reader : The reader object for synchronisation purpose (de.toem.impulse.serializer.base.IScriptedReader) // generator : Record generator (de.toem.impulse.samples.ISingleDomainRecordGenerator) // inputStream : Input stream object (java.io.InputStream) // progress : Progress control (de.toem.pattern.threading.IProgress) // console : Console output (de.toem.impulse.scripting.IScriptConsole) // Init the record generator.initRecord("Example Record", TimeBase.ns); // analog data var a = generator.addSignal(null, "a", "", ProcessType.Discrete, SignalType.Integer, SignalDescriptor.DEFAULT); var b = generator.addSignal(null, "b", "", ProcessType.Discrete, SignalType.Integer, SignalDescriptor.DEFAULT); var c = generator.addSignal(null, "c", "", ProcessType.Discrete, SignalType.Float, SignalDescriptor.DEFAULT); var d = generator.addSignal(null, "d", "", ProcessType.Discrete, SignalType.Float, SignalDescriptor.DEFAULT); var wa /*:IIntegerSamplesWriter:*/ = generator.getWriter(a); var wb /*:IIntegerSamplesWriter:*/ = generator.getWriter(b); var wc /*:IFloatSamplesWriter:*/ = generator.getWriter(c); var wd /*:IFloatSamplesWriter:*/ = generator.getWriter(d); // task data var ti = generator.addSignal(null, "idle", "", ProcessType.Discrete, SignalType.Event, SignalDescriptor.EventGantt); var tu = generator.addSignal(null, "unknown", "", ProcessType.Discrete, SignalType.Event, SignalDescriptor.EventGantt); var t0 = generator.addSignal(null, "task0", "", ProcessType.Discrete, SignalType.Event, SignalDescriptor.EventGantt); var t1 = generator.addSignal(null, "task1", "", ProcessType.Discrete, SignalType.Event, SignalDescriptor.EventGantt); var t2 = generator.addSignal(null, "task2", "", ProcessType.Discrete, SignalType.Event, SignalDescriptor.EventGantt); var t3 = generator.addSignal(null, "task3", "", ProcessType.Discrete, SignalType.Event, SignalDescriptor.EventGantt); var wti /*:IEventSamplesWriter:*/ = generator.getWriter(ti); var wt = { idle: generator.getWriter(ti), unknown: generator.getWriter(tu), task0: generator.getWriter(t0), task1: generator.getWriter(t1), task2: generator.getWriter(t2), task3: generator.getWriter(t3) }; // log data var log = generator.addSignal(null, "log", "", ProcessType.Discrete, SignalType.Struct, SignalDescriptor.DEFAULT); var wl /*:IStructSamplesWriter:*/ = generator.getWriter(log); var members = wl.createMembers(3); wl.createMember(members, 0, "Message", ISample.STRUCT_TYPE_TEXT, null, ISample.FORMAT_DEFAULT); wl.createMember(members, 1, "Severity", ISample.STRUCT_TYPE_LOCAL_ENUM, null, ISample.FORMAT_TEXT); wl.createMember(members, 2, "Source", ISample.STRUCT_TYPE_INTEGER, null, ISample.FORMAT_HEXADECIMAL); // start var current = 0; generator.open(current); reader.changed(IRecordReader.CHANGED_RECORD); var currentTask /*:IEventSamplesWriter:*/ = wt.idle; currentTask.writeString(current, false, 'Running'); // parser try { // read lines var BufferedReader = Java.type("java.io.BufferedReader"); var InputStreamReader = Java.type("java.io.InputStreamReader"); var Integer = Java.type("java.lang.Integer"); var input /*:BufferedReader:*/ = new BufferedReader(new InputStreamReader(inputStream)); var line /*:String:*/ ; while ((line = input.readLine()) != null) { // trim and check line = line.trim(); if (!line.isEmpty() && !line.startsWith('#')) { // split and time value var splitted = line.split(";"); var current = TimeBase.ns.parseUnits(splitted[0] + "ms"); console.println(current + " " + line + " " + line.length()); // add samples reader.lock(); // analog data if (splitted[1].trim() == 'A') { wa.writeInt(current, false, splitted[2]); wb.writeInt(current, false, splitted[3]); wc.writeFloat(current, false, splitted[4]); wd.writeFloat(current, false, splitted[5]); } // task data else if (splitted[1].trim() == 'T') { var nTask /*:IEventSamplesWriter:*/ ; if (splitted[2].trim() in wt) nTask = wt[splitted[2].trim()]; else nTask = wt.unknown; if (nTask != currentTask) { currentTask.writeString(current, false, null); currentTask.attachRelation(nTask.getId(), "/666666/line/none", 0); nTask.writeString(current, false, 'Running'); currentTask = nTask; } } // log data else if (splitted[1].trim() == 'L') { members[0].setValue(splitted[2]); members[1].setValue(splitted[3]); members[2].setValue(Integer.parseInt(splitted[4])); wl.write(current, false, members); } reader.unlock(IRecordReader.CHANGED_SIGNALS); } } } catch (e) { console.println(e); // pass exception object to error handler } reader.lock(); generator.close(current + 1); reader.unlock(IRecordReader.CHANGED_NONE);