/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2003 Danet GmbH (www.danet.de), GS-AN.
* All rights reserved.
*
* 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.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: XPDLGenerator.java 2345 2007-04-05 12:46:37Z drmlipp $
*
* $Log$
* Revision 1.3 2007/03/27 21:59:44 mlipp
* Fixed lots of checkstyle warnings.
*
* Revision 1.2 2006/09/29 12:32:13 drmlipp
* Consistently using WfMOpen as projct name now.
*
* Revision 1.1.1.1 2003/06/30 20:07:00 drmlipp
* Initial import
*
* Revision 1.4 2003/06/27 09:44:13 lipp
* Fixed copyright/license information.
*
* Revision 1.3 2003/04/24 20:51:21 lipp
* Fixed some warnings.
*
* Revision 1.2 2003/04/16 19:25:03 lipp
* Adapted to jdk 1.4
*
* Revision 1.1 2002/11/25 14:36:22 huaiyang
* initial.
*
* Revision 1.4 2002/11/25 14:11:35 huaiyang
* Call xpdl generator with given parameter.
*
* Revision 1.3 2002/11/21 16:01:43 huaiyang
* check in because of breaking up.
*
* Revision 1.2 2002/11/21 15:09:06 huaiyang
* Add generating transitions.
*
* Revision 1.1 2002/11/21 13:41:51 huaiyang
* testfile used for XPDLGenerator.
*
*
*/
package xpdlgen;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.text.SimpleDateFormat;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import de.danet.an.workflow.util.XPDLUtil;
/**
* The <code>XPDLGenerator</code> provides functions for generating XPDL
*/
public class XPDLGenerator {
private static SimpleDateFormat toDateTimeFormatter = null;
// XPDLGenerator Object
private static XPDLGenerator xpdlGen = null;
// depth
private int depth;
// joinMode
private String joinMode;
// output directory
private String outputPath;
// XPDL JDOM Element
private Element xpdlElement = null;
// namespace
private Namespace ns;
/**
* Generate a valid process definition.
* @param depth the depth of the nested activities of a process
* @param joinMode the mode of the join type.
* @param outputPath the output path of the generated xpdl file.
*/
public XPDLGenerator (int depth, String joinMode, String outputPath)
throws Exception {
this.depth = depth;
this.joinMode = joinMode;
this.outputPath = outputPath;
start();
}
private void start() throws JDOMException, IOException {
ns = Namespace.getNamespace(XPDLUtil.XPDL_NS);
xpdlElement = new Element("Package");
createPackageHeader(xpdlElement);
createWorkflowProcess(xpdlElement);
generateXPDL(xpdlElement);
}
private void createPackageHeader(Element xpdlElem) throws JDOMException {
// create package element
String xmlns_xpdl = "http://www.wfmc.org/2002/XPDL1.0";
String xmlns_xsi = "http://www.w3.org/2001/XMLSchema-instance";
String xmlns_vx = "http://www.an.danet.de/2002/XPDL-Extensions1.0";
String xsi_schemaLocation
= "http://www.wfmc.org/2002/XPDL1.0 http://www.wfmc.org/standards/docs/xpdl.xsd";
String packageId = "xpdl";
String packageName = "generatedXPDL";
xpdlElem.addNamespaceDeclaration(ns);
Namespace xpdlns = Namespace.getNamespace("xpdl", xmlns_xpdl);
xpdlElem.addNamespaceDeclaration(xpdlns);
Namespace vxns = Namespace.getNamespace("vx", xmlns_vx);
xpdlElem.addNamespaceDeclaration(vxns);
Namespace xsins = Namespace.getNamespace("xsi", xmlns_xsi);
xpdlElem.setAttribute("schemaLocation", xsi_schemaLocation, xsins);
xpdlElem.setAttribute("Id", packageId);
xpdlElem.setAttribute("Name", packageName);
// create package header
Element packageHeader = new Element("PackageHeader", ns);
Element xpdlVersion = new Element("XPDLVersion", ns);
xpdlVersion.addContent(XPDLUtil.XPDL_VERSION);
packageHeader.addContent(xpdlVersion);
Element vendor = new Element("Vendor", ns);
vendor.addContent("Danet GmbH, GS AN");
packageHeader.addContent(vendor);
Element created = new Element("Created", ns);
Date createDate = new Date();
created.addContent(createDate.toString());
packageHeader.addContent(created);
xpdlElem.addContent(packageHeader);
}
private void createWorkflowProcess(Element xpdlElem) throws JDOMException {
// create header of workflow process
Element workflowProcesses = new Element("WorkflowProcesses", ns);
Element workflowProcess = new Element("WorkflowProcess", ns);
workflowProcess.setAttribute("Id", "1");
workflowProcess.setAttribute("Name", "1");
Element processHeader = new Element("ProcessHeader",ns);
workflowProcess.addContent(processHeader);
// createActivities
createActivities(workflowProcess);
// createTransitions
createTransitions(workflowProcess);
workflowProcesses.addContent(workflowProcess);
xpdlElement.addContent(workflowProcesses);
}
private void generateXPDL(Element xpdlElem)
throws JDOMException, IOException {
FileWriter writer = new FileWriter(outputPath);
//StringWriter writer = new StringWriter();
Format format = Format.getPrettyFormat();
format.setLineSeparator("\n");
format.setEncoding("ISO-8859-1");
XMLOutputter outputter = new XMLOutputter(format);
Document doc = new Document(xpdlElem);
outputter.output(doc, writer);
//System.out.println(writer.toString());
writer.close();
}
private void createActivities(Element wfProc) throws JDOMException {
Element activities = new Element("Activities", ns);
wfProc.addContent(activities);
for (int i = 1; i <= depth ; i++) {
for (int j = 1; j <= i; j++) {
if ((i+j) > (depth +1)) {
break;
}
String id = Integer.toString(i) + Integer.toString(j);
Element activity = createActivity(id);
activities.addContent(activity);
if (i == depth) {
break;
}
}
}
}
private Element createActivity(String id) throws JDOMException {
Element activity = new Element("Activity", ns);
activity.setAttribute("Id", id);
activity.setAttribute("Name", id);
Element implementation = new Element("Implementation", ns);
implementation.addContent(new Element("No", ns));
activity.addContent(implementation);
Element startmode = new Element("StartMode", ns);
startmode.addContent(new Element("Automatic", ns));
activity.addContent(startmode);
Element finishmode = new Element("FinishMode", ns);
finishmode.addContent(new Element("Automatic", ns));
activity.addContent(finishmode);
Element transRestrics = createTransitionRestrictions();
activity.addContent(transRestrics);
return activity;
}
private Element createTransitionRestrictions() throws JDOMException {
Element transRestrics = new Element("TransitionRestrictions", ns);
Element transRestric = new Element("TransitionRestriction", ns);
// Join Type
Element join = new Element("Join", ns);
join.setAttribute("Type", joinMode);
transRestric.addContent(join);
// Split Type = "AND"
Element split = new Element("Split", ns);
split.setAttribute("Type", "AND");
transRestric.addContent(split);
transRestrics.addContent(transRestric);
return transRestrics;
}
private void createTransitions(Element wfProc) throws JDOMException {
Element trans = new Element("Transitions", ns);
String toId1 = null;
String toId2 = null;
for (int i = 1; i <= depth ; i++) {
for (int j = 1; j <= i; j++) {
if (((i+j) > (depth +1)) || (i == depth)) {
break;
}
String fromId = Integer.toString(i) + Integer.toString(j);
if (( i <= depth/2 ) && ((i + j + 1) < (depth + 1))) {
toId1 = Integer.toString(i + 1) + Integer.toString(j);
if ((i + j + 1) < (depth + 1)) {
toId2 = Integer.toString(i+1) + Integer.toString(j+1);
}
} else {
if ((i + j) < (depth + 1)) {
toId1 = Integer.toString(i+1) + Integer.toString(j);
}
if ((j -1) > 0) {
toId2 = Integer.toString(i+1) + Integer.toString(j-1);
}
}
createTransition(trans, fromId + "To" + toId1, fromId, toId1);
createTransition(trans, fromId + "To" + toId2, fromId, toId2);
toId1 = null;
toId2 = null;
}
}
wfProc.addContent(trans);
}
private void createTransition
(Element transitions, String id, String from, String to)
throws JDOMException {
if ((id == null) || (from == null) || (to == null)) {
return;
}
Element transition = new Element("Transition", ns);
transition.setAttribute("Id", id);
transition.setAttribute("From", from);
transition.setAttribute("To", to);
transitions.addContent(transition);
}
/**
* Main-Methode des XPDLGenerator.
* @param args arguments
* @throws Exception any error occurred.
*/
public static void main(String[] args) throws Exception {
if (args.length != 3) {
System.out.println
("Usage: java de.danet.an.workflow.util.XPDLGenerator "
+ "<depth> <join mode> <output path>");
System.exit(-1);
}
xpdlGen = new XPDLGenerator
(Integer.parseInt(args[0]), args[1], args[2]);
}
}