2ronwalf/*
2ronwalf * Created on Jun 1, 2004
2ronwalf */
2ronwalfpackage org.mindswap.shop;
2ronwalf
2ronwalfimport java.io.IOException;
2ronwalfimport java.io.OutputStream;
2ronwalfimport java.io.OutputStreamWriter;
2ronwalfimport java.io.PrintWriter;
2ronwalfimport java.io.Writer;
2ronwalfimport java.util.ArrayList;
2ronwalfimport java.util.HashMap;
2ronwalfimport java.util.List;
2ronwalfimport java.util.Map;
2ronwalf
2ronwalfimport org.mindswap.owl.OWLIndividual;
2ronwalfimport org.mindswap.owls.process.AtomicProcess;
2ronwalfimport org.mindswap.owls.process.CompositeProcess;
2ronwalfimport org.mindswap.owls.process.ControlConstruct;
2ronwalfimport org.mindswap.owls.process.ControlConstructList;
2ronwalfimport org.mindswap.owls.process.InputList;
2ronwalfimport org.mindswap.owls.process.OutputList;
2ronwalfimport org.mindswap.owls.process.Parameter;
2ronwalfimport org.mindswap.owls.process.Process;
2ronwalfimport org.mindswap.owls.process.Sequence;
2ronwalfimport org.mindswap.owls.service.Service;
2ronwalfimport org.mindswap.utils.QNameProvider;
2ronwalf
2ronwalf
2ronwalf/**
2ronwalf * This is a experimental version of OWL-S to SHOP2 translator.
2ronwalf *
2ronwalf * @author Evren Sirin
2ronwalf */
2ronwalfpublic class SHOPTranslator {
2ronwalf private QNameProvider qnames = new QNameProvider();
2ronwalf
2ronwalf private PrintWriter out;
2ronwalf
2ronwalf private List processes;
7daenzerorama// private Set written;
2ronwalf
2ronwalf private String getSHOPAbbr(OWLIndividual resource) {
2ronwalf String s = qnames.shortForm(resource.getURI());
2ronwalf if(resource instanceof AtomicProcess)
2ronwalf s += "!";
2ronwalf return s;
2ronwalf }
2ronwalf
2ronwalf /* (non-Javadoc)
2ronwalf * @see org.mindswap.owls.io.OWLSWriter#write(org.mindswap.owls.service.Service, java.io.OutputStream)
2ronwalf */
2ronwalf public void write(Service service, OutputStream out) throws IOException {
2ronwalf write(service, new OutputStreamWriter(out, "UTF8"));
2ronwalf }
2ronwalf
2ronwalf /* (non-Javadoc)
2ronwalf * @see org.mindswap.owls.io.OWLSWriter#write(org.mindswap.owls.service.Service, java.io.Writer)
2ronwalf */
2ronwalf public void write(Service service, Writer writer) {
2ronwalf out = new PrintWriter(writer);
7daenzerorama// written = new HashSet();
2ronwalf processes = new ArrayList();
2ronwalf
2ronwalf processes.add(service.getProcess());
2ronwalf for(int i = 0; i < processes.size(); i++)
2ronwalf writeProcess((Process) processes.get(i));
2ronwalf }
2ronwalf
2ronwalf private void writeProcess(Process process) {
2ronwalf if(process instanceof AtomicProcess)
2ronwalf writeAtomicProcess((AtomicProcess) process);
2ronwalf else if(process instanceof CompositeProcess)
2ronwalf writeCompositeProcess((CompositeProcess) process);
2ronwalf }
2ronwalf
2ronwalf private void writeCompositeProcess(CompositeProcess process) {
2ronwalf ControlConstruct construct = process.getComposedOf();
2ronwalf
2ronwalf if(construct instanceof Sequence)
2ronwalf writeSequenceProcess(process);
2ronwalf }
2ronwalf
2ronwalf private Map createValueMap(Process process) {
2ronwalf Map valueMap = new HashMap();
2ronwalf
2ronwalf InputList inputs = process.getInputs();
2ronwalf for(int i = 0; i < inputs.size(); i++) {
2ronwalf Parameter p = inputs.parameterAt(i);
2ronwalf valueMap.put(p, getSHOPAbbr(p));
2ronwalf }
2ronwalf
2ronwalf OutputList outputs = process.getOutputs();
2ronwalf for(int i = 0; i < outputs.size(); i++) {
2ronwalf Parameter p = outputs.parameterAt(i);
2ronwalf valueMap.put(p, getSHOPAbbr(p));
2ronwalf }
2ronwalf
2ronwalf return valueMap;
2ronwalf }
2ronwalf
2ronwalf private void writeTask(Process process){
2ronwalf writeTask(process, new HashMap());
2ronwalf }
2ronwalf
2ronwalf private void writeTask(Process process, Map valueMap){
2ronwalf out.print("(" + getSHOPAbbr(process) + " ");
2ronwalf
2ronwalf InputList inputs = process.getInputs();
2ronwalf for(int i = 0; i < inputs.size(); i++) {
2ronwalf Parameter p = inputs.parameterAt(i);
2ronwalf String mappedValue = (String) valueMap.get(p);
2ronwalf out.print(mappedValue + " ");
2ronwalf }
2ronwalf
2ronwalf OutputList outputs = process.getOutputs();
2ronwalf for(int i = 0; i < outputs.size(); i++) {
2ronwalf Parameter p = outputs.parameterAt(i);
2ronwalf String mappedValue = (String) valueMap.get(p);
2ronwalf out.print(mappedValue + " ");
2ronwalf }
2ronwalf out.println(")");
2ronwalf }
2ronwalf
2ronwalf private void writeSequenceProcess(CompositeProcess process) {
2ronwalf out.print("(:method");
2ronwalf writeTask(process);
2ronwalf writePreconditions(process);
2ronwalf
2ronwalf Map valueMap = createValueMap(process);
2ronwalf out.println("(:ordered");
2ronwalf
2ronwalf Sequence sequence = (Sequence) process.getComposedOf();
2ronwalf ControlConstructList components = sequence.getComponents();
2ronwalf for(int i = 0; i < components.size(); i++){
2ronwalf ControlConstruct p = components.constructAt(i);
2ronwalf if(p instanceof Process)
2ronwalf writeTask((Process) p, valueMap);
2ronwalf else
2ronwalf throw new RuntimeException("Cannot handle nested control constructs inside a sequence!");
2ronwalf }
2ronwalf }
2ronwalf
2ronwalf
2ronwalf private void writeAtomicProcess(AtomicProcess process) {
2ronwalf out.print("(");
2ronwalf writeTask(process);
2ronwalf writePreconditions(process);
2ronwalf writeEffects(process);
2ronwalf }
2ronwalf
2ronwalf private void writePreconditions(Process process) {
2ronwalf out.println("()");
2ronwalf }
2ronwalf
2ronwalf private void writeEffects(Process process) {
2ronwalf out.println("()");
2ronwalf }
2ronwalf}