package ever.pipeline; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.Serializable; import java.util.ArrayList; import javax.xml.transform.stream.StreamSource; import net.sf.saxon.s9api.DocumentBuilder; import net.sf.saxon.s9api.Processor; import net.sf.saxon.s9api.SaxonApiException; import net.sf.saxon.s9api.Serializer; import net.sf.saxon.s9api.XQueryCompiler; import net.sf.saxon.s9api.XQueryEvaluator; import net.sf.saxon.s9api.XQueryExecutable; import net.sf.saxon.s9api.XdmNode; import ever.workflowRepresentation.Workflow; import ever.workflowRepresentation.WorkflowElement; /** * A case is the wrapper which holds togehter the different representations of a process description * e.g. the originial text description, the workflow structure, the case frames and clauses * case frames and clauses can be accessed via sentences List * @author Pol Schumacher, Wirtschaftsinformatik, Institut fuer Informatik, Goethe Universitaet Frankfurt * */ public class Case implements Serializable { /** * */ private static final long serialVersionUID = 1; /** * Case id */ private int id; private static int currentId = 0; /** * A list of sentences. A sentence contains the raw text, a list of case frames and list of clauses */ private ArrayList sentenceList = new ArrayList(); /** * String of the xml file which was used as input */ private String xmlFile; /** * The workflow structure */ private Workflow wf; /** * Pipe Id is necessary too produce intermediate steps */ private int pipeId; /** * Pipe Step is necessary too produce intermediate steps */ private int pipeStep; /** * Case name should be the same as input and output file */ private String name; public Case() { this.id = currentId; currentId++; } public void setId(int id) { this.id = id; } public int getId() { return id; } public int getPipeId() { return pipeId; } public void setPipeId(int pipeId) { this.pipeId = pipeId; } public int getPipeStep() { return pipeStep; } public void setPipeStep(int pipeStep) { this.pipeStep = pipeStep; } public void setName(String n) { this.name=n; } /** * Returns the whole xml file as string * @return */ public String getXmlString() { return xmlFile; } /** * Appends a sentence to the sentence list * @param sen */ public void addSentence(Sentence sen) { sentenceList.add(sen); } /** * Replaces the xml string of the input file * @param in */ public void addXmlString(String in) { xmlFile = in; } /** * Returns a list with all sentences * @return */ public ArrayList getSentences() { return sentenceList; } /** * Returns the workflow structure * @return */ public Workflow getWf() { return wf; } public void setWf(Workflow wf) { this.wf = wf; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; result = prime * result + ((sentenceList == null) ? 0 : sentenceList.hashCode()); result = prime * result + ((xmlFile == null) ? 0 : xmlFile.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Case other = (Case) obj; if (id != other.id) return false; if (sentenceList == null) { if (other.sentenceList != null) return false; } else if (!sentenceList.equals(other.sentenceList)) return false; if (xmlFile == null) { if (other.xmlFile != null) return false; } else if (!xmlFile.equals(other.xmlFile)) return false; return true; } /** * Returns name * @return */ public String getName() { // Processor proc = new Processor(false); // String query = "for $i in //title\n" + "return $i"; // String out = ""; // try { // DocumentBuilder newDocumentBuilder = proc.newDocumentBuilder(); // XQueryCompiler newXQueryCompiler = proc.newXQueryCompiler(); // XdmNode doc = null; // XQueryExecutable compile = null; // // try { // // doc = newDocumentBuilder.build(new StreamSource( // new ByteArrayInputStream(xmlFile.getBytes()))); // } catch (net.sf.saxon.s9api.SaxonApiException e) { // xmlFile=xmlFile.replace("& ", ""); // // doc = newDocumentBuilder.build(new StreamSource( // new ByteArrayInputStream(xmlFile.getBytes()))); // // } // compile = newXQueryCompiler.compile(query); // // XQueryEvaluator load = compile.load(); // load.setContextItem(doc); // Serializer serial = new Serializer(); // ByteArrayOutputStream outs; // outs = new ByteArrayOutputStream(); // serial.setOutputStream(outs); // serial.setOutputProperty(Serializer.Property.METHOD, "text"); // serial.setOutputProperty(Serializer.Property.OMIT_XML_DECLARATION, // "yes"); // serial.setOutputProperty(Serializer.Property.ENCODING, "utf-8"); // load.run(serial); // out = outs.toString(); // } catch (SaxonApiException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } return name; } /** * Gets the index of a workflow element of a sentence in sequence. Because one sentence can produce multiple workflow * elements, it is not possible to just map the sentence position to the workflow element posisiton * @param s * @return */ public int getFirstIndexForSentence(Sentence s) { boolean found=false; int pos=0; if(wf.getTopSequence().getAllElements().indexOf(s.getRelatedElem().get(0))>-1) pos=wf.getTopSequence().getAllElements().indexOf(s.getRelatedElem().get(0)); else { for(int i=sentenceList.indexOf(s)-1; i>=0;i--) { for(int j=sentenceList.get(i).getRelatedElem().size()-1;i>0;i--) { if(wf.getTopSequence().getAllElements().indexOf(sentenceList.get(i).getRelatedElem().get(j))>-1) pos=wf.getTopSequence().getAllElements().indexOf(sentenceList.get(i).getRelatedElem().get(j)); } } } return pos; } }