Coverage Report - org.deri.wsmo4j.io.serializer.wsml.ChoreographySerializerWSML
 
Classes in this File Line Coverage Branch Coverage Complexity
ChoreographySerializerWSML
0%
0/125
0%
0/76
0
 
 1  
 /*
 2  
  wsmo4j extension - a Choreography API and Reference Implementation
 3  
 
 4  
  Copyright (c) 2005, University of Innsbruck, Austria
 5  
 
 6  
  This library is free software; you can redistribute it and/or modify it under
 7  
  the terms of the GNU Lesser General Public License as published by the Free
 8  
  Software Foundation; either version 2.1 of the License, or (at your option)
 9  
  any later version.
 10  
  This library is distributed in the hope that it will be useful, but WITHOUT
 11  
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 12  
  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 13  
  details.
 14  
  You should have received a copy of the GNU Lesser General Public License along
 15  
  with this library; if not, write to the Free Software Foundation, Inc.,
 16  
  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 17  
  */
 18  
 
 19  
 package org.deri.wsmo4j.io.serializer.wsml;
 20  
 
 21  
 import java.io.*;
 22  
 import java.util.*;
 23  
 
 24  
 import org.omwg.logicalexpression.terms.*;
 25  
 import org.wsmo.common.*;
 26  
 import org.wsmo.service.*;
 27  
 import org.wsmo.service.choreography.Choreography;
 28  
 import org.wsmo.service.choreography.rule.*;
 29  
 import org.wsmo.service.rule.*;
 30  
 import org.wsmo.service.signature.*;
 31  
 
 32  
 import com.ontotext.wsmo4j.serializer.wsml.*;
 33  
 
 34  
 /**
 35  
  * Choreography Serializer Class
 36  
  *
 37  
  * <pre>
 38  
  *    Created on Sep 18, 2005
 39  
  *    Committed by $Author: morcen $
 40  
  *    $Source$
 41  
  * </pre>
 42  
  *
 43  
  * @author James Scicluna
 44  
  * @author Thomas Haselwanter
 45  
  *
 46  
  * @version $Revision: 1949 $ $Date: 2007-04-02 16:05:18 +0300 (Mon, 02 Apr 2007) $
 47  
  */
 48  
 public class ChoreographySerializerWSML extends WSMLTextExportHelper {
 49  
 
 50  
     private VisitorSerializeWSMLTransitionRules ruleVisitor;
 51  
 
 52  
     public ChoreographySerializerWSML(Writer writer, TopEntity topEntity) {
 53  0
         super(writer);
 54  0
         if (writer == null || topEntity == null) {
 55  0
             throw new IllegalArgumentException();
 56  
         }
 57  0
         this.writer = writer;
 58  0
         super.visitorSerializeTerm = new VisitorSerializeWSMLTerms(topEntity);
 59  0
         this.ruleVisitor = new VisitorSerializeWSMLTransitionRules(topEntity);
 60  0
     }
 61  
 
 62  
     public void visit(Interface item) {
 63  0
         inInterface(item);
 64  0
         visit((TopEntity)item);
 65  0
         if (item.getChoreography() != null) {
 66  0
             if (item.getChoreography() instanceof org.wsmo.service.choreography.Choreography) {
 67  0
                 helpChoreography((org.wsmo.service.choreography.Choreography)
 68  
                         item.getChoreography());
 69  
             }
 70  
         }
 71  0
         if (item.getOrchestration() != null) {
 72  0
             new WSMLItem(item.getOrchestration()).apply(this);
 73  
         }
 74  0
         outInterface(item);
 75  0
     }
 76  
 
 77  
     /*
 78  
      * Serializes the Choreography Object
 79  
      */
 80  
     public void helpChoreography(Choreography c) {
 81  0
         writeln("").write(indentation).write("choreography ");
 82  0
         write(helpSerializeTerm(c.getIdentifier()));
 83  
         //Non-Functional Properties
 84  0
         visit((Entity) c);
 85  
         //State Signature
 86  0
         if (c.getStateSignature() != null)
 87  0
             helpStateSignature(c.getStateSignature());
 88  
         //Transition Rules
 89  0
         if (c.getRules() != null)
 90  0
             helpTransitionRules(c);
 91  0
     }
 92  
 
 93  
     /*
 94  
      * Serliazises the StateSignature object
 95  
      */
 96  
     private void helpStateSignature(StateSignature sig){
 97  
         //Identifier
 98  0
         writeln("").write(indentation).write("stateSignature ");
 99  0
         write(helpSerializeTerm(sig.getIdentifier()));
 100  
 
 101  
         //Non-Functional Properties
 102  0
         visit(sig);
 103  
 
 104  
         //Imported Ontologies (State)
 105  0
         if (!sig.listOntologies().isEmpty()) {
 106  0
             writeln("").write(indentation).write(indentation).write("importsOntology");
 107  0
             onImportedOntologies(sig.listOntologies());
 108  0
             writeln("");
 109  
         }
 110  
 
 111  
         //In
 112  0
         if (sig.listInModes().size() > 0) {
 113  0
             helpGroundedMode(sig.listInModes(),"in");
 114  
         }
 115  
 
 116  
         //Out
 117  0
         if (sig.listOutModes().size() > 0) {
 118  0
             helpGroundedMode(sig.listOutModes(),"out");
 119  
         }
 120  
 
 121  
         //Shared
 122  0
         if (sig.listSharedModes().size() > 0) {
 123  0
             helpGroundedMode(sig.listSharedModes(),"shared");
 124  
         }
 125  
 
 126  
         //Controlled
 127  0
         if (sig.listControlledModes().size() > 0)
 128  0
             helpUnGroundedMode(sig.listControlledModes(),"controlled");
 129  
 
 130  
         //Static
 131  0
         if (sig.listStaticModes().size() > 0) {
 132  0
             helpUnGroundedMode(sig.listStaticModes(), "static");
 133  
         }
 134  0
     }
 135  
 
 136  
     /*
 137  
      * Serializes the Grounded Modes
 138  
      *
 139  
      * TODO: Exception throwing checking Not-Grounded Modes
 140  
      */
 141  
     private void helpGroundedMode(Set modes, String mName) {
 142  0
         writeln("").write(indentation).write(indentation);
 143  0
         write(mName).write(" ");
 144  0
         if (modes != null) {
 145  0
             if (modes.size() > 0) {
 146  0
                 Iterator i = modes.iterator();
 147  
                 GroundedMode m;
 148  0
                 while (i.hasNext()) {
 149  0
                     m = (GroundedMode) i.next();
 150  0
                     if(m.isConcept()) {
 151  0
                         writeln("").write(indentation).write(indentation);
 152  0
                         write(indentation + "concept ");
 153  0
                         write(helpSerializeTerm(m.getConcept().getIdentifier()));
 154  
                     }
 155  
                     else {
 156  0
                         writeln("").write(indentation).write(indentation);
 157  0
                         write(indentation + "relation ");
 158  0
                         write(helpSerializeTerm(m.getRelation().getIdentifier()));
 159  
                     }
 160  
                     try {
 161  0
                         helpGroundingList(m.getGrounding());
 162  
                     }
 163  0
                     catch (NotGroundedException ex) {
 164  0
                     }
 165  
                     
 166  0
                     if (i.hasNext())
 167  0
                         write(", ");
 168  
                 }
 169  
             }
 170  
         }
 171  0
         writeln("");
 172  0
     }
 173  
 
 174  
     /*
 175  
      * Serializes the Grounding List
 176  
      */
 177  
     private void helpGroundingList(Set<Grounding> grounding) {
 178  0
         if (grounding != null) {
 179  0
             if (grounding.size() > 0) {
 180  0
                 write(" withGrounding ");
 181  0
                 if (grounding.size() > 1)
 182  0
                     write("{");
 183  0
                 Iterator<Grounding> i = grounding.iterator();
 184  0
                 while (i.hasNext()) {
 185  0
                     helpGroundingEntry(i.next());
 186  0
                     if (i.hasNext())
 187  0
                         write(",");
 188  
                 }
 189  0
                 if (grounding.size() > 1)
 190  0
                     write("}");
 191  
             }
 192  
         }
 193  0
     }
 194  
 
 195  
     /*
 196  
      * Checks the different types of grounding and serializes accordingly
 197  
      */
 198  
     private void helpGroundingEntry(Grounding g) {
 199  0
         if (g instanceof WSDLGrounding) {
 200  0
             WSDLGrounding wsdlG = (WSDLGrounding) g;
 201  0
             write(helpSerializeTerm(wsdlG.getIRI()));
 202  
         }
 203  0
     }
 204  
 
 205  
     /*
 206  
      * Returns a string representation of the concept identifiers of the
 207  
      * Ungrounded Modes Static and Controlled
 208  
      */
 209  
     private void helpUnGroundedMode(Set modes, String mName) {
 210  0
         writeln("").write(indentation).write(indentation);
 211  0
         write(mName).write(" ");
 212  0
         if (modes != null) {
 213  0
             Iterator i = modes.iterator();
 214  0
             while (i.hasNext()) {
 215  0
                 Mode m = (Mode) i.next();
 216  0
                 if(m.isConcept()) {
 217  0
                     writeln("").write(indentation).write(indentation);
 218  0
                     write(indentation + "concept ");
 219  0
                     write(helpSerializeTerm(m.getConcept().getIdentifier()));
 220  
                 }else {
 221  0
                     writeln("").write(indentation).write(indentation);
 222  0
                     write(indentation + "relation ");
 223  0
                     write(helpSerializeTerm(m.getRelation().getIdentifier()));
 224  
                 }
 225  0
                 if (i.hasNext())
 226  0
                     write(",");
 227  0
             }
 228  
         }
 229  0
         write(" ");
 230  0
     }
 231  
 
 232  
     /*
 233  
      * Serializes the transition rules
 234  
      */
 235  
     private void helpTransitionRules(Choreography c){
 236  0
         ChoreographyRules rules = c.getRules();
 237  0
         if (rules == null) {
 238  0
             return;
 239  
         }
 240  0
         writeln("").write(indentation).write("transitionRules ").writeln(helpSerializeTerm(rules.getIdentifier()));
 241  0
         if (rules.listRules().size() == 0) {
 242  0
             return;
 243  
         }
 244  
         
 245  0
         Iterator <ChoreographyRule> i = rules.listRules().iterator();
 246  0
         ChoreographyRule r = null;
 247  0
         while(i.hasNext()){
 248  0
             r = i.next();
 249  0
             if(r instanceof ChoreographyIfThen) this.ruleVisitor.visitChoreographyIfThen((ChoreographyIfThen)r);
 250  0
             else if(r instanceof ChoreographyForAll) this.ruleVisitor.visitChoreographyForAll((ChoreographyForAll)r);
 251  0
             else if(r instanceof ChoreographyChoose) this.ruleVisitor.visitChoreographyChoose((ChoreographyChoose)r);
 252  0
             else if(r instanceof Add) this.ruleVisitor.visitAdd((Add)r);
 253  0
             else if(r instanceof Delete) this.ruleVisitor.visitDelete((Delete)r);
 254  0
             else if(r instanceof Update) this.ruleVisitor.visitUpdate((Update)r);
 255  0
             writeln(this.ruleVisitor.getSerializedObject());
 256  
         }
 257  0
     }
 258  
     
 259  
     protected String helpSerializeTerm(Term term){
 260  0
         term.accept(visitorSerializeTerm);
 261  0
         return visitorSerializeTerm.getSerializedObject();
 262  
     }
 263  
     
 264  
     protected ChoreographySerializerWSML write(String str) {
 265  
         try {
 266  0
             writer.write(str);
 267  
         }
 268  0
         catch (IOException ioe) {
 269  0
             throw new RuntimeException("while writing", ioe);
 270  0
         }
 271  0
         return this;
 272  
     }
 273  
     
 274  
     protected ChoreographySerializerWSML writeln(String str) {
 275  0
         return write(str).write("\n");
 276  
     }
 277  
     
 278  0
     protected final String indentation = "     ";
 279  
 }
 280  
 
 281  
 /*
 282  
  * $Log$
 283  
  * Revision 1.22  2007/04/02 13:05:18  morcen
 284  
  * Generics support added to wsmo-ext
 285  
  *
 286  
  * Revision 1.21  2006/10/24 14:11:47  vassil_momtchev
 287  
  * choreography/orchestration rules refactored. different types where appropriate now supported
 288  
  *
 289  
  * Revision 1.20  2006/04/17 10:52:11  vassil_momtchev
 290  
  * serialization of Choreography getRules() == null or getStateSignature == null is now allowed (skipped in wsml)
 291  
  *
 292  
  * Revision 1.19  2006/03/01 15:22:15  vassil_momtchev
 293  
  * all modes can be serialized with no grounding information
 294  
  *
 295  
  * Revision 1.18  2006/02/21 09:05:02  vassil_momtchev
 296  
  * serialization formatting improved
 297  
  *
 298  
  * Revision 1.17  2006/02/14 15:25:15  alex_simov
 299  
  * output text indentation improved for StateSignature modes
 300  
  *
 301  
  * Revision 1.16  2006/02/14 10:59:04  vassil_momtchev
 302  
  * choreography that do not define rules container or rules is allowed to be serialized
 303  
  *
 304  
  * Revision 1.15  2006/02/03 13:32:49  jamsci001
 305  
  * - Serializer reflects changes in API and implementation:
 306  
  *  - PipedRules
 307  
  *  - Relation Facts
 308  
  *  - Updates
 309  
  *
 310  
  * Revision 1.14  2006/02/01 15:26:25  vassil_momtchev
 311  
  * dublicated fields with the super class removed
 312  
  *
 313  
  * Revision 1.13  2006/02/01 14:43:44  vassil_momtchev
 314  
  * extend wsmo4j parser; do not implement org.wsmo.service.choreography.io.Serializer anymore see SerializerImpl; repeated code removed; all nfps/importOntologies are serialized; all string concatinations replaced with writer
 315  
  *
 316  
  * Revision 1.12  2006/01/31 10:51:42  vassil_momtchev
 317  
  * unused fields removed; log footer added
 318  
  *
 319  
 */