|
From: jistrawn <jis...@us...> - 2006-10-30 15:36:11
|
Update of /cvsroot/modelwizard/source/Chameleon Plugin/src/net/sourceforge/modelWizard/chameleon/exporters In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv31665/src/net/sourceforge/modelWizard/chameleon/exporters Modified Files: SubModelGenerator.java Log Message: support for raisedException dependencies and sub-model interface issue resolved Index: SubModelGenerator.java =================================================================== RCS file: /cvsroot/modelwizard/source/Chameleon Plugin/src/net/sourceforge/modelWizard/chameleon/exporters/SubModelGenerator.java,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** SubModelGenerator.java 13 Oct 2006 20:31:34 -0000 1.8 --- SubModelGenerator.java 30 Oct 2006 15:36:06 -0000 1.9 *************** *** 19,23 **** --- 19,25 ---- import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; + import java.util.Collection; import java.util.Iterator; + import java.util.List; import java.util.TreeMap; *************** *** 29,33 **** --- 31,39 ---- import org.eclipse.core.runtime.SubProgressMonitor; import org.eclipse.emf.common.util.URI; + import org.eclipse.emf.ecore.EAttribute; + import org.eclipse.emf.ecore.EClass; import org.eclipse.emf.ecore.EObject; + import org.eclipse.emf.ecore.EReference; + import org.eclipse.emf.ecore.EStructuralFeature; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; *************** *** 39,42 **** --- 45,49 ---- import org.eclipse.uml2.DataType; import org.eclipse.uml2.Enumeration; + import org.eclipse.uml2.Implementation; import org.eclipse.uml2.Interface; import org.eclipse.uml2.Model; *************** *** 55,62 **** public class SubModelGenerator { - public static SubModelGenerator INSTANCE = new SubModelGenerator(); private TreeMap rqdElements; /** * This method returns a new model that is a subset of --- 62,145 ---- public class SubModelGenerator { private TreeMap rqdElements; + private List classList; + + public static SubModelGenerator INSTANCE = new SubModelGenerator(); + + class MyCopier extends Copier { + + private static final long serialVersionUID = 1L; + + /** + * Override super + * Returns a copy of the given eObject. + * + * @param eObject + * the object to copy. + * @return the copy. + */ + public EObject copy(EObject eObject) { + + // do not include the class interfaces that are not included in the sub-model. + // throwing a runtime so if this + if (eObject instanceof Implementation) { + if (!isInterfaceInSubModel((Implementation) eObject)) { + throw new RuntimeException(); + } + } + + EObject copyEObject = createCopy(eObject); + put(eObject, copyEObject); + EClass eClass = eObject.eClass(); + for (int i = 0, size = eClass.getFeatureCount(); i < size; ++i) { + EStructuralFeature eStructuralFeature = eClass.getEStructuralFeature(i); + if (eStructuralFeature.isChangeable() && !eStructuralFeature.isDerived()) { + if (eStructuralFeature instanceof EAttribute) { + copyAttribute((EAttribute) eStructuralFeature, eObject, copyEObject); + } else { + EReference eReference = (EReference) eStructuralFeature; + if (eReference.isContainment()) { + copyContainment(eReference, eObject, copyEObject); + } + } + } + } + + return copyEObject; + } + + /** + * Override super + * Returns a collection containing a copy of each EObject in the given + * collection. + * + * @param eObjects + * the collection of objects to copy. + * @return the collection of copies. + */ + public Collection copyAll(Collection eObjects) { + Collection result = new ArrayList(eObjects.size()); + for (Iterator i = eObjects.iterator(); i.hasNext();) { + EObject eObject; + try { + eObject = copy((EObject) i.next()); + } catch (RuntimeException e) { + continue; + } + result.add(eObject); + } + return result; + } + + private boolean isInterfaceInSubModel(Implementation theElement) { + if (classList.contains(theElement.getRealizingClassifier().getName())) { + return true; + } + return false; + } + } + /** * This method returns a new model that is a subset of *************** *** 71,97 **** * @throws ModelTransformationException */ ! public Model getSubModel(Iterator classNames, ! IProgressMonitor parentMonitor, ModelModel model, String modelName, ! String modelDesc, String basePackageName) throws IOException, ! InvocationTargetException, InterruptedException, ModelTransformationException { IProgressMonitor monitor = new SubProgressMonitor(parentMonitor, 2); monitor.beginTask("Creating sub model", 3); ! rqdElements = new TreeMap(); // for each class add the class and any superclasses to the rqdClasses // treemap int n = 0; ! ! while (classNames.hasNext()) { n++; ! String cN = (String) classNames.next(); Classifier theElement = (Classifier) model.findRealElement(cN); if (theElement == null) { ! throw new ModelTransformationException(cN ! + " does not exist in model!"); } addDependencies(rqdElements, theElement, false); --- 154,180 ---- * @throws ModelTransformationException */ ! public Model getSubModel(Iterator classNames, IProgressMonitor parentMonitor, ModelModel model, String modelName, ! String modelDesc, String basePackageName) throws IOException, InvocationTargetException, InterruptedException, ModelTransformationException { + IProgressMonitor monitor = new SubProgressMonitor(parentMonitor, 2); monitor.beginTask("Creating sub model", 3); ! classList = initClassesList(classNames); ! // for each class add the class and any superclasses to the rqdClasses // treemap + rqdElements = new TreeMap(); int n = 0; ! for (int i = 0; i < classList.size(); i++) { n++; ! String cN = (String) classList.get(i); Classifier theElement = (Classifier) model.findRealElement(cN); if (theElement == null) { ! throw new ModelTransformationException(cN + " does not exist in model!"); } + addDependencies(rqdElements, theElement, false); *************** *** 104,108 **** // now create the new model with the required elements. ! Copier copier = new Copier(); Model newModel = UML2Factory.eINSTANCE.createModel(); newModel.setName(modelName); --- 187,191 ---- // now create the new model with the required elements. ! Copier copier = new MyCopier(); Model newModel = UML2Factory.eINSTANCE.createModel(); newModel.setName(modelName); *************** *** 115,129 **** // add profile applications... ArrayList pList = new ArrayList(); ! for (Iterator p = model.getRealModel().getAppliedProfiles().iterator(); p ! .hasNext();) { ProfileApplication pa = (ProfileApplication) p.next(); if (!pa.getImportedProfile().eIsProxy()) { EObject o = copier.copy(pa); newModel.getAppliedProfiles().add(o); ! pList.add(pa.getImportedProfile().eResource().getURI() ! .toString()); } else { - // System.out.println("Ignoring - // "+((InternalEObject)pa.getImportedProfile()).eProxyURI().toString()); continue; } --- 198,208 ---- // add profile applications... ArrayList pList = new ArrayList(); ! for (Iterator p = model.getRealModel().getAppliedProfiles().iterator(); p.hasNext();) { ProfileApplication pa = (ProfileApplication) p.next(); if (!pa.getImportedProfile().eIsProxy()) { EObject o = copier.copy(pa); newModel.getAppliedProfiles().add(o); ! pList.add(pa.getImportedProfile().eResource().getURI().toString()); } else { continue; } *************** *** 131,145 **** // add packageImports... ! for (Iterator p = model.getRealModel().getPackageImports().iterator(); p ! .hasNext();) { PackageImport pi = (PackageImport) p.next(); if (!pi.getImportedPackage().eIsProxy() ! && !pList.contains(pi.getImportedPackage().eResource() ! .getURI().toString())) { EObject o = copier.copy(pi); newModel.getPackageImports().add(o); } else { - // System.out.println("Ignoring - // "+((InternalEObject)pi.getImportedPackage()).eProxyURI().toString()); continue; } --- 210,220 ---- // add packageImports... ! for (Iterator p = model.getRealModel().getPackageImports().iterator(); p.hasNext();) { PackageImport pi = (PackageImport) p.next(); if (!pi.getImportedPackage().eIsProxy() ! && !pList.contains(pi.getImportedPackage().eResource().getURI().toString())) { EObject o = copier.copy(pi); newModel.getPackageImports().add(o); } else { continue; } *************** *** 147,156 **** if ((modelDesc != null) & !modelDesc.equals("")) { ! Comment newComment = newModel ! .createOwnedComment(UML2Package.eINSTANCE.getComment()); newComment.setBody(modelDesc); ! Stereotype s = newComment ! .getApplicableStereotype("Default::documentation"); if (s != null) { --- 222,229 ---- if ((modelDesc != null) & !modelDesc.equals("")) { ! Comment newComment = newModel.createOwnedComment(UML2Package.eINSTANCE.getComment()); newComment.setBody(modelDesc); ! Stereotype s = newComment.getApplicableStereotype("Default::documentation"); if (s != null) { *************** *** 162,170 **** Iterator keys = rqdElements.keySet().iterator(); - while (keys.hasNext()) { NamedElement el = (NamedElement) rqdElements.get(keys.next()); Package owner = addPackageHierarchy(basePackage, el); ! EObject o = copier.copy(el); deleteUnneededAssocEnds(el, o, rqdElements); owner.getOwnedMembers().add(o); --- 235,243 ---- Iterator keys = rqdElements.keySet().iterator(); while (keys.hasNext()) { NamedElement el = (NamedElement) rqdElements.get(keys.next()); Package owner = addPackageHierarchy(basePackage, el); ! EObject o; ! o = copier.copy(el); deleteUnneededAssocEnds(el, o, rqdElements); owner.getOwnedMembers().add(o); *************** *** 174,182 **** Stereotype stereo = newModel.getApplicableStereotype("MODEL::Version"); ! String version = (String) model.getRealModel() ! .getValue(stereo, "value"); String derived = "Derived from " + version.replaceAll("\\$", ""); - // newModel.apply(stereo); newModel.setValue(stereo, "value", derived); --- 247,253 ---- Stereotype stereo = newModel.getApplicableStereotype("MODEL::Version"); ! String version = (String) model.getRealModel().getValue(stereo, "value"); String derived = "Derived from " + version.replaceAll("\\$", ""); newModel.setValue(stereo, "value", derived); *************** *** 187,206 **** } private Package createPackageHierachy(String basePackage, Package parent) { String top = basePackage.split("\\.")[0]; ! Package newPackage = (Package) parent ! .createOwnedMember(UML2Package.eINSTANCE.getPackage()); newPackage.setName(top); if (top.equals(basePackage)) { return newPackage; } else { ! String rest = basePackage.substring(top.length() + 1, basePackage ! .length()); return createPackageHierachy(rest, newPackage); } } ! private void deleteUnneededAssocEnds(NamedElement sourceEl, ! EObject targetEl, TreeMap rqdElements) { if (sourceEl instanceof Class) { Class src = (Class) sourceEl; --- 258,283 ---- } + // put classes into a list so we can use the list in helper methods. + private List initClassesList(Iterator classNames) { + List classList = new ArrayList(); + while (classNames.hasNext()) { + classList.add(classNames.next()); + } + return classList; + } + private Package createPackageHierachy(String basePackage, Package parent) { String top = basePackage.split("\\.")[0]; ! Package newPackage = (Package) parent.createOwnedMember(UML2Package.eINSTANCE.getPackage()); newPackage.setName(top); if (top.equals(basePackage)) { return newPackage; } else { ! String rest = basePackage.substring(top.length() + 1, basePackage.length()); return createPackageHierachy(rest, newPackage); } } ! private void deleteUnneededAssocEnds(NamedElement sourceEl, EObject targetEl, TreeMap rqdElements) { if (sourceEl instanceof Class) { Class src = (Class) sourceEl; *************** *** 211,216 **** continue; } ! if (!rqdElements.containsKey(p.getAssociation() ! .getQualifiedName())) { Property excessProp = tgt.getAttribute(p.getName()); tgt.getOwnedAttributes().remove(excessProp); --- 288,292 ---- continue; } ! if (!rqdElements.containsKey(p.getAssociation().getQualifiedName())) { Property excessProp = tgt.getAttribute(p.getName()); tgt.getOwnedAttributes().remove(excessProp); *************** *** 226,231 **** continue; } ! if (!rqdElements.containsKey(p.getAssociation() ! .getQualifiedName())) { Property excessProp = tgt.getAttribute(p.getName()); tgt.getOwnedAttributes().remove(excessProp); --- 302,306 ---- continue; } ! if (!rqdElements.containsKey(p.getAssociation().getQualifiedName())) { Property excessProp = tgt.getAttribute(p.getName()); tgt.getOwnedAttributes().remove(excessProp); *************** *** 292,299 **** * @throws ModelTransformationException */ ! private void addDependencies(TreeMap rqdElements, Classifier theElement, ! boolean ignoreSuper) throws ModelTransformationException { ! String qName = theElement.getQualifiedName(); if (!rqdElements.containsKey(qName)) { rqdElements.put(qName, theElement); --- 367,374 ---- * @throws ModelTransformationException */ ! private void addDependencies(TreeMap rqdElements, Classifier theElement, boolean ignoreSuper) ! throws ModelTransformationException { + String qName = theElement.getQualifiedName(); if (!rqdElements.containsKey(qName)) { rqdElements.put(qName, theElement); *************** *** 317,324 **** if (theElement instanceof Class || theElement instanceof Interface) { // add the superclass if (!ignoreSuper) { ! Classifier sc = (Classifier) UmlUtilities ! .getGeneral(theElement); if (sc != null) { --- 392,399 ---- if (theElement instanceof Class || theElement instanceof Interface) { + // add the superclass if (!ignoreSuper) { ! Classifier sc = (Classifier) UmlUtilities.getGeneral(theElement); if (sc != null) { *************** *** 331,346 **** if (theElement instanceof Class) { Class c = (Class) theElement; ! props = UmlUtilities.getProperties(c.getAttributes(), c ! .getInheritedMembers(), false); ! methods = UmlUtilities.getMethods(c.getOwnedOperations(), c ! .getInheritedMembers(), false); } if (theElement instanceof Interface) { Interface intfc = (Interface) theElement; ! props = UmlUtilities.getProperties(intfc.getAttributes(), intfc ! .getInheritedMembers(), false); ! methods = UmlUtilities.getMethods(intfc.getOwnedOperations(), ! intfc.getInheritedMembers(), false); } --- 406,417 ---- if (theElement instanceof Class) { Class c = (Class) theElement; ! props = UmlUtilities.getProperties(c.getAttributes(), c.getInheritedMembers(), false, true); ! methods = UmlUtilities.getMethods(c.getOwnedOperations(), c.getInheritedMembers(), false); } if (theElement instanceof Interface) { Interface intfc = (Interface) theElement; ! props = UmlUtilities.getProperties(intfc.getAttributes(), intfc.getInheritedMembers(), false); ! methods = UmlUtilities.getMethods(intfc.getOwnedOperations(), intfc.getInheritedMembers(), false); } *************** *** 348,354 **** Property p = (Property) props.next(); - // ignore associations for now. if (p.getAssociation() != null) { ! continue; } --- 419,424 ---- Property p = (Property) props.next(); if (p.getAssociation() != null) { ! addDependencies(rqdElements, (Classifier) p.getAssociation(), false); } *************** *** 358,370 **** } if (t == null) ! throw new ModelTransformationException("Type of " ! + p.getQualifiedName() + " is null"); String tQname = t.getQualifiedName(); if (t instanceof Classifier) { - // if (!rqdElements.containsKey(tQname)) { - // rqdElements.put(tQname, t); - // } addDependencies(rqdElements, (Classifier) t, false); } --- 428,436 ---- } if (t == null) ! throw new ModelTransformationException("Type of " + p.getQualifiedName() + " is null"); String tQname = t.getQualifiedName(); if (t instanceof Classifier) { addDependencies(rqdElements, (Classifier) t, false); } *************** *** 385,403 **** } } - - // if (t instanceof DataType) { - // if (!rqdElements.containsKey(tQname)) { - // rqdElements.put(tQname, t); - // } - // } } while (methods.hasNext()) { Operation theMethod = (Operation) methods.next(); - Type mt = theMethod.getType(); if (mt != null && !(mt instanceof PrimitiveType)) { ! if (mt instanceof Enumeration ! && !rqdElements.containsKey(mt.getQualifiedName())) { rqdElements.put(mt.getQualifiedName(), mt); } --- 451,463 ---- } } } while (methods.hasNext()) { Operation theMethod = (Operation) methods.next(); + // get return type dependencies + Type mt = theMethod.getType(); if (mt != null && !(mt instanceof PrimitiveType)) { ! if (mt instanceof Enumeration && !rqdElements.containsKey(mt.getQualifiedName())) { rqdElements.put(mt.getQualifiedName(), mt); } *************** *** 408,413 **** } ! Iterator p = theMethod.getOwnedParameters().iterator(); while (p.hasNext()) { Parameter param = (Parameter) p.next(); --- 468,479 ---- } ! // get exception dependencies ! Iterator r = theMethod.getRaisedExceptions().iterator(); ! while (r.hasNext()) { ! Class re = (Class) r.next(); ! addDependencies(rqdElements, (Classifier) re, false); ! } + Iterator p = theMethod.getOwnedParameters().iterator(); while (p.hasNext()) { Parameter param = (Parameter) p.next(); *************** *** 420,430 **** if (t instanceof Class) { - // if (!rqdElements.containsKey(pQname)) - // { - // rqdElements.put( - // pQname, - // t); - // } - addDependencies(rqdElements, (Class) t, false); } --- 486,489 ---- *************** *** 450,455 **** // and finally look at the MemberEnds if it is an association if (theElement instanceof Association) { ! Iterator ends = ((Association) theElement).getMemberEnds() ! .iterator(); while (ends.hasNext()) { --- 509,513 ---- // and finally look at the MemberEnds if it is an association if (theElement instanceof Association) { ! Iterator ends = ((Association) theElement).getMemberEnds().iterator(); while (ends.hasNext()) { *************** *** 459,476 **** if (t instanceof Classifier) { - // if (!rqdElements.containsKey(pQname)) - // { - // rqdElements.put( - // pQname, - // t); - // } - addDependencies(rqdElements, (Classifier) t, false); } else { ! throw new ModelTransformationException("AssociationEnd " ! + pQname + " not a class"); } } } } } \ No newline at end of file --- 517,527 ---- if (t instanceof Classifier) { addDependencies(rqdElements, (Classifier) t, false); } else { ! throw new ModelTransformationException("AssociationEnd " + pQname + " not a class"); } } } } + } \ No newline at end of file |