XD01 Scripted Reader Example: CSV like file reader


Outline

This scripted reader reads a CSV like file with 4 columns (a,b,c,d). The content type check is performed by checking for a keyword.

Platforms
32/64bit 32/64bit 32/64bit
Requirements
  • None
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:

	#CANSPEED
	1565436982527 ;140 ;0 ;0.0 ;0.0
	1565436982578 ;51 ;0 ;0.0 ;0.0
	1565436982606 ;28 ;0 ;0.0 ;0.0
	1565436982618 ;12 ;0 ;0.0 ;0.0
	1565436982639 ;21 ;0 ;0.0 ;0.0
	1565436982658 ;19 ;0 ;0.0 ;0.0
	1565436982686 ;28 ;0 ;0.0 ;0.0
	1565436982766 ;80 ;170314 ;2128.925 ;112.04868421052633
	1565436982770 ;4 ;0 ;0.0 ;106.44625
	1565436982798 ;28 ;0 ;0.0 ;101.12393750000001


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 'CANSPEED' is available.

	var StringType = Java.type("java.lang.String");
	var s = new StringType(buffer);
	if (s.contains('CANSPEED'))
	    IRecordReader.APPLICABLE;


Reader Script

The script read the input line by line, splits the input lines and writes them in 4 signals.


// 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);
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.Integer, SignalDescriptor.DEFAULT);
var d = generator.addSignal(null, "d", "", ProcessType.Discrete, SignalType.Integer, SignalDescriptor.DEFAULT);
var wa /*:IIntegerSamplesWriter:*/ = generator.getWriter(a);
var wb /*:IIntegerSamplesWriter:*/ = generator.getWriter(b);
var wc /*:IIntegerSamplesWriter:*/ = generator.getWriter(c);
var wd /*:IIntegerSamplesWriter:*/ = generator.getWriter(d);

var current = 0;
var started;
generator.open(current);
reader.changed(IRecordReader.CHANGED_RECORD);

try {
    var input /*:java.io.BufferedReader:*/ = new BufferedReader(new InputStreamReader(inputStream));
    var line /*:java.lang.String:*/ = input.readLine(); // skip type header
    while ((line = input.readLine()) != null) {
        var splitted = line.split(";");
        var parsed = TimeBase.ns.parseUnits(splitted[0] + "ms");
        if (started == null)
            started = parsed;
        current = parsed - started;
        console.println(current + " " + line + " " + line.length());

        // add samples
        reader.lock();
        wa.writeInt(current, false, splitted[1]);
        wb.writeInt(current, false, splitted[2]);
        wc.writeInt(current, false, splitted[3]);
        wd.writeInt(current, false, splitted[4]);
        reader.unlock(IRecordReader.CHANGED_SIGNALS);

    }
} catch (e) {
    console.println(e);
}
reader.lock();
generator.close(current + 1);
reader.unlock(IRecordReader.CHANGED_NONE);

toem

technical software and tooling

Company

Contact Us

This email address is being protected from spambots. You need JavaScript enabled to view it.