User’s Manual Thomas Jefferson National Accelerator Facility


Services that read/write EvIO files



Download 415.94 Kb.
Page14/17
Date28.01.2017
Size415.94 Kb.
#10668
1   ...   9   10   11   12   13   14   15   16   17

Services that read/write EvIO files

As we learned the ClaRA transient data envelope can package arbitrary data objects as well as primitive types and arrays of primitive types. The ClaRA framework is designed to be transient data object agnostic with the exception of EvIO objects. EvIO is the JLAB data acquisition (CODA) common event format, and is adopted to be the Clas12 offline software standard data format. So, it would be a useful exercise to write a service that can read/write EvIO format files. One can anticipate that these services are going to be very similar and can be designed as one service with a special configuration option. Yet, let us write two separate services, one for reading and another one for writing. This will prepare us for the example in the following chapter where we will learn how to link together services and compose an application. The code for the EvioFileReaderService class is shown below.

public class EvioFileReaderService extends JService {
private EvioReader reader;

private String filename;


// stores file IO error. no error = null

private String openingError;


@Override

public void configure(JioSerial data) {

try {

filename = data.getStringObject();



if(reader != null){

reader.close();

}

reader = new EvioReader(filename);



openingError = null;

} catch (IOException e) {

openingError = e.toString();

e.printStackTrace();

}

}

The above code snippet shows the class definition with three object fields that store EvIO reader object, the EvIO file name, and file I/O operation status, as well as the implementation of the ClaRA configure interface method. The main action of this service is coded inside of the execute method (shown bellow).



@Override

public JioSerial execute(JioSerial data) {

if(reader == null){

JioSerial out = new JioSerial(CConstants.REJECT);

out.setStatus(CConstants.error);

out.setLanguage(CConstants.LANG_JAVA);

if(openingError != null){

out.setDataDescription("file not specified");

} else {

out.setDataDescription(openingError);

}

return out;



}

try {


EvioEvent event = reader.parseNextEvent();

if(event == null){

JioSerial out = new JioSerial(CConstants.REJECT);

out.setStatus(CConstants.error);

out.setLanguage(CConstants.LANG_JAVA);

out.setDataDescription("no more events!");

}

JioSerial out = new JioSerial(event);



out.setMimeType(MimeType.EVIO_OBJECT);

out.setLanguage(CConstants.LANG_JAVA);

out.setDataDescription("data from file " + filename);

return out;

} catch (EvioException e) {

JioSerial out = new JioSerial(CConstants.REJECT);

out.setStatus(CConstants.error);

out.setLanguage(CConstants.LANG_JAVA);

out.setDataDescription(e.toString());

return out;

}

}

In the above code we reject the service in case we have a problem opening the file and create the Evioreader object. We also do this in the case of an exception thrown during the creation of the EvioEvent object. Note that the data description contains the string describing the reason for the rejection. The EvioEvent object, which is successfully created as a result of reading the EvIO file, will be packed inside of the transient data envelope and be presented as the output of this service. The rest of the code of the service is shown below where you see the usual implementation of the ClaRA interface methods, description, author, version, etc. of the service.



@Override

public JioSerial execute(JioSerial[] data) {

return null;

}
@Override

public void destruct() {

reader.close();

}
@Override

public String getName() {

return "ReadEvio";

}
@Override

public String getAuthor() {

return "Sebouh Paul";

}
@Override

public String getDescription() {

return "Reads evio events from file and returns them";

}
@Override

public String getVersion() {

return "4.0";

}
@Override

public String getLanguage() {

return CConstants.LANG_JAVA;

}
As we mention previously the EvIO writer service (EvioFileWriterService) code is very similar to the above-presented EvioFileReaderService service class, and is presented below (without explanation) mainly for convenience to the reader that might cut and paste the code into the preferred IDE for further tests and modifications.

public class EvioFileWriterService extends JService {
private EventWriter writer;

private String filename;


// stores file IO error. no error = null

private String openingError;


@Override

public void configure(JioSerial data) {

try {

filename = data.getStringObject();



if(writer != null){

writer.close();

}

writer = new EventWriter(filename);



} catch (IOException e) {

openingError = e.toString();

e.printStackTrace();

} catch (EvioException e) {

e.printStackTrace();

}

}


@Override

public JioSerial execute(JioSerial data) {

if(writer == null){

JioSerial out = new JioSerial(CConstants.REJECT);

out.setStatus(CConstants.error);

out.setLanguage(CConstants.LANG_JAVA);

if(openingError != null){

out.setDataDescription("file not specified");

} else {

out.setDataDescription(openingError);

}

return out;



}

if(data.getMimeType() != MimeType.EVIO_OBJECT){

JioSerial out = new JioSerial(CConstants.REJECT);

out.setDataDescription("expected mime type EVIO_OBJECT, received type " +

data.getMimeType());

out.setStatus(CConstants.error);

System.out.println("error: wrong data type");

return out;

}
try {

EvioEvent event = (EvioEvent) data.getData();

writer.writeEvent(event);

JioSerial out = new JioSerial("done");

out.setMimeType(MimeType.STRING);

out.setLanguage(CConstants.LANG_JAVA);

out.setDataDescription("data has successfully been written to file " + filename);

return out;

} catch (EvioException e) {

JioSerial out = new JioSerial(CConstants.REJECT);

out.setStatus(CConstants.error);

out.setLanguage(CConstants.LANG_JAVA);

out.setDataDescription(e.toString());

return out;

} catch (IOException e) {

JioSerial out = new JioSerial(CConstants.REJECT);

out.setStatus(CConstants.error);

out.setLanguage(CConstants.LANG_JAVA);

out.setDataDescription(e.toString());

return out;

}

}

@Override



public JioSerial execute(JioSerial[] data) {

return null;

}

@Override



public void destruct() {

try {


writer.close();

} catch (EvioException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}



@Override

public String getName() {

return "WriteEvio";

}

@Override



public String getAuthor() {

return "Sebouh Paul";

}

@Override



public String getDescription() {

return "writes evio events to a file";

}

@Override



public String getVersion() {

return "4.0";

}

@Override



public String getLanguage() {

return CConstants.LANG_JAVA;

} }
Refer to previous paragraphs of this chapter for similar steps that can be used to compile and deploy discussed service engines as ClaRA services.


Download 415.94 Kb.

Share with your friends:
1   ...   9   10   11   12   13   14   15   16   17




The database is protected by copyright ©ininet.org 2024
send message

    Main page