I'm trying to figure out how to best parse the following log file, splitting each section seperated by the horizontal lines and extract various pieces of data, e.g. 'COMPANY123', 'BIMMU', the date (2/18 etc.) and then create a string containing all of the other data contained in a section delimited by the horizontal lines.
I.e., I want to create an array of 'statement' objects each with the following attributes:
Company name, Account, Date, Data.
E.g. for the second record below,
Account = 'BIMMU'
Firm = 'Super Corporation'
Date= 9/14/11
Data = '* * * * * * * * TODAYS ACCOUNT ACTIVITY * * * * * * * * * * *
9/14/11 Y9 CALL OESX OCT 11 ........ etc'
The log is a fixed-width text file and the variables (date etc.) always occur at the same position in the line, e.g. sSalesCode = line.substring(142, 147);
Should I maybe do this in two passes, e.g. split the code into sections delimited by the horizontal line, and then parse these sections individually?
Just writing this out here has helped me get my train of thought, but if anybody else has any smart ideas then it would be great to hear them.
------------------------------------------------------------------------------------------------------------------------------------F BIASPBIMMU
BIMMU BIASP-COMPANY123 KG (Z ) 9/14/11 EU (T- I- ) MT-0 F BIASP²BIMMU
CALLS 2/18 YI 50.00-X (49) F BIASP²BIMMU
------------------------------------------------------------------------------------------------------------------------------------F BIASPBIMMU
BIMMU BIMM2-SUPER CORPORATION KG (Z ) 9/14/11 EU (T- I- ) MT-0 F BIMM2²BIMMU
F BIMM2²BIMMU
* * * * * * * * * * * * * * * * * * * T O D A Y S A C C O U N T A C T I V I T Y * * * * * * * * * * * * * * * * * * * *F BIMM2²BIMMU
9/14/11 Y9 500 GO CALL OESX OCT 11 2400 9.60 EU .00 F BIMM2²BIMMU
GO-PARFSecurities Ser F BIMM2²BIMMU
Y9 * 500 * COMMISSIONS EU 250.00- F BIMM2²BIMMU
Y9 PERTES & PROFITS NETS EU 250.00- F BIMM2BIMMU
CALLS 9/14 E1 17,825.00-H ( 1) F BIMM2²BIMMU
CALLS 9/14 E1 17,825.00-N ( 1) F BIMM2²BIMMU
-----------------------------------------------------------------------------------------------------------------------------------
You can try to use framework Fixedformat4j. It uses annotations and works fast. I have implemented it partially for my project to understand how it works.
You can create class with annotations like this:
#Record
public class LogRecord {
private String firm;
private String user;
private Date logonDate;
private String logData;
public String getFirm() {
return firm;
}
#field(offset=10, length=10)
public void setFirm(String firm) {
this.firm = firm;
}
public String getUser() {
return user;
}
#field(offset=0, length=10)
public void setUser(String user) {
this.user = user;
}
public Date getLogonDate() {
return logonDate;
}
#field(offset=nn, length=8)
#FixedFormatPattern("mm/dd/yy")
public void setLogonDate(Date logonDate) {
this.logonDate = logonDate;
}
public String getLogData() {
return logData;
}
#field(offset=mm, length=yy)
public void setLogData(String logData) {
this.logData = logData;
}
}
And then instantiate it with FixedFormatManager.
i had similar problem recenly, i ended up using Flapjack (Google Code: Flapjack)... See the examples on google code, i guess it should help you out.
Related
I would like to get the GMT [ Greenwich Mean Time ], and also I don't want to rely on my system date time for that. Basically, I want to use time sync server like in.pool.ntp.org [ India ] for GMT calculation, or may be I am going in wrong direction!
How to do this in java ?
Is there any java library to get time from Time server?
sp0d is not quite right:
timeInfo.getReturnTime(); // Returns time at which time message packet was received by local machine
So it just returns current system time, not the received one. See TimeInfo man page.
You should use
timeInfo.getMessage().getTransmitTimeStamp().getTime();
instead.
So the code block will be:
String TIME_SERVER = "time-a.nist.gov";
NTPUDPClient timeClient = new NTPUDPClient();
InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
TimeInfo timeInfo = timeClient.getTime(inetAddress);
long returnTime = timeInfo.getMessage().getTransmitTimeStamp().getTime();
Date time = new Date(returnTime);
Here is a code i found somewhere else.. and i am using it. Uses apache commons library.
List of time servers: NIST Internet Time Service
import java.net.InetAddress;
import java.util.Date;
import org.apache.commons.net.ntp.NTPUDPClient;
import org.apache.commons.net.ntp.TimeInfo;
public class TimeLookup {
public static void main() throws Exception {
String TIME_SERVER = "time-a.nist.gov";
NTPUDPClient timeClient = new NTPUDPClient();
InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
TimeInfo timeInfo = timeClient.getTime(inetAddress);
long returnTime = timeInfo.getReturnTime();
Date time = new Date(returnTime);
System.out.println("Time from " + TIME_SERVER + ": " + time);
}
}
Returns the output
Time from time-d.nist.gov: Sun Nov 25 06:04:34 IST 2012
I know this is an old question but I notice that all the answers are not correct or are complicated.
A nice and simple way to implement it is using Apache Commons Net library. This library will provide a NTPUDPClient class to manage connectionless NTP requests. This class will return a TimeInfo instance. This object should run the compute method to calculate the offset between your system's time and the NTP server's time. Lets try to implement it here
Add the Apache Commons Net library to your project.
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
Create a new instance of the NTPUDPClient class.
Setup the default timeout
Get the InetAddress of the NTP Server.
Call the NTPUDPClient.getTime() method to retrieve a TimeInfo instance with the time information from the specified server.
Call the computeDetails() method to compute and validate details of the NTP message packet.
Finally, get a NTP timestamp object based on a Java time by using this code TimeStamp.getNtpTime(currentTime + offset).getTime().
Here we have a basic implementation:
import java.net.InetAddress;
import java.util.Date;
import org.apache.commons.net.ntp.NTPUDPClient;
import org.apache.commons.net.ntp.TimeInfo;
public class NTPClient {
private static final String SERVER_NAME = "pool.ntp.org";
private volatile TimeInfo timeInfo;
private volatile Long offset;
public static void main() throws Exception {
NTPUDPClient client = new NTPUDPClient();
// We want to timeout if a response takes longer than 10 seconds
client.setDefaultTimeout(10_000);
InetAddress inetAddress = InetAddress.getByName(SERVER_NAME);
TimeInfo timeInfo = client.getTime(inetAddress);
timeInfo.computeDetails();
if (timeInfo.getOffset() != null) {
this.timeInfo = timeInfo;
this.offset = timeInfo.getOffset();
}
// This system NTP time
TimeStamp systemNtpTime = TimeStamp.getCurrentTime();
System.out.println("System time:\t" + systemNtpTime + " " + systemNtpTime.toDateString());
// Calculate the remote server NTP time
long currentTime = System.currentTimeMillis();
TimeStamp atomicNtpTime = TimeStamp.getNtpTime(currentTime + offset).getTime()
System.out.println("Atomic time:\t" + atomicNtpTime + " " + atomicNtpTime.toDateString());
}
public boolean isComputed()
{
return timeInfo != null && offset != null;
}
}
You will get something like that:
System time: dfaa2c15.2083126e Thu, Nov 29 2018 18:12:53.127
Atomic time: dfaa2c15.210624dd Thu, Nov 29 2018 18:12:53.129
This link demonstrates a java class called NtpMessage.java that you can paste into your program which will fetch the current time from an NTP server.
At the following link, Find the "Attachment" section near the bottom and download NtpMessage.java and SntpClient.java and paste it into your java application. It will do all the work and fetch you the time.
http://support.ntp.org/bin/view/Support/JavaSntpClient
Copy and paste of the code if it goes down:
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* This class represents a NTP message, as specified in RFC 2030. The message
* format is compatible with all versions of NTP and SNTP.
*
* This class does not support the optional authentication protocol, and
* ignores the key ID and message digest fields.
*
* For convenience, this class exposes message values as native Java types, not
* the NTP-specified data formats. For example, timestamps are
* stored as doubles (as opposed to the NTP unsigned 64-bit fixed point
* format).
*
* However, the contructor NtpMessage(byte[]) and the method toByteArray()
* allow the import and export of the raw NTP message format.
*
*
* Usage example
*
* // Send message
* DatagramSocket socket = new DatagramSocket();
* InetAddress address = InetAddress.getByName("ntp.cais.rnp.br");
* byte[] buf = new NtpMessage().toByteArray();
* DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 123);
* socket.send(packet);
*
* // Get response
* socket.receive(packet);
* System.out.println(msg.toString());
*
*
* This code is copyright (c) Adam Buckley 2004
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version. A HTML version of the GNU General Public License can be
* seen at http://www.gnu.org/licenses/gpl.html
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
*
* Comments for member variables are taken from RFC2030 by David Mills,
* University of Delaware.
*
* Number format conversion code in NtpMessage(byte[] array) and toByteArray()
* inspired by http://www.pps.jussieu.fr/~jch/enseignement/reseaux/
* NTPMessage.java which is copyright (c) 2003 by Juliusz Chroboczek
*
* #author Adam Buckley
*/
public class NtpMessage
{
/**
* This is a two-bit code warning of an impending leap second to be
* inserted/deleted in the last minute of the current day. It's values
* may be as follows:
*
* Value Meaning
* ----- -------
* 0 no warning
* 1 last minute has 61 seconds
* 2 last minute has 59 seconds)
* 3 alarm condition (clock not synchronized)
*/
public byte leapIndicator = 0;
/**
* This value indicates the NTP/SNTP version number. The version number
* is 3 for Version 3 (IPv4 only) and 4 for Version 4 (IPv4, IPv6 and OSI).
* If necessary to distinguish between IPv4, IPv6 and OSI, the
* encapsulating context must be inspected.
*/
public byte version = 3;
/**
* This value indicates the mode, with values defined as follows:
*
* Mode Meaning
* ---- -------
* 0 reserved
* 1 symmetric active
* 2 symmetric passive
* 3 client
* 4 server
* 5 broadcast
* 6 reserved for NTP control message
* 7 reserved for private use
*
* In unicast and anycast modes, the client sets this field to 3 (client)
* in the request and the server sets it to 4 (server) in the reply. In
* multicast mode, the server sets this field to 5 (broadcast).
*/
public byte mode = 0;
/**
* This value indicates the stratum level of the local clock, with values
* defined as follows:
*
* Stratum Meaning
* ----------------------------------------------
* 0 unspecified or unavailable
* 1 primary reference (e.g., radio clock)
* 2-15 secondary reference (via NTP or SNTP)
* 16-255 reserved
*/
public short stratum = 0;
/**
* This value indicates the maximum interval between successive messages,
* in seconds to the nearest power of two. The values that can appear in
* this field presently range from 4 (16 s) to 14 (16284 s); however, most
* applications use only the sub-range 6 (64 s) to 10 (1024 s).
*/
public byte pollInterval = 0;
/**
* This value indicates the precision of the local clock, in seconds to
* the nearest power of two. The values that normally appear in this field
* range from -6 for mains-frequency clocks to -20 for microsecond clocks
* found in some workstations.
*/
public byte precision = 0;
/**
* This value indicates the total roundtrip delay to the primary reference
* source, in seconds. Note that this variable can take on both positive
* and negative values, depending on the relative time and frequency
* offsets. The values that normally appear in this field range from
* negative values of a few milliseconds to positive values of several
* hundred milliseconds.
*/
public double rootDelay = 0;
/**
* This value indicates the nominal error relative to the primary reference
* source, in seconds. The values that normally appear in this field
* range from 0 to several hundred milliseconds.
*/
public double rootDispersion = 0;
/**
* This is a 4-byte array identifying the particular reference source.
* In the case of NTP Version 3 or Version 4 stratum-0 (unspecified) or
* stratum-1 (primary) servers, this is a four-character ASCII string, left
* justified and zero padded to 32 bits. In NTP Version 3 secondary
* servers, this is the 32-bit IPv4 address of the reference source. In NTP
* Version 4 secondary servers, this is the low order 32 bits of the latest
* transmit timestamp of the reference source. NTP primary (stratum 1)
* servers should set this field to a code identifying the external
* reference source according to the following list. If the external
* reference is one of those listed, the associated code should be used.
* Codes for sources not listed can be contrived as appropriate.
*
* Code External Reference Source
* ---- -------------------------
* LOCL uncalibrated local clock used as a primary reference for
* a subnet without external means of synchronization
* PPS atomic clock or other pulse-per-second source
* individually calibrated to national standards
* ACTS NIST dialup modem service
* USNO USNO modem service
* PTB PTB (Germany) modem service
* TDF Allouis (France) Radio 164 kHz
* DCF Mainflingen (Germany) Radio 77.5 kHz
* MSF Rugby (UK) Radio 60 kHz
* WWV Ft. Collins (US) Radio 2.5, 5, 10, 15, 20 MHz
* WWVB Boulder (US) Radio 60 kHz
* WWVH Kaui Hawaii (US) Radio 2.5, 5, 10, 15 MHz
* CHU Ottawa (Canada) Radio 3330, 7335, 14670 kHz
* LORC LORAN-C radionavigation system
* OMEG OMEGA radionavigation system
* GPS Global Positioning Service
* GOES Geostationary Orbit Environment Satellite
*/
public byte[] referenceIdentifier = {0, 0, 0, 0};
/**
* This is the time at which the local clock was last set or corrected, in
* seconds since 00:00 1-Jan-1900.
*/
public double referenceTimestamp = 0;
/**
* This is the time at which the request departed the client for the
* server, in seconds since 00:00 1-Jan-1900.
*/
public double originateTimestamp = 0;
/**
* This is the time at which the request arrived at the server, in seconds
* since 00:00 1-Jan-1900.
*/
public double receiveTimestamp = 0;
/**
* This is the time at which the reply departed the server for the client,
* in seconds since 00:00 1-Jan-1900.
*/
public double transmitTimestamp = 0;
/**
* Constructs a new NtpMessage from an array of bytes.
*/
public NtpMessage(byte[] array)
{
// See the packet format diagram in RFC 2030 for details
leapIndicator = (byte) ((array[0] >> 6) & 0x3);
version = (byte) ((array[0] >> 3) & 0x7);
mode = (byte) (array[0] & 0x7);
stratum = unsignedByteToShort(array[1]);
pollInterval = array[2];
precision = array[3];
rootDelay = (array[4] * 256.0) +
unsignedByteToShort(array[5]) +
(unsignedByteToShort(array[6]) / 256.0) +
(unsignedByteToShort(array[7]) / 65536.0);
rootDispersion = (unsignedByteToShort(array[8]) * 256.0) +
unsignedByteToShort(array[9]) +
(unsignedByteToShort(array[10]) / 256.0) +
(unsignedByteToShort(array[11]) / 65536.0);
referenceIdentifier[0] = array[12];
referenceIdentifier[1] = array[13];
referenceIdentifier[2] = array[14];
referenceIdentifier[3] = array[15];
referenceTimestamp = decodeTimestamp(array, 16);
originateTimestamp = decodeTimestamp(array, 24);
receiveTimestamp = decodeTimestamp(array, 32);
transmitTimestamp = decodeTimestamp(array, 40);
}
/**
* Constructs a new NtpMessage in client -> server mode, and sets the
* transmit timestamp to the current time.
*/
public NtpMessage()
{
// Note that all the other member variables are already set with
// appropriate default values.
this.mode = 3;
this.transmitTimestamp = (System.currentTimeMillis()/1000.0) + 2208988800.0;
}
/**
* This method constructs the data bytes of a raw NTP packet.
*/
public byte[] toByteArray()
{
// All bytes are automatically set to 0
byte[] p = new byte[48];
p[0] = (byte) (leapIndicator << 6 | version << 3 | mode);
p[1] = (byte) stratum;
p[2] = (byte) pollInterval;
p[3] = (byte) precision;
// root delay is a signed 16.16-bit FP, in Java an int is 32-bits
int l = (int) (rootDelay * 65536.0);
p[4] = (byte) ((l >> 24) & 0xFF);
p[5] = (byte) ((l >> 16) & 0xFF);
p[6] = (byte) ((l >> 8) & 0xFF);
p[7] = (byte) (l & 0xFF);
// root dispersion is an unsigned 16.16-bit FP, in Java there are no
// unsigned primitive types, so we use a long which is 64-bits
long ul = (long) (rootDispersion * 65536.0);
p[8] = (byte) ((ul >> 24) & 0xFF);
p[9] = (byte) ((ul >> 16) & 0xFF);
p[10] = (byte) ((ul >> 8) & 0xFF);
p[11] = (byte) (ul & 0xFF);
p[12] = referenceIdentifier[0];
p[13] = referenceIdentifier[1];
p[14] = referenceIdentifier[2];
p[15] = referenceIdentifier[3];
encodeTimestamp(p, 16, referenceTimestamp);
encodeTimestamp(p, 24, originateTimestamp);
encodeTimestamp(p, 32, receiveTimestamp);
encodeTimestamp(p, 40, transmitTimestamp);
return p;
}
/**
* Returns a string representation of a NtpMessage
*/
public String toString()
{
String precisionStr =
new DecimalFormat("0.#E0").format(Math.pow(2, precision));
return "Leap indicator: " + leapIndicator + "\n" +
"Version: " + version + "\n" +
"Mode: " + mode + "\n" +
"Stratum: " + stratum + "\n" +
"Poll: " + pollInterval + "\n" +
"Precision: " + precision + " (" + precisionStr + " seconds)\n" +
"Root delay: " + new DecimalFormat("0.00").format(rootDelay*1000) + " ms\n" +
"Root dispersion: " + new DecimalFormat("0.00").format(rootDispersion*1000) + " ms\n" +
"Reference identifier: " + referenceIdentifierToString(referenceIdentifier, stratum, version) + "\n" +
"Reference timestamp: " + timestampToString(referenceTimestamp) + "\n" +
"Originate timestamp: " + timestampToString(originateTimestamp) + "\n" +
"Receive timestamp: " + timestampToString(receiveTimestamp) + "\n" +
"Transmit timestamp: " + timestampToString(transmitTimestamp);
}
/**
* Converts an unsigned byte to a short. By default, Java assumes that
* a byte is signed.
*/
public static short unsignedByteToShort(byte b)
{
if((b & 0x80)==0x80) return (short) (128 + (b & 0x7f));
else return (short) b;
}
/**
* Will read 8 bytes of a message beginning at <code>pointer</code>
* and return it as a double, according to the NTP 64-bit timestamp
* format.
*/
public static double decodeTimestamp(byte[] array, int pointer)
{
double r = 0.0;
for(int i=0; i<8; i++)
{
r += unsignedByteToShort(array[pointer+i]) * Math.pow(2, (3-i)*8);
}
return r;
}
/**
* Encodes a timestamp in the specified position in the message
*/
public static void encodeTimestamp(byte[] array, int pointer, double timestamp)
{
// Converts a double into a 64-bit fixed point
for(int i=0; i<8; i++)
{
// 2^24, 2^16, 2^8, .. 2^-32
double base = Math.pow(2, (3-i)*8);
// Capture byte value
array[pointer+i] = (byte) (timestamp / base);
// Subtract captured value from remaining total
timestamp = timestamp - (double) (unsignedByteToShort(array[pointer+i]) * base);
}
// From RFC 2030: It is advisable to fill the non-significant
// low order bits of the timestamp with a random, unbiased
// bitstring, both to avoid systematic roundoff errors and as
// a means of loop detection and replay detection.
array[7] = (byte) (Math.random()*255.0);
}
/**
* Returns a timestamp (number of seconds since 00:00 1-Jan-1900) as a
* formatted date/time string.
*/
public static String timestampToString(double timestamp)
{
if(timestamp==0) return "0";
// timestamp is relative to 1900, utc is used by Java and is relative
// to 1970
double utc = timestamp - (2208988800.0);
// milliseconds
long ms = (long) (utc * 1000.0);
// date/time
String date = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss").format(new Date(ms));
// fraction
double fraction = timestamp - ((long) timestamp);
String fractionSting = new DecimalFormat(".000000").format(fraction);
return date + fractionSting;
}
/**
* Returns a string representation of a reference identifier according
* to the rules set out in RFC 2030.
*/
public static String referenceIdentifierToString(byte[] ref, short stratum, byte version)
{
// From the RFC 2030:
// In the case of NTP Version 3 or Version 4 stratum-0 (unspecified)
// or stratum-1 (primary) servers, this is a four-character ASCII
// string, left justified and zero padded to 32 bits.
if(stratum==0 || stratum==1)
{
return new String(ref);
}
// In NTP Version 3 secondary servers, this is the 32-bit IPv4
// address of the reference source.
else if(version==3)
{
return unsignedByteToShort(ref[0]) + "." +
unsignedByteToShort(ref[1]) + "." +
unsignedByteToShort(ref[2]) + "." +
unsignedByteToShort(ref[3]);
}
// In NTP Version 4 secondary servers, this is the low order 32 bits
// of the latest transmit timestamp of the reference source.
else if(version==4)
{
return "" + ((unsignedByteToShort(ref[0]) / 256.0) +
(unsignedByteToShort(ref[1]) / 65536.0) +
(unsignedByteToShort(ref[2]) / 16777216.0) +
(unsignedByteToShort(ref[3]) / 4294967296.0));
}
return "";
}
}
The server time-a.nist.gov does not list the time port; you have to use correct server ntp.xs4all.nl for getting date and time from internet:
String TIME_SERVER = "ntp.xs4all.nl";
//... some other code
How to parse HL7 multiple segments(ORC/OBR/OBX) using HAPI Framework.
I am also facing issue related to parsing the multiple lab order and also facing issue related to special character(MSH|^~\&#|) encoding .
MSH|^~\&|NIST Test Lab APP|NIST Lab Facility||NIST EHR Facility|20110531140551-0500||ORU^R01^ORU_R01|NIST-LRI-NG-RN-005.01|T|2.5.1|||AL|NE|||||LRI_Common_Component^^2.16.840.1.113883.9.16^ISO~LRI_NG_Component^^2.16.840.1.113883.9.13^ISO~LRI_RN_Component^^2.16.840.1.113883.9.15^ISO
PID|1||PATID1239^^^NIST MPI^MR||Smirnoff^Peggy^^^^^M||19750401|F||2106-3^White^HL70005^wh^white^L
ORC|RE|ORD448811^NIST EHR|R-511^NIST Lab Filler|||||||||1234567890^Fine^Larry^^^Dr.^^^NIST-AA-1^L^^^NPI
OBR|1|ORD448811^NIST EHR|R-511^NIST Lab Filler|HepABC Panel^Hepatitis A B C Panel^L|||20120628070100|||||||||1234567890^Fine^Larry^^^Dr.^^^NIST-AA-1^L^^^NPI||||||20120629132900-0500|||F
OBX|1|CWE|22314-9^Hepatitis A virus IgM Ab [Presence] in Serum^LN^HAVM^Hepatitis A IgM antibodies (IgM anti-HAV)^L||260385009^Negative (qualifier value)^SCT^NEG^NEGATIVE^L^^^Negative (qualifier value)||Negative|N|||F|||20120628070100|||||20120628100500||||Princeton Hospital Laboratory^^^^^NIST HCAA-1^XX^^^34D4567890|123 High Street^^Princeton^NJ^08540^USA^O^^34021|^Martin^Steven^M^^Dr.
OBX|2|CWE|20575-7^Hepatitis A virus Ab [Presence] in Serum^LN^HAVAB^Hepatitis A antibodies (anti-HAV)^L||260385009^Negative (qualifier value)^SCT^NEG^NEGATIVE^L^^^Negative (qualifier value)||Negative|N|||F|||20120628070100|||||20120628100500||||Princeton Hospital Laboratory^^^^^NIST HCAA-1^XX^^^34D4567890|123 High Street^^Princeton^NJ^08540^USA^O^^34021|^Martin^Steven^M^^Dr.
OBX|3|CWE|16933-4^Hepatitis B virus core Ab [Presence] in Serum^LN^HBVcAB^Hepatitis B core antibodies (anti-HBVc)^L||260385009^Negative (qualifier value)^SCT^NEG^NEGATIVE^L^^^Negative (qualifier value)||Negative|N|||F|||20120628070100|||||20120628100500||||Princeton Hospital Laboratory^^^^^NIST HCAA-1^XX^^^34D4567890|123 High Street^^Princeton^NJ^08540^USA^O^^34021|^Martin^Steven^M^^Dr.
OBX|4|SN|22316-4^Hepatitis B virus core Ab [Units/volume] in Serum^LN^HBcAbQ^Hepatitis B core antibodies (anti-HBVc) Quant^L||^0.40|[IU]/mL^international unit per milliliter^UCUM^IU/ml^^L|<0.50 IU/mL|N|||F|||20120628070100|||||20120628100500||||Princeton Hospital Laboratory^^^^^NIST HCAA-1^XX^^^34D4567890|123 High Street^^Princeton^NJ^08540^USA^O^^34021|^Martin^Steven^M^^Dr.
OBX|5|CWE|22320-6^Hepatitis B virus e Ab [Presence] in Serum^LN^HBVeAB^Hepatitis B e antibodies (anti-HBVe)^L||260385009^Negative (qualifier value)^SCT^NEG^NEGATIVE^L^^^Negative (qualifier value)||Negative|N|||F|||20120628070100|||||20120628100500||||Princeton Hospital Laboratory^^^^^NIST HCAA-1^XX^^^34D4567890|123 High Street^^Princeton^NJ^08540^USA^O^^34021|^Martin^Steven^M^^Dr.
OBX|6|CWE|5195-3^Hepatitis B virus surface Ag [Presence] in Serum^LN^HBVsAG^Hepatitis B surface antigen (HBsAg)^L||260385009^Negative (qualifier value)^SCT^NEG^NEGATIVE^L^^^Negative (qualifier value)||Negative|N|||F|||20120628070100|||||20120628100500||||Princeton Hospital Laboratory^^^^^NIST HCAA-1^XX^^^34D4567890|123 High Street^^Princeton^NJ^08540^USA^O^^34021|^Martin^Steven^M^^Dr.
OBX|7|CWE|22322-2^Hepatitis B virus surface Ab [Presence] in Serum^LN^HBVSAB^Hepatitis B surface antibody (anti-HBVs)^L||260385009^Negative (qualifier value)^SCT^NEG^NEGATIVE^L^^^Negative (qualifier value)||Negative|N|||F|||20120628070100|||||20120628100500||||Princeton Hospital Laboratory^^^^^NIST HCAA-1^XX^^^34D4567890|123 High Street^^Princeton^NJ^08540^USA^O^^34021|^Martin^Steven^M^^Dr.
OBX|8|CWE|16128-1^Hepatitis C virus Ab [Presence] in Serum^LN^HCVAB^Hepatitis C antibody screen (anti-HCV)^L||10828004^Positive (qualifier value)^SCT^POS^POSITIVE^L^^^Positive (qualifier value)||Negative|A|||F|||20120628070100|||||20120628100500||||Princeton Hospital Laboratory^^^^^NIST HCAA-1^XX^^^34D4567890|123 High Street^^Princeton^NJ^08540^USA^O^^34021|^Martin^Steven^M^^Dr.
OBX|9|SN|48159-8^Hepatitis C virus Ab Signal/Cutoff in Serum or Plasma by Immunoassay^LN^HCVSCO^Hepatitis C antibodies Signal to Cut-off Ratio^L||^10.8|{s_co_ratio}^Signal to cutoff ratio^UCUM^s/co^^L|0.0-0.9 s/co|H|||F|||20120628070100|||||20120628100500||||Princeton Hospital Laboratory^^^^^NIST HCAA-1^XX^^^34D4567890|123 High Street^^Princeton^NJ^08540^USA^O^^34021|^Martin^Steven^M^^Dr.
NTE|1||Negative: < 0.8; Indeterminate 0.8 - 0.9; Positive: > 0.9. In order to reduce the incidence of a false positive result, the CDC recommends that all s/co ratios between 1.0 and 10.9 be confirmed with additional Verification or PCR testing.
SPM|1|||119364003^Serum specimen (specimen)^SCT^SER^Serum^L|||||||||||||20120628070100
ORC|RE||R-511^NIST Lab Filler|||||||||1234567890^Fine^Larry^^^Dr.^^^NIST-AA-1^L^^^NPI|||||||||||||||||||HepABC Panel^Hepatitis A B C Panel^L
OBR|2||R-511^NIST Lab Filler|11011-4^Hepatitis C virus RNA [Units/volume] (viral load) in Serum or Plasma by Probe and target amplification method^LN^HCVRNA^Hepatitis C RNA PCR^L|||20120628070100||||G|||||1234567890^Fine^Larry^^^Dr.^^^NIST-AA-1^L^^^NPI||||||20120629132900-0500|||F|16128-1&Hepatitis C virus Ab [Presence] in Serum&LN&HCVAB&Hepatitis C antibody screen (anti-HCV)&L|||ORD448811&NIST EHR^R-511&NIST Lab Filler|||||||||||||||||||||HepABC Panel^Hepatitis A B C Panel^L
OBX|1|SN|11011-4^Hepatitis C virus RNA [Units/volume] (viral load) in Serum or Plasma by Probe and target amplification method^LN^HCVRNA^Hepatitis C RNA PCR^L||^7611200|[IU]/mL^international unit per milliliter^UCUM^IU/ml^^L|<43 IU/mL|H|||F|||20120628070100|||||20120629092700||||Princeton Hospital Laboratory^^^^^NIST HCAA-1^XX^^^34D4567890|123 High Street^^Princeton^NJ^08540^USA^O^^34021|^Martin^Steven^M^^Dr.
Code Snippet:
package HAPIHL7Parser.HAPIHL7Parser;
import java.io.FileReader;
import ca.uhn.hl7v2.DefaultHapiContext;
import ca.uhn.hl7v2.HapiContext;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.parser.Parser;
import ca.uhn.hl7v2.util.Hl7InputStreamMessageIterator;
import ca.uhn.hl7v2.util.Terser;
import ca.uhn.hl7v2.validation.impl.NoValidation;
/**
* 40 * Example code for using the {#link Terser} 41 * 42 * #author
* Sitansu 43 * #version $Revision:
* 1.3 $ updated on $Date: 2009-08-06 22:43:33 $ by $Author: Sitansu $ 44
*/
public class ExampleUseTerser {
/**
* 49 * A simple example of parsing a message 50 * 51 * #throws HL7Exception
* 52 * #throws EncodingNotSupportedException 53
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
FileReader reader = new FileReader("src\\main\\resource\\LRI_5.1-NG-RN_Reflex_Hepatitis_Parent_Child_Message.txt");
// Create an iterator to iterate over all the messages
Hl7InputStreamMessageIterator iter = new Hl7InputStreamMessageIterator(reader);
HapiContext context = new DefaultHapiContext();
context.setValidationContext(new NoValidation());
Parser p = context.getGenericParser();
String msg = null;
while (iter.hasNext()) {
/*
* If we don't already have a connection, create one. 42 * Note that
* unless something goes wrong, it's very common 43 * to keep
* reusing the same connection until we are done 44 * sending
* messages. Many systems keep a connection open 45 * even if a long
* period will pass between messages being 46 * sent. This is good
* practice, as it is much faster than 47 * creating a new
* connection each time. 48
*/
msg = iter.next().toString();
System.out.println("Sent message. Response was " + msg);
}
Message hapiMsg = p.parse(msg);
// Message hapiMsg = p.parse(next);
/*
* 70 * Another way of reading messages is to use a Terser. The terser
* 71 * accepts a particular syntax to retrieve segments. See the API 72
* * documentation for the Terser for more information. 73
*/
Terser terser = new Terser(hapiMsg);
/*
* 77 * Sending Application is in MSH-3-1 (the first component of the
* third 78 * field of the MSH segment) 79
*/
String sendingApplication = terser.get("/.MSH-3");
System.out.println("Sending Application:" + sendingApplication);
String sendingfacility = terser.get("/.MSH-4");
System.out.println("Sending Facility:" + sendingfacility);
String timeofdate = terser.get("/.MSH-7");
System.out.println("Date/time of the message:" + timeofdate);
String messagetype = terser.get("/.MSH-9");
System.out.println("Message Type:" + messagetype);
String messageControlID = terser.get("/.MSH-10");
System.out.println("Message Control ID:" + messageControlID);
String patientIDTypeCode = terser.get("/.PID-3-5");
System.out.println("Patient ID Type Code:" + patientIDTypeCode);
String dateOfBirth = terser.get("/.PID-7");
System.out.println("Date of Birth:" + dateOfBirth);
String placerGroupNumber = terser.get("/.ORC-4-1");
System.out.println("Placer Group Number:" + placerGroupNumber);
String placerOrderNo = terser.get("/.ORC-2-1");
System.out.println("Placer Order Number:" + placerOrderNo);
String fillerOrderNo = terser.get("/.ORC-3-1");
System.out.println("Filler Order Number:" + fillerOrderNo);
String orderProviderID = terser.get("/.ORC-12-1");
System.out.println("Ordering Provider ID:" + orderProviderID);
String orderProviderIDType = terser.get("/.ORC-12-13");
System.out.println("Ordering Provider ID Type:" + orderProviderIDType);
String plcOrderNO = terser.get("/.OBR-2-1");
System.out.println("Placer Order Number:" + plcOrderNO);
String fillrOrdNo = terser.get("/.OBR-3-1");
System.out.println("Filler Order Number:" + fillrOrdNo);
String testCode = terser.get("/.OBR-4-1");
System.out.println("Test Code:" + testCode);
String testDesc = terser.get("/.OBR-4-2");
System.out.println("Test Description:" + testDesc);
String testALTDesc = terser.get("/.OBR-4-9");
System.out.println("Test alternative Description:" + testALTDesc);
String obsDateTime = terser.get("/.OBR-7-1");
System.out.println("Observation Date Time:" + obsDateTime);
String speciActionCode = terser.get("/.OBR-11");
System.out.println("Specimen Action Code:" + speciActionCode);
String relevantClinicInfo = terser.get("/.OBR-13-1");
System.out.println("Relevant Clinical Information:" + relevantClinicInfo);
String relevantClinicInfo2 = terser.get("/.OBR-13-2");
System.out.println("Relevant Clinical Information:" + relevantClinicInfo2);
String resultStatus = terser.get("/.OBR-25");
System.out.println("Result Status:" + resultStatus);
String test = terser.get("/.OBSERVATION(0)/OBX-3-1");
System.out.println("Result Status:" + test);
String test1 = terser.get("/.OBSERVATION(2)/OBX-3-1");
System.out.println("Result Status:" + test1);
String test2 = terser.get("/.OBSERVATION(3)/OBX-3-1");
System.out.println("Result Status:" + test2);
String test3 = terser.get("/.OBSERVATION(4)/OBX-3-1");
System.out.println("Result Status:" + test3);
//terser.getFinder().iterate(, true);
String testNotes = terser.get("/.NTE");
System.out.println("Test Notes:" + testNotes);
String speType = terser.get("/.SPM-4-9");
System.out.println("Specimen Type:" + speType);
String speciRejReason = terser.get("/.SPM-21-9");
System.out.println("Specimen Reject reason:" + speciRejReason);
String specCond = terser.get("/.SPM-24-9");
System.out.println("Specimen condition:" + specCond);
String specCollStartDateTime = terser.get("/.SPM-17-1-1");
System.out.println("Speciment Collection Start Date Time:" + specCollStartDateTime);
String notesCommts = terser.get("/.NTE-3");
System.out.println("Notes and Comments:" + notesCommts);
String obxID = terser.get("/.OBX-3-1");
System.out.println("Observation ID:" + obxID);
String obxText = terser.get("/.OBX-3-2");
System.out.println("Observation Text:" + obxText);
String obxText2 = terser.get("/.OBX-3-9");
System.out.println("Observation Text:" + obxText2);
String obxValue = terser.get("/.OBX-5-1");
System.out.println("Observation Value:" + obxValue);
String obxUnits = terser.get("/.OBX-6-1");
System.out.println("Observation Units:" + obxUnits);
String refRange = terser.get("/.OBX-7");
System.out.println("Reference Range:" + refRange);
String abnormalFlags = terser.get("/.OBX-8");
System.out.println("Abnormal Flags:" + abnormalFlags);
String obXResultStatus = terser.get("/.OBX-11");
System.out.println("Observation Result Status:" + obXResultStatus);
String dateOfObservation = terser.get("/.OBX-14-1");
System.out.println("Date/Time of the Observation:" + dateOfObservation);
String perOrgName = terser.get("/.OBX-23-1");
System.out.println("Performing Organization Name:" + perOrgName);
String perOrgAdd1 = terser.get("/.OBX-24-1");
System.out.println("Performing Organization address Line 1:" + perOrgAdd1);
String perOrgAdd2 = terser.get("/.OBX-24-2");
System.out.println("Performing Organization address Line 2:" + perOrgAdd2);
String perOrgCity = terser.get("/.OBX-24-3");
System.out.println("Performing Organization City:" + perOrgCity);
String perOrgState = terser.get("/.OBX-24-4");
System.out.println("Performing Organization State:" + perOrgState);
String perOrgZipCode = terser.get("/.OBX-24-5");
System.out.println("Performing Organization Zip code:" + perOrgZipCode);
// HIS
/*
* 85 * We can use brackets to get particular repetitions 86
*/
/*
* 108 * MSH|^~\&|||||||ORU^R01|||2.5 109 * OBX|1||ST||This is the value
* for rep 0 110 * OBX|2||ST||This is the value for rep 1 111 *
* OBX|3||ST||This is the value for rep 2 112 * OBX|4||ST||This is the
* value for rep 3 113 * OBX|5||ST||This is the value for rep 4
*/
}
}
You can specify index while get call as below:
String perOrgCity = terser.get("/.OBX1-24-3");
Note the 1 in OBX1 in above code. Indexes zero based. You can simply replace 1 with your loop counter if you want to read multiple of them.
Here is the similar question.
I am trying to access a java program MELTING 5 in R using the rjava package.
I can do it using the system function as follows using the batch file.
path <- "path/to/melting.bat"
sequence = "GTCGTATCCAGTGCAGGGTCCGAGGTATTCGCACTGGATACGACTTCCAC"
hybridisation.type = "dnadna"
OligomerConc = 5e-8
Sodium = 0.05
command=paste("-S", sequence,
"-H", hybridisation.type,
"-P", OligomerConc,
"-E", paste("Na=", Sodium, sep = ""))
system(paste("melting.bat", command))
I am trying to do the same using a wrapper, following the steps in hellowjavaworld without any success.
.jaddClassPath('path/to/melting5.jar')
main <- .jnew("melting/Main")
out <- .jcall(obj = main, returnSig = "V", method = "main", .jarray(list(), "java/lang/String"),
argument = command)
The java code in melting/Main.java in the melting5.jar that I am trying to access is as follows.
package melting;
import java.text.NumberFormat;
import melting.configuration.OptionManagement;
import melting.configuration.RegisterMethods;
import melting.methodInterfaces.MeltingComputationMethod;
import melting.nearestNeighborModel.NearestNeighborMode;
/**
* The Melting main class which contains the public static void main(String[] args) method.
*/
public class Main {
// private static methods
/**
* Compute the entropy, enthalpy and the melting temperature and display the results.
* #param args : contains the options entered by the user.
* #param OptionManagement optionManager : the OptionManegement which allows to manage
* the different options entered by the user.
*/
private static ThermoResult runMelting(String [] args, OptionManagement optionManager){
try {
ThermoResult results =
getMeltingResults(args, optionManager);
displaysMeltingResults(results);
return results;
} catch (Exception e) {
OptionManagement.logError(e.getMessage());
return null;
}
}
/**
* Compute the entropy, enthalpy and melting temperature, and return
* these results.
* #param args options (entered by the user) that determine the
* sequence, hybridization type and other features of the
* environment.
* #param optionManager the {#link
* melting.configuration.OptionManagement
* <code>OptionManagement</code>} which
* allows the program to manage the different
* options entered by the user.
* #return The results of the Melting computation.
*/
public static ThermoResult getMeltingResults(String[] args,
OptionManagement optionManager)
{
NumberFormat format = NumberFormat.getInstance();
format.setMaximumFractionDigits(2);
// Set up the environment from the supplied arguments and get the
// results.
Environment environment = optionManager.createEnvironment(args);
RegisterMethods register = new RegisterMethods();
MeltingComputationMethod calculMethod =
register.getMeltingComputationMethod(environment.getOptions());
ThermoResult results = calculMethod.computesThermodynamics();
results.setCalculMethod(calculMethod);
environment.setResult(results);
// Apply corrections to the results.
results = calculMethod.getRegister().
computeOtherMeltingCorrections(environment);
environment.setResult(results);
return environment.getResult();
}
/**
* displays the results of Melting : the computed enthalpy and entropy (in cal/mol and J/mol), and the computed
* melting temperature (in degrees).
* #param results : the ThermoResult containing the computed enthalpy, entropy and
* melting temperature
* #param MeltingComputationMethod calculMethod : the melting computation method (Approximative or nearest neighbor computation)
*/
private static void displaysMeltingResults(ThermoResult results)
{
NumberFormat format = NumberFormat.getInstance();
format.setMaximumFractionDigits(2);
MeltingComputationMethod calculMethod =
results.getCalculMethod();
double enthalpy = results.getEnthalpy();
double entropy = results.getEntropy();
OptionManagement.logInfo("\n The MELTING results are : ");
if (calculMethod instanceof NearestNeighborMode){
OptionManagement.logInfo("Enthalpy : " + format.format(enthalpy) + " cal/mol ( " + format.format(results.getEnergyValueInJ(enthalpy)) + " J /mol)");
OptionManagement.logInfo("Entropy : " + format.format(entropy) + " cal/mol-K ( " + format.format(results.getEnergyValueInJ(entropy)) + " J /mol-K)");
}
OptionManagement.logInfo("Melting temperature : " + format.format(results.getTm()) + " degrees C.\n");
}
// public static main method
/**
* #param args : contains the options entered by the user.
*/
public static void main(String[] args) {
OptionManagement optionManager = new OptionManagement();
if (args.length == 0){
optionManager.initialiseLogger();
optionManager.readMeltingHelp();
}
else if (optionManager.isMeltingInformationOption(args)){
try {
optionManager.readOptions(args);
} catch (Exception e) {
OptionManagement.logError(e.getMessage());
}
}
else {
runMelting(args, optionManager);
}
}
}
How to pass arguments in command to public static void main in java jar?
Over at https://github.com/hrbrmstr/melting5jars I made a pkg wrapper for the MELTING 5 jar (melting5.jar) and also put the Data/ directory in it so you don't have to deal with jar-file management. It can be installed via devtools::install_github("hrbrmstr/melting5jars"),
BEFORE you load that library, you need to set the NN_PATH since the Data/ dir is not where the jar expects it to be by default and you may run into issues setting it afterwards (YMMV).
NOTE: I don't work with this Java library and am not in your field, so please double check the results with the command-line you're used to running!
So, the first things to do to try to get this to work are:
Sys.setenv("NN_PATH"=system.file("extdata", "Data", package="melting5jars"))
library(melting5jars) # devtools::install_github("hrbrmstr/melting5jars")
Now, one of the cooler parts of rJava is that you get to work in R (code) if you want to vs Java (code). We can recreate the core parts of that Main class right in R.
First, get a new melting.Main object and a new OptionManagement object just like the Java code does:
melting <- new(J("melting.Main"))
optionManager <- new(J("melting.configuration.OptionManagement"))
Next, we setup your options. I left Sodium the way it is just to ensure I didn't mess anything up.
Sodium <- 0.05
opts <- c(
"-S", "GTCGTATCCAGTGCAGGGTCCGAGGTATTCGCACTGGATACGACTTCCAC",
"-H", "dnadna",
"-P", 5e-8,
"-E", paste("Na=", Sodium, sep = "")
)
Now, we can call getMeltingResults() from that Main class directly:
results <- melting$getMeltingResults(opts, optionManager)
and then perform the same calls on those results:
calculMethod <- results$getCalculMethod()
enthalpy <- results$getEnthalpy()
entropy <- results$getEntropy()
if (.jinstanceof(calculMethod, J("melting.nearestNeighborModel.NearestNeighborMode"))) {
enthalpy <- results$getEnergyValueInJ(enthalpy)
entropy <- results$getEnergyValueInJ(entropy)
}
melting_temperature <- results$getTm()
enthalpy
## [1] -1705440
entropy
## [1] -4566.232
melting_temperature
## [1] 72.04301
We can wrap all that up into a function that will make it easier to call in the future:
get_melting_results <- function(opts = c()) {
stopifnot(length(opts) > 2) # a sanity check that could be improved
Sys.setenv("NN_PATH"=system.file("extdata", "Data", package="melting5jars"))
require(melting5jars)
melting <- new(J("melting.Main"))
optionManager <- new(J("melting.configuration.OptionManagement"))
results <- melting$getMeltingResults(opts, optionManager)
calculMethod <- results$getCalculMethod()
enthalpy_cal <- results$getEnthalpy()
entropy_cal <- results$getEntropy()
enthalpy_J <- entropy_J <- NULL
if (.jinstanceof(calculMethod, J("melting.nearestNeighborModel.NearestNeighborMode"))) {
enthalpy_J <- results$getEnergyValueInJ(enthalpy_cal)
entropy_J <- results$getEnergyValueInJ(entropy_cal)
}
melting_temp_C <- results$getTm()
list(
enthalpy_cal = enthalpy_cal,
entropy_cal = entropy_cal,
enthalpy_J = enthalpy_J,
entropy_J = entropy_J,
melting_temp_C = melting_temp_C
) -> out
class(out) <- c("melting_res")
out
}
That also has separate values for enthalpy and entropy depending on the method result.
We can also make a print helper function since we classed the list() we're returning:
print.melting_res <- function(x, ...) {
cat(
"The MELTING results are:\n\n",
" - Enthalpy: ", prettyNum(x$enthalpy_cal), " cal/mol",
{if (!is.null(x$enthalpy_J)) paste0(" (", prettyNum(x$enthalpy_J), " J /mol)", collapse="") else ""}, "\n",
" - Entropy: ", prettyNum(x$entropy_cal), " cal/mol-K",
{if (!is.null(x$entropy_J)) paste0(" (", prettyNum(x$entropy_J), " J /mol-K)", collapse="") else ""}, "\n",
" - Meltng temperature: ", prettyNum(x$melting_temp_C), " degress C\n",
sep=""
)
}
(I made an assumption you're used to seeing the MELTING 5 command line output)
And, finally, re-run the computation:
Sodium <- 0.05
opts <- c(
"-S", "GTCGTATCCAGTGCAGGGTCCGAGGTATTCGCACTGGATACGACTTCCAC",
"-H", "dnadna",
"-P", 5e-8,
"-E", paste("Na=", Sodium, sep = "")
)
res <- get_melting_results(opts)
res
## The MELTING results are:
##
## - Enthalpy: -408000 cal/mol (-1705440 J /mol)
## - Entropy: -1092.4 cal/mol-K (-4566.232 J /mol-K)
## - Meltng temperature: 72.04301 degress C
str(res)
## List of 5
## $ enthalpy_cal : num -408000
## $ entropy_cal : num -1092
## $ enthalpy_J : num -1705440
## $ entropy_J : num -4566
## $ melting_temp_C: num 72
## - attr(*, "class")= chr "melting_res"
You should be able to use the above methodology to wrap other components (if any) in the MELTING library.
I would like to perform a 10 fold cross validation on my data and I used the weka java programme. However, I encountered exception problems.
Here is the exceptions:
---Registering Weka Editors---
Trying to add database driver (JDBC): jdbc.idbDriver - Error, not in CLASSPATH?
Exception in thread "main" java.lang.IllegalArgumentException: No suitable converter found for ''!
at weka.core.converters.ConverterUtils$DataSource.<init>(ConverterUtils.java:137)
at weka.core.converters.ConverterUtils$DataSource.read(ConverterUtils.java:441)
at crossvalidationmultipleruns.CrossValidationMultipleRuns.main(CrossValidationMultipleRuns.java:45)
C:\Users\TomXavier\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 1 second)
Here is the programme I used:
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;
import weka.core.Utils;
import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import java.util.Random;
/**
* Performs a single run of cross-validation.
*
* Command-line parameters:
* <ul>
* <li>-t filename - the dataset to use</li>
* <li>-x int - the number of folds to use</li>
* <li>-s int - the seed for the random number generator</li>
* <li>-c int - the class index, "first" and "last" are accepted as well;
* "last" is used by default</li>
* <li>-W classifier - classname and options, enclosed by double quotes;
* the classifier to cross-validate</li>
* </ul>
*
* Example command-line:
* <pre>
* java CrossValidationSingleRun -t anneal.arff -c last -x 10 -s 1 -W "weka.classifiers.trees.J48 -C 0.25"
* </pre>
*
* #author FracPete (fracpete at waikato dot ac dot nz)
*/
public class CrossValidationSingleRun {
/**
* Performs the cross-validation. See Javadoc of class for information
* on command-line parameters.
*
* #param args the command-line parameters
* #throws Excecption if something goes wrong
*/
public static void main(String[] args) throws Exception {
// loads data and set class index
Instances data = DataSource.read(Utils.getOption("C:/Users/TomXavier/Documents/MATLAB/total_data.arff", args));
String clsIndex = Utils.getOption("first", args);
if (clsIndex.length() == 0)
clsIndex = "last";
if (clsIndex.equals("first"))
data.setClassIndex(0);
else if (clsIndex.equals("last"))
data.setClassIndex(data.numAttributes() - 1);
else
data.setClassIndex(Integer.parseInt(clsIndex) - 1);
// classifier
String[] tmpOptions;
String classname;
tmpOptions = Utils.splitOptions(Utils.getOption("weka.classifiers.trees.J48", args));
classname = tmpOptions[0];
tmpOptions[0] = "";
Classifier cls = (Classifier) Utils.forName(Classifier.class, classname, tmpOptions);
// other options
int seed = Integer.parseInt(Utils.getOption("1", args));
int folds = Integer.parseInt(Utils.getOption("10", args));
// randomize data
Random rand = new Random(seed);
Instances randData = new Instances(data);
randData.randomize(rand);
if (randData.classAttribute().isNominal())
randData.stratify(folds);
// perform cross-validation
Evaluation eval = new Evaluation(randData);
for (int n = 0; n < folds; n++) {
Instances train = randData.trainCV(folds, n);
Instances test = randData.testCV(folds, n);
// the above code is used by the StratifiedRemoveFolds filter, the
// code below by the Explorer/Experimenter:
// Instances train = randData.trainCV(folds, n, rand);
// build and evaluate classifier
Classifier clsCopy = Classifier.makeCopy(cls);
clsCopy.buildClassifier(train);
eval.evaluateModel(clsCopy, test);
}
// output evaluation
System.out.println();
System.out.println("=== Setup ===");
System.out.println("Classifier: " + cls.getClass().getName() + " " + Utils.joinOptions(cls.getOptions()));
System.out.println("Dataset: " + data.relationName());
System.out.println("Folds: " + folds);
System.out.println("Seed: " + seed);
System.out.println();
System.out.println(eval.toSummaryString("=== " + folds + "-fold Cross-validation ===", false));
}
}
Is there any solution for this problem?
Many thanks!
/*
* DynamicJasper: A library for creating reports dynamically by specifying
* columns, groups, styles, etc. at runtime. It also saves a lot of development
* time in many cases! (http://sourceforge.net/projects/dynamicjasper)
*
* Copyright (C) 2008 FDV Solutions (http://www.fdvsolutions.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
*
* License as published by the Free Software Foundation; either
*
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
*
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
*
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
*
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*
*/
package ar.com.fdvs.dj.test;
import java.sql.*;
import java.awt.Color;
import java.util.Date;
import java.util.Locale;
import net.sf.jasperreports.view.*;
import ar.com.fdvs.dj.domain.AutoText;
import ar.com.fdvs.dj.domain.DynamicReport;
import ar.com.fdvs.dj.domain.Style;
import ar.com.fdvs.dj.domain.builders.FastReportBuilder;
import ar.com.fdvs.dj.domain.builders.StyleBuilder;
import ar.com.fdvs.dj.domain.constants.Font;
import ar.com.fdvs.dj.core.DJConstants;
// import ar.com.fdvs.dj.test.*;
public class Main extends BaseDjReportTest {
public DynamicReport buildReport() throws Exception {
// Connection C = new Connection();
// C.Con();
CConnection C= new CConnection();
C.Connection();
Statement stmt;
ResultSet rs = null;
String SQL = "SELECT * FROM student";
stmt = C.Con().createStatement();
rs = stmt.executeQuery(SQL);
String res= "";
FastReportBuilder drb = new FastReportBuilder();
drb.setQuery(SQL, DJConstants.QUERY_LANGUAGE_SQL);
while (rs.next()){
res= rs.getString("Name");
**drb.addColumn("Name","Name", String.class.getName(),30);**
// drb.addc
}
//.addColumn("Branch", "branch", String.class.getName(),30)
// .addColumn("Item", "item", String.class.getName(),50)
// .addColumn("Item Code", "id", Long.class.getName(),30,true)
// .addColumn("Quantity", "quantity", Long.class.getName(),60,true)
// .addColumn("Amount", "amount", Float.class.getName(),70,true)
drb.addGroups(2);
DynamicReport sa =drb.build();
drb.setSubtitle("This report was generated at " + new Date())
.setTemplateFile("templates/TemplateReportTest.jrxml")
.setUseFullPageWidth(true);
Style atStyle = new StyleBuilder(true).setFont(Font.COMIC_SANS_SMALL).setTextColor(Color.red).build();
Style atStyle2 = new StyleBuilder(true).setFont(new Font(9, Font._FONT_TIMES_NEW_ROMAN, false, true, false)).setTextColor(Color.BLUE).build();
/***
* Adding many autotexts in the same position (header/footer and aligment) makes them to be one on top of the other
*/
//First add in the FOOTER
drb.addAutoText(AutoText.AUTOTEXT_PAGE_X, AutoText.POSITION_HEADER, AutoText.ALIGNMENT_LEFT,200,40, atStyle);
drb.addAutoText("Autotext below Page counter", AutoText.POSITION_FOOTER, AutoText.ALIGNMENT_LEFT);
//Note the styled text: <b>msimone</b>, valid tags are: <b>, <i> and <u>
drb.addAutoText("Created by <b>msimone</b>", AutoText.POSITION_FOOTER, AutoText.ALIGNMENT_RIGHT,200);
drb.addAutoText(AutoText.AUTOTEXT_PAGE_X_SLASH_Y, AutoText.POSITION_FOOTER, AutoText.ALIGNMENT_RIGHT,30,30,atStyle2);
drb.addAutoText(AutoText.AUTOTEXT_CREATED_ON, AutoText.POSITION_FOOTER, AutoText.ALIGNMENT_LEFT,AutoText.PATTERN_DATE_DATE_TIME);
//Now in HEADER
drb.addAutoText(AutoText.AUTOTEXT_PAGE_X_OF_Y, AutoText.POSITION_HEADER, AutoText.ALIGNMENT_LEFT,100,40);
drb.addAutoText("Autotext at top-left", AutoText.POSITION_HEADER, AutoText.ALIGNMENT_LEFT,200);
drb.addAutoText("Autotext at top-left (2)", AutoText.POSITION_HEADER, AutoText.ALIGNMENT_LEFT,200);
drb.addAutoText("Autotext at top-center", AutoText.POSITION_HEADER, AutoText.ALIGNMENT_CENTER,200,atStyle);
// DynamicReport dr = drb.build();
//i18N, you can set a Locale, different tha n the default in the VM
drb.setReportLocale(new Locale("es","AR"));
drb.setReportLocale(new Locale("pt","BR"));
drb.setReportLocale(new Locale("fr","FR"));
return sa;
}
public static void main(String[] args) throws Exception {
**Main test = new Main();
test.testReport();**
JasperViewer.viewReport(test.jp);
JasperDesignViewer.viewReportDesign(test.jr);
//JasperDesignViewer.viewReportDesign(DynamicJasperHelper.generateJasperReport(test.dr, test.getLayoutManager(),new HashMap()));
}
}
Writing this code i m getting following exception and i really cant figure out the reason
Exception in thread "main" net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : varchar at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:123)
at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getFieldValue(JRAbstractBeanDataSource.java:96)
at net.sf.jasperreports.engine.data.JRBeanCollectionDataSource.getFieldValue(JRBeanCollectionDataSource.java:100)
at net.sf.jasperreports.engine.fill.JRFillDataset.setOldValues(JRFillDataset.java:818)
at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:782)
at net.sf.jasperreports.engine.fill.JRBaseFiller.next(JRBaseFiller.java:1448)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:108)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:923)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:845)
at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:85)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:624)
at ar.com.fdvs.dj.test.BaseDjReportTest.testReport(BaseDjReportTest.java:93)
at ar.com.fdvs.dj.test.Main.main(Main.java:121)
Caused by: java.lang.NoSuchMethodException: Unknown property 'varchar' on class 'class ar.com.fdvs.dj.test.domain.Product'
at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1322)
at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:770)
at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:846)
at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:426)
at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:111)
... 12 more
Java Result: 1
You may be aware of this already, but it appears that somewhere, the code is trying to access a property called "varchar" on the Product object:
Caused by: java.lang.NoSuchMethodException: Unknown property 'varchar' on class 'class ar.com.fdvs.dj.test.domain.Product' at .....
I don't see where this is happening in your example. It may be that there is a place where name is expected and you are instead passing in type. Or somewhere in your configuration, you've got name and type switched around, so it's looking for a field named "varchar". Does that make sense?
In general, I find that the "Caused by" portion of the error log is the most informative.