package ever.workflowRepresentation; import java.util.LinkedList; import java.util.Random; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; import ever.pipeline.Sentence; /** * This class represents the sequence. It contains a linked list which contains ether Tasks, AND or XORs. * @author Pol Schumacher, Wirtschaftsinformatik, Institut fuer Informatik, Goethe Universitaet Frankfurt * */ public class Sequence extends WorkflowElement { /** * Contains all elements of the sequence. They might be of type And, Xor or * Task. */ private LinkedList elements; /** * Initializes the list with sequence elements */ public Sequence(WorkflowElement parent,Sentence s) { super(parent,s); elements = new LinkedList(); } /** * Adds a workflow element at the end of the list. * ho * @param workflowElement */ public void addWfElement(WorkflowElement wfe) { elements.addLast(wfe); } /** * Get Element which is at position "pos" in the sequence * @param pos position of element * @return */ public WorkflowElement getElementAt(int pos) { return elements.get(pos); } /** * Returns the linkedList of workflow elements of the sequence. * @return */ public LinkedList getAllElements() { return elements; } public void putAllElements(LinkedList elem) { elements=elem; } /** * Puts Element elem at position pos in the sequence * @param elem * @param pos */ public void putElementAt(WorkflowElement elem, int pos) { try { elements.add(pos, elem); } catch (Exception e) { // TODO Auto-generated catch block System.err.println(elem.relatedSentence.getRaw()); } } /** * Returns the index of the workflow element in the sequence * @param elem * @return index of workflow element */ public int indexOf(WorkflowElement elem) { return elements.indexOf(elem); } /** * Retrieves and removes the last workflowelement from the list. * * @return workflowElement */ public WorkflowElement popLast() { return elements.pollLast(); } /** * Gets the last element of the list without removing it. * * @return workflowElement */ public WorkflowElement getLast() { return elements.getLast(); } /** * Remove element wfe from sequence */ public void removeWfe(WorkflowElement wfe) { elements.remove(wfe); } /** * Writes all workflowelements of the sequence to output-format. * * @param writer * stream-writer that is used to write out */ public void exportWf2Xml(XMLStreamWriter writer) { try { WorkflowElement last = null; writer.writeStartElement("rwfl:Sequence"); writer.writeAttribute("refID", this.getId() + ""); writer.writeAttribute("status", "READY"); for (WorkflowElement elem : elements) { if (last == null) { try { elem.exportWf2Xml(writer); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { if (!last.equals(elem)) elem.exportWf2Xml(writer); } last = elem; } writer.writeEndElement(); } catch (XMLStreamException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * Writes all workflowelements of the sequence to output-format. Special * handling for condition. * * @param writer * stream-writer that is used to write out */ public void exportWf2Xml(XMLStreamWriter writer, String cond) { try { WorkflowElement last = null; writer.writeStartElement("rwfl:Sequence"); writer.writeAttribute("refID", this.getId() + ""); writer.writeAttribute("status", "READY"); writer.writeAttribute("condition",cond); for (WorkflowElement elem : elements) { if (last == null) { elem.exportWf2Xml(writer); } else { if (!last.equals(elem)) elem.exportWf2Xml(writer); } last = elem; } writer.writeEndElement(); } catch (XMLStreamException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // // /** // * Writes all workflowelements of the sequence to output-format. Special // * handling artificially created workflows. // * // * @param writer // * stream-writer that is used to write out // */ // public void exportWf2XmlArti(XMLStreamWriter writer) { // try { // WorkflowElement last = null; // writer.writeStartElement("rwfl:Sequence"); // writer.writeAttribute("refID", this.getId() + ""); // writer.writeAttribute("status", "READY"); // for (WorkflowElement elem : elements) { // if (last == null) { // elem.exportWf2XmlArti(writer); // // } else { // if (!last.equals(elem)) // elem.exportWf2XmlArti(writer); // } // last = elem; // } // writer.writeEndElement(); // } catch (XMLStreamException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // // } @Override public int hashCode() { final int prime = 31; int result = super.hashCode(); result = prime * result + ((elements == null) ? 0 : elements.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (!super.equals(obj)) return false; if (getClass() != obj.getClass()) return false; Sequence other = (Sequence) obj; if (elements == null) { if (other.elements != null) return false; } else if (!elements.equals(other.elements)) return false; return true; } }