Coverage Report - th.co.edge.jseq.argouml.util.MModelUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
MModelUtil
87%
13/15
N/A
0
MModelUtil$SequenceDiagramBuilder
96%
74/77
79%
11/14
0
 
 1  
 /*
 2  
  * Copyright (c) 2003-2008, by Henrik Arro and Contributors
 3  
  *
 4  
  * This file is part of JSeq, a tool to automatically create
 5  
  * sequence diagrams by tracing program execution.
 6  
  *
 7  
  * See <http://jseq.sourceforge.net> for more information.
 8  
  *
 9  
  * JSeq is free software: you can redistribute it and/or modify
 10  
  * it under the terms of the GNU Lesser General Public License as
 11  
  * published by the Free Software Foundation, either version 3 of
 12  
  * the License, or (at your option) any later version.
 13  
  *
 14  
  * JSeq is distributed in the hope that it will be useful,
 15  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 17  
  * GNU Lesser General Public License for more details.
 18  
  *
 19  
  * You should have received a copy of the GNU Lesser General Public License
 20  
  * along with JSeq. If not, see <http://www.gnu.org/licenses/>.
 21  
  */
 22  
 
 23  
 package th.co.edge.jseq.argouml.util;
 24  
 
 25  
 import java.util.HashMap;
 26  
 import java.util.Map;
 27  
 
 28  
 import javax.xml.parsers.ParserConfigurationException;
 29  
 
 30  
 import ru.novosoft.uml.MFactory;
 31  
 import ru.novosoft.uml.behavior.collaborations.MCollaboration;
 32  
 import ru.novosoft.uml.behavior.common_behavior.MCallAction;
 33  
 import ru.novosoft.uml.behavior.common_behavior.MLink;
 34  
 import ru.novosoft.uml.behavior.common_behavior.MLinkEnd;
 35  
 import ru.novosoft.uml.behavior.common_behavior.MObject;
 36  
 import ru.novosoft.uml.behavior.common_behavior.MStimulus;
 37  
 import ru.novosoft.uml.foundation.core.MClass;
 38  
 import ru.novosoft.uml.foundation.core.MNamespace;
 39  
 import ru.novosoft.uml.foundation.data_types.MVisibilityKind;
 40  
 import ru.novosoft.uml.model_management.MModel;
 41  
 
 42  
 import th.co.edge.jseq.Activation;
 43  
 import th.co.edge.jseq.argouml.SequenceDiagram;
 44  
 import th.co.edge.jseq.argouml.UUID;
 45  
 
 46  
 /**
 47  
  * A utility class to work with <code>MModel</code> objects.
 48  
  */
 49  51
 public class MModelUtil {
 50  1
     private static MFactory factory = MFactory.getDefaultFactory();
 51  
 
 52  
     /**
 53  
      * This class only contains static methods, so we hide the constructor.
 54  
      */
 55  0
     private MModelUtil() {
 56  0
     }
 57  
 
 58  
     /**
 59  
      * Returns a new <code>MModel</code> object with the given name.
 60  
      *
 61  
      * @param name
 62  
      *            the name of the model to create
 63  
      *
 64  
      * @return a new <code>MModel</code> object
 65  
      */
 66  
     public static MModel createMModel(String name) {
 67  1
         MModel model = factory.createModel();
 68  1
         model.setName(name);
 69  1
         model.setUUID(UUID.getID());
 70  1
         return model;
 71  
     }
 72  
 
 73  
     /**
 74  
      * Creates a new <code>SequenceDiagram</code> based on an
 75  
      * <code>Activation</code> and adds it to the given <code>MModel</code>.
 76  
      *
 77  
      * @param model
 78  
      *            the <code>MModel</code> to which to add the sequence diagram
 79  
      * @param activation
 80  
      *            the <code>Activation</code> on which to base the sequence
 81  
      *            diagram
 82  
      *
 83  
      * @return the newly created <code>SequenceDiagram</code>
 84  
      *
 85  
      * @throws ParserConfigurationException
 86  
      *             if there is some serious error in the XML configuration
 87  
      *             (should normally not occur)
 88  
      */
 89  
     public static SequenceDiagram addSequenceDiagram(MModel model,
 90  
             Activation activation) throws ParserConfigurationException {
 91  1
         MCollaboration collaboration = factory.createCollaboration();
 92  1
         model.addOwnedElement(collaboration);
 93  1
         collaboration.setName("newCollaboration");
 94  1
         collaboration.setUUID(UUID.getID());
 95  1
         model.addOwnedElement(collaboration);
 96  1
         SequenceDiagramBuilder builder =
 97  
                 new SequenceDiagramBuilder(collaboration, activation);
 98  1
         return builder.getDiagram();
 99  
     }
 100  
 
 101  
     private static class SequenceDiagramBuilder {
 102  
         private MNamespace namespace;
 103  
         private SequenceDiagram diagram;
 104  1
         private Map<String, MClass> addedClasses =
 105  
                 new HashMap<String, MClass>();
 106  1
         private Map<String, MObject> addedObjects =
 107  
                 new HashMap<String, MObject>();
 108  
 
 109  
         public SequenceDiagramBuilder(MNamespace namespace,
 110  1
                 Activation activation) throws ParserConfigurationException {
 111  1
             this.namespace = namespace;
 112  1
             this.diagram = new SequenceDiagram(namespace, activation);
 113  1
             fillSequenceDiagram(activation);
 114  1
         }
 115  
 
 116  
         public SequenceDiagram getDiagram() {
 117  23
             return diagram;
 118  
         }
 119  
 
 120  
         private void fillSequenceDiagram(Activation activation) {
 121  10
             MObject receiver = addObject(activation.getClassName(), "");
 122  10
             Activation parent = activation.getParent();
 123  10
             if (parent != null) {
 124  9
                 MObject sender = addObject(parent.getClassName(), "");
 125  9
                 addCall(sender, receiver, activation.getMethod().name());
 126  
             }
 127  10
             for (Activation nestedActivation : activation
 128  
                     .getNestedActivations()) {
 129  9
                 fillSequenceDiagram(nestedActivation);
 130  
             }
 131  10
             getDiagram().deactivate(receiver);
 132  10
         }
 133  
 
 134  
         private MObject addObject(String className, String name) {
 135  19
             MClass cls = createClass(className);
 136  19
             namespace.addOwnedElement(cls);
 137  19
             MObject object = createObject(cls, name);
 138  19
             namespace.addOwnedElement(object);
 139  19
             return object;
 140  
         }
 141  
 
 142  
         private void addCall(MObject sender, MObject receiver, String name) {
 143  9
             MCallAction call = createCall(name);
 144  9
             namespace.addOwnedElement(call);
 145  
 
 146  9
             MStimulus stimulus = createStimulus(sender, receiver, call);
 147  9
             namespace.addOwnedElement(stimulus);
 148  
 
 149  9
             MLink link = createLink(sender, receiver, name, stimulus);
 150  9
             namespace.addOwnedElement(link);
 151  
 
 152  9
             getDiagram().addCall(sender, receiver, link);
 153  9
         }
 154  
 
 155  
         private MClass createClass(String name) {
 156  
             MClass cls;
 157  19
             if (addedClasses.containsKey(name)) {
 158  16
                 cls = addedClasses.get(name);
 159  
             } else {
 160  3
                 cls = MModelUtil.factory.createClass();
 161  3
                 addedClasses.put(name, cls);
 162  3
                 if (name != null) {
 163  3
                     cls.setName(name);
 164  
                 } else {
 165  0
                     cls.setName("");
 166  
                 }
 167  3
                 cls.setUUID(UUID.getID());
 168  3
                 cls.setVisibility(MVisibilityKind.PUBLIC);
 169  
             }
 170  19
             return cls;
 171  
         }
 172  
 
 173  
         private MObject createObject(MClass cls, String name) {
 174  
             MObject object;
 175  19
             String fullName = cls.getName() + "." + name;
 176  19
             if (addedObjects.containsKey(fullName)) {
 177  16
                 object = addedObjects.get(fullName);
 178  
             } else {
 179  3
                 object = MModelUtil.factory.createObject();
 180  3
                 addedObjects.put(fullName, object);
 181  3
                 object.addClassifier(cls);
 182  3
                 if (name != null) {
 183  3
                     object.setName(name);
 184  
                 } else {
 185  0
                     object.setName("");
 186  
                 }
 187  3
                 object.setUUID(UUID.getID());
 188  3
                 getDiagram().addObject(object);
 189  
             }
 190  19
             return object;
 191  
         }
 192  
 
 193  
         private MLink createLink(MObject fromObject, MObject toObject,
 194  
                 String name, MStimulus stimulus) {
 195  9
             MLink link = MModelUtil.factory.createLink();
 196  9
             link.setUUID(UUID.getID());
 197  9
             MLinkEnd linkFrom = MModelUtil.factory.createLinkEnd();
 198  9
             linkFrom.setInstance(fromObject);
 199  9
             linkFrom.setUUID(UUID.getID());
 200  9
             link.addConnection(linkFrom);
 201  9
             MLinkEnd linkTo = MModelUtil.factory.createLinkEnd();
 202  9
             linkTo.setInstance(toObject);
 203  9
             linkTo.setUUID(UUID.getID());
 204  9
             link.addConnection(linkTo);
 205  9
             link.addStimulus(stimulus);
 206  9
             return link;
 207  
         }
 208  
 
 209  
         private MCallAction createCall(String name) {
 210  9
             MCallAction call = MModelUtil.factory.createCallAction();
 211  9
             if (name != null) {
 212  9
                 call.setName(name);
 213  
             } else {
 214  0
                 call.setName("");
 215  
             }
 216  9
             call.setUUID(UUID.getID());
 217  9
             return call;
 218  
         }
 219  
 
 220  
         private MStimulus createStimulus(MObject fromObject, MObject toObject,
 221  
                 MCallAction call) {
 222  9
             MStimulus stimulus = MModelUtil.factory.createStimulus();
 223  9
             stimulus.setName("");
 224  9
             stimulus.setSender(fromObject);
 225  9
             stimulus.setReceiver(toObject);
 226  9
             stimulus.setDispatchAction(call);
 227  9
             stimulus.setUUID(UUID.getID());
 228  9
             return stimulus;
 229  
         }
 230  
     }
 231  
 }