001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.sdlctools.models.metabossmodel;
016:
017: import java.util.Collection;
018:
019: import javax.jmi.reflect.ConstraintViolationException;
020: import javax.jmi.reflect.JmiException;
021: import javax.jmi.reflect.RefPackage;
022:
023: import com.metaboss.sdlctools.models.ModelRepositoryException;
024: import com.metaboss.sdlctools.models.ModelRepositoryIllegalArgumentException;
025: import com.metaboss.sdlctools.models.ModelValidationException;
026: import com.metaboss.sdlctools.models.impl.ModelRepositoryImpl;
027: import com.metaboss.sdlctools.models.impl.metabossmodel.MetaBossModelToUMLModelConvertor;
028: import com.metaboss.sdlctools.models.impl.metabossmodel.MetaBossUMLProfileCreator;
029: import com.metaboss.sdlctools.models.impl.metabossmodel.UMLModelToMetaBossModelConvertor;
030: import com.metaboss.sdlctools.models.metabossmodel.designlibrarymodel.DesignLibrary;
031: import com.metaboss.sdlctools.models.metabossmodel.designlibrarymodel.DesignLibraryModelPackage;
032: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Enterprise;
033: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.EnterpriseModelPackage;
034: import com.metaboss.sdlctools.models.metabossmodel.technologylibrarymodel.TechnologyLibrary;
035: import com.metaboss.sdlctools.models.metabossmodel.technologylibrarymodel.TechnologyLibraryModelPackage;
036:
037: /** Set of MetaBossModel utility and helper methods realising some facilities seemingly not provided by MDR. */
038: public class ModelUtils {
039: /** This class is a "toolbox" with static utilities and can not be instantiated */
040: private ModelUtils() {
041: }
042:
043: /** Returns one and only one top level ModelElement. This element can be of
044: * type DesignLibrary, TechnologyLibrary or Enterprise. The type of returned element
045: * determines what kind of model we are looking at.
046: * @param pModelName the name of the model previously loaded in the repository
047: * @exception ModelRepositoryException is thrown if model with the given name is not found, or it is not of the expected type */
048: public static ModelElement getRootElement(String pModelName)
049: throws ModelRepositoryException {
050: ModelRepositoryImpl lRepository = ModelRepositoryImpl
051: .getRepository();
052: RefPackage lOutermostPackage = lRepository
053: .getModelExtent(pModelName);
054: if (lOutermostPackage instanceof MetaBossModelPackage)
055: return getRootElement((MetaBossModelPackage) lOutermostPackage);
056: else
057: throw new ModelRepositoryIllegalArgumentException("Model '"
058: + pModelName + "' is not MetaBossModel.");
059: }
060:
061: /** Returns one and only one top level ModelElement. This element can be of
062: * type DesignLibrary, TechnologyLibrary or Enterprise. The type of returned element
063: * determines what kind of model we are looking at.
064: * @param pModelName the name of the model previously loaded in the repository
065: * @exception ModelRepositoryException is thrown if model with the given name is not found, or it is not of the expected type */
066: public static ModelElement getRootElement(
067: MetaBossModelPackage pMetaBossModelPackage)
068: throws ModelRepositoryException {
069: // Try top elements one by one.
070: // Start from the enteprise as this is presumably most often occuring scenario
071: {
072: EnterpriseModelPackage lEnterpriseModelPackage = pMetaBossModelPackage
073: .getEnterpriseModel();
074: Collection lEnterprises = lEnterpriseModelPackage
075: .getEnterprise().refAllOfType();
076: if (lEnterprises.size() == 1)
077: return (Enterprise) lEnterprises.iterator().next();
078: else if (lEnterprises.size() != 0)
079: throw new ModelValidationException(
080: "Unable to determine root element of the model.",
081: new JmiException[] { new ConstraintViolationException(
082: lEnterpriseModelPackage,
083: lEnterpriseModelPackage.refMetaObject(),
084: "There can only be at most one instance of Enterprise element in the model.") });
085: }
086: // Now to design library
087: {
088: DesignLibraryModelPackage lDesignLibraryModelPackage = pMetaBossModelPackage
089: .getDesignLibraryModel();
090: Collection lDesignLibraries = lDesignLibraryModelPackage
091: .getDesignLibrary().refAllOfType();
092: if (lDesignLibraries.size() == 1)
093: return (DesignLibrary) lDesignLibraries.iterator()
094: .next();
095: else if (lDesignLibraries.size() != 0)
096: throw new ModelValidationException(
097: "Unable to determine root element of the model.",
098: new JmiException[] { new ConstraintViolationException(
099: lDesignLibraryModelPackage,
100: lDesignLibraryModelPackage
101: .refMetaObject(),
102: "There can only be at most one instance of DesignLibrary element in the model.") });
103: }
104: // Now to technology library
105: {
106: TechnologyLibraryModelPackage lTechnologyLibraryModelPackage = pMetaBossModelPackage
107: .getTechnologyLibraryModel();
108: Collection lTechnologyLibraries = lTechnologyLibraryModelPackage
109: .getTechnologyLibrary().refAllOfType();
110: if (lTechnologyLibraries.size() == 1)
111: return (TechnologyLibrary) lTechnologyLibraries
112: .iterator().next();
113: else if (lTechnologyLibraries.size() != 0)
114: throw new ModelValidationException(
115: "Unable to determine root element of the model.",
116: new JmiException[] { new ConstraintViolationException(
117: lTechnologyLibraryModelPackage,
118: lTechnologyLibraryModelPackage
119: .refMetaObject(),
120: "There can only be at most one instance of TechnologyLibrary element in the model.") });
121: }
122: throw new ModelValidationException(
123: "Unable to determine root element of the model.",
124: new JmiException[] { new ConstraintViolationException(
125: pMetaBossModelPackage,
126: pMetaBossModelPackage.refMetaObject(),
127: "There must be one and only one instance of Enterprise or DesignLibrary or TechnologyLibrary element in the model.") });
128: }
129:
130: /** This method converts MetaBoss model to UML model with MetaBoss profile
131: * Note that resulting UML model is a 100% UML in all aspect but it is
132: * enriched and constrained with special stereotypes and tagged values.
133: * In UML this is quite normal and allowed technique.
134: * @param pSourceMetaBossModelName the name of already loaded MetaBoss model
135: * @param pTargetUMLModelName the name of destination UML model. This model
136: * should not exist at the time this method is called. It will be created inside this method.
137: * @exception ModelRepositoryException is thrown in case of trouble (e.g. source model not found, target model already exists etc) */
138: public static void convertMetaBossModelToUMLModel(
139: String pSourceMetaBossModelName, String pTargetUMLModelName)
140: throws ModelRepositoryException {
141: // The job is too big - delegate it to the separate class in the model implementation
142: MetaBossModelToUMLModelConvertor.doConversion(
143: pSourceMetaBossModelName, pTargetUMLModelName);
144: }
145:
146: /** This method creates UML Profile for MetaBoss model. The resulting profile
147: * may be used as a starting point for the UML Models containing MetaBoss enterprise
148: * definition.
149: * @param pTargetUMLModelName the name of destination UML model. This model
150: * should not exist at the time this method is called. It will be created inside this method.
151: * @exception ModelRepositoryException is thrown in case of trouble (e.g. source model not found, target model already exists etc) */
152: public static void createMetaBossUMLProfileModel(
153: String pTargetUMLModelName) throws ModelRepositoryException {
154: // The job is too big - delegate it to the separate class in the model implementation
155: MetaBossUMLProfileCreator.doCreation(pTargetUMLModelName);
156: }
157:
158: /** This method converts UML model to MetaBoss model.
159: * Note that source UML model is a 100% UML in all aspect but it is
160: * enriched and constrained with special stereotypes and tagged values.
161: * In UML this is quite normal and allowed technique.
162: * @param pSourceUMLModelName the name of already loaded UML model
163: * @param pTargetMetaBossModelName the name of destination MetaBoss model. This model
164: * should not exist at the time this method is called. It will be created inside this method.
165: * @exception ModelRepositoryException is thrown in case of trouble (e.g. source model not found, target model already exists etc) */
166: public static void convertUMLModelToMetaBossModel(
167: String pSourceUMLModelName, String pTargetMetaBossModelName)
168: throws ModelRepositoryException {
169: // The job is too big - delegate it to the separate class in the model implementation
170: UMLModelToMetaBossModelConvertor.doConversion(
171: pSourceUMLModelName, pTargetMetaBossModelName);
172: }
173: }
|