src.openwfe.org.decision.tools.TableTester Maven / Gradle / Ivy
/*
* Copyright (c) 2006, John Mettraux, OpenWFE.org
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* . Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* . Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* . Neither the name of the "OpenWFE" nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* $Id: DecisionException.java 1882 2005-05-17 16:41:07Z jmettraux $
*/
//
// TableTester.java
//
// [email protected]
//
// generated with
// jtmpl 1.1.01 2004/05/19 ([email protected])
//
package openwfe.org.decision.tools;
import openwfe.org.Utils;
import openwfe.org.xml.XmlCoder;
import openwfe.org.engine.workitem.InFlowWorkItem;
import openwfe.org.engine.workitem.XmlCoderLoader;
import openwfe.org.engine.impl.workitem.xml.XmlWorkItemCoder;
import openwfe.org.decision.DecisionTable;
import openwfe.org.decision.impl.CsvDecisionTableFactory;
/**
* A command-line utility for testing decision tables without having
* to launch workflows.
*
* CVS Info :
*
$Author$
*
$Id$
*
* @author [email protected]
*/
public class TableTester
{
/*
private final static org.apache.log4j.Logger log = org.apache.log4j.Logger
.getLogger(TableTester.class.getName());
*/
//
// CONSTANTS & co
private static boolean verbose = false;
//
// kinky global var
//
// FIELDS
//
// CONSTRUCTORS
//
// METHODS
//
// STATIC METHODS
private static InFlowWorkItem loadWorkitem (final String filename)
throws Exception
{
InFlowWorkItem wi = null;
// "naechster halt : Olten"
try
{
wi = (InFlowWorkItem)XmlCoder.load(filename);
}
catch (final Throwable t)
{
if (verbose)
{
System.err.println
("failed to use the XmlCoder to decode workitem at "+
filename);
System.err.println
("attempting with the XmlWorkItemCoder...");
}
}
if (wi != null) return wi;
final XmlCoderLoader cl = XmlCoderLoader
.buildCoderLoader("etc/engine/coder-configuration.xml");
final XmlWorkItemCoder wic = (XmlWorkItemCoder)cl.getXmlCoder();
final java.net.URL url = new java.net.URL(filename);
return (InFlowWorkItem)wic.decode(url, null, null);
}
private static void log (final String message)
{
if ( ! verbose) return;
System.err.println(message);
}
private static void generateEmptyWorkitem ()
throws Exception
{
final InFlowWorkItem wi = new InFlowWorkItem();
wi.getAttributes().puts("yourkey", "yourvalue");
//wi.getAttributes().puts("ifield0", "a");
//wi.getAttributes().puts("ifield1", "b");
XmlCoder.save("workitem.xml", wi);
log("saved empty workitem in file workitem.xml");
}
private static void printUsage ()
{
System.err.println();
System.err.println(TableTester.class.getName());
System.err.println();
System.err.println(" Testing the decision tables");
System.err.println();
System.err.println(" USAGE");
System.err.println();
System.err.println(" java "+TableTester.class.getName()+" -w {workitem_file} -d {table} [-o {outfile}]");
System.err.println(" to test a workitem against a table");
System.err.println();
System.err.println(" java "+TableTester.class.getName()+" -g");
System.err.println(" to generate an [almost] empty workitem in the file ./workitem.xml");
System.err.println();
System.err.println(" -v : verbose (to stderr)");
System.err.println();
System.err.println();
//System.exit(-1);
//
// it's more explicit when done in the 'client' code
}
public static void main (final String[] args)
throws Exception
{
if (args.length < 1)
{
printUsage();
System.exit(-1);
}
String tablePath = null;
String itemPath = null;
String outFile = null;
boolean generate = false;
int i = 0;
while (i < args.length)
{
String arg = null;
String value = null;
try
{
arg = args[i++];
if (arg.equals("-g"))
{
generate = true;
}
else if (arg.equals("-w"))
{
itemPath = args[i++];
}
else if (arg.equals("-d"))
{
tablePath = args[i++];
}
else if (arg.equals("-o"))
{
outFile = args[i++];
}
else if (arg.equals("-v"))
{
verbose = true;
}
else
{
System.err.println("invalid arg '"+arg+"'");
printUsage();
System.exit(2);
}
}
catch (final ArrayIndexOutOfBoundsException e)
{
System.err.println("missing arg");
printUsage();
System.exit(1);
}
}
if (generate)
{
generateEmptyWorkitem();
System.exit(0);
}
//
// load table
final java.util.Map dtfParams = new java.util.HashMap(0);
final CsvDecisionTableFactory dtf = new CsvDecisionTableFactory();
dtf.init(dtfParams);
log("path to table is "+tablePath);
tablePath = Utils.expandUrl(tablePath);
log("path to table is "+tablePath);
final DecisionTable table = dtf.loadTableDirectly(tablePath);
log("loaded decision table at "+tablePath);
//
// load item
//final InFlowWorkItem wi = (InFlowWorkItem)XmlCoder.load(itemPath);
final InFlowWorkItem wi = loadWorkitem(itemPath);
log("loaded workitem at "+itemPath);
//
// apply table to item
table.apply(wi);
//
// output 'decided' workitem
java.io.OutputStream os = null;
if (outFile.equals("-"))
os = System.out;
else
os = new java.io.FileOutputStream(outFile);
XmlCoder.save(os, wi);
log("saved 'decided' workitem into file "+outFile);
log("done.");
}
}