diff --git a/.gitignore b/.gitignore index 1395c2d..2c18da1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ -.idea/ -out/ +**/.pytest_cache +**/.vscode +**/__pycache__ +*.noseids diff --git a/CodeableModels.iml b/CodeableModels.iml deleted file mode 100644 index 3a8ffcf..0000000 --- a/CodeableModels.iml +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/README.md b/README.md index b941644..9ba12d0 100644 --- a/README.md +++ b/README.md @@ -17,27 +17,28 @@ clear from the interfaces. ### Prerequisites / Installing -The project - by purpose - only uses plain old Java objects. So there are no requirements -to get the project up and running, other than placing it in your classpath. +The project - by purpose - only uses plain Python. So there are no requirements +to get the project up and running. -JUnit is required in the classpath for executing the test cases. +Nosetest is required for executing the test cases. Find installation instructions under: +[Nosetest](https://nose.readthedocs.io/en/latest/) ## Running the tests -``` -codedableModels/tests/AllTests.java -``` - -contains the test suite. Run it as a JUnit test suite. All other files in the tests -directory contain individual JUnit tests. +Execute `nosetests` either in the main directory of the project or in `./tests`. The test files contained in +this directory comprise the test suite. ## Deployment -No specific instructions so far; simply build +No specific instructions so far; simply import from the `codeableModels` module like: + +``` +from codeableModels import CMetaclass, CClass, CObject, CAttribute, CException, CEnum, CStereotype +``` ## Built With -* [JUnit](https://junit.org) - The test framework used +* [Nosetest](https://nose.readthedocs.io/en/latest/) - The test framework used ## Contributing diff --git a/codeableModels/__init__.py b/codeableModels/__init__.py new file mode 100644 index 0000000..b008312 --- /dev/null +++ b/codeableModels/__init__.py @@ -0,0 +1,22 @@ +#from os.path import dirname, basename, isfile +#import glob +#import importlib + +#modules = glob.glob(dirname(__file__)+"/*.py") +#__all__ = [ basename(f)[:-3] for f in modules if isfile(f) and not f.endswith('__init__.py')] + +#from .impl import * + +from codeableModels.cexception import CException +from codeableModels.cnamedelement import CNamedElement +from codeableModels.cbundlable import CBundlable +from codeableModels.cattribute import CAttribute +from codeableModels.cclassifier import CClassifier +from codeableModels.cmetaclass import CMetaclass +from codeableModels.cstereotype import CStereotype +from codeableModels.cclass import CClass +from codeableModels.cobject import CObject +from codeableModels.cenum import CEnum +from codeableModels.cbundle import CBundle, CPackage, CLayer +from codeableModels.cassociation import CAssociation +from codeableModels.clink import CLink, setLinks, addLinks, deleteLinks diff --git a/codeableModels/cassociation.py b/codeableModels/cassociation.py new file mode 100644 index 0000000..358cace --- /dev/null +++ b/codeableModels/cassociation.py @@ -0,0 +1,241 @@ +from codeableModels.internal.commons import setKeywordArgs, isCClassifier, isCMetaclass, isCStereotype +from codeableModels.cexception import CException +from codeableModels.cnamedelement import CNamedElement +from codeableModels.internal.stereotype_holders import CStereotypesHolder +import re + +class CAssociation(CNamedElement): + STAR_MULTIPLICITY = -1 + + def __init__(self, source, target, descriptor = None, **kwargs): + self.source = source + self.target = target + self.roleName = None + self.sourceRoleName = None + self._sourceMultiplicityString = "1" + self._sourceLowerMultiplicity = 1 + self._sourceUpperMultiplicity = 1 + self._multiplicityString = "*" + self._lowerMultiplicity = 0 + self._upperMultiplicity = self.STAR_MULTIPLICITY + self._aggregation = False + self._composition = False + self._stereotypesHolder = CStereotypesHolder(self) + name = kwargs.pop("name", None) + self.ends = None + super().__init__(name, **kwargs) + if descriptor != None: + self._evalDescriptor(descriptor) + + def _initKeywordArgs(self, legalKeywordArgs = None, **kwargs): + if legalKeywordArgs == None: + legalKeywordArgs = [] + legalKeywordArgs.extend(["multiplicity", "roleName", "sourceMultiplicity", + "sourceRoleName", "aggregation", "composition", "stereotypes"]) + super()._initKeywordArgs(legalKeywordArgs, **kwargs) + + def __str__(self): + return super(CAssociation, self).__str__() + def __repr__(self): + name = "" + if self.name != None: + name = self.name + return f"CAssociation name = {name!s}, source = {self.source!s} -> target = {self.target!s}" + + def _getOppositeClass(self, cl): + if cl == self.source: + return self.target + else: + return self.source + + def _matches(self, classifier, roleName, associationClassifier, associationRoleName): + if classifier == None and roleName == None: + return False + matches = True + if roleName != None: + if roleName != associationRoleName: + matches = False + if matches and classifier != None: + if not classifier.conformsToType(associationClassifier): + matches = False + if matches: + return True + return False + + def _matchesTarget(self, classifier, roleName): + return self._matches(classifier, roleName, self.target, self.roleName) + + def _matchesSource(self, classifier, roleName): + return self._matches(classifier, roleName, self.source, self.sourceRoleName) + + @property + def aggregation(self): + return self._aggregation + @aggregation.setter + def aggregation(self, aggregation): + if aggregation: + self._composition = False + self._aggregation = aggregation + + def _setMultiplicity(self, multiplicity, isTargetMultiplicity): + if not isinstance(multiplicity, str): + raise CException("multiplicity must be provided as a string") + lower = -2 + upper = -2 + try: + dotsPos = multiplicity.find("..") + if dotsPos != -1: + lowerMatch = multiplicity[:dotsPos] + upperMatch = multiplicity[dotsPos+2:] + lower = int(lowerMatch) + if lower < 0: + raise CException(f"negative multiplicity in '{multiplicity!s}'") + if upperMatch.strip() == "*": + upper = self.STAR_MULTIPLICITY + else: + upper = int(upperMatch) + if lower < 0 or upper < 0: + raise CException(f"negative multiplicity in '{multiplicity!s}'") + elif multiplicity.strip() == "*": + lower = 0 + upper = self.STAR_MULTIPLICITY + else: + lower = int(multiplicity) + if lower < 0: + raise CException(f"negative multiplicity in '{multiplicity!s}'") + upper = lower + except Exception as e: + if isinstance(e, CException): + raise e + raise CException(f"malformed multiplicity: '{multiplicity!s}'") + + if isTargetMultiplicity: + self._upperMultiplicity = upper + self._lowerMultiplicity = lower + else: + self._sourceUpperMultiplicity = upper + self._sourceLowerMultiplicity = lower + + @property + def multiplicity(self): + return self._multiplicityString + @multiplicity.setter + def multiplicity(self, multiplicity): + self._multiplicityString = multiplicity + self._setMultiplicity(multiplicity, True) + + @property + def sourceMultiplicity(self): + return self._sourceMultiplicityString + @sourceMultiplicity.setter + def sourceMultiplicity(self, multiplicity): + self._sourceMultiplicityString = multiplicity + self._setMultiplicity(multiplicity, False) + + @property + def composition(self): + return self._composition + @composition.setter + def composition(self, composition): + if composition: + self._aggregation = False + self._composition = composition + + @property + def stereotypes(self): + return self._stereotypesHolder.stereotypes + + @stereotypes.setter + def stereotypes(self, elements): + self._stereotypesHolder.stereotypes = elements + + def delete(self): + if self._isDeleted == True: + return + if isCMetaclass(self.source): + allInstances = self.source.allClasses + elif isCStereotype(self.source): + allInstances = self.source.allExtendedInstances + else: + allInstances = self.source.allObjects + for instance in allInstances: + for link in instance.linkObjects: + link.delete() + self.source._associations.remove(self) + if self.source != self.target: + self.target._associations.remove(self) + for s in self._stereotypesHolder._stereotypes: + s._extended.remove(self) + self._stereotypesHolder._stereotypes = [] + super().delete() + + def _checkMultiplicity(self, object, actualLength, actualOppositeLength, checkTargetMultiplicity): + if checkTargetMultiplicity: + upper = self._upperMultiplicity + lower = self._lowerMultiplicity + otherSideLower = self._sourceLowerMultiplicity + multiplicityString = self._multiplicityString + else: + upper = self._sourceUpperMultiplicity + lower = self._sourceLowerMultiplicity + otherSideLower = self._lowerMultiplicity + multiplicityString = self._sourceMultiplicityString + + if (upper != CAssociation.STAR_MULTIPLICITY and actualLength > upper) or actualLength < lower: + # if there is actually no link as actualOppositeLength is zero, this is ok, if the otherLower including zero: + if not (actualOppositeLength == 0 and otherSideLower == 0): + raise CException(f"links of object '{object}' have wrong multiplicity '{actualLength!s}': should be '{multiplicityString!s}'") + + def _evalDescriptor(self, descriptor): + # handle name only if a ':' is found in the descriptor + index = descriptor.find(":") + if index != -1: + name = descriptor[0:index] + descriptor = descriptor[index+1:] + self.name = name.strip() + + # handle type of relation + aggregation = False + composition = False + index = descriptor.find("->") + length = 2 + if index == -1: + index = descriptor.find("<>-") + if index != -1: + length = 3 + aggregation = True + else: + index = descriptor.find("<*>-") + length = 4 + composition = True + if index == -1: + raise CException("association descriptor malformed: '" + descriptor + "'") + + # handle role names and multiplicities + sourceStr = descriptor[0:index] + targetStr = descriptor[index+length:] + regexpWithRoleName = '\s*\[([^\]]+)\]\s*(\S*)\s*' + regexpOnlyMultiplicity = '\s*(\S*)\s*' + + m = re.search(regexpWithRoleName, sourceStr) + if m != None: + self.sourceRoleName = m.group(1) + if m.group(2) != '': + self.sourceMultiplicity = m.group(2) + else: + m = re.search(regexpOnlyMultiplicity, sourceStr) + self.sourceMultiplicity = m.group(1) + + m = re.search(regexpWithRoleName, targetStr) + if m != None: + self.roleName = m.group(1) + if m.group(2) != '': + self.multiplicity = m.group(2) + else: + m = re.search(regexpOnlyMultiplicity, targetStr) + self.multiplicity = m.group(1) + + if aggregation: + self.aggregation = True + elif composition: + self.composition = True diff --git a/codeableModels/cattribute.py b/codeableModels/cattribute.py new file mode 100644 index 0000000..977a52a --- /dev/null +++ b/codeableModels/cattribute.py @@ -0,0 +1,98 @@ +from codeableModels.internal.commons import isCNamedElement, checkNamedElementIsNotDeleted, setKeywordArgs, isCClassifier, isCEnum, getAttributeType +from codeableModels.cexception import CException +from codeableModels.cenum import CEnum + +class CAttribute(object): + def __init__(self, **kwargs): + self._name = None + self._classifier = None + self._type = None + self._default = None + setKeywordArgs(self, ["type", "default"], **kwargs) + + def __str__(self): + return self.__repr__() + def __repr__(self): + return f"CAttribute type = {self._type!s}, default = {self._default!s}" + + def checkAttributeTypeIsNotDeleted(self): + if isCNamedElement(self._type): + checkNamedElementIsNotDeleted(self._type) + + @property + def name(self): + return self._name + + @property + def classifier(self): + return self._classifier + + @property + def type(self): + self.checkAttributeTypeIsNotDeleted() + return self._type + + def __wrongDefaultException(self, default, type): + raise CException(f"default value '{default!s}' incompatible with attribute's type '{type!s}'") + + @type.setter + def type(self, type): + if isCNamedElement(type): + checkNamedElementIsNotDeleted(type) + if self._default != None: + if isCEnum(type): + if not self._default in type.values: + self.__wrongDefaultException(self._default, type) + elif isCClassifier(type): + if (not self._default == type) and (not type in type.allSuperclasses): + self.__wrongDefaultException(self._default, type) + else: + if not isinstance(self._default, type): + self.__wrongDefaultException(self._default, type) + self._type = type + + @property + def default(self): + self.checkAttributeTypeIsNotDeleted() + return self._default + + @default.setter + def default(self, default): + if default == None: + self._default = None + return + + self.checkAttributeTypeIsNotDeleted() + if self._type != None: + if isCEnum(self._type): + if not default in self._type.values: + self.__wrongDefaultException(default, self._type) + elif isCClassifier(self._type): + if not default in self._type.objects: + self.__wrongDefaultException(default, self._type) + elif not isinstance(default, self._type): + self.__wrongDefaultException(default, self._type) + else: + attrType = getAttributeType(default) + if attrType == None: + raise CException(f"unknown attribute type: '{default!r}'") + self.type = attrType + self._default = default + if self._classifier != None: + self._classifier._updateDefaultValuesOfClassifier(self) + + def checkAttributeValueType(self, name, value): + attrType = getAttributeType(value) + if attrType == None: + raise CException(f"value for attribute '{name!s}' is not a known attribute type") + if isCClassifier(attrType): + if attrType != self._type and (not self._type in attrType.allSuperclasses): + raise CException(f"type of object '{value!s}' is not matching type of attribute '{name!s}'") + return + if attrType != self._type: + if (self.type == float and attrType == int): + return + if not (isCEnum(self._type) and attrType == str): + raise CException(f"value type for attribute '{name!s}' does not match attribute type") + if isCEnum(self._type) and attrType == str and (not self._type.isLegalValue(value)): + raise CException(f"value '{value!s}' is not element of enumeration") diff --git a/codeableModels/cbundlable.py b/codeableModels/cbundlable.py new file mode 100644 index 0000000..39ec647 --- /dev/null +++ b/codeableModels/cbundlable.py @@ -0,0 +1,122 @@ +from codeableModels.internal.commons import setKeywordArgs, checkNamedElementIsNotDeleted, isCBundle, isCMetaclass, isCClass, isCStereotype, isCBundlable +from codeableModels.cexception import CException +from codeableModels.cnamedelement import CNamedElement + +class CBundlable(CNamedElement): + def __init__(self, name, **kwargs): + self._bundles = [] + super().__init__(name, **kwargs) + + def _initKeywordArgs(self, legalKeywordArgs = None, **kwargs): + if legalKeywordArgs == None: + legalKeywordArgs = [] + legalKeywordArgs.append("bundles") + super()._initKeywordArgs(legalKeywordArgs, **kwargs) + + @property + def bundles(self): + return list(self._bundles) + + @bundles.setter + def bundles(self, bundles): + if bundles == None: + bundles = [] + for b in self._bundles: + b.remove(self) + self._bundles = [] + if isCBundle(bundles): + bundles = [bundles] + elif not isinstance(bundles, list): + raise CException(f"bundles requires a list of bundles or a bundle as input") + for b in bundles: + if not isCBundle(b): + raise CException(f"bundles requires a list of bundles or a bundle as input") + checkNamedElementIsNotDeleted(b) + if b in self._bundles: + raise CException(f"'{b.name!s}' is already a bundle of '{self.name!s}'") + self._bundles.append(b) + b._elements.append(self) + + def delete(self): + if self._isDeleted == True: + return + bundlesToDelete = list(self._bundles) + for b in bundlesToDelete: + b.remove(self) + self._bundles = [] + super().delete() + + def getConnectedElements(self, **kwargs): + context = ConnectedElementsContext() + + allowedKeywordArgs = ["addBundles", "processBundles", "stopElementsInclusive", "stopElementsExclusive"] + if isCMetaclass(self) or isCBundle(self) or isCStereotype(self): + allowedKeywordArgs = ["addStereotypes", "processStereotypes"] + allowedKeywordArgs + setKeywordArgs(context, allowedKeywordArgs, **kwargs) + + if self in context.stopElementsExclusive: + return [] + context.elements.append(self) + self._computeConnected(context) + if context.addBundles == False: + context.elements = [elt for elt in context.elements if not isCBundle(elt)] + if context.addStereotypes == False: + context.elements = [elt for elt in context.elements if not isCStereotype(elt)] + return context.elements + + def _appendConnected(self, context, connected): + for c in connected: + if not c in context.elements: + context.elements.append(c) + if not c in context._allStopElements: + c._computeConnected(context) + + def _computeConnected(self, context): + connected = [] + for bundle in self._bundles: + if not bundle in context.stopElementsExclusive: + connected.append(bundle) + self._appendConnected(context, connected) + +class ConnectedElementsContext(object): + def __init__(self): + self.elements = [] + self.addBundles = False + self.addStereotypes = False + self.processBundles = False + self.processStereotypes = False + self._stopElementsInclusive = [] + self._stopElementsExclusive = [] + self._allStopElements = [] + + @property + def stopElementsInclusive(self): + return list(self._stopElementsInclusive) + + @stopElementsInclusive.setter + def stopElementsInclusive(self, stopElementsInclusive): + if isCBundlable(stopElementsInclusive): + stopElementsInclusive = [stopElementsInclusive] + if not isinstance(stopElementsInclusive, list): + raise CException(f"expected one element or a list of stop elements, but got: '{stopElementsInclusive!s}'") + for e in stopElementsInclusive: + if not isCBundlable(e): + raise CException(f"expected one element or a list of stop elements, but got: '{stopElementsInclusive!s}' with element of wrong type: '{e!s}'") + self._stopElementsInclusive = stopElementsInclusive + self._allStopElements = self._stopElementsInclusive + self._stopElementsExclusive + + @property + def stopElementsExclusive(self): + return list(self._stopElementsExclusive) + + @stopElementsExclusive.setter + def stopElementsExclusive(self, stopElementsExclusive): + if isCBundlable(stopElementsExclusive): + stopElementsExclusive = [stopElementsExclusive] + if not isinstance(stopElementsExclusive, list): + raise CException(f"expected a list of stop elements, but got: '{stopElementsExclusive!s}'") + for e in stopElementsExclusive: + if not isCBundlable(e): + raise CException(f"expected a list of stop elements, but got: '{stopElementsExclusive!s}' with element of wrong type: '{e!s}'") + self._stopElementsExclusive = stopElementsExclusive + self._allStopElements = self._stopElementsInclusive + self._stopElementsExclusive \ No newline at end of file diff --git a/codeableModels/cbundle.py b/codeableModels/cbundle.py new file mode 100644 index 0000000..9543875 --- /dev/null +++ b/codeableModels/cbundle.py @@ -0,0 +1,161 @@ +from codeableModels.cobject import CObject +from codeableModels.cclassifier import CClassifier +from codeableModels.cexception import CException +from codeableModels.cmetaclass import CMetaclass +from codeableModels.cbundlable import CBundlable +from codeableModels.cstereotype import CStereotype +from codeableModels.cclass import CClass +from codeableModels.cenum import CEnum +from codeableModels.internal.commons import isCNamedElement, setKeywordArgs, checkNamedElementIsNotDeleted + +class CBundle(CBundlable): + def __init__(self, name=None, **kwargs): + self._elements = [] + super().__init__(name, **kwargs) + + def _initKeywordArgs(self, legalKeywordArgs = None, **kwargs): + if legalKeywordArgs == None: + legalKeywordArgs = [] + legalKeywordArgs.append("elements") + super()._initKeywordArgs(legalKeywordArgs, **kwargs) + + def add(self, elt): + if elt != None: + if elt in self._elements: + raise CException(f"element '{elt!s}' cannot be added to bundle: element is already in bundle") + if isinstance(elt, CBundlable): + self._elements.append(elt) + elt._bundles.append(self) + return + raise CException(f"can't add '{elt!s}': not an element") + + def remove(self, element): + if (element == None or + (not isinstance(element, CBundlable)) or + (not self in element.bundles)): + raise CException(f"'{element!s}' is not an element of the bundle") + self._elements.remove(element) + element._bundles.remove(self) + + def delete(self): + if self._isDeleted == True: + return + elementsToDelete = list(self._elements) + for e in elementsToDelete: + e._bundles.remove(self) + self._elements = [] + super().delete() + + @property + def elements(self): + return list(self._elements) + + @elements.setter + def elements(self, elements): + if elements == None: + elements = [] + for e in self._elements: + e._bundle = None + self._elements = [] + if isCNamedElement(elements): + elements = [elements] + elif not isinstance(elements, list): + raise CException(f"elements requires a list or a named element as input") + for e in elements: + if e != None: + checkNamedElementIsNotDeleted(e) + else: + raise CException(f"'None' cannot be an element of bundle") + isCNamedElement(e) + if e in self._elements: + raise CException(f"'{e.name!s}' is already an element of bundle '{self.name!s}'") + self._elements.append(e) + e._bundles.append(self) + + def getElements(self, **kwargs): + type = None + name = None + # use this as name can also be provided as None + nameSpecified = False + for key in kwargs: + if key == "type": + type = kwargs["type"] + elif key == "name": + name = kwargs["name"] + nameSpecified = True + else: + raise CException(f"unknown argument to getElements: '{key!s}'") + elements = [] + for elt in self._elements: + append = True + if nameSpecified and elt.name != name: + append = False + if type != None and not isinstance(elt, type): + append = False + if append: + elements.append(elt) + return elements + + def getElement(self, **kwargs): + l = self.getElements(**kwargs) + return None if len(l) == 0 else l[0] + + def _computeConnected(self, context): + super()._computeConnected(context) + if context.processBundles == False: + return + connected = [] + for element in self._elements: + if not element in context.stopElementsExclusive: + connected.append(element) + self._appendConnected(context, connected) + +class CPackage(CBundle): + pass + +class CLayer(CBundle): + def __init__(self, name=None, **kwargs): + self._subLayer = None + self._superLayer = None + super().__init__(name, **kwargs) + + def _initKeywordArgs(self, legalKeywordArgs = None, **kwargs): + if legalKeywordArgs == None: + legalKeywordArgs = [] + legalKeywordArgs.append("subLayer") + legalKeywordArgs.append("superLayer") + super()._initKeywordArgs(legalKeywordArgs, **kwargs) + + @property + def subLayer(self): + return self._subLayer + + @subLayer.setter + def subLayer(self, layer): + if layer != None and not isinstance(layer, CLayer): + raise CException(f"not a layer: {layer!s}") + if self._subLayer != None: + self._subLayer._superLayer = None + self._subLayer = layer + if layer != None: + if layer._superLayer != None: + layer._superLayer._subLayer = None + layer._superLayer = self + + @property + def superLayer(self): + return self._superLayer + + @superLayer.setter + def superLayer(self, layer): + if layer != None and not isinstance(layer, CLayer): + raise CException(f"not a layer: {layer!s}") + if self._superLayer != None: + self._superLayer._subLayer = None + self._superLayer = layer + if layer != None: + if layer._subLayer != None: + layer._subLayer._superLayer = None + layer._subLayer = self + + diff --git a/codeableModels/cclass.py b/codeableModels/cclass.py new file mode 100644 index 0000000..74e3b65 --- /dev/null +++ b/codeableModels/cclass.py @@ -0,0 +1,164 @@ +from codeableModels.cclassifier import CClassifier +from codeableModels.internal.commons import checkIsCMetaclass, checkIsCObject, checkIsCStereotype, isCStereotype, checkNamedElementIsNotDeleted +from codeableModels.cexception import CException +from codeableModels.cobject import CObject +from codeableModels.internal.taggedvalues import CTaggedValues +from codeableModels.internal.stereotype_holders import CStereotypeInstancesHolder + +class CClass(CClassifier): + def __init__(self, metaclass, name=None, **kwargs): + self._metaclass = None + self.metaclass = metaclass + self._objects = [] + self._classObject = CObject(self.metaclass, name, _classObjectClass = self) + self._stereotypeInstancesHolder = CStereotypeInstancesHolder(self) + self._taggedValues = CTaggedValues() + super().__init__(name, **kwargs) + + def _initKeywordArgs(self, legalKeywordArgs = None, **kwargs): + if legalKeywordArgs == None: + legalKeywordArgs = [] + legalKeywordArgs.append("stereotypeInstances") + super()._initKeywordArgs(legalKeywordArgs, **kwargs) + + @property + def metaclass(self): + return self._metaclass + + @property + def classObject(self): + return self._classObject + + @metaclass.setter + def metaclass(self, mcl): + checkIsCMetaclass(mcl) + if (self._metaclass != None): + self._metaclass._removeClass(self) + if mcl != None: + checkNamedElementIsNotDeleted(mcl) + self._metaclass = mcl + self._metaclass._addClass(self) + + @property + def objects(self): + return list(self._objects) + + @property + def allObjects(self): + allObjects = list(self._objects) + for scl in self.allSubclasses: + for cl in scl._objects: + allObjects.append(cl) + return allObjects + + def _addObject(self, obj): + if obj in self._objects: + raise CException(f"object '{obj!s}' is already an instance of the class '{self!s}'") + checkIsCObject(obj) + self._objects.append(obj) + + def _removeObject(self, obj): + if not obj in self._objects: + raise CException(f"can't remove object '{obj!s}'' from class '{self!s}': not an instance") + self._objects.remove(obj) + + def delete(self): + if self._isDeleted == True: + return + + objectsToDelete = list(self._objects) + for obj in objectsToDelete: + obj.delete() + self._objects = [] + + for si in self.stereotypeInstances: + si._extendedInstances.remove(self) + self._stereotypeInstancesHolder._stereotypes = [] + + self.metaclass._removeClass(self) + self._metaclass = None + + super().delete() + + self._classObject.delete() + + + @property + def classPath(self): + return self._classObject.classPath + + def instanceOf(self, cl): + return self._classObject.instanceOf(cl) + + def _updateDefaultValuesOfClassifier(self, attribute = None): + for i in self.allObjects: + attrItems = self._attributes.items() + if attribute != None: + attrItems = {attribute._name: attribute}.items() + for attrName, attr in attrItems: + if attr.default != None: + if i.getValue(attrName, self) == None: + i.setValue(attrName, attr.default, self) + + def _removeAttributeValuesOfClassifier(self, attributesToKeep): + for i in self.allObjects: + for attrName in self.attributeNames: + if not attrName in attributesToKeep: + i._removeValue(attrName, self) + + def getValue(self, attributeName, cl = None): + return self._classObject.getValue(attributeName, cl) + + def setValue(self, attributeName, value, cl = None): + return self._classObject.setValue(attributeName, value, cl) + + def _removeValue(self, attributeName, cl): + return self._classObject._removeValue(attributeName, cl) + + def getObjects(self, name): + return list(o for o in self.objects if o.name == name) + def getObject(self, name): + l = self.getObjects(name) + return None if len(l) == 0 else l[0] + + @property + def stereotypeInstances(self): + return self._stereotypeInstancesHolder.stereotypes + + @stereotypeInstances.setter + def stereotypeInstances(self, elements): + self._stereotypeInstancesHolder.stereotypes = elements + + def getTaggedValue(self, name, stereotype = None): + return self._taggedValues.getTaggedValue(name, self._stereotypeInstancesHolder.getStereotypeInstancePath(), stereotype) + + def setTaggedValue(self, name, value, stereotype = None): + self._taggedValues.setTaggedValue(name, value, self._stereotypeInstancesHolder.getStereotypeInstancePath(), stereotype) + + def _removeTaggedValue(self, attributeName, stereotype): + self._taggedValues.removeTaggedValue(attributeName, stereotype) + + def association(self, target, descriptor = None, **kwargs): + if not isinstance(target, CClass): + raise CException(f"class '{self!s}' is not compatible with association target '{target!s}'") + return super(CClass, self).association(target, descriptor, **kwargs) + + @property + def linkObjects(self): + return self._classObject.linkObjects + + @property + def links(self): + return self._classObject.links + + def getLinks(self, **kwargs): + return self._classObject.getLinks(**kwargs) + + def _getLinksForAssociation(self, association): + return self._classObject._getLinksForAssociation(association) + + def addLinks(self, links, **kwargs): + return self._classObject.addLinks(links, **kwargs) + + def deleteLinks(self, links, **kwargs): + return self._classObject.deleteLinks(links, **kwargs) \ No newline at end of file diff --git a/codeableModels/cclassifier.py b/codeableModels/cclassifier.py new file mode 100644 index 0000000..0416711 --- /dev/null +++ b/codeableModels/cclassifier.py @@ -0,0 +1,206 @@ +from codeableModels.cbundlable import CBundlable +from codeableModels.cenum import CEnum +from codeableModels.cattribute import CAttribute +from codeableModels.cassociation import CAssociation +from codeableModels.cexception import CException +from codeableModels.internal.commons import setKeywordArgs, isKnownAttributeType, isCAttribute, isCClassifier, checkNamedElementIsNotDeleted + +class CClassifier(CBundlable): + def __init__(self, name=None, **kwargs): + self._superclasses = [] + self._subclasses = [] + self._attributes = {} + self._associations = [] + super().__init__(name, **kwargs) + + def _initKeywordArgs(self, legalKeywordArgs = None, **kwargs): + if legalKeywordArgs == None: + legalKeywordArgs = [] + legalKeywordArgs.append("attributes") + legalKeywordArgs.append("superclasses") + super()._initKeywordArgs(legalKeywordArgs, **kwargs) + + @property + def attributes(self): + return list(self._attributes.values()) + + def _setAttribute(self, name, value): + if name in self._attributes.keys(): + raise CException(f"duplicate attribute name: '{name!s}'") + attr = None + if isCAttribute(value): + attr = value + elif isKnownAttributeType(value) or isinstance(value, CEnum) or isinstance(value, CClassifier): + attr = CAttribute(type = value) + else: + attr = CAttribute(default = value) + attr._name = name + attr._classifier = self + self._attributes.update({name: attr}) + + @attributes.setter + def attributes(self, attributeDescriptions): + if attributeDescriptions == None: + attributeDescriptions = {} + self._removeAttributeValuesOfClassifier(attributeDescriptions.keys()) + self._attributes = {} + if not isinstance(attributeDescriptions, dict): + raise CException(f"malformed attribute description: '{attributeDescriptions!s}'") + for attributeName in attributeDescriptions: + self._setAttribute(attributeName, attributeDescriptions[attributeName]) + self._updateDefaultValuesOfClassifier() + + def _removeAttributeValuesOfClassifier(self, attributesToKeep): + raise CException("should be overridden by subclasses to update defaults on instances") + def _updateDefaultValuesOfClassifier(self, attribute = None): + raise CException("should be overridden by subclasses to update defaults on instances") + + def _checkSameTypeAsSelf(self, cl): + return isinstance(cl, self.__class__) + + @property + def subclasses(self): + return list(self._subclasses) + + @property + def superclasses(self): + return list(self._superclasses) + + @superclasses.setter + def superclasses(self, elements): + if elements == None: + elements = [] + for sc in self._superclasses: + sc._subclasses.remove(self) + self._superclasses = [] + if isCClassifier(elements): + elements = [elements] + for scl in elements: + if scl != None: + checkNamedElementIsNotDeleted(scl) + if not isinstance(scl, self.__class__): + raise CException(f"cannot add superclass '{scl!s}' to '{self!s}': not of type {self.__class__!s}") + if scl in self._superclasses: + raise CException(f"'{scl.name!s}' is already a superclass of '{self.name!s}'") + self._superclasses.append(scl) + scl._subclasses.append(self) + + @property + def allSuperclasses(self): + return self._getAllSuperclasses() + + @property + def allSubclasses(self): + return self._getAllSubclasses() + + def conformsToType(self, classifier): + typeClassifiers = classifier.allSubclasses + typeClassifiers.add(classifier) + if self in typeClassifiers: + return True + return False + + @property + def attributeNames(self): + return list(self._attributes.keys()) + + def getAttribute(self, attributeName): + if attributeName == None or not isinstance(attributeName, str): + return None + try: + return self._attributes[attributeName] + except KeyError: + return None + + def _removeAllAssociations(self): + associations = self.associations.copy() + for association in associations: + association.delete() + + def _removeSubclass(self, cl): + if not cl in self._subclasses: + raise CException(f"can't remove subclass '{cl!s}' from classifier '{self!s}': not a subclass") + self._subclasses.remove(cl) + + def _removeSuperclass(self, cl): + if not cl in self._superclasses: + raise CException(f"can't remove superclass '{cl!s}' from classifier '{self!s}': not a superclass") + self._superclasses.remove(cl) + + def delete(self): + if self._isDeleted == True: + return + super().delete() + # self.superclasses removes the self subclass from the superclasses + self.superclasses = [] + for subclass in self._subclasses: + subclass._removeSuperclass(self) + self._subclasses = [] + self._removeAllAssociations() + for a in self.attributes: + a._name = None + a._classifier = None + self._attributes = {} + + def _getAllSuperclasses(self, iteratedClasses = None): + if iteratedClasses == None: + iteratedClasses = set() + result = set() + for sc in self.superclasses: + if not sc in iteratedClasses: + iteratedClasses.add(sc) + result.add(sc) + result.update(sc._getAllSuperclasses(iteratedClasses)) + return result + + def _getAllSubclasses(self, iteratedClasses = None): + if iteratedClasses == None: + iteratedClasses = set() + result = set() + for sc in self.subclasses: + if not sc in iteratedClasses: + iteratedClasses.add(sc) + result.add(sc) + result.update(sc._getAllSubclasses(iteratedClasses)) + return result + + def hasSubclass(self, cl): + return (cl in self._getAllSubclasses()) + + def hasSuperclass(self, cl): + return (cl in self._getAllSuperclasses()) + + @property + def associations(self): + return list(self._associations) + + @property + def allAssociations(self): + allAssociations = self.associations + for sc in self.allSuperclasses: + for a in sc.associations: + if not a in allAssociations: + allAssociations.extend([a]) + return allAssociations + + def association(self, target, descriptor = None, **kwargs): + a = CAssociation(self, target, descriptor, **kwargs) + self._associations.append(a) + if self != target: + target._associations.append(a) + return a + + def _computeConnected(self, context): + super()._computeConnected(context) + connectedCandidates = [] + connected = [] + for association in self.associations: + connectedCandidates.append(association._getOppositeClass(self)) + connectedCandidates = self.superclasses + self.subclasses + connectedCandidates + for c in connectedCandidates: + if not c in context.stopElementsExclusive: + connected.append(c) + self._appendConnected(context, connected) + + + diff --git a/codeableModels/cenum.py b/codeableModels/cenum.py new file mode 100644 index 0000000..041be7d --- /dev/null +++ b/codeableModels/cenum.py @@ -0,0 +1,37 @@ +from codeableModels.cbundlable import CBundlable +from codeableModels.internal.commons import setKeywordArgs +from codeableModels.cexception import CException + +class CEnum(CBundlable): + def __init__(self, name = None, **kwargs): + self._values = [] + super().__init__(name, **kwargs) + + def _initKeywordArgs(self, legalKeywordArgs = None, **kwargs): + if legalKeywordArgs == None: + legalKeywordArgs = [] + legalKeywordArgs.append("values") + super()._initKeywordArgs(legalKeywordArgs, **kwargs) + + @property + def values(self): + return list(self._values) + + @values.setter + def values(self, values): + if values == None: + values = [] + if not isinstance(values, list): + raise CException(f"an enum needs to be initialized with a list of values, but got: '{values!s}'") + self._values = values + + def isLegalValue(self, value): + if value in self._values: + return True + return False + + def delete(self): + if self._isDeleted == True: + return + self._values = [] + super().delete() \ No newline at end of file diff --git a/codeableModels/cexception.py b/codeableModels/cexception.py new file mode 100644 index 0000000..87dcdff --- /dev/null +++ b/codeableModels/cexception.py @@ -0,0 +1,7 @@ +class CException(Exception): + def __init__(self, value): + self.value = value + def __str__(self): + return self.__repr__() + def __repr__(self): + return repr(self.value) \ No newline at end of file diff --git a/codeableModels/clink.py b/codeableModels/clink.py new file mode 100644 index 0000000..44c6f53 --- /dev/null +++ b/codeableModels/clink.py @@ -0,0 +1,315 @@ +from codeableModels.internal.commons import setKeywordArgs, isCObject, isCClass, getCommonMetaclass, getCommonClassifier, checkIsCAssociation, checkIsCObject +from codeableModels.cexception import CException +from codeableModels.internal.taggedvalues import CTaggedValues +from codeableModels.internal.stereotype_holders import CStereotypeInstancesHolder + +class CLink(object): + def __init__(self, association, sourceObject, targetObject, **kwargs): + self._isDeleted = False + self._source = sourceObject + self._target = targetObject + self.association = association + self._stereotypeInstancesHolder = CStereotypeInstancesHolder(self) + self._taggedValues = CTaggedValues() + super().__init__() + self._initKeywordArgs(**kwargs) + + def _initKeywordArgs(self, legalKeywordArgs = None, **kwargs): + if legalKeywordArgs == None: + legalKeywordArgs = [] + legalKeywordArgs.append("stereotypeInstances") + setKeywordArgs(self, legalKeywordArgs, **kwargs) + + def _getOppositeObject(self, object): + if object == self._source: + return self._target + else: + return self._source + + @property + def roleName(self): + return self.association.roleName + + @property + def sourceRoleName(self): + return self.association.sourceRoleName + + @property + def source(self): + if self._source._classObjectClass != None: + return self._source._classObjectClass + return self._source + + @property + def target(self): + if self._target._classObjectClass != None: + return self._target._classObjectClass + return self._target + + def delete(self): + if self._isDeleted == True: + return + self._isDeleted = True + for si in self.stereotypeInstances: + si._extendedInstances.remove(self) + self._stereotypeInstancesHolder._stereotypes = [] + if self._source != self._target: + self._target._linkObjects.remove(self) + self._source._linkObjects.remove(self) + + @property + def stereotypeInstances(self): + return self._stereotypeInstancesHolder.stereotypes + + @stereotypeInstances.setter + def stereotypeInstances(self, elements): + self._stereotypeInstancesHolder.stereotypes = elements + + def getTaggedValue(self, name, stereotype = None): + return self._taggedValues.getTaggedValue(name, self._stereotypeInstancesHolder.getStereotypeInstancePath(), stereotype) + + def setTaggedValue(self, name, value, stereotype = None): + self._taggedValues.setTaggedValue(name, value, self._stereotypeInstancesHolder.getStereotypeInstancePath(), stereotype) + + def _removeTaggedValue(self, attributeName, stereotype): + self._taggedValues.removeTaggedValue(attributeName, stereotype) + +def _getTargetObjectsFromDefinition(targets, isClassLinks): + if targets == None: + targets = [] + elif not isinstance(targets, list): + targets = [targets] + newTargets = [] + for t in targets: + if isCClass(t): + if not isClassLinks: + raise CException(f"link target '{t!s}' is a class, but source is an object") + newTargets.append(t._classObject) + elif isCObject(t): + if isClassLinks and t._classObjectClass == None: + raise CException(f"link target '{t!s}' is an object, but source is an class") + if not isClassLinks and t._classObjectClass != None: + raise CException(f"link target '{t!s}' is an class, but source is an object") + newTargets.append(t) + else: + raise CException(f"link target '{t!s}' is neither an object nor a class") + return newTargets + +def _checkLinkDefinitionAndReplaceClasses(linkDefinitions): + if not isinstance(linkDefinitions, dict): + raise CException("link definitions should be of the form {: , ..., : }") + + newDefs = {} + for source in linkDefinitions: + sourceObj = source + if source == None or source == []: + raise CException("link should not contain an empty source") + if isCClass(source): + sourceObj = source._classObject + elif not isCObject(source): + raise CException(f"link source '{source!s}' is neither an object nor a class") + targets = _getTargetObjectsFromDefinition(linkDefinitions[source], sourceObj._classObjectClass != None) + newDefs[sourceObj] = targets + + return newDefs + +def _determineMatchingAssociationAndSetContextInfo(context, source, targets): + if source._classObjectClass != None: + context.targetClassifier = getCommonMetaclass([co._classObjectClass for co in targets]) + context.sourceClassifier = source._classObjectClass.metaclass + else: + context.targetClassifier = getCommonClassifier(targets) + context.sourceClassifier = source.classifier + + if context.association != None and context.targetClassifier == None: + if context.sourceClassifier.conformsToType(context.association.source): + context.targetClassifier = context.association.target + context.sourceClassifier = context.association.source + elif context.sourceClassifier.conformsToType(context.association.target): + context.targetClassifier = context.association.source + context.sourceClassifier = context.association.target + + associations = context.sourceClassifier.allAssociations + if context.association != None: + associations = [context.association] + matchesAssociationOrder = [] + matchesReverseAssociationOrder = [] + for association in associations: + if (association._matchesTarget(context.targetClassifier, context.roleName) and + association._matchesSource(context.sourceClassifier, None)): + matchesAssociationOrder.append(association) + elif (association._matchesSource(context.targetClassifier, context.roleName) and + association._matchesTarget(context.sourceClassifier, None)): + matchesReverseAssociationOrder.append(association) + matches = len(matchesAssociationOrder) + len(matchesReverseAssociationOrder) + if matches == 1: + if len(matchesAssociationOrder) == 1: + context.association = matchesAssociationOrder[0] + context.matchesInOrder[source] = True + else: + context.association = matchesReverseAssociationOrder[0] + context.matchesInOrder[source] = False + elif matches == 0: + raise CException(f"matching association not found for source '{source!s}' and targets '{[str(item) for item in targets]!s}'") + else: + raise CException(f"link specification ambiguous, multiple matching associations found for source '{source!s}' and targets '{[str(item) for item in targets]!s}'") + + +def _linkObjects(context, source, targets): + newLinks = [] + sourceObj = source + if isCClass(source): + sourceObj = source._classObject + for t in targets: + target = t + if isCClass(t): + target = t._classObject + + sourceForLink = sourceObj + targetForLink = target + if not context.matchesInOrder[sourceObj]: + sourceForLink = target + targetForLink = sourceObj + for existingLink in sourceObj._linkObjects: + if (existingLink._source == sourceForLink and existingLink._target == targetForLink + and existingLink.association == context.association): + for link in newLinks: + link.delete() + raise CException(f"trying to link the same link twice '{source!s} -> {target!s}'' twice for the same association") + link = CLink(context.association, sourceForLink, targetForLink) + + newLinks.append(link) + sourceObj._linkObjects.append(link) + # for links from this object to itself, store only one link object + if sourceObj != target: + target._linkObjects.append(link) + if context.stereotypeInstances != None: + link.stereotypeInstances = context.stereotypeInstances + return newLinks + +def _removeLinksForAssociations(context, source, targets): + if not source in context.objectLinksHaveBeenRemoved: + context.objectLinksHaveBeenRemoved.append(source) + for link in source._getLinksForAssociation(context.association): + link.delete() + for target in targets: + if not target in context.objectLinksHaveBeenRemoved: + context.objectLinksHaveBeenRemoved.append(target) + for link in target._getLinksForAssociation(context.association): + link.delete() + + +def setLinks(linkDefinitions, addLinks = False, **kwargs): + context = LinkKeywordsContext(**kwargs) + linkDefinitions = _checkLinkDefinitionAndReplaceClasses(linkDefinitions) + + newLinks = [] + for source in linkDefinitions: + targets = linkDefinitions[source] + _determineMatchingAssociationAndSetContextInfo(context, source, targets) + if not addLinks: + _removeLinksForAssociations(context, source, targets) + try: + newLinks.extend(_linkObjects(context, source, targets)) + except CException as e: + for link in newLinks: + link.delete() + raise e + try: + for source in linkDefinitions: + targets = linkDefinitions[source] + sourceLen = len(source._getLinksForAssociation(context.association)) + if len(targets) == 0: + context.association._checkMultiplicity(source, sourceLen, 0, context.matchesInOrder[source]) + else: + for target in targets: + targetLen = len(target._getLinksForAssociation(context.association)) + context.association._checkMultiplicity(source, sourceLen, targetLen, context.matchesInOrder[source]) + context.association._checkMultiplicity(target, targetLen, sourceLen, not context.matchesInOrder[source]) + except CException as e: + for link in newLinks: + link.delete() + raise e + return newLinks + +def addLinks(linkDefinitions, **kwargs): + return setLinks(linkDefinitions, True, **kwargs) + +def deleteLinks(linkDefinitions, **kwargs): + # stereotypeInstances is not supported for delete links + if "stereotypeInstances" in kwargs: + raise CException(f"unknown keywords argument") + + context = LinkKeywordsContext(**kwargs) + linkDefinitions = _checkLinkDefinitionAndReplaceClasses(linkDefinitions) + + for source in linkDefinitions: + targets = linkDefinitions[source] + #_determineMatchingAssociationAndSetContextInfo(context, source, targets) + + for target in targets: + matchingLink = None + for link in source._linkObjects: + if context.association != None and link.association != context.association: + continue + # if ((context.matchesInOrder[source] and source == link._source and target == link._target) or + # (not context.matchesInOrder[source] and target == link._source and source == link._target)): + matchesInOrder = True + matches = False + if (source == link._source and target == link._target): + matches = True + if context.roleName != None and not link.association.roleName == context.roleName: + matches = False + if (target == link._source and source == link._target): + matches = True + matchesInOrder = False + if context.roleName != None and not link.association.sourceRoleName == context.roleName: + matches = False + if matches: + if matchingLink == None: + matchingLink = link + else: + raise CException(f"link definition in delete links ambiguous for link '{source!s}->{target!s}': found multiple matches") + if matchingLink == None: + roleNameString = "" + if context.roleName != None: + roleNameString = f" for given role name '{context.roleName!s}'" + associationString = "" + if context.association != None: + associationString = f" for given association" + if roleNameString != "": + associationString = " and" + associationString + raise CException(f"no link found for '{source!s} -> {target!s}' in delete links" + roleNameString + associationString) + else: + sourceLen = len(source._getLinksForAssociation(matchingLink.association)) - 1 + targetLen = len(target._getLinksForAssociation(matchingLink.association)) - 1 + matchingLink.association._checkMultiplicity(source, sourceLen, targetLen, matchesInOrder) + matchingLink.association._checkMultiplicity(target, targetLen, sourceLen, not matchesInOrder) + matchingLink.delete() + + def delete(self): + if self._isDeleted == True: + return + self._isDeleted = True + for si in self.stereotypeInstances: + si._extendedInstances.remove(self) + self._stereotypeInstancesHolder._stereotypes = [] + if self._source != self._target: + self._target._linkObjects.remove(self) + self._source._linkObjects.remove(self) + + + +class LinkKeywordsContext(object): + def __init__(self, **kwargs): + self.roleName = kwargs.pop("roleName", None) + self.association = kwargs.pop("association", None) + self.stereotypeInstances = kwargs.pop("stereotypeInstances", None) + if len(kwargs) != 0: + raise CException(f"unknown keywords argument") + if self.association != None: + checkIsCAssociation(self.association) + self.sourceClassifier = None + self.targetClassifier = None + self.matchesInOrder = {} + self.objectLinksHaveBeenRemoved = [] diff --git a/codeableModels/cmetaclass.py b/codeableModels/cmetaclass.py new file mode 100644 index 0000000..49c96f2 --- /dev/null +++ b/codeableModels/cmetaclass.py @@ -0,0 +1,100 @@ +from codeableModels.cclassifier import CClassifier +from codeableModels.cexception import CException +from codeableModels.internal.commons import checkIsCClass, setKeywordArgs, checkIsCStereotype, isCStereotype, checkNamedElementIsNotDeleted +from codeableModels.internal.stereotype_holders import CStereotypesHolder + +class CMetaclass(CClassifier): + def __init__(self, name=None, **kwargs): + self._classes = [] + self._stereotypesHolder = CStereotypesHolder(self) + super().__init__(name, **kwargs) + + def _initKeywordArgs(self, legalKeywordArgs = None, **kwargs): + if legalKeywordArgs == None: + legalKeywordArgs = [] + legalKeywordArgs.append("stereotypes") + super()._initKeywordArgs(legalKeywordArgs, **kwargs) + + @property + def classes(self): + return list(self._classes) + + @property + def allClasses(self): + allClasses = list(self._classes) + for scl in self.allSubclasses: + for cl in scl._classes: + allClasses.append(cl) + return allClasses + + def getClasses(self, name): + return list(cl for cl in self.classes if cl.name == name) + def getClass(self, name): + l = self.getClasses(name) + return None if len(l) == 0 else l[0] + + def getStereotypes(self, name): + return list(cl for cl in self.stereotypes if cl.name == name) + def getStereotype(self, name): + l = self.getStereotypes(name) + return None if len(l) == 0 else l[0] + + def _addClass(self, cl): + checkIsCClass(cl) + if cl in self._classes: + raise CException(f"class '{cl!s}' is already a class of the metaclass '{self!s}'") + self._classes.append(cl) + + def _removeClass(self, cl): + if not cl in self._classes: + raise CException(f"can't remove class instance '{cl!s}' from metaclass '{self!s}': not a class instance") + self._classes.remove(cl) + + def delete(self): + if self._isDeleted == True: + return + classesToDelete = list(self._classes) + for cl in classesToDelete: + cl.delete() + self._classes = [] + for s in self._stereotypesHolder._stereotypes: + s._extended.remove(self) + self._stereotypesHolder._stereotypes = [] + super().delete() + + @property + def stereotypes(self): + return self._stereotypesHolder.stereotypes + + @stereotypes.setter + def stereotypes(self, elements): + self._stereotypesHolder.stereotypes = elements + + def _updateDefaultValuesOfClassifier(self, attribute = None): + for i in self.allClasses: + attrItems = self._attributes.items() + if attribute != None: + attrItems = {attribute._name: attribute}.items() + for attrName, attr in attrItems: + if attr.default != None: + if i.getValue(attrName, self) == None: + i.setValue(attrName, attr.default, self) + + def _removeAttributeValuesOfClassifier(self, attributesToKeep): + for i in self.allClasses: + for attrName in self.attributeNames: + if not attrName in attributesToKeep: + i._removeValue(attrName, self) + + def association(self, target, descriptor = None, **kwargs): + if not isinstance(target, CMetaclass): + raise CException(f"metaclass '{self!s}' is not compatible with association target '{target!s}'") + return super(CMetaclass, self).association(target, descriptor, **kwargs) + + def _computeConnected(self, context): + super()._computeConnected(context) + connected = [] + for s in self.stereotypes: + if not s in context.stopElementsExclusive: + connected.append(s) + self._appendConnected(context, connected) \ No newline at end of file diff --git a/codeableModels/cnamedelement.py b/codeableModels/cnamedelement.py new file mode 100644 index 0000000..4b9e341 --- /dev/null +++ b/codeableModels/cnamedelement.py @@ -0,0 +1,37 @@ +from codeableModels.internal.commons import setKeywordArgs, checkNamedElementIsNotDeleted +from codeableModels.cexception import CException + +class CNamedElement(object): + def __init__(self, name, **kwargs): + self.name = name + super().__init__() + self._isDeleted = False + if name != None and not isinstance(name, str): + raise CException(f"is not a name string: '{name!r}'") + self._initKeywordArgs(**kwargs) + + def __str__(self): + if (self.name == None): + return "" + return self.name + def __repr__(self): + result = super().__repr__() + if (self.name != None): + result = f"{result}: {self.name!s}" + return result + + def _initKeywordArgs(self, legalKeywordArgs = None, **kwargs): + if legalKeywordArgs == None: + legalKeywordArgs = [] + setKeywordArgs(self, legalKeywordArgs, **kwargs) + + def delete(self): + if self._isDeleted == True: + return + self.name = None + self._isDeleted = True + + + + + diff --git a/codeableModels/cobject.py b/codeableModels/cobject.py new file mode 100644 index 0000000..82c0983 --- /dev/null +++ b/codeableModels/cobject.py @@ -0,0 +1,215 @@ +from codeableModels.cbundlable import CBundlable +from codeableModels.cmetaclass import CMetaclass +from codeableModels.cexception import CException +from codeableModels.clink import CLink, LinkKeywordsContext, addLinks, deleteLinks +from codeableModels.internal.commons import (checkIsCClass, isCClass, setKeywordArgs, isCMetaclass, + checkNamedElementIsNotDeleted, getCommonClassifier, checkIsCommonClassifier, checkIsCAssociation, + checkIsCClassifier, getCommonMetaclass) + +class CObject(CBundlable): + def __init__(self, cl, name=None, **kwargs): + self._classObjectClass = None + if '_classObjectClass' in kwargs: + classObjectClass = kwargs.pop('_classObjectClass', None) + self._classObjectClass = classObjectClass + else: + # don't check if this is a class object, as classifier is then a metaclass + checkIsCClass(cl) + if cl != None: + checkNamedElementIsNotDeleted(cl) + super().__init__(name, **kwargs) + self._classifier = cl + if self._classObjectClass == None: + # don't add instance if this is a class object + self._classifier._addObject(self) + self._initAttributeValues() + self._linkObjects = [] + + def _initAttributeValues(self): + self._attributeValues = {} + # init default values of attributes + for cl in self.classPath: + for attrName, attr in cl._attributes.items(): + if attr.default != None: + self.setValue(attrName, attr.default, cl) + + @property + def classifier(self): + return self._classifier + + @classifier.setter + def classifier(self, cl): + checkIsCClass(cl) + if (self._classifier != None): + self._classifier._removeObject(self) + if cl != None: + checkNamedElementIsNotDeleted(cl) + self._classifier = cl + self._classifier._addObject(self) + + def _getClassPath(self, classifier = None): + if classifier == None: + classifier = self.classifier + classPath = [classifier] + for sc in classifier.superclasses: + for cl in self._getClassPath(sc): + if not cl in classPath: + classPath.append(cl) + return classPath + + @property + def classPath(self): + return self._getClassPath() + + def delete(self): + if self._isDeleted == True: + return + if not isinstance(self.classifier, CMetaclass): + # for class objects, the class cleanup removes the instance + self._classifier._removeObject(self) + self._classifier = None + super().delete() + + def instanceOf(self, cl): + if self.classifier == None: + return False + if cl == None: + raise CException(f"'None' is not a valid argument") + if isinstance(self.classifier, CMetaclass): + # this is a class object + if not isCMetaclass(cl): + raise CException(f"'{cl!s}' is not a metaclass") + else: + if not isCClass(cl): + raise CException(f"'{cl!s}' is not a class") + + if self.classifier == cl: + return True + if cl in self.classifier.allSuperclasses: + return True + return False + + def _checkIsAttributeOfOwnClassifier(self, attributeName, classifier): + attribute = classifier.getAttribute(attributeName) + if attribute == None: + raise CException(f"attribute '{attributeName!s}' unknown for '{classifier!s}'") + if not classifier in self.classPath: + raise CException(f"'{classifier!s}' is not a classifier of '{self!s}'") + return attribute + + def getValue(self, attributeName, classifier = None): + if self._isDeleted: + raise CException(f"can't get value '{attributeName!s}' on deleted object") + if classifier == None: + # search on class path + for cl in self.classPath: + if cl.getAttribute(attributeName) != None: + return self.getValue(attributeName, cl) + raise CException(f"attribute '{attributeName!s}' unknown for object '{self!s}'") + else: + # search only on specified classifier + attribute = self._checkIsAttributeOfOwnClassifier(attributeName, classifier) + attribute.checkAttributeTypeIsNotDeleted() + try: + attributeValuesClassifier = self._attributeValues[classifier] + except KeyError: + return None + try: + return attributeValuesClassifier[attributeName] + except KeyError: + return None + + def setValue(self, attributeName, value, classifier = None): + if self._isDeleted: + raise CException(f"can't set value '{attributeName!s}' on deleted object") + if classifier == None: + # search on class path + for cl in self.classPath: + if cl.getAttribute(attributeName) != None: + return self.setValue(attributeName, value, cl) + raise CException(f"attribute '{attributeName!s}' unknown for object '{self!s}'") + else: + # search only on specified classifier + attribute = self._checkIsAttributeOfOwnClassifier(attributeName, classifier) + attribute.checkAttributeTypeIsNotDeleted() + attribute.checkAttributeValueType(attributeName, value) + try: + self._attributeValues[classifier].update({attributeName: value}) + except KeyError: + self._attributeValues[classifier] = {attributeName: value} + + def _removeValue(self, attributeName, classifier): + try: + self._attributeValues[classifier].pop(attributeName, None) + except KeyError: + return + + @property + def linkObjects(self): + return list(self._linkObjects) + + @property + def links(self): + result = [] + for link in self._linkObjects: + if self._classObjectClass != None: + result.append(link._getOppositeObject(self)._classObjectClass) + else: + result.append(link._getOppositeObject(self)) + return result + + def _getLinksForAssociation(self, association): + associationLinks = [] + for link in list(self._linkObjects): + if link.association == association: + associationLinks.extend([link]) + return associationLinks + + def _removeLinksForAssociations(self, association, matchesinAssociationsDirection, targets): + for link in self._getLinksForAssociation(association): + link.delete() + for t in targets: + for link in t._getLinksForAssociation(association): + link.delete() + + def getLinks(self, **kwargs): + context = LinkKeywordsContext(**kwargs) + + linkedObjs = [] + for l in self._linkObjects: + append = True + if context.association != None: + if l.association != context.association: + append = False + if context.roleName != None: + if ((self == l._source and not l.association.roleName == context.roleName) or + (self == l._target and not l.association.sourceRoleName == context.roleName)): + append = False + if append: + if self == l._source: + if self._classObjectClass != None: + linkedObjs.append(l._target._classObjectClass) + else: + linkedObjs.append(l._target) + else: + if self._classObjectClass != None: + linkedObjs.append(l._source._classObjectClass) + else: + linkedObjs.append(l._source) + return linkedObjs + + def addLinks(self, links, **kwargs): + return addLinks({self: links}, **kwargs) + + def deleteLinks(self, links, **kwargs): + return deleteLinks({self: links}, **kwargs) + + def _computeConnected(self, context): + super()._computeConnected(context) + connected = [] + for link in self._linkObjects: + opposite = link._getOppositeObject(self) + if not opposite in context.stopElementsExclusive: + connected.append(opposite) + self._appendConnected(context, connected) + diff --git a/codeableModels/cstereotype.py b/codeableModels/cstereotype.py new file mode 100644 index 0000000..bdc7dcd --- /dev/null +++ b/codeableModels/cstereotype.py @@ -0,0 +1,142 @@ +from codeableModels.cclassifier import CClassifier +from codeableModels.cexception import CException +from codeableModels.cmetaclass import CMetaclass +from codeableModels.cassociation import CAssociation +from codeableModels.internal.commons import setKeywordArgs, checkIsCMetaclass, isCClass, isCMetaclass, checkNamedElementIsNotDeleted, isCLink, isCAssociation, checkIsCAssociation + +class CStereotype(CClassifier): + def __init__(self, name=None, **kwargs): + self._extended = [] + self._extendedInstances = [] + self._extendedType = None + super().__init__(name, **kwargs) + + def _initKeywordArgs(self, legalKeywordArgs = None, **kwargs): + if legalKeywordArgs == None: + legalKeywordArgs = [] + legalKeywordArgs.append("extended") + super()._initKeywordArgs(legalKeywordArgs, **kwargs) + + def _determineExtendedTypeOfList(self, elements): + if len(elements) == 0: + return + if isCMetaclass(elements[0]): + self._extendedType = CMetaclass + return + if isCAssociation(elements[0]): + self._extendedType = CAssociation + return + raise CException(f"unknown type of extend element: '{elements[0]!s}'") + + @property + def extended(self): + return list(self._extended) + + @extended.setter + def extended(self, elements): + if elements == None: + elements = [] + for e in self._extended: + e._stereotypesHolder._stereotypes.remove(self) + self._extended = [] + if isCMetaclass(elements): + self._extendedType = CMetaclass + elements = [elements] + elif isCAssociation(elements): + self._extendedType = CAssociation + elements = [elements] + elif not isinstance(elements, list): + raise CException(f"extended requires a list or a metaclass as input") + else: + self._determineExtendedTypeOfList(elements) + + for e in elements: + if self._extendedType == CMetaclass: + checkIsCMetaclass(e) + elif self._extendedType == CAssociation: + checkIsCAssociation(e) + else: + raise CException(f"type of extend element incompatible: '{e!s}'") + checkNamedElementIsNotDeleted(e) + if e in self._extended: + raise CException(f"'{e.name!s}' is already extended by stereotype '{self.name!s}'") + self._extended.append(e) + e._stereotypesHolder._stereotypes.append(self) + + @property + def extendedInstances(self): + return list(self._extendedInstances) + + @property + def allExtendedInstances(self): + allInstances = list(self._extendedInstances) + for scl in self.allSubclasses: + for cl in scl._extendedInstances: + allInstances.append(cl) + return allInstances + + def delete(self): + if self._isDeleted == True: + return + for e in self._extended: + e._stereotypesHolder._stereotypes.remove(self) + self._extended = [] + super().delete() + + def _updateDefaultValuesOfClassifier(self, attribute = None): + allClasses = [self] + list(self.allSubclasses) + for sc in allClasses: + for i in sc._extendedInstances: + attrItems = self._attributes.items() + if attribute != None: + attrItems = {attribute._name: attribute}.items() + for attrName, attr in attrItems: + if attr.default != None: + if i.getTaggedValue(attrName, self) == None: + i.setTaggedValue(attrName, attr.default, self) + + def _removeAttributeValuesOfClassifier(self, attributesToKeep): + for i in self._extendedInstances: + for attrName in self.attributeNames: + if not attrName in attributesToKeep: + i._removeTaggedValue(attrName, self) + + def isMetaclassExtendedByThisStereotype(self, metaclass): + if metaclass in self._extended: + return True + for mcSuperclass in metaclass._getAllSuperclasses(): + if mcSuperclass in self._extended: + return True + return False + + def isElementExtendedByStereotype(self, element): + if isCClass(element): + if self.isMetaclassExtendedByThisStereotype(element.metaclass): + return True + for superclass in self._getAllSuperclasses(): + if superclass.isMetaclassExtendedByThisStereotype(element.metaclass): + return True + return False + elif isCLink(element): + if element.association in self.extended: + return True + for superclass in self._getAllSuperclasses(): + if element.association in superclass.extended: + return True + return False + raise CException("element is neither a metaclass nor an association") + + def association(self, target, descriptor = None, **kwargs): + if not isinstance(target, CStereotype): + raise CException(f"stereotype '{self!s}' is not compatible with association target '{target!s}'") + return super(CStereotype, self).association(target, descriptor, **kwargs) + + def _computeConnected(self, context): + super()._computeConnected(context) + if context.processStereotypes == False: + return + connected = [] + for e in self.extended: + if not e in context.stopElementsExclusive: + connected.append(e) + self._appendConnected(context, connected) \ No newline at end of file diff --git a/codeableModels/internal/commons.py b/codeableModels/internal/commons.py new file mode 100644 index 0000000..305fb22 --- /dev/null +++ b/codeableModels/internal/commons.py @@ -0,0 +1,188 @@ +from codeableModels.cexception import CException + +def setKeywordArgs(object, allowedValues, **kwargs): + for key in kwargs: + if key in allowedValues: + setattr(object, key, kwargs[key]) + else: + raise CException(f"unknown keyword argument '{key!s}', should be one of: {allowedValues!s}") + +def getAttributeType(attr): + if isinstance(attr, str): + return str + elif isinstance(attr, bool): + return bool + elif isinstance(attr, int): + return int + elif isinstance(attr, float): + return float + elif isCObject(attr): + checkNamedElementIsNotDeleted(attr) + return attr.classifier + return None + +def isKnownAttributeType(type): + if isCNamedElement(type): + return False + return (type == str or type == bool or type == int or type == float) + +def isCEnum(elt): + from codeableModels.cenum import CEnum + if isinstance(elt, CEnum): + return True + return False + +def isCClassifier(elt): + from codeableModels.cclassifier import CClassifier + if isinstance(elt, CClassifier): + return True + return False + +def isCNamedElement(elt): + from codeableModels.cnamedelement import CNamedElement + if isinstance(elt, CNamedElement): + return True + return False + +def isCAttribute(elt): + from codeableModels.cattribute import CAttribute + if isinstance(elt, CAttribute): + return True + return False + +def isCObject(elt): + from codeableModels.cobject import CObject + if isinstance(elt, CObject): + return True + return False + +def isCClass(elt): + from codeableModels.cclass import CClass + if isinstance(elt, CClass): + return True + return False + +def isCMetaclass(elt): + from codeableModels.cmetaclass import CMetaclass + if isinstance(elt, CMetaclass): + return True + return False + +def isCStereotype(elt): + from codeableModels.cstereotype import CStereotype + if isinstance(elt, CStereotype): + return True + return False + +def isCBundle(elt): + from codeableModels.cbundle import CBundle + if isinstance(elt, CBundle): + return True + return False + +def isCBundlable(elt): + from codeableModels.cbundlable import CBundlable + if isinstance(elt, CBundlable): + return True + return False + +def isCAssociation(elt): + from codeableModels.cassociation import CAssociation + if isinstance(elt, CAssociation): + return True + return False + +def isCLink(elt): + from codeableModels.clink import CLink + if isinstance(elt, CLink): + return True + return False + +def checkIsCMetaclass(elt): + if not isCMetaclass(elt): + raise CException(f"'{elt!s}' is not a metaclass") + +def checkIsCClassifier(elt): + if not isCClassifier(elt): + raise CException(f"'{elt!s}' is not a classifier") + +def checkIsCClass(elt): + if not isCClass(elt): + raise CException(f"'{elt!s}' is not a class") + +def checkIsCStereotype(elt): + if not isCStereotype(elt): + raise CException(f"'{elt!s}' is not a stereotype") + +def checkIsCObject(elt): + if not isCObject(elt): + raise CException(f"'{elt!s}' is not an object") + +def checkIsCBundle(elt): + if not isCBundle(elt): + raise CException(f"'{elt!s}' is not a bundle") + +def checkIsCAssociation(elt): + if not isCAssociation(elt): + raise CException(f"'{elt!s}' is not a association") + +def checkNamedElementIsNotDeleted(namedElement): + if namedElement._isDeleted == True: + raise CException(f"cannot access named element that has been deleted") + +# get the common (top level) classifier in a list of objects +def getCommonClassifier(objects): + commonClassifier = None + for o in objects: + if o == None or not isCObject(o): + raise CException(f"not an object: '{o!s}'") + if commonClassifier == None: + commonClassifier = o.classifier + else: + if commonClassifier == o.classifier: + continue + if commonClassifier in o.classifier.allSuperclasses: + continue + if commonClassifier in o.classifier.allSubclasses: + commonClassifier = o.classifier + continue + raise CException(f"object '{o!s}' has an incompatible classifier") + return commonClassifier + +def checkIsCommonClassifier(classifier, objects): + for o in objects: + if not o.instanceOf(classifier): + raise CException(f"object '{o!s}' not compatible with classifier '{classifier!s}'") + + +# get the common (top level) metaclass in a list of classes +def getCommonMetaclass(classes): + commonMetaclass = None + for c in classes: + if c == None or not isCClass(c): + raise CException(f"not a class: '{c!s}'") + if commonMetaclass == None: + commonMetaclass = c.metaclass + else: + if commonMetaclass == c.metaclass: + continue + if commonMetaclass in c.metaclass.allSuperclasses: + continue + if commonMetaclass in c.metaclass.allSubclasses: + commonMetaclass = c.metaclass + continue + raise CException(f"class '{c!s}' has an incompatible classifier") + return commonMetaclass + + +def getLinkObjects(objList): + result = [] + for o in objList: + obj = o + if not isCObject(o): + if isCClass(o): + obj = o.classObject + else: + raise CException(f"'{o!s}' is not an object") + result.extend(obj.linkObjects) + return result \ No newline at end of file diff --git a/codeableModels/internal/stereotype_holders.py b/codeableModels/internal/stereotype_holders.py new file mode 100644 index 0000000..0f0cf96 --- /dev/null +++ b/codeableModels/internal/stereotype_holders.py @@ -0,0 +1,99 @@ +from codeableModels.cexception import CException +from codeableModels.internal.commons import checkIsCClass, isCClass, isCLink, setKeywordArgs, checkIsCStereotype, isCStereotype, checkNamedElementIsNotDeleted + +class CStereotypesHolder: + def __init__(self, element): + self._stereotypes = [] + self.element = element + + @property + def stereotypes(self): + return list(self._stereotypes) + + # methods to be overridden in subclass + def _removeFromStereotype(self): + for s in self._stereotypes: + s._extended.remove(self.element) + def _appendOnStereotype(self, stereotype): + stereotype._extended.append(self.element) + def _checkStereotypeCanBeAdded(self, stereotype): + if stereotype in self._stereotypes: + raise CException(f"'{stereotype.name!s}' is already a stereotype of '{self.element.name!s}'") + def _initExtendedElement(self, stereotype): + pass + + # template method + def _setStereotypes(self, elements): + if elements == None: + elements = [] + self._removeFromStereotype() + self._stereotypes = [] + if isCStereotype(elements): + elements = [elements] + elif not isinstance(elements, list): + raise CException(f"a list or a stereotype is required as input") + for s in elements: + checkIsCStereotype(s) + if s != None: + checkNamedElementIsNotDeleted(s) + self._checkStereotypeCanBeAdded(s) + self._stereotypes.append(s) + self._appendOnStereotype(s) + self._initExtendedElement(s) + + @stereotypes.setter + def stereotypes(self, elements): + self._setStereotypes(elements) + + + + +class CStereotypeInstancesHolder(CStereotypesHolder): + def __init__(self, element): + super().__init__(element) + + def _setAllDefaultTaggedValuesOfStereotype(self, stereotype): + for a in stereotype.attributes: + if a.default != None: + self.element.setTaggedValue(a._name, a.default, stereotype) + + def _getStereotypeInstancePathSuperclasses(self, stereotype): + stereotypePath = [stereotype] + for superclass in stereotype.superclasses: + for superclassStereotype in self._getStereotypeInstancePathSuperclasses(superclass): + if not superclassStereotype in stereotypePath: + stereotypePath.append(superclassStereotype) + return stereotypePath + + def getStereotypeInstancePath(self): + stereotypePath = [] + for stereotypeOfThisElement in self.stereotypes: + for stereotype in self._getStereotypeInstancePathSuperclasses(stereotypeOfThisElement): + if not stereotype in stereotypePath: + stereotypePath.append(stereotype) + return stereotypePath + + def _removeFromStereotype(self): + for s in self._stereotypes: + s._extendedInstances.remove(self.element) + + def _appendOnStereotype(self, stereotype): + stereotype._extendedInstances.append(self.element) + + def _getElementNameString(self): + if isCClass(self.element): + return f"'{self.element.name!s}'" + elif isCLink(self.element): + return f"link from '{self.element.source!s}' to '{self.element.target!s}'" + raise CException(f"unexpected element type: {self.element!r}") + + def _checkStereotypeCanBeAdded(self, stereotype): + if stereotype in self._stereotypes: + raise CException(f"'{stereotype.name!s}' is already a stereotype instance on {self._getElementNameString()!s}") + if not stereotype.isElementExtendedByStereotype(self.element): + raise CException(f"stereotype '{stereotype!s}' cannot be added to {self._getElementNameString()!s}: no extension by this stereotype found") + + def _initExtendedElement(self, stereotype): + self._setAllDefaultTaggedValuesOfStereotype(stereotype) + for sc in stereotype.allSuperclasses: + self._setAllDefaultTaggedValuesOfStereotype(sc) \ No newline at end of file diff --git a/codeableModels/internal/taggedvalues.py b/codeableModels/internal/taggedvalues.py new file mode 100644 index 0000000..38b13c2 --- /dev/null +++ b/codeableModels/internal/taggedvalues.py @@ -0,0 +1,59 @@ +from codeableModels.cexception import CException + +class CTaggedValues: + def __init__(self): + self._taggedValues = {} + + def setTaggedValue(self, taggedValueName, value, legalStereotypes, stereotype = None): + if stereotype == None: + for s in legalStereotypes: + if s.getAttribute(taggedValueName) != None: + self._setValueUsingSpecifiedStereotype(taggedValueName, value, s) + return + raise CException(f"tagged value '{taggedValueName!s}' unknown") + else: + if not stereotype in legalStereotypes: + raise CException(f"stereotype '{stereotype!s}' is not a stereotype of element") + return self._setValueUsingSpecifiedStereotype(taggedValueName, value, stereotype) + + def _setValueUsingSpecifiedStereotype(self, name, value, stereotype): + attribute = stereotype.getAttribute(name) + if attribute == None: + raise CException(f"tagged value '{name!s}' unknown for stereotype '{stereotype!s}'") + attribute.checkAttributeTypeIsNotDeleted() + attribute.checkAttributeValueType(name, value) + try: + self._taggedValues[stereotype].update({name: value}) + except KeyError: + self._taggedValues[stereotype] = {name: value} + + def getTaggedValue(self, taggedValueName, legalStereotypes, stereotype = None): + if stereotype == None: + for s in legalStereotypes: + if s.getAttribute(taggedValueName) != None: + return self._getValueUsingSpecifiedStereotype(taggedValueName, s) + raise CException(f"tagged value '{taggedValueName!s}' unknown") + else: + if not stereotype in legalStereotypes: + raise CException(f"stereotype '{stereotype!s}' is not a stereotype of element") + return self._getValueUsingSpecifiedStereotype(taggedValueName, stereotype) + + def _getValueUsingSpecifiedStereotype(self, name, stereotype): + attribute = stereotype.getAttribute(name) + if attribute == None: + raise CException(f"tagged value '{name!s}' unknown for stereotype '{stereotype!s}'") + attribute.checkAttributeTypeIsNotDeleted() + try: + taggedValuesClassifier = self._taggedValues[stereotype] + except KeyError: + return None + try: + return taggedValuesClassifier[name] + except KeyError: + return None + + def removeTaggedValue(self, attributeName, stereotype): + try: + self._taggedValues[stereotype].pop(attributeName, None) + except KeyError: + return diff --git a/src/codeableModels/CAssociation.java b/src/codeableModels/CAssociation.java deleted file mode 100644 index 57abffb..0000000 --- a/src/codeableModels/CAssociation.java +++ /dev/null @@ -1,36 +0,0 @@ -package codeableModels; - -import java.util.*; - -public interface CAssociation extends CNamedElement, CStereotypedElement { - - // for Aggregation/Composition we assume end1 is the aggregating end - boolean isAggregation(); - - void setAggregation(boolean isAggregation); - - boolean isComposition(); - - void setComposition(boolean isComposition); - - List getEnds(); - - boolean hasEndType(CClassifier type); - - CAssociationEnd getEndByRoleName(String roleName); - - CAssociationEnd getEndByClassifier(CClassifier classifier); - - CAssociationEnd getOtherEnd(CAssociationEnd end) throws CException; - - CLink addLink(CAssociationEnd targetEnd, CObject object1, CObject object2) throws CException; - - List getLinksByObject(CAssociationEnd targetEnd, CObject object); - - void removeLink(CAssociationEnd targetEnd, CObject object1, CObject object2) throws CException; - - void removeLink(CLink linkToBeRemoved); - - List getLinks(); - -} \ No newline at end of file diff --git a/src/codeableModels/CAssociationEnd.java b/src/codeableModels/CAssociationEnd.java deleted file mode 100644 index 6e9ff81..0000000 --- a/src/codeableModels/CAssociationEnd.java +++ /dev/null @@ -1,16 +0,0 @@ -package codeableModels; - -public interface CAssociationEnd { - - CClassifier getClassifier(); - - String getRoleName(); - - boolean isNavigable(); - - void setNavigable(boolean isNavigable); - - CMultiplicity getMultiplicity(); - - String getMultiplicityString(); -} \ No newline at end of file diff --git a/src/codeableModels/CAttribute.java b/src/codeableModels/CAttribute.java deleted file mode 100644 index 311b4a0..0000000 --- a/src/codeableModels/CAttribute.java +++ /dev/null @@ -1,19 +0,0 @@ -package codeableModels; - -public interface CAttribute { - - String getName(); - - void setName(String name); - - String getType() throws CException; - - CClassifier getTypeClassifier(); - - CEnum getEnumType(); - - Object getDefaultValue(); - - void setDefaultValue(Object defaultValue) throws CException; - -} \ No newline at end of file diff --git a/src/codeableModels/CClass.java b/src/codeableModels/CClass.java deleted file mode 100644 index 3854a59..0000000 --- a/src/codeableModels/CClass.java +++ /dev/null @@ -1,68 +0,0 @@ -package codeableModels; - -import java.util.*; - -public interface CClass extends CClassifier, CObject, CStereotypedElementInstance { - - List getInstances(); - - List getAllInstances(); - - CMetaclass getMetaclass(); - - @Override - CClass addSuperclass(CClassifier superclass) throws CException; - - @Override - CClass addSuperclass(String superclassString) throws CException; - - @Override - CClass deleteSuperclass(String name) throws CException; - - @Override - CClass deleteSuperclass(CClassifier superclass) throws CException; - - CClass addObjectAttribute(String name, CClass classifier) throws CException; - - @Override - CClass addObjectAttribute(String name, String classifierName) throws CException; - - @Override - CClass addStringAttribute(String name) throws CException; - - @Override - CClass addIntAttribute(String name) throws CException; - - @Override - CClass addBooleanAttribute(String name) throws CException; - - @Override - CClass addFloatAttribute(String name) throws CException; - - @Override - CClass addDoubleAttribute(String name) throws CException; - - @Override - CClass addLongAttribute(String name) throws CException; - - @Override - CClass addCharAttribute(String name) throws CException; - - @Override - CClass addByteAttribute(String name) throws CException; - - @Override - CClass addShortAttribute(String name) throws CException; - - @Override - CClass addEnumAttribute(String name, CEnum enumType) throws CException; - - @Override - CClass addAttribute(String name, Object defaultValue) throws CException; - - @Override - CClass setAttributeDefaultValue(String name, Object defaultValue) throws CException; - - @Override - CClass deleteAttribute(String name) throws CException; -} \ No newline at end of file diff --git a/src/codeableModels/CClassifier.java b/src/codeableModels/CClassifier.java deleted file mode 100644 index 678cb24..0000000 --- a/src/codeableModels/CClassifier.java +++ /dev/null @@ -1,86 +0,0 @@ -package codeableModels; - -import java.util.*; - -public interface CClassifier extends CNamedElement { - - CAssociationEnd createEnd(String roleName, String multiplicity) throws CException; - - CAssociationEnd createEnd(String multiplicity) throws CException; - - CAssociationEnd createEnd(String roleName, String multiplicity, boolean isNavigable) throws CException; - - List getAssociations(); - - CAssociation getAssociationByRoleName(String roleName); - - CAssociation getAssociationByName(String name); - - CAssociation getAssociationByEnd(CAssociationEnd associationEnd); - - List getAttributes(); - - List getAttributeNames(); - - CAttribute getAttribute(String name); - - CClassifier addObjectAttribute(String name, CClassifier classifier) throws CException; - - CClassifier addObjectAttribute(String name, String classifierName) throws CException; - - CClassifier addStringAttribute(String name) throws CException; - - CClassifier addIntAttribute(String name) throws CException; - - CClassifier addBooleanAttribute(String name) throws CException; - - CClassifier addFloatAttribute(String name) throws CException; - - CClassifier addDoubleAttribute(String name) throws CException; - - CClassifier addLongAttribute(String name) throws CException; - - CClassifier addCharAttribute(String name) throws CException; - - CClassifier addByteAttribute(String name) throws CException; - - CClassifier addShortAttribute(String name) throws CException; - - CClassifier addEnumAttribute(String name, CEnum enumType) throws CException; - - CClassifier addAttribute(String name, Object defaultValue) throws CException; - - CClassifier setAttributeDefaultValue(String name, Object defaultValue) throws CException; - - CClassifier deleteAttribute(String name) throws CException; - - CMetaclass asMetaclass() throws CException; - - CStereotype asStereotype() throws CException; - - CClass asClass() throws CException; - - List getSuperclasses(); - - List getSubclasses(); - - List getAllSuperclasses(); - - List getAllSubclasses(); - - CClassifier addSuperclass(String name) throws CException; - - CClassifier addSuperclass(CClassifier superclass) throws CException; - - CClassifier deleteSuperclass(CClassifier superclass) throws CException; - - CClassifier deleteSuperclass(String name) throws CException; - - boolean hasSuperclass(CClassifier cl); - - boolean hasSubclass(CClassifier cl); - - boolean hasSuperclass(String clName); - - boolean hasSubclass(String clName); -} \ No newline at end of file diff --git a/src/codeableModels/CElement.java b/src/codeableModels/CElement.java deleted file mode 100644 index cc0649f..0000000 --- a/src/codeableModels/CElement.java +++ /dev/null @@ -1,9 +0,0 @@ -package codeableModels; - -public interface CElement { - - CModel getModel(); - - void setModel(CModel model); - -} \ No newline at end of file diff --git a/src/codeableModels/CEnum.java b/src/codeableModels/CEnum.java deleted file mode 100644 index f9812cd..0000000 --- a/src/codeableModels/CEnum.java +++ /dev/null @@ -1,11 +0,0 @@ -package codeableModels; - -import java.util.*; - -public interface CEnum extends CNamedElement { - - List getValues(); - - boolean isLegalValue(String value); - -} \ No newline at end of file diff --git a/src/codeableModels/CException.java b/src/codeableModels/CException.java deleted file mode 100644 index 74ff520..0000000 --- a/src/codeableModels/CException.java +++ /dev/null @@ -1,9 +0,0 @@ -package codeableModels; - -public class CException extends Exception { - private static final long serialVersionUID = 1L; - - public CException(String m) { - super(m); - } -} diff --git a/src/codeableModels/CLink.java b/src/codeableModels/CLink.java deleted file mode 100644 index c075cb1..0000000 --- a/src/codeableModels/CLink.java +++ /dev/null @@ -1,16 +0,0 @@ -package codeableModels; - -import java.util.*; - -public interface CLink extends CStereotypedElementInstance { - - CAssociation getAssociation(); - - List getLinkedObjects(); - - CObject getLinkedObjectByName(String objectName); - - CObject getLinkedObjectByClassifier(CClassifier classifier); - - CObject getLinkedObjectAtTargetEnd(CAssociationEnd targetEnd) throws CException; -} \ No newline at end of file diff --git a/src/codeableModels/CMetaclass.java b/src/codeableModels/CMetaclass.java deleted file mode 100644 index e98d898..0000000 --- a/src/codeableModels/CMetaclass.java +++ /dev/null @@ -1,68 +0,0 @@ -package codeableModels; - -import java.util.*; - -public interface CMetaclass extends CClassifier, CStereotypedElement { - - List getClassInstances(); - - List getAllClassInstances(); - - @Override - CMetaclass addSuperclass(CClassifier superclass) throws CException; - - @Override - CMetaclass addSuperclass(String superclassString) throws CException; - - @Override - CMetaclass deleteSuperclass(String name) throws CException; - - @Override - CMetaclass deleteSuperclass(CClassifier superclass) throws CException; - - @Override - CMetaclass addObjectAttribute(String name, CClassifier classifier) throws CException; - - @Override - CMetaclass addObjectAttribute(String name, String classifierName) throws CException; - - @Override - CMetaclass addStringAttribute(String name) throws CException; - - @Override - CMetaclass addIntAttribute(String name) throws CException; - - @Override - CMetaclass addBooleanAttribute(String name) throws CException; - - @Override - CMetaclass addFloatAttribute(String name) throws CException; - - @Override - CMetaclass addDoubleAttribute(String name) throws CException; - - @Override - CMetaclass addLongAttribute(String name) throws CException; - - @Override - CMetaclass addCharAttribute(String name) throws CException; - - @Override - CMetaclass addByteAttribute(String name) throws CException; - - @Override - CMetaclass addShortAttribute(String name) throws CException; - - @Override - CMetaclass addEnumAttribute(String name, CEnum enumType) throws CException; - - @Override - CMetaclass addAttribute(String name, Object defaultValue) throws CException; - - @Override - CMetaclass setAttributeDefaultValue(String name, Object defaultValue) throws CException; - - @Override - CMetaclass deleteAttribute(String name) throws CException; - -} \ No newline at end of file diff --git a/src/codeableModels/CModel.java b/src/codeableModels/CModel.java deleted file mode 100644 index 6b8e24e..0000000 --- a/src/codeableModels/CModel.java +++ /dev/null @@ -1,108 +0,0 @@ -package codeableModels; - -import java.util.*; - -public interface CModel { - - CMetaclass createMetaclass(String name) throws CException; - - CMetaclass createMetaclass() throws CException; - - CClass createClass(CMetaclass metaclass, String name) throws CException; - - CClass createClass(CMetaclass metaclass) throws CException; - - CClass createClass(String metaclassName, String name) throws CException; - - CClass createClass(String metaclassName) throws CException; - - CStereotype createStereotype(String name) throws CException; - - CStereotype createStereotype() throws CException; - - CStereotype createStereotype(String name, CStereotypedElement stereotypedElement) throws CException; - - CStereotype createStereotype(String name, String stereotypedElementName) throws CException; - - CObject createObject(CClass cl, String name) throws CException; - - CObject createObject(CClass cl) throws CException; - - CObject createObject(String className, String name) throws CException; - - CObject createObject(String className) throws CException; - - CAssociation createAssociation(String name, CAssociationEnd end1, CAssociationEnd end2) throws CException; - - CAssociation createComposition(String name, CAssociationEnd composingEnd, CAssociationEnd composedEnd) throws - CException; - - CAssociation createAggregation(String name, CAssociationEnd aggregatingEnd, CAssociationEnd aggregatedEnd) throws - CException; - - CAssociation createAssociation(CAssociationEnd end1, CAssociationEnd end2) throws CException; - - CAssociation createComposition(CAssociationEnd end1, CAssociationEnd end2) throws CException; - - CAssociation createAggregation(CAssociationEnd end1, CAssociationEnd end2) throws CException; - - CModel importModel(CModel model); - - CClassifier getClassifier(String name); - - CMetaclass getMetaclass(String name) throws CException; - - CStereotype getStereotype(String name) throws CException; - - CClass getClass(String name) throws CException; - - CObject getObject(String name); - - CClassifier lookupClassifier(String name); - - CMetaclass lookupMetaclass(String name) throws CException; - - CClass lookupClass(String name) throws CException; - - CStereotype lookupStereotype(String name) throws CException; - - List getClassifierNames(); - - List getClassifiers(); - - List getMetaclasses(); - - List getClasses(); - - List getStereotypes(); - - List getMetaclassNames(); - - List getStereotypeNames(); - - List getClassNames(); - - void deleteClassifier(CClassifier cl) throws CException; - - List getObjectNames(); - - List getObjects(); - - void deleteObject(CObject o) throws CException; - - List getImportedModels(); - - List getFullModelList(); - - List getAssociations(); - - void deleteAssociation(CAssociation assoc) throws CException; - - // returns an array list, as association names don't have to be unique, - // including null == association with no name - List getAssociationsByName(String name); - - CEnum createEnum(String name, List enumValues); - - List getAssociationsForType(CClassifier type); -} \ No newline at end of file diff --git a/src/codeableModels/CMultiplicity.java b/src/codeableModels/CMultiplicity.java deleted file mode 100644 index 2e56fbe..0000000 --- a/src/codeableModels/CMultiplicity.java +++ /dev/null @@ -1,13 +0,0 @@ -package codeableModels; - -public interface CMultiplicity { - - int STAR_MULTIPLICITY = -1; - - String getMultiplicity(); - - int getUpperMultiplicity(); - - int getLowerMultiplicity(); - -} \ No newline at end of file diff --git a/src/codeableModels/CNamedElement.java b/src/codeableModels/CNamedElement.java deleted file mode 100644 index 4934d65..0000000 --- a/src/codeableModels/CNamedElement.java +++ /dev/null @@ -1,7 +0,0 @@ -package codeableModels; - -public interface CNamedElement extends CElement { - - String getName(); - -} \ No newline at end of file diff --git a/src/codeableModels/CObject.java b/src/codeableModels/CObject.java deleted file mode 100644 index 7a35f35..0000000 --- a/src/codeableModels/CObject.java +++ /dev/null @@ -1,53 +0,0 @@ -package codeableModels; - -import java.util.*; - -public interface CObject extends CNamedElement { - - CClassifier getClassifier(); - - boolean instanceOf(CClassifier classifier); - - boolean instanceOf(String classifierName); - - // Terminology as in UML: Associations represent relationships between classes; links represent relationships between objects. - - List addLinks(CAssociationEnd targetEnd, List links) throws CException; - - CLink addLink(CAssociationEnd targetEnd, CObject link) throws CException; - - CLink addLink(CAssociationEnd targetEnd, String link) throws CException; - - List getLinkObjects(CAssociationEnd targetEnd) throws CException; - - List getLinkObjects(CAssociation association) throws CException; - - List getLinks(CAssociationEnd targetEnd) throws CException; - - void removeLink(CAssociationEnd targetEnd, CObject target) throws CException; - - void removeLink(CAssociationEnd targetEnd, String targetString) throws CException; - - void removeLinks(CAssociationEnd targetEnd, List targets) throws CException; - - void removeAllLinks(CAssociationEnd targetEnd) throws CException; - - List setLinks(CAssociationEnd targetEnd, List links) throws CException; - - CLink setLink(CAssociationEnd targetEnd, CObject link) throws CException; - - CLink setLink(CAssociationEnd targetEnd, String link) throws CException; - - Object getAttributeValue(CClassifier classifier, String attributeName) throws CException; - - void setAttributeValue(CClassifier classifier, String attributeName, Object value) throws CException; - - // uses the first classifier on the classifier path of the object that defines an attribute with the name - Object getAttributeValue(String attributeName) throws CException; - - // uses the first classifier on the classifier path of the object that defines an attribute with the name - void setAttributeValue(String attributeName, Object value) throws CException; - - List getClassifierPath(); - -} \ No newline at end of file diff --git a/src/codeableModels/CStereotype.java b/src/codeableModels/CStereotype.java deleted file mode 100644 index d6f79e2..0000000 --- a/src/codeableModels/CStereotype.java +++ /dev/null @@ -1,68 +0,0 @@ -package codeableModels; - -import java.util.*; - -public interface CStereotype extends CClassifier { - - List getStereotypedElements(); - - List getStereotypedElementInstances(); - - @Override - CStereotype addSuperclass(CClassifier superclass) throws CException; - - @Override - CStereotype addSuperclass(String superclassString) throws CException; - - @Override - CStereotype deleteSuperclass(String name) throws CException; - - @Override - CStereotype deleteSuperclass(CClassifier superclass) throws CException; - - @Override - CStereotype addObjectAttribute(String name, CClassifier classifier) throws CException; - - @Override - CStereotype addObjectAttribute(String name, String classifierName) throws CException; - - @Override - CStereotype addStringAttribute(String name) throws CException; - - @Override - CStereotype addIntAttribute(String name) throws CException; - - @Override - CStereotype addBooleanAttribute(String name) throws CException; - - @Override - CStereotype addFloatAttribute(String name) throws CException; - - @Override - CStereotype addDoubleAttribute(String name) throws CException; - - @Override - CStereotype addLongAttribute(String name) throws CException; - - @Override - CStereotype addCharAttribute(String name) throws CException; - - @Override - CStereotype addByteAttribute(String name) throws CException; - - @Override - CStereotype addShortAttribute(String name) throws CException; - - @Override - CStereotype addEnumAttribute(String name, CEnum enumType) throws CException; - - @Override - CStereotype addAttribute(String name, Object defaultValue) throws CException; - - @Override - CStereotype setAttributeDefaultValue(String name, Object defaultValue) throws CException; - - @Override - CStereotype deleteAttribute(String name) throws CException; - -} \ No newline at end of file diff --git a/src/codeableModels/CStereotypedElement.java b/src/codeableModels/CStereotypedElement.java deleted file mode 100644 index f3fc2da..0000000 --- a/src/codeableModels/CStereotypedElement.java +++ /dev/null @@ -1,17 +0,0 @@ -package codeableModels; - -import java.util.*; - -public interface CStereotypedElement extends CNamedElement { - - List getStereotypes(); - - void addStereotype(CStereotype stereotype) throws CException; - - void removeStereotype(CStereotype stereotype) throws CException; - - void addStereotype(String stereotypeString) throws CException; - - void removeStereotype(String stereotypeString) throws CException; - -} \ No newline at end of file diff --git a/src/codeableModels/CStereotypedElementInstance.java b/src/codeableModels/CStereotypedElementInstance.java deleted file mode 100644 index 155c9bc..0000000 --- a/src/codeableModels/CStereotypedElementInstance.java +++ /dev/null @@ -1,26 +0,0 @@ -package codeableModels; - -import java.util.*; - -public interface CStereotypedElementInstance extends CNamedElement { - - Object getTaggedValue(CStereotype stereotype, String taggedValueName) throws CException; - - void setTaggedValue(CStereotype stereotype, String taggedValueName, Object value) throws CException; - - Object getTaggedValue(String taggedValueName) throws CException; - - void setTaggedValue(String taggedValueName, Object value) throws CException; - - CStereotypedElement getStereotypedElement(); - - void addStereotypeInstance(String name) throws CException; - - void addStereotypeInstance(CStereotype stereotype) throws CException; - - void deleteStereotypeInstance(String name) throws CException; - - void deleteStereotypeInstance(CStereotype stereotype) throws CException; - - List getStereotypeInstances(); -} \ No newline at end of file diff --git a/src/codeableModels/CodeableModels.java b/src/codeableModels/CodeableModels.java deleted file mode 100644 index 2973a5c..0000000 --- a/src/codeableModels/CodeableModels.java +++ /dev/null @@ -1,9 +0,0 @@ -package codeableModels; - -import codeableModels.impl.*; - -public class CodeableModels { - public static CModel createModel() { - return new CModelImpl(); - } -} diff --git a/src/codeableModels/impl/CAssociationEndImpl.java b/src/codeableModels/impl/CAssociationEndImpl.java deleted file mode 100644 index 63bac46..0000000 --- a/src/codeableModels/impl/CAssociationEndImpl.java +++ /dev/null @@ -1,58 +0,0 @@ -package codeableModels.impl; - -import codeableModels.*; - -public class CAssociationEndImpl implements CAssociationEnd { - private String roleName; - private CMultiplicity multiplicity; - private boolean isNavigable = true; - private CClassifier classifier; - - CAssociationEndImpl(CClassifier classifier, String roleName, String multiplicity) throws CException { - setRoleName(roleName); - setMultiplicity(multiplicity); - setClassifier(classifier); - } - - @Override - public CClassifier getClassifier() { - return classifier; - } - - private void setClassifier(CClassifier classifier) { - this.classifier = classifier; - } - - @Override - public String getRoleName() { - return roleName; - } - - private void setRoleName(String roleName) { - this.roleName = roleName; - } - - @Override - public boolean isNavigable() { - return isNavigable; - } - - @Override - public void setNavigable(boolean isNavigable) { - this.isNavigable = isNavigable; - } - - @Override - public CMultiplicity getMultiplicity() { - return multiplicity; - } - - @Override - public String getMultiplicityString() { - return multiplicity.getMultiplicity(); - } - - private void setMultiplicity(String multiplicityString) throws CException { - multiplicity = new CMultiplicityImpl(multiplicityString); - } -} diff --git a/src/codeableModels/impl/CAssociationImpl.java b/src/codeableModels/impl/CAssociationImpl.java deleted file mode 100644 index ed6d2bb..0000000 --- a/src/codeableModels/impl/CAssociationImpl.java +++ /dev/null @@ -1,243 +0,0 @@ -package codeableModels.impl; - -import codeableModels.*; - -import java.util.*; - -public class CAssociationImpl extends CNamedElementImpl implements CAssociation { - private CAssociationEnd end1, end2; - private boolean isAggregation = false; - private boolean isComposition = false; - private final CStereotypeList stereotypeList = new CStereotypeList(this); - private final List links = new ArrayList<>(); - - public CAssociationImpl(CModel model, String name) { - // if an association should have no name, pass null as an argument - super(name); - setModel(model); - } - - // for Aggregation/Composition we assume end1 is the aggregating end - @Override - public boolean isAggregation() { - return isAggregation; - } - - @Override - public void setAggregation(boolean isAggregation) { - if (isAggregation) { - setComposition(false); - } - this.isAggregation = isAggregation; - } - - @Override - public boolean isComposition() { - return isComposition; - } - - @Override - public void setComposition(boolean isComposition) { - if (isComposition) { - setAggregation(false); - } - this.isComposition = isComposition; - } - - @Override - public List getEnds() { - return Arrays.asList(end1, end2); - } - - void setEnd1(CAssociationEnd end1) { - this.end1 = end1; - } - - void setEnd2(CAssociationEnd end2) { - this.end2 = end2; - } - - @Override - public boolean hasEndType(CClassifier type) { - return end1.getClassifier() == type || end2.getClassifier() == type; - } - - @Override - public CAssociationEnd getEndByRoleName(String roleName) { - if (end1.getRoleName().equals(roleName)) { - return end1; - } else if (end2.getRoleName().equals(roleName)) { - return end2; - } - return null; - } - - public static boolean checkClassifierMatches(CClassifier classifier, CClassifier endClassifier) { - // a check identical to the one in getEndByClassifier, but just for one classifier - return endClassifier == classifier || endClassifier.getAllSuperclasses().contains(classifier) || - classifier.getAllSuperclasses().contains(endClassifier); - } - - @Override - public CAssociationEnd getEndByClassifier(CClassifier classifier) { - // first check direct classifiers, then go up the hierarchy for the types of ends, if that also fails - // check whether the provided classifier is of the type of one of the ends - if (end1.getClassifier() == classifier) { - return end1; - } else if (end2.getClassifier() == classifier) { - return end2; - } else if (end1.getClassifier().getAllSuperclasses().contains(classifier)) { - return end1; - } else if (end2.getClassifier().getAllSuperclasses().contains(classifier)) { - return end2; - } else if (classifier.getAllSuperclasses().contains(end1.getClassifier())) { - return end1; - } else if (classifier.getAllSuperclasses().contains(end2.getClassifier())) { - return end2; - } - - return null; - } - - @Override - public CAssociationEnd getOtherEnd(CAssociationEnd end) throws CException { - if (end1 == end) { - return end2; - } else if (end2 == end) { - return end1; - } else { - String roleNameString = ""; - if (end.getRoleName() != null) { - roleNameString = ": '" + end.getRoleName() + "'"; - } - throw new CException("end unknown in association" + roleNameString); - } - } - - @Override - public List getStereotypes() { - return stereotypeList.getStereotypes(); - } - - @Override - public void addStereotype(CStereotype stereotype) throws CException { - CClassifier cl1 = end1.getClassifier(), cl2 = end2.getClassifier(); - if (!(cl1 instanceof CMetaclass && cl2 instanceof CMetaclass)) { - throw new CException("association classifiers '" + cl1.getName() + - "' and/or '" + cl2.getName() + "' are not metaclasses"); - } - stereotypeList.addStereotype(stereotype); - ((CStereotypeImpl)stereotype).addStereotypedElement(this); - } - - @Override - public void removeStereotype(CStereotype stereotype) throws CException { - stereotypeList.removeStereotype(stereotype); - ((CStereotypeImpl)stereotype).removeStereotypedElement(this); - } - - @Override - public void addStereotype(String stereotypeString) throws CException { - CStereotype extension = getModel().lookupStereotype(stereotypeString); - if (extension == null) { - throw new CException("stereotype '" + stereotypeString + "' does not exist"); - } - addStereotype(extension); - } - - @Override - public void removeStereotype(String stereotypeString) throws CException { - CStereotype extension = getModel().lookupStereotype(stereotypeString); - if (extension == null) { - throw new CException("stereotype '" + stereotypeString + "' does not exist"); - } - stereotypeList.removeStereotype(extension); - } - - static void checkAssociationMultiplicityRange(CAssociationEnd end, int listSize) throws CException { - CMultiplicity endMultiplicity = end.getMultiplicity(); - if ((endMultiplicity.getUpperMultiplicity() != CMultiplicityImpl.STAR_MULTIPLICITY && listSize > - endMultiplicity.getUpperMultiplicity()) || listSize < endMultiplicity.getLowerMultiplicity()) { - throw new CException("link has wrong multiplicity '" + listSize + "', but " + - "should be '" + endMultiplicity.toString() + "'"); - } - } - - @Override - public CLink addLink(CAssociationEnd targetEnd, CObject from, CObject to) throws CException { - CObject object1 = from, object2 = to; - - if (end1 == targetEnd) { - object1 = to; - object2 = from; - } - - if(!object1.getClassifierPath().contains(end1.getClassifier())) { - throw new CException("link object '" + object1.getName() + "' not compatible with association classifier '" + - end1.getClassifier().getName() + "'"); - } - if(!object2.getClassifierPath().contains(end2.getClassifier())) { - throw new CException("link object '" + object2.getName() + "' not compatible with association classifier '" + - end2.getClassifier().getName() + "'"); - } - - int object1ToAnyCount = 1, object2ToAnyCount = 1; - - for (CLink link : links) { - if (link.getLinkedObjects().get(0) == object1 && link.getLinkedObjects().get(1) == object2) { - throw new CException("link between '" + object1.getName() + "' and '" + object2.getName() + "' already exists"); - } - if (link.getLinkedObjects().get(0) == object1) { - object1ToAnyCount++; - } - if (link.getLinkedObjects().get(1) == object2) { - object2ToAnyCount++; - } - } - - checkAssociationMultiplicityRange(end2, object1ToAnyCount); - checkAssociationMultiplicityRange(end1, object2ToAnyCount); - - CLink link = new CLinkImpl(object1.getModel(),this, object1, object2); - links.add(link); - return link; - } - - @Override - public List getLinksByObject(CAssociationEnd targetEnd, CObject object) { - List results = new ArrayList<>(); - for (CLink link : links) { - if (targetEnd == end1 && link.getLinkedObjects().get(1) == object) { - results.add(link); - } - if (targetEnd == end2 && link.getLinkedObjects().get(0) == object) { - results.add(link); - } - } - return results; - } - - @Override - public void removeLink(CAssociationEnd targetEnd, CObject object1, CObject object2) throws CException { - List linksOfObject1 = getLinksByObject(targetEnd, object1); - for (CLink link : linksOfObject1) { - if (link.getLinkedObjects().contains(object2)) { - links.remove(link); - return; - } - } - throw new CException("link between '" + object1.getName() + "' and '" + object2.getName() + "' can't be removed: it does not exist"); - } - - @Override - public void removeLink(CLink linkToBeRemoved) { - links.remove(linkToBeRemoved); - } - - - @Override - public List getLinks() { - return links; - } - -} diff --git a/src/codeableModels/impl/CAttributeImpl.java b/src/codeableModels/impl/CAttributeImpl.java deleted file mode 100644 index 239db49..0000000 --- a/src/codeableModels/impl/CAttributeImpl.java +++ /dev/null @@ -1,155 +0,0 @@ -package codeableModels.impl; - -import codeableModels.*; - -public class CAttributeImpl implements CAttribute { - - // for isLegalAttributeTypeCode() to work, OBJECT should stay the first element and - // ENUM the last, and every constant in between should be incremented by 1 - static final int OBJECT = 0; - static final int STRING = 1; - static final int INT = 2; - static final int BOOLEAN = 3; - static final int FLOAT = 4; - static final int DOUBLE = 5; - static final int LONG = 6; - static final int CHAR = 7; - static final int BYTE = 8; - static final int SHORT = 9; - static final int ENUM = 10; - static final int UNKNOWN_TYPE = -1; - - private String name; - private int typeCode; - private Object defaultValue; - private CClassifier typeClassifier = null; - private CEnum enumType = null; - - public static boolean isLegalAttributeTypeCode(int type) { - return type >= OBJECT && type <= ENUM; - } - - public static int getAttributeTypeCodeOfObject(Object object) { - if (object instanceof CObject) - return OBJECT; - if (object instanceof String) - return STRING; - if (object instanceof Integer) - return INT; - if (object instanceof Boolean) - return BOOLEAN; - if (object instanceof Float) - return FLOAT; - if (object instanceof Double) - return DOUBLE; - if (object instanceof Long) - return LONG; - if (object instanceof Byte) - return BYTE; - if (object instanceof Short) - return SHORT; - if (object instanceof Character) - return CHAR; - // ENUM cannot be guessed by default value, but requires explicit creation, as its value is also a string. - - return UNKNOWN_TYPE; - } - - public static void checkAttributeValueType(String name, CAttributeImpl attribute, Object value) throws CException { - int actualAttributeTypeCode = getAttributeTypeCodeOfObject(value); - if (actualAttributeTypeCode == CAttributeImpl.UNKNOWN_TYPE) - throw new CException("value for attribute '" + name + "' is not a known attribute type"); - if (actualAttributeTypeCode != attribute.typeCode) { - // an enum requires a string to be provided as a value - if (!(attribute.typeCode == ENUM && actualAttributeTypeCode == STRING)) { - throw new CException("value type for attribute '" + name + "' does not match attribute type"); - } - } - if (actualAttributeTypeCode == OBJECT) { - CObject object = (CObject) value; - if (object.getClassifier() != attribute.getTypeClassifier()) { - throw new CException("type classifier of object '" + object.getName() + "' is not matching attribute " + - "type of attribute '" + name + "'"); - } - } - if (attribute.typeCode == ENUM && actualAttributeTypeCode == STRING) { - String stringValue = (String) value; - if (!attribute.getEnumType().isLegalValue(stringValue)) { - throw new CException("value '" + stringValue + "' not element of enumeration"); - } - } - - } - - public CClassifier getTypeClassifier() { - return typeClassifier; - } - - void setTypeClassifier(CClassifier classifier) { - this.typeClassifier = classifier; - } - - @Override - public CEnum getEnumType() { - return enumType; - } - - void setEnumType(CEnum enumType) { - this.enumType = enumType; - } - - @Override - public String getName() { - return name; - } - - @Override - public void setName(String name) { - this.name = name; - } - - @Override - public String getType() throws CException { - switch (typeCode) { - case OBJECT: - return "Object"; - case STRING: - return "String"; - case INT: - return "Int"; - case BOOLEAN: - return "Boolean"; - case FLOAT: - return "Float"; - case DOUBLE: - return "Double"; - case LONG: - return "Long"; - case CHAR: - return "Char"; - case BYTE: - return "Byte"; - case SHORT: - return "Short"; - case ENUM: - return "Enum"; - } - throw new CException("type string unknown"); - } - - - void setTypeCode(int typeCode) { - this.typeCode = typeCode; - } - - @Override - public Object getDefaultValue() { - return defaultValue; - } - - @Override - public void setDefaultValue(Object defaultValue) throws CException { - checkAttributeValueType(this.name, this, defaultValue); - this.defaultValue = defaultValue; - } -} diff --git a/src/codeableModels/impl/CAttributeValues.java b/src/codeableModels/impl/CAttributeValues.java deleted file mode 100644 index a97cb9a..0000000 --- a/src/codeableModels/impl/CAttributeValues.java +++ /dev/null @@ -1,27 +0,0 @@ -package codeableModels.impl; - -import codeableModels.*; - -import java.util.*; - -class CAttributeValues { - private final Map> attributeValues = new HashMap<>(); - - public Object get(CClassifier classifier, String attributeName) { - Map attributeValuesClassifier = attributeValues.get(classifier); - if (attributeValuesClassifier == null) { - return null; - } - return attributeValuesClassifier.get(attributeName); - } - - public void add(CClassifier classifier, String attributeName, Object value) { - Map attributeValuesClassifier = attributeValues.get(classifier); - if (attributeValuesClassifier == null) { - attributeValuesClassifier = new HashMap<>(); - attributeValues.put(classifier, attributeValuesClassifier); - } - attributeValuesClassifier.put(attributeName, value); - } -} - diff --git a/src/codeableModels/impl/CClassImpl.java b/src/codeableModels/impl/CClassImpl.java deleted file mode 100644 index 3ebff1a..0000000 --- a/src/codeableModels/impl/CClassImpl.java +++ /dev/null @@ -1,347 +0,0 @@ -package codeableModels.impl; - -import codeableModels.*; - -import java.util.*; - -public class CClassImpl extends CClassifierImpl implements CClass, CObject, CStereotypedElementInstance { - private final List instances = new ArrayList<>(); - private final CStereotypeInstanceList stereotypeInstances = new CStereotypeInstanceList(this); - private final CObject classObject; - private final CTaggedValues taggedValues = new CTaggedValues(); - - @SuppressWarnings("RedundantThrows") - public CClassImpl(String name) throws CException { - super(name); - classObject = new CObjectImpl(name); - ((CObjectImpl) classObject).setThisInLinks(this); - } - - void addInstance(CObject obj) { - instances.add(obj); - } - - @Override - public void setModel(CModel model) { - super.setModel(model); - classObject.setModel(model); - } - - CObjectImpl getClassObjectImpl() { - return (CObjectImpl) classObject; - } - - @Override - public List getInstances() { - return instances; - } - - @Override - public List getAllInstances() { - List result = new ArrayList<>(instances); - for (CClassifier cl : getAllSubclasses()) { - result.addAll(((CClass) cl).getInstances()); - } - return result; - } - - void removeInstance(CObject delObj) throws CException { - boolean success = instances.remove(delObj); - if (!success) { - throw new CException("can't remove instance '" + delObj.getName() + "' from class '" + this.getName() - + "': is not an instance of this class"); - } - } - - @Override - protected void cleanupClassifier() throws CException { - super.cleanupClassifier(); - ArrayList objectsToDelete = new ArrayList<>(getInstances()); - for (CObject obj : objectsToDelete) { - getModel().deleteObject(obj); - } - if (getMetaclass() != null) { - ((CMetaclassImpl) getMetaclass()).removeClassInstance(this); - setMetaclass(null); - } else { - throw new CException("trying to delete class '" + this.getName() + "' that has been deleted before"); - } - } - - @Override - public CMetaclass getMetaclass() { - return (CMetaclass) classObject.getClassifier(); - } - - void setMetaclass(CMetaclass metaclass) { - ((CObjectImpl) classObject).setClassifier(metaclass); - if (metaclass != null) - ((CMetaclassImpl) metaclass).addClassInstance(this); - } - - @Override - public CClass addSuperclass(CClassifier superclass) throws CException { - if (!(superclass instanceof CClass)) { - throw new CException( - "cannot add superclass '" + superclass.getName() + "' to '" + getName() + "': not a class"); - } - return super.addSuperclass(superclass).asClass(); - } - - @Override - public CClass addSuperclass(String superclassString) throws CException { - return super.addSuperclass(superclassString).asClass(); - } - - @Override - public CClass deleteSuperclass(String name) throws CException { - return super.deleteSuperclass(name).asClass(); - } - - @Override - public CClass deleteSuperclass(CClassifier superclass) throws CException { - return super.deleteSuperclass(superclass).asClass(); - } - - - @Override - public CClass addObjectAttribute(String name, CClass classifier) throws CException { - return super.addObjectAttribute(name, classifier).asClass(); - } - - @Override - public CClass addObjectAttribute(String name, String classifierName) throws CException { - return super.addObjectAttribute(name, classifierName).asClass(); - } - - @Override - public CClass addStringAttribute(String name) throws CException { - return super.addStringAttribute(name).asClass(); - } - - @Override - public CClass addIntAttribute(String name) throws CException { - return super.addIntAttribute(name).asClass(); - } - - @Override - public CClass addBooleanAttribute(String name) throws CException { - return super.addBooleanAttribute(name).asClass(); - } - - @Override - public CClass addFloatAttribute(String name) throws CException { - return super.addFloatAttribute(name).asClass(); - } - - @Override - public CClass addDoubleAttribute(String name) throws CException { - return super.addDoubleAttribute(name).asClass(); - } - - @Override - public CClass addLongAttribute(String name) throws CException { - return super.addLongAttribute(name).asClass(); - } - - @Override - public CClass addCharAttribute(String name) throws CException { - return super.addCharAttribute(name).asClass(); - } - - @Override - public CClass addByteAttribute(String name) throws CException { - return super.addByteAttribute(name).asClass(); - } - - @Override - public CClass addShortAttribute(String name) throws CException { - return super.addShortAttribute(name).asClass(); - } - - @Override - public CClass addEnumAttribute(String name, CEnum enumType) throws CException { - return super.addEnumAttribute(name, enumType).asClass(); - } - - @Override - public CClass addAttribute(String name, Object defaultValue) throws CException { - return super.addAttribute(name, defaultValue).asClass(); - } - - @Override - public CClass setAttributeDefaultValue(String name, Object defaultValue) throws CException { - return super.setAttributeDefaultValue(name, defaultValue).asClass(); - } - - @Override - public CClass deleteAttribute(String name) throws CException { - return super.deleteAttribute(name).asClass(); - } - - @Override - public CStereotypedElement getStereotypedElement() { - return getMetaclass(); - } - - @Override - public void addStereotypeInstance(String name) throws CException { - CStereotype stereotype = getModel().lookupStereotype(name); - if (stereotype == null) { - throw new CException("stereotype '" + name + "' does not exist"); - } - addStereotypeInstance(stereotype); - } - - @Override - public void addStereotypeInstance(CStereotype stereotype) throws CException { - stereotypeInstances.addStereotype(stereotype); - } - - @Override - public void deleteStereotypeInstance(String name) throws CException { - CStereotype stereotype = getModel().getStereotype(name); - if (stereotype == null) { - throw new CException("stereotype '" + name + "' does not exist"); - } - deleteStereotypeInstance(stereotype); - } - - @Override - public void deleteStereotypeInstance(CStereotype stereotype) throws CException { - stereotypeInstances.removeStereotype(stereotype); - } - - @Override - public List getStereotypeInstances() { - return stereotypeInstances.getStereotypes(); - } - - @Override - public CClassifier getClassifier() { - return classObject.getClassifier(); - } - - @Override - public boolean instanceOf(CClassifier classifier) { - return classObject.instanceOf(classifier); - } - - @Override - public boolean instanceOf(String classifierName) { - return classObject.instanceOf(classifierName); - } - - @Override - public List addLinks(CAssociationEnd targetEnd, List links) throws CException { - return classObject.addLinks(targetEnd, links); - } - - @Override - public CLink addLink(CAssociationEnd targetEnd, CObject link) throws CException { - return classObject.addLink(targetEnd, link); - } - - @Override - public CLink addLink(CAssociationEnd targetEnd, String link) throws CException { - List links = addLinks(targetEnd, new ArrayList<>(Collections.singletonList(link))); - return links.get(0); - } - - @Override - public List setLinks(CAssociationEnd targetEnd, List links) throws CException { - return classObject.setLinks(targetEnd, links); - } - - @Override - public CLink setLink(CAssociationEnd targetEnd, CObject link) throws CException { - return classObject.setLink(targetEnd, link); - } - - @Override - public CLink setLink(CAssociationEnd targetEnd, String link) throws CException { - List links = setLinks(targetEnd, new ArrayList<>(Collections.singletonList(link))); - return links.get(0); - } - - @Override - public List getLinkObjects(CAssociationEnd targetEnd) throws CException { - return classObject.getLinkObjects(targetEnd); - } - - @Override - public List getLinkObjects(CAssociation association) throws CException { - return classObject.getLinkObjects(association); - } - - @Override - public List getLinks(CAssociationEnd targetEnd) throws CException { - return classObject.getLinks(targetEnd); - } - - @Override - public void removeLink(CAssociationEnd targetEnd, CObject target) throws CException { - classObject.removeLink(targetEnd, target); - } - - @Override - public void removeLink(CAssociationEnd targetEnd, String targetString) throws CException { - classObject.removeLink(targetEnd, targetString); - } - - @Override - public void removeLinks(CAssociationEnd targetEnd, List targets) throws CException { - classObject.removeLinks(targetEnd, targets); - } - - @Override - public void removeAllLinks(CAssociationEnd targetEnd) throws CException { - classObject.removeAllLinks(targetEnd); - } - - @Override - public Object getAttributeValue(CClassifier classifier, String attributeName) throws CException { - return classObject.getAttributeValue(classifier, attributeName); - } - - @Override - public void setAttributeValue(CClassifier classifier, String attributeName, Object value) throws CException { - classObject.setAttributeValue(classifier, attributeName, value); - } - - @Override - public Object getAttributeValue(String attributeName) throws CException { - return classObject.getAttributeValue(attributeName); - } - - @Override - public void setAttributeValue(String attributeName, Object value) throws CException { - classObject.setAttributeValue(attributeName, value); - } - - @Override - public Object getTaggedValue(CStereotype stereotype, String taggedValueName) throws CException { - return taggedValues.getTaggedValue(stereotypeInstances.getStereotypeInstancePath(), stereotype, taggedValueName); - } - - @Override - public void setTaggedValue(CStereotype stereotype, String taggedValueName, Object value) throws CException { - taggedValues.setTaggedValue(stereotypeInstances.getStereotypeInstancePath(), stereotype, taggedValueName, value); - } - - @Override - public Object getTaggedValue(String taggedValueName) throws CException { - return taggedValues.getTaggedValue(stereotypeInstances.getStereotypeInstancePath(), taggedValueName); - } - - @Override - public void setTaggedValue(String taggedValueName, Object value) throws CException { - taggedValues.setTaggedValue(stereotypeInstances.getStereotypeInstancePath(), taggedValueName, value); - } - - @Override - public List getClassifierPath() { - return classObject.getClassifierPath(); - } - -} diff --git a/src/codeableModels/impl/CClassifierImpl.java b/src/codeableModels/impl/CClassifierImpl.java deleted file mode 100644 index d8dec4b..0000000 --- a/src/codeableModels/impl/CClassifierImpl.java +++ /dev/null @@ -1,427 +0,0 @@ -package codeableModels.impl; - -import codeableModels.*; - -import java.util.*; - -public abstract class CClassifierImpl extends CNamedElementImpl implements CClassifier { - private final List associations = new ArrayList<>(); - private final Map attributes = new LinkedHashMap<>(); - private List superclasses = new ArrayList<>(); - private List subclasses = new ArrayList<>(); - - CClassifierImpl(String name) { - super(name); - } - - @Override - public CAssociationEnd createEnd(String multiplicity) throws CException { - // guess the role name from the classifier name - String roleName = this.getName().toLowerCase(); - return new CAssociationEndImpl(this, roleName, multiplicity); - } - - @Override - public CAssociationEnd createEnd(String roleName, String multiplicity) throws CException { - return new CAssociationEndImpl(this, roleName, multiplicity); - } - - @Override - public CAssociationEnd createEnd(String roleName, String multiplicity, boolean isNavigable) throws CException { - CAssociationEnd ae = createEnd(roleName, multiplicity); - ae.setNavigable(isNavigable); - return ae; - } - - @Override - public List getAssociations() { - return associations; - } - - @Override - public CAssociation getAssociationByRoleName(String roleName) { - List objAssociations = getModel().getAssociationsForType(this); - for (CAssociation a : objAssociations) { - CAssociationEnd end = a.getEndByRoleName(roleName); - if (end != null && end.getClassifier() != this && - !this.getAllSuperclasses().contains(end.getClassifier())) { - return a; - } - } - return null; - } - - @Override - public CAssociation getAssociationByEnd(CAssociationEnd associationEnd) { - List objAssociations = getModel().getAssociationsForType(this); - for (CAssociation a : objAssociations) { - if (a.getEnds().get(0) == associationEnd || a.getEnds().get(1) == associationEnd) { - return a; - } - } - return null; - } - - - @Override - public CAssociation getAssociationByName(String name) { - List objAssociations = getModel().getAssociationsForType(this); - for (CAssociation a : objAssociations) { - if (a.getName() != null && a.getName().equals(name)) { - return a; - } - } - return null; - } - - void addAssociation(CAssociation assoc) { - associations.add(assoc); - } - - void removeAssociation(CAssociation assoc) throws CException { - boolean success = associations.remove(assoc); - if (!success) { - throw new CException("can't remove association '" + assoc.getName() + "' from classifier '" + this.getName() - + "': is not an association"); - } - } - - private void removeAllAssociations() throws CException { - List associations = new ArrayList<>(); - - // make a copy of associations as elements get deleted below. - // make it unique, as not to double trigger deletion of a self-reference, which - // is twice listed in the list of associations - for (CAssociation assoc : this.associations) { - if (!associations.contains(assoc)) { - associations.add(assoc); - } - } - - for (CAssociation assoc : associations) { - getModel().deleteAssociation(assoc); - } - } - - @Override - public List getAttributes() { - return new ArrayList<>(attributes.values()); - } - - @Override - public List getAttributeNames() { - return new ArrayList<>(attributes.keySet()); - } - - private CAttribute createAttribute(int typeCode, String name) throws CException { - if (!CAttributeImpl.isLegalAttributeTypeCode(typeCode)) { - throw new CException("attribute type for attribute '" + name + "' unknown"); - } - if (getAttribute(name) != null) { - throw new CException("attribute '" + name + "' cannot be created, attribute name already exists"); - } - - CAttribute attr = new CAttributeImpl(); - ((CAttributeImpl) attr).setTypeCode(typeCode); - attr.setName(name); - attributes.put(name, attr); - return attr; - } - - @Override - public CAttribute getAttribute(String name) { - return attributes.get(name); - } - - private CClassifier addAttribute(int type, String name) throws CException { - createAttribute(type, name); - return this; - } - - @Override - public CClassifier addObjectAttribute(String name, CClassifier classifier) throws CException { - CAttributeImpl attribute = (CAttributeImpl) createAttribute(CAttributeImpl.OBJECT, name); - attribute.setTypeClassifier(classifier); - return this; - } - - @Override - public CClassifier addObjectAttribute(String name, String classifierName) throws CException { - CClassifier classifier = getModel().getClassifier(classifierName); - return addObjectAttribute(name, classifier); - } - - @Override - public CClassifier addStringAttribute(String name) throws CException { - return addAttribute(CAttributeImpl.STRING, name); - } - - @Override - public CClassifier addIntAttribute(String name) throws CException { - return addAttribute(CAttributeImpl.INT, name); - } - - @Override - public CClassifier addBooleanAttribute(String name) throws CException { - return addAttribute(CAttributeImpl.BOOLEAN, name); - } - - @Override - public CClassifier addFloatAttribute(String name) throws CException { - return addAttribute(CAttributeImpl.FLOAT, name); - } - - @Override - public CClassifier addDoubleAttribute(String name) throws CException { - return addAttribute(CAttributeImpl.DOUBLE, name); - } - - @Override - public CClassifier addLongAttribute(String name) throws CException { - return addAttribute(CAttributeImpl.LONG, name); - } - - @Override - public CClassifier addCharAttribute(String name) throws CException { - return addAttribute(CAttributeImpl.CHAR, name); - } - - @Override - public CClassifier addByteAttribute(String name) throws CException { - return addAttribute(CAttributeImpl.BYTE, name); - } - - @Override - public CClassifier addShortAttribute(String name) throws CException { - return addAttribute(CAttributeImpl.SHORT, name); - } - - @Override - public CClassifier addEnumAttribute(String name, CEnum enumType) throws CException { - CAttributeImpl attribute = (CAttributeImpl) createAttribute(CAttributeImpl.ENUM, name); - attribute.setEnumType(enumType); - return this; - } - - @Override - public CClassifier setAttributeDefaultValue(String name, Object defaultValue) throws CException { - CAttribute attribute = attributes.get(name); - if (attribute == null) { - throw new CException("unknown attribute name '" + name +"' on classifier '" + getName() + "'"); - } - attribute.setDefaultValue(defaultValue); - return this; - } - - @Override - public CClassifier addAttribute(String name, Object defaultValue) throws CException { - int attributeType = CAttributeImpl.getAttributeTypeCodeOfObject(defaultValue); - if (attributeType == CAttributeImpl.UNKNOWN_TYPE) - throw new CException("value for attribute '" + name + "' is not a known attribute type"); - CAttribute attr = createAttribute(attributeType, name); - if (attributeType == CAttributeImpl.OBJECT) { - ((CAttributeImpl) attr).setTypeClassifier(((CObject) defaultValue).getClassifier()); - } - attr.setDefaultValue(defaultValue); - return this; - } - - @Override - public CClassifier deleteAttribute(String name) throws CException { - if (attributes.containsKey(name)) { - attributes.remove(name); - return this; - } - throw new CException( - "attribute '" + name + "' to be deleted not defined on classifier '" + this.getName() + "'"); - } - - @Override - public CMetaclass asMetaclass() throws CException { - if (this instanceof CMetaclass) { - return (CMetaclass) this; - } - throw new CException("'" + getName() + "' is not a metaclass"); - } - - @Override - public CStereotype asStereotype() throws CException { - if (this instanceof CStereotype) { - return (CStereotype) this; - } - throw new CException("'" + getName() + "' is not a stereotype"); - } - - @Override - public CClass asClass() throws CException { - if (this instanceof CClass) { - return (CClass) this; - } - throw new CException("'" + getName() + "' is not a class"); - } - - @Override - public List getSuperclasses() { - return superclasses; - } - - @Override - public List getSubclasses() { - return subclasses; - } - - private Set getAllSuperclasses(Set iteratedClasses) { - if (superclasses == null) { - return null; - } - Set orderedResultSet = new LinkedHashSet<>(); - - for (CClassifier c : superclasses) { - CClassifierImpl cImpl = (CClassifierImpl) c; - if (!iteratedClasses.contains(cImpl)) { - iteratedClasses.add(cImpl); - orderedResultSet.add(cImpl); - Set sc = cImpl.getAllSuperclasses(iteratedClasses); - if (sc != null) { - orderedResultSet.addAll(sc); - } - } - } - return orderedResultSet; - } - - @Override - public List getAllSuperclasses() { - Set orderedResultSet = getAllSuperclasses(new LinkedHashSet<>()); - if (orderedResultSet == null) { - return new ArrayList<>(); - } - return new ArrayList<>(orderedResultSet); - } - - private Set getAllSubclasses(Set iteratedClasses) { - if (subclasses == null) { - return null; - } - Set orderedResultSet = new LinkedHashSet<>(); - - for (CClassifier c : subclasses) { - CClassifierImpl cImpl = (CClassifierImpl) c; - if (!iteratedClasses.contains(cImpl)) { - iteratedClasses.add(cImpl); - orderedResultSet.add(cImpl); - Set sc = cImpl.getAllSubclasses(iteratedClasses); - if (sc != null) { - orderedResultSet.addAll(sc); - } - } - } - return orderedResultSet; - } - - @Override - public List getAllSubclasses() { - Set orderedResultSet = getAllSubclasses(new LinkedHashSet<>()); - if (orderedResultSet == null) { - return new ArrayList<>(); - } - return new ArrayList<>(orderedResultSet); - } - - private void resetSuperclasses() { - superclasses = new ArrayList<>(); - } - - private void resetSubclasses() { - subclasses = new ArrayList<>(); - } - - private void removeSubclass(CClassifier subclass) throws CException { - boolean success = subclasses.remove(subclass); - if (!success) { - throw new CException("can't remove subclass '" + subclass.getName() + "' from classifier '" + this.getName() - + "': is not a subclass"); - } - } - - private void removeSuperclass(CClassifier superclass) throws CException { - boolean success = superclasses.remove(superclass); - if (!success) { - throw new CException("can't remove subclass '" + superclass.getName() + "' from classifier '" - + this.getName() + "': is not a subclass"); - } - } - - @Override - public CClassifier addSuperclass(String name) throws CException { - CModel model = getModel(); - CClassifier superclass = model.lookupClassifier(name); - if (superclass == null) { - throw new CException("can't find superclass '" + name + "'"); - } - return addSuperclass(superclass); - } - - @Override - public CClassifier addSuperclass(CClassifier superclass) throws CException { - CClassifierImpl superclassImpl = (CClassifierImpl) superclass; - if (superclasses.contains(superclassImpl)) { - throw new CException( - "'" + superclassImpl.getName() + "' is already a superclass of '" + this.getName() + "'"); - } - if (superclassImpl.subclasses.contains(this)) { - throw new CException( - "'" + this.getName() + "' is already a subclass of '" + superclassImpl.getName() + "'"); - } - this.superclasses.add(superclassImpl); - superclassImpl.subclasses.add(this); - return this; - } - - @Override - public CClassifier deleteSuperclass(CClassifier superclass) throws CException { - CClassifierImpl superclassImpl = (CClassifierImpl) superclass; - superclassImpl.removeSubclass(this); - this.removeSuperclass(superclassImpl); - return this; - } - - @Override - public CClassifier deleteSuperclass(String name) throws CException { - CClassifier superclass = getModel().getClassifier(name); - return deleteSuperclass(superclass); - } - - void cleanupClassifier() throws CException { - for (CClassifier superclass : getSuperclasses()) { - ((CClassifierImpl) superclass).removeSubclass(this); - } - resetSuperclasses(); - for (CClassifier subclass : getSubclasses()) { - ((CClassifierImpl) subclass).removeSuperclass(this); - } - resetSubclasses(); - removeAllAssociations(); - } - - @Override - public boolean hasSuperclass(CClassifier cl) { - return getAllSuperclasses().contains(cl); - } - - @Override - public boolean hasSuperclass(String clName) { - return hasSuperclass(getModel().getClassifier(clName)); - } - - @Override - public boolean hasSubclass(CClassifier cl) { - return getAllSubclasses().contains(cl); - } - - @Override - public boolean hasSubclass(String clName) { - return hasSubclass(getModel().getClassifier(clName)); - } - -} diff --git a/src/codeableModels/impl/CElementImpl.java b/src/codeableModels/impl/CElementImpl.java deleted file mode 100644 index 1aabca6..0000000 --- a/src/codeableModels/impl/CElementImpl.java +++ /dev/null @@ -1,18 +0,0 @@ -package codeableModels.impl; - -import codeableModels.*; - -public abstract class CElementImpl implements CElement { - private CModel model; - - @Override - public CModel getModel() { - return model; - } - - @Override - public void setModel(CModel model) { - this.model = model; - } - -} diff --git a/src/codeableModels/impl/CEnumImpl.java b/src/codeableModels/impl/CEnumImpl.java deleted file mode 100644 index 578429b..0000000 --- a/src/codeableModels/impl/CEnumImpl.java +++ /dev/null @@ -1,26 +0,0 @@ -package codeableModels.impl; - -import codeableModels.*; - -import java.util.*; - -public class CEnumImpl extends CNamedElementImpl implements CEnum { - private List enumValues = new ArrayList<>(); - - public CEnumImpl(String name, List enumValueStrings) { - super(name); - enumValues = enumValueStrings; - } - - @Override - public List getValues() { - return enumValues; - } - - @Override - public boolean isLegalValue(String value) { - return enumValues.contains(value); - } - - -} diff --git a/src/codeableModels/impl/CLinkImpl.java b/src/codeableModels/impl/CLinkImpl.java deleted file mode 100644 index b3c2455..0000000 --- a/src/codeableModels/impl/CLinkImpl.java +++ /dev/null @@ -1,126 +0,0 @@ -package codeableModels.impl; - -import codeableModels.*; - -import java.util.*; - -public class CLinkImpl extends CNamedElementImpl implements CLink { - private final CObject object1; - private final CObject object2; - private final CAssociation association; - private final CTaggedValues taggedValues = new CTaggedValues(); - private final CStereotypeInstanceList stereotypeInstances = new CStereotypeInstanceList(this); - - @Override - public CAssociation getAssociation() { - return association; - } - - @SuppressWarnings("RedundantThrows") - public CLinkImpl(CModel model, CAssociation association, CObject object1, CObject object2) throws CException { - super("[" + object1.getName() + " -> " + object2.getName() + "]"); - setModel(model); - this.association = association; - this.object1 = object1; - this.object2 = object2; - - } - - @Override - public List getLinkedObjects() { - return Arrays.asList(object1, object2); - } - - @Override - public CObject getLinkedObjectByName(String objectName) { - if (object1.getName().equals(objectName)) { - return object1; - } else if (object2.getName().equals(objectName)) { - return object2; - } - return null; - } - - @Override - public CObject getLinkedObjectByClassifier(CClassifier classifier) { - if (object1.getClassifier() == classifier) { - return object1; - } else if (object2.getClassifier() == classifier) { - return object2; - } - return null; - } - - @Override - public CObject getLinkedObjectAtTargetEnd(CAssociationEnd targetEnd) throws CException { - if (targetEnd == association.getEnds().get(0)) { - return object1; - } else if (targetEnd == association.getEnds().get(1)) { - return object2; - } - throw new CException("target end unknown in link"); - } - - @Override - public Object getTaggedValue(CStereotype stereotype, String taggedValueName) throws CException { - return taggedValues.getTaggedValue(stereotypeInstances.getStereotypeInstancePath(), stereotype, taggedValueName); - } - - @Override - public void setTaggedValue(CStereotype stereotype, String taggedValueName, Object value) throws CException { - taggedValues.setTaggedValue(stereotypeInstances.getStereotypeInstancePath(), stereotype, taggedValueName, value); - } - - @Override - public Object getTaggedValue(String taggedValueName) throws CException { - return taggedValues.getTaggedValue(stereotypeInstances.getStereotypeInstancePath(), taggedValueName); - } - - @Override - public void setTaggedValue(String taggedValueName, Object value) throws CException { - taggedValues.setTaggedValue(stereotypeInstances.getStereotypeInstancePath(), taggedValueName, value); - } - - @Override - public CStereotypedElement getStereotypedElement() { - return association; - } - - @Override - public String toString() { - return getName() + "<" + super.toString() + ">"; - } - - @Override - public void addStereotypeInstance(String name) throws CException { - CStereotype stereotype = getModel().lookupStereotype(name); - if (stereotype == null) { - throw new CException("stereotype '" + name + "' does not exist"); - } - addStereotypeInstance(stereotype); - } - - @Override - public void addStereotypeInstance(CStereotype stereotype) throws CException { - stereotypeInstances.addStereotype(stereotype); - } - - @Override - public void deleteStereotypeInstance(String name) throws CException { - CStereotype stereotype = getModel().getStereotype(name); - if (stereotype == null) { - throw new CException("stereotype '" + name + "' does not exist"); - } - deleteStereotypeInstance(stereotype); - } - - @Override - public void deleteStereotypeInstance(CStereotype stereotype) throws CException { - stereotypeInstances.removeStereotype(stereotype); - } - - @Override - public List getStereotypeInstances() { - return stereotypeInstances.getStereotypes(); - } -} diff --git a/src/codeableModels/impl/CMetaclassImpl.java b/src/codeableModels/impl/CMetaclassImpl.java deleted file mode 100644 index 6cd762c..0000000 --- a/src/codeableModels/impl/CMetaclassImpl.java +++ /dev/null @@ -1,188 +0,0 @@ -package codeableModels.impl; - -import codeableModels.*; - -import java.util.*; - -public class CMetaclassImpl extends CClassifierImpl implements CMetaclass { - private final List classInstances = new ArrayList<>(); - private final CStereotypeList stereotypeList = new CStereotypeList(this); - - public CMetaclassImpl(String name) { - super(name); - } - - void addClassInstance(CClass cl) { - classInstances.add(cl); - } - - @Override - public List getClassInstances() { - return classInstances; - } - - @Override - public List getAllClassInstances() { - List result = new ArrayList<>(classInstances); - for (CClassifier cl : getAllSubclasses()) { - result.addAll(((CMetaclass) cl).getClassInstances()); - } - return result; - } - - void removeClassInstance(CClass cl) throws CException { - boolean success = classInstances.remove(cl); - if (!success) { - throw new CException("can't remove class instance '" + cl.getName() + "' from metaclass '" + this.getName() - + "': is not a class instance"); - } - } - - @Override - public List getStereotypes() { - return stereotypeList.getStereotypes(); - } - - @Override - public void addStereotype(CStereotype stereotype) throws CException { - stereotypeList.addStereotype(stereotype); - ((CStereotypeImpl)stereotype).addStereotypedElement(this); - } - - @Override - public void addStereotype(String stereotypeString) throws CException { - CStereotype stereotype = getModel().lookupStereotype(stereotypeString); - if (stereotype == null) { - throw new CException("stereotype '" + stereotypeString + "' does not exist"); - } - addStereotype(stereotype); - } - - @Override - public void removeStereotype(CStereotype stereotype) throws CException { - stereotypeList.removeStereotype(stereotype); - ((CStereotypeImpl)stereotype).removeStereotypedElement(this); - } - - @Override - public void removeStereotype(String stereotypeString) throws CException { - CStereotype stereotype = getModel().lookupStereotype(stereotypeString); - if (stereotype == null) { - throw new CException("stereotype '" + stereotypeString + "' does not exist"); - } - removeStereotype(stereotype); - } - - @Override - protected void cleanupClassifier() throws CException { - super.cleanupClassifier(); - ArrayList delClasses = new ArrayList<>(getClassInstances()); - for (CClass cl : delClasses) { - getModel().deleteClassifier(cl); - } - List stereotypesCopy = new ArrayList<>(stereotypeList.getStereotypes()); - for (CStereotype stereotype : stereotypesCopy) { - removeStereotype(stereotype); - } - stereotypeList.resetStereotypes(); - } - - @Override - public CMetaclass addSuperclass(CClassifier superclass) throws CException { - if (!(superclass instanceof CMetaclass)) { - throw new CException( - "cannot add superclass '" + superclass.getName() + "' to '" + getName() + "': not a metaclass"); - } - return super.addSuperclass(superclass).asMetaclass(); - } - - @Override - public CMetaclass addSuperclass(String superclassString) throws CException { - return super.addSuperclass(superclassString).asMetaclass(); - } - - @Override - public CMetaclass deleteSuperclass(String name) throws CException { - return super.deleteSuperclass(name).asMetaclass(); - } - - @Override - public CMetaclass deleteSuperclass(CClassifier superclass) throws CException { - return super.deleteSuperclass(superclass).asMetaclass(); - } - - @Override - public CMetaclass addObjectAttribute(String name, CClassifier classifier) throws CException { - return super.addObjectAttribute(name, classifier).asMetaclass(); - } - - @Override - public CMetaclass addObjectAttribute(String name, String classifierName) throws CException { - return super.addObjectAttribute(name, classifierName).asMetaclass(); - } - - @Override - public CMetaclass addStringAttribute(String name) throws CException { - return super.addStringAttribute(name).asMetaclass(); - } - - @Override - public CMetaclass addIntAttribute(String name) throws CException { - return super.addIntAttribute(name).asMetaclass(); - } - - @Override - public CMetaclass addBooleanAttribute(String name) throws CException { - return super.addBooleanAttribute(name).asMetaclass(); - } - - @Override - public CMetaclass addFloatAttribute(String name) throws CException { - return super.addFloatAttribute(name).asMetaclass(); - } - - @Override - public CMetaclass addDoubleAttribute(String name) throws CException { - return super.addDoubleAttribute(name).asMetaclass(); - } - - @Override - public CMetaclass addLongAttribute(String name) throws CException { - return super.addLongAttribute(name).asMetaclass(); - } - - @Override - public CMetaclass addCharAttribute(String name) throws CException { - return super.addCharAttribute(name).asMetaclass(); - } - - @Override - public CMetaclass addByteAttribute(String name) throws CException { - return super.addByteAttribute(name).asMetaclass(); - } - - @Override - public CMetaclass addShortAttribute(String name) throws CException { - return super.addShortAttribute(name).asMetaclass(); - } - - @Override - public CMetaclass addEnumAttribute(String name, CEnum enumType) throws CException { - return super.addEnumAttribute(name, enumType).asMetaclass(); - } - - @Override - public CMetaclass setAttributeDefaultValue(String name, Object defaultValue) throws CException { - return super.setAttributeDefaultValue(name, defaultValue).asMetaclass(); - } - - @Override - public CMetaclass addAttribute(String name, Object defaultValue) throws CException { - return super.addAttribute(name, defaultValue).asMetaclass(); - } - - @Override - public CMetaclass deleteAttribute(String name) throws CException { - return super.deleteAttribute(name).asMetaclass(); - } -} diff --git a/src/codeableModels/impl/CModelImpl.java b/src/codeableModels/impl/CModelImpl.java deleted file mode 100644 index 9f4cf54..0000000 --- a/src/codeableModels/impl/CModelImpl.java +++ /dev/null @@ -1,452 +0,0 @@ -package codeableModels.impl; - -import codeableModels.*; - -import java.util.*; - -public class CModelImpl implements CModel { - private final Map classifiers = new LinkedHashMap<>(); - private final Map objects = new LinkedHashMap<>(); - private final List associations = new ArrayList<>(); - private final List importedModels = new ArrayList<>(); - private static long autoID = 0; - - private static List classifiersAsStringList(List cls) { - ArrayList result = new ArrayList<>(); - for (CClassifier cl : cls) { - result.add(cl.getName()); - } - return result; - } - - private String getAutoID() { - return "##" + autoID++; - } - - private CClassifier initClassifier(String name, CClassifier cl) - throws CException { - if (getClassifier(name) != null) { - throw new CException("classifier '" + name + "' cannot be created, classifier name already exists"); - } - classifiers.put(name, cl); - cl.setModel(this); - return cl; - } - - @Override - public CMetaclass createMetaclass(String name) throws CException { - return (CMetaclass) initClassifier(name, new CMetaclassImpl(name)); - } - - @Override - public CMetaclass createMetaclass() throws CException { - return createMetaclass(getAutoID()); - } - - private void initObject(CObject obj) throws CException { - obj.setModel(this); - for (CClassifier classifier : obj.getClassifierPath()) { - for (CAttribute attribute : classifier.getAttributes()) { - if (attribute.getDefaultValue() != null) { - obj.setAttributeValue(classifier, attribute.getName(), attribute.getDefaultValue()); - } - } - } - } - - @Override - public CClass createClass(CMetaclass metaclass, String name) throws CException { - CClass cl = (CClass) initClassifier(name, new CClassImpl(name)); - ((CClassImpl) cl).setMetaclass(metaclass); - initObject(((CClassImpl) cl).getClassObjectImpl()); - return cl; - } - - @Override - public CClass createClass(CMetaclass metaclass) throws CException { - return createClass(metaclass, getAutoID()); - } - - - @Override - public CClass createClass(String metaclassName, String name) throws CException { - CMetaclass mcl = lookupMetaclass(metaclassName); - if (mcl == null) { - throw new CException("can't find metaclass: '" + metaclassName + "' to be instantiated"); - } - return createClass(mcl, name); - } - - @Override - public CClass createClass(String metaclassName) throws CException { - return createClass(metaclassName, getAutoID()); - } - - @Override - public CObject createObject(CClass cl, String name) throws CException { - if (getObject(name) != null) { - throw new CException("object '" + name + "' cannot be created, object name already exists"); - } - CObject obj = new CObjectImpl(name); - ((CObjectImpl) obj).setClassifier(cl); - ((CClassImpl) cl).addInstance(obj); - objects.put(name, obj); - initObject(obj); - return obj; - } - - @Override - public CObject createObject(CClass cl) throws CException { - return createObject(cl, getAutoID()); - } - - @Override - public CObject createObject(String className, String name) throws CException { - CClass cl = lookupClass(className); - if (cl == null) { - throw new CException("can't find class: '" + className + "' to be instantiated"); - } - return createObject(cl, name); - } - - @Override - public CObject createObject(String className) throws CException { - return createObject(className, getAutoID()); - } - - @Override - public CStereotype createStereotype(String name) throws CException { - return (CStereotype) initClassifier(name, new CStereotypeImpl(name)); - } - - @Override - public CStereotype createStereotype() throws CException { - return createStereotype(getAutoID()); - } - - @Override - public CStereotype createStereotype(String name, CStereotypedElement stereotypedElement) throws CException { - CStereotype stereotype = (CStereotype) initClassifier(name, new CStereotypeImpl(name)); - stereotypedElement.addStereotype(stereotype); - return stereotype; - } - - @Override - public CStereotype createStereotype(String name, String stereotypedElementName) throws CException { - CMetaclass metaclass = lookupMetaclass(stereotypedElementName); - if (metaclass != null) { - return createStereotype(name, metaclass); - } - List associations = getAssociationsByName(stereotypedElementName); - if (associations.size() == 0) { - throw new CException("can't find association '" + stereotypedElementName + "' to be stereotyped"); - } - if (associations.size() > 1) { - throw new CException("found multiple associations with the name '" + stereotypedElementName + - "', use reference to select stereotype instead"); - } - return createStereotype(name, associations.get(0)); - } - - @Override - public CAssociation createAssociation(String name, CAssociationEnd end1, CAssociationEnd end2) throws CException { - if ((end1.getClassifier() instanceof CClass && !(end2.getClassifier() instanceof CClass))) { - throw new CException("association between model element and metamodel element not allowed: '" - + end1.getClassifier().getName() + ", " + end2.getClassifier().getName()); - } - - if ((end1.getClassifier() instanceof CMetaclass || end1.getClassifier() instanceof CStereotype) - && !(end2.getClassifier() instanceof CMetaclass || end2.getClassifier() instanceof CStereotype)) { - throw new CException("association between model element and metamodel element not allowed: '" - + end1.getClassifier().getName() + ", " + end2.getClassifier().getName()); - } - - CAssociation assoc = new CAssociationImpl(this, name); - ((CAssociationImpl) assoc).setEnd1(end1); - ((CAssociationImpl) assoc).setEnd2(end2); - assoc.setModel(this); - associations.add(assoc); - ((CClassifierImpl) end1.getClassifier()).addAssociation(assoc); - ((CClassifierImpl) end2.getClassifier()).addAssociation(assoc); - return assoc; - } - - @Override - public CAssociation createComposition(String name, CAssociationEnd composingEnd, CAssociationEnd composedEnd) - throws CException { - CAssociation assoc = createAssociation(name, composingEnd, composedEnd); - assoc.setComposition(true); - return assoc; - } - - @Override - public CAssociation createAggregation(String name, CAssociationEnd aggregatingEnd, CAssociationEnd aggregatedEnd) - throws CException { - CAssociation assoc = createAssociation(name, aggregatingEnd, aggregatedEnd); - assoc.setAggregation(true); - return assoc; - } - - @Override - public CAssociation createAssociation(CAssociationEnd end1, CAssociationEnd end2) throws CException { - return createAssociation(null, end1, end2); - } - - @Override - public CAssociation createComposition(CAssociationEnd end1, CAssociationEnd end2) throws CException { - return createComposition(null, end1, end2); - } - - @Override - public CAssociation createAggregation(CAssociationEnd end1, CAssociationEnd end2) throws CException { - return createAggregation(null, end1, end2); - } - - @Override - public CModel importModel(CModel model) { - importedModels.add(model); - return this; - } - - @Override - public CClassifier getClassifier(String name) { - return classifiers.get(name); - } - - @Override - public CMetaclass getMetaclass(String name) throws CException { - CClassifier cl = getClassifier(name); - return cl == null ? null : cl.asMetaclass(); - } - - @Override - public CStereotype getStereotype(String name) throws CException { - CClassifier cl = getClassifier(name); - return cl == null ? null : cl.asStereotype(); - } - - @Override - public CClass getClass(String name) throws CException { - CClassifier cl = getClassifier(name); - return cl == null ? null : cl.asClass(); - } - - @Override - public CObject getObject(String name) { - return objects.get(name); - } - - @Override - public CClassifier lookupClassifier(String name) { - CClassifier result = getClassifier(name); - if (result != null) { - return result; - } - for (CModel m : importedModels) { - result = m.lookupClassifier(name); - if (result != null) { - return result; - } - } - return null; - } - - @Override - public CMetaclass lookupMetaclass(String name) throws CException { - CClassifier cl = lookupClassifier(name); - return cl == null ? null : cl.asMetaclass(); - } - - @Override - public CClass lookupClass(String name) throws CException { - CClassifier cl = lookupClassifier(name); - return cl == null ? null : cl.asClass(); - } - - @Override - public CStereotype lookupStereotype(String name) throws CException { - CClassifier cl = lookupClassifier(name); - return cl == null ? null : cl.asStereotype(); - } - - @Override - public List getClassifierNames() { - return new ArrayList<>(classifiers.keySet()); - } - - @Override - public List getClassifiers() { - return new ArrayList<>(classifiers.values()); - } - - @Override - public List getMetaclasses() { - ArrayList result = new ArrayList<>(); - for (CClassifier cl : getClassifiers()) { - if (cl instanceof CMetaclass) { - result.add((CMetaclass) cl); - } - } - return result; - } - - @Override - public List getClasses() { - ArrayList result = new ArrayList<>(); - for (CClassifier cl : getClassifiers()) { - if (cl instanceof CClass) { - result.add((CClass) cl); - } - } - return result; - } - - @Override - public List getStereotypes() { - ArrayList result = new ArrayList<>(); - for (CClassifier cl : getClassifiers()) { - if (cl instanceof CStereotype) { - result.add((CStereotype) cl); - } - } - return result; - } - - @Override - public List getMetaclassNames() { - return classifiersAsStringList(getMetaclasses()); - } - - @Override - public List getStereotypeNames() { - return classifiersAsStringList(getStereotypes()); - } - - @Override - public List getClassNames() { - return classifiersAsStringList(getClasses()); - } - - @Override - public void deleteClassifier(CClassifier cl) throws CException { - if (cl == null) { - throw new CException("classifier '' to be deleted does not exist"); - } - if (!classifiers.containsKey(cl.getName())) { - throw new CException("classifier '" + cl.getName() + "' to be deleted does not exist"); - } - - ((CClassifierImpl) cl).cleanupClassifier(); - - classifiers.remove(cl.getName()); - cl.setModel(null); - } - - @Override - public List getObjectNames() { - return new ArrayList<>(objects.keySet()); - } - - @Override - public List getObjects() { - return new ArrayList<>(objects.values()); - } - - @Override - public void deleteObject(CObject o) throws CException { - if (o == null) { - throw new CException("object '' to be deleted does not exist"); - } - if (!objects.containsKey(o.getName())) { - throw new CException("object '" + o.getName() + "' to be deleted does not exist"); - } - if (o.getClassifier() == null) { - throw new CException("trying to delete object '" + o.getName() + "' that has been deleted before"); - } - ((CClassImpl) o.getClassifier()).removeInstance(o); - ((CObjectImpl) o).setClassifier(null); - objects.remove(o.getName()); - o.setModel(null); - } - - @Override - public List getImportedModels() { - return importedModels; - } - - private void getFullModelList(List models) { - models.add(this); - for (CModel importedModel : importedModels) { - ((CModelImpl) importedModel).getFullModelList(models); - } - } - - @Override - public List getFullModelList() { - List models = new ArrayList<>(); - getFullModelList(models); - return models; - } - - @Override - public List getAssociations() { - return associations; - } - - private List getAssociationsForTypeForThisModel(CClassifier type) { - List result = new ArrayList<>(); - for (CAssociation association : associations) { - if (association.hasEndType(type)) { - result.add(association); - } - } - return result; - } - - @Override - public List getAssociationsForType(CClassifier type) { - List result = new ArrayList<>(); - List types = new ArrayList<>(); - types.add(type); - types.addAll(type.getAllSuperclasses()); - for (CModel model : getFullModelList()) { - for (CClassifier t : types) { - result.addAll(((CModelImpl) model).getAssociationsForTypeForThisModel(t)); - } - } - return result; - } - - @Override - public void deleteAssociation(CAssociation assoc) throws CException { - assoc.setModel(null); - associations.remove(assoc); - List ends = assoc.getEnds(); - ((CClassifierImpl) ends.get(0).getClassifier()).removeAssociation(assoc); - ((CClassifierImpl) ends.get(1).getClassifier()).removeAssociation(assoc); - } - - // returns an array list, as association names don't have to be unique, - // including null == association with no name - @Override - public List getAssociationsByName(String name) { - List result = new ArrayList<>(); - for (CAssociation a : associations) { - if (a.getName() == null) { - if (name == null) { - result.add(a); - } - } else { - if (a.getName().equals(name)) { - result.add(a); - } - } - } - return result; - } - - @Override - public CEnum createEnum(String name, List enumValues) { - return new CEnumImpl(name, enumValues); - } -} diff --git a/src/codeableModels/impl/CMultiplicityImpl.java b/src/codeableModels/impl/CMultiplicityImpl.java deleted file mode 100644 index eb07892..0000000 --- a/src/codeableModels/impl/CMultiplicityImpl.java +++ /dev/null @@ -1,93 +0,0 @@ -package codeableModels.impl; - -import codeableModels.*; - -public class CMultiplicityImpl implements CMultiplicity { - private int upperMultiplicity, lowerMultiplicity; - private String multiplicityString; - - public CMultiplicityImpl(String multiplicity) throws CException { - setMultiplicity(multiplicity); - multiplicityString = multiplicity; - } - - /* - public CMultiplicityImpl(int lower, int upper) { - this.upperMultiplicity = upper; - this.lowerMultiplicity = lower; - } - */ - - private void setMultiplicity(String multiplicity) throws CException { - try { - String lowerStr, upperStr; - if (multiplicity.contains("..")) { - lowerStr = multiplicity.replaceFirst("..([^.]*)$", "").trim(); - upperStr = multiplicity.replaceFirst("^([^.]*)..", "").trim(); - lowerMultiplicity = Integer.parseInt(lowerStr); - if (lowerMultiplicity < 0) { - throw new CException("negative multiplicity in '" - + multiplicity + "'"); - } - if (upperStr.equals("*")) { - upperMultiplicity = STAR_MULTIPLICITY; - } else { - upperMultiplicity = Integer.parseInt(upperStr); - if (upperMultiplicity < 0) { - throw new CException("negative multiplicity in '" - + multiplicity + "'"); - } - } - } else if (multiplicity.trim().equals("*")) { - lowerMultiplicity = 0; - upperMultiplicity = STAR_MULTIPLICITY; - } else { - lowerMultiplicity = Integer.parseInt(multiplicity.trim()); - if (lowerMultiplicity < 0) { - throw new CException("negative multiplicity in '" - + multiplicity + "'"); - } - upperMultiplicity = lowerMultiplicity; - } - } catch (Exception e) { - if (e instanceof CException) { - throw (CException) e; - } - throw new CException("malformed multiplicity '" + multiplicity - + "'"); - } - } - - @Override - public String getMultiplicity() { - if (lowerMultiplicity == 0 && upperMultiplicity == STAR_MULTIPLICITY) { - return "*"; - } - StringBuilder r = new StringBuilder(); - r.append(lowerMultiplicity); - if (lowerMultiplicity != upperMultiplicity) { - r.append(".."); - if (upperMultiplicity == STAR_MULTIPLICITY) { - r.append("*"); - } else { - r.append(upperMultiplicity); - } - } - return r.toString(); - } - - @Override - public int getUpperMultiplicity() { - return upperMultiplicity; - } - - @Override - public int getLowerMultiplicity() { - return lowerMultiplicity; - } - - public String toString() { - return multiplicityString; - } - -} diff --git a/src/codeableModels/impl/CNamedElementImpl.java b/src/codeableModels/impl/CNamedElementImpl.java deleted file mode 100644 index 6d50f55..0000000 --- a/src/codeableModels/impl/CNamedElementImpl.java +++ /dev/null @@ -1,26 +0,0 @@ -package codeableModels.impl; - -import codeableModels.*; - -public abstract class CNamedElementImpl extends CElementImpl implements CNamedElement { - final private String name; - - CNamedElementImpl(String name) { - super(); - this.name = name; - } - - @Override - public String getName() { - return name; - } - - @Override - public String toString() { - String n = ""; - if (name != null) { - n = name; - } - return n + "<" + super.toString() + ">"; - } -} diff --git a/src/codeableModels/impl/CObjectImpl.java b/src/codeableModels/impl/CObjectImpl.java deleted file mode 100644 index d7616bb..0000000 --- a/src/codeableModels/impl/CObjectImpl.java +++ /dev/null @@ -1,302 +0,0 @@ -package codeableModels.impl; - -import codeableModels.*; - -import java.util.*; - -public class CObjectImpl extends CNamedElementImpl implements CObject { - private final CAttributeValues attributeValues = new CAttributeValues(); - // instance used in association links is: for CObjects = this, for CClass = the class as a CObject - private CObject thisInLinks; - private CClassifier classifier; - - public CObjectImpl(String name) { - super(name); - setThisInLinks(this); - } - - @Override - public CClassifier getClassifier() { - return classifier; - } - - void setClassifier(CClassifier classifier) { - this.classifier = classifier; - } - - @Override - public boolean instanceOf(CClassifier classifier) { - return getClassifier() == classifier || getClassifier().getAllSuperclasses().contains(classifier); - } - - @Override - public boolean instanceOf(String classifierName) { - return instanceOf(getModel().lookupClassifier(classifierName)); - } - - private CObject getThisInLinks() { - return thisInLinks; - } - - void setThisInLinks(CObject thisObject) { - thisInLinks = thisObject; - } - - private static String getRoleNameString(CAssociationEnd targetEnd) { - String roleNameString = ""; - if (targetEnd.getRoleName() != null) { - roleNameString = "with role name '" + targetEnd.getRoleName() + "' "; - } - return roleNameString; - } - - private CAssociation getAssociationFromTargetEnd(CAssociationEnd targetEnd) throws CException { - CAssociation association = getClassifier().getAssociationByEnd(targetEnd); - if (association != null) { - CAssociationEnd myEnd = association.getOtherEnd(targetEnd); - if (thisInLinks.getClassifierPath().contains(myEnd.getClassifier())) { - // this is my association and my end has my classifier - return association; - } - } - throw new CException("end " + getRoleNameString(targetEnd) + "is not an association target end of '" + - thisInLinks.getName() + "'"); - } - - private void checkAssociationWithTargetEndExistsForClassifier(CAssociationEnd targetEnd) throws CException { - // if we can get the association, it exists, else the get throws an exception - getAssociationFromTargetEnd(targetEnd); - } - - private void checkIsNavigable(CAssociationEnd targetEnd) throws CException { - if (!targetEnd.isNavigable()) { - throw new CException("end " + getRoleNameString(targetEnd) + "is not navigable and thus cannot be accessed from object '" + - thisInLinks.getName() + "'"); - } - } - - - private CLink addLinkToCObject(CAssociationEnd targetEnd, CObject linkObject) throws CException { - CAssociation association = getAssociationFromTargetEnd(targetEnd); - return association.addLink(targetEnd, getThisInLinks(), linkObject); - } - - private List addLinksToCObjects(CAssociationEnd targetEnd, List linkObjects) throws CException { - List addedLinks = new ArrayList<>(); - for (CObject object : linkObjects) { - addedLinks.add(addLinkToCObject(targetEnd, object)); - } - return addedLinks; - } - - private List convertObjectListToCObjects(List links) throws CException { - List objectList = new ArrayList<>(); - for (Object link : links) { - CObject object; - if (link instanceof CObject) { - object = (CObject) link; - } else if (link instanceof String) { - if (classifier instanceof CMetaclass) { - object = getModel().getClass((String) link); - if (object == null) { - throw new CException("class '" + link + "' unknown"); - } - } else { - object = getModel().getObject((String) link); - if (object == null) { - throw new CException("object '" + link + "' unknown"); - } - } - } else { - throw new CException("argument '" + link + "' is not of type String or CObject"); - } - objectList.add(object); - } - return objectList; - } - - @Override - public List addLinks(CAssociationEnd targetEnd, List links) throws CException { - return addLinksToCObjects(targetEnd, convertObjectListToCObjects(links)); - } - - @Override - public CLink addLink(CAssociationEnd targetEnd, CObject link) throws CException { - if (link == null) { - addLinks(targetEnd, new ArrayList<>(Collections.emptyList())); - return null; - } - List addedLinks = addLinks(targetEnd, new ArrayList<>(Collections.singletonList(link))); - return addedLinks.get(0); - } - - @Override - public CLink addLink(CAssociationEnd targetEnd, String link) throws CException { - if (link == null) { - addLinks(targetEnd, new ArrayList<>(Collections.emptyList())); - return null; - } - List addedLinks = addLinks(targetEnd, new ArrayList<>(Collections.singletonList(link))); - return addedLinks.get(0); - } - - @Override - public List getLinkObjects(CAssociationEnd targetEnd) throws CException { - CAssociation association = getAssociationFromTargetEnd(targetEnd); - return association.getLinksByObject(targetEnd, thisInLinks); - } - - @Override - public List getLinkObjects(CAssociation association) throws CException { - CAssociationEnd myEnd = association.getEndByClassifier(getClassifier()); - if (myEnd == null) { - throw new CException("end for classifier '" + getClassifier().getName() + "' unknown in association"); - } - CAssociationEnd targetEnd = association.getOtherEnd(myEnd); - List links = new ArrayList<>(association.getLinksByObject(targetEnd, thisInLinks)); - // if the target end also matches the classifier, this is a link from a class to itself -> consider also - // the link objects stored in the other direction. - if (CAssociationImpl.checkClassifierMatches(getClassifier(), targetEnd.getClassifier())) { - links.addAll(association.getLinksByObject(myEnd, thisInLinks)); - } - return links; - } - - @Override - public List getLinks(CAssociationEnd targetEnd) throws CException { - checkIsNavigable(targetEnd); - List results = new ArrayList<>(); - List links = getLinkObjects(targetEnd); - for (CLink link : links) { - CObject object = link.getLinkedObjectAtTargetEnd(targetEnd); - if (object != thisInLinks) { - results.add(object); - } - } - return results; - } - - @Override - public void removeLink(CAssociationEnd targetEnd, CObject target) throws CException { - CAssociation association = getAssociationFromTargetEnd(targetEnd); - checkIsNavigable(targetEnd); - association.removeLink(targetEnd, thisInLinks, target); - } - - @Override - public void removeLink(CAssociationEnd targetEnd, String targetString) throws CException { - removeLinks(targetEnd, new ArrayList<>(Collections.singletonList(targetString))); - } - - @Override - public void removeLinks(CAssociationEnd targetEnd, List targets) throws CException { - List targetObjects = convertObjectListToCObjects(targets); - for (CObject target : targetObjects) { - removeLink(targetEnd, target); - } - } - - @Override - public void removeAllLinks(CAssociationEnd targetEnd) throws CException { - CAssociation association = getAssociationFromTargetEnd(targetEnd); - checkIsNavigable(targetEnd); - List linksOfThisObject = new ArrayList<>(association.getLinksByObject(targetEnd, thisInLinks)); - for (CLink targetLink : linksOfThisObject) { - association.removeLink(targetLink); - } - } - - @Override - public List setLinks(CAssociationEnd targetEnd, List links) throws CException { - checkAssociationWithTargetEndExistsForClassifier(targetEnd); - checkIsNavigable(targetEnd); - if (links.isEmpty()) { - // if this sets the links to 0, addLinks is not triggered and will not check that - // the multiplicity 0 is acceptable, so we check here, before performing the removal - CAssociationImpl.checkAssociationMultiplicityRange(targetEnd, 0); - } - removeAllLinks(targetEnd); - return addLinks(targetEnd, links); - } - - @Override - public CLink setLink(CAssociationEnd targetEnd, CObject link) throws CException { - if (link == null) { - setLinks(targetEnd, new ArrayList<>(Collections.emptyList())); - return null; - } - List links = setLinks(targetEnd, new ArrayList<>(Collections.singletonList(link))); - return links.get(0); - } - - @Override - public CLink setLink(CAssociationEnd targetEnd, String link) throws CException { - if (link == null) { - setLinks(targetEnd, new ArrayList<>(Collections.emptyList())); - return null; - } - List links = setLinks(targetEnd, new ArrayList<>(Collections.singletonList(link))); - return links.get(0); - } - - @Override - public Object getAttributeValue(CClassifier classifier, String attributeName) throws CException { - CAttribute attribute = classifier.getAttribute(attributeName); - if (attribute == null) { - throw new CException("attribute '" + attributeName + "' unknown for classifier '" + classifier.getName() - + "' on object '" + getName() + "'"); - } - return attributeValues.get(classifier, attributeName); - } - - @Override - public void setAttributeValue(CClassifier classifier, String attributeName, Object value) throws CException { - CAttribute attribute = classifier.getAttribute(attributeName); - if (attribute == null) { - throw new CException("attribute '" + attributeName + "' unknown for classifier '" + classifier.getName() - + "' on object '" + getName() + "'"); - } - CAttributeImpl.checkAttributeValueType(attributeName, ((CAttributeImpl) attribute), value); - attributeValues.add(classifier, attributeName, value); - } - - @Override - public Object getAttributeValue(String attributeName) throws CException { - for (CClassifier classifier : getClassifierPath()) { - if (classifier.getAttribute(attributeName) != null) { - return getAttributeValue(classifier, attributeName); - } - } - throw new CException("attribute '" + attributeName + "' unknown for object '" + getName() + "'"); - } - - @Override - public void setAttributeValue(String attributeName, Object value) throws CException { - for (CClassifier classifier : getClassifierPath()) { - if (classifier.getAttribute(attributeName) != null) { - setAttributeValue(classifier, attributeName, value); - return; - } - } - throw new CException("attribute '" + attributeName + "' unknown for object '" + getName() + "'"); - } - - private List getClassifierPathSuperclasses(CClassifier classifier) { - List classifierPath = new ArrayList<>(); - classifierPath.add(classifier); - for (CClassifier superclass : classifier.getSuperclasses()) { - for (CClassifier cl : getClassifierPathSuperclasses(superclass)) { - if (!classifierPath.contains(cl)) { - classifierPath.add(cl); - } - } - } - return classifierPath; - } - - @Override - public List getClassifierPath() { - return getClassifierPathSuperclasses(getClassifier()); - } - -} diff --git a/src/codeableModels/impl/CStereotypeImpl.java b/src/codeableModels/impl/CStereotypeImpl.java deleted file mode 100644 index fefd11a..0000000 --- a/src/codeableModels/impl/CStereotypeImpl.java +++ /dev/null @@ -1,240 +0,0 @@ -package codeableModels.impl; - -import codeableModels.*; - -import java.util.*; - -public class CStereotypeImpl extends CClassifierImpl implements CStereotype { - private final List stereotypedElements = new ArrayList<>(); - private final List stereotypedElementInstances = new ArrayList<>(); - - public CStereotypeImpl(String name) { - super(name); - } - - @Override - public List getStereotypedElements() { - return stereotypedElements; - } - - void addStereotypedElement(CStereotypedElement element) { - stereotypedElements.add(element); - } - - void removeStereotypedElement(CStereotypedElement element) { - stereotypedElements.remove(element); - } - - /* - @Override - public CStereotype setStereotypedElement(CStereotypedElement newStereotypedElement) throws CException { - if (stereotypedElement != null) { - stereotypedElement.removeStereotype(this); - } - stereotypedElement = newStereotypedElement; - if (newStereotypedElement != null) { - if (newStereotypedElement instanceof CAssociation) { - CAssociation association = (CAssociation) newStereotypedElement; - CClassifier cl1 = association.getEnds().get(0).getClassifier(), - cl2 = association.getEnds().get(1).getClassifier(); - if (!(cl1 instanceof CMetaclass && cl2 instanceof CMetaclass)) { - throw new CException("association classifiers '" + cl1.getName() + - "' and/or '" + cl2.getName() + "' are not metaclasses"); - } - } - newStereotypedElement.addStereotype(this); - } - return this; - } - */ - - - private boolean isMetaclassExtendedByThisStereotype(CMetaclass metaclass) { - if (stereotypedElements.contains(metaclass)) { - return true; - } - for (CClassifier superclass : metaclass.getAllSuperclasses()) { - CMetaclass scMetaclass = (CMetaclass) superclass; - if (stereotypedElements.contains(scMetaclass)) { - return true; - } - } - return false; - } - - boolean isElementExtendedByStereotype(CStereotypedElement element) throws CException { - if (element instanceof CMetaclass) { - CMetaclass metaclass = (CMetaclass) element; - if (isMetaclassExtendedByThisStereotype(metaclass)) { - return true; - } - for (CClassifier superclass : getAllSuperclasses()) { - if (((CStereotypeImpl) superclass).isMetaclassExtendedByThisStereotype(metaclass)) { - return true; - } - } - return false; - } else if (element instanceof CAssociation) { - CAssociation association = (CAssociation) element; - if (stereotypedElements.contains(association)) { - return true; - } - for (CClassifier superclass : getAllSuperclasses()) { - if (((CStereotypeImpl) superclass).stereotypedElements.contains(association)) { - return true; - } - } - return false; - } - String nameString = ""; - if (element.getName() != null) { - nameString = element.getName() + " "; - } - throw new CException("element " + nameString + "is neither a metaclass nor an association"); - } - - @Override - public List getStereotypedElementInstances() { - return stereotypedElementInstances; - } - - void addStereotypedElementInstance(CStereotypedElementInstance stereotypedElementInstance) { - this.stereotypedElementInstances.add(stereotypedElementInstance); - } - - void removeStereotypedElementInstance(CStereotypedElementInstance stereotypedElementInstance) throws CException { - boolean success = stereotypedElementInstances.remove(stereotypedElementInstance); - if (!success) { - throw new CException("can't remove stereotyped element instance from stereotype" + - " '" + this.getName() + "'"); - } - } - - @Override - protected void cleanupClassifier() throws CException { - super.cleanupClassifier(); - List stereotypedElementsCopy = new ArrayList<>(stereotypedElements); - for (CStereotypedElement stereotypedElement : stereotypedElementsCopy) { - stereotypedElement.removeStereotype(this); - } - } - - @Override - public CStereotype addSuperclass(CClassifier superclass) throws CException { - if (!(superclass instanceof CStereotype)) { - throw new CException("cannot add superclass '" + superclass.getName() + "' to '" - + getName() + "': not a stereotype"); - } - return super.addSuperclass(superclass).asStereotype(); - } - - @Override - public CStereotype addSuperclass(String superclassString) throws CException { - return super.addSuperclass(superclassString).asStereotype(); - } - - @Override - public CStereotype deleteSuperclass(String name) throws CException { - return super.deleteSuperclass(name).asStereotype(); - } - - @Override - public CStereotype deleteSuperclass(CClassifier superclass) throws CException { - return super.deleteSuperclass(superclass).asStereotype(); - } - - @Override - public CStereotype addObjectAttribute(String name, CClassifier classifier) throws CException { - return super.addObjectAttribute(name, classifier).asStereotype(); - } - - @Override - public CStereotype addObjectAttribute(String name, String classifierName) throws CException { - return super.addObjectAttribute(name, classifierName).asStereotype(); - } - - @Override - public CStereotype addStringAttribute(String name) throws CException { - return super.addStringAttribute(name).asStereotype(); - } - - @Override - public CStereotype addIntAttribute(String name) throws CException { - return super.addIntAttribute(name).asStereotype(); - } - - @Override - public CStereotype addBooleanAttribute(String name) throws CException { - return super.addBooleanAttribute(name).asStereotype(); - } - - @Override - public CStereotype addFloatAttribute(String name) throws CException { - return super.addFloatAttribute(name).asStereotype(); - } - - @Override - public CStereotype addDoubleAttribute(String name) throws CException { - return super.addDoubleAttribute(name).asStereotype(); - } - - @Override - public CStereotype addLongAttribute(String name) throws CException { - return super.addLongAttribute(name).asStereotype(); - } - - @Override - public CStereotype addCharAttribute(String name) throws CException { - return super.addCharAttribute(name).asStereotype(); - } - - @Override - public CStereotype addByteAttribute(String name) throws CException { - return super.addByteAttribute(name).asStereotype(); - } - - @Override - public CStereotype addShortAttribute(String name) throws CException { - return super.addShortAttribute(name).asStereotype(); - } - - @Override - public CStereotype addEnumAttribute(String name, CEnum enumType) throws CException { - return super.addEnumAttribute(name, enumType).asStereotype(); - } - - private void setTaggedValueOnStereotypedElementInstances(List instances, - CStereotype stereotype, String name, Object value) - throws CException { - for (CStereotypedElementInstance instance : instances) { - instance.setTaggedValue(stereotype, name, value); - } - } - - private void setStereotypeDefaultValuesForOneAttribute(CStereotype stereotype, String name, Object defaultValue) throws CException { - setTaggedValueOnStereotypedElementInstances(stereotypedElementInstances, stereotype, name, defaultValue); - for (CClassifier sc : stereotype.getAllSubclasses()) { - setTaggedValueOnStereotypedElementInstances(((CStereotypeImpl) sc).stereotypedElementInstances, stereotype, name, defaultValue); - } - } - - @Override - public CStereotype addAttribute(String name, Object defaultValue) throws CException { - CStereotype stereotype = super.addAttribute(name, defaultValue).asStereotype(); - setStereotypeDefaultValuesForOneAttribute(stereotype, name, defaultValue); - return stereotype; - } - - @Override - public CStereotype setAttributeDefaultValue(String name, Object defaultValue) throws CException { - CStereotype stereotype = super.setAttributeDefaultValue(name, defaultValue).asStereotype(); - setStereotypeDefaultValuesForOneAttribute(stereotype, name, defaultValue); - return stereotype; - } - - @Override - public CStereotype deleteAttribute(String name) throws CException { - return super.deleteAttribute(name).asStereotype(); - } - -} diff --git a/src/codeableModels/impl/CStereotypeInstanceList.java b/src/codeableModels/impl/CStereotypeInstanceList.java deleted file mode 100644 index 03851bd..0000000 --- a/src/codeableModels/impl/CStereotypeInstanceList.java +++ /dev/null @@ -1,79 +0,0 @@ -package codeableModels.impl; - -import codeableModels.*; - -import java.util.*; - -class CStereotypeInstanceList extends CStereotypeList { - private CStereotypedElementInstance stereotypedElementInstance; - - public CStereotypeInstanceList(CNamedElement element) throws CException { - super(element); - if (element instanceof CStereotypedElementInstance) { - stereotypedElementInstance = (CStereotypedElementInstance) element; - } else { - throw new CException("element " + element.getName() + - " is not of type CStereotypedElementInstance"); - } - } - - private void setAllDefaultTaggedValuesOfStereotype(CStereotype stereotype) throws CException { - for (CAttribute attribute : stereotype.getAttributes()) { - if (attribute.getDefaultValue() != null) { - stereotypedElementInstance.setTaggedValue(stereotype, - attribute.getName(), attribute.getDefaultValue()); - } - } - } - - public void addStereotype(CStereotype stereotype) throws CException { - if (stereotypes.contains(stereotype)) { - throw new CException("stereotype '" + stereotype.getName() + "' cannot be added: it is already a " + - "stereotype of '" + element.getName() + "'"); - } - if (!((CStereotypeImpl) stereotype).isElementExtendedByStereotype(stereotypedElementInstance.getStereotypedElement())) { - throw new CException("stereotype '" + stereotype.getName() + "' cannot be added to '" + element - .getName() + "': no extension by this stereotype found"); - } - stereotypes.add(stereotype); - ((CStereotypeImpl) stereotype).addStereotypedElementInstance(stereotypedElementInstance); - - setAllDefaultTaggedValuesOfStereotype(stereotype); - for (CClassifier sc : stereotype.getAllSuperclasses()) { - setAllDefaultTaggedValuesOfStereotype((CStereotypeImpl) sc); - } - } - - public void removeStereotype(CStereotype stereotype) throws CException { - super.removeStereotype(stereotype); - ((CStereotypeImpl) stereotype).removeStereotypedElementInstance(stereotypedElementInstance); - } - - - private List getStereotypeInstancePathSuperclasses(CStereotype stereotype) { - List stereotypePath = new ArrayList<>(); - stereotypePath.add(stereotype); - for (CClassifier superclass : stereotype.getSuperclasses()) { - for (CStereotype superclassStereotype : getStereotypeInstancePathSuperclasses((CStereotype) superclass)) { - if (!stereotypePath.contains(superclassStereotype)) { - stereotypePath.add(superclassStereotype); - } - } - } - return stereotypePath; - } - - public List getStereotypeInstancePath() { - List stereotypePath = new ArrayList<>(); - for (CStereotype stereotypeOfThisClass : getStereotypes()) { - for (CStereotype stereotype : getStereotypeInstancePathSuperclasses(stereotypeOfThisClass)) { - if (!stereotypePath.contains(stereotype)) { - stereotypePath.add(stereotype); - } - } - } - return stereotypePath; - } - - -} diff --git a/src/codeableModels/impl/CStereotypeList.java b/src/codeableModels/impl/CStereotypeList.java deleted file mode 100644 index cc610b4..0000000 --- a/src/codeableModels/impl/CStereotypeList.java +++ /dev/null @@ -1,47 +0,0 @@ -package codeableModels.impl; - -import codeableModels.*; - -import java.util.*; - -class CStereotypeList { - List stereotypes = new ArrayList<>(); - final CNamedElement element; - - public CStereotypeList(CNamedElement element) { - this.element = element; - } - - public List getStereotypes() { - return stereotypes; - } - - private String getNameString() { - String nameString = ""; - if (element.getName() != null) { - nameString = " on '" + element.getName() + "'"; - } - return nameString; - } - - void addStereotype(CStereotype stereotype) throws CException { - if (stereotypes.contains(stereotype)) { - throw new CException("stereotype '" + stereotype.getName() + - "' already exists" + getNameString()); - } - stereotypes.add(stereotype); - } - - void removeStereotype(CStereotype stereotype) throws CException { - boolean success = stereotypes.remove(stereotype); - if (!success) { - throw new CException("can't remove stereotype '" + stereotype.getName() + "'" + - getNameString() + ": does not exist"); - } - } - - void resetStereotypes() { - stereotypes = new ArrayList<>(); - } - -} diff --git a/src/codeableModels/impl/CTaggedValues.java b/src/codeableModels/impl/CTaggedValues.java deleted file mode 100644 index c4c730e..0000000 --- a/src/codeableModels/impl/CTaggedValues.java +++ /dev/null @@ -1,61 +0,0 @@ -package codeableModels.impl; - -import codeableModels.*; - -import java.util.*; - -class CTaggedValues { - private final CAttributeValues taggedValues = new CAttributeValues(); - - public Object getTaggedValue(List legalStereotypes, CStereotype stereotype, String taggedValueName) throws CException { - if (!legalStereotypes.contains(stereotype)) { - throw new CException("stereotype '" + stereotype.getName() + "' is not a stereotype of element"); - } - return getTaggedValue(stereotype, taggedValueName); - } - - private Object getTaggedValue(CStereotype stereotype, String taggedValueName) throws CException { - CAttribute attribute = stereotype.getAttribute(taggedValueName); - if (attribute == null) { - throw new CException("tagged value '" + taggedValueName + "' unknown for stereotype '" + stereotype.getName() + "'"); - } - return taggedValues.get(stereotype, taggedValueName); - } - - public void setTaggedValue(List legalStereotypes, CStereotype stereotype, String taggedValueName, Object value) throws CException { - if (!legalStereotypes.contains(stereotype)) { - throw new CException("stereotype '" + stereotype.getName() + "' is not a stereotype of element"); - } - setTaggedValue(stereotype, taggedValueName, value); - } - - private void setTaggedValue(CStereotype stereotype, String taggedValueName, Object value) throws CException { - CAttribute attribute = stereotype.getAttribute(taggedValueName); - if (attribute == null) { - throw new CException("tagged value '" + taggedValueName + "' unknown for stereotype '" + stereotype.getName() + "'"); - } - CAttributeImpl.checkAttributeValueType(taggedValueName, ((CAttributeImpl) attribute), value); - taggedValues.add(stereotype, taggedValueName, value); - } - - // uses the first stereotype in the list that has the tagged value defined - public Object getTaggedValue(List legalStereotypes, String taggedValueName) throws CException { - for (CStereotype stereotype : legalStereotypes) { - if (stereotype.getAttribute(taggedValueName) != null) { - return getTaggedValue(stereotype, taggedValueName); - } - } - throw new CException("tagged value '" + taggedValueName + "' unknown"); - } - - // uses the first stereotype in the list that has the tagged value defined - public void setTaggedValue(List legalStereotypes, String taggedValueName, Object value) throws CException { - for (CStereotype stereotype : legalStereotypes) { - if (stereotype.getAttribute(taggedValueName) != null) { - setTaggedValue(stereotype, taggedValueName, value); - return; - } - } - throw new CException("tagged value '" + taggedValueName + "' unknown"); - } -} diff --git a/src/codeableModels/tests/AllTests.java b/src/codeableModels/tests/AllTests.java deleted file mode 100644 index 1a427c8..0000000 --- a/src/codeableModels/tests/AllTests.java +++ /dev/null @@ -1,18 +0,0 @@ -package codeableModels.tests; - -import org.junit.runner.*; -import org.junit.runners.*; -import org.junit.runners.Suite.*; - -@RunWith(Suite.class) -@SuiteClasses({TestsAssociationsInModel.class, TestsAttributes.class, TestsAttributesMetaclass.class, - TestsAttributesStereotype.class, TestsClass.class, TestsClassifier.class, - TestsImportModel.class, TestsInheritanceClass.class, TestsInheritanceMetaclass.class, - TestsInheritanceStereotype.class, TestsMetaclass.class, TestsObject.class, TestsStereotypesOnClasses.class, - TestsObjectLinks.class, TestsClassLinks.class, TestsAttributeValues.class, TestsTaggedValuesOnClasses.class, - TestsStereotypesOnAssociations.class, TestsTaggedValuesOnLinks.class, - TestsStereotypeInstancesOnClasses.class, - TestsStereotypeInstancesOnAssociations.class}) -public class AllTests { - -} diff --git a/src/codeableModels/tests/TestsAssociationsInModel.java b/src/codeableModels/tests/TestsAssociationsInModel.java deleted file mode 100644 index b41580d..0000000 --- a/src/codeableModels/tests/TestsAssociationsInModel.java +++ /dev/null @@ -1,587 +0,0 @@ -package codeableModels.tests; - -import codeableModels.*; -import org.junit.*; - -import java.util.*; - -import static org.junit.Assert.*; - -public class TestsAssociationsInModel { - - @Test - public void testAssociationCreationInModelWithoutName() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClassifier cl1 = model.createClass(mcl, "Class1"); - CClassifier cl2 = model.createClass(mcl, "Class2"); - CClassifier cl3 = model.createClass(mcl, "Class3"); - CClassifier cl4 = model.createClass(mcl, "Class4"); - - CAssociation assoc1 = model.createAssociation(cl1.createEnd("i", "*"), cl2.createEnd("t", "1")); - CAssociation assoc2 = model.createAssociation(cl1.createEnd("o", "*"), cl2.createEnd("s", "1")); - CAssociation assoc3 = model.createComposition(cl1.createEnd("a", "0..1"), cl3.createEnd("n", "*")); - CAssociation assoc4 = model.createComposition(cl1.createEnd("a", "0..1"), cl3.createEnd("e", "*")); - CAssociation assoc5 = model.createAggregation(cl4.createEnd("a", "0..1"), cl3.createEnd("n", "*")); - CAssociation assoc6 = model.createAggregation(cl3.createEnd("a", "3", false), cl2.createEnd("e", "*", false)); - - List associations1 = model.getAssociationsByName(null); - assertEquals(6, associations1.size()); - - List assoc1Ends = assoc1.getEnds(); - assertEquals("i", assoc1Ends.get(0).getRoleName()); - assertEquals("n", assoc5.getEndByClassifier(cl3).getRoleName()); - assertEquals("s", assoc2.getEndByClassifier(cl2).getRoleName()); - assertEquals("1", assoc1Ends.get(1).getMultiplicityString()); - assertEquals("*", assoc1Ends.get(0).getMultiplicityString()); - assertEquals("0..1", assoc4.getEndByRoleName("a").getMultiplicityString()); - assertEquals("3", assoc6.getEndByRoleName("a").getMultiplicityString()); - assertEquals(true, assoc1Ends.get(0).isNavigable()); - assoc1Ends.get(0).setNavigable(false); - assertEquals(false, assoc1Ends.get(0).isNavigable()); - assertEquals(false, assoc6.getEndByRoleName("a").isNavigable()); - assertEquals(false, assoc6.getEndByRoleName("e").isNavigable()); - - assertEquals(false, assoc1.isComposition()); - assertEquals(false, assoc1.isAggregation()); - assertEquals(true, assoc3.isComposition()); - assertEquals(false, assoc3.isAggregation()); - assertEquals(false, assoc5.isComposition()); - assertEquals(true, assoc5.isAggregation()); - - assoc1.setAggregation(true); - assertEquals(false, assoc1.isComposition()); - assertEquals(true, assoc1.isAggregation()); - assoc1.setComposition(true); - assertEquals(true, assoc1.isComposition()); - assertEquals(false, assoc1.isAggregation()); - - assertEquals(assoc1.getModel(), model); - } - - @Test - public void testRoleNameGuessing() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClassifier cl1 = model.createClass(mcl, "Class1"); - CClassifier cl2 = model.createClass(mcl, "Class2"); - - CAssociation assoc1 = model.createAssociation(cl1.createEnd("*"), cl2.createEnd("1")); - - assertEquals("class1", assoc1.getEnds().get(0).getRoleName()); - assertEquals("class2", assoc1.getEnds().get(1).getRoleName()); - } - - @Test - public void testGuessRoleNamesOnEnds() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClassifier cl1 = model.createClass(mcl, "Decision"); - CClassifier cl2 = model.createClass(mcl, "Model"); - CClassifier cl3 = model.createClass(mcl, "Class3"); - - CAssociation assoc1 = model.createAssociation(cl1.createEnd("*"), cl2.createEnd("1")); - CAssociation assoc2 = model.createAssociation(cl2.createEnd("*"), cl3.createEnd("1")); - - assertEquals("decision", assoc1.getEnds().get(0).getRoleName()); - assertEquals("model", assoc1.getEnds().get(1).getRoleName()); - assertEquals("model", assoc2.getEnds().get(0).getRoleName()); - assertEquals("class3", assoc2.getEnds().get(1).getRoleName()); - } - - @Test - public void testGetEndMethods() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClassifier cl2 = model.createClass(mcl, "Class2"); - CClassifier cl1 = model.createClass(mcl, "Class1").addSuperclass(cl2); - CClassifier cl3 = model.createClass(mcl, "Class3"); - CAssociationEnd endCL1 = cl1.createEnd("cl", "*"); - CAssociationEnd endCL2 = cl2.createEnd("cl", "*"); - CAssociationEnd endCL3 = cl3.createEnd("cl3", "*"); - CAssociation assoc1 = model.createAssociation("CL1-CL2", endCL1, endCL2); - CAssociation assoc2 = model.createAssociation("CL2-CL1", endCL2, endCL1); - CAssociation assoc3 = model.createAssociation("CL3-CL1", endCL3, endCL1); - - assertEquals(endCL1, assoc1.getEndByClassifier(cl1)); - assertEquals(endCL2, assoc1.getEndByClassifier(cl2)); - assertEquals(endCL1, assoc1.getEnds().get(0)); - assertEquals(endCL2, assoc1.getEnds().get(1)); - assertEquals(endCL1, assoc1.getEndByRoleName("cl")); - assertEquals(endCL1, assoc2.getEndByClassifier(cl1)); - assertEquals(endCL2, assoc2.getEndByClassifier(cl2)); - assertEquals(endCL2, assoc2.getEnds().get(0)); - assertEquals(endCL1, assoc2.getEnds().get(1)); - assertEquals(endCL2, assoc2.getEndByRoleName("cl")); - assertEquals(endCL1, assoc3.getEndByClassifier(cl1)); - assertEquals(endCL1, assoc3.getEndByClassifier(cl2)); - assertEquals(endCL3, assoc3.getEndByClassifier(cl3)); - assertEquals(endCL3, assoc3.getEnds().get(0)); - assertEquals(endCL1, assoc3.getEnds().get(1)); - assertEquals(endCL1, assoc3.getEndByRoleName("cl")); - assertEquals(endCL3, assoc3.getEndByRoleName("cl3")); - - assertEquals(endCL1, assoc1.getOtherEnd(endCL2)); - assertEquals(endCL2, assoc1.getOtherEnd(endCL1)); - assertEquals(endCL1, assoc2.getOtherEnd(endCL2)); - assertEquals(endCL2, assoc2.getOtherEnd(endCL1)); - assertEquals(endCL1, assoc3.getOtherEnd(endCL3)); - assertEquals(endCL3, assoc3.getOtherEnd(endCL1)); - - assertEquals(null, assoc3.getEndByRoleName("x")); - assertEquals(null, assoc1.getEndByClassifier(cl3)); - - try { - assertEquals(endCL1, assoc1.getOtherEnd(endCL3)); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end unknown in association: 'cl3'", e.getMessage()); - } - } - - - @Test - public void testAssociationCreationInModelWithName() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClassifier cl1 = model.createClass(mcl, "Class1"); - CClassifier cl2 = model.createClass(mcl, "Class2"); - CClassifier cl3 = model.createClass(mcl, "Class3"); - CClassifier cl4 = model.createClass(mcl, "Class4"); - - CAssociation assoc1 = model.createAssociation("a", cl1.createEnd("i", "*"), cl2.createEnd("t", "1")); - CAssociation assoc2 = model.createAssociation("b", cl1.createEnd("o", "*"), cl2.createEnd("s", "1")); - CAssociation assoc3 = model.createComposition("c", cl1.createEnd("a", "0..1"), cl3.createEnd("n", "*")); - CAssociation assoc4 = model.createComposition("d", cl1.createEnd("a", "0..1"), cl3.createEnd("e", "*")); - CAssociation assoc5 = model.createAggregation("e", cl4.createEnd("a", "0..1"), cl3.createEnd("n", "*")); - CAssociation assoc6 = model.createAggregation("f", cl3.createEnd("a", "3"), cl2.createEnd("e", "*")); - - List associations1 = model.getAssociationsByName("a"); - assertEquals(associations1.size(), 1); - List associations2 = model.getAssociationsByName("f"); - assertEquals(1, associations2.size()); - List associations3 = model.getAssociationsByName("x"); - assertEquals(0, associations3.size()); - List associations4 = model.getAssociationsByName(null); - assertEquals(0, associations4.size()); - - assertEquals(assoc1, associations1.get(0)); - assertEquals(assoc6, associations2.get(0)); - - List assoc1Ends = assoc1.getEnds(); - assertEquals("i", assoc1Ends.get(0).getRoleName()); - assertEquals("n", assoc5.getEndByClassifier(cl3).getRoleName()); - assertEquals("s", assoc2.getEndByClassifier(cl2).getRoleName()); - assertEquals("1", assoc1Ends.get(1).getMultiplicityString()); - assertEquals("*", assoc1Ends.get(0).getMultiplicityString()); - assertEquals("0..1", assoc4.getEndByRoleName("a").getMultiplicityString()); - assertEquals("3", assoc6.getEndByRoleName("a").getMultiplicityString()); - assertEquals(true, assoc1Ends.get(0).isNavigable()); - assoc1Ends.get(0).setNavigable(false); - assertEquals(false, assoc1Ends.get(0).isNavigable()); - - assertEquals(false, assoc1.isComposition()); - assertEquals(false, assoc1.isAggregation()); - assertEquals(true, assoc3.isComposition()); - assertEquals(false, assoc3.isAggregation()); - assertEquals(false, assoc5.isComposition()); - assertEquals(true, assoc5.isAggregation()); - - assoc1.setAggregation(true); - assertEquals(false, assoc1.isComposition()); - assertEquals(true, assoc1.isAggregation()); - assoc1.setComposition(true); - assertEquals(true, assoc1.isComposition()); - assertEquals(false, assoc1.isAggregation()); - - assertEquals(assoc1.getModel(), model); - } - - @Test - public void testGetAssociations() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClassifier cl1 = model.createClass(mcl, "Class1"); - CClassifier cl2 = model.createClass(mcl, "Class2"); - CClassifier cl3 = model.createClass(mcl, "Class3"); - CClassifier cl4 = model.createClass(mcl, "Class4"); - - CAssociation assoc1 = model.createAssociation(cl1.createEnd("i", "*"), cl2.createEnd("t", "1")); - CAssociation assoc2 = model.createAssociation(cl1.createEnd("o", "*"), cl2.createEnd("s", "1")); - CAssociation assoc3 = model.createComposition(cl1.createEnd("a", "0..1"), cl3.createEnd("n", "*")); - CAssociation assoc4 = model.createComposition(cl1.createEnd("a", "0..1"), cl3.createEnd("e", "*")); - CAssociation assoc5 = model.createAggregation(cl4.createEnd("a", "0..1"), cl3.createEnd("n", "*")); - CAssociation assoc6 = model.createAggregation(cl3.createEnd("a", "3"), cl2.createEnd("e", "*")); - List associations = model.getAssociations(); - - assertTrue(associations.contains(assoc1)); - assertTrue(associations.contains(assoc2)); - assertTrue(associations.contains(assoc3)); - assertTrue(associations.contains(assoc4)); - assertTrue(associations.contains(assoc5)); - assertTrue(associations.contains(assoc6)); - assertEquals(assoc1.getModel(), model); - - CModel model2 = CodeableModels.createModel(); - CAssociation assoc7 = model2.createAssociation(cl1.createEnd("i", "*"), cl2.createEnd("t", "1")); - assertTrue(!associations.contains(assoc7)); - assertEquals(assoc7.getModel(), model2); - } - - @Test - public void testGetAssociationsEmptyModel() throws CException { - CModel model = CodeableModels.createModel(); - List associations = model.getAssociations(); - assertEquals(0, associations.size()); - } - - @Test - public void testDeleteAssociations() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClassifier cl1 = model.createClass(mcl, "Class1"); - CClassifier cl2 = model.createClass(mcl, "Class2"); - CClassifier cl3 = model.createClass(mcl, "Class3"); - CClassifier cl4 = model.createClass(mcl, "Class4"); - - model.createAssociation("a", cl1.createEnd("i", "*"), cl2.createEnd("t", "1")); - CAssociation assoc2 = model.createAssociation("b", cl1.createEnd("o", "*"), cl2.createEnd("s", "1")); - model.createComposition("c", cl1.createEnd("a", "0..1"), cl3.createEnd("n", "*")); - CAssociation assoc4 = model.createComposition(cl1.createEnd("a", "0..1"), cl3.createEnd("e", "*")); - model.createAggregation(cl4.createEnd("a", "0..1"), cl3.createEnd("n", "*")); - model.createAggregation(cl3.createEnd("a", "3"), cl2.createEnd("e", "*")); - - List associations = model.getAssociations(); - assertEquals(6, associations.size()); - List associations4 = model.getAssociationsByName(null); - assertEquals(3, associations4.size()); - - model.deleteAssociation(assoc2); - model.deleteAssociation(assoc4); - - associations = model.getAssociations(); - assertEquals(4, associations.size()); - - assertEquals(null, assoc2.getModel()); - - List associations1 = model.getAssociationsByName("a"); - assertEquals(1, associations1.size()); - List associations2 = model.getAssociationsByName("b"); - assertEquals(0, associations2.size()); - List associations3 = model.getAssociationsByName("c"); - assertEquals(1, associations3.size()); - associations4 = model.getAssociationsByName(null); - assertEquals(2, associations4.size()); - } - - private int countContains(List list, Object o) { - int count = 0; - for (Object l : list) { - if (l == o) - count++; - } - return count; - } - - - @Test - public void testGetAssociationsFromClass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClassifier cl1 = model.createClass(mcl, "Class1"); - CClassifier cl2 = model.createClass(mcl, "Class2"); - CClassifier cl3 = model.createClass(mcl, "Class3"); - CClassifier cl4 = model.createClass(mcl, "Class4"); - CClassifier cl5 = model.createClass(mcl, "Class5"); - - - CAssociation assoc1 = model.createAssociation(cl1.createEnd("i", "*"), cl2.createEnd("t", "1")); - CAssociation assoc2 = model.createAssociation(cl1.createEnd("o", "*"), cl2.createEnd("s", "1")); - CAssociation assoc3 = model.createComposition(cl1.createEnd("a", "0..1"), cl3.createEnd("n", "*")); - CAssociation assoc4 = model.createComposition(cl1.createEnd("a", "0..1"), cl3.createEnd("e", "*")); - CAssociation assoc5 = model.createComposition(cl1.createEnd("a", "0..1"), cl1.createEnd("e", "*")); - model.createAggregation(cl4.createEnd("a", "0..1"), cl3.createEnd("n", "*")); - model.createAggregation(cl3.createEnd("a", "3"), cl2.createEnd("e", "*")); - - List associations1 = cl1.getAssociations(); - List associations2 = cl2.getAssociations(); - List associations3 = cl3.getAssociations(); - List associations4 = cl4.getAssociations(); - List associations5 = cl5.getAssociations(); - - assertEquals(6, associations1.size()); - assertEquals(3, associations2.size()); - assertEquals(4, associations3.size()); - assertEquals(1, associations4.size()); - assertEquals(0, associations5.size()); - - assertTrue(associations1.contains(assoc1)); - assertTrue(associations1.contains(assoc2)); - assertTrue(associations1.contains(assoc3)); - assertTrue(associations1.contains(assoc4)); - // a self referencing associations should be twice in the class' assoc list - assertEquals(2, countContains(associations1, assoc5)); - } - - @Test - public void testGetAssociationsFromClassDeleteAssociations() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClassifier cl1 = model.createClass(mcl, "Class1"); - CClassifier cl2 = model.createClass(mcl, "Class2"); - CClassifier cl3 = model.createClass(mcl, "Class3"); - CClassifier cl4 = model.createClass(mcl, "Class4"); - CClassifier cl5 = model.createClass(mcl, "Class5"); - - CAssociation assoc1 = model.createAssociation(cl1.createEnd("i", "*"), cl2.createEnd("t", "1")); - CAssociation assoc2 = model.createAssociation(cl1.createEnd("o", "*"), cl2.createEnd("s", "1")); - CAssociation assoc3 = model.createComposition(cl1.createEnd("a", "0..1"), cl3.createEnd("n", "*")); - CAssociation assoc4 = model.createComposition(cl1.createEnd("a", "0..1"), cl3.createEnd("e", "*")); - CAssociation assoc5 = model.createComposition(cl1.createEnd("a", "0..1"), cl1.createEnd("e", "*")); - model.createAggregation(cl4.createEnd("a", "0..1"), cl3.createEnd("n", "*")); - model.createAggregation(cl3.createEnd("a", "3"), cl2.createEnd("e", "*")); - - model.deleteAssociation(assoc1); - model.deleteAssociation(assoc3); - model.deleteAssociation(assoc5); - - List associations1 = cl1.getAssociations(); - List associations2 = cl2.getAssociations(); - List associations3 = cl3.getAssociations(); - List associations4 = cl4.getAssociations(); - List associations5 = cl5.getAssociations(); - - assertEquals(2, associations1.size()); - assertEquals(2, associations2.size()); - assertEquals(3, associations3.size()); - assertEquals(1, associations4.size()); - assertEquals(0, associations5.size()); - - assertTrue(!associations1.contains(assoc1)); - assertTrue(associations1.contains(assoc2)); - assertTrue(!associations1.contains(assoc3)); - assertTrue(associations1.contains(assoc4)); - assertTrue(!associations1.contains(assoc5)); - } - - @Test - public void testDeleteClassGetAssociations() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClassifier cl1 = model.createClass(mcl, "Class1"); - CClassifier cl2 = model.createClass(mcl, "Class2"); - CClassifier cl3 = model.createClass(mcl, "Class3"); - CClassifier cl4 = model.createClass(mcl, "Class4"); - CClassifier cl5 = model.createClass(mcl, "Class5"); - - - CAssociation assoc1 = model.createAssociation(cl1.createEnd("i", "*"), cl2.createEnd("t", "1")); - CAssociation assoc2 = model.createAssociation(cl1.createEnd("o", "*"), cl2.createEnd("s", "1")); - CAssociation assoc3 = model.createComposition(cl1.createEnd("a", "0..1"), cl3.createEnd("n", "*")); - CAssociation assoc4 = model.createComposition(cl1.createEnd("a", "0..1"), cl3.createEnd("e", "*")); - CAssociation assoc5 = model.createComposition(cl1.createEnd("a", "0..1"), cl1.createEnd("e", "*")); - CAssociation assoc6 = model.createAggregation(cl4.createEnd("a", "0..1"), cl3.createEnd("n", "*")); - CAssociation assoc7 = model.createAggregation(cl3.createEnd("a", "3"), cl2.createEnd("e", "*")); - - model.deleteClassifier(cl1); - - List associations1 = cl1.getAssociations(); - List associations2 = cl2.getAssociations(); - List associations3 = cl3.getAssociations(); - List associations4 = cl4.getAssociations(); - List associations5 = cl5.getAssociations(); - - assertEquals(0, associations1.size()); - assertEquals(1, associations2.size()); - assertEquals(2, associations3.size()); - assertEquals(1, associations4.size()); - assertEquals(0, associations5.size()); - - List associationsM = model.getAssociations(); - assertEquals(2, associationsM.size()); - - assertTrue(!associationsM.contains(assoc1)); - assertTrue(!associationsM.contains(assoc2)); - assertTrue(!associationsM.contains(assoc3)); - assertTrue(!associationsM.contains(assoc4)); - assertTrue(!associationsM.contains(assoc5)); - assertTrue(associationsM.contains(assoc6)); - assertTrue(associationsM.contains(assoc7)); - } - - - @Test - public void testCreateAndDeleteAssociationsMetaclass() throws CException { - CModel model = CodeableModels.createModel(); - CClassifier mcl1 = model.createMetaclass("MClass1"); - CClassifier mcl2 = model.createMetaclass("MClass2"); - CClassifier mcl3 = model.createMetaclass("MClass3"); - CClassifier mcl4 = model.createMetaclass("MClass4"); - CClassifier mcl5 = model.createMetaclass("MClass5"); - - - CAssociation assoc1 = model.createAssociation(mcl1.createEnd("i", "*"), mcl2.createEnd("t", "1")); - CAssociation assoc2 = model.createAssociation(mcl1.createEnd("o", "*"), mcl2.createEnd("s", "1")); - CAssociation assoc3 = model.createComposition(mcl1.createEnd("a", "0..1"), mcl3.createEnd("n", "*")); - CAssociation assoc4 = model.createComposition(mcl1.createEnd("a", "0..1"), mcl3.createEnd("e", "*")); - CAssociation assoc5 = model.createComposition(mcl1.createEnd("a", "0..1"), mcl1.createEnd("e", "*")); - CAssociation assoc6 = model.createAggregation(mcl4.createEnd("a", "0..1"), mcl3.createEnd("n", "*")); - CAssociation assoc7 = model.createAggregation(mcl3.createEnd("a", "3"), mcl2.createEnd("e", "*")); - - model.deleteClassifier(mcl1); - - List associations1 = mcl1.getAssociations(); - List associations2 = mcl2.getAssociations(); - List associations3 = mcl3.getAssociations(); - List associations4 = mcl4.getAssociations(); - List associations5 = mcl5.getAssociations(); - - assertEquals(0, associations1.size()); - assertEquals(1, associations2.size()); - assertEquals(2, associations3.size()); - assertEquals(1, associations4.size()); - assertEquals(0, associations5.size()); - - List associationsM = model.getAssociations(); - assertEquals(2, associationsM.size()); - - assertTrue(!associationsM.contains(assoc1)); - assertTrue(!associationsM.contains(assoc2)); - assertTrue(!associationsM.contains(assoc3)); - assertTrue(!associationsM.contains(assoc4)); - assertTrue(!associationsM.contains(assoc5)); - assertTrue(associationsM.contains(assoc6)); - assertTrue(associationsM.contains(assoc7)); - } - - @Test - public void testCreateAndDeleteAssociationsStereotype() throws CException { - CModel model = CodeableModels.createModel(); - CClassifier stereotype1 = model.createStereotype("Stereotype1"); - CClassifier stereotype2 = model.createStereotype("Stereotype2"); - CClassifier stereotype3 = model.createStereotype("Stereotype3"); - CClassifier stereotype4 = model.createStereotype("Stereotype4"); - CClassifier stereotype5 = model.createStereotype("Stereotype5"); - - - CAssociation assoc1 = model.createAssociation(stereotype1.createEnd("i", "*"), stereotype2.createEnd("t", "1")); - CAssociation assoc2 = model.createAssociation(stereotype1.createEnd("o", "*"), stereotype2.createEnd("s", "1")); - CAssociation assoc3 = model.createComposition(stereotype1.createEnd("a", "0..1"), stereotype3.createEnd("n", "*")); - CAssociation assoc4 = model.createComposition(stereotype1.createEnd("a", "0..1"), stereotype3.createEnd("e", "*")); - CAssociation assoc5 = model.createComposition(stereotype1.createEnd("a", "0..1"), stereotype1.createEnd("e", "*")); - CAssociation assoc6 = model.createAggregation(stereotype4.createEnd("a", "0..1"), stereotype3.createEnd("n", "*")); - CAssociation assoc7 = model.createAggregation(stereotype3.createEnd("a", "3"), stereotype2.createEnd("e", "*")); - - model.deleteClassifier(stereotype1); - - List associations1 = stereotype1.getAssociations(); - List associations2 = stereotype2.getAssociations(); - List associations3 = stereotype3.getAssociations(); - List associations4 = stereotype4.getAssociations(); - List associations5 = stereotype5.getAssociations(); - - assertEquals(0, associations1.size()); - assertEquals(1, associations2.size()); - assertEquals(2, associations3.size()); - assertEquals(1, associations4.size()); - assertEquals(0, associations5.size()); - - List associationsM = model.getAssociations(); - assertEquals(2, associationsM.size()); - - assertTrue(!associationsM.contains(assoc1)); - assertTrue(!associationsM.contains(assoc2)); - assertTrue(!associationsM.contains(assoc3)); - assertTrue(!associationsM.contains(assoc4)); - assertTrue(!associationsM.contains(assoc5)); - assertTrue(associationsM.contains(assoc6)); - assertTrue(associationsM.contains(assoc7)); - } - - - @Test - public void testAssociationEndGetMultiplicity() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClassifier cl1 = model.createClass(mcl, "Class1"); - CClassifier cl2 = model.createClass(mcl, "Class2"); - CAssociation assoc1 = model.createAssociation(cl1.createEnd("i", "*"), cl2.createEnd("t", "1")); - - assertEquals(0, assoc1.getEndByClassifier(cl1).getMultiplicity().getLowerMultiplicity()); - assertEquals(1, assoc1.getEndByClassifier(cl2).getMultiplicity().getLowerMultiplicity()); - assertEquals(CMultiplicity.STAR_MULTIPLICITY, assoc1.getEndByClassifier(cl1).getMultiplicity().getUpperMultiplicity()); - assertEquals(1, assoc1.getEndByClassifier(cl2).getMultiplicity().getUpperMultiplicity()); - } - - @Test - public void testGetAssociationByRoleName() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CClass cl3 = model.createClass(mcl, "Class3"); - - CAssociation assoc1 = model.createAssociation("x", cl1.createEnd("i", "*"), cl2.createEnd("t", "1")); - model.createAssociation("y", cl1.createEnd("o", "*"), cl2.createEnd("s", "1")); - model.createComposition(cl1.createEnd("a", "0..1"), cl3.createEnd("n", "*")); - model.createComposition(cl1.createEnd("a", "0..1"), cl3.createEnd("e", "*")); - - assertEquals(assoc1, cl1.getAssociationByRoleName("t")); - assertEquals(null, cl1.getAssociationByRoleName("i")); - } - - @Test - public void testGetAssociationByName() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CClass cl3 = model.createClass(mcl, "Class3"); - - CAssociation assoc1 = model.createAssociation("x", cl1.createEnd("i", "*"), cl2.createEnd("t", "1")); - model.createAssociation("y", cl1.createEnd("o", "*"), cl2.createEnd("s", "1")); - model.createComposition(cl1.createEnd("a", "0..1"), cl3.createEnd("n", "*")); - model.createComposition(cl1.createEnd("a", "0..1"), cl3.createEnd("e", "*")); - - assertEquals(assoc1, cl1.getAssociationByName("x")); - assertEquals(null, cl1.getAssociationByName("z")); - } - - @Test - public void testGetClassifierAssociationByRoleName() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CClass cl1Sub = model.createClass(mcl, "Class1Sub").addSuperclass(cl1); - - CAssociation assoc1 = model.createAssociation("x", cl1.createEnd("i", "*"), cl2.createEnd("t", "1")); - CObject obj1 = model.createObject(cl1); - CObject obj1Sub = model.createObject(cl1Sub); - - assertEquals(assoc1, obj1.getClassifier().getAssociationByRoleName("t")); - assertEquals(null, obj1.getClassifier().getAssociationByRoleName("i")); - assertEquals(assoc1, obj1Sub.getClassifier().getAssociationByRoleName("t")); - assertEquals(null, obj1Sub.getClassifier().getAssociationByRoleName("i")); - } - - @Test - public void testGetClassifierAssociationByName() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CClass cl1Sub = model.createClass(mcl, "Class1Sub").addSuperclass(cl1); - - CAssociation assoc1 = model.createAssociation("x", cl1.createEnd("i", "*"), cl2.createEnd("t", "1")); - CObject obj1 = model.createObject(cl1); - CObject obj1Sub = model.createObject(cl1Sub); - - assertEquals(assoc1, obj1.getClassifier().getAssociationByName("x")); - assertEquals(null, obj1.getClassifier().getAssociationByName("z")); - assertEquals(assoc1, obj1Sub.getClassifier().getAssociationByName("x")); - assertEquals(null, obj1Sub.getClassifier().getAssociationByName("z")); - } - - -} diff --git a/src/codeableModels/tests/TestsAttributeValues.java b/src/codeableModels/tests/TestsAttributeValues.java deleted file mode 100644 index cffb37f..0000000 --- a/src/codeableModels/tests/TestsAttributeValues.java +++ /dev/null @@ -1,688 +0,0 @@ -package codeableModels.tests; - -import codeableModels.*; -import org.junit.*; - -import java.util.*; - -import static org.junit.Assert.*; - -public class TestsAttributeValues { - - @Test - public void testValuesOnPrimitiveTypeAttributes() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl = model.createClass(mcl, "C"); - cl.addAttribute("isBoolean", true); - cl.addAttribute("intVal", 1); - cl.addAttribute("longVal", (long) 100); - cl.addAttribute("doubleVal", 1.1); - cl.addAttribute("floatVal", (float) 1.1); - cl.addAttribute("string", "abc"); - cl.addAttribute("char", 'a'); - cl.addAttribute("byte", (byte) 1); - cl.addAttribute("short", (short) 2); - - CObject obj = model.createObject(cl, "o"); - - assertEquals(true, obj.getAttributeValue("isBoolean")); - assertEquals(1, obj.getAttributeValue("intVal")); - assertEquals((long) 100, obj.getAttributeValue("longVal")); - assertEquals(1.1, obj.getAttributeValue("doubleVal")); - assertEquals((float) 1.1, obj.getAttributeValue("floatVal")); - assertEquals("abc", obj.getAttributeValue("string")); - assertEquals('a', obj.getAttributeValue("char")); - assertEquals((byte) 1, obj.getAttributeValue("byte")); - assertEquals((short) 2, obj.getAttributeValue("short")); - - obj.setAttributeValue("isBoolean", false); - obj.setAttributeValue("intVal", 10); - obj.setAttributeValue("longVal", (long) 1000); - obj.setAttributeValue("doubleVal", 100.1); - obj.setAttributeValue("floatVal", (float) 102.1); - obj.setAttributeValue("string", ""); - obj.setAttributeValue("char", 'x'); - obj.setAttributeValue("byte", (byte) 15); - obj.setAttributeValue("short", (short) 12); - - assertEquals(false, obj.getAttributeValue("isBoolean")); - assertEquals(10, obj.getAttributeValue("intVal")); - assertEquals((long) 1000, obj.getAttributeValue("longVal")); - assertEquals(100.1, obj.getAttributeValue("doubleVal")); - assertEquals((float) 102.1, obj.getAttributeValue("floatVal")); - assertEquals("", obj.getAttributeValue("string")); - assertEquals('x', obj.getAttributeValue("char")); - assertEquals((byte) 15, obj.getAttributeValue("byte")); - assertEquals((short) 12, obj.getAttributeValue("short")); - } - - @Test - public void testAttributeOfValueUnknown() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl = model.createClass(mcl, "C"); - CObject obj = model.createObject(cl, "o"); - - try { - obj.getAttributeValue("x"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("attribute 'x' unknown for object 'o'", e.getMessage()); - } - - cl.addAttribute("isBoolean", true); - cl.addAttribute("intVal", 1); - - try { - obj.setAttributeValue("x", 1); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("attribute 'x' unknown for object 'o'", e.getMessage()); - } - } - - @Test - public void testObjectTypeAttributeValues() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass attrType = model.createClass(mcl, "AttrType"); - CObject attrValue = model.createObject(attrType, "attrValue"); - - CClass cl = model.createClass(mcl, "C"); - - cl.addAttribute("attrTypeObj", attrValue); - CAttribute attribute = cl.getAttribute("attrTypeObj"); - - CObject obj = model.createObject(cl, "o"); - - assertEquals(attrValue, obj.getAttributeValue("attrTypeObj")); - assertEquals("attrValue", ((CObject) obj.getAttributeValue("attrTypeObj")).getName()); - - CObject attrValue2 = model.createObject(attrType, "attrValue2"); - obj.setAttributeValue("attrTypeObj", attrValue2); - - assertEquals(attrValue2, obj.getAttributeValue("attrTypeObj")); - assertEquals("attrValue2", ((CObject) obj.getAttributeValue("attrTypeObj")).getName()); - - CObject nonAttrValue = model.createObject(cl, "nonAttrValue"); - - try { - obj.setAttributeValue("attrTypeObj", nonAttrValue); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("type classifier of object 'nonAttrValue' is not matching attribute type of attribute " + - "'attrTypeObj'", e.getMessage()); - } - - assertEquals(attrType, attribute.getTypeClassifier()); - } - - @Test - public void testAddObjectAttributeGetSetValue() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass attrType = model.createClass(mcl, "AttrType"); - CObject attrValue = model.createObject(attrType, "attrValue"); - CClass cl = model.createClass(mcl, "C"); - CClass cl2 = model.createClass(mcl, "C2"); - CObject obj = model.createObject(cl, "o"); - CObject obj2 = model.createObject(cl, "o2"); - - cl.addObjectAttribute("attrTypeObj", attrType).addObjectAttribute("attrTypeObj2", "AttrType"); - // test the super-interface on CClassifier method directly for once - ((CClassifier) cl2).addObjectAttribute("attrTypeObj", attrType).addObjectAttribute("attrTypeObj2", "AttrType"); - - assertEquals(null, obj.getAttributeValue("attrTypeObj")); - assertEquals(null, obj2.getAttributeValue("attrTypeObj")); - - obj.setAttributeValue("attrTypeObj", attrValue); - obj2.setAttributeValue("attrTypeObj", attrValue); - assertEquals(attrValue, obj.getAttributeValue("attrTypeObj")); - assertEquals(attrValue, obj2.getAttributeValue("attrTypeObj")); - } - - @Test - public void testAddObjectAttributeGetSetValueWithStringClassifier() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass attrType = model.createClass(mcl, "AttrType"); - CObject attrValue = model.createObject(attrType, "attrValue"); - CClass cl = model.createClass(mcl, "C"); - CObject obj = model.createObject(cl, "o"); - - cl.addObjectAttribute("attrTypeObj", "AttrType"); - assertEquals(null, obj.getAttributeValue("attrTypeObj")); - obj.setAttributeValue("attrTypeObj", attrValue); - assertEquals(attrValue, obj.getAttributeValue("attrTypeObj")); - } - - @Test - public void testValuesOnAttributesWithNoDefaultValues() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl = model.createClass(mcl, "C"); - cl.addBooleanAttribute("isBoolean"); - cl.addIntAttribute("intVal"); - cl.addLongAttribute("longVal"); - cl.addDoubleAttribute("doubleVal"); - cl.addFloatAttribute("floatVal"); - cl.addStringAttribute("string"); - cl.addCharAttribute("char"); - cl.addByteAttribute("byte"); - cl.addShortAttribute("short"); - CClass attrClass = model.createClass(mcl, "attrClass"); - cl.addObjectAttribute("obj1", attrClass); - cl.addObjectAttribute("obj2", "attrClass"); - CEnum enumType = model.createEnum("XYEnum", Arrays.asList("X", "Y")); - cl.addEnumAttribute("enum", enumType); - - CObject obj = model.createObject(cl, "o"); - - assertEquals(null, obj.getAttributeValue("isBoolean")); - assertEquals(null, obj.getAttributeValue("intVal")); - assertEquals(null, obj.getAttributeValue("longVal")); - assertEquals(null, obj.getAttributeValue("doubleVal")); - assertEquals(null, obj.getAttributeValue("floatVal")); - assertEquals(null, obj.getAttributeValue("string")); - assertEquals(null, obj.getAttributeValue("char")); - assertEquals(null, obj.getAttributeValue("byte")); - assertEquals(null, obj.getAttributeValue("short")); - assertEquals(null, obj.getAttributeValue("obj1")); - assertEquals(null, obj.getAttributeValue("obj2")); - assertEquals(null, obj.getAttributeValue("enum")); - } - - - @Test - public void testEnumTypeAttributeValues() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - - List enumValues = Arrays.asList("A", "B", "C"); - CEnum enumObj = model.createEnum("ABCEnum", enumValues); - - CClass cl = model.createClass(mcl, "C"); - assertEquals(0, cl.getAttributes().size()); - cl.addEnumAttribute("l1", enumObj).setAttributeDefaultValue("l1", "A"); - cl.addEnumAttribute("l2", enumObj); - CObject obj = model.createObject(cl, "o"); - - assertEquals("A", obj.getAttributeValue("l1")); - assertEquals(null, obj.getAttributeValue("l2")); - - obj.setAttributeValue("l1", "B"); - obj.setAttributeValue("l2", "C"); - - assertEquals("B", obj.getAttributeValue("l1")); - assertEquals("C", obj.getAttributeValue("l2")); - - try { - obj.setAttributeValue("l1", "X"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value 'X' not element of enumeration", e.getMessage()); - } - } - - @Test - public void testAttributeTypeCheckForObjAttrValues() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl = model.createClass(mcl, "C"); - cl.addBooleanAttribute("a"); - CObject obj = model.createObject(cl, "o"); - try { - obj.setAttributeValue("a", CodeableModels.createModel()); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value for attribute 'a' is not a known attribute type", e.getMessage()); - } - try { - obj.setAttributeValue("a", 1); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'a' does not match attribute type", e.getMessage()); - } - - cl.addIntAttribute("i"); - try { - obj.setAttributeValue("i", Boolean.TRUE); - } catch (CException e) { - assertEquals("value type for attribute 'i' does not match attribute type", e.getMessage()); - } - - cl.addShortAttribute("s"); - try { - obj.setAttributeValue("s", Boolean.TRUE); - } catch (CException e) { - assertEquals("value type for attribute 's' does not match attribute type", e.getMessage()); - } - - cl.addByteAttribute("b"); - try { - obj.setAttributeValue("b", Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'b' does not match attribute type", e.getMessage()); - } - - cl.addLongAttribute("l"); - try { - obj.setAttributeValue("l", Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'l' does not match attribute type", e.getMessage()); - } - - cl.addFloatAttribute("f"); - try { - obj.setAttributeValue("f", Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'f' does not match attribute type", e.getMessage()); - } - - cl.addDoubleAttribute("d"); - try { - obj.setAttributeValue("d", Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'd' does not match attribute type", e.getMessage()); - } - - CEnum enumType = model.createEnum("XYEnum", Arrays.asList("X", "Y")); - cl.addEnumAttribute("e", enumType); - try { - obj.setAttributeValue("e", Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'e' does not match attribute type", e.getMessage()); - } - - CClass attrClass = model.createClass(mcl, "AttrClass"); - CClass otherClass = model.createClass(mcl, "OtherClass"); - cl.addObjectAttribute("o", attrClass); - try { - obj.setAttributeValue("o", Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'o' does not match attribute type", e.getMessage()); - } - - try { - obj.setAttributeValue("o", otherClass); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("type classifier of object 'OtherClass' is not matching attribute type of attribute 'o'", e - .getMessage()); - } - - } - - @Test - public void testDeleteAttributesSetValue() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl = model.createClass(mcl, "C"); - CObject obj = model.createObject(cl, "o"); - cl.addAttribute("isBoolean", true); - cl.addAttribute("intVal", 1); - - obj.setAttributeValue("isBoolean", true); - - cl.deleteAttribute("isBoolean"); - - try { - obj.getAttributeValue("isBoolean"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("attribute 'isBoolean' unknown for object 'o'", e.getMessage()); - } - - obj.setAttributeValue("intVal", 1); - - try { - obj.setAttributeValue("isBoolean", false); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("attribute 'isBoolean' unknown for object 'o'", e.getMessage()); - } - } - - @Test - public void testDeleteAttributesSetValueOnClassifier() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl = model.createClass(mcl, "C"); - CObject obj = model.createObject(cl, "o"); - cl.addAttribute("isBoolean", true); - cl.addAttribute("intVal", 1); - - obj.setAttributeValue("isBoolean", true); - - ((CClassifier) cl).deleteAttribute("isBoolean"); - - try { - obj.getAttributeValue("isBoolean"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("attribute 'isBoolean' unknown for object 'o'", e.getMessage()); - } - - obj.setAttributeValue("intVal", 1); - - try { - obj.setAttributeValue("isBoolean", false); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("attribute 'isBoolean' unknown for object 'o'", e.getMessage()); - } - } - - @Test - public void testAttributeValuesInheritance() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass top1 = model.createClass(mcl, "Top1"); - CClass top2 = model.createClass(mcl, "Top2"); - CClass classA = model.createClass(mcl, "ClassA").addSuperclass("Top1").addSuperclass("Top2"); - CClass subclassA = model.createClass(mcl, "SubA").addSuperclass("ClassA"); - - top1.addAttribute("int0", 0); - top2.addAttribute("int1", 1); - classA.addAttribute("int2", 2); - subclassA.addAttribute("int3", 3); - - CObject obj = model.createObject(subclassA, "o"); - - assertEquals(0, obj.getAttributeValue("int0")); - assertEquals(1, obj.getAttributeValue("int1")); - assertEquals(2, obj.getAttributeValue("int2")); - assertEquals(3, obj.getAttributeValue("int3")); - - assertEquals(0, obj.getAttributeValue(top1,"int0")); - assertEquals(1, obj.getAttributeValue(top2, "int1")); - assertEquals(2, obj.getAttributeValue(classA, "int2")); - assertEquals(3, obj.getAttributeValue(subclassA, "int3")); - - obj.setAttributeValue("int0", 10); - obj.setAttributeValue("int1", 11); - obj.setAttributeValue("int2", 12); - obj.setAttributeValue("int3", 13); - - assertEquals(10, obj.getAttributeValue("int0")); - assertEquals(11, obj.getAttributeValue("int1")); - assertEquals(12, obj.getAttributeValue("int2")); - assertEquals(13, obj.getAttributeValue("int3")); - - assertEquals(10, obj.getAttributeValue(top1,"int0")); - assertEquals(11, obj.getAttributeValue(top2, "int1")); - assertEquals(12, obj.getAttributeValue(classA, "int2")); - assertEquals(13, obj.getAttributeValue(subclassA, "int3")); - - obj.setAttributeValue(top1,"int0", 100); - obj.setAttributeValue(top2,"int1", 110); - obj.setAttributeValue(classA,"int2", 120); - obj.setAttributeValue(subclassA,"int3", 130); - - assertEquals(100, obj.getAttributeValue("int0")); - assertEquals(110, obj.getAttributeValue("int1")); - assertEquals(120, obj.getAttributeValue("int2")); - assertEquals(130, obj.getAttributeValue("int3")); - - assertEquals(100, obj.getAttributeValue(top1,"int0")); - assertEquals(110, obj.getAttributeValue(top2, "int1")); - assertEquals(120, obj.getAttributeValue(classA, "int2")); - assertEquals(130, obj.getAttributeValue(subclassA, "int3")); - } - - @Test - public void testAttributeValuesInheritanceAfterDeleteSuperclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass top1 = model.createClass(mcl, "Top1"); - CClass top2 = model.createClass(mcl, "Top2"); - CClass classA = model.createClass(mcl, "ClassA").addSuperclass("Top1").addSuperclass("Top2"); - CClass subclassA = model.createClass(mcl, "SubA").addSuperclass("ClassA"); - - top1.addAttribute("int0", 0); - top2.addAttribute("int1", 1); - classA.addAttribute("int2", 2); - subclassA.addAttribute("int3", 3); - - CObject obj = model.createObject(subclassA, "o"); - - ((CClassifier) classA).deleteSuperclass("Top2"); - - assertEquals(0, obj.getAttributeValue("int0")); - try { - obj.getAttributeValue("int1"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("attribute 'int1' unknown for object 'o'", e.getMessage()); - } - assertEquals(2, obj.getAttributeValue("int2")); - assertEquals(3, obj.getAttributeValue("int3")); - - obj.setAttributeValue("int0", 10); - try { - obj.setAttributeValue("int1", 11); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("attribute 'int1' unknown for object 'o'", e.getMessage()); - } - } - - - @Test - public void testAttributeValuesSameNameInheritance() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass top1 = model.createClass(mcl, "Top1"); - CClass top2 = model.createClass(mcl, "Top2"); - CClass classA = model.createClass(mcl, "ClassA").addSuperclass("Top1").addSuperclass("Top2"); - CClass subclassA = model.createClass(mcl, "SubA").addSuperclass("ClassA"); - - top1.addAttribute("int", 0); - top2.addAttribute("int", 1); - classA.addAttribute("int", 2); - subclassA.addAttribute("int", 3); - - CObject obj1 = model.createObject(subclassA, "o1"); - CObject obj2 = model.createObject(classA, "o2"); - CObject obj3 = model.createObject(top1, "o3"); - - assertEquals(3, obj1.getAttributeValue("int")); - assertEquals(2, obj2.getAttributeValue("int")); - assertEquals(0, obj3.getAttributeValue("int")); - - assertEquals(3, obj1.getAttributeValue(subclassA, "int")); - assertEquals(2, obj1.getAttributeValue(classA, "int")); - assertEquals(1, obj1.getAttributeValue(top2, "int")); - assertEquals(0, obj1.getAttributeValue(top1, "int")); - assertEquals(2, obj2.getAttributeValue(classA, "int")); - assertEquals(1, obj2.getAttributeValue(top2, "int")); - assertEquals(0, obj2.getAttributeValue(top1, "int")); - assertEquals(0, obj3.getAttributeValue(top1,"int")); - - obj1.setAttributeValue("int", 10); - obj2.setAttributeValue("int", 11); - obj3.setAttributeValue("int", 12); - - assertEquals(10, obj1.getAttributeValue("int")); - assertEquals(11, obj2.getAttributeValue("int")); - assertEquals(12, obj3.getAttributeValue("int")); - - assertEquals(10, obj1.getAttributeValue(subclassA, "int")); - assertEquals(2, obj1.getAttributeValue(classA, "int")); - assertEquals(1, obj1.getAttributeValue(top2, "int")); - assertEquals(0, obj1.getAttributeValue(top1, "int")); - assertEquals(11, obj2.getAttributeValue(classA, "int")); - assertEquals(1, obj2.getAttributeValue(top2, "int")); - assertEquals(0, obj2.getAttributeValue(top1, "int")); - assertEquals(12, obj3.getAttributeValue(top1,"int")); - - obj1.setAttributeValue(subclassA,"int", 130); - obj1.setAttributeValue(top1,"int", 100); - obj1.setAttributeValue(top2,"int", 110); - obj1.setAttributeValue(classA,"int", 120); - - assertEquals(130, obj1.getAttributeValue("int")); - - assertEquals(100, obj1.getAttributeValue(top1,"int")); - assertEquals(110, obj1.getAttributeValue(top2, "int")); - assertEquals(120, obj1.getAttributeValue(classA, "int")); - assertEquals(130, obj1.getAttributeValue(subclassA, "int")); - } - - - @Test - public void testCreateAndDeleteAttributeValuesMetaclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl = model.createClass(mcl, "C"); - mcl.addAttribute("isBoolean", true); - mcl.addAttribute("intVal", 1); - - cl.setAttributeValue("isBoolean", true); - cl.setAttributeValue("intVal", 1); - - assertEquals(true, cl.getAttributeValue("isBoolean")); - assertEquals(1, cl.getAttributeValue("intVal")); - - mcl.deleteAttribute("isBoolean"); - - assertEquals(1, cl.getAttributeValue("intVal")); - cl.setAttributeValue("intVal", 100); - assertEquals(100, cl.getAttributeValue("intVal")); - - try { - cl.setAttributeValue("isBoolean", false); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("attribute 'isBoolean' unknown for object 'C'", e.getMessage()); - } - } - - - @Test - public void testAttributeValuesInheritanceMetaclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass top1 = model.createMetaclass( "Top1"); - CMetaclass top2 = model.createMetaclass("Top2"); - CMetaclass classA = model.createMetaclass( "ClassA").addSuperclass("Top1").addSuperclass("Top2"); - CMetaclass subclassA = model.createMetaclass( "SubA").addSuperclass("ClassA"); - - top1.addAttribute("int0", 0); - top2.addAttribute("int1", 1); - classA.addAttribute("int2", 2); - subclassA.addAttribute("int3", 3); - - CClass cl = model.createClass(subclassA, "CL"); - - assertEquals(0, cl.getAttributeValue("int0")); - assertEquals(1, cl.getAttributeValue("int1")); - assertEquals(2, cl.getAttributeValue("int2")); - assertEquals(3, cl.getAttributeValue("int3")); - - assertEquals(0, cl.getAttributeValue(top1,"int0")); - assertEquals(1, cl.getAttributeValue(top2, "int1")); - assertEquals(2, cl.getAttributeValue(classA, "int2")); - assertEquals(3, cl.getAttributeValue(subclassA, "int3")); - - cl.setAttributeValue("int0", 10); - cl.setAttributeValue("int1", 11); - cl.setAttributeValue("int2", 12); - cl.setAttributeValue("int3", 13); - - assertEquals(10, cl.getAttributeValue("int0")); - assertEquals(11, cl.getAttributeValue("int1")); - assertEquals(12, cl.getAttributeValue("int2")); - assertEquals(13, cl.getAttributeValue("int3")); - - assertEquals(10, cl.getAttributeValue(top1,"int0")); - assertEquals(11, cl.getAttributeValue(top2, "int1")); - assertEquals(12, cl.getAttributeValue(classA, "int2")); - assertEquals(13, cl.getAttributeValue(subclassA, "int3")); - - cl.setAttributeValue(top1,"int0", 100); - cl.setAttributeValue(top2,"int1", 110); - cl.setAttributeValue(classA,"int2", 120); - cl.setAttributeValue(subclassA,"int3", 130); - - assertEquals(100, cl.getAttributeValue("int0")); - assertEquals(110, cl.getAttributeValue("int1")); - assertEquals(120, cl.getAttributeValue("int2")); - assertEquals(130, cl.getAttributeValue("int3")); - - assertEquals(100, cl.getAttributeValue(top1,"int0")); - assertEquals(110, cl.getAttributeValue(top2, "int1")); - assertEquals(120, cl.getAttributeValue(classA, "int2")); - assertEquals(130, cl.getAttributeValue(subclassA, "int3")); - } - - @Test - public void testAttributeValuesSameNameInheritanceMetaclass() throws CException { - CModel model = CodeableModels.createModel(); - model.createMetaclass("MCL"); - CMetaclass top1 = model.createMetaclass( "Top1"); - CMetaclass top2 = model.createMetaclass("Top2"); - CMetaclass classA = model.createMetaclass( "ClassA").addSuperclass("Top1").addSuperclass("Top2"); - CMetaclass subclassA = model.createMetaclass( "SubA").addSuperclass("ClassA"); - - top1.addAttribute("int", 0); - top2.addAttribute("int", 1); - classA.addAttribute("int", 2); - subclassA.addAttribute("int", 3); - - CClass cl1 = model.createClass(subclassA, "C1"); - CClass cl2 = model.createClass(classA, "C2"); - CClass cl3 = model.createClass(top1, "C3"); - - assertEquals(3, cl1.getAttributeValue("int")); - assertEquals(2, cl2.getAttributeValue("int")); - assertEquals(0, cl3.getAttributeValue("int")); - - assertEquals(3, cl1.getAttributeValue(subclassA, "int")); - assertEquals(2, cl1.getAttributeValue(classA, "int")); - assertEquals(1, cl1.getAttributeValue(top2, "int")); - assertEquals(0, cl1.getAttributeValue(top1, "int")); - assertEquals(2, cl2.getAttributeValue(classA, "int")); - assertEquals(1, cl2.getAttributeValue(top2, "int")); - assertEquals(0, cl2.getAttributeValue(top1, "int")); - assertEquals(0, cl3.getAttributeValue(top1,"int")); - - cl1.setAttributeValue("int", 10); - cl2.setAttributeValue("int", 11); - cl3.setAttributeValue("int", 12); - - assertEquals(10, cl1.getAttributeValue("int")); - assertEquals(11, cl2.getAttributeValue("int")); - assertEquals(12, cl3.getAttributeValue("int")); - - assertEquals(10, cl1.getAttributeValue(subclassA, "int")); - assertEquals(2, cl1.getAttributeValue(classA, "int")); - assertEquals(1, cl1.getAttributeValue(top2, "int")); - assertEquals(0, cl1.getAttributeValue(top1, "int")); - assertEquals(11, cl2.getAttributeValue(classA, "int")); - assertEquals(1, cl2.getAttributeValue(top2, "int")); - assertEquals(0, cl2.getAttributeValue(top1, "int")); - assertEquals(12, cl3.getAttributeValue(top1,"int")); - - cl1.setAttributeValue(subclassA,"int", 130); - cl1.setAttributeValue(top1,"int", 100); - cl1.setAttributeValue(top2,"int", 110); - cl1.setAttributeValue(classA,"int", 120); - - assertEquals(130, cl1.getAttributeValue("int")); - - assertEquals(100, cl1.getAttributeValue(top1,"int")); - assertEquals(110, cl1.getAttributeValue(top2, "int")); - assertEquals(120, cl1.getAttributeValue(classA, "int")); - assertEquals(130, cl1.getAttributeValue(subclassA, "int")); - } - - - -} diff --git a/src/codeableModels/tests/TestsAttributes.java b/src/codeableModels/tests/TestsAttributes.java deleted file mode 100644 index 66f6b8e..0000000 --- a/src/codeableModels/tests/TestsAttributes.java +++ /dev/null @@ -1,414 +0,0 @@ -package codeableModels.tests; - -import codeableModels.*; -import org.junit.*; - -import java.util.*; - -import static org.junit.Assert.*; - -public class TestsAttributes { - - @Test - public void testPrimitiveTypeAttributes() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClassifier cl = model.createClass(mcl, "C"); - cl.addAttribute("isBoolean", true); - cl.addAttribute("intVal", 1); - cl.addAttribute("longVal", (long) 100); - cl.addAttribute("doubleVal", 1.1); - cl.addAttribute("floatVal", (float) 1.1); - cl.addAttribute("string", "abc"); - cl.addAttribute("char", 'a'); - cl.addAttribute("byte", (byte) 1); - cl.addAttribute("short", (short) 2); - - try { - cl.addAttribute("isBoolean", true); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("attribute 'isBoolean' cannot be created, attribute name already exists", e.getMessage()); - } - - List attributes = cl.getAttributes(); - List attributeNames = cl.getAttributeNames(); - - assertEquals(9, attributes.size()); - assertEquals(9, attributeNames.size()); - - assertTrue(attributeNames.contains("isBoolean")); - assertTrue(attributeNames.contains("intVal")); - assertTrue(attributeNames.contains("longVal")); - assertTrue(attributeNames.contains("doubleVal")); - assertTrue(attributeNames.contains("floatVal")); - assertTrue(attributeNames.contains("string")); - assertTrue(attributeNames.contains("char")); - assertTrue(attributeNames.contains("byte")); - assertTrue(attributeNames.contains("short")); - - CAttribute a0 = cl.getAttribute("X"); - CAttribute a1 = cl.getAttribute("isBoolean"); - CAttribute a2 = cl.getAttribute("intVal"); - CAttribute a3 = cl.getAttribute("longVal"); - CAttribute a4 = cl.getAttribute("doubleVal"); - CAttribute a5 = cl.getAttribute("floatVal"); - CAttribute a6 = cl.getAttribute("string"); - CAttribute a7 = cl.getAttribute("char"); - CAttribute a8 = cl.getAttribute("byte"); - CAttribute a9 = cl.getAttribute("short"); - - assertEquals(null, a0); - - assertTrue(attributes.contains(a1)); - assertTrue(attributes.contains(a2)); - assertTrue(attributes.contains(a3)); - assertTrue(attributes.contains(a4)); - assertTrue(attributes.contains(a5)); - assertTrue(attributes.contains(a6)); - assertTrue(attributes.contains(a7)); - assertTrue(attributes.contains(a8)); - assertTrue(attributes.contains(a9)); - - assertEquals("Boolean", a1.getType()); - assertEquals("Int", a2.getType()); - assertEquals("Long", a3.getType()); - assertEquals("Double", a4.getType()); - assertEquals("Float", a5.getType()); - assertEquals("String", a6.getType()); - assertEquals("Char", a7.getType()); - assertEquals("Byte", a8.getType()); - assertEquals("Short", a9.getType()); - - - Object d1 = a1.getDefaultValue(); - Object d2 = a2.getDefaultValue(); - Object d3 = a3.getDefaultValue(); - Object d4 = a4.getDefaultValue(); - Object d5 = a5.getDefaultValue(); - Object d6 = a6.getDefaultValue(); - Object d7 = a7.getDefaultValue(); - Object d8 = a8.getDefaultValue(); - Object d9 = a9.getDefaultValue(); - - assertTrue(d1 instanceof Boolean); - assertTrue(d2 instanceof Integer); - assertTrue(d3 instanceof Long); - assertTrue(d4 instanceof Double); - assertTrue(d5 instanceof Float); - assertTrue(d6 instanceof String); - assertTrue(d7 instanceof Character); - assertTrue(d8 instanceof Byte); - assertTrue(d9 instanceof Short); - - assertEquals(true, d1); - assertEquals(1, ((Integer) d2).intValue()); - assertEquals(100, ((Long) d3).longValue()); - assertEquals(1.1, (Double) d4, 0.001); - assertEquals(1.1, (Float) d5, 0.001); - assertEquals("abc", d6); - assertEquals('a', ((Character) d7).charValue()); - assertEquals(1, ((Byte) d8).byteValue()); - assertEquals(2, ((Short) d9).shortValue()); - } - - @Test - public void testObjectTypeAttribute() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass attrType = model.createClass(mcl, "AttrType"); - CObject attrValue = model.createObject(attrType, "attrValue"); - - CClassifier cl = model.createClass(mcl, "C"); - - cl.addAttribute("attrTypeObj", attrValue); - CAttribute objAttr = cl.getAttribute("attrTypeObj"); - List attributes = cl.getAttributes(); - - assertEquals(1, attributes.size()); - assertTrue(attributes.contains(objAttr)); - - cl.addAttribute("isBoolean", true); - - attributes = cl.getAttributes(); - - assertEquals(2, attributes.size()); - objAttr = cl.getAttribute("attrTypeObj"); - assertTrue(attributes.contains(objAttr)); - - assertEquals("Object", objAttr.getType()); - - Object d1 = objAttr.getDefaultValue(); - - assertTrue(d1 instanceof CObject); - - CObject value = (CObject) d1; - - assertEquals(attrValue, value); - } - - - @Test - public void testEnumGetValues() throws CException { - CModel model = CodeableModels.createModel(); - List enumValues = Arrays.asList("A", "B", "C"); - CEnum enumObj = model.createEnum("ABCEnum", enumValues); - assertEquals(Arrays.asList("A", "B", "C"), enumObj.getValues()); - } - - @Test - public void testEnumGetName() throws CException { - CModel model = CodeableModels.createModel(); - List enumValues = Arrays.asList("A", "B", "C"); - CEnum enumObj = model.createEnum("ABCEnum", enumValues); - assertEquals("ABCEnum", enumObj.getName()); - } - - @Test - public void testEnumTypeAttribute() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - - List enumValues = Arrays.asList("A", "B", "C"); - CEnum enumObj = model.createEnum("ABCEnum", enumValues); - CClassifier cl = model.createClass(mcl, "C"); - assertEquals(0, cl.getAttributes().size()); - - assertEquals(false, enumObj.isLegalValue("X")); - assertEquals(true, enumObj.isLegalValue("A")); - - cl.addEnumAttribute("letters", enumObj).setAttributeDefaultValue("letters", "A"); - cl.addEnumAttribute("letters2", enumObj); - - CAttribute enumAttr = cl.getAttribute("letters"); - CAttribute enumAttr2 = cl.getAttribute("letters2"); - List attributes = cl.getAttributes(); - - assertEquals(2, attributes.size()); - assertTrue(attributes.contains(enumAttr)); - - cl.addAttribute("isBoolean", true); - - attributes = cl.getAttributes(); - - assertEquals(3, attributes.size()); - enumAttr = cl.getAttribute("letters"); - assertTrue(attributes.contains(enumAttr)); - - assertEquals("Enum", enumAttr.getType()); - - assertEquals(enumObj, enumAttr.getEnumType()); - - Object d1 = enumAttr.getDefaultValue(); - Object d2 = enumAttr2.getDefaultValue(); - - assertEquals("A", d1); - assertEquals(null, d2); - - try { - cl.addEnumAttribute("letters3", enumObj).setAttributeDefaultValue("letters3", "X"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value 'X' not element of enumeration", e.getMessage()); - } - } - - - @Test - public void testSetAttributeDefaultValue() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - - List enumValues = Arrays.asList("A", "B", "C"); - CEnum enumObj = model.createEnum("ABCEnum", enumValues); - CClassifier cl = model.createClass(mcl, "C"); - - cl.addEnumAttribute("letters", enumObj); - cl.addBooleanAttribute("b"); - - try { - cl.setAttributeDefaultValue("x", "A"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("unknown attribute name 'x' on classifier 'C'", e.getMessage()); - } - cl.setAttributeDefaultValue("letters", "A"); - cl.setAttributeDefaultValue("b", true); - - Object d1 = cl.getAttribute("letters").getDefaultValue(); - Object d2 = cl.getAttribute("b").getDefaultValue(); - - assertEquals("A", d1); - assertEquals(true, d2); - - } - - @Test - public void testAttributeTypeCheck() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClassifier cl = model.createClass(mcl, "C"); - cl.addBooleanAttribute("a"); - CAttribute attr = cl.getAttribute("a"); - try { - attr.setDefaultValue(CodeableModels.createModel()); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value for attribute 'a' is not a known attribute type", e.getMessage()); - } - try { - attr.setDefaultValue(1); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'a' does not match attribute type", e.getMessage()); - } - - cl.addIntAttribute("i"); - attr = cl.getAttribute("i"); - try { - attr.setDefaultValue(Boolean.TRUE); - } catch (CException e) { - assertEquals("value type for attribute 'i' does not match attribute type", e.getMessage()); - } - - cl.addShortAttribute("s"); - attr = cl.getAttribute("s"); - try { - attr.setDefaultValue(Boolean.TRUE); - } catch (CException e) { - assertEquals("value type for attribute 's' does not match attribute type", e.getMessage()); - } - - cl.addByteAttribute("b"); - attr = cl.getAttribute("b"); - try { - attr.setDefaultValue(Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'b' does not match attribute type", e.getMessage()); - } - - cl.addLongAttribute("l"); - attr = cl.getAttribute("l"); - try { - attr.setDefaultValue(Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'l' does not match attribute type", e.getMessage()); - } - - cl.addFloatAttribute("f"); - attr = cl.getAttribute("f"); - try { - attr.setDefaultValue(Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'f' does not match attribute type", e.getMessage()); - } - - cl.addDoubleAttribute("d"); - attr = cl.getAttribute("d"); - try { - attr.setDefaultValue(Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'd' does not match attribute type", e.getMessage()); - } - - cl.addStringAttribute("st"); - attr = cl.getAttribute("st"); - try { - attr.setDefaultValue(Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'st' does not match attribute type", e.getMessage()); - } - - cl.addCharAttribute("ch"); - attr = cl.getAttribute("ch"); - try { - attr.setDefaultValue(Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'ch' does not match attribute type", e.getMessage()); - } - - CEnum enumType = model.createEnum("XYEnum", Arrays.asList("X", "Y")); - cl.addEnumAttribute("e", enumType); - attr = cl.getAttribute("e"); - try { - attr.setDefaultValue(Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'e' does not match attribute type", e.getMessage()); - } - - CClass attrClass = model.createClass(mcl, "AttrClass"); - CClass otherClass = model.createClass(mcl, "OtherClass"); - cl.addObjectAttribute("o", attrClass); - attr = cl.getAttribute("o"); - try { - attr.setDefaultValue(Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'o' does not match attribute type", e.getMessage()); - } - - try { - attr.setDefaultValue(otherClass); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("type classifier of object 'OtherClass' is not matching attribute type of attribute 'o'", e.getMessage()); - } - - } - - @Test - public void testDeleteAttributes() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl = model.createClass(mcl, "C"); - cl.addAttribute("isBoolean", true); - cl.addAttribute("intVal", 1); - cl.addAttribute("longVal", (long) 100); - cl.addAttribute("doubleVal", 1.1); - cl.addAttribute("floatVal", (float) 1.1); - cl.addAttribute("string", "abc"); - cl.addAttribute("char", 'a'); - cl.addAttribute("byte", (byte) 1); - cl = cl.addAttribute("short", (short) 2); - - try { - cl.deleteAttribute("X"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("attribute 'X' to be deleted not defined on classifier 'C'", e.getMessage()); - } - - cl.deleteAttribute("isBoolean").deleteAttribute("doubleVal").deleteAttribute("short").deleteAttribute("byte"); - - List attributes = cl.getAttributes(); - List attributeNames = cl.getAttributeNames(); - - assertEquals(5, attributes.size()); - assertEquals(5, attributeNames.size()); - - assertTrue(attributeNames.contains("intVal")); - assertTrue(attributeNames.contains("longVal")); - assertTrue(attributeNames.contains("floatVal")); - assertTrue(attributeNames.contains("string")); - assertTrue(attributeNames.contains("char")); - - model = CodeableModels.createModel(); - cl = model.createClass(mcl, "C"); - cl = cl.addAttribute("isBoolean", true); - - cl.deleteAttribute("isBoolean"); - - attributes = cl.getAttributes(); - attributeNames = cl.getAttributeNames(); - - assertEquals(0, attributes.size()); - assertEquals(0, attributeNames.size()); - } -} diff --git a/src/codeableModels/tests/TestsAttributesMetaclass.java b/src/codeableModels/tests/TestsAttributesMetaclass.java deleted file mode 100644 index f1ef623..0000000 --- a/src/codeableModels/tests/TestsAttributesMetaclass.java +++ /dev/null @@ -1,424 +0,0 @@ -package codeableModels.tests; - -import codeableModels.*; -import org.junit.*; - -import java.util.*; - -import static org.junit.Assert.*; - -public class TestsAttributesMetaclass { - - @Test - public void testPrimitiveTypeAttributesMetaclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - mcl.addAttribute("isBoolean", true); - mcl.addAttribute("intVal", 1); - mcl.addAttribute("longVal", (long) 100); - mcl.addAttribute("doubleVal", 1.1); - mcl.addAttribute("floatVal", (float) 1.1); - mcl.addAttribute("string", "abc"); - mcl.addAttribute("char", 'a'); - mcl.addAttribute("byte", (byte) 1); - mcl.addAttribute("short", (short) 2); - - try { - mcl.addAttribute("isBoolean", true); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("attribute 'isBoolean' cannot be created, attribute name already exists", e.getMessage()); - } - - List attributes = mcl.getAttributes(); - List attributeNames = mcl.getAttributeNames(); - - assertEquals(9, attributes.size()); - assertEquals(9, attributeNames.size()); - - assertTrue(attributeNames.contains("isBoolean")); - assertTrue(attributeNames.contains("intVal")); - assertTrue(attributeNames.contains("longVal")); - assertTrue(attributeNames.contains("doubleVal")); - assertTrue(attributeNames.contains("floatVal")); - assertTrue(attributeNames.contains("string")); - assertTrue(attributeNames.contains("char")); - assertTrue(attributeNames.contains("byte")); - assertTrue(attributeNames.contains("short")); - - CAttribute a0 = mcl.getAttribute("X"); - CAttribute a1 = mcl.getAttribute("isBoolean"); - CAttribute a2 = mcl.getAttribute("intVal"); - CAttribute a3 = mcl.getAttribute("longVal"); - CAttribute a4 = mcl.getAttribute("doubleVal"); - CAttribute a5 = mcl.getAttribute("floatVal"); - CAttribute a6 = mcl.getAttribute("string"); - CAttribute a7 = mcl.getAttribute("char"); - CAttribute a8 = mcl.getAttribute("byte"); - CAttribute a9 = mcl.getAttribute("short"); - - assertEquals(null, a0); - - assertTrue(attributes.contains(a1)); - assertTrue(attributes.contains(a2)); - assertTrue(attributes.contains(a3)); - assertTrue(attributes.contains(a4)); - assertTrue(attributes.contains(a5)); - assertTrue(attributes.contains(a6)); - assertTrue(attributes.contains(a7)); - assertTrue(attributes.contains(a8)); - assertTrue(attributes.contains(a9)); - - assertEquals("Boolean", a1.getType()); - assertEquals("Int", a2.getType()); - assertEquals("Long", a3.getType()); - assertEquals("Double", a4.getType()); - assertEquals("Float", a5.getType()); - assertEquals("String", a6.getType()); - assertEquals("Char", a7.getType()); - assertEquals("Byte", a8.getType()); - assertEquals("Short", a9.getType()); - - - Object d1 = a1.getDefaultValue(); - Object d2 = a2.getDefaultValue(); - Object d3 = a3.getDefaultValue(); - Object d4 = a4.getDefaultValue(); - Object d5 = a5.getDefaultValue(); - Object d6 = a6.getDefaultValue(); - Object d7 = a7.getDefaultValue(); - Object d8 = a8.getDefaultValue(); - Object d9 = a9.getDefaultValue(); - - assertTrue(d1 instanceof Boolean); - assertTrue(d2 instanceof Integer); - assertTrue(d3 instanceof Long); - assertTrue(d4 instanceof Double); - assertTrue(d5 instanceof Float); - assertTrue(d6 instanceof String); - assertTrue(d7 instanceof Character); - assertTrue(d8 instanceof Byte); - assertTrue(d9 instanceof Short); - - assertEquals(true, d1); - assertEquals(1, ((Integer) d2).intValue()); - assertEquals(100, ((Long) d3).longValue()); - assertEquals(1.1, (Double) d4, 0.001); - assertEquals(1.1, (Float) d5, 0.001); - assertEquals("abc", d6); - assertEquals('a', ((Character) d7).charValue()); - assertEquals(1, ((Byte) d8).byteValue()); - assertEquals(2, ((Short) d9).shortValue()); - } - - @Test - public void testObjectTypeAttributeMetaclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CClass attrType = model.createClass(mcl2, "AttrType"); - CObject attrValue = model.createObject(attrType, "attrValue"); - - mcl.addAttribute("attrTypeObj", attrValue); - CAttribute objAttr = mcl.getAttribute("attrTypeObj"); - List attributes = mcl.getAttributes(); - - assertEquals(1, attributes.size()); - assertTrue(attributes.contains(objAttr)); - - mcl.addAttribute("isBoolean", true); - - attributes = mcl.getAttributes(); - - assertEquals(2, attributes.size()); - objAttr = mcl.getAttribute("attrTypeObj"); - assertTrue(attributes.contains(objAttr)); - - assertEquals("Object", objAttr.getType()); - - Object d1 = objAttr.getDefaultValue(); - - assertTrue(d1 instanceof CObject); - - CObject value = (CObject) d1; - - assertEquals(attrValue, value); - } - - @Test - public void testEnumTypeAttributeMetaclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - - List enumValues = Arrays.asList("A", "B", "C"); - CEnum enumObj = model.createEnum("ABCEnum", enumValues); - assertEquals(0, mcl.getAttributes().size()); - - assertEquals(false, enumObj.isLegalValue("X")); - assertEquals(true, enumObj.isLegalValue("A")); - - mcl.addEnumAttribute("letters", enumObj).setAttributeDefaultValue("letters", "A"); - mcl.addEnumAttribute("letters2", enumObj); - - CAttribute enumAttr = mcl.getAttribute("letters"); - CAttribute enumAttr2 = mcl.getAttribute("letters2"); - List attributes = mcl.getAttributes(); - - assertEquals(2, attributes.size()); - assertTrue(attributes.contains(enumAttr)); - - mcl.addAttribute("isBoolean", true); - - attributes = mcl.getAttributes(); - - assertEquals(3, attributes.size()); - enumAttr = mcl.getAttribute("letters"); - assertTrue(attributes.contains(enumAttr)); - - assertEquals("Enum", enumAttr.getType()); - - Object d1 = enumAttr.getDefaultValue(); - Object d2 = enumAttr2.getDefaultValue(); - - assertEquals("A", d1); - assertEquals(null, d2); - - try { - mcl.addEnumAttribute("letters3", enumObj).setAttributeDefaultValue("letters3", "X"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value 'X' not element of enumeration", e.getMessage()); - } - } - - - @Test - public void testSetAttributeDefaultValueMetaclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - - List enumValues = Arrays.asList("A", "B", "C"); - CEnum enumObj = model.createEnum("ABCEnum", enumValues); - - mcl.addEnumAttribute("letters", enumObj); - mcl.addBooleanAttribute("b"); - - try { - mcl.setAttributeDefaultValue("x", "A"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("unknown attribute name 'x' on classifier 'MCL'", e.getMessage()); - } - mcl.setAttributeDefaultValue("letters", "A"); - mcl.setAttributeDefaultValue("b", true); - - Object d1 = mcl.getAttribute("letters").getDefaultValue(); - Object d2 = mcl.getAttribute("b").getDefaultValue(); - - assertEquals("A", d1); - assertEquals(true, d2); - - } - - @Test - public void testAttributeTypeCheckMetaclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - mcl.addBooleanAttribute("a"); - CAttribute attr = mcl.getAttribute("a"); - try { - attr.setDefaultValue(CodeableModels.createModel()); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value for attribute 'a' is not a known attribute type", e.getMessage()); - } - try { - attr.setDefaultValue(1); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'a' does not match attribute type", e.getMessage()); - } - - mcl.addIntAttribute("i"); - attr = mcl.getAttribute("i"); - try { - attr.setDefaultValue(Boolean.TRUE); - } catch (CException e) { - assertEquals("value type for attribute 'i' does not match attribute type", e.getMessage()); - } - - mcl.addShortAttribute("s"); - attr = mcl.getAttribute("s"); - try { - attr.setDefaultValue(Boolean.TRUE); - } catch (CException e) { - assertEquals("value type for attribute 's' does not match attribute type", e.getMessage()); - } - - mcl.addByteAttribute("b"); - attr = mcl.getAttribute("b"); - try { - attr.setDefaultValue(Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'b' does not match attribute type", e.getMessage()); - } - - mcl.addLongAttribute("l"); - attr = mcl.getAttribute("l"); - try { - attr.setDefaultValue(Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'l' does not match attribute type", e.getMessage()); - } - - mcl.addFloatAttribute("f"); - attr = mcl.getAttribute("f"); - try { - attr.setDefaultValue(Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'f' does not match attribute type", e.getMessage()); - } - - mcl.addDoubleAttribute("d"); - attr = mcl.getAttribute("d"); - try { - attr.setDefaultValue(Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'd' does not match attribute type", e.getMessage()); - } - - CEnum enumType = model.createEnum("XYEnum", Arrays.asList("X", "Y")); - mcl.addEnumAttribute("e", enumType); - attr = mcl.getAttribute("e"); - try { - attr.setDefaultValue(Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'e' does not match attribute type", e.getMessage()); - } - - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CClass attrClass = model.createClass(mcl2, "AttrClass"); - CClass otherClass = model.createClass(mcl2, "OtherClass"); - mcl.addObjectAttribute("o", attrClass); - attr = mcl.getAttribute("o"); - try { - attr.setDefaultValue(Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'o' does not match attribute type", e.getMessage()); - } - - try { - attr.setDefaultValue(otherClass); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("type classifier of object 'OtherClass' is not matching attribute type of attribute 'o'", e.getMessage()); - } - - } - - - @Test - public void testCreateAndDeleteAttributesMetaclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - mcl.addAttribute("isBoolean", true); - mcl.addAttribute("intVal", 1); - mcl.addAttribute("longVal", (long) 100); - mcl.addAttribute("doubleVal", 1.1); - mcl.addAttribute("floatVal", (float) 1.1); - mcl.addAttribute("string", "abc"); - mcl.addAttribute("char", 'a'); - mcl.addAttribute("byte", (byte) 1); - mcl = mcl.addAttribute("short", (short) 2); - - try { - mcl.deleteAttribute("X"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("attribute 'X' to be deleted not defined on classifier 'MCL'", e.getMessage()); - } - - mcl = mcl.deleteAttribute("isBoolean").deleteAttribute("doubleVal").deleteAttribute("short").deleteAttribute - ("byte"); - - List attributes = mcl.getAttributes(); - List attributeNames = mcl.getAttributeNames(); - - assertEquals(5, attributes.size()); - assertEquals(5, attributeNames.size()); - - assertTrue(attributeNames.contains("intVal")); - assertTrue(attributeNames.contains("longVal")); - assertTrue(attributeNames.contains("floatVal")); - assertTrue(attributeNames.contains("string")); - assertTrue(attributeNames.contains("char")); - - model = CodeableModels.createModel(); - mcl = model.createMetaclass("MCL"); - mcl.addAttribute("isBoolean", true); - - mcl.deleteAttribute("isBoolean"); - - attributes = mcl.getAttributes(); - attributeNames = mcl.getAttributeNames(); - - assertEquals(0, attributes.size()); - assertEquals(0, attributeNames.size()); - } - - @Test - public void testCreateAndDeleteAttributesStereotype() throws CException { - CModel model = CodeableModels.createModel(); - CStereotype stereo = model.createStereotype("Stereo"); - stereo.addAttribute("isBoolean", true); - stereo.addAttribute("intVal", 1); - stereo.addAttribute("longVal", (long) 100); - stereo.addAttribute("doubleVal", 1.1); - stereo.addAttribute("floatVal", (float) 1.1); - stereo.addAttribute("string", "abc"); - stereo.addAttribute("char", 'a'); - stereo.addAttribute("byte", (byte) 1); - stereo = stereo.addAttribute("short", (short) 2); - - try { - stereo.deleteAttribute("X"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("attribute 'X' to be deleted not defined on classifier 'Stereo'", e.getMessage()); - } - - stereo = stereo.deleteAttribute("isBoolean").deleteAttribute("doubleVal").deleteAttribute("short").deleteAttribute("byte"); - - List attributes = stereo.getAttributes(); - List attributeNames = stereo.getAttributeNames(); - - assertEquals(5, attributes.size()); - assertEquals(5, attributeNames.size()); - - assertTrue(attributeNames.contains("intVal")); - assertTrue(attributeNames.contains("longVal")); - assertTrue(attributeNames.contains("floatVal")); - assertTrue(attributeNames.contains("string")); - assertTrue(attributeNames.contains("char")); - - model = CodeableModels.createModel(); - stereo = model.createStereotype("Stereo"); - stereo.addAttribute("isBoolean", true); - - stereo.deleteAttribute("isBoolean"); - - attributes = stereo.getAttributes(); - attributeNames = stereo.getAttributeNames(); - - assertEquals(0, attributes.size()); - assertEquals(0, attributeNames.size()); - } - - -} diff --git a/src/codeableModels/tests/TestsAttributesStereotype.java b/src/codeableModels/tests/TestsAttributesStereotype.java deleted file mode 100644 index 44363e1..0000000 --- a/src/codeableModels/tests/TestsAttributesStereotype.java +++ /dev/null @@ -1,377 +0,0 @@ -package codeableModels.tests; - -import codeableModels.*; -import org.junit.*; - -import java.util.*; - -import static org.junit.Assert.*; - -public class TestsAttributesStereotype { - - /* Attribute on a stereotype are used as tagged values on CObjects */ - - @Test - public void testPrimitiveTypeAttributesStereotype() throws CException { - CModel model = CodeableModels.createModel(); - CStereotype stereo = model.createStereotype("ST"); - stereo.addAttribute("isBoolean", true); - stereo.addAttribute("intVal", 1); - stereo.addAttribute("longVal", (long) 100); - stereo.addAttribute("doubleVal", 1.1); - stereo.addAttribute("floatVal", (float) 1.1); - stereo.addAttribute("string", "abc"); - stereo.addAttribute("char", 'a'); - stereo.addAttribute("byte", (byte) 1); - stereo.addAttribute("short", (short) 2); - - try { - stereo.addAttribute("isBoolean", true); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("attribute 'isBoolean' cannot be created, attribute name already exists", e.getMessage()); - } - - - List attributes = stereo.getAttributes(); - List attributeNames = stereo.getAttributeNames(); - - assertEquals(9, attributes.size()); - assertEquals(9, attributeNames.size()); - - assertTrue(attributeNames.contains("isBoolean")); - assertTrue(attributeNames.contains("intVal")); - assertTrue(attributeNames.contains("longVal")); - assertTrue(attributeNames.contains("doubleVal")); - assertTrue(attributeNames.contains("floatVal")); - assertTrue(attributeNames.contains("string")); - assertTrue(attributeNames.contains("char")); - assertTrue(attributeNames.contains("byte")); - assertTrue(attributeNames.contains("short")); - - CAttribute a0 = stereo.getAttribute("X"); - CAttribute a1 = stereo.getAttribute("isBoolean"); - CAttribute a2 = stereo.getAttribute("intVal"); - CAttribute a3 = stereo.getAttribute("longVal"); - CAttribute a4 = stereo.getAttribute("doubleVal"); - CAttribute a5 = stereo.getAttribute("floatVal"); - CAttribute a6 = stereo.getAttribute("string"); - CAttribute a7 = stereo.getAttribute("char"); - CAttribute a8 = stereo.getAttribute("byte"); - CAttribute a9 = stereo.getAttribute("short"); - - assertEquals(null, a0); - - assertTrue(attributes.contains(a1)); - assertTrue(attributes.contains(a2)); - assertTrue(attributes.contains(a3)); - assertTrue(attributes.contains(a4)); - assertTrue(attributes.contains(a5)); - assertTrue(attributes.contains(a6)); - assertTrue(attributes.contains(a7)); - assertTrue(attributes.contains(a8)); - assertTrue(attributes.contains(a9)); - - assertEquals("Boolean", a1.getType()); - assertEquals("Int", a2.getType()); - assertEquals("Long", a3.getType()); - assertEquals("Double", a4.getType()); - assertEquals("Float", a5.getType()); - assertEquals("String", a6.getType()); - assertEquals("Char", a7.getType()); - assertEquals("Byte", a8.getType()); - assertEquals("Short", a9.getType()); - - - Object d1 = a1.getDefaultValue(); - Object d2 = a2.getDefaultValue(); - Object d3 = a3.getDefaultValue(); - Object d4 = a4.getDefaultValue(); - Object d5 = a5.getDefaultValue(); - Object d6 = a6.getDefaultValue(); - Object d7 = a7.getDefaultValue(); - Object d8 = a8.getDefaultValue(); - Object d9 = a9.getDefaultValue(); - - assertTrue(d1 instanceof Boolean); - assertTrue(d2 instanceof Integer); - assertTrue(d3 instanceof Long); - assertTrue(d4 instanceof Double); - assertTrue(d5 instanceof Float); - assertTrue(d6 instanceof String); - assertTrue(d7 instanceof Character); - assertTrue(d8 instanceof Byte); - assertTrue(d9 instanceof Short); - - assertEquals(true, d1); - assertEquals(1, ((Integer) d2).intValue()); - assertEquals(100, ((Long) d3).longValue()); - assertEquals(1.1, (Double) d4, 0.001); - assertEquals(1.1, (Float) d5, 0.001); - assertEquals("abc", d6); - assertEquals('a', ((Character) d7).charValue()); - assertEquals(1, ((Byte) d8).byteValue()); - assertEquals(2, ((Short) d9).shortValue()); - } - - @Test - public void testObjectTypeAttributeStereotype() throws CException { - CModel model = CodeableModels.createModel(); - CStereotype stereo = model.createStereotype("ST"); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass attrType = model.createClass(mcl, "AttrType"); - CObject attrValue = model.createObject(attrType, "attrValue"); - - stereo.addAttribute("attrTypeObj", attrValue); - CAttribute objAttr = stereo.getAttribute("attrTypeObj"); - List attributes = stereo.getAttributes(); - - assertEquals(1, attributes.size()); - assertTrue(attributes.contains(objAttr)); - - stereo.addAttribute("isBoolean", true); - - attributes = stereo.getAttributes(); - - assertEquals(2, attributes.size()); - objAttr = stereo.getAttribute("attrTypeObj"); - assertTrue(attributes.contains(objAttr)); - - assertEquals("Object", objAttr.getType()); - - Object d1 = objAttr.getDefaultValue(); - - assertTrue(d1 instanceof CObject); - - CObject value = (CObject) d1; - - assertEquals(attrValue, value); - } - - @Test - public void testEnumTypeAttributeStereotype() throws CException { - CModel model = CodeableModels.createModel(); - CStereotype stereotype = model.createStereotype("ST"); - - List enumValues = Arrays.asList("A", "B", "C"); - CEnum enumObj = model.createEnum("ABCEnum", enumValues); - assertEquals(0, stereotype.getAttributes().size()); - - assertEquals(false, enumObj.isLegalValue("X")); - assertEquals(true, enumObj.isLegalValue("A")); - - stereotype.addEnumAttribute("letters", enumObj).setAttributeDefaultValue("letters", "A"); - stereotype.addEnumAttribute("letters2", enumObj); - - CAttribute enumAttr = stereotype.getAttribute("letters"); - CAttribute enumAttr2 = stereotype.getAttribute("letters2"); - List attributes = stereotype.getAttributes(); - - assertEquals(2, attributes.size()); - assertTrue(attributes.contains(enumAttr)); - - stereotype.addAttribute("isBoolean", true); - - attributes = stereotype.getAttributes(); - - assertEquals(3, attributes.size()); - enumAttr = stereotype.getAttribute("letters"); - assertTrue(attributes.contains(enumAttr)); - - assertEquals("Enum", enumAttr.getType()); - - Object d1 = enumAttr.getDefaultValue(); - Object d2 = enumAttr2.getDefaultValue(); - - assertEquals("A", d1); - assertEquals(null, d2); - - try { - stereotype.addEnumAttribute("letters3", enumObj).setAttributeDefaultValue("letters3", "X"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value 'X' not element of enumeration", e.getMessage()); - } - } - - - @Test - public void testSetAttributeDefaultValueStereotype() throws CException { - CModel model = CodeableModels.createModel(); - CStereotype stereotype = model.createStereotype("ST"); - - List enumValues = Arrays.asList("A", "B", "C"); - CEnum enumObj = model.createEnum("ABCEnum", enumValues); - - stereotype.addEnumAttribute("letters", enumObj); - stereotype.addBooleanAttribute("b"); - - try { - stereotype.setAttributeDefaultValue("x", "A"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("unknown attribute name 'x' on classifier 'ST'", e.getMessage()); - } - stereotype.setAttributeDefaultValue("letters", "A"); - stereotype.setAttributeDefaultValue("b", true); - - Object d1 = stereotype.getAttribute("letters").getDefaultValue(); - Object d2 = stereotype.getAttribute("b").getDefaultValue(); - - assertEquals("A", d1); - assertEquals(true, d2); - - } - - @Test - public void testAttributeTypeCheckMetaclassStereotype() throws CException { - CModel model = CodeableModels.createModel(); - CStereotype stereotype = model.createStereotype("ST"); - stereotype.addBooleanAttribute("a"); - CAttribute attr = stereotype.getAttribute("a"); - try { - attr.setDefaultValue(CodeableModels.createModel()); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value for attribute 'a' is not a known attribute type", e.getMessage()); - } - try { - attr.setDefaultValue(1); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'a' does not match attribute type", e.getMessage()); - } - - stereotype.addIntAttribute("i"); - attr = stereotype.getAttribute("i"); - try { - attr.setDefaultValue(Boolean.TRUE); - } catch (CException e) { - assertEquals("value type for attribute 'i' does not match attribute type", e.getMessage()); - } - - stereotype.addShortAttribute("s"); - attr = stereotype.getAttribute("s"); - try { - attr.setDefaultValue(Boolean.TRUE); - } catch (CException e) { - assertEquals("value type for attribute 's' does not match attribute type", e.getMessage()); - } - - stereotype.addByteAttribute("b"); - attr = stereotype.getAttribute("b"); - try { - attr.setDefaultValue(Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'b' does not match attribute type", e.getMessage()); - } - - stereotype.addLongAttribute("l"); - attr = stereotype.getAttribute("l"); - try { - attr.setDefaultValue(Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'l' does not match attribute type", e.getMessage()); - } - - stereotype.addFloatAttribute("f"); - attr = stereotype.getAttribute("f"); - try { - attr.setDefaultValue(Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'f' does not match attribute type", e.getMessage()); - } - - stereotype.addDoubleAttribute("d"); - attr = stereotype.getAttribute("d"); - try { - attr.setDefaultValue(Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'd' does not match attribute type", e.getMessage()); - } - - CEnum enumType = model.createEnum("XYEnum", Arrays.asList("X", "Y")); - stereotype.addEnumAttribute("e", enumType); - attr = stereotype.getAttribute("e"); - try { - attr.setDefaultValue(Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'e' does not match attribute type", e.getMessage()); - } - - CMetaclass mcl = model.createMetaclass("MCL"); - CClass attrClass = model.createClass(mcl, "AttrClass"); - CClass otherClass = model.createClass(mcl, "OtherClass"); - stereotype.addObjectAttribute("o", attrClass); - attr = stereotype.getAttribute("o"); - try { - attr.setDefaultValue(Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'o' does not match attribute type", e.getMessage()); - } - - try { - attr.setDefaultValue(otherClass); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("type classifier of object 'OtherClass' is not matching attribute type of attribute 'o'", e.getMessage()); - } - - } - - @Test - public void testCreateAndDeleteAttributesStereotype() throws CException { - CModel model = CodeableModels.createModel(); - CStereotype stereo = model.createStereotype("Stereo"); - stereo.addAttribute("isBoolean", true); - stereo.addAttribute("intVal", 1); - stereo.addAttribute("longVal", (long) 100); - stereo.addAttribute("doubleVal", 1.1); - stereo.addAttribute("floatVal", (float) 1.1); - stereo.addAttribute("string", "abc"); - stereo.addAttribute("char", 'a'); - stereo.addAttribute("byte", (byte) 1); - stereo = stereo.addAttribute("short", (short) 2); - - try { - stereo.deleteAttribute("X"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("attribute 'X' to be deleted not defined on classifier 'Stereo'", e.getMessage()); - } - - stereo = stereo.deleteAttribute("isBoolean").deleteAttribute("doubleVal").deleteAttribute("short").deleteAttribute("byte"); - - List attributes = stereo.getAttributes(); - List attributeNames = stereo.getAttributeNames(); - - assertEquals(5, attributes.size()); - assertEquals(5, attributeNames.size()); - - assertTrue(attributeNames.contains("intVal")); - assertTrue(attributeNames.contains("longVal")); - assertTrue(attributeNames.contains("floatVal")); - assertTrue(attributeNames.contains("string")); - assertTrue(attributeNames.contains("char")); - - model = CodeableModels.createModel(); - stereo = model.createStereotype("Stereo"); - stereo.addAttribute("isBoolean", true); - - stereo.deleteAttribute("isBoolean"); - - attributes = stereo.getAttributes(); - attributeNames = stereo.getAttributeNames(); - - assertEquals(0, attributes.size()); - assertEquals(0, attributeNames.size()); - } - - -} diff --git a/src/codeableModels/tests/TestsClass.java b/src/codeableModels/tests/TestsClass.java deleted file mode 100644 index f229098..0000000 --- a/src/codeableModels/tests/TestsClass.java +++ /dev/null @@ -1,214 +0,0 @@ -package codeableModels.tests; - -import codeableModels.*; -import org.junit.*; - -import java.util.*; - -import static org.junit.Assert.*; - -public class TestsClass { - - @Test - public void testCreationOfOneClass() throws CException { - CModel model = CodeableModels.createModel(); - model.createMetaclass("MCL"); - CClass cl = model.createClass("MCL", "Class1"); - CClass cl2 = model.getClass("Class1"); - assertEquals(cl, cl2); - assertEquals(model, cl2.getModel()); - } - - @Test - public void testCreationOfAutoNamedClass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass("MCL"); - CClass cl2 = model.createClass(mcl); - String cl1Name = cl1.getName(), cl2Name = cl2.getName(); - assertTrue(cl1Name.startsWith("##")); - assertTrue(cl2Name.startsWith("##")); - CObject cl3 = model.getClass(cl1Name); - CObject cl4 = model.getClass(cl2Name); - assertEquals(cl1, cl3); - assertEquals(cl2, cl4); - assertEquals(mcl, cl3.getClassifier()); - assertEquals(mcl, cl4.getClassifier()); - } - - @Test - public void testCreationOf3Classes() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CClass cl3 = model.createClass(mcl, "Class3"); - CClass gcl1 = model.getClass("Class1"); - CClass gcl2 = model.getClass("Class2"); - CClass gcl3 = model.getClass("Class3"); - assertEquals(cl1, gcl1); - assertEquals(cl2, gcl2); - assertEquals(cl3, gcl3); - } - - @Test - public void testCCreationOfNoClass() throws CException { - CModel model = CodeableModels.createModel(); - CClass gcl1 = model.getClass("Class1"); - assertEquals(null, gcl1); - } - - @Test - public void testClassUniqueNameUsedException() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - model.createClass(mcl, "Class1"); - try { - model.createClass(mcl, "Class1"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("classifier 'Class1' cannot be created, classifier name already exists", e.getMessage()); - } - } - - @Test - public void testLookupClassifierLocally() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl = model.createClass(mcl, "Class1"); - CClass cl2 = model.lookupClass("Class1"); - assertEquals(cl, cl2); - } - - @Test - public void testLookup3ClassesLocally() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CClass cl3 = model.createClass(mcl, "Class3"); - CClass gcl1 = model.lookupClass("Class1"); - CClass gcl2 = model.lookupClass("Class2"); - CClass gcl3 = model.lookupClass("Class3"); - assertEquals(cl1, gcl1); - assertEquals(cl2, gcl2); - assertEquals(cl3, gcl3); - } - - @Test - public void testLookupClassifierLocallyThatDoesNotExist() throws CException { - CModel model = CodeableModels.createModel(); - CClass gcl1 = model.lookupClass("Class1"); - assertEquals(null, gcl1); - } - - @Test - public void testGetClassesAndGetClassNames() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - List classes = model.getClasses(); - List classNames = model.getClassNames(); - assertEquals(0, classes.size()); - assertEquals(0, classNames.size()); - - CClass cl1 = model.createClass(mcl, "Class1"); - - classes = model.getClasses(); - classNames = model.getClassNames(); - assertEquals(1, classes.size()); - assertEquals(1, classNames.size()); - assertEquals("Class1", classNames.get(0)); - assertEquals(cl1, classes.get(0)); - - CClass cl2 = model.createClass(mcl, "Class2"); - CClass cl3 = model.createClass(mcl, "Class3"); - - classes = model.getClasses(); - classNames = model.getClassNames(); - assertEquals(3, classes.size()); - assertEquals(3, classNames.size()); - assertTrue(classes.contains(cl1)); - assertTrue(classes.contains(cl2)); - assertTrue(classes.contains(cl3)); - } - - @Test - public void testDeleteClass() throws CException { - CModel model = CodeableModels.createModel(); - CModel model2 = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - try { - model.deleteClassifier(null); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("classifier '' to be deleted does not exist", e.getMessage()); - } - try { - model.deleteClassifier(model2.createMetaclass("x")); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("classifier 'x' to be deleted does not exist", e.getMessage()); - } - CClass cl1 = model.createClass(mcl, "Class1"); - model.deleteClassifier(cl1); - - CClass gcl1 = model.getClass("Class1"); - assertEquals(null, gcl1); - List classes = model.getClasses(); - assertEquals(0, classes.size()); - - cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CClass cl3 = model.createClass(mcl, "Class3"); - - model.deleteClassifier(cl1); - try { - model.deleteClassifier(model2.createMetaclass("y")); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("classifier 'y' to be deleted does not exist", e.getMessage()); - } - model.deleteClassifier(cl3); - - gcl1 = model.getClass("Class1"); - CClass gcl2 = model.getClass("Class2"); - CClass gcl3 = model.getClass("Class3"); - - assertEquals(null, gcl1); - assertEquals(cl2, gcl2); - assertEquals(null, gcl3); - - classes = model.getClasses(); - assertEquals(1, classes.size()); - } - - - @Test - public void testDeleteClassInstanceRelation() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CObject obj1 = model.createObject(cl1, "O1"); - CObject obj2 = model.createObject(cl1, "O2"); - CObject obj3 = model.createObject(cl2, "O3"); - - model.deleteClassifier(cl1); - - assertEquals(0, cl1.getInstances().size()); - assertEquals(null, obj1.getClassifier()); - assertEquals(null, obj2.getClassifier()); - assertEquals(cl2, obj3.getClassifier()); - - List instances2 = cl2.getInstances(); - assertEquals(1, instances2.size()); - assertTrue(!instances2.contains(obj1)); - assertTrue(!instances2.contains(obj2)); - assertTrue(instances2.contains(obj3)); - - List objects = model.getObjects(); - assertEquals(1, objects.size()); - assertEquals(obj3, objects.get(0)); - } - -} diff --git a/src/codeableModels/tests/TestsClassLinks.java b/src/codeableModels/tests/TestsClassLinks.java deleted file mode 100644 index bb7c6b1..0000000 --- a/src/codeableModels/tests/TestsClassLinks.java +++ /dev/null @@ -1,1112 +0,0 @@ -package codeableModels.tests; - -import codeableModels.*; -import org.junit.*; - -import java.util.*; - -import static org.junit.Assert.*; - -public class TestsClassLinks { - @Test - public void testSetOneToOneLink() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociation association = model.createAssociation(mcl1.createEnd("metaclass1", "1"), - mcl2.createEnd("metaclass2", "1")); - CAssociationEnd mcl1End = association.getEndByRoleName("metaclass1"); - CAssociationEnd mcl2End = association.getEndByRoleName("metaclass2"); - - CClass cl2 = model.createClass(mcl2, "CL2"); - CClass cl1 = model.createClass(mcl1, "CL1"); - cl1.setLink(mcl2End, cl2); - CClass cl3 = model.createClass(mcl2, "CL3"); - - assertEquals(Collections.singletonList(cl2), cl1.getLinks(mcl2End)); - assertEquals(Collections.singletonList(cl1), cl2.getLinks(mcl1End)); - - cl1.setLink(association.getEndByClassifier(mcl2), "CL3"); - - assertEquals(Collections.singletonList(cl3), cl1.getLinks(mcl2End)); - assertEquals(Collections.singletonList(cl1), cl3.getLinks(mcl1End)); - } - - @Test - public void testSetLinkVariants() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociation assoc = model.createAssociation(mcl1.createEnd("metaclass1", "1"), - mcl2.createEnd("metaclass2", "1")); - CAssociationEnd end1 = assoc.getEndByClassifier(mcl1), end2 = assoc.getEndByClassifier(mcl2); - - CClass cl1 = model.createClass(mcl1, "CL1"); - CClass cl2 = model.createClass(mcl2, "CL2"); - - cl1.setLinks(end2, Collections.singletonList(cl2)); - - assertEquals(Collections.singletonList(cl2), cl1.getLinks(end2)); - assertEquals(Collections.singletonList(cl1), cl2.getLinks(end1)); - - cl1.setLinks(end2, Collections.singletonList(cl2)); - - assertEquals(Collections.singletonList(cl2), cl1.getLinks(end2)); - assertEquals(Collections.singletonList(cl1), cl2.getLinks(end1)); - - cl1.setLinks(end2, Collections.singletonList("CL2")); - - assertEquals(Collections.singletonList(cl2), cl1.getLinks(end2)); - assertEquals(Collections.singletonList(cl1), cl2.getLinks(end1)); - - cl2.setLink(end1, cl1); - - assertEquals(Collections.singletonList(cl2), cl1.getLinks(end2)); - assertEquals(Collections.singletonList(cl1), cl2.getLinks(end1)); - - CLink link = cl2.setLink(end1, "CL1"); - - assertEquals(Collections.singletonList(cl2), cl1.getLinks(end2)); - assertEquals(Collections.singletonList(cl1), cl2.getLinks(end1)); - - assertEquals(cl2.getLinkObjects(end1).get(0), link); - } - - @Test - public void testAddOneToOneLink() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociationEnd end1 = mcl1.createEnd("metaclass1", "1"), - end2 = mcl2.createEnd("metaclass2", "1"); - model.createAssociation(end1, end2); - - CClass cl2 = model.createClass(mcl2, "CL2"); - CClass cl1 = model.createClass(mcl1, "CL1"); - cl1.addLink(end2, cl2); - model.createClass(mcl2, "CL3"); - - assertEquals(Collections.singletonList(cl2), cl1.getLinks(end2)); - assertEquals(Collections.singletonList(cl1), cl2.getLinks(end1)); - - try { - cl1.addLink(end2, "CL3"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("link has wrong multiplicity '2', but should be '1'", e.getMessage()); - } - } - - @Test - public void testAddOneToOneLinkVariants() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociationEnd end1 = mcl1.createEnd("metaclass1", "1"), - end2 = mcl2.createEnd("metaclass2", "1"); - model.createAssociation(end1, end2); - - CClass cl1 = model.createClass(mcl1, "CL1"); - CClass cl2 = model.createClass(mcl2, "CL2"); - - cl1.addLinks(end2, Collections.singletonList(cl2)); - - assertEquals(Collections.singletonList(cl2), cl1.getLinks(end2)); - assertEquals(Collections.singletonList(cl1), cl2.getLinks(end1)); - - try { - cl1.addLinks(end2, Collections.singletonList(cl2)); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("link between 'CL1' and 'CL2' already exists", e.getMessage()); - } - - cl1.removeLinks(end2, Collections.singletonList(cl2)); - cl1.addLinks(end2, Collections.singletonList(cl2)); - - assertEquals(Collections.singletonList(cl2), cl1.getLinks(end2)); - assertEquals(Collections.singletonList(cl1), cl2.getLinks(end1)); - - cl2.removeLinks(end1, Collections.singletonList("CL1")); - cl2.addLinks(end1, Collections.singletonList("CL1")); - - assertEquals(Collections.singletonList(cl2), cl1.getLinks(end2)); - assertEquals(Collections.singletonList(cl1), cl2.getLinks(end1)); - - cl2.removeLink(end1, "CL1"); - CLink link = cl2.addLink(end1, "CL1"); - - assertEquals(Collections.singletonList(cl2), cl1.getLinks(end2)); - assertEquals(Collections.singletonList(cl1), cl2.getLinks(end1)); - - assertEquals(cl2.getLinkObjects(end1).get(0), link); - - cl2.removeLink(end1, cl1); - cl2.addLink(end1, cl1); - - assertEquals(Collections.singletonList(cl2), cl1.getLinks(end2)); - assertEquals(Collections.singletonList(cl1), cl2.getLinks(end1)); - } - - @Test - public void testOneToOneRemoveAllLinks() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociationEnd end1 = mcl1.createEnd("metaclass1", "1"), - end2 = mcl2.createEnd("metaclass2", "1"); - model.createAssociation(end1, end2); - - CClass cl1 = model.createClass(mcl1, "CL1"); - CClass cl2 = model.createClass(mcl2, "CL2"); - - cl1.removeAllLinks(end2); - - cl1.setLinks(end2, Collections.singletonList(cl2)); - - assertEquals(Collections.singletonList(cl2), cl1.getLinks(end2)); - assertEquals(Collections.singletonList(cl1), cl2.getLinks(end1)); - - cl1.removeAllLinks(end2); - - assertEquals(Collections.emptyList(), cl1.getLinks(end2)); - assertEquals(Collections.emptyList(), cl2.getLinks(end1)); - - cl1.setLinks(end2, Collections.singletonList(cl2)); - - assertEquals(Collections.singletonList(cl2), cl1.getLinks(end2)); - assertEquals(Collections.singletonList(cl1), cl2.getLinks(end1)); - - cl2.removeAllLinks(end1); - - assertEquals(Collections.emptyList(), cl1.getLinks(end2)); - assertEquals(Collections.emptyList(), cl2.getLinks(end1)); - } - - @Test - public void testOneToOneRemoveLinks() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociationEnd end1 = mcl1.createEnd("metaclass1", "1"), - end2 = mcl2.createEnd("metaclass2", "1"); - model.createAssociation(end1, end2); - - CClass cl1 = model.createClass(mcl1, "CL1"); - CClass cl2 = model.createClass(mcl2, "CL2"); - - try { - cl1.removeLink(end2, cl2); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("link between 'CL1' and 'CL2' can't be removed: it does not exist", e.getMessage()); - } - - try { - cl1.removeLinks(end2, Collections.singletonList(cl2)); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("link between 'CL1' and 'CL2' can't be removed: it does not exist", e.getMessage()); - } - - cl1.setLinks(end2, Collections.singletonList(cl2)); - - assertEquals(Collections.singletonList(cl2), cl1.getLinks(end2)); - assertEquals(Collections.singletonList(cl1), cl2.getLinks(end1)); - - cl1.removeLinks(end2, Collections.singletonList(cl2)); - - assertEquals(Collections.emptyList(), cl1.getLinks(end2)); - assertEquals(Collections.emptyList(), cl2.getLinks(end1)); - - cl1.setLink(end2, cl2); - - assertEquals(Collections.singletonList(cl2), cl1.getLinks(end2)); - assertEquals(Collections.singletonList(cl1), cl2.getLinks(end1)); - - cl2.removeLink(end1, cl1); - - assertEquals(Collections.emptyList(), cl1.getLinks(end2)); - assertEquals(Collections.emptyList(), cl2.getLinks(end1)); - } - - - @Test - public void testOneToOneLinkMultiplicity() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociationEnd end1 = mcl1.createEnd("metaclass1", "1"), - end2 = mcl2.createEnd("metaclass2", "1"); - model.createAssociation(end1, end2); - CClass cl1 = model.createClass(mcl1, "CL1"); - CClass cl2 = model.createClass(mcl2, "CL2"); - - try { - cl1.setLinks(end2, Collections.emptyList()); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("link has wrong multiplicity '0', but should be '1'", e.getMessage()); - } - - try { - cl1.setLink(end2, (CObject) null); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("link has wrong multiplicity '0', but should be '1'", e.getMessage()); - } - - - cl1.addLinks(end2, Collections.emptyList()); - assertEquals(Collections.emptyList(), cl1.getLinks(end2)); - - cl1.addLink(end2, (CObject) null); - assertEquals(Collections.emptyList(), cl1.getLinks(end2)); - - CClass cl3 = model.createClass(mcl2, "CL3"); - - try { - cl1.setLinks(end2, Arrays.asList(cl2, cl3)); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("link has wrong multiplicity '2', but should be '1'", e.getMessage()); - } - } - - @Test - public void testSetLinksByStrings() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociationEnd end1 = mcl1.createEnd("metaclass1", "1"), - end2 = mcl2.createEnd("metaclass2", "*"); - model.createAssociation(end1, end2); - CClass cl1 = model.createClass(mcl1, "CL1"); - CClass cl2 = model.createClass(mcl2, "CL2"); - CClass cl3 = model.createClass(mcl2, "CL3"); - - cl1.setLinks(end2, Collections.singletonList("CL2")); - assertEquals(Collections.singletonList(cl2), cl1.getLinks(end2)); - - cl1.setLinks(end2, Arrays.asList("CL2", "CL3")); - assertEquals(Arrays.asList(cl2, cl3), cl1.getLinks(end2)); - - - try { - cl1.setLinks(end2, Arrays.asList("CL2", "CL4")); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("class 'CL4' unknown", e.getMessage()); - } - - try { - Integer i = 4; - cl1.setLinks(end2, Arrays.asList("CL2", i)); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("argument '4' is not of type String or CObject", e.getMessage()); - } - } - - @Test - public void testSetLinksWithSubclassInstance() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CMetaclass mcl3 = model.createMetaclass("MCL3").addSuperclass(mcl2); - CAssociationEnd end1 = mcl1.createEnd("metaclass1", "1"), - end2 = mcl2.createEnd("metaclass2", "*"); - model.createAssociation(end1, end2); - CClass cl1 = model.createClass(mcl1, "CL1"); - CClass cl2 = model.createClass(mcl2, "CL2"); - CClass cl3 = model.createClass(mcl3, "CL3"); - - cl1.setLinks(end2, Arrays.asList("CL2", "CL3")); - assertEquals(Arrays.asList(cl2, cl3), cl1.getLinks(end2)); - } - - @Test - public void testOneToOneLinkIncompatibleTypes() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociationEnd end1 = mcl1.createEnd("metaclass1", "1"), - end2 = mcl2.createEnd("metaclass2", "1"); - model.createAssociation(end1, end2); - CClass cl1 = model.createClass(mcl1, "CL1"); - CClass cl2 = model.createClass(mcl2, "CL2"); - CClass cl3 = model.createClass(mcl2, "CL3"); - - try { - cl1.setLinks(end2, Arrays.asList(cl1, cl2, cl3)); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("link object 'CL1' not compatible with association classifier 'MCL2'", e.getMessage()); - } - } - - @Test - public void testOneToOneLinkIsNavigable() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociationEnd end1 = mcl1.createEnd("metaclass1", "1"); - end1.setNavigable(false); - CAssociationEnd end2 = mcl2.createEnd("metaclass2", "1"); - end2.setNavigable(false); - model.createAssociation(end1, end2); - - CClass cl1 = model.createClass(mcl1, "CL1"); - CClass cl2 = model.createClass(mcl2, "CL2"); - - try { - cl1.setLinks(end2, Collections.singletonList(cl2)); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'metaclass2' is not navigable and thus cannot be accessed from object " + - "'CL1'", e.getMessage()); - } - - try { - cl2.setLinks(end1, Collections.singletonList(cl1)); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'metaclass1' is not navigable and thus cannot be accessed from object " + - "'CL2'", e.getMessage()); - } - - try { - cl1.getLinks(end2); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'metaclass2' is not navigable and thus cannot be accessed from object " + - "'CL1'", e.getMessage()); - } - - try { - cl2.getLinks(end1); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'metaclass1' is not navigable and thus cannot be accessed from object " + - "'CL2'", e.getMessage()); - } - - try { - cl1.removeAllLinks(end2); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'metaclass2' is not navigable and thus cannot be accessed from object " + - "'CL1'", e.getMessage()); - } - - try { - cl2.removeAllLinks(end1); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'metaclass1' is not navigable and thus cannot be accessed from object " + - "'CL2'", e.getMessage()); - } - - try { - cl1.removeLink(end2, cl2); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'metaclass2' is not navigable and thus cannot be accessed from object " + - "'CL1'", e.getMessage()); - } - - try { - cl2.removeLink(end1, cl1); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'metaclass1' is not navigable and thus cannot be accessed from object " + - "'CL2'", e.getMessage()); - } - - end1.setNavigable(true); - - try { - cl1.setLink(end2, cl2); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'metaclass2' is not navigable and thus cannot be accessed from object " + - "'CL1'", e.getMessage()); - } - - cl2.setLink(end1, cl1); - - try { - cl1.getLinks(end2); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'metaclass2' is not navigable and thus cannot be accessed from object " + - "'CL1'", e.getMessage()); - } - - assertEquals(Collections.singletonList(cl1), cl2.getLinks(end1)); - - try { - cl1.removeAllLinks(end2); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'metaclass2' is not navigable and thus cannot be accessed from object " + - "'CL1'", e.getMessage()); - } - - cl2.removeAllLinks(end1); - assertEquals(Collections.emptyList(), cl2.getLinks(end1)); - - - try { - cl1.removeLink(end2, cl2); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'metaclass2' is not navigable and thus cannot be accessed from object " + - "'CL1'", e.getMessage()); - } - - cl2.addLink(end1, cl1); - assertEquals(Collections.singletonList(cl1), cl2.getLinks(end1)); - - cl2.removeLink(end1, cl1); - assertEquals(Collections.emptyList(), cl2.getLinks(end1)); - } - - - @Test - public void testOneToOneAssociationUnknown() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociationEnd end1 = mcl1.createEnd("metaclass1Forward", "1"); - CAssociationEnd end1Back = mcl1.createEnd("metaclass1Back", "1"); - model.createAssociation(end1, end1Back); - - CClass cl2 = model.createClass(mcl2, "CL2"); - - try { - cl2.setLinks(end1, Collections.emptyList()); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'metaclass1Forward' is not an association target end of 'CL2'", e - .getMessage()); - } - } - - - @Test - public void testOneToNLink() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociationEnd end1 = mcl1.createEnd("metaclass1", "1"), - end2 = mcl2.createEnd("metaclass2", "*"); - model.createAssociation(end1, end2); - CClass cl1 = model.createClass(mcl1, "CL1"); - CClass cl2a = model.createClass(mcl2, "CL2a"); - CClass cl2b = model.createClass(mcl2, "CL2b"); - - cl1.setLinks(end2, Arrays.asList(cl2a, cl2b)); - - assertEquals(Arrays.asList(cl2a, cl2b), cl1.getLinks(end2)); - assertEquals(Collections.singletonList(cl1), cl2a.getLinks(end1)); - assertEquals(Collections.singletonList(cl1), cl2b.getLinks(end1)); - - cl1.setLinks(end2, Collections.singletonList(cl2a)); - - assertEquals(Collections.singletonList(cl2a), cl1.getLinks(end2)); - assertEquals(Collections.singletonList(cl1), cl2a.getLinks(end1)); - assertEquals(Collections.emptyList(), cl2b.getLinks(end1)); - - cl2b.setLinks(end1, Collections.singletonList(cl1)); - - assertEquals(Arrays.asList(cl2a, cl2b), cl1.getLinks(end2)); - assertEquals(Collections.singletonList(cl1), cl2a.getLinks(end1)); - assertEquals(Collections.singletonList(cl1), cl2b.getLinks(end1)); - } - - @Test - public void testOneToNLinkDelete() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociationEnd end1 = mcl1.createEnd("metaclass1", "1"), - end2 = mcl2.createEnd("metaclass2", "*"); - model.createAssociation(end1, end2); - CClass cl1 = model.createClass(mcl1, "CL1"); - CClass cl2a = model.createClass(mcl2, "CL2a"); - CClass cl2b = model.createClass(mcl2, "CL2b"); - - cl1.removeAllLinks(end2); - - cl1.setLinks(end2, Arrays.asList(cl2a, cl2b)); - - assertEquals(Arrays.asList(cl2a, cl2b), cl1.getLinks(end2)); - assertEquals(Collections.singletonList(cl1), cl2a.getLinks(end1)); - assertEquals(Collections.singletonList(cl1), cl2b.getLinks(end1)); - - cl1.removeAllLinks(end2); - - assertEquals(Collections.emptyList(), cl1.getLinks(end2)); - assertEquals(Collections.emptyList(), cl2a.getLinks(end1)); - assertEquals(Collections.emptyList(), cl2b.getLinks(end1)); - - cl1.setLinks(end2, Arrays.asList(cl2a, cl2b)); - - assertEquals(Arrays.asList(cl2a, cl2b), cl1.getLinks(end2)); - assertEquals(Collections.singletonList(cl1), cl2a.getLinks(end1)); - assertEquals(Collections.singletonList(cl1), cl2b.getLinks(end1)); - - cl2a.removeAllLinks(end1); - - assertEquals(Collections.singletonList(cl2b), cl1.getLinks(end2)); - assertEquals(Collections.emptyList(), cl2a.getLinks(end1)); - assertEquals(Collections.singletonList(cl1), cl2b.getLinks(end1)); - } - - @Test - public void testOneTo2NLinkMultiplicity() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociationEnd end1 = mcl1.createEnd("metaclass1", "1"), - end2 = mcl2.createEnd("metaclass2", "2..*"); - model.createAssociation(end1, end2); - CClass cl1 = model.createClass(mcl1, "CL1"); - CClass cl2a = model.createClass(mcl2, "CL2a"); - - try { - cl1.setLinks(end2, Collections.emptyList()); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("link has wrong multiplicity '0', but should be '2..*'", e.getMessage()); - } - - try { - cl1.setLinks(end2, Collections.singletonList(cl2a)); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("link has wrong multiplicity '1', but should be '2..*'", e.getMessage()); - } - } - - - @Test - public void testNToNLink() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociationEnd end1 = mcl1.createEnd("metaclass1", "*"), - end2 = mcl2.createEnd("metaclass2", "*"); - model.createAssociation(end1, end2); - - CClass cl1a = model.createClass(mcl1, "CL1a"); - CClass cl1b = model.createClass(mcl1, "CL1b"); - CClass cl1c = model.createClass(mcl1, "CL1c"); - CClass cl2a = model.createClass(mcl2, "CL2a"); - CClass cl2b = model.createClass(mcl2, "CL2b"); - - cl1a.setLinks(end2, Arrays.asList(cl2a, cl2b)); - cl1b.setLinks(end2, Collections.singletonList(cl2a)); - cl1c.setLinks(end2, Collections.singletonList(cl2b)); - - assertEquals(Arrays.asList(cl2a, cl2b), cl1a.getLinks(end2)); - assertEquals(Collections.singletonList(cl2a), cl1b.getLinks(end2)); - assertEquals(Collections.singletonList(cl2b), cl1c.getLinks(end2)); - assertEquals(Arrays.asList(cl1a, cl1b), cl2a.getLinks(end1)); - assertEquals(Arrays.asList(cl1a, cl1c), cl2b.getLinks(end1)); - - cl2a.setLinks(end1, Arrays.asList(cl1a, cl1b)); - cl2b.setLinks(end1, Collections.emptyList()); - - assertEquals(Collections.singletonList(cl2a), cl1a.getLinks(end2)); - assertEquals(Collections.singletonList(cl2a), cl1b.getLinks(end2)); - assertEquals(Collections.emptyList(), cl1c.getLinks(end2)); - assertEquals(Arrays.asList(cl1a, cl1b), cl2a.getLinks(end1)); - assertEquals(Collections.emptyList(), cl2b.getLinks(end1)); - } - - @Test - public void testAssociationGetLinks() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociationEnd end1 = mcl1.createEnd("metaclass1", "*"), - end2 = mcl2.createEnd("metaclass2", "*"); - CAssociation association = model.createAssociation(end1, end2); - - CClass cl1a = model.createClass(mcl1, "CL1a"); - CClass cl1b = model.createClass(mcl1, "CL1b"); - CClass cl1c = model.createClass(mcl1, "CL1c"); - CClass cl2a = model.createClass(mcl2, "CL2a"); - CClass cl2b = model.createClass(mcl2, "CL2b"); - - cl1a.setLinks(end2, Arrays.asList(cl2a, cl2b)); - cl1b.setLinks(end2, Collections.singletonList(cl2a)); - cl1c.setLinks(end2, Collections.singletonList(cl2b)); - - List links = association.getLinks(); - - assertEquals(4, links.size()); - assertEquals(Arrays.asList(cl1a, cl2a), links.get(0).getLinkedObjects()); - assertEquals(Arrays.asList(cl1a, cl2b), links.get(1).getLinkedObjects()); - assertEquals(Arrays.asList(cl1b, cl2a), links.get(2).getLinkedObjects()); - assertEquals(Arrays.asList(cl1c, cl2b), links.get(3).getLinkedObjects()); - } - - @Test - public void testNToNLinkRemoveAll() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociationEnd end1 = mcl1.createEnd("metaclass1", "*"), - end2 = mcl2.createEnd("metaclass2", "*"); - model.createAssociation(end1, end2); - CClass cl1a = model.createClass(mcl1, "CL1a"); - CClass cl1b = model.createClass(mcl1, "CL1b"); - CClass cl1c = model.createClass(mcl1, "CL1c"); - CClass cl2a = model.createClass(mcl2, "CL2a"); - CClass cl2b = model.createClass(mcl2, "CL2b"); - - cl1a.setLinks(end2, Arrays.asList(cl2a, cl2b)); - cl1b.setLinks(end2, Collections.singletonList(cl2a)); - cl1c.setLinks(end2, Collections.singletonList(cl2b)); - - assertEquals(Arrays.asList(cl2a, cl2b), cl1a.getLinks(end2)); - assertEquals(Collections.singletonList(cl2a), cl1b.getLinks(end2)); - assertEquals(Collections.singletonList(cl2b), cl1c.getLinks(end2)); - assertEquals(Arrays.asList(cl1a, cl1b), cl2a.getLinks(end1)); - assertEquals(Arrays.asList(cl1a, cl1c), cl2b.getLinks(end1)); - - cl2b.removeAllLinks(end1); - - assertEquals(Collections.singletonList(cl2a), cl1a.getLinks(end2)); - assertEquals(Collections.singletonList(cl2a), cl1b.getLinks(end2)); - assertEquals(Collections.emptyList(), cl1c.getLinks(end2)); - assertEquals(Arrays.asList(cl1a, cl1b), cl2a.getLinks(end1)); - assertEquals(Collections.emptyList(), cl2b.getLinks(end1)); - } - - @Test - public void testNToNLinkRemoveLinks() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociationEnd end1 = mcl1.createEnd("metaclass1", "*"), - end2 = mcl2.createEnd("metaclass2", "*"); - model.createAssociation(end1, end2); - CClass cl1a = model.createClass(mcl1, "CL1a"); - CClass cl1b = model.createClass(mcl1, "CL1b"); - CClass cl1c = model.createClass(mcl1, "CL1c"); - CClass cl2a = model.createClass(mcl2, "CL2a"); - CClass cl2b = model.createClass(mcl2, "CL2b"); - - cl1a.setLinks(end2, Arrays.asList(cl2a, cl2b)); - cl1b.setLinks(end2, Collections.singletonList(cl2a)); - cl1c.setLinks(end2, Collections.singletonList(cl2b)); - - assertEquals(Arrays.asList(cl2a, cl2b), cl1a.getLinks(end2)); - assertEquals(Collections.singletonList(cl2a), cl1b.getLinks(end2)); - assertEquals(Collections.singletonList(cl2b), cl1c.getLinks(end2)); - assertEquals(Arrays.asList(cl1a, cl1b), cl2a.getLinks(end1)); - assertEquals(Arrays.asList(cl1a, cl1c), cl2b.getLinks(end1)); - - cl2b.removeLinks(end1, Arrays.asList(cl1a, "CL1c")); - cl1a.removeLink(end2, "CL2a"); - - assertEquals(Collections.emptyList(), cl1a.getLinks(end2)); - assertEquals(Collections.singletonList(cl2a), cl1b.getLinks(end2)); - assertEquals(Collections.emptyList(), cl1c.getLinks(end2)); - assertEquals(Collections.singletonList(cl1b), cl2a.getLinks(end1)); - assertEquals(Collections.emptyList(), cl2b.getLinks(end1)); - } - - - @Test - public void testNToNSetSelfLink() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - - CAssociationEnd superEnd = mcl1.createEnd("super", "*"), - subEnd = mcl1.createEnd("sub", "*"); - model.createAssociation(superEnd, subEnd); - - CClass top = model.createClass(mcl1, "Top"); - CClass mid1 = model.createClass(mcl1, "Mid1"); - CClass mid2 = model.createClass(mcl1, "Mid2"); - CClass mid3 = model.createClass(mcl1, "Mid3"); - CClass bottom1 = model.createClass(mcl1, "Bottom1"); - CClass bottom2 = model.createClass(mcl1, "Bottom2"); - - top.setLinks(subEnd, Arrays.asList(mid1, mid2, mid3)); - mid1.setLinks(subEnd, Arrays.asList(bottom1, bottom2)); - - assertEquals(Collections.emptyList(), top.getLinks(superEnd)); - assertEquals(Arrays.asList(mid1, mid2, mid3), top.getLinks(subEnd)); - assertEquals(Collections.singletonList(top), mid1.getLinks(superEnd)); - assertEquals(Arrays.asList(bottom1, bottom2), mid1.getLinks(subEnd)); - assertEquals(Collections.singletonList(top), mid2.getLinks(superEnd)); - assertEquals(Collections.emptyList(), mid2.getLinks(subEnd)); - assertEquals(Collections.singletonList(top), mid3.getLinks(superEnd)); - assertEquals(Collections.emptyList(), mid3.getLinks(subEnd)); - assertEquals(Collections.singletonList(mid1), bottom1.getLinks(superEnd)); - assertEquals(Collections.emptyList(), bottom1.getLinks(subEnd)); - assertEquals(Collections.singletonList(mid1), bottom2.getLinks(superEnd)); - assertEquals(Collections.emptyList(), bottom2.getLinks(subEnd)); - } - - @Test - public void testNToNAddSelfLink() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CAssociationEnd superEnd = mcl1.createEnd("super", "*"), - subEnd = mcl1.createEnd("sub", "*"); - model.createAssociation(superEnd, subEnd); - - CClass top = model.createClass(mcl1, "Top"); - CClass mid1 = model.createClass(mcl1, "Mid1"); - CClass mid2 = model.createClass(mcl1, "Mid2"); - CClass mid3 = model.createClass(mcl1, "Mid3"); - CClass bottom1 = model.createClass(mcl1, "Bottom1"); - CClass bottom2 = model.createClass(mcl1, "Bottom2"); - - top.addLinks(subEnd, Arrays.asList(mid1, mid2, mid3)); - mid1.addLinks(subEnd, Arrays.asList(bottom1, bottom2)); - - assertEquals(Collections.emptyList(), top.getLinks(superEnd)); - assertEquals(Arrays.asList(mid1, mid2, mid3), top.getLinks(subEnd)); - assertEquals(Collections.singletonList(top), mid1.getLinks(superEnd)); - assertEquals(Arrays.asList(bottom1, bottom2), mid1.getLinks(subEnd)); - assertEquals(Collections.singletonList(top), mid2.getLinks(superEnd)); - assertEquals(Collections.emptyList(), mid2.getLinks(subEnd)); - assertEquals(Collections.singletonList(top), mid3.getLinks(superEnd)); - assertEquals(Collections.emptyList(), mid3.getLinks(subEnd)); - assertEquals(Collections.singletonList(mid1), bottom1.getLinks(superEnd)); - assertEquals(Collections.emptyList(), bottom1.getLinks(subEnd)); - assertEquals(Collections.singletonList(mid1), bottom2.getLinks(superEnd)); - assertEquals(Collections.emptyList(), bottom2.getLinks(subEnd)); - - } - - - @Test - public void testUnknownEnd() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociationEnd end1 = mcl1.createEnd("metaclass1", "1"), - end2 = mcl2.createEnd("metaclass2", "*"), - end3 = mcl2.createEnd("metaclass2", "*"); - model.createAssociation(end1, end2); - - CClass cl2 = model.createClass(mcl2, "CL2"); - CClass cl1 = model.createClass(mcl1, "CL1"); - try { - cl1.setLink(end3, cl2); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'metaclass2' is not an association target end of 'CL1'", e.getMessage()); - } - try { - cl1.setLinks(end3, Collections.singletonList(cl2)); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'metaclass2' is not an association target end of 'CL1'", e.getMessage()); - } - cl1.setLink(end2, cl2); - - try { - assertEquals(Collections.singletonList(cl2), cl1.getLinks(end3)); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'metaclass2' is not an association target end of 'CL1'", e.getMessage()); - } - assertEquals(Collections.singletonList(cl2), cl1.getLinks(end2)); - - try { - cl1.removeAllLinks(end3); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'metaclass2' is not an association target end of 'CL1'", e.getMessage()); - } - } - - @Test - public void testSetOneToOneLinkInheritance1() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CMetaclass mcl2Sub = model.createMetaclass("MCL2Sub").addSuperclass(mcl2); - CAssociation association = model.createAssociation(mcl1.createEnd("metaclass1", "1"), - mcl2.createEnd("metaclass2", "1")); - CAssociationEnd mcl1End = association.getEndByRoleName("metaclass1"); - CAssociationEnd mcl2End = association.getEndByRoleName("metaclass2"); - - CClass cl2 = model.createClass(mcl2Sub, "CL2"); - CClass cl1 = model.createClass(mcl1, "CL1"); - cl1.setLink(mcl2End, cl2); - CClass cl3 = model.createClass(mcl2Sub, "CL3"); - - assertEquals(Collections.singletonList(cl2), cl1.getLinks(mcl2End)); - assertEquals(Collections.singletonList(cl1), cl2.getLinks(mcl1End)); - - cl1.setLink(association.getEndByClassifier(mcl2), "CL3"); - - assertEquals(Collections.singletonList(cl3), cl1.getLinks(mcl2End)); - assertEquals(Collections.singletonList(cl1), cl3.getLinks(mcl1End)); - } - - - @Test - public void testSetOneToOneLinkInheritance2() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CMetaclass mcl1Sub = model.createMetaclass("MCL1Sub").addSuperclass(mcl1); - CMetaclass mcl2Sub = model.createMetaclass("MCL2Sub").addSuperclass(mcl2).addSuperclass(mcl1); - CAssociation association = model.createAssociation(mcl1.createEnd("metaclass1", "1"), - mcl2.createEnd("metaclass2", "1")); - CAssociationEnd mcl1End = association.getEndByRoleName("metaclass1"); - CAssociationEnd mcl2End = association.getEndByRoleName("metaclass2"); - - CClass cl2 = model.createClass(mcl2Sub, "CL2"); - CClass cl1 = model.createClass(mcl1Sub, "CL1"); - cl1.setLink(mcl2End, cl2); - CClass cl3 = model.createClass(mcl2Sub, "CL3"); - - assertEquals(Collections.singletonList(cl2), cl1.getLinks(mcl2End)); - assertEquals(Collections.singletonList(cl1), cl2.getLinks(mcl1End)); - - cl1.setLink(association.getEndByClassifier(mcl2), "CL3"); - - assertEquals(Collections.singletonList(cl3), cl1.getLinks(mcl2End)); - assertEquals(Collections.singletonList(cl1), cl3.getLinks(mcl1End)); - } - - - @Test - public void testOneToNLinkDeleteInheritance() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CMetaclass mcl1Sub = model.createMetaclass("MCL1Sub").addSuperclass(mcl1); - CMetaclass mcl2Sub = model.createMetaclass("MCL2Sub").addSuperclass(mcl2); - CAssociationEnd end1 = mcl1.createEnd("metaclass1", "1"), - end2 = mcl2.createEnd("metaclass2", "*"); - model.createAssociation(end1, end2); - CClass cl1 = model.createClass(mcl1Sub, "CL1"); - CClass cl2a = model.createClass(mcl2Sub, "CL2a"); - CClass cl2b = model.createClass(mcl2Sub, "CL2b"); - - cl1.removeAllLinks(end2); - - cl1.setLinks(end2, Arrays.asList(cl2a, cl2b)); - - assertEquals(Arrays.asList(cl2a, cl2b), cl1.getLinks(end2)); - assertEquals(Collections.singletonList(cl1), cl2a.getLinks(end1)); - assertEquals(Collections.singletonList(cl1), cl2b.getLinks(end1)); - - cl1.removeAllLinks(end2); - - assertEquals(Collections.emptyList(), cl1.getLinks(end2)); - assertEquals(Collections.emptyList(), cl2a.getLinks(end1)); - assertEquals(Collections.emptyList(), cl2b.getLinks(end1)); - - cl1.setLinks(end2, Arrays.asList(cl2a, cl2b)); - - assertEquals(Arrays.asList(cl2a, cl2b), cl1.getLinks(end2)); - assertEquals(Collections.singletonList(cl1), cl2a.getLinks(end1)); - assertEquals(Collections.singletonList(cl1), cl2b.getLinks(end1)); - - cl2a.removeAllLinks(end1); - - assertEquals(Collections.singletonList(cl2b), cl1.getLinks(end2)); - assertEquals(Collections.emptyList(), cl2a.getLinks(end1)); - assertEquals(Collections.singletonList(cl1), cl2b.getLinks(end1)); - } - - @Test - public void testNToNLinkRemoveAllInheritance() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CMetaclass mcl1Sub = model.createMetaclass("MCL1Sub").addSuperclass(mcl1); - CMetaclass mcl2Sub = model.createMetaclass("MCL2Sub").addSuperclass(mcl2); - CAssociationEnd end1 = mcl1.createEnd("metaclass1", "*"), - end2 = mcl2.createEnd("metaclass2", "*"); - model.createAssociation(end1, end2); - CClass cl1a = model.createClass(mcl1Sub, "CL1a"); - CClass cl1b = model.createClass(mcl1Sub, "CL1b"); - CClass cl1c = model.createClass(mcl1Sub, "CL1c"); - CClass cl2a = model.createClass(mcl2Sub, "CL2a"); - CClass cl2b = model.createClass(mcl2Sub, "CL2b"); - - cl1a.setLinks(end2, Arrays.asList(cl2a, cl2b)); - cl1b.setLinks(end2, Collections.singletonList(cl2a)); - cl1c.setLinks(end2, Collections.singletonList(cl2b)); - - assertEquals(Arrays.asList(cl2a, cl2b), cl1a.getLinks(end2)); - assertEquals(Collections.singletonList(cl2a), cl1b.getLinks(end2)); - assertEquals(Collections.singletonList(cl2b), cl1c.getLinks(end2)); - assertEquals(Arrays.asList(cl1a, cl1b), cl2a.getLinks(end1)); - assertEquals(Arrays.asList(cl1a, cl1c), cl2b.getLinks(end1)); - - cl2b.removeAllLinks(end1); - - assertEquals(Collections.singletonList(cl2a), cl1a.getLinks(end2)); - assertEquals(Collections.singletonList(cl2a), cl1b.getLinks(end2)); - assertEquals(Collections.emptyList(), cl1c.getLinks(end2)); - assertEquals(Arrays.asList(cl1a, cl1b), cl2a.getLinks(end1)); - assertEquals(Collections.emptyList(), cl2b.getLinks(end1)); - } - - @Test - public void testGetLinkObjects() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl1Sub = model.createMetaclass("MCL1SUB").addSuperclass(mcl1); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CMetaclass mcl2Sub = model.createMetaclass("MCL2SUB").addSuperclass(mcl2); - CAssociationEnd end1 = mcl1.createEnd("metaclass1", "1"), - end2 = mcl2.createEnd("metaclass2", "1"); - CAssociation association1 = model.createAssociation(end1, end2); - - CAssociationEnd end3 = mcl1.createEnd("prior", "1"), - end4 = mcl1.createEnd("next", "1"); - CAssociation association2 = model.createAssociation(end3, end4); - - CClass cl1 = model.createClass(mcl1, "CL1"); - CClass cl2 = model.createClass(mcl2, "CL2"); - CClass cl_mcl2Sub = model.createClass(mcl2Sub, "CL_MCL2SUB"); - CClass cl_mcl1Sub = model.createClass(mcl1Sub, "CL_MCL1SUB"); - - CLink link, linkOrig; - - linkOrig = cl1.setLink(end2, cl2); - link = cl1.getLinkObjects(end2).get(0); - assertEquals(linkOrig, link); - assertEquals(Arrays.asList(cl1, cl2), link.getLinkedObjects()); - - linkOrig = cl1.setLink(end2, cl_mcl2Sub); - link = cl1.getLinkObjects(end2).get(0); - assertEquals(linkOrig, link); - assertEquals(Arrays.asList(cl1, cl_mcl2Sub), link.getLinkedObjects()); - - linkOrig = cl1.setLink(end2, cl2); - link = cl1.getLinkObjects(association1).get(0); - assertEquals(linkOrig, link); - assertEquals(Arrays.asList(cl1, cl2), link.getLinkedObjects()); - - linkOrig = cl1.setLink(end2, cl_mcl2Sub); - link = cl1.getLinkObjects(association1).get(0); - assertEquals(linkOrig, link); - assertEquals(Arrays.asList(cl1, cl_mcl2Sub), link.getLinkedObjects()); - - linkOrig = cl1.setLink(end4, cl1); - link = cl1.getLinkObjects(end4).get(0); - assertEquals(linkOrig, link); - assertEquals(Arrays.asList(cl1, cl1), link.getLinkedObjects()); - - linkOrig = cl1.setLink(end4, cl_mcl1Sub); - link = cl1.getLinkObjects(end4).get(0); - assertEquals(linkOrig, link); - assertEquals(Arrays.asList(cl1, cl_mcl1Sub), link.getLinkedObjects()); - - linkOrig = cl1.setLink(end4, cl1); - link = cl1.getLinkObjects(association2).get(0); - assertEquals(linkOrig, link); - assertEquals(Arrays.asList(cl1, cl1), link.getLinkedObjects()); - - cl1.removeAllLinks(end4); - - linkOrig = cl_mcl1Sub.setLink(end4, cl1); - link = cl_mcl1Sub.getLinkObjects(association2).get(0); - assertEquals(linkOrig, link); - assertEquals(Arrays.asList(cl_mcl1Sub, cl1), link.getLinkedObjects()); - } - - @Test - public void testGetLinkObjectsSelfLink() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CAssociationEnd end1 = mcl1.createEnd("from", "*"), - end2 = mcl1.createEnd("to", "*"); - CAssociation association = model.createAssociation(end1, end2); - - CClass cl1 = model.createClass(mcl1, "CL1"); - CClass cl2 = model.createClass(mcl1, "CL2"); - - CLink link, linkOrig; - - // get link objects via association - linkOrig = cl1.setLink(end2, cl2); - link = cl1.getLinkObjects(association).get(0); - assertEquals(linkOrig, link); - assertEquals(Arrays.asList(cl1, cl2), link.getLinkedObjects()); - - // get link objects via end - linkOrig = cl1.setLink(end2, cl2); - link = cl1.getLinkObjects(end2).get(0); - assertEquals(linkOrig, link); - assertEquals(Arrays.asList(cl1, cl2), link.getLinkedObjects()); - - // test the same with the other end - CClass cl3 = model.createClass(mcl1, "CL3"); - CClass cl4 = model.createClass(mcl1, "CL4"); - - // get link objects via association - linkOrig = cl3.setLink(end1, cl4); - link = cl3.getLinkObjects(association).get(0); - assertEquals(linkOrig, link); - assertEquals(Arrays.asList(cl4, cl3), link.getLinkedObjects()); - - // get link objects via end - linkOrig = cl3.setLink(end1, cl4); - link = cl3.getLinkObjects(end1).get(0); - assertEquals(linkOrig, link); - assertEquals(Arrays.asList(cl4, cl3), link.getLinkedObjects()); - - } - - @Test - public void testGetLinkObjectsSetList() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociationEnd end1 = mcl1.createEnd("metaclass1", "1"), - end2 = mcl2.createEnd("metaclass2", "*"); - model.createAssociation(end1, end2); - - CClass cl1 = model.createClass(mcl1, "CL1"); - CClass cl2a = model.createClass(mcl2, "CL2a"); - CClass cl2b = model.createClass(mcl2, "CL2b"); - - List linksOrig = cl1.setLinks(end2, Arrays.asList(cl2a, cl2b)); - List links = cl1.getLinkObjects(end2); - assertEquals(linksOrig, links); - assertEquals(2, links.size()); - assertEquals(cl2a, links.get(0).getLinkedObjectAtTargetEnd(end2)); - assertEquals(cl2b, links.get(1).getLinkedObjectAtTargetEnd(end2)); - } - - @Test - public void testGetLinkObjectsAddList() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociationEnd end1 = mcl1.createEnd("metaclass1", "1"), - end2 = mcl2.createEnd("metaclass2", "*"); - model.createAssociation(end1, end2); - - CClass cl1 = model.createClass(mcl1, "CL1"); - CClass cl2a = model.createClass(mcl2, "CL2a"); - CClass cl2b = model.createClass(mcl2, "CL2b"); - - List linksOrig = cl1.addLinks(end2, Arrays.asList(cl2a, cl2b)); - List links = cl1.getLinkObjects(end2); - assertEquals(linksOrig, links); - assertEquals(2, links.size()); - assertEquals(cl2a, links.get(0).getLinkedObjectAtTargetEnd(end2)); - assertEquals(cl2b, links.get(1).getLinkedObjectAtTargetEnd(end2)); - } -} diff --git a/src/codeableModels/tests/TestsClassifier.java b/src/codeableModels/tests/TestsClassifier.java deleted file mode 100644 index 555cc0c..0000000 --- a/src/codeableModels/tests/TestsClassifier.java +++ /dev/null @@ -1,148 +0,0 @@ -package codeableModels.tests; - -import codeableModels.*; -import org.junit.*; - -import java.util.*; - -import static org.junit.Assert.*; - -public class TestsClassifier { - - @Test - public void getClassifiers() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass c1 = model.createMetaclass("Metaclass1"); - CMetaclass c2 = model.createMetaclass("Metaclass2"); - CClass c3 = model.createClass(c1, "Class1"); - CClass c4 = model.createClass(c2, "Class2"); - CStereotype c5 = model.createStereotype("Stereotype1"); - - List results = model.getClassifiers(); - - assertEquals(5, results.size()); - assertTrue(results.contains(c1)); - assertTrue(results.contains(c2)); - assertTrue(results.contains(c3)); - assertTrue(results.contains(c4)); - assertTrue(results.contains(c5)); - } - - @Test - public void getClassifierNames() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass c1 = model.createMetaclass("Metaclass1"); - CMetaclass c2 = model.createMetaclass("Metaclass2"); - model.createClass(c1, "Class1"); - model.createClass(c2, "Class2"); - model.createStereotype("Stereotype1"); - - List results = model.getClassifierNames(); - - assertEquals(5, results.size()); - assertTrue(results.contains("Metaclass1")); - assertTrue(results.contains("Metaclass2")); - assertTrue(results.contains("Class2")); - assertTrue(results.contains("Class1")); - assertTrue(results.contains("Stereotype1")); - } - - @Test - public void asMetaclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass c1 = model.createMetaclass("Metaclass1"); - CClass c3 = model.createClass(c1, "Class1"); - CStereotype c5 = model.createStereotype("Stereotype1"); - - assertEquals(c1, model.getClassifier("Metaclass1").asMetaclass()); - try { - assertEquals(c3, model.getClassifier("Class1").asMetaclass()); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("'Class1' is not a metaclass", e.getMessage()); - } - - try { - assertEquals(c5, model.getClassifier("Stereotype1").asMetaclass()); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("'Stereotype1' is not a metaclass", e.getMessage()); - } - } - - @Test - public void asClass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass c1 = model.createMetaclass("Metaclass1"); - CClass c3 = model.createClass(c1, "Class1"); - CStereotype c5 = model.createStereotype("Stereotype1"); - - try { - assertEquals(c1, model.getClassifier("Metaclass1").asClass()); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("'Metaclass1' is not a class", e.getMessage()); - } - - assertEquals(c3, model.getClassifier("Class1").asClass()); - - try { - assertEquals(c5, model.getClassifier("Stereotype1").asClass()); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("'Stereotype1' is not a class", e.getMessage()); - } - } - - @Test - public void asStereotype() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass c1 = model.createMetaclass("Metaclass1"); - CClass c3 = model.createClass(c1, "Class1"); - CStereotype c5 = model.createStereotype("Stereotype1"); - - try { - assertEquals(c1, model.getClassifier("Metaclass1").asStereotype()); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("'Metaclass1' is not a stereotype", e.getMessage()); - } - - try { - assertEquals(c3, model.getClassifier("Class1").asStereotype()); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("'Class1' is not a stereotype", e.getMessage()); - } - - assertEquals(c5, model.getClassifier("Stereotype1").asStereotype()); - } - - - @Test - public void hasSuperclassHasSubclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("Metaclass1"); - CMetaclass mcl2 = model.createMetaclass("Metaclass2").addSuperclass(mcl1); - CMetaclass mcl3 = model.createMetaclass("Metaclass3").addSuperclass(mcl2); - CMetaclass mcl4 = model.createMetaclass("Metaclass4").addSuperclass(mcl2); - CMetaclass mcl5 = model.createMetaclass("Metaclass5"); - - assertEquals(false, mcl1.hasSuperclass(mcl2)); - assertEquals(false, mcl1.hasSuperclass((CClassifier) null)); - assertEquals(true, mcl2.hasSuperclass(mcl1)); - assertEquals(true, mcl3.hasSuperclass("Metaclass1")); - assertEquals(true, mcl3.hasSuperclass(mcl2)); - assertEquals(true, mcl4.hasSuperclass(mcl2)); - assertEquals(false, mcl5.hasSuperclass("Metaclass2")); - assertEquals(false, mcl5.hasSuperclass("")); - - assertEquals(false, mcl3.hasSubclass(mcl2)); - assertEquals(false, mcl3.hasSubclass((CClassifier) null)); - assertEquals(false, mcl2.hasSubclass("Metaclass1")); - assertEquals(true, mcl1.hasSubclass(mcl3)); - assertEquals(true, mcl1.hasSubclass(mcl2)); - assertEquals(false, mcl5.hasSubclass("Metaclass2")); - assertEquals(false, mcl5.hasSubclass("")); - } -} diff --git a/src/codeableModels/tests/TestsImportModel.java b/src/codeableModels/tests/TestsImportModel.java deleted file mode 100644 index 6516762..0000000 --- a/src/codeableModels/tests/TestsImportModel.java +++ /dev/null @@ -1,277 +0,0 @@ -package codeableModels.tests; - -import codeableModels.*; -import org.junit.*; - -import java.util.*; - -import static org.junit.Assert.*; - -public class TestsImportModel { - - private static CModel buildModel1() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClassifier a = model.createClass(mcl, "A"); - CClassifier b = model.createClass(mcl, "B"); - CClassifier c = model.createClass(mcl, "C"); - model.createAssociation(c.createEnd("c", "*"), a.createEnd("a", "1")); - model.createAssociation(a.createEnd("a", "*"), b.createEnd("b", "1")); - model.createClass(mcl, "X").addSuperclass("A"); - return model; - } - - private static CModel buildMetaModel1() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass a = model.createMetaclass("A"); - model.createStereotype("Stereotype1"); - a.addStereotype("Stereotype1"); - CMetaclass b = model.createMetaclass("B"); - model.createStereotype("Stereotype2"); - b.addStereotype("Stereotype2"); - CMetaclass c = model.createMetaclass("C"); - model.createAssociation(c.createEnd("c", "*"), a.createEnd("a", "1")); - model.createAssociation(a.createEnd("a", "*"), b.createEnd("b", "1")); - model.createMetaclass("X").addSuperclass("A"); - return model; - } - - @Test - public void testImportModel() throws CException { - CModel model1 = buildModel1(); - CModel model = CodeableModels.createModel(); - model.importModel(model1); - CObject xObj = model.createObject("X", "x"); - CClassifier xClass = model1.getClassifier("X"); - CClassifier aClass = model1.getClassifier("A"); - - assertEquals(Collections.singletonList(model1), model.getImportedModels()); - assertEquals(Arrays.asList(model, model1), model.getFullModelList()); - assertEquals(xClass, xObj.getClassifier()); - assertEquals(2, aClass.getAssociations().size()); - assertEquals(1, xClass.getSuperclasses().size()); - assertEquals(aClass, xClass.getSuperclasses().get(0)); - } - - @Test - public void testImportModelLinks() throws CException { - CModel model1 = buildModel1(); - CModel model = CodeableModels.createModel(); - model.importModel(model1); - CObject aObj = model.createObject("A", "a"); - CObject bObj = model.createObject("B", "b"); - CAssociation association = bObj.getClassifier().getAssociationByRoleName("a"); - CAssociationEnd aEnd = association.getEndByRoleName("a"); - CAssociationEnd bEnd = association.getEndByRoleName("b"); - bObj.addLink(aEnd, aObj); - assertEquals(Collections.singletonList(bObj), aObj.getLinks(bEnd)); - assertEquals(Collections.singletonList(aObj), bObj.getLinks(aEnd)); - } - - @Test - public void testImportImportedModelLinks() throws CException { - CModel model1 = buildModel1(); - CModel model2 = CodeableModels.createModel().importModel(model1); - model2.createClass("MCL","BSub").addSuperclass("B"); - CModel model = CodeableModels.createModel(); - model.importModel(model2); - CObject aObj = model.createObject("A", "a"); - CObject bObj = model.createObject("BSub", "b"); - CAssociation association = bObj.getClassifier().getAssociationByRoleName("a"); - CAssociationEnd aEnd = association.getEndByRoleName("a"); - CAssociationEnd bEnd = association.getEndByRoleName("b"); - bObj.addLink(aEnd, aObj); - assertEquals(Collections.singletonList(bObj), aObj.getLinks(bEnd)); - assertEquals(Collections.singletonList(aObj), bObj.getLinks(aEnd)); - } - - @Test - public void testImportMultipleModels() throws CException { - CModel model1 = CodeableModels.createModel(); - CModel model2 = CodeableModels.createModel().importModel(model1); - CModel model3 = CodeableModels.createModel(); - CModel model4 = CodeableModels.createModel().importModel(model2).importModel(model3); - - model1.createMetaclass("MCL"); - model2.createClass("MCL", "CL"); - model3.createMetaclass("MCL2"); - model4.createClass("MCL", "CL2"); - model4.createClass("MCL2", "CL3"); - model4.createObject("CL", "O"); - - assertEquals(Collections.singletonList(model1), model2.getImportedModels()); - assertEquals(Collections.emptyList(), model3.getImportedModels()); - assertEquals(Arrays.asList(model2, model3), model4.getImportedModels()); - - assertEquals(Arrays.asList(model2, model1), model2.getFullModelList()); - assertEquals(Collections.singletonList(model3), model3.getFullModelList()); - assertEquals(Arrays.asList(model4, model2, model1, model3), model4.getFullModelList()); - } - - @Test - public void testImportImportedModel() throws CException { - CModel model1 = buildModel1(); - - // import model - CModel model2 = CodeableModels.createModel().importModel(model1); - CMetaclass mcl = model2.createMetaclass("MCL"); - model2.createClass(mcl, "Y").addSuperclass("B"); - - // import imported model - CModel model = CodeableModels.createModel(); - model.importModel(model2); - CObject xObj = model.createObject("X", "x"); - CObject yObj = model.createObject("Y", "y"); - CClassifier xClass = model.lookupClass("X"); - CClassifier yClass = model.lookupClass("Y"); - CClassifier aClass = model.lookupClass("A"); - CClassifier bClass = model.lookupClass("B"); - - assertEquals(xClass, xObj.getClassifier()); - assertEquals(yClass, yObj.getClassifier()); - assertEquals(2, aClass.getAssociations().size()); - assertEquals(1, xClass.getSuperclasses().size()); - assertEquals(1, yClass.getSuperclasses().size()); - assertEquals(aClass, xClass.getSuperclasses().get(0)); - assertEquals(bClass, yClass.getSuperclasses().get(0)); - - CClassifier x2Class = model1.lookupClass("X"); - CClassifier y2Class = model1.lookupClass("Y"); - CClassifier a2Class = model1.lookupClass("A"); - CClassifier x3Class = model2.lookupClass("X"); - CClassifier y3Class = model2.lookupClass("Y"); - CClassifier a3Class = model2.lookupClass("A"); - - assertEquals(xClass, x2Class); - assertEquals(xClass, x3Class); - assertEquals(null, y2Class); - assertEquals(yClass, y3Class); - assertEquals(aClass, a2Class); - assertEquals(aClass, a3Class); - } - - @Test - public void testImportMetaModel() throws CException { - CModel model1 = buildMetaModel1(); - CModel model = CodeableModels.createModel(); - model.importModel(model1); - CClass xClass = model.createClass("X", "x"); - CClassifier xMetaclass = model1.getClassifier("X"); - CClassifier aMetaclass = model1.getClassifier("A"); - - assertEquals(xMetaclass, xClass.getMetaclass()); - assertEquals(2, aMetaclass.getAssociations().size()); - assertEquals(1, xMetaclass.getSuperclasses().size()); - assertEquals(aMetaclass, xMetaclass.getSuperclasses().get(0)); - } - - @Test - public void testImportMetaModelLinks() throws CException { - CModel model1 = buildMetaModel1(); - CModel model = CodeableModels.createModel(); - model.importModel(model1); - CClass aClass = model.createClass("A", "a"); - CClass bClass = model.createClass("B", "b"); - CAssociation association = bClass.getClassifier().getAssociationByRoleName("a"); - CAssociationEnd aAssociationEnd = association.getEndByClassifier(aClass.getClassifier()); - CAssociationEnd bAssociationEnd = association.getEndByClassifier(bClass.getClassifier()); - bClass.addLink(aAssociationEnd, aClass); - assertEquals(Collections.singletonList(bClass), aClass.getLinks(bAssociationEnd)); - assertEquals(Collections.singletonList(aClass), bClass.getLinks(aAssociationEnd)); - } - - - @Test - public void testImportImportedMetaModelLinks() throws CException { - CModel model1 = buildMetaModel1(); - CModel model2 = CodeableModels.createModel().importModel(model1); - CModel model = CodeableModels.createModel(); - model.importModel(model2); - CClass aClass = model.createClass("A", "a"); - CClass bClass = model.createClass("B", "b"); - CAssociation association = bClass.getClassifier().getAssociationByRoleName("a"); - CAssociationEnd aAssociationEnd = association.getEndByClassifier(aClass.getClassifier()); - CAssociationEnd bAssociationEnd = association.getEndByClassifier(bClass.getClassifier()); - bClass.addLink(aAssociationEnd, aClass); - assertEquals(Collections.singletonList(bClass), aClass.getLinks(bAssociationEnd)); - assertEquals(Collections.singletonList(aClass), bClass.getLinks(aAssociationEnd)); - } - - @Test - public void testImportImportedMetaModel() throws CException { - CModel model1 = buildMetaModel1(); - - // import model - CModel model2 = CodeableModels.createModel().importModel(model1); - model2.createMetaclass("Y").addSuperclass("B"); - - // import imported model - CModel model = CodeableModels.createModel(); - model.importModel(model2); - CClass xClass = model.createClass("X", "x"); - CClass yClass = model.createClass("Y", "y"); - CClassifier xMetaclass = model.lookupMetaclass("X"); - CClassifier yMetaclass = model.lookupMetaclass("Y"); - CClassifier aMetaclass = model.lookupMetaclass("A"); - CClassifier bMetaclass = model.lookupMetaclass("B"); - - assertEquals(xMetaclass, xClass.getMetaclass()); - assertEquals(yMetaclass, yClass.getMetaclass()); - assertEquals(2, aMetaclass.getAssociations().size()); - assertEquals(1, xMetaclass.getSuperclasses().size()); - assertEquals(1, yMetaclass.getSuperclasses().size()); - assertEquals(aMetaclass, xMetaclass.getSuperclasses().get(0)); - assertEquals(bMetaclass, yMetaclass.getSuperclasses().get(0)); - - CClassifier x2Class = model1.lookupMetaclass("X"); - CClassifier y2Class = model1.lookupMetaclass("Y"); - CClassifier a2Class = model1.lookupMetaclass("A"); - CClassifier x3Class = model2.lookupMetaclass("X"); - CClassifier y3Class = model2.lookupMetaclass("Y"); - CClassifier a3Class = model2.lookupMetaclass("A"); - - assertEquals(xMetaclass, x2Class); - assertEquals(xMetaclass, x3Class); - assertEquals(null, y2Class); - assertEquals(yMetaclass, y3Class); - assertEquals(aMetaclass, a2Class); - assertEquals(aMetaclass, a3Class); - } - - - @Test - public void testImportStereotype() throws CException { - CModel model1 = buildMetaModel1(); - - CModel model2 = CodeableModels.createModel().importModel(model1); - model2.createClass("A", "C1"); - model2.createClass("A", "C2").addStereotypeInstance("Stereotype1"); - - assertEquals(model1.getMetaclass("A").getStereotypes().get(0), model2.getClass("C2").getStereotypeInstances().get(0)); - } - - - @Test - public void testImportStereotypeNameConflict() throws CException { - CModel model1 = buildMetaModel1(); - - CModel model2 = CodeableModels.createModel().importModel(model1); - model2.createClass("A", "M1"); - CClass cl = model2.createClass("A", "Stereotype1"); - - // name conflict ... - try { - cl.addStereotypeInstance("Stereotype1"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("'Stereotype1' is not a stereotype", e.getMessage()); - } - - // ... can be resolved by looking up the object reference - cl.addStereotypeInstance(model1.getStereotype("Stereotype1")); - - assertEquals(model1.getMetaclass("A").getStereotypes().get(0), model2.getClass("Stereotype1").getStereotypeInstances().get(0)); - } - - -} diff --git a/src/codeableModels/tests/TestsInheritanceClass.java b/src/codeableModels/tests/TestsInheritanceClass.java deleted file mode 100644 index c26cab4..0000000 --- a/src/codeableModels/tests/TestsInheritanceClass.java +++ /dev/null @@ -1,382 +0,0 @@ -package codeableModels.tests; - -import codeableModels.*; -import org.junit.*; - -import java.util.*; - -import static org.junit.Assert.*; - -public class TestsInheritanceClass { - - @Test - public void testClassNoInheritance() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass top = model.createClass(mcl, "Top"); - assertEquals(0, top.getSuperclasses().size()); - assertEquals(0, top.getSubclasses().size()); - assertEquals(0, top.getAllSuperclasses().size()); - } - - @Test - public void testClassAddSuperclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass top = model.createClass(mcl, "Top"); - try { - top.addSuperclass("X"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("can't find superclass 'X'", e.getMessage()); - } - } - - @Test - public void testClassSimpleInheritance() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass top = model.createClass(mcl, "Top"); - CClass l1a = model.createClass(mcl, "L1a").addSuperclass("Top"); - CClass l1b = model.createClass(mcl, "L1b").addSuperclass(top); - CClass l2a = model.createClass(mcl, "L2a").addSuperclass("L1a"); - CClass l2b = model.createClass(mcl, "L2b").addSuperclass("L1a"); - CClass l2c = model.createClass(mcl, "L2c").addSuperclass(top); - - assertEquals(3, top.getSubclasses().size()); - assertEquals(0, top.getSuperclasses().size()); - assertEquals(0, top.getAllSuperclasses().size()); - assertEquals(2, l1a.getSubclasses().size()); - assertEquals(1, l1a.getSuperclasses().size()); - assertEquals(1, l1a.getAllSuperclasses().size()); - assertEquals(0, l1b.getSubclasses().size()); - assertEquals(1, l1b.getSuperclasses().size()); - assertEquals(1, l1b.getAllSuperclasses().size()); - assertEquals(0, l2a.getSubclasses().size()); - assertEquals(1, l2a.getSuperclasses().size()); - assertEquals(2, l2a.getAllSuperclasses().size()); - assertEquals(0, l2b.getSubclasses().size()); - assertEquals(1, l2b.getSuperclasses().size()); - assertEquals(2, l2b.getAllSuperclasses().size()); - assertEquals(0, l2c.getSubclasses().size()); - assertEquals(1, l2c.getSuperclasses().size()); - assertEquals(1, l2c.getAllSuperclasses().size()); - - List l1aSuper = l1a.getSuperclasses(); - List l1aSub = l1a.getSubclasses(); - - assertTrue(l1aSuper.contains(top)); - assertTrue(l1aSub.contains(l2a)); - assertTrue(l1aSub.contains(l2b)); - - List l2aAllSuper = l2a.getAllSuperclasses(); - - assertTrue(l2aAllSuper.contains(l1a)); - assertTrue(l2aAllSuper.contains(top)); - } - - @Test - public void testClassInheritanceDoubleAssignment() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass top = model.createClass(mcl, "Top"); - CClass l1a = model.createClass(mcl, "L1a").addSuperclass("Top"); - try { - l1a.addSuperclass(top); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("'Top' is already a superclass of 'L1a'", e.getMessage()); - } - - assertEquals(1, top.getSubclasses().size()); - assertEquals(0, top.getSuperclasses().size()); - assertEquals(0, top.getAllSuperclasses().size()); - assertEquals(0, l1a.getSubclasses().size()); - assertEquals(1, l1a.getSuperclasses().size()); - assertEquals(1, l1a.getAllSuperclasses().size()); - } - - @Test - public void testInheritanceDeleteTopClass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass top = model.createClass(mcl, "Top"); - CClass l1a = model.createClass(mcl, "L1a").addSuperclass("Top"); - CClass l1b = model.createClass(mcl, "L1b").addSuperclass(top); - CClass l2a = model.createClass(mcl, "L2a").addSuperclass("L1a"); - CClass l2b = model.createClass(mcl, "L2b").addSuperclass("L1a"); - CClass l2c = model.createClass(mcl, "L2c").addSuperclass(top); - - model.deleteClassifier(top); - - assertEquals(0, top.getSubclasses().size()); - assertEquals(0, top.getSuperclasses().size()); - assertEquals(0, top.getAllSuperclasses().size()); - assertEquals(2, l1a.getSubclasses().size()); - assertEquals(0, l1a.getSuperclasses().size()); - assertEquals(0, l1a.getAllSuperclasses().size()); - assertEquals(0, l1b.getSubclasses().size()); - assertEquals(0, l1b.getSuperclasses().size()); - assertEquals(0, l1b.getAllSuperclasses().size()); - assertEquals(0, l2a.getSubclasses().size()); - assertEquals(1, l2a.getSuperclasses().size()); - assertEquals(1, l2a.getAllSuperclasses().size()); - assertEquals(0, l2b.getSubclasses().size()); - assertEquals(1, l2b.getSuperclasses().size()); - assertEquals(1, l2b.getAllSuperclasses().size()); - assertEquals(0, l2c.getSubclasses().size()); - assertEquals(0, l2c.getSuperclasses().size()); - assertEquals(0, l2c.getAllSuperclasses().size()); - - List l1aSuper = l1a.getSuperclasses(); - List l1aSub = l1a.getSubclasses(); - - assertTrue(!l1aSuper.contains(top)); - assertTrue(l1aSub.contains(l2a)); - assertTrue(l1aSub.contains(l2b)); - - List l2aAllSuper = l2a.getAllSuperclasses(); - - assertTrue(l2aAllSuper.contains(l1a)); - - } - - @Test - public void testInheritanceDeleteInnerClass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass top = model.createClass(mcl, "Top"); - CClass l1a = model.createClass(mcl, "L1a").addSuperclass("Top"); - CClass l1b = model.createClass(mcl, "L1b").addSuperclass(top); - CClass l2a = model.createClass(mcl, "L2a").addSuperclass("L1a"); - CClass l2b = model.createClass(mcl, "L2b").addSuperclass("L1a"); - CClass l2c = model.createClass(mcl, "L2c").addSuperclass(top); - - model.deleteClassifier(l1a); - - assertEquals(2, top.getSubclasses().size()); - assertEquals(0, top.getSuperclasses().size()); - assertEquals(0, top.getAllSuperclasses().size()); - assertEquals(0, l1a.getSubclasses().size()); - assertEquals(0, l1a.getSuperclasses().size()); - assertEquals(0, l1a.getAllSuperclasses().size()); - assertEquals(0, l1b.getSubclasses().size()); - assertEquals(1, l1b.getSuperclasses().size()); - assertEquals(1, l1b.getAllSuperclasses().size()); - assertEquals(0, l2a.getSubclasses().size()); - assertEquals(0, l2a.getSuperclasses().size()); - assertEquals(0, l2a.getAllSuperclasses().size()); - assertEquals(0, l2b.getSubclasses().size()); - assertEquals(0, l2b.getSuperclasses().size()); - assertEquals(0, l2b.getAllSuperclasses().size()); - assertEquals(0, l2c.getSubclasses().size()); - assertEquals(1, l2c.getSuperclasses().size()); - assertEquals(1, l2c.getAllSuperclasses().size()); - - List l1aSuper = l1a.getSuperclasses(); - List l1aSub = l1a.getSubclasses(); - - assertTrue(!l1aSuper.contains(top)); - assertTrue(!l1aSub.contains(l2a)); - assertTrue(!l1aSub.contains(l2b)); - } - - @Test - public void testDeleteSuperclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass top = model.createClass(mcl, "Top"); - CClass l1a = model.createClass(mcl, "L1a").addSuperclass("Top"); - CClass l1b = model.createClass(mcl, "L1b").addSuperclass(top); - CClass l2a = model.createClass(mcl, "L2a").addSuperclass("L1a"); - CClass l2b = model.createClass(mcl, "L2b").addSuperclass("L1a"); - CClass l2c = model.createClass(mcl, "L2c").addSuperclass(top).addSuperclass("L1a").addSuperclass("L1b"); - CClass x = model.createClass(mcl, "X"); - - try { - top.deleteSuperclass(x); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("can't remove subclass 'Top' from classifier 'X': is not a subclass", e.getMessage()); - } - - try { - l1a.deleteSuperclass("X"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("can't remove subclass 'L1a' from classifier 'X': is not a subclass", e.getMessage()); - } - - l1a.deleteSuperclass("Top"); - l2b.deleteSuperclass(l1a); - l2c.deleteSuperclass(l1a); - - assertEquals(top.getSubclasses(), Arrays.asList(l1b, l2c)); - assertEquals(top.getSuperclasses(), Collections.emptyList()); - assertEquals(top.getAllSuperclasses(), Collections.emptyList()); - - assertEquals(l1a.getSubclasses(), Collections.singletonList(l2a)); - assertEquals(l1a.getSuperclasses(), Collections.emptyList()); - assertEquals(l1a.getAllSuperclasses(), Collections.emptyList()); - - assertEquals(l2b.getSubclasses(), Collections.emptyList()); - assertEquals(l2b.getSuperclasses(), Collections.emptyList()); - assertEquals(l2b.getAllSuperclasses(), Collections.emptyList()); - - assertEquals(l2c.getSubclasses(), Collections.emptyList()); - assertEquals(l2c.getSuperclasses(), Arrays.asList(top, l1b)); - assertEquals(l2c.getAllSuperclasses(), Arrays.asList(top, l1b)); - - } - - - @Test - public void testClassMultipleInheritance() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass top1 = model.createClass(mcl, "Top1"); - CClass top2 = model.createClass(mcl, "Top2"); - CClass top3 = model.createClass(mcl, "Top3"); - CClass l1a = model.createClass(mcl, "L1A").addSuperclass("Top1").addSuperclass("Top3"); - CClass l1b = model.createClass(mcl, "L1B").addSuperclass("Top2").addSuperclass("Top3"); - CClass l2a = model.createClass(mcl, "L2A").addSuperclass("L1A"); - CClass l2b = model.createClass(mcl, "L2B").addSuperclass("L1A").addSuperclass("L1B"); - CClass l2c = model.createClass(mcl, "L2C").addSuperclass("L1B").addSuperclass("L1A"); - - assertEquals(l1a.getSubclasses(), Arrays.asList(l2a, l2b, l2c)); - assertEquals(l1a.getSuperclasses(), Arrays.asList(top1, top3)); - assertEquals(l1a.getAllSuperclasses(), Arrays.asList(top1, top3)); - - assertEquals(l1b.getSubclasses(), Arrays.asList(l2b, l2c)); - assertEquals(l1b.getSuperclasses(), Arrays.asList(top2, top3)); - assertEquals(l1b.getAllSuperclasses(), Arrays.asList(top2, top3)); - - assertEquals(l2a.getSubclasses(), Collections.emptyList()); - assertEquals(l2a.getSuperclasses(), Collections.singletonList(l1a)); - assertEquals(l2a.getAllSuperclasses(), Arrays.asList(l1a, top1, top3)); - - assertEquals(l2b.getSubclasses(), Collections.emptyList()); - assertEquals(l2b.getSuperclasses(), Arrays.asList(l1a, l1b)); - assertEquals(l2b.getAllSuperclasses(), Arrays.asList(l1a, top1, top3, l1b, top2)); - - assertEquals(l2c.getSubclasses(), Collections.emptyList()); - assertEquals(l2c.getSuperclasses(), Arrays.asList(l1b, l1a)); - assertEquals(l2c.getAllSuperclasses(), Arrays.asList(l1b, top2, top3, l1a, top1)); - } - - @Test - public void testClassAsWrongTypeOfSuperclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - model.createClass(mcl, "Top1"); - try { - model.createMetaclass("Sub1").addSuperclass("Top1"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("cannot add superclass 'Top1' to 'Sub1': not a metaclass", e.getMessage()); - } - try { - model.createStereotype("StereoSub1").addSuperclass("Top1"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("cannot add superclass 'Top1' to 'StereoSub1': not a stereotype", e.getMessage()); - } - } - - @Test - public void testClassDuplicatedSuperclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass l1a = model.createClass(mcl, "L1a"); - CClass l1b = model.createClass(mcl, "L1b"); - CClass l2 = model.createClass(mcl, "L2").addSuperclass(l1a).addSuperclass(l1b); - try { - l2.addSuperclass(l1a); - fail("exception not thrown"); - } catch (CException e) { - assertEquals( - "'L1a' is already a superclass of 'L2'", - e.getMessage()); - } - assertEquals(l2.getSuperclasses(), Arrays.asList(l1a, l1b)); - } - - @Test - public void testClassifierPathNoInheritance() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass top = model.createClass(mcl, "Top"); - - CObject obj = model.createObject(top, "o"); - assertEquals(Collections.singletonList(top), obj.getClassifierPath()); - } - - @Test - public void testClassifierPathSimpleInheritance() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass top = model.createClass(mcl, "Top"); - CClass l1a = model.createClass(mcl, "L1a").addSuperclass("Top"); - CClass l2b = model.createClass(mcl, "L2b").addSuperclass("L1a"); - CClass l2c = model.createClass(mcl, "L2c").addSuperclass(top); - - CObject obj1 = model.createObject(l2c, "o1"); - assertEquals(Arrays.asList(l2c, top), obj1.getClassifierPath()); - CObject obj2 = model.createObject(l2b, "o2"); - assertEquals(Arrays.asList(l2b, l1a, top), obj2.getClassifierPath()); - } - - @Test - public void testClassifierPathMultipleInheritance() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass top = model.createClass(mcl, "Top"); - CClass top2 = model.createClass(mcl, "Top2"); - CClass l1a = model.createClass(mcl, "L1a").addSuperclass("Top").addSuperclass("Top2"); - CClass l1b = model.createClass(mcl, "L1b").addSuperclass("Top"); - CClass l2c = model.createClass(mcl, "L2c").addSuperclass(l1b).addSuperclass(l1a); - - CObject obj1 = model.createObject(l2c, "o1"); - assertEquals(Arrays.asList(l2c, l1b, top, l1a, top2), obj1.getClassifierPath()); - CObject obj2 = model.createObject(l1a, "o2"); - assertEquals(Arrays.asList(l1a, top, top2), obj2.getClassifierPath()); - } - - @Test - public void testInstanceOf() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass a = model.createClass(mcl, "A"); - CClass b = model.createClass(mcl, "B").addSuperclass(a); - CClass c = model.createClass(mcl, "C"); - - CObject bObj = model.createObject(b, "bObj"); - assertEquals(true, bObj.instanceOf(a)); - assertEquals(true, bObj.instanceOf(b)); - assertEquals(false, bObj.instanceOf(c)); - assertEquals(true, bObj.instanceOf("A")); - assertEquals(true, bObj.instanceOf("B")); - assertEquals(false, bObj.instanceOf("C")); - } - - @Test - public void testGetAllInstances() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass a = model.createClass(mcl, "A"); - CClass b = model.createClass(mcl, "B").addSuperclass(a); - CClass c = model.createClass(mcl, "C").addSuperclass(b); - - CObject aObject = model.createObject(a, "aObject"); - CObject b1Object = model.createObject(b, "b1Object"); - CObject b2Object = model.createObject(b, "b2Object"); - CObject cObject = model.createObject(c, "cObject"); - - assertEquals(Collections.singletonList(aObject), a.getInstances()); - assertEquals(Arrays.asList(aObject, b1Object, b2Object, cObject), a.getAllInstances()); - assertEquals(Arrays.asList(b1Object, b2Object), b.getInstances()); - assertEquals(Arrays.asList(b1Object, b2Object, cObject), b.getAllInstances()); - assertEquals(Collections.singletonList(cObject), c.getInstances()); - assertEquals(Collections.singletonList(cObject), c.getAllInstances()); - } -} \ No newline at end of file diff --git a/src/codeableModels/tests/TestsInheritanceMetaclass.java b/src/codeableModels/tests/TestsInheritanceMetaclass.java deleted file mode 100644 index 91c89c5..0000000 --- a/src/codeableModels/tests/TestsInheritanceMetaclass.java +++ /dev/null @@ -1,301 +0,0 @@ -package codeableModels.tests; - -import codeableModels.*; -import org.junit.*; - -import java.util.*; - -import static org.junit.Assert.*; - -public class TestsInheritanceMetaclass { - - @Test - public void testNoInheritanceMetaclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass top = model.createMetaclass("Top"); - assertEquals(0, top.getSuperclasses().size()); - assertEquals(0, top.getSubclasses().size()); - assertEquals(0, top.getAllSuperclasses().size()); - } - - @Test - public void testMetaclassAddSuperclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass top = model.createMetaclass("Top"); - try { - top.addSuperclass("X"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("can't find superclass 'X'", e.getMessage()); - } - } - - @Test - public void testSimpleInheritanceMetaclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass top = model.createMetaclass("Top"); - CMetaclass l1a = model.createMetaclass("L1a").addSuperclass("Top"); - CMetaclass l1b = model.createMetaclass("L1b").addSuperclass(top); - CMetaclass l2a = model.createMetaclass("L2a").addSuperclass("L1a"); - CMetaclass l2b = model.createMetaclass("L2b").addSuperclass("L1a"); - CMetaclass l2c = model.createMetaclass("L2c").addSuperclass(top); - - assertEquals(3, top.getSubclasses().size()); - assertEquals(0, top.getSuperclasses().size()); - assertEquals(0, top.getAllSuperclasses().size()); - assertEquals(2, l1a.getSubclasses().size()); - assertEquals(1, l1a.getSuperclasses().size()); - assertEquals(1, l1a.getAllSuperclasses().size()); - assertEquals(0, l1b.getSubclasses().size()); - assertEquals(1, l1b.getSuperclasses().size()); - assertEquals(1, l1b.getAllSuperclasses().size()); - assertEquals(0, l2a.getSubclasses().size()); - assertEquals(1, l2a.getSuperclasses().size()); - assertEquals(2, l2a.getAllSuperclasses().size()); - assertEquals(0, l2b.getSubclasses().size()); - assertEquals(1, l2b.getSuperclasses().size()); - assertEquals(2, l2b.getAllSuperclasses().size()); - assertEquals(0, l2c.getSubclasses().size()); - assertEquals(1, l2c.getSuperclasses().size()); - assertEquals(1, l2c.getAllSuperclasses().size()); - - List l1aSuper = l1a.getSuperclasses(); - List l1aSub = l1a.getSubclasses(); - - assertTrue(l1aSuper.contains(top)); - assertTrue(l1aSub.contains(l2a)); - assertTrue(l1aSub.contains(l2b)); - - List l2aAllSuper = l2a.getAllSuperclasses(); - - assertTrue(l2aAllSuper.contains(l1a)); - assertTrue(l2aAllSuper.contains(top)); - } - - @Test - public void testInheritanceDoubleAssignmentMetaclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass top = model.createMetaclass("Top"); - CMetaclass l1a = model.createMetaclass("L1a").addSuperclass("Top"); - try { - l1a.addSuperclass(top); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("'Top' is already a superclass of 'L1a'", e.getMessage()); - } - - assertEquals(1, top.getSubclasses().size()); - assertEquals(0, top.getSuperclasses().size()); - assertEquals(0, top.getAllSuperclasses().size()); - assertEquals(0, l1a.getSubclasses().size()); - assertEquals(1, l1a.getSuperclasses().size()); - assertEquals(1, l1a.getAllSuperclasses().size()); - } - - @Test - public void testInheritanceDeleteTopMetaclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass top = model.createMetaclass("Top"); - CMetaclass l1a = model.createMetaclass("L1a").addSuperclass("Top"); - CMetaclass l1b = model.createMetaclass("L1b").addSuperclass(top); - CMetaclass l2a = model.createMetaclass("L2a").addSuperclass("L1a"); - CMetaclass l2b = model.createMetaclass("L2b").addSuperclass("L1a"); - CMetaclass l2c = model.createMetaclass("L2c").addSuperclass(top); - - model.deleteClassifier(top); - - assertEquals(0, top.getSubclasses().size()); - assertEquals(0, top.getSuperclasses().size()); - assertEquals(0, top.getAllSuperclasses().size()); - assertEquals(2, l1a.getSubclasses().size()); - assertEquals(0, l1a.getSuperclasses().size()); - assertEquals(0, l1a.getAllSuperclasses().size()); - assertEquals(0, l1b.getSubclasses().size()); - assertEquals(0, l1b.getSuperclasses().size()); - assertEquals(0, l1b.getAllSuperclasses().size()); - assertEquals(0, l2a.getSubclasses().size()); - assertEquals(1, l2a.getSuperclasses().size()); - assertEquals(1, l2a.getAllSuperclasses().size()); - assertEquals(0, l2b.getSubclasses().size()); - assertEquals(1, l2b.getSuperclasses().size()); - assertEquals(1, l2b.getAllSuperclasses().size()); - assertEquals(0, l2c.getSubclasses().size()); - assertEquals(0, l2c.getSuperclasses().size()); - assertEquals(0, l2c.getAllSuperclasses().size()); - - List l1aSuper = l1a.getSuperclasses(); - List l1aSub = l1a.getSubclasses(); - - assertTrue(!l1aSuper.contains(top)); - assertTrue(l1aSub.contains(l2a)); - assertTrue(l1aSub.contains(l2b)); - - List l2aAllSuper = l2a.getAllSuperclasses(); - - assertTrue(l2aAllSuper.contains(l1a)); - - } - - @Test - public void testInheritanceDeleteInnerMetaclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass top = model.createMetaclass("Top"); - CMetaclass l1a = model.createMetaclass("L1a").addSuperclass("Top"); - CMetaclass l1b = model.createMetaclass("L1b").addSuperclass(top); - CMetaclass l2a = model.createMetaclass("L2a").addSuperclass("L1a"); - CMetaclass l2b = model.createMetaclass("L2b").addSuperclass("L1a"); - CMetaclass l2c = model.createMetaclass("L2c").addSuperclass(top); - - model.deleteClassifier(l1a); - - assertEquals(2, top.getSubclasses().size()); - assertEquals(0, top.getSuperclasses().size()); - assertEquals(0, top.getAllSuperclasses().size()); - assertEquals(0, l1a.getSubclasses().size()); - assertEquals(0, l1a.getSuperclasses().size()); - assertEquals(0, l1a.getAllSuperclasses().size()); - assertEquals(0, l1b.getSubclasses().size()); - assertEquals(1, l1b.getSuperclasses().size()); - assertEquals(1, l1b.getAllSuperclasses().size()); - assertEquals(0, l2a.getSubclasses().size()); - assertEquals(0, l2a.getSuperclasses().size()); - assertEquals(0, l2a.getAllSuperclasses().size()); - assertEquals(0, l2b.getSubclasses().size()); - assertEquals(0, l2b.getSuperclasses().size()); - assertEquals(0, l2b.getAllSuperclasses().size()); - assertEquals(0, l2c.getSubclasses().size()); - assertEquals(1, l2c.getSuperclasses().size()); - assertEquals(1, l2c.getAllSuperclasses().size()); - - List l1aSuper = l1a.getSuperclasses(); - List l1aSub = l1a.getSubclasses(); - - assertTrue(!l1aSuper.contains(top)); - assertTrue(!l1aSub.contains(l2a)); - assertTrue(!l1aSub.contains(l2b)); - } - - @Test - public void testMetaclassMultipleInheritance() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass top1 = model.createMetaclass("Top1"); - CMetaclass top2 = model.createMetaclass("Top2"); - CMetaclass top3 = model.createMetaclass("Top3"); - CMetaclass l1a = model.createMetaclass("L1A").addSuperclass("Top1").addSuperclass("Top3"); - CMetaclass l1b = model.createMetaclass("L1B").addSuperclass("Top2").addSuperclass("Top3"); - CMetaclass l2a = model.createMetaclass("L2A").addSuperclass("L1A"); - CMetaclass l2b = model.createMetaclass("L2B").addSuperclass("L1A").addSuperclass("L1B"); - CMetaclass l2c = model.createMetaclass("L2C").addSuperclass("L1B").addSuperclass("L1A"); - - assertEquals(l1a.getSubclasses(), Arrays.asList(l2a, l2b, l2c)); - assertEquals(l1a.getSuperclasses(), Arrays.asList(top1, top3)); - assertEquals(l1a.getAllSuperclasses(), Arrays.asList(top1, top3)); - - assertEquals(l1b.getSubclasses(), Arrays.asList(l2b, l2c)); - assertEquals(l1b.getSuperclasses(), Arrays.asList(top2, top3)); - assertEquals(l1b.getAllSuperclasses(), Arrays.asList(top2, top3)); - - assertEquals(l2a.getSubclasses(), Collections.emptyList()); - assertEquals(l2a.getSuperclasses(), Collections.singletonList(l1a)); - assertEquals(l2a.getAllSuperclasses(), Arrays.asList(l1a, top1, top3)); - - assertEquals(l2b.getSubclasses(), Collections.emptyList()); - assertEquals(l2b.getSuperclasses(), Arrays.asList(l1a, l1b)); - assertEquals(l2b.getAllSuperclasses(), Arrays.asList(l1a, top1, top3, l1b, top2)); - - assertEquals(l2c.getSubclasses(), Collections.emptyList()); - assertEquals(l2c.getSuperclasses(), Arrays.asList(l1b, l1a)); - assertEquals(l2c.getAllSuperclasses(), Arrays.asList(l1b, top2, top3, l1a, top1)); - } - - @Test - public void testMetaclassAsWrongTypeOfSuperclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass top1 = model.createMetaclass("Top1"); - try { - model.createClass(top1, "Sub1").addSuperclass("Top1"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("cannot add superclass 'Top1' to 'Sub1': not a class", e.getMessage()); - } - try { - model.createStereotype("StereoSub1").addSuperclass("Top1"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("cannot add superclass 'Top1' to 'StereoSub1': not a stereotype", e.getMessage()); - } - } - - @Test - public void testClassifierPathNoInheritance() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass top = model.createMetaclass("Top"); - CClass cl = model.createClass(top, "CL"); - assertEquals(Collections.singletonList(top), cl.getClassifierPath()); - } - - @Test - public void testClassifierPathSimpleInheritance() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass top = model.createMetaclass("Top"); - CMetaclass l1a = model.createMetaclass("L1a").addSuperclass("Top"); - CMetaclass l2b = model.createMetaclass("L2b").addSuperclass("L1a"); - CMetaclass l2c = model.createMetaclass("L2c").addSuperclass(top); - - CClass cl1 = model.createClass(l2c, "CL1"); - assertEquals(Arrays.asList(l2c, top), cl1.getClassifierPath()); - CClass cl2 = model.createClass(l2b, "CL2"); - assertEquals(Arrays.asList(l2b, l1a, top), cl2.getClassifierPath()); - } - - @Test - public void testClassifierPathMultipleInheritance() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass top = model.createMetaclass("Top"); - CMetaclass top2 = model.createMetaclass("Top2"); - CMetaclass l1a = model.createMetaclass("L1a").addSuperclass("Top").addSuperclass("Top2"); - CMetaclass l1b = model.createMetaclass("L1b").addSuperclass("Top"); - CMetaclass l2c = model.createMetaclass("L2c").addSuperclass(l1b).addSuperclass(l1a); - - CClass cl1 = model.createClass(l2c, "CL1"); - assertEquals(Arrays.asList(l2c, l1b, top, l1a, top2), cl1.getClassifierPath()); - CClass cl2 = model.createClass(l1a, "CL2"); - assertEquals(Arrays.asList(l1a, top, top2), cl2.getClassifierPath()); - } - - @Test - public void testInstanceOf() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass a = model.createMetaclass("A"); - CMetaclass b = model.createMetaclass("B").addSuperclass(a); - CMetaclass c = model.createMetaclass("C"); - - CClass bClass = model.createClass(b, "bClass"); - assertEquals(true, bClass.instanceOf(a)); - assertEquals(true, bClass.instanceOf(b)); - assertEquals(false, bClass.instanceOf(c)); - assertEquals(true, bClass.instanceOf("A")); - assertEquals(true, bClass.instanceOf("B")); - assertEquals(false, bClass.instanceOf("C")); - } - - @Test - public void testGetAllClassInstances() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass a = model.createMetaclass("A"); - CMetaclass b = model.createMetaclass("B").addSuperclass(a); - CMetaclass c = model.createMetaclass("C").addSuperclass(b); - - CClass aClass = model.createClass(a, "aClass"); - CClass b1Class = model.createClass(b, "b1Class"); - CClass b2Class = model.createClass(b, "b2Class"); - CClass cClass = model.createClass(c, "cClass"); - - assertEquals(Collections.singletonList(aClass), a.getClassInstances()); - assertEquals(Arrays.asList(aClass, b1Class, b2Class, cClass), a.getAllClassInstances()); - assertEquals(Arrays.asList(b1Class, b2Class), b.getClassInstances()); - assertEquals(Arrays.asList(b1Class, b2Class, cClass), b.getAllClassInstances()); - assertEquals(Collections.singletonList(cClass), c.getClassInstances()); - assertEquals(Collections.singletonList(cClass), c.getAllClassInstances()); - } -} diff --git a/src/codeableModels/tests/TestsInheritanceStereotype.java b/src/codeableModels/tests/TestsInheritanceStereotype.java deleted file mode 100644 index bf4bf54..0000000 --- a/src/codeableModels/tests/TestsInheritanceStereotype.java +++ /dev/null @@ -1,342 +0,0 @@ -package codeableModels.tests; - -import codeableModels.*; -import org.junit.*; - -import java.util.*; - -import static org.junit.Assert.*; - -public class TestsInheritanceStereotype { - - @Test - public void testNoInheritanceStereotype() throws CException { - CModel model = CodeableModels.createModel(); - CStereotype top = model.createStereotype("Top"); - assertEquals(0, top.getSuperclasses().size()); - assertEquals(0, top.getSubclasses().size()); - assertEquals(0, top.getAllSuperclasses().size()); - } - - @Test - public void testStereotypeAddSuperclass() throws CException { - CModel model = CodeableModels.createModel(); - CStereotype top = model.createStereotype("Top"); - try { - top.addSuperclass("X"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("can't find superclass 'X'", e.getMessage()); - } - } - - @Test - public void testSimpleInheritanceStereotype() throws CException { - CModel model = CodeableModels.createModel(); - CStereotype top = model.createStereotype("Top"); - CStereotype l1a = model.createStereotype("L1a").addSuperclass("Top"); - CStereotype l1b = model.createStereotype("L1b").addSuperclass(top); - CStereotype l2a = model.createStereotype("L2a").addSuperclass("L1a"); - CStereotype l2b = model.createStereotype("L2b").addSuperclass("L1a"); - CStereotype l2c = model.createStereotype("L2c").addSuperclass(top); - - assertEquals(3, top.getSubclasses().size()); - assertEquals(0, top.getSuperclasses().size()); - assertEquals(0, top.getAllSuperclasses().size()); - assertEquals(2, l1a.getSubclasses().size()); - assertEquals(1, l1a.getSuperclasses().size()); - assertEquals(1, l1a.getAllSuperclasses().size()); - assertEquals(0, l1b.getSubclasses().size()); - assertEquals(1, l1b.getSuperclasses().size()); - assertEquals(1, l1b.getAllSuperclasses().size()); - assertEquals(0, l2a.getSubclasses().size()); - assertEquals(1, l2a.getSuperclasses().size()); - assertEquals(2, l2a.getAllSuperclasses().size()); - assertEquals(0, l2b.getSubclasses().size()); - assertEquals(1, l2b.getSuperclasses().size()); - assertEquals(2, l2b.getAllSuperclasses().size()); - assertEquals(0, l2c.getSubclasses().size()); - assertEquals(1, l2c.getSuperclasses().size()); - assertEquals(1, l2c.getAllSuperclasses().size()); - - List l1aSuper = l1a.getSuperclasses(); - List l1aSub = l1a.getSubclasses(); - - assertTrue(l1aSuper.contains(top)); - assertTrue(l1aSub.contains(l2a)); - assertTrue(l1aSub.contains(l2b)); - - List l2aAllSuper = l2a.getAllSuperclasses(); - - assertTrue(l2aAllSuper.contains(l1a)); - assertTrue(l2aAllSuper.contains(top)); - } - - @Test - public void testInheritanceDoubleAssignmentStereotype() throws CException { - CModel model = CodeableModels.createModel(); - CStereotype top = model.createStereotype("Top"); - CStereotype l1a = model.createStereotype("L1a").addSuperclass("Top"); - try { - l1a.addSuperclass(top); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("'Top' is already a superclass of 'L1a'", e.getMessage()); - } - - assertEquals(1, top.getSubclasses().size()); - assertEquals(0, top.getSuperclasses().size()); - assertEquals(0, top.getAllSuperclasses().size()); - assertEquals(0, l1a.getSubclasses().size()); - assertEquals(1, l1a.getSuperclasses().size()); - assertEquals(1, l1a.getAllSuperclasses().size()); - } - - @Test - public void testInheritanceDeleteTopStereotype() throws CException { - CModel model = CodeableModels.createModel(); - CStereotype top = model.createStereotype("Top"); - CStereotype l1a = model.createStereotype("L1a").addSuperclass("Top"); - CStereotype l1b = model.createStereotype("L1b").addSuperclass(top); - CStereotype l2a = model.createStereotype("L2a").addSuperclass("L1a"); - CStereotype l2b = model.createStereotype("L2b").addSuperclass("L1a"); - CStereotype l2c = model.createStereotype("L2c").addSuperclass(top); - - model.deleteClassifier(top); - - assertEquals(0, top.getSubclasses().size()); - assertEquals(0, top.getSuperclasses().size()); - assertEquals(0, top.getAllSuperclasses().size()); - assertEquals(2, l1a.getSubclasses().size()); - assertEquals(0, l1a.getSuperclasses().size()); - assertEquals(0, l1a.getAllSuperclasses().size()); - assertEquals(0, l1b.getSubclasses().size()); - assertEquals(0, l1b.getSuperclasses().size()); - assertEquals(0, l1b.getAllSuperclasses().size()); - assertEquals(0, l2a.getSubclasses().size()); - assertEquals(1, l2a.getSuperclasses().size()); - assertEquals(1, l2a.getAllSuperclasses().size()); - assertEquals(0, l2b.getSubclasses().size()); - assertEquals(1, l2b.getSuperclasses().size()); - assertEquals(1, l2b.getAllSuperclasses().size()); - assertEquals(0, l2c.getSubclasses().size()); - assertEquals(0, l2c.getSuperclasses().size()); - assertEquals(0, l2c.getAllSuperclasses().size()); - - List l1aSuper = l1a.getSuperclasses(); - List l1aSub = l1a.getSubclasses(); - - assertTrue(!l1aSuper.contains(top)); - assertTrue(l1aSub.contains(l2a)); - assertTrue(l1aSub.contains(l2b)); - - List l2aAllSuper = l2a.getAllSuperclasses(); - - assertTrue(l2aAllSuper.contains(l1a)); - - } - - @Test - public void testInheritanceDeleteInnerStereotype() throws CException { - CModel model = CodeableModels.createModel(); - CStereotype top = model.createStereotype("Top"); - CStereotype l1a = model.createStereotype("L1a").addSuperclass("Top"); - CStereotype l1b = model.createStereotype("L1b").addSuperclass(top); - CStereotype l2a = model.createStereotype("L2a").addSuperclass("L1a"); - CStereotype l2b = model.createStereotype("L2b").addSuperclass("L1a"); - CStereotype l2c = model.createStereotype("L2c").addSuperclass(top); - - model.deleteClassifier(l1a); - - assertEquals(2, top.getSubclasses().size()); - assertEquals(0, top.getSuperclasses().size()); - assertEquals(0, top.getAllSuperclasses().size()); - assertEquals(0, l1a.getSubclasses().size()); - assertEquals(0, l1a.getSuperclasses().size()); - assertEquals(0, l1a.getAllSuperclasses().size()); - assertEquals(0, l1b.getSubclasses().size()); - assertEquals(1, l1b.getSuperclasses().size()); - assertEquals(1, l1b.getAllSuperclasses().size()); - assertEquals(0, l2a.getSubclasses().size()); - assertEquals(0, l2a.getSuperclasses().size()); - assertEquals(0, l2a.getAllSuperclasses().size()); - assertEquals(0, l2b.getSubclasses().size()); - assertEquals(0, l2b.getSuperclasses().size()); - assertEquals(0, l2b.getAllSuperclasses().size()); - assertEquals(0, l2c.getSubclasses().size()); - assertEquals(1, l2c.getSuperclasses().size()); - assertEquals(1, l2c.getAllSuperclasses().size()); - - List l1aSuper = l1a.getSuperclasses(); - List l1aSub = l1a.getSubclasses(); - - assertTrue(!l1aSuper.contains(top)); - assertTrue(!l1aSub.contains(l2a)); - assertTrue(!l1aSub.contains(l2b)); - } - - @Test - public void testStereotypeMultipleInheritance() throws CException { - CModel model = CodeableModels.createModel(); - CStereotype top1 = model.createStereotype("Top1"); - CStereotype top2 = model.createStereotype("Top2"); - CStereotype top3 = model.createStereotype("Top3"); - CStereotype l1a = model.createStereotype("L1A").addSuperclass("Top1").addSuperclass("Top3"); - CStereotype l1b = model.createStereotype("L1B").addSuperclass("Top2").addSuperclass("Top3"); - CStereotype l2a = model.createStereotype("L2A").addSuperclass("L1A"); - CStereotype l2b = model.createStereotype("L2B").addSuperclass("L1A").addSuperclass("L1B"); - CStereotype l2c = model.createStereotype("L2C").addSuperclass("L1B").addSuperclass("L1A"); - - assertEquals(l1a.getSubclasses(), Arrays.asList(l2a, l2b, l2c)); - assertEquals(l1a.getSuperclasses(), Arrays.asList(top1, top3)); - assertEquals(l1a.getAllSuperclasses(), Arrays.asList(top1, top3)); - - assertEquals(l1b.getSubclasses(), Arrays.asList(l2b, l2c)); - assertEquals(l1b.getSuperclasses(), Arrays.asList(top2, top3)); - assertEquals(l1b.getAllSuperclasses(), Arrays.asList(top2, top3)); - - assertEquals(l2a.getSubclasses(), Collections.emptyList()); - assertEquals(l2a.getSuperclasses(), Collections.singletonList(l1a)); - assertEquals(l2a.getAllSuperclasses(), Arrays.asList(l1a, top1, top3)); - - assertEquals(l2b.getSubclasses(), Collections.emptyList()); - assertEquals(l2b.getSuperclasses(), Arrays.asList(l1a, l1b)); - assertEquals(l2b.getAllSuperclasses(), Arrays.asList(l1a, top1, top3, l1b, top2)); - - assertEquals(l2c.getSubclasses(), Collections.emptyList()); - assertEquals(l2c.getSuperclasses(), Arrays.asList(l1b, l1a)); - assertEquals(l2c.getAllSuperclasses(), Arrays.asList(l1b, top2, top3, l1a, top1)); - } - - @Test - public void testStereotypeAsWrongTypeOfSuperclass() throws CException { - CModel model = CodeableModels.createModel(); - model.createStereotype("Top1"); - CMetaclass mcl = model.createMetaclass("MCL"); - try { - model.createClass(mcl, "Sub1").addSuperclass("Top1"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("cannot add superclass 'Top1' to 'Sub1': not a class", e.getMessage()); - } - try { - model.createMetaclass("MetaSub1").addSuperclass("Top1"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("cannot add superclass 'Top1' to 'MetaSub1': not a metaclass", e.getMessage()); - } - } - - @Test - public void testExtendedClassesOfInheritingStereotypes_SuperclassHasNone() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2").addSuperclass(mcl1); - CStereotype stereotype1 = model.createStereotype("ST1"); - CStereotype stereotype2 = model.createStereotype("ST2").addSuperclass("ST1"); - mcl2.addStereotype("ST2"); - - assertEquals(0, stereotype1.getStereotypedElements().size()); - assertEquals(mcl2, stereotype2.getStereotypedElements().get(0)); - assertEquals(Collections.singletonList(stereotype2), mcl2.getStereotypes()); - } - - @Test - public void testExtendedClassesOfInheritingStereotypes_SuperclassHasTheSame() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CStereotype stereotype1 = model.createStereotype("ST1", mcl1); - CStereotype stereotype2 = model.createStereotype("ST2", mcl1).addSuperclass("ST1"); - - assertEquals(mcl1, stereotype1.getStereotypedElements().get(0)); - assertEquals(mcl1, stereotype2.getStereotypedElements().get(0)); - assertEquals(Arrays.asList(stereotype1, stereotype2), mcl1.getStereotypes()); - } - - @Test - public void testExtendedClassesOfInheritingStereotypes_RemoveSuperclassStereotype() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CStereotype stereotype1 = model.createStereotype("ST1", mcl1); - CStereotype stereotype2 = model.createStereotype("ST2", mcl1).addSuperclass("ST1"); - mcl1.removeStereotype(stereotype1); - - assertEquals(0, stereotype1.getStereotypedElements().size()); - assertEquals(mcl1, stereotype2.getStereotypedElements().get(0)); - assertEquals(Collections.singletonList(stereotype2), mcl1.getStereotypes()); - } - - @Test - public void testExtendedClassesOfInheritingStereotypes_SuperclassIsSetToTheSame() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CStereotype stereotype1 = model.createStereotype("ST1"); - CStereotype stereotype2 = model.createStereotype("ST2", mcl1).addSuperclass("ST1"); - mcl1.addStereotype(stereotype1); - - assertEquals(mcl1, stereotype1.getStereotypedElements().get(0)); - assertEquals(mcl1, stereotype2.getStereotypedElements().get(0)); - assertEquals(Arrays.asList(stereotype2, stereotype1), mcl1.getStereotypes()); - } - - @Test - public void testExtendedClassesOfInheritingStereotypes_SuperclassHasMetaclassesSuperclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2").addSuperclass(mcl1); - CStereotype stereotype1 = model.createStereotype("ST1", mcl1); - CStereotype stereotype2 = model.createStereotype("ST2", mcl2).addSuperclass("ST1"); - - assertEquals(mcl1, stereotype1.getStereotypedElements().get(0)); - assertEquals(mcl2, stereotype2.getStereotypedElements().get(0)); - assertEquals(Collections.singletonList(stereotype1), mcl1.getStereotypes()); - assertEquals(Collections.singletonList(stereotype2), mcl2.getStereotypes()); - } - - @Test - public void testExtendedClassesOfInheritingStereotypes_SuperclassIsSetToMetaclassesSuperclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2").addSuperclass(mcl1); - CStereotype stereotype1 = model.createStereotype("ST1"); - CStereotype stereotype2 = model.createStereotype("ST2", mcl2).addSuperclass("ST1"); - mcl1.addStereotype("ST1"); - - assertEquals(mcl1, stereotype1.getStereotypedElements().get(0)); - assertEquals(mcl2, stereotype2.getStereotypedElements().get(0)); - assertEquals(Collections.singletonList(stereotype1), mcl1.getStereotypes()); - assertEquals(Collections.singletonList(stereotype2), mcl2.getStereotypes()); - } - - @Test - public void testExtendedClassesOfInheritingStereotypes_SuperclassHasMetaclassesSuperclassIndirectly() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2").addSuperclass(mcl1); - CMetaclass mcl3 = model.createMetaclass("MCL3").addSuperclass(mcl2); - CStereotype stereotype1 = model.createStereotype("ST1", mcl1); - CStereotype stereotype2 = model.createStereotype("ST2", mcl3).addSuperclass("ST1"); - - assertEquals(mcl1, stereotype1.getStereotypedElements().get(0)); - assertEquals(mcl3, stereotype2.getStereotypedElements().get(0)); - assertEquals(Collections.singletonList(stereotype1), mcl1.getStereotypes()); - assertEquals(Collections.singletonList(stereotype2), mcl3.getStereotypes()); - } - - @Test - public void testExtendedClassesOfInheritingStereotypes_SuperclassIsSetToMetaclassesSuperclassIndirectly() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2").addSuperclass(mcl1); - CMetaclass mcl3 = model.createMetaclass("MCL3").addSuperclass(mcl2); - CStereotype stereotype1 = model.createStereotype("ST1"); - CStereotype stereotype2 = model.createStereotype("ST2", mcl3).addSuperclass("ST1"); - mcl1.addStereotype("ST1"); - - assertEquals(mcl1, stereotype1.getStereotypedElements().get(0)); - assertEquals(mcl3, stereotype2.getStereotypedElements().get(0)); - assertEquals(Collections.singletonList(stereotype1), mcl1.getStereotypes()); - assertEquals(Collections.singletonList(stereotype2), mcl3.getStereotypes()); - } - -} diff --git a/src/codeableModels/tests/TestsMetaclass.java b/src/codeableModels/tests/TestsMetaclass.java deleted file mode 100644 index d1d8b25..0000000 --- a/src/codeableModels/tests/TestsMetaclass.java +++ /dev/null @@ -1,204 +0,0 @@ -package codeableModels.tests; - -import codeableModels.*; -import org.junit.*; - -import java.util.*; - -import static org.junit.Assert.*; - -public class TestsMetaclass { - - @Test - public void testCreationOfOneMetaclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass cl = model.createMetaclass("MClass1"); - CMetaclass cl2 = model.getMetaclass("MClass1"); - assertEquals(cl, cl2); - assertEquals(model, cl2.getModel()); - } - - @Test - public void testCreationOfAutoNamedMetaclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass(); - String mclName = mcl.getName(); - assertTrue(mclName.startsWith("##")); - CMetaclass mcl2 = model.getMetaclass(mclName); - assertEquals(mcl, mcl2); - } - - @Test - public void testCreationOf3Metaclasses() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass cl1 = model.createMetaclass("MClass1"); - CMetaclass cl2 = model.createMetaclass("MClass2"); - CMetaclass cl3 = model.createMetaclass("MClass3"); - CMetaclass gcl1 = model.getMetaclass("MClass1"); - CMetaclass gcl2 = model.getMetaclass("MClass2"); - CMetaclass gcl3 = model.getMetaclass("MClass3"); - assertEquals(cl1, gcl1); - assertEquals(cl2, gcl2); - assertEquals(cl3, gcl3); - } - - @Test - public void testCCreationOfNoMetaclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass gcl1 = model.getMetaclass("MClass1"); - assertEquals(null, gcl1); - } - - @Test - public void testMetaclassUniqueNameUsedException() throws CException { - CModel model = CodeableModels.createModel(); - model.createMetaclass("MClass1"); - try { - model.createMetaclass("MClass1"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("classifier 'MClass1' cannot be created, classifier name already exists", e.getMessage()); - } - } - - @Test - public void testLookupMetaclassLocally() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass cl = model.createMetaclass("MClass1"); - CMetaclass cl2 = model.lookupMetaclass("MClass1"); - assertEquals(cl, cl2); - CClassifier cl3 = model.lookupClassifier("MClass1"); - assertEquals(cl, cl3); - - } - - @Test - public void testLookup3MetaclassesLocally() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass cl1 = model.createMetaclass("Metaclass1"); - CMetaclass cl2 = model.createMetaclass("Metaclass2"); - CMetaclass cl3 = model.createMetaclass("Metaclass3"); - CMetaclass gcl1 = model.lookupMetaclass("Metaclass1"); - CMetaclass gcl2 = model.lookupMetaclass("Metaclass2"); - CClassifier gcl3 = model.lookupClassifier("Metaclass3"); - assertEquals(cl1, gcl1); - assertEquals(cl2, gcl2); - assertEquals(cl3, gcl3); - } - - @Test - public void testLookupMetaclassLocallyThatDoesNotExist() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass gcl1 = model.lookupMetaclass("Metaclass1"); - assertEquals(null, gcl1); - } - - @Test - public void testGetMetaclassesAndGetMetaclassNames() throws CException { - CModel model = CodeableModels.createModel(); - List metaclasses = model.getMetaclasses(); - List metaclassNames = model.getMetaclassNames(); - assertEquals(0, metaclasses.size()); - assertEquals(0, metaclassNames.size()); - - CMetaclass cl1 = model.createMetaclass("Metaclass1"); - - metaclasses = model.getMetaclasses(); - metaclassNames = model.getMetaclassNames(); - assertEquals(1, metaclasses.size()); - assertEquals(1, metaclassNames.size()); - assertEquals("Metaclass1", metaclassNames.get(0)); - assertEquals(cl1, metaclasses.get(0)); - - CMetaclass cl2 = model.createMetaclass("Metaclass2"); - CMetaclass cl3 = model.createMetaclass("Metaclass3"); - - metaclasses = model.getMetaclasses(); - metaclassNames = model.getMetaclassNames(); - assertEquals(3, metaclasses.size()); - assertEquals(3, metaclassNames.size()); - assertTrue(metaclasses.contains(cl1)); - assertTrue(metaclasses.contains(cl2)); - assertTrue(metaclasses.contains(cl3)); - } - - @Test - public void testDeleteMetaclass() throws CException { - CModel model = CodeableModels.createModel(); - CModel model2 = CodeableModels.createModel(); - CMetaclass mcl = model2.createMetaclass("MCL"); - try { - model.deleteClassifier(null); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("classifier '' to be deleted does not exist", e.getMessage()); - } - try { - model.deleteClassifier(model2.createClass(mcl, "x")); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("classifier 'x' to be deleted does not exist", e.getMessage()); - } - CMetaclass cl1 = model.createMetaclass("MetaClass1"); - model.deleteClassifier(cl1); - - CMetaclass gcl1 = model.getMetaclass("MetaClass1"); - assertEquals(null, gcl1); - List classes = model.getMetaclasses(); - assertEquals(0, classes.size()); - - cl1 = model.createMetaclass("MetaClass1"); - CMetaclass cl2 = model.createMetaclass("MetaClass2"); - CMetaclass cl3 = model.createMetaclass("MetaClass3"); - - model.deleteClassifier(cl1); - try { - model.deleteClassifier(model2.createClass(mcl, "y")); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("classifier 'y' to be deleted does not exist", e.getMessage()); - } - model.deleteClassifier(cl3); - - gcl1 = model.getMetaclass("MetaClass1"); - CMetaclass gcl2 = model.getMetaclass("MetaClass2"); - CMetaclass gcl3 = model.getMetaclass("MetaClass3"); - - assertEquals(null, gcl1); - assertEquals(cl2, gcl2); - assertEquals(null, gcl3); - - classes = model.getMetaclasses(); - assertEquals(1, classes.size()); - } - - - @Test - public void testDeleteMetaclassClassRelation() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("Metaclass1"); - CMetaclass mcl2 = model.createMetaclass("Metaclass2"); - CClass cl1 = model.createClass(mcl1, "C1"); - CClass cl2 = model.createClass(mcl1, "C2"); - CClass cl3 = model.createClass(mcl2, "C3"); - - model.deleteClassifier(mcl1); - - assertEquals(0, mcl1.getClassInstances().size()); - assertEquals(null, cl1.getMetaclass()); - assertEquals(null, cl2.getMetaclass()); - assertEquals(mcl2, cl3.getMetaclass()); - - List classes = mcl2.getClassInstances(); - assertEquals(1, classes.size()); - assertTrue(!classes.contains(cl1)); - assertTrue(!classes.contains(cl2)); - assertTrue(classes.contains(cl3)); - - classes = model.getClasses(); - assertEquals(1, classes.size()); - assertEquals(cl3, classes.get(0)); - } - - -} diff --git a/src/codeableModels/tests/TestsObject.java b/src/codeableModels/tests/TestsObject.java deleted file mode 100644 index d61acd3..0000000 --- a/src/codeableModels/tests/TestsObject.java +++ /dev/null @@ -1,271 +0,0 @@ -package codeableModels.tests; - -import codeableModels.*; -import org.junit.*; - -import java.util.*; - -import static org.junit.Assert.*; - -public class TestsObject { - @Test - public void testCreationOfNoObjects() throws CException { - CModel model = CodeableModels.createModel(); - CObject obj = model.getObject("Obj1"); - assertEquals(null, obj); - } - - @Test - public void testCreationOfOneObject() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl = model.createClass(mcl, "Class1"); - CObject obj1 = model.createObject("Class1", "Obj1"); - CObject obj2 = model.getObject("Obj1"); - assertEquals(obj1, obj2); - assertEquals(cl, obj2.getClassifier()); - } - - @Test - public void testCreationOfAutoNamedObject() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl = model.createClass(mcl, "Class1"); - CObject obj1 = model.createObject("Class1"); - CObject obj2 = model.createObject(cl); - String obj1Name = obj1.getName(), obj2Name = obj2.getName(); - assertTrue(obj1Name.startsWith("##")); - assertTrue(obj2Name.startsWith("##")); - CObject obj3 = model.getObject(obj1Name); - CObject obj4 = model.getObject(obj2Name); - assertEquals(obj1, obj3); - assertEquals(obj2, obj4); - assertEquals(cl, obj3.getClassifier()); - assertEquals(cl, obj4.getClassifier()); - } - - @Test - public void testCreationOf3Objects() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CObject obj1 = model.createObject("Class1", "Obj1"); - CObject obj2 = model.createObject("Class2", "Obj2"); - CObject obj3 = model.createObject("Class1", "Obj3"); - - CObject o1 = model.getObject("Obj1"); - CObject o2 = model.getObject("Obj2"); - CObject o3 = model.getObject("Obj3"); - - assertEquals(obj1, o1); - assertEquals(obj2, o2); - assertEquals(obj3, o3); - assertEquals(cl1, o1.getClassifier()); - assertEquals(cl2, o2.getClassifier()); - assertEquals(cl1, o3.getClassifier()); - } - - @Test - public void testCreationOf3ObjectsWithoutNameLookup() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CObject obj1 = model.createObject(cl1, "Obj1"); - CObject obj2 = model.createObject(cl2, "Obj2"); - CObject obj3 = model.createObject(cl1, "Obj3"); - - CObject o1 = model.getObject("Obj1"); - CObject o2 = model.getObject("Obj2"); - CObject o3 = model.getObject("Obj3"); - - assertEquals(obj1, o1); - assertEquals(obj2, o2); - assertEquals(obj3, o3); - assertEquals(cl1, o1.getClassifier()); - assertEquals(cl2, o2.getClassifier()); - assertEquals(cl1, o3.getClassifier()); - } - - @Test - public void testObjectUniqueNameUsedException() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - model.createClass(mcl, "Class1"); - model.createObject("Class1", "Obj1"); - try { - model.createObject("Class1", "Obj1"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("object 'Obj1' cannot be created, object name already exists", e.getMessage()); - } - } - - @Test - public void testObjectCreationClassDoesNotExistException() throws CException { - CModel model = CodeableModels.createModel(); - try { - model.createObject("Class1", "Obj1"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("can't find class: 'Class1' to be instantiated", e.getMessage()); - } - } - - @Test - public void testGetObjectsAndGetObjectNames() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - List objects = model.getObjects(); - List objectNames = model.getObjectNames(); - assertEquals(0, objects.size()); - assertEquals(0, objectNames.size()); - - model.createClass(mcl, "Class1"); - model.createClass(mcl, "Class2"); - CObject obj1 = model.createObject("Class1", "Obj1"); - - objects = model.getObjects(); - objectNames = model.getObjectNames(); - assertEquals(1, objects.size()); - assertEquals(1, objectNames.size()); - assertEquals("Obj1", objectNames.get(0)); - assertEquals(obj1, objects.get(0)); - - model.createObject("Class2", "Obj2"); - model.createObject("Class1", "Obj3"); - - objects = model.getObjects(); - objectNames = model.getObjectNames(); - assertEquals(3, objects.size()); - assertEquals(3, objectNames.size()); - } - - @Test - public void testDeleteObject() throws CException { - CModel model = CodeableModels.createModel(); - CModel model2 = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CMetaclass mcl2 = model2.createMetaclass("MCL"); - CClass mcl2_cl2 = model2.createClass(mcl2, "CL2"); - try { - model.deleteObject(null); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("object '' to be deleted does not exist", e.getMessage()); - } - try { - model.deleteObject(model2.createObject(mcl2_cl2, "x")); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("object 'x' to be deleted does not exist", e.getMessage()); - } - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - - CObject obj1 = model.createObject(cl1, "O1"); - model.deleteObject(obj1); - - CObject retrievedObj1 = model.getObject("O1"); - assertEquals(null, retrievedObj1); - List objects = model.getObjects(); - assertEquals(0, objects.size()); - - obj1 = model.createObject(cl1, "O1"); - CObject obj2 = model.createObject(cl1, "O2"); - CObject obj3 = model.createObject(cl2, "O3"); - - model.deleteObject(obj1); - try { - model.deleteObject(model2.createObject(mcl2_cl2, "y")); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("object 'y' to be deleted does not exist", e.getMessage()); - } - model.deleteObject(obj3); - - retrievedObj1 = model.getObject("O1"); - CObject retrievedObj2 = model.getObject("O2"); - CObject retrievedObj3 = model.getObject("O3"); - - assertEquals(null, retrievedObj1); - assertEquals(obj2, retrievedObj2); - assertEquals(null, retrievedObj3); - - objects = model.getObjects(); - assertEquals(1, objects.size()); - } - - @Test - public void testClassInstanceRelation() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - - List instances = cl1.getInstances(); - assertEquals(0, instances.size()); - - CObject obj1 = model.createObject(cl1, "O1"); - CObject obj2 = model.createObject(cl1, "O2"); - CObject obj3 = model.createObject(cl2, "O3"); - - assertEquals(cl1, obj1.getClassifier()); - assertEquals(cl1, obj2.getClassifier()); - assertEquals(cl2, obj3.getClassifier()); - - List instances1 = cl1.getInstances(); - assertEquals(2, instances1.size()); - assertTrue(instances1.contains(obj1)); - assertTrue(instances1.contains(obj2)); - assertTrue(!instances1.contains(obj3)); - - List instances2 = cl2.getInstances(); - assertEquals(1, instances2.size()); - assertTrue(!instances2.contains(obj1)); - assertTrue(!instances2.contains(obj2)); - assertTrue(instances2.contains(obj3)); - } - - @Test - public void testClassInstanceRelationObjectDeletion() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - - List instances = cl1.getInstances(); - assertEquals(0, instances.size()); - - CObject obj1 = model.createObject(cl1, "O1"); - model.deleteObject(obj1); - - instances = cl1.getInstances(); - assertEquals(0, instances.size()); - - obj1 = model.createObject(cl1, "O1"); - CObject obj2 = model.createObject(cl1, "O2"); - CObject obj3 = model.createObject(cl2, "O3"); - - assertEquals(cl1, obj1.getClassifier()); - assertEquals(cl1, obj2.getClassifier()); - assertEquals(cl2, obj3.getClassifier()); - - model.deleteObject(obj1); - model.deleteObject(obj3); - - List instances1 = cl1.getInstances(); - assertEquals(1, instances1.size()); - assertTrue(!instances1.contains(obj1)); - assertTrue(instances1.contains(obj2)); - assertTrue(!instances1.contains(obj3)); - - List instances2 = cl2.getInstances(); - assertEquals(0, instances2.size()); - assertTrue(!instances2.contains(obj1)); - assertTrue(!instances2.contains(obj2)); - assertTrue(!instances2.contains(obj3)); - } - -} diff --git a/src/codeableModels/tests/TestsObjectLinks.java b/src/codeableModels/tests/TestsObjectLinks.java deleted file mode 100644 index b700220..0000000 --- a/src/codeableModels/tests/TestsObjectLinks.java +++ /dev/null @@ -1,1142 +0,0 @@ -package codeableModels.tests; - -import codeableModels.*; -import org.junit.*; - -import java.util.*; - -import static org.junit.Assert.*; - -public class TestsObjectLinks { - - @Test - public void testSetOneToOneLink() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CAssociation association = model.createAssociation(cl1.createEnd("class1", "1"), - cl2.createEnd("class2", "1")); - CAssociationEnd cl1End = association.getEndByRoleName("class1"); - CAssociationEnd cl2End = association.getEndByRoleName("class2"); - - CObject obj2 = model.createObject(cl2, "obj2"); - CObject obj1 = model.createObject(cl1, "obj1"); - obj1.setLink(cl2End, obj2); - CObject obj3 = model.createObject(cl2, "obj3"); - - assertEquals(Collections.singletonList(obj2), obj1.getLinks(cl2End)); - assertEquals(Collections.singletonList(obj1), obj2.getLinks(cl1End)); - - obj1.setLink(association.getEndByClassifier(cl2), "obj3"); - - assertEquals(Collections.singletonList(obj3), obj1.getLinks(cl2End)); - assertEquals(Collections.singletonList(obj1), obj3.getLinks(cl1End)); - } - - @Test - public void testSetLinkVariants() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CAssociation association = model.createAssociation(cl1.createEnd("class1", "1"), - cl2.createEnd("class2", "1")); - CAssociationEnd end1 = association.getEndByClassifier(cl1), end2 = association.getEndByClassifier(cl2); - - CObject obj1 = model.createObject(cl1, "obj1"); - CObject obj2 = model.createObject(cl2, "obj2"); - - obj1.setLinks(end2, Collections.singletonList(obj2)); - - assertEquals(Collections.singletonList(obj2), obj1.getLinks(end2)); - assertEquals(Collections.singletonList(obj1), obj2.getLinks(end1)); - - obj1.setLinks(end2, Collections.singletonList(obj2)); - - assertEquals(Collections.singletonList(obj2), obj1.getLinks(end2)); - assertEquals(Collections.singletonList(obj1), obj2.getLinks(end1)); - - obj1.setLinks(end2, Collections.singletonList("obj2")); - - assertEquals(Collections.singletonList(obj2), obj1.getLinks(end2)); - assertEquals(Collections.singletonList(obj1), obj2.getLinks(end1)); - - obj2.setLink(end1, obj1); - - assertEquals(Collections.singletonList(obj2), obj1.getLinks(end2)); - assertEquals(Collections.singletonList(obj1), obj2.getLinks(end1)); - - obj2.setLink(end1, "obj1"); - - assertEquals(Collections.singletonList(obj2), obj1.getLinks(end2)); - assertEquals(Collections.singletonList(obj1), obj2.getLinks(end1)); - } - - @Test - public void testAddOneToOneLink() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CAssociationEnd end1 = cl1.createEnd("class1", "1"), - end2 = cl2.createEnd("class2", "1"); - model.createAssociation(end1, end2); - - CObject obj2 = model.createObject(cl2, "obj2"); - CObject obj1 = model.createObject(cl1, "obj1"); - obj1.addLink(end2, obj2); - model.createObject(cl2, "obj3"); - - assertEquals(Collections.singletonList(obj2), obj1.getLinks(end2)); - assertEquals(Collections.singletonList(obj1), obj2.getLinks(end1)); - - try { - obj1.addLink(end2, "obj3"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("link has wrong multiplicity '2', but should be '1'", e.getMessage()); - } - } - - @Test - public void testAddOneToOneLinkVariants() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CAssociationEnd end1 = cl1.createEnd("class1", "1"), - end2 = cl2.createEnd("class2", "1"); - model.createAssociation(end1, end2); - - CObject obj1 = model.createObject(cl1, "obj1"); - CObject obj2 = model.createObject(cl2, "obj2"); - - obj1.addLinks(end2, Collections.singletonList(obj2)); - - assertEquals(Collections.singletonList(obj2), obj1.getLinks(end2)); - assertEquals(Collections.singletonList(obj1), obj2.getLinks(end1)); - - try { - obj1.addLinks(end2, Collections.singletonList(obj2)); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("link between 'obj1' and 'obj2' already exists", e.getMessage()); - } - - obj1.removeLinks(end2, Collections.singletonList(obj2)); - obj1.addLinks(end2, Collections.singletonList(obj2)); - - assertEquals(Collections.singletonList(obj2), obj1.getLinks(end2)); - assertEquals(Collections.singletonList(obj1), obj2.getLinks(end1)); - - obj2.removeLinks(end1, Collections.singletonList("obj1")); - obj2.addLinks(end1, Collections.singletonList("obj1")); - - assertEquals(Collections.singletonList(obj2), obj1.getLinks(end2)); - assertEquals(Collections.singletonList(obj1), obj2.getLinks(end1)); - - obj2.removeLink(end1, "obj1"); - obj2.addLink(end1, "obj1"); - - assertEquals(Collections.singletonList(obj2), obj1.getLinks(end2)); - assertEquals(Collections.singletonList(obj1), obj2.getLinks(end1)); - - obj2.removeLink(end1, obj1); - obj2.addLink(end1, obj1); - - assertEquals(Collections.singletonList(obj2), obj1.getLinks(end2)); - assertEquals(Collections.singletonList(obj1), obj2.getLinks(end1)); - } - - @Test - public void testOneToOneRemoveAllLinks() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CAssociationEnd end1 = cl1.createEnd("class1", "1"), - end2 = cl2.createEnd("class2", "1"); - model.createAssociation(end1, end2); - - CObject obj1 = model.createObject(cl1, "obj1"); - CObject obj2 = model.createObject(cl2, "obj2"); - - obj1.removeAllLinks(end2); - - obj1.setLinks(end2, Collections.singletonList(obj2)); - - assertEquals(Collections.singletonList(obj2), obj1.getLinks(end2)); - assertEquals(Collections.singletonList(obj1), obj2.getLinks(end1)); - - obj1.removeAllLinks(end2); - - assertEquals(Collections.emptyList(), obj1.getLinks(end2)); - assertEquals(Collections.emptyList(), obj2.getLinks(end1)); - - obj1.setLinks(end2, Collections.singletonList(obj2)); - - assertEquals(Collections.singletonList(obj2), obj1.getLinks(end2)); - assertEquals(Collections.singletonList(obj1), obj2.getLinks(end1)); - - obj2.removeAllLinks(end1); - - assertEquals(Collections.emptyList(), obj1.getLinks(end2)); - assertEquals(Collections.emptyList(), obj2.getLinks(end1)); - } - - @Test - public void testOneToOneRemoveLinks() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CAssociationEnd end1 = cl1.createEnd("class1", "1"), - end2 = cl2.createEnd("class2", "1"); - model.createAssociation(end1, end2); - - CObject obj1 = model.createObject(cl1, "obj1"); - CObject obj2 = model.createObject(cl2, "obj2"); - - try { - obj1.removeLink(end2, obj2); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("link between 'obj1' and 'obj2' can't be removed: it does not exist", e.getMessage()); - } - - try { - obj1.removeLinks(end2, Collections.singletonList(obj2)); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("link between 'obj1' and 'obj2' can't be removed: it does not exist", e.getMessage()); - } - - obj1.setLinks(end2, Collections.singletonList(obj2)); - - assertEquals(Collections.singletonList(obj2), obj1.getLinks(end2)); - assertEquals(Collections.singletonList(obj1), obj2.getLinks(end1)); - - obj1.removeLinks(end2, Collections.singletonList(obj2)); - - assertEquals(Collections.emptyList(), obj1.getLinks(end2)); - assertEquals(Collections.emptyList(), obj2.getLinks(end1)); - - obj1.setLink(end2, obj2); - - assertEquals(Collections.singletonList(obj2), obj1.getLinks(end2)); - assertEquals(Collections.singletonList(obj1), obj2.getLinks(end1)); - - obj2.removeLink(end1, obj1); - - assertEquals(Collections.emptyList(), obj1.getLinks(end2)); - assertEquals(Collections.emptyList(), obj2.getLinks(end1)); - } - - - @Test - public void testOneToOneLinkMultiplicity() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CAssociationEnd end1 = cl1.createEnd("class1", "1"), - end2 = cl2.createEnd("class2", "1"); - model.createAssociation(end1, end2); - CObject obj1 = model.createObject(cl1, "obj1"); - CObject obj2 = model.createObject(cl2, "obj2"); - - try { - obj1.setLinks(end2, Collections.emptyList()); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("link has wrong multiplicity '0', but should be '1'", e.getMessage()); - } - - try { - obj1.setLink(end2, (CObject) null); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("link has wrong multiplicity '0', but should be '1'", e.getMessage()); - } - - - obj1.addLinks(end2, Collections.emptyList()); - assertEquals(Collections.emptyList(), obj1.getLinks(end2)); - - obj1.addLink(end2, (CObject) null); - assertEquals(Collections.emptyList(), obj1.getLinks(end2)); - - CObject obj3 = model.createObject(cl2, "obj3"); - - try { - obj1.setLinks(end2, Arrays.asList(obj2, obj3)); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("link has wrong multiplicity '2', but should be '1'", e.getMessage()); - } - } - - @Test - public void testSetLinksByStrings() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CAssociationEnd end1 = cl1.createEnd("class1", "1"), - end2 = cl2.createEnd("class2", "*"); - model.createAssociation(end1, end2); - CObject obj1 = model.createObject(cl1, "obj1"); - CObject obj2 = model.createObject(cl2, "obj2"); - CObject obj3 = model.createObject(cl2, "obj3"); - - obj1.setLinks(end2, Collections.singletonList("obj2")); - assertEquals(Collections.singletonList(obj2), obj1.getLinks(end2)); - - obj1.setLinks(end2, Arrays.asList("obj2", "obj3")); - assertEquals(Arrays.asList(obj2, obj3), obj1.getLinks(end2)); - - - try { - obj1.setLinks(end2, Arrays.asList("obj2", "obj4")); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("object 'obj4' unknown", e.getMessage()); - } - - try { - Integer i = 4; - obj1.setLinks(end2, Arrays.asList("obj2", i)); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("argument '4' is not of type String or CObject", e.getMessage()); - } - } - - @Test - public void testSetLinksWithSubclassInstance() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CClass cl3 = model.createClass(mcl,"CL3").addSuperclass(cl2); - CAssociationEnd end1 = cl1.createEnd("class1", "1"), - end2 = cl2.createEnd("class2", "*"); - model.createAssociation(end1, end2); - CObject obj1 = model.createObject(cl1, "obj1"); - CObject obj2 = model.createObject(cl2, "obj2"); - CObject obj3 = model.createObject(cl3, "obj3"); - - obj1.setLinks(end2, Arrays.asList("obj2", "obj3")); - assertEquals(Arrays.asList(obj2, obj3), obj1.getLinks(end2)); - } - - @Test - public void testOneToOneLinkIncompatibleTypes() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CAssociationEnd end1 = cl1.createEnd("class1", "1"), - end2 = cl2.createEnd("class2", "1"); - model.createAssociation(end1, end2); - CObject obj1 = model.createObject(cl1, "obj1"); - CObject obj2 = model.createObject(cl2, "obj2"); - CObject obj3 = model.createObject(cl2, "obj3"); - - try { - obj1.setLinks(end2, Arrays.asList(obj1, obj2, obj3)); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("link object 'obj1' not compatible with association classifier 'Class2'", e.getMessage()); - } - } - - @Test - public void testOneToOneLinkIsNavigable() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CAssociationEnd end1 = cl1.createEnd("class1", "1"); - end1.setNavigable(false); - CAssociationEnd end2 = cl2.createEnd("class2", "1"); - end2.setNavigable(false); - model.createAssociation(end1, end2); - - CObject obj1 = model.createObject(cl1, "obj1"); - CObject obj2 = model.createObject(cl2, "obj2"); - - try { - obj1.setLinks(end2, Collections.singletonList(obj2)); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'class2' is not navigable and thus cannot be accessed from object 'obj1'", e.getMessage()); - } - - try { - obj2.setLinks(end1, Collections.singletonList(obj1)); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'class1' is not navigable and thus cannot be accessed from object 'obj2'", e.getMessage()); - } - - try { - obj1.getLinks(end2); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'class2' is not navigable and thus cannot be accessed from object 'obj1'", e.getMessage()); - } - - try { - obj2.getLinks(end1); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'class1' is not navigable and thus cannot be accessed from object 'obj2'", e.getMessage()); - } - - try { - obj1.removeAllLinks(end2); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'class2' is not navigable and thus cannot be accessed from object 'obj1'", e.getMessage()); - } - - try { - obj2.removeAllLinks(end1); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'class1' is not navigable and thus cannot be accessed from object 'obj2'", e.getMessage()); - } - - try { - obj1.removeLink(end2, obj2); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'class2' is not navigable and thus cannot be accessed from object 'obj1'", e.getMessage()); - } - - try { - obj2.removeLink(end1, obj1); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'class1' is not navigable and thus cannot be accessed from object 'obj2'", e.getMessage()); - } - - end1.setNavigable(true); - - try { - obj1.setLink(end2, obj2); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'class2' is not navigable and thus cannot be accessed from object 'obj1'", e.getMessage()); - } - - obj2.setLink(end1, obj1); - - try { - obj1.getLinks(end2); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'class2' is not navigable and thus cannot be accessed from object 'obj1'", e.getMessage()); - } - - assertEquals(Collections.singletonList(obj1), obj2.getLinks(end1)); - - try { - obj1.removeAllLinks(end2); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'class2' is not navigable and thus cannot be accessed from object 'obj1'", e.getMessage()); - } - - obj2.removeAllLinks(end1); - assertEquals(Collections.emptyList(), obj2.getLinks(end1)); - - - try { - obj1.removeLink(end2, obj2); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'class2' is not navigable and thus cannot be accessed from object 'obj1'", e.getMessage()); - } - - obj2.addLink(end1, obj1); - assertEquals(Collections.singletonList(obj1), obj2.getLinks(end1)); - - obj2.removeLink(end1, obj1); - assertEquals(Collections.emptyList(), obj2.getLinks(end1)); - } - - - @Test - public void testOneToOneAssociationUnknown() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CAssociationEnd end1 = cl1.createEnd("class1Forward", "1"); - CAssociationEnd end1Back = cl1.createEnd("class1Back", "1"); - model.createAssociation(end1, end1Back); - - CObject obj2 = model.createObject(cl2, "obj2"); - - try { - obj2.setLinks(end1, Collections.emptyList()); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'class1Forward' is not an association target end of 'obj2'", e.getMessage()); - } - } - - - @Test - public void testOneToNLink() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CAssociationEnd end1 = cl1.createEnd("class1", "1"), - end2 = cl2.createEnd("class2", "*"); - model.createAssociation(end1, end2); - CObject obj1 = model.createObject(cl1, "obj1"); - CObject obj2a = model.createObject(cl2, "obj2a"); - CObject obj2b = model.createObject(cl2, "obj2b"); - - obj1.setLinks(end2, Arrays.asList(obj2a, obj2b)); - - assertEquals(Arrays.asList(obj2a, obj2b), obj1.getLinks(end2)); - assertEquals(Collections.singletonList(obj1), obj2a.getLinks(end1)); - assertEquals(Collections.singletonList(obj1), obj2b.getLinks(end1)); - - obj1.setLinks(end2, Collections.singletonList(obj2a)); - - assertEquals(Collections.singletonList(obj2a), obj1.getLinks(end2)); - assertEquals(Collections.singletonList(obj1), obj2a.getLinks(end1)); - assertEquals(Collections.emptyList(), obj2b.getLinks(end1)); - - obj2b.setLinks(end1, Collections.singletonList(obj1)); - - assertEquals(Arrays.asList(obj2a, obj2b), obj1.getLinks(end2)); - assertEquals(Collections.singletonList(obj1), obj2a.getLinks(end1)); - assertEquals(Collections.singletonList(obj1), obj2b.getLinks(end1)); - } - - @Test - public void testOneToNLinkDelete() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CAssociationEnd end1 = cl1.createEnd("class1", "1"), - end2 = cl2.createEnd("class2", "*"); - model.createAssociation(end1, end2); - CObject obj1 = model.createObject(cl1, "obj1"); - CObject obj2a = model.createObject(cl2, "obj2a"); - CObject obj2b = model.createObject(cl2, "obj2b"); - - obj1.removeAllLinks(end2); - - obj1.setLinks(end2, Arrays.asList(obj2a, obj2b)); - - assertEquals(Arrays.asList(obj2a, obj2b), obj1.getLinks(end2)); - assertEquals(Collections.singletonList(obj1), obj2a.getLinks(end1)); - assertEquals(Collections.singletonList(obj1), obj2b.getLinks(end1)); - - obj1.removeAllLinks(end2); - - assertEquals(Collections.emptyList(), obj1.getLinks(end2)); - assertEquals(Collections.emptyList(), obj2a.getLinks(end1)); - assertEquals(Collections.emptyList(), obj2b.getLinks(end1)); - - obj1.setLinks(end2, Arrays.asList(obj2a, obj2b)); - - assertEquals(Arrays.asList(obj2a, obj2b), obj1.getLinks(end2)); - assertEquals(Collections.singletonList(obj1), obj2a.getLinks(end1)); - assertEquals(Collections.singletonList(obj1), obj2b.getLinks(end1)); - - obj2a.removeAllLinks(end1); - - assertEquals(Collections.singletonList(obj2b), obj1.getLinks(end2)); - assertEquals(Collections.emptyList(), obj2a.getLinks(end1)); - assertEquals(Collections.singletonList(obj1), obj2b.getLinks(end1)); - } - - @Test - public void testOneTo2NLinkMultiplicity() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CAssociationEnd end1 = cl1.createEnd("class1", "1"), - end2 = cl2.createEnd("class2", "2..*"); - model.createAssociation(end1, end2); - CObject obj1 = model.createObject(cl1, "obj1"); - CObject obj2a = model.createObject(cl2, "obj2a"); - - try { - obj1.setLinks(end2, Collections.emptyList()); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("link has wrong multiplicity '0', but should be '2..*'", e.getMessage()); - } - - try { - obj1.setLinks(end2, Collections.singletonList(obj2a)); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("link has wrong multiplicity '1', but should be '2..*'", e.getMessage()); - } - } - - - @Test - public void testNToNLink() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CAssociationEnd end1 = cl1.createEnd("class1", "*"), - end2 = cl2.createEnd("class2", "*"); - model.createAssociation(end1, end2); - - CObject obj1a = model.createObject(cl1, "obj1a"); - CObject obj1b = model.createObject(cl1, "obj1b"); - CObject obj1c = model.createObject(cl1, "obj1c"); - CObject obj2a = model.createObject(cl2, "obj2a"); - CObject obj2b = model.createObject(cl2, "obj2b"); - - obj1a.setLinks(end2, Arrays.asList(obj2a, obj2b)); - obj1b.setLinks(end2, Collections.singletonList(obj2a)); - obj1c.setLinks(end2, Collections.singletonList(obj2b)); - - assertEquals(Arrays.asList(obj2a, obj2b), obj1a.getLinks(end2)); - assertEquals(Collections.singletonList(obj2a), obj1b.getLinks(end2)); - assertEquals(Collections.singletonList(obj2b), obj1c.getLinks(end2)); - assertEquals(Arrays.asList(obj1a, obj1b), obj2a.getLinks(end1)); - assertEquals(Arrays.asList(obj1a, obj1c), obj2b.getLinks(end1)); - - obj2a.setLinks(end1, Arrays.asList(obj1a, obj1b)); - obj2b.setLinks(end1, Collections.emptyList()); - - assertEquals(Collections.singletonList(obj2a), obj1a.getLinks(end2)); - assertEquals(Collections.singletonList(obj2a), obj1b.getLinks(end2)); - assertEquals(Collections.emptyList(), obj1c.getLinks(end2)); - assertEquals(Arrays.asList(obj1a, obj1b), obj2a.getLinks(end1)); - assertEquals(Collections.emptyList(), obj2b.getLinks(end1)); - } - - @Test - public void testAssociationGetLinks() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CAssociationEnd end1 = cl1.createEnd("class1", "*"), - end2 = cl2.createEnd("class2", "*"); - CAssociation association = model.createAssociation(end1, end2); - - CObject obj1a = model.createObject(cl1, "obj1a"); - CObject obj1b = model.createObject(cl1, "obj1b"); - CObject obj1c = model.createObject(cl1, "obj1c"); - CObject obj2a = model.createObject(cl2, "obj2a"); - CObject obj2b = model.createObject(cl2, "obj2b"); - - obj1a.setLinks(end2, Arrays.asList(obj2a, obj2b)); - obj1b.setLinks(end2, Collections.singletonList(obj2a)); - obj1c.setLinks(end2, Collections.singletonList(obj2b)); - - List links = association.getLinks(); - - assertEquals(4, links.size()); - assertEquals(Arrays.asList(obj1a, obj2a), links.get(0).getLinkedObjects()); - assertEquals(Arrays.asList(obj1a, obj2b), links.get(1).getLinkedObjects()); - assertEquals(Arrays.asList(obj1b, obj2a), links.get(2).getLinkedObjects()); - assertEquals(Arrays.asList(obj1c, obj2b), links.get(3).getLinkedObjects()); - } - - @Test - public void testNToNLinkRemoveAll() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CAssociationEnd end1 = cl1.createEnd("class1", "*"), - end2 = cl2.createEnd("class2", "*"); - model.createAssociation(end1, end2); - CObject obj1a = model.createObject(cl1, "obj1a"); - CObject obj1b = model.createObject(cl1, "obj1b"); - CObject obj1c = model.createObject(cl1, "obj1c"); - CObject obj2a = model.createObject(cl2, "obj2a"); - CObject obj2b = model.createObject(cl2, "obj2b"); - - obj1a.setLinks(end2, Arrays.asList(obj2a, obj2b)); - obj1b.setLinks(end2, Collections.singletonList(obj2a)); - obj1c.setLinks(end2, Collections.singletonList(obj2b)); - - assertEquals(Arrays.asList(obj2a, obj2b), obj1a.getLinks(end2)); - assertEquals(Collections.singletonList(obj2a), obj1b.getLinks(end2)); - assertEquals(Collections.singletonList(obj2b), obj1c.getLinks(end2)); - assertEquals(Arrays.asList(obj1a, obj1b), obj2a.getLinks(end1)); - assertEquals(Arrays.asList(obj1a, obj1c), obj2b.getLinks(end1)); - - obj2b.removeAllLinks(end1); - - assertEquals(Collections.singletonList(obj2a), obj1a.getLinks(end2)); - assertEquals(Collections.singletonList(obj2a), obj1b.getLinks(end2)); - assertEquals(Collections.emptyList(), obj1c.getLinks(end2)); - assertEquals(Arrays.asList(obj1a, obj1b), obj2a.getLinks(end1)); - assertEquals(Collections.emptyList(), obj2b.getLinks(end1)); - } - - @Test - public void testNToNLinkRemoveLinks() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CAssociationEnd end1 = cl1.createEnd("class1", "*"), - end2 = cl2.createEnd("class2", "*"); - model.createAssociation(end1, end2); - CObject obj1a = model.createObject(cl1, "obj1a"); - CObject obj1b = model.createObject(cl1, "obj1b"); - CObject obj1c = model.createObject(cl1, "obj1c"); - CObject obj2a = model.createObject(cl2, "obj2a"); - CObject obj2b = model.createObject(cl2, "obj2b"); - - obj1a.setLinks(end2, Arrays.asList(obj2a, obj2b)); - obj1b.setLinks(end2, Collections.singletonList(obj2a)); - obj1c.setLinks(end2, Collections.singletonList(obj2b)); - - assertEquals(Arrays.asList(obj2a, obj2b), obj1a.getLinks(end2)); - assertEquals(Collections.singletonList(obj2a), obj1b.getLinks(end2)); - assertEquals(Collections.singletonList(obj2b), obj1c.getLinks(end2)); - assertEquals(Arrays.asList(obj1a, obj1b), obj2a.getLinks(end1)); - assertEquals(Arrays.asList(obj1a, obj1c), obj2b.getLinks(end1)); - - obj2b.removeLinks(end1, Arrays.asList(obj1a, "obj1c")); - obj1a.removeLink(end2, "obj2a"); - - assertEquals(Collections.emptyList(), obj1a.getLinks(end2)); - assertEquals(Collections.singletonList(obj2a), obj1b.getLinks(end2)); - assertEquals(Collections.emptyList(), obj1c.getLinks(end2)); - assertEquals(Collections.singletonList(obj1b), obj2a.getLinks(end1)); - assertEquals(Collections.emptyList(), obj2b.getLinks(end1)); - } - - - @Test - public void testNToNSetSelfLink() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - - CAssociationEnd superEnd = cl1.createEnd("super", "*"), - subEnd = cl1.createEnd("sub", "*"); - model.createAssociation(superEnd, subEnd); - - CObject top = model.createObject(cl1, "Top"); - CObject mid1 = model.createObject(cl1, "Mid1"); - CObject mid2 = model.createObject(cl1, "Mid2"); - CObject mid3 = model.createObject(cl1, "Mid3"); - CObject bottom1 = model.createObject(cl1, "Bottom1"); - CObject bottom2 = model.createObject(cl1, "Bottom2"); - - top.setLinks(subEnd, Arrays.asList(mid1, mid2, mid3)); - mid1.setLinks(subEnd, Arrays.asList(bottom1, bottom2)); - - assertEquals(Collections.emptyList(), top.getLinks(superEnd)); - assertEquals(Arrays.asList(mid1, mid2, mid3), top.getLinks(subEnd)); - assertEquals(Collections.singletonList(top), mid1.getLinks(superEnd)); - assertEquals(Arrays.asList(bottom1, bottom2), mid1.getLinks(subEnd)); - assertEquals(Collections.singletonList(top), mid2.getLinks(superEnd)); - assertEquals(Collections.emptyList(), mid2.getLinks(subEnd)); - assertEquals(Collections.singletonList(top), mid3.getLinks(superEnd)); - assertEquals(Collections.emptyList(), mid3.getLinks(subEnd)); - assertEquals(Collections.singletonList(mid1), bottom1.getLinks(superEnd)); - assertEquals(Collections.emptyList(), bottom1.getLinks(subEnd)); - assertEquals(Collections.singletonList(mid1), bottom2.getLinks(superEnd)); - assertEquals(Collections.emptyList(), bottom2.getLinks(subEnd)); - } - - @Test - public void testNToNAddSelfLink() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CAssociationEnd superEnd = cl1.createEnd("super", "*"), - subEnd = cl1.createEnd("sub", "*"); - model.createAssociation(superEnd, subEnd); - - CObject top = model.createObject(cl1, "Top"); - CObject mid1 = model.createObject(cl1, "Mid1"); - CObject mid2 = model.createObject(cl1, "Mid2"); - CObject mid3 = model.createObject(cl1, "Mid3"); - CObject bottom1 = model.createObject(cl1, "Bottom1"); - CObject bottom2 = model.createObject(cl1, "Bottom2"); - - top.addLinks(subEnd, Arrays.asList(mid1, mid2, mid3)); - mid1.addLinks(subEnd, Arrays.asList(bottom1, bottom2)); - - assertEquals(Collections.emptyList(), top.getLinks(superEnd)); - assertEquals(Arrays.asList(mid1, mid2, mid3), top.getLinks(subEnd)); - assertEquals(Collections.singletonList(top), mid1.getLinks(superEnd)); - assertEquals(Arrays.asList(bottom1, bottom2), mid1.getLinks(subEnd)); - assertEquals(Collections.singletonList(top), mid2.getLinks(superEnd)); - assertEquals(Collections.emptyList(), mid2.getLinks(subEnd)); - assertEquals(Collections.singletonList(top), mid3.getLinks(superEnd)); - assertEquals(Collections.emptyList(), mid3.getLinks(subEnd)); - assertEquals(Collections.singletonList(mid1), bottom1.getLinks(superEnd)); - assertEquals(Collections.emptyList(), bottom1.getLinks(subEnd)); - assertEquals(Collections.singletonList(mid1), bottom2.getLinks(superEnd)); - assertEquals(Collections.emptyList(), bottom2.getLinks(subEnd)); - - } - - - - @Test - public void testUnknownEnd() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CAssociationEnd end1 = cl1.createEnd("class1", "1"), - end2 = cl2.createEnd("class2", "*"), - end3 = cl2.createEnd("class2", "*"); - model.createAssociation(end1, end2); - - CObject obj2 = model.createObject(cl2, "obj2"); - CObject obj1 = model.createObject(cl1, "obj1"); - try { - obj1.setLink(end3, obj2); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'class2' is not an association target end of 'obj1'", e.getMessage()); - } - try { - obj1.setLinks(end3, Collections.singletonList(obj2)); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'class2' is not an association target end of 'obj1'", e.getMessage()); - } - obj1.setLink(end2, obj2); - - try { - assertEquals(Collections.singletonList(obj2), obj1.getLinks(end3)); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'class2' is not an association target end of 'obj1'", e.getMessage()); - } - assertEquals(Collections.singletonList(obj2), obj1.getLinks(end2)); - - try { - obj1.removeAllLinks(end3); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("end with role name 'class2' is not an association target end of 'obj1'", e.getMessage()); - } - } - - @Test - public void testSetOneToOneLinkInheritance1() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CClass cl2Sub = model.createClass(mcl,"CL2Sub").addSuperclass(cl2); - CAssociation association = model.createAssociation(cl1.createEnd("class1", "1"), - cl2.createEnd("class2", "1")); - CAssociationEnd cl1End = association.getEndByRoleName("class1"); - CAssociationEnd cl2End = association.getEndByRoleName("class2"); - - CObject obj2 = model.createObject(cl2Sub, "obj2"); - CObject obj1 = model.createObject(cl1, "obj1"); - obj1.setLink(cl2End, obj2); - CObject obj3 = model.createObject(cl2Sub, "obj3"); - - assertEquals(Collections.singletonList(obj2), obj1.getLinks(cl2End)); - assertEquals(Collections.singletonList(obj1), obj2.getLinks(cl1End)); - - obj1.setLink(association.getEndByClassifier(cl2), "obj3"); - - assertEquals(Collections.singletonList(obj3), obj1.getLinks(cl2End)); - assertEquals(Collections.singletonList(obj1), obj3.getLinks(cl1End)); - } - - - @Test - public void testSetOneToOneLinkInheritance2() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CClass cl1Sub = model.createClass(mcl,"CL1Sub").addSuperclass(cl1); - CClass cl2Sub = model.createClass(mcl,"CL2Sub").addSuperclass(cl2).addSuperclass(cl1); - CAssociation association = model.createAssociation(cl1.createEnd("class1", "1"), - cl2.createEnd("class2", "1")); - CAssociationEnd cl1End = association.getEndByRoleName("class1"); - CAssociationEnd cl2End = association.getEndByRoleName("class2"); - - CObject obj2 = model.createObject(cl2Sub, "obj2"); - CObject obj1 = model.createObject(cl1Sub, "obj1"); - obj1.setLink(cl2End, obj2); - CObject obj3 = model.createObject(cl2Sub, "obj3"); - - assertEquals(Collections.singletonList(obj2), obj1.getLinks(cl2End)); - assertEquals(Collections.singletonList(obj1), obj2.getLinks(cl1End)); - - obj1.setLink(association.getEndByClassifier(cl2), "obj3"); - - assertEquals(Collections.singletonList(obj3), obj1.getLinks(cl2End)); - assertEquals(Collections.singletonList(obj1), obj3.getLinks(cl1End)); - } - - - @Test - public void testOneToNLinkDeleteInheritance() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CClass cl1Sub = model.createClass(mcl, "CL1Sub").addSuperclass(cl1); - CClass cl2Sub = model.createClass(mcl,"CL2Sub").addSuperclass(cl2); - CAssociationEnd end1 = cl1.createEnd("class1", "1"), - end2 = cl2.createEnd("class2", "*"); - model.createAssociation(end1, end2); - CObject obj1 = model.createObject(cl1Sub, "obj1"); - CObject obj2a = model.createObject(cl2Sub, "obj2a"); - CObject obj2b = model.createObject(cl2Sub, "obj2b"); - - obj1.removeAllLinks(end2); - - obj1.setLinks(end2, Arrays.asList(obj2a, obj2b)); - - assertEquals(Arrays.asList(obj2a, obj2b), obj1.getLinks(end2)); - assertEquals(Collections.singletonList(obj1), obj2a.getLinks(end1)); - assertEquals(Collections.singletonList(obj1), obj2b.getLinks(end1)); - - obj1.removeAllLinks(end2); - - assertEquals(Collections.emptyList(), obj1.getLinks(end2)); - assertEquals(Collections.emptyList(), obj2a.getLinks(end1)); - assertEquals(Collections.emptyList(), obj2b.getLinks(end1)); - - obj1.setLinks(end2, Arrays.asList(obj2a, obj2b)); - - assertEquals(Arrays.asList(obj2a, obj2b), obj1.getLinks(end2)); - assertEquals(Collections.singletonList(obj1), obj2a.getLinks(end1)); - assertEquals(Collections.singletonList(obj1), obj2b.getLinks(end1)); - - obj2a.removeAllLinks(end1); - - assertEquals(Collections.singletonList(obj2b), obj1.getLinks(end2)); - assertEquals(Collections.emptyList(), obj2a.getLinks(end1)); - assertEquals(Collections.singletonList(obj1), obj2b.getLinks(end1)); - } - - @Test - public void testNToNLinkRemoveAllInheritance() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CClass cl1Sub = model.createClass(mcl,"CL1Sub").addSuperclass(cl1); - CClass cl2Sub = model.createClass(mcl,"CL2Sub").addSuperclass(cl2); - CAssociationEnd end1 = cl1.createEnd("class1", "*"), - end2 = cl2.createEnd("class2", "*"); - model.createAssociation(end1, end2); - CObject obj1a = model.createObject(cl1Sub, "obj1a"); - CObject obj1b = model.createObject(cl1Sub, "obj1b"); - CObject obj1c = model.createObject(cl1Sub, "obj1c"); - CObject obj2a = model.createObject(cl2Sub, "obj2a"); - CObject obj2b = model.createObject(cl2Sub, "obj2b"); - - obj1a.setLinks(end2, Arrays.asList(obj2a, obj2b)); - obj1b.setLinks(end2, Collections.singletonList(obj2a)); - obj1c.setLinks(end2, Collections.singletonList(obj2b)); - - assertEquals(Arrays.asList(obj2a, obj2b), obj1a.getLinks(end2)); - assertEquals(Collections.singletonList(obj2a), obj1b.getLinks(end2)); - assertEquals(Collections.singletonList(obj2b), obj1c.getLinks(end2)); - assertEquals(Arrays.asList(obj1a, obj1b), obj2a.getLinks(end1)); - assertEquals(Arrays.asList(obj1a, obj1c), obj2b.getLinks(end1)); - - obj2b.removeAllLinks(end1); - - assertEquals(Collections.singletonList(obj2a), obj1a.getLinks(end2)); - assertEquals(Collections.singletonList(obj2a), obj1b.getLinks(end2)); - assertEquals(Collections.emptyList(), obj1c.getLinks(end2)); - assertEquals(Arrays.asList(obj1a, obj1b), obj2a.getLinks(end1)); - assertEquals(Collections.emptyList(), obj2b.getLinks(end1)); - } - - @Test - public void testGetLinkObjects() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - CClass cl1Sub = model.createClass(mcl,"CL1Sub").addSuperclass(cl1); - CClass cl2Sub = model.createClass(mcl,"CL2Sub").addSuperclass(cl2); - CAssociationEnd end1 = cl1.createEnd("class1", "*"), - end2 = cl2.createEnd("class2", "*"); - CAssociation association1 = model.createAssociation(end1, end2); - - CAssociationEnd end3 = cl1.createEnd("prior", "1"), - end4 = cl1.createEnd("next", "1"); - CAssociation association2 = model.createAssociation(end3, end4); - - CObject obj1 = model.createObject(cl1, "obj1"); - CObject obj2 = model.createObject(cl2, "obj2"); - CObject obj_cl1Sub = model.createObject(cl1Sub, "obj1_cl1Sub"); - CObject obj_cl2Sub = model.createObject(cl2Sub, "obj2_cl2Sub"); - - CLink link, linkOrig; - - linkOrig = obj1.setLink(end2, obj2); - link = obj1.getLinkObjects(end2).get(0); - assertEquals(linkOrig, link); - assertEquals(Arrays.asList(obj1, obj2), link.getLinkedObjects()); - - assertEquals(association1, link.getAssociation()); - - assertEquals(obj1, link.getLinkedObjectByName("obj1")); - assertEquals(obj2, link.getLinkedObjectByName("obj2")); - assertEquals(null, link.getLinkedObjectByName("obj3")); - - assertEquals(obj1, link.getLinkedObjectByClassifier(cl1)); - assertEquals(obj2, link.getLinkedObjectByClassifier(cl2)); - assertEquals(null, link.getLinkedObjectByClassifier(model.createClass(mcl, "ClassX"))); - - linkOrig = obj1.setLink(end2, obj_cl2Sub); - link = obj1.getLinkObjects(end2).get(0); - assertEquals(linkOrig, link); - assertEquals(Arrays.asList(obj1, obj_cl2Sub), link.getLinkedObjects()); - - linkOrig = obj1.setLink(end2, obj2); - link = obj1.getLinkObjects(association1).get(0); - assertEquals(linkOrig, link); - assertEquals(Arrays.asList(obj1, obj2), link.getLinkedObjects()); - - linkOrig = obj1.setLink(end2, obj_cl2Sub); - link = obj1.getLinkObjects(association1).get(0); - assertEquals(linkOrig, link); - assertEquals(Arrays.asList(obj1, obj_cl2Sub), link.getLinkedObjects()); - - linkOrig = obj1.setLink(end4, obj1); - link = obj1.getLinkObjects(end4).get(0); - assertEquals(linkOrig, link); - assertEquals(Arrays.asList(obj1, obj1), link.getLinkedObjects()); - - linkOrig = obj1.setLink(end4, obj_cl1Sub); - link = obj1.getLinkObjects(end4).get(0); - assertEquals(linkOrig, link); - assertEquals(Arrays.asList(obj1, obj_cl1Sub), link.getLinkedObjects()); - - linkOrig = obj1.setLink(end4, obj1); - link = obj1.getLinkObjects(association2).get(0); - assertEquals(linkOrig, link); - assertEquals(Arrays.asList(obj1, obj1), link.getLinkedObjects()); - - obj1.removeAllLinks(end4); - - linkOrig = obj_cl1Sub.setLink(end4, obj1); - link = obj_cl1Sub.getLinkObjects(association2).get(0); - assertEquals(linkOrig, link); - assertEquals(Arrays.asList(obj_cl1Sub, obj1), link.getLinkedObjects()); - } - - @Test - public void testGetLinkObjectsSelfLink() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - - CAssociationEnd end1 = cl1.createEnd("from", "*"), - end2 = cl1.createEnd("to", "*"); - CAssociation association = model.createAssociation(end1, end2); - - CObject obj1 = model.createObject(cl1, "obj1"); - CObject obj2 = model.createObject(cl1, "obj2"); - - CLink link, linkOrig; - - // get link objects via association - linkOrig = obj1.setLink(end2, obj2); - link = obj1.getLinkObjects(association).get(0); - assertEquals(linkOrig, link); - assertEquals(Arrays.asList(obj1, obj2), link.getLinkedObjects()); - - // get link objects via end - linkOrig = obj1.setLink(end2, obj2); - link = obj1.getLinkObjects(end2).get(0); - assertEquals(linkOrig, link); - assertEquals(Arrays.asList(obj1, obj2), link.getLinkedObjects()); - - // test the same with the other end - CObject obj3 = model.createObject(cl1, "obj3"); - CObject obj4 = model.createObject(cl1, "obj4"); - - // get link objects via association - linkOrig = obj3.setLink(end1, obj4); - link = obj3.getLinkObjects(association).get(0); - assertEquals(linkOrig, link); - assertEquals(Arrays.asList(obj4, obj3), link.getLinkedObjects()); - - // get link objects via end - linkOrig = obj3.setLink(end1, obj4); - link = obj3.getLinkObjects(end1).get(0); - assertEquals(linkOrig, link); - assertEquals(Arrays.asList(obj4, obj3), link.getLinkedObjects()); - - } - - @Test - public void testGetLinkObjectsSetList() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - - CAssociationEnd end1 = cl1.createEnd("class1", "1"), - end2 = cl2.createEnd("class2", "*"); - model.createAssociation(end1, end2); - - CObject obj1 = model.createObject(cl1, "obj1"); - CObject obj2a = model.createObject(cl2, "obj2a"); - CObject obj2b = model.createObject(cl2, "obj2b"); - - List linksOrig = obj1.setLinks(end2, Arrays.asList(obj2a, obj2b)); - List links = obj1.getLinkObjects(end2); - assertEquals(linksOrig, links); - assertEquals(2, links.size()); - assertEquals(obj2a, links.get(0).getLinkedObjectAtTargetEnd(end2)); - assertEquals(obj2b, links.get(1).getLinkedObjectAtTargetEnd(end2)); - } - - @Test - public void testGetLinkObjectsAddList() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CClass cl1 = model.createClass(mcl, "Class1"); - CClass cl2 = model.createClass(mcl, "Class2"); - - CAssociationEnd end1 = cl1.createEnd("class1", "1"), - end2 = cl2.createEnd("class2", "*"); - model.createAssociation(end1, end2); - - CObject obj1 = model.createObject(cl1, "obj1"); - CObject obj2a = model.createObject(cl2, "obj2a"); - CObject obj2b = model.createObject(cl2, "obj2b"); - - List linksOrig = obj1.addLinks(end2, Arrays.asList(obj2a, obj2b)); - List links = obj1.getLinkObjects(end2); - assertEquals(linksOrig, links); - assertEquals(2, links.size()); - assertEquals(obj2a, links.get(0).getLinkedObjectAtTargetEnd(end2)); - assertEquals(obj2b, links.get(1).getLinkedObjectAtTargetEnd(end2)); - } - - -} diff --git a/src/codeableModels/tests/TestsStereotypeInstancesOnAssociations.java b/src/codeableModels/tests/TestsStereotypeInstancesOnAssociations.java deleted file mode 100644 index 6cc0144..0000000 --- a/src/codeableModels/tests/TestsStereotypeInstancesOnAssociations.java +++ /dev/null @@ -1,202 +0,0 @@ -package codeableModels.tests; - -import codeableModels.*; -import org.junit.*; - -import java.util.*; - -import static org.junit.Assert.*; - - -public class TestsStereotypeInstancesOnAssociations { - @Test - public void testStereotypeInstancesOnAssociations() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociation assoc1 = model.createAssociation(mcl1.createEnd("1"), mcl2.createEnd("1")); - CStereotype stereotype1 = model.createStereotype("ST1", assoc1); - CStereotype stereotype2 = model.createStereotype("ST2", assoc1); - CStereotype stereotype3 = model.createStereotype("ST3", assoc1); - CClass cl1 = model.createClass(mcl1, "CL1"); - CClass cl2 = model.createClass(mcl2, "CL2"); - CLink link = cl1.setLink(assoc1.getEnds().get(1), cl2); - - assertEquals(0, link.getStereotypeInstances().size()); - - link.addStereotypeInstance(stereotype1); - assertEquals(link.getStereotypeInstances(), Collections.singletonList(stereotype1)); - link.addStereotypeInstance(stereotype2); - link.addStereotypeInstance("ST3"); - assertEquals(link.getStereotypeInstances(), Arrays.asList(stereotype1, stereotype2, stereotype3)); - try { - link.addStereotypeInstance(stereotype2); - fail("exception not thrown"); - } catch (CException e) { - assertEquals( - "stereotype 'ST2' cannot be added: it is already a stereotype of '[CL1 -> CL2]'", - e.getMessage()); - } - assertEquals(link.getStereotypeInstances(), Arrays.asList(stereotype1, stereotype2, stereotype3)); - } - - @Test - public void testAddStereotypeWhichDoesNotExist() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociation assoc1 = model.createAssociation(mcl1.createEnd("1"), mcl2.createEnd("1")); - CClass cl1 = model.createClass(mcl1, "CL1"); - CClass cl2 = model.createClass(mcl2, "CL2"); - CLink link = cl1.setLink(assoc1.getEnds().get(1), cl2); - try { - link.addStereotypeInstance("Stereotype"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("stereotype 'Stereotype' does not exist", e.getMessage()); - } - } - - @Test - public void testAddStereotypeInstance() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociation assoc1 = model.createAssociation(mcl1.createEnd("*"), mcl2.createEnd("*")); - CClass cl1 = model.createClass(mcl1, "CL1"); - CClass cl2 = model.createClass(mcl2, "CL2"); - cl1.setLink(assoc1.getEnds().get(1), cl2); - CStereotype stereotype1 = model.createStereotype("ST1", assoc1); - CStereotype stereotype2 = model.createStereotype("ST2", assoc1); - CStereotype stereotype3 = model.createStereotype("ST3", assoc1); - CStereotype stereotype4 = model.createStereotype("ST4", assoc1); - - assertEquals(0, stereotype1.getStereotypedElementInstances().size()); - - CLink link1 = cl1.setLink(assoc1.getEnds().get(1), cl2); - link1.addStereotypeInstance(stereotype1); - CLink link2 = cl1.setLink(assoc1.getEnds().get(1), cl2); - link2.addStereotypeInstance("ST3"); - link2.addStereotypeInstance("ST4"); - - CLink link3 = cl1.setLink(assoc1.getEnds().get(1), cl2); - assertEquals(0, link3.getStereotypeInstances().size()); - - link3.addStereotypeInstance("ST2"); - assertEquals(1, link3.getStereotypeInstances().size()); - assertEquals(stereotype2, link3.getStereotypeInstances().get(0)); - assertEquals(1, stereotype2.getStereotypedElementInstances().size()); - assertEquals(link3, stereotype2.getStereotypedElementInstances().get(0)); - - assertEquals(1, link1.getStereotypeInstances().size()); - assertEquals(stereotype1, link1.getStereotypeInstances().get(0)); - - assertEquals(2, link2.getStereotypeInstances().size()); - assertEquals(Arrays.asList(stereotype3, stereotype4), link2.getStereotypeInstances()); - - link3.addStereotypeInstance(stereotype4); - assertEquals(2, stereotype4.getStereotypedElementInstances().size()); - assertEquals(Arrays.asList(link2, link3), stereotype4.getStereotypedElementInstances()); - } - - @Test - public void testDeleteStereotypeInstance() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociation assoc1 = model.createAssociation(mcl1.createEnd("*"), mcl2.createEnd("*")); - CClass cl1 = model.createClass(mcl1, "CL1"); - CClass cl2 = model.createClass(mcl2, "CL2"); - cl1.setLink(assoc1.getEnds().get(1), cl2); - CStereotype stereotype1 = model.createStereotype("ST1", assoc1); - CStereotype stereotype2 = model.createStereotype("ST2", assoc1); - CStereotype stereotype3 = model.createStereotype("ST3", assoc1); - CStereotype stereotype4 = model.createStereotype("ST4", assoc1); - CLink link1 = cl1.setLink(assoc1.getEnds().get(1), cl2); - link1.addStereotypeInstance(stereotype1); - CLink link2 = cl1.setLink(assoc1.getEnds().get(1), cl2); - link2.addStereotypeInstance("ST3"); - link2.addStereotypeInstance("ST4"); - CLink link3 = cl1.setLink(assoc1.getEnds().get(1), cl2); - - try { - link3.deleteStereotypeInstance(stereotype1); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("can't remove stereotype 'ST1' on '[CL1 -> CL2]': does not exist", e - .getMessage()); - } - - try { - link1.deleteStereotypeInstance(stereotype2); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("can't remove stereotype 'ST2' on '[CL1 -> CL2]': does not exist", e - .getMessage()); - } - - - link1.deleteStereotypeInstance("ST1"); - assertEquals(0, link1.getStereotypeInstances().size()); - assertEquals(0, stereotype1.getStereotypedElementInstances().size()); - - link3.addStereotypeInstance("ST1"); - link3.deleteStereotypeInstance("ST1"); - link3.addStereotypeInstance("ST3"); - link3.addStereotypeInstance("ST4"); - - link2.deleteStereotypeInstance("ST4"); - assertEquals(1, link2.getStereotypeInstances().size()); - assertEquals(stereotype3, link2.getStereotypeInstances().get(0)); - assertEquals(1, stereotype4.getStereotypedElementInstances().size()); - assertEquals(Collections.singletonList(link3), stereotype4.getStereotypedElementInstances()); - assertEquals(2, stereotype3.getStereotypedElementInstances().size()); - assertEquals(Arrays.asList(link2, link3), stereotype3.getStereotypedElementInstances()); - - link2.deleteStereotypeInstance("ST3"); - assertEquals(0, link2.getStereotypeInstances().size()); - assertEquals(1, stereotype3.getStereotypedElementInstances().size()); - assertEquals(Collections.singletonList(link3), stereotype3.getStereotypedElementInstances()); - } - - @Test - public void testAddStereotypeInstanceWrongMetaclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociation assoc1 = model.createAssociation(mcl1.createEnd("*"), mcl2.createEnd("*")); - CAssociation assoc2 = model.createAssociation(mcl1.createEnd("*"), mcl2.createEnd("*")); - CClass cl1 = model.createClass(mcl1, "CL1"); - CClass cl2 = model.createClass(mcl2, "CL2"); - CLink link = cl1.setLink(assoc1.getEnds().get(1), cl2); - - CStereotype stereotype1 = model.createStereotype("ST1", assoc2); - try { - link.addStereotypeInstance(stereotype1); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("stereotype 'ST1' cannot be added to '[CL1 -> CL2]': no extension by this stereotype found", - e.getMessage()); - } - } - - - @Test - public void testAddStereotypeInstanceAssociationCorrectByInheritanceOfStereotype() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociation assoc1 = model.createAssociation(mcl1.createEnd("*"), mcl2.createEnd("*")); - model.createAssociation(mcl1.createEnd("*"), mcl2.createEnd("*")); - CClass cl1 = model.createClass(mcl1, "CL1"); - CClass cl2 = model.createClass(mcl2, "CL2"); - CLink link = cl1.setLink(assoc1.getEnds().get(1), cl2); - - CStereotype stereotype1 = model.createStereotype("ST1", assoc1); - CStereotype stereotype2 = model.createStereotype("ST2").addSuperclass("ST1"); - - link.addStereotypeInstance("ST2"); - assertEquals(Collections.singletonList(link), stereotype2.getStereotypedElementInstances()); - assertEquals(0, stereotype1.getStereotypedElementInstances().size()); - } -} diff --git a/src/codeableModels/tests/TestsStereotypeInstancesOnClasses.java b/src/codeableModels/tests/TestsStereotypeInstancesOnClasses.java deleted file mode 100644 index 401e148..0000000 --- a/src/codeableModels/tests/TestsStereotypeInstancesOnClasses.java +++ /dev/null @@ -1,231 +0,0 @@ -package codeableModels.tests; - -import codeableModels.*; -import org.junit.*; - -import java.util.*; - -import static org.junit.Assert.*; - - -public class TestsStereotypeInstancesOnClasses { - @Test - public void testStereotypeInstancesOnClass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CStereotype stereotype1 = model.createStereotype("ST1", mcl); - CStereotype stereotype2 = model.createStereotype("ST2", mcl); - CStereotype stereotype3 = model.createStereotype("ST3", mcl); - CClass cl = model.createClass("MCL", "CL"); - assertEquals(0, cl.getStereotypeInstances().size()); - cl.addStereotypeInstance(stereotype1); - assertEquals(cl.getStereotypeInstances(), Collections.singletonList(stereotype1)); - cl.addStereotypeInstance(stereotype2); - cl.addStereotypeInstance("ST3"); - assertEquals(cl.getStereotypeInstances(), Arrays.asList(stereotype1, stereotype2, stereotype3)); - try { - cl.addStereotypeInstance(stereotype2); - fail("exception not thrown"); - } catch (CException e) { - assertEquals( - "stereotype 'ST2' cannot be added: it is already a stereotype of 'CL'", - e.getMessage()); - } - assertEquals(cl.getStereotypeInstances(), Arrays.asList(stereotype1, stereotype2, stereotype3)); - } - - @Test - public void testAddStereotypeWhichDoesNotExist() throws CException { - CModel model = CodeableModels.createModel(); - model.createMetaclass("MCL1"); - try { - model.createClass("MCL1", "CL2").addStereotypeInstance("Stereotype"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("stereotype 'Stereotype' does not exist", e.getMessage()); - } - } - - @Test - public void testAddStereotypeInstance() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CStereotype stereotype1 = model.createStereotype("ST1", mcl1); - CStereotype stereotype2 = model.createStereotype("ST2", mcl1); - CStereotype stereotype3 = model.createStereotype("ST3", mcl1); - CStereotype stereotype4 = model.createStereotype("ST4", mcl1); - - assertEquals(0, stereotype1.getStereotypedElementInstances().size()); - - CClass cl1 = model.createClass("MCL1", "CL1"); - cl1.addStereotypeInstance(stereotype1); - CClass cl2 = model.createClass("MCL1", "CL2"); - cl2.addStereotypeInstance("ST3"); - cl2.addStereotypeInstance("ST4"); - - CClass cl3 = model.createClass("MCL1", "CL3"); - assertEquals(0, cl3.getStereotypeInstances().size()); - - cl3.addStereotypeInstance("ST2"); - assertEquals(1, cl3.getStereotypeInstances().size()); - assertEquals(stereotype2, cl3.getStereotypeInstances().get(0)); - assertEquals(1, stereotype2.getStereotypedElementInstances().size()); - assertEquals(cl3, stereotype2.getStereotypedElementInstances().get(0)); - - assertEquals(1, cl1.getStereotypeInstances().size()); - assertEquals(stereotype1, cl1.getStereotypeInstances().get(0)); - - assertEquals(2, cl2.getStereotypeInstances().size()); - assertEquals(Arrays.asList(stereotype3, stereotype4), cl2.getStereotypeInstances()); - - cl3.addStereotypeInstance(stereotype4); - assertEquals(2, stereotype4.getStereotypedElementInstances().size()); - assertEquals(Arrays.asList(cl2, cl3), stereotype4.getStereotypedElementInstances()); - } - - @Test - public void testDeleteStereotypeInstance() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CStereotype stereotype1 = model.createStereotype("ST1", mcl1); - CStereotype stereotype2 = model.createStereotype("ST2", mcl2); - CStereotype stereotype3 = model.createStereotype("ST3", mcl1); - CStereotype stereotype4 = model.createStereotype("ST4", mcl1); - CClass cl1 = model.createClass("MCL1", "CL1"); - cl1.addStereotypeInstance(stereotype1); - CClass cl2 = model.createClass("MCL1", "CL2"); - cl2.addStereotypeInstance("ST3"); - cl2.addStereotypeInstance("ST4"); - CClass cl3 = model.createClass("MCL1", "CL3"); - - try { - cl3.deleteStereotypeInstance(stereotype1); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("can't remove stereotype 'ST1' on 'CL3': does not exist", e - .getMessage()); - } - - try { - cl1.deleteStereotypeInstance(stereotype2); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("can't remove stereotype 'ST2' on 'CL1': does not exist", e - .getMessage()); - } - - - cl1.deleteStereotypeInstance("ST1"); - assertEquals(0, cl1.getStereotypeInstances().size()); - assertEquals(0, stereotype1.getStereotypedElementInstances().size()); - - cl3.addStereotypeInstance("ST1"); - cl3.deleteStereotypeInstance("ST1"); - cl3.addStereotypeInstance("ST3"); - cl3.addStereotypeInstance("ST4"); - - cl2.deleteStereotypeInstance("ST4"); - assertEquals(1, cl2.getStereotypeInstances().size()); - assertEquals(stereotype3, cl2.getStereotypeInstances().get(0)); - assertEquals(1, stereotype4.getStereotypedElementInstances().size()); - assertEquals(Collections.singletonList(cl3), stereotype4.getStereotypedElementInstances()); - assertEquals(2, stereotype3.getStereotypedElementInstances().size()); - assertEquals(Arrays.asList(cl2, cl3), stereotype3.getStereotypedElementInstances()); - - cl2.deleteStereotypeInstance("ST3"); - assertEquals(0, cl2.getStereotypeInstances().size()); - assertEquals(1, stereotype3.getStereotypedElementInstances().size()); - assertEquals(Collections.singletonList(cl3), stereotype3.getStereotypedElementInstances()); - } - - - @Test - public void testAddStereotypeInstanceWrongMetaclass() throws CException { - CModel model = CodeableModels.createModel(); - model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CStereotype stereotype1 = model.createStereotype("ST1", mcl2); - CClass cl = model.createClass("MCL1", "CL"); - try { - cl.addStereotypeInstance(stereotype1); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("stereotype 'ST1' cannot be added to 'CL': no extension by this stereotype found", - e.getMessage()); - } - } - - @Test - public void testApplyStereotypeInstancesWrongMetaclassInheritance() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass stereotypeMetaclass = model.createMetaclass("SM"); - CMetaclass mcl1 = model.createMetaclass("MCL1").addSuperclass(stereotypeMetaclass); - CMetaclass mcl2 = model.createMetaclass("MCL2").addSuperclass(stereotypeMetaclass); - CStereotype stereotype1 = model.createStereotype("ST1", mcl1); - CStereotype stereotype2 = model.createStereotype("ST2", mcl2); - CStereotype s_stereotype = model.createStereotype("S_ST", stereotypeMetaclass); - - CClass smClass = model.createClass(stereotypeMetaclass, "SMClass"); - - try { - smClass.addStereotypeInstance(stereotype1); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("stereotype 'ST1' cannot be added to 'SMClass': no extension by this stereotype found", e.getMessage()); - } - smClass.addStereotypeInstance(s_stereotype); - - CClass mcl1Class = model.createClass(mcl1, "Mcl1Class"); - mcl1Class.addStereotypeInstance(stereotype1); - try { - mcl1Class.addStereotypeInstance(stereotype2); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("stereotype 'ST2' cannot be added to 'Mcl1Class': no extension by this stereotype found", - e.getMessage()); - } - mcl1Class.addStereotypeInstance(s_stereotype); - - assertEquals(Collections.singletonList(s_stereotype), smClass.getStereotypeInstances()); - assertEquals(Arrays.asList(stereotype1, s_stereotype), mcl1Class.getStereotypeInstances()); - } - - @Test - public void testAddStereotypeInstanceMetaclassCorrectByInheritanceOfMetaclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - model.createMetaclass("MCL2").addSuperclass(mcl1); - CStereotype stereotype1 = model.createStereotype("ST1", mcl1); - CClass cl = model.createClass("MCL2", "CL"); - cl.addStereotypeInstance("ST1"); - assertEquals(Collections.singletonList(cl), stereotype1.getStereotypedElementInstances()); - } - - @Test - public void testAddStereotypeInstanceMetaclassWrongInheritanceHierarchy() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2").addSuperclass(mcl1); - model.createStereotype("ST1", mcl2); - CClass cl = model.createClass("MCL1", "CL"); - try { - cl.addStereotypeInstance("ST1"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("stereotype 'ST1' cannot be added to 'CL': no extension by this stereotype found", e.getMessage()); - } - } - - @Test - public void testAddStereotypeInstanceMetaclassCorrectByInheritanceOfStereotype() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CStereotype stereotype1 = model.createStereotype("ST1", mcl2); - CStereotype stereotype2 = model.createStereotype("ST2").addSuperclass("ST1"); - CClass cl = model.createClass("MCL2", "CL"); - cl.addStereotypeInstance("ST2"); - assertEquals(Collections.singletonList(cl), stereotype2.getStereotypedElementInstances()); - assertEquals(0, stereotype1.getStereotypedElementInstances().size()); - } -} diff --git a/src/codeableModels/tests/TestsStereotypesOnAssociations.java b/src/codeableModels/tests/TestsStereotypesOnAssociations.java deleted file mode 100644 index 2cff987..0000000 --- a/src/codeableModels/tests/TestsStereotypesOnAssociations.java +++ /dev/null @@ -1,199 +0,0 @@ -package codeableModels.tests; - -import codeableModels.*; -import org.junit.*; - -import java.util.*; - -import static org.junit.Assert.*; - - -public class TestsStereotypesOnAssociations { - - @Test - public void testCreationOfOneStereotype() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociation assoc1 = model.createAssociation(mcl1.createEnd("1"), mcl2.createEnd("1")); - CStereotype stereotype = model.createStereotype("Stereotype1", assoc1); - - assertEquals(Collections.emptyList(), mcl1.getStereotypes()); - assertEquals(Collections.emptyList(), mcl2.getStereotypes()); - assertEquals(Collections.singletonList(stereotype), assoc1.getStereotypes()); - assertEquals(assoc1, stereotype.getStereotypedElements().get(0)); - } - - @Test - public void testCreationOfStereotypeOnClassAssociation() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL1"); - CClass cl1 = model.createClass(mcl,"CL1"); - CClass cl2 = model.createClass(mcl,"CL2"); - CAssociation assoc1 = model.createAssociation(cl1.createEnd("1"), cl2.createEnd("1")); - try { - model.createStereotype("Stereotype1", assoc1); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("association classifiers 'CL1' and/or 'CL2' are not metaclasses", e.getMessage()); - } - } - - @Test - public void testCreateAssociationStereotypeByName() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociation assoc1 = model.createAssociation("assoc1", mcl1.createEnd("1"), mcl2.createEnd("1")); - CStereotype stereotype = model.createStereotype("Stereotype1", "assoc1"); - - assertEquals(Collections.emptyList(), mcl1.getStereotypes()); - assertEquals(Collections.emptyList(), mcl2.getStereotypes()); - assertEquals(Collections.singletonList(stereotype), assoc1.getStereotypes()); - assertEquals(assoc1, stereotype.getStereotypedElements().get(0)); - } - - @Test - public void testCreateAssociationStereotypeByNameFail() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - - try { - model.createStereotype("Stereotype1", "assoc1"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("can't find association 'assoc1' to be stereotyped", e.getMessage()); - } - - model.createAssociation("assoc1", mcl1.createEnd("1"), mcl2.createEnd("1")); - model.createAssociation("assoc1", mcl1.createEnd("1"), mcl2.createEnd("1")); - - try { - model.createStereotype("Stereotype1", "assoc1"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("found multiple associations with the name 'assoc1', use reference to select stereotype instead", e.getMessage()); - } - } - - @Test - public void testCreationOfThreeStereotypes() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociation assoc1 = model.createAssociation(mcl1.createEnd("1"), mcl2.createEnd("1")); - CStereotype st1 = model.createStereotype("Stereotype1", assoc1); - CStereotype st2 = model.createStereotype("Stereotype2", assoc1); - CStereotype st3 = model.createStereotype("Stereotype3", assoc1); - - assertEquals(Arrays.asList(st1, st2, st3), assoc1.getStereotypes()); - assertEquals(assoc1, st1.getStereotypedElements().get(0)); - assertEquals(assoc1, st2.getStereotypedElements().get(0)); - assertEquals(assoc1, st3.getStereotypedElements().get(0)); - } - - @Test - public void testCreationOfThreeStereotypesOnExtendedElement() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociation assoc1 = model.createAssociation(mcl1.createEnd("1"), mcl2.createEnd("1")); - CStereotype st1 = model.createStereotype("Stereotype1"); - CStereotype st2 = model.createStereotype("Stereotype2"); - CStereotype st3 = model.createStereotype("Stereotype3"); - - assertEquals(Collections.emptyList(), assoc1.getStereotypes()); - assoc1.addStereotype("Stereotype1"); - assertEquals(Collections.singletonList(st1), assoc1.getStereotypes()); - assoc1.addStereotype("Stereotype2"); - assoc1.addStereotype(st3); - assertEquals(Arrays.asList(st1, st2, st3), assoc1.getStereotypes()); - } - - @Test - public void testAddRemoveStereotypes() throws CException { - CModel model = CodeableModels.createModel(); - CStereotype st1 = model.createStereotype("Stereotype1"); - CStereotype st2 = model.createStereotype("Stereotype2"); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociation assoc1 = model.createAssociation(mcl1.createEnd("1"), mcl2.createEnd("1")); - CAssociation assoc2 = model.createAssociation(mcl1.createEnd("1"), mcl2.createEnd("1")); - assertEquals(Collections.emptyList(), assoc1.getStereotypes()); - assertEquals(Collections.emptyList(), assoc2.getStereotypes()); - assertEquals(Collections.emptyList(), st1.getStereotypedElements()); - assertEquals(Collections.emptyList(), st2.getStereotypedElements()); - - assoc1.addStereotype(st1); - assoc1.addStereotype(st2); - assoc2.addStereotype(st2); - - assertEquals(Arrays.asList(st1, st2), assoc1.getStereotypes()); - assertEquals(Collections.singletonList(st2), assoc2.getStereotypes()); - assertEquals(Collections.singletonList(assoc1), st1.getStereotypedElements()); - assertEquals(Arrays.asList(assoc1, assoc2), st2.getStereotypedElements()); - - assoc1.removeStereotype(st1); - assertEquals(Collections.singletonList(st2), assoc1.getStereotypes()); - assoc1.removeStereotype(st2); - assertEquals(Collections.emptyList(), assoc1.getStereotypes()); - - assertEquals(Collections.singletonList(st2), assoc2.getStereotypes()); - assertEquals(Collections.emptyList(), st1.getStereotypedElements()); - assertEquals(Collections.singletonList(assoc2), st2.getStereotypedElements()); - } - - @Test - public void testStereotypeAddStereotypeTwice() throws CException { - CModel model = CodeableModels.createModel(); - CStereotype st1 = model.createStereotype("Stereotype1"); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociation assoc1 = model.createAssociation(mcl1.createEnd("1"), mcl2.createEnd("1")); - CAssociation assoc2 = model.createAssociation("named", mcl1.createEnd("1"), mcl2.createEnd("1")); - assoc1.addStereotype(st1); - assoc2.addStereotype(st1); - - try { - assoc1.addStereotype(st1); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("stereotype 'Stereotype1' already exists", e.getMessage()); - } - - try { - assoc2.addStereotype(st1); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("stereotype 'Stereotype1' already exists on 'named'", e.getMessage()); - } - - } - - @Test - public void testStereotypeRemoveNonExisting() throws CException { - CModel model = CodeableModels.createModel(); - CStereotype st1 = model.createStereotype("Stereotype1"); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociation assoc1 = model.createAssociation(mcl1.createEnd("1"), mcl2.createEnd("1")); - CAssociation assoc2 = model.createAssociation("named", mcl1.createEnd("1"), mcl2.createEnd("1")); - - try { - assoc1.removeStereotype(st1); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("can't remove stereotype 'Stereotype1': does not exist", e.getMessage()); - } - - try { - assoc2.removeStereotype(st1); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("can't remove stereotype 'Stereotype1' on 'named': does not exist", e.getMessage()); - } - } - - -} diff --git a/src/codeableModels/tests/TestsStereotypesOnClasses.java b/src/codeableModels/tests/TestsStereotypesOnClasses.java deleted file mode 100644 index df0e83e..0000000 --- a/src/codeableModels/tests/TestsStereotypesOnClasses.java +++ /dev/null @@ -1,330 +0,0 @@ -package codeableModels.tests; - -import codeableModels.*; -import org.junit.*; - -import java.util.*; - -import static org.junit.Assert.*; - - -public class TestsStereotypesOnClasses { - - @Test - public void testCreationOfOneStereotype() throws CException { - CModel model = CodeableModels.createModel(); - CStereotype st = model.createStereotype("Stereotype1"); - CClassifier st2 = model.getClassifier("Stereotype1"); - assertEquals(st, st2); - assertEquals(model, st2.getModel()); - } - - @Test - public void testCreationOfAutoNamedStereotype() throws CException { - CModel model = CodeableModels.createModel(); - CStereotype st = model.createStereotype(); - String stName = st.getName(); - assertTrue(stName.startsWith("##")); - CStereotype st2 = model.getStereotype(stName); - assertEquals(st, st2); - } - - @Test - public void testCreationOf3Stereotypes() throws CException { - CModel model = CodeableModels.createModel(); - CStereotype st1 = model.createStereotype("Stereotype1"); - CStereotype st2 = model.createStereotype("Stereotype2"); - CStereotype st3 = model.createStereotype("Stereotype3"); - CStereotype gst1 = model.getStereotype("Stereotype1"); - CStereotype gst2 = model.getStereotype("Stereotype2"); - CStereotype gst3 = model.getStereotype("Stereotype3"); - assertEquals(st1, gst1); - assertEquals(st2, gst2); - assertEquals(st3, gst3); - } - - @Test - public void testCreationOfOneStereotypeWithMetaclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CStereotype st = model.createStereotype("Stereotype1", mcl); - CClassifier st2 = model.getClassifier("Stereotype1"); - assertEquals(st, st2); - assertEquals(model, st2.getModel()); - } - - @Test - public void testCreationOf3StereotypesWithoutMetaclass() throws CException { - CModel model = CodeableModels.createModel(); - model.createMetaclass("MCL"); - CStereotype st1 = model.createStereotype("Stereotype1", "MCL"); - CStereotype st2 = model.createStereotype("Stereotype2", "MCL"); - CStereotype st3 = model.createStereotype("Stereotype3", "MCL"); - CStereotype gst1 = model.getStereotype("Stereotype1"); - CStereotype gst2 = model.getStereotype("Stereotype2"); - CStereotype gst3 = model.getStereotype("Stereotype3"); - assertEquals(st1, gst1); - assertEquals(st2, gst2); - assertEquals(st3, gst3); - } - - @Test - public void testStereotypeUniqueNameUsedException() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - model.createStereotype("Stereotype1", mcl); - try { - model.createStereotype("Stereotype1", mcl); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("classifier 'Stereotype1' cannot be created, classifier name already exists", e.getMessage()); - } - } - - @Test - public void testStereotypeExtensionAddRemove() throws CException { - CModel model = CodeableModels.createModel(); - CStereotype st = model.createStereotype("Stereotype1"); - assertEquals(0, st.getStereotypedElements().size()); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - mcl1.addStereotype("Stereotype1"); - assertEquals(mcl1, st.getStereotypedElements().get(0)); - assertEquals(st, mcl1.getStereotypes().get(0)); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - mcl1.removeStereotype("Stereotype1"); - mcl1.addStereotype(st); - mcl2.addStereotype(st); - assertEquals(Arrays.asList(mcl1, mcl2), st.getStereotypedElements()); - assertEquals(st, mcl2.getStereotypes().get(0)); - CStereotype st2 = model.createStereotype("Stereotype2", mcl1); - assertEquals(mcl1, st2.getStereotypedElements().get(0)); - assertEquals(Arrays.asList(st, st2), mcl1.getStereotypes()); - - mcl1.removeStereotype(st); - assertEquals(mcl2, st.getStereotypedElements().get(0)); - assertEquals(mcl1, st2.getStereotypedElements().get(0)); - assertEquals(st2, mcl1.getStereotypes().get(0)); - - mcl1.removeStereotype("Stereotype2"); - assertEquals(mcl2, st.getStereotypedElements().get(0)); - assertEquals(0, st2.getStereotypedElements().size()); - assertEquals(0, mcl1.getStereotypes().size()); - } - - @Test - public void testStereotypeAddStereotypeTwice() throws CException { - CModel model = CodeableModels.createModel(); - - CMetaclass mcl1 = model.createMetaclass("MCL1"); - model.createStereotype("Stereotype1"); - mcl1.addStereotype("Stereotype1"); - - try { - mcl1.addStereotype("Stereotype1"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("stereotype 'Stereotype1' already exists on 'MCL1'", e.getMessage()); - } - } - - @Test - public void testStereotypeRemoveNonExisting() throws CException { - CModel model = CodeableModels.createModel(); - - CMetaclass mcl1 = model.createMetaclass("MCL1"); - model.createStereotype("Stereotype1"); - - try { - mcl1.removeStereotype("Stereotype1"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("can't remove stereotype 'Stereotype1' on 'MCL1': does not exist", e.getMessage()); - } - } - - - @Test - public void testStereotypeExtensionExceptions() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - model.createClass(mcl1, "CL1"); - model.createStereotype("S1", mcl1); - - try { - model.createStereotype("S1", "CL1"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("'CL1' is not a metaclass", e.getMessage()); - } - - try { - model.createStereotype("Stereotype1", "S1"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("'S1' is not a metaclass", e.getMessage()); - } - try { - model.createStereotype("Stereotype2", "CL1"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("'CL1' is not a metaclass", e.getMessage()); - } - } - - @Test - public void testAddRemoveStereotypes() throws CException { - CModel model = CodeableModels.createModel(); - CStereotype st1 = model.createStereotype("Stereotype1"); - CStereotype st2 = model.createStereotype("Stereotype2"); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - assertEquals(Collections.emptyList(), mcl1.getStereotypes()); - mcl1.addStereotype(st1); - assertEquals(Collections.singletonList(st1), mcl1.getStereotypes()); - mcl1.addStereotype(st2); - assertEquals(Arrays.asList(st1, st2), mcl1.getStereotypes()); - mcl1.removeStereotype(st1); - assertEquals(Collections.singletonList(st2), mcl1.getStereotypes()); - mcl1.removeStereotype(st2); - assertEquals(Collections.emptyList(), mcl1.getStereotypes()); - } - - @Test - public void testStereotypeExtensionsOfMetaclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - List result = mcl1.getStereotypes(); - assertEquals(0, result.size()); - - CStereotype st1 = model.createStereotype("Stereotype1", mcl1); - result = mcl1.getStereotypes(); - assertEquals(1, result.size()); - assertEquals(st1, result.get(0)); - - CStereotype st2 = model.createStereotype("Stereotype2", mcl1); - CStereotype st3 = model.createStereotype("Stereotype3", mcl1); - result = mcl1.getStereotypes(); - assertEquals(Arrays.asList(st1, st2, st3), result); - - CStereotype st4 = model.createStereotype("Stereotype4", mcl1); - model.deleteClassifier(st2); - result = mcl1.getStereotypes(); - assertEquals(Arrays.asList(st1, st3, st4), result); - assertEquals(mcl1, st1.getStereotypedElements().get(0)); - assertEquals(0, st2.getStereotypedElements().size()); - - model.deleteClassifier(mcl1); - result = mcl1.getStereotypes(); - assertEquals(Collections.emptyList(), result); - assertEquals(0, st1.getStereotypedElements().size()); - } - - @Test - public void testLookupStereotypeLocally() throws CException { - CModel model = CodeableModels.createModel(); - CStereotype st = model.createStereotype("Stereotype1"); - CClassifier st2 = model.lookupClassifier("Stereotype1"); - assertEquals(st, st2); - } - - @Test - public void testLookup3StereotypesLocally() throws CException { - CModel model = CodeableModels.createModel(); - CStereotype st1 = model.createStereotype("Stereotype1"); - CStereotype st2 = model.createStereotype("Stereotype2"); - CStereotype st3 = model.createStereotype("Stereotype3"); - CClassifier gst1 = model.lookupClassifier("Stereotype1"); - CClassifier gst2 = model.lookupClassifier("Stereotype2"); - CStereotype gst3 = model.lookupStereotype("Stereotype3"); - assertEquals(st1, gst1); - assertEquals(st2, gst2); - assertEquals(st3, gst3); - } - - @Test - public void testGetStereotypesAndGetStereotypeNames() throws CException { - CModel model = CodeableModels.createModel(); - List stereotypes = model.getStereotypes(); - List stereotypeNames = model.getStereotypeNames(); - assertEquals(0, stereotypes.size()); - assertEquals(0, stereotypeNames.size()); - - CStereotype cl1 = model.createStereotype("S1"); - - stereotypes = model.getStereotypes(); - stereotypeNames = model.getStereotypeNames(); - assertEquals(1, stereotypes.size()); - assertEquals(1, stereotypeNames.size()); - assertEquals("S1", stereotypeNames.get(0)); - assertEquals(cl1, stereotypes.get(0)); - - CStereotype cl2 = model.createStereotype("S2"); - CStereotype cl3 = model.createStereotype("S3"); - model.createMetaclass("MCL"); - model.createClass("MCL", "Stereotype2"); - model.createClass("MCL", "Stereotype3"); - - stereotypes = model.getStereotypes(); - stereotypeNames = model.getStereotypeNames(); - assertEquals(3, stereotypes.size()); - assertEquals(3, stereotypeNames.size()); - assertTrue(stereotypes.contains(cl1)); - assertTrue(stereotypes.contains(cl2)); - assertTrue(stereotypes.contains(cl3)); - } - - - @Test - public void testStereotypeSuperclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("A"); - CClassifier cl2 = model.createStereotype("AType", mcl1); - CClassifier cl3 = model.createStereotype("B"); - CClassifier cl4 = model.createClass("A", "X"); - try { - cl3.addSuperclass("A"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals( - "cannot add superclass 'A' to 'B': not a stereotype", - e.getMessage()); - } - try { - cl3.addSuperclass(cl4); - fail("exception not thrown"); - } catch (CException e) { - assertEquals( - "cannot add superclass 'X' to 'B': not a stereotype", - e.getMessage()); - } - cl3.addSuperclass(cl2); - List scs = cl3.getSuperclasses(); - assertEquals(1, scs.size()); - assertTrue(scs.contains(cl2)); - } - - @Test - public void testChangingExtendedMetaclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CStereotype stereotype1 = model.createStereotype("ST1", mcl2); - assertEquals(mcl2, stereotype1.getStereotypedElements().get(0)); - assertEquals(Collections.emptyList(), mcl1.getStereotypes()); - assertEquals(Collections.singletonList(stereotype1), mcl2.getStereotypes()); - mcl1.addStereotype(stereotype1); - mcl2.removeStereotype(stereotype1); - assertEquals(mcl1, stereotype1.getStereotypedElements().get(0)); - assertEquals(Collections.singletonList(stereotype1), mcl1.getStereotypes()); - assertEquals(Collections.emptyList(), mcl2.getStereotypes()); - - mcl1.removeStereotype(stereotype1); - assertEquals(0, stereotype1.getStereotypedElements().size()); - assertEquals(Collections.emptyList(), mcl1.getStereotypes()); - assertEquals(Collections.emptyList(), mcl2.getStereotypes()); - - mcl1.addStereotype(stereotype1); - assertEquals(mcl1, stereotype1.getStereotypedElements().get(0)); - assertEquals(Collections.singletonList(stereotype1), mcl1.getStereotypes()); - assertEquals(Collections.emptyList(), mcl2.getStereotypes()); - } -} diff --git a/src/codeableModels/tests/TestsTaggedValuesOnClasses.java b/src/codeableModels/tests/TestsTaggedValuesOnClasses.java deleted file mode 100644 index 8e50ea0..0000000 --- a/src/codeableModels/tests/TestsTaggedValuesOnClasses.java +++ /dev/null @@ -1,738 +0,0 @@ -package codeableModels.tests; - -import codeableModels.*; -import org.junit.*; - -import java.util.*; - -import static org.junit.Assert.*; - -public class TestsTaggedValuesOnClasses { - - @Test - public void testTaggedValuesOnPrimitiveTypeAttributes() throws CException { - CModel model = CodeableModels.createModel(); - CStereotype stereotype = model.createStereotype("ST"); - CMetaclass mcl = model.createMetaclass("MCL"); - mcl.addStereotype(stereotype); - CClass cl = model.createClass(mcl, "C"); - cl.addStereotypeInstance(stereotype); - - stereotype.addAttribute("isBoolean", true); - stereotype.addAttribute("intVal", 1); - stereotype.addAttribute("longVal", (long) 100); - stereotype.addAttribute("doubleVal", 1.1); - stereotype.addAttribute("floatVal", (float) 1.1); - stereotype.addAttribute("string", "abc"); - stereotype.addAttribute("char", 'a'); - stereotype.addAttribute("byte", (byte) 1); - stereotype.addAttribute("short", (short) 2); - - assertEquals(true, cl.getTaggedValue("isBoolean")); - assertEquals(1, cl.getTaggedValue("intVal")); - assertEquals((long) 100, cl.getTaggedValue("longVal")); - assertEquals(1.1, cl.getTaggedValue("doubleVal")); - assertEquals((float) 1.1, cl.getTaggedValue("floatVal")); - assertEquals("abc", cl.getTaggedValue("string")); - assertEquals('a', cl.getTaggedValue("char")); - assertEquals((byte) 1, cl.getTaggedValue("byte")); - assertEquals((short) 2, cl.getTaggedValue("short")); - - cl.setTaggedValue("isBoolean", false); - cl.setTaggedValue("intVal", 10); - cl.setTaggedValue("longVal", (long) 1000); - cl.setTaggedValue("doubleVal", 100.1); - cl.setTaggedValue("floatVal", (float) 102.1); - cl.setTaggedValue("string", ""); - cl.setTaggedValue("char", 'x'); - cl.setTaggedValue("byte", (byte) 15); - cl.setTaggedValue("short", (short) 12); - - assertEquals(false, cl.getTaggedValue("isBoolean")); - assertEquals(10, cl.getTaggedValue("intVal")); - assertEquals((long) 1000, cl.getTaggedValue("longVal")); - assertEquals(100.1, cl.getTaggedValue("doubleVal")); - assertEquals((float) 102.1, cl.getTaggedValue("floatVal")); - assertEquals("", cl.getTaggedValue("string")); - assertEquals('x', cl.getTaggedValue("char")); - assertEquals((byte) 15, cl.getTaggedValue("byte")); - assertEquals((short) 12, cl.getTaggedValue("short")); - } - - @Test - public void testTaggedValuesOnPrimitiveTypeAttributesCreateClassAfterAttributeSetting() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CStereotype stereotype = model.createStereotype("ST"); - mcl.addStereotype(stereotype); - - stereotype.addAttribute("isBoolean", true); - stereotype.addAttribute("intVal", 1); - stereotype.addAttribute("longVal", (long) 100); - stereotype.addAttribute("doubleVal", 1.1); - stereotype.addAttribute("floatVal", (float) 1.1); - stereotype.addAttribute("string", "abc"); - stereotype.addAttribute("char", 'a'); - stereotype.addAttribute("byte", (byte) 1); - stereotype.addAttribute("short", (short) 2); - - CClass cl = model.createClass(mcl, "C"); - cl.addStereotypeInstance(stereotype); - - assertEquals(true, cl.getTaggedValue("isBoolean")); - assertEquals(1, cl.getTaggedValue("intVal")); - assertEquals((long) 100, cl.getTaggedValue("longVal")); - assertEquals(1.1, cl.getTaggedValue("doubleVal")); - assertEquals((float) 1.1, cl.getTaggedValue("floatVal")); - assertEquals("abc", cl.getTaggedValue("string")); - assertEquals('a', cl.getTaggedValue("char")); - assertEquals((byte) 1, cl.getTaggedValue("byte")); - assertEquals((short) 2, cl.getTaggedValue("short")); - } - - @Test - public void testAttributeOfTaggedValueUnknown() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CStereotype stereotype = model.createStereotype("ST"); - mcl.addStereotype(stereotype); - CClass cl = model.createClass(mcl, "C"); - cl.addStereotypeInstance(stereotype); - - try { - cl.getTaggedValue("x"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("tagged value 'x' unknown", e.getMessage()); - } - - cl.addAttribute("isBoolean", true); - cl.addAttribute("intVal", 1); - - try { - cl.setTaggedValue("x", 1); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("tagged value 'x' unknown", e.getMessage()); - } - } - - @Test - public void testObjectTypeTaggedValues() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CStereotype stereotype = model.createStereotype("ST"); - mcl.addStereotype(stereotype); - CClass cl = model.createClass(mcl, "C"); - cl.addStereotypeInstance(stereotype); - - CClass attrType = model.createClass(mcl, "AttrType"); - CObject attrValue = model.createObject(attrType, "attrValue"); - - stereotype.addAttribute("attrTypeObj", attrValue); - stereotype.getAttribute("attrTypeObj"); - - assertEquals(attrValue, cl.getTaggedValue("attrTypeObj")); - assertEquals("attrValue", ((CObject) cl.getTaggedValue("attrTypeObj")).getName()); - - CObject attrValue2 = model.createObject(attrType, "attrValue2"); - cl.setTaggedValue("attrTypeObj", attrValue2); - - assertEquals(attrValue2, cl.getTaggedValue("attrTypeObj")); - assertEquals("attrValue2", ((CObject) cl.getTaggedValue("attrTypeObj")).getName()); - - CObject nonAttrValue = model.createObject(cl, "nonAttrValue"); - - try { - cl.setTaggedValue("attrTypeObj", nonAttrValue); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("type classifier of object 'nonAttrValue' is not matching attribute type of attribute " + - "'attrTypeObj'", e.getMessage()); - } - } - - @Test - public void testAddObjectAttributeGetSetTaggedValue() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CStereotype stereotype = model.createStereotype("ST"); - mcl.addStereotype(stereotype); - CClass cl = model.createClass(mcl, "C"); - cl.addStereotypeInstance(stereotype); - - CClass attrType = model.createClass(mcl, "AttrType"); - CObject attrValue = model.createObject(attrType, "attrValue"); - - stereotype.addObjectAttribute("attrTypeObj", attrType).addObjectAttribute("attrTypeObj2", attrType); - assertEquals(null, cl.getTaggedValue("attrTypeObj")); - cl.setTaggedValue("attrTypeObj", attrValue); - assertEquals(attrValue, cl.getTaggedValue("attrTypeObj")); - } - - - @Test - public void testTaggedValuesOnAttributesWithNoDefaultValues() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CStereotype stereotype = model.createStereotype("ST"); - mcl.addStereotype(stereotype); - - stereotype.addBooleanAttribute("isBoolean"); - stereotype.addIntAttribute("intVal"); - stereotype.addLongAttribute("longVal"); - stereotype.addDoubleAttribute("doubleVal"); - stereotype.addFloatAttribute("floatVal"); - stereotype.addStringAttribute("string"); - stereotype.addCharAttribute("char"); - stereotype.addByteAttribute("byte"); - stereotype.addShortAttribute("short"); - CClass attrClass = model.createClass(mcl, "attrClass"); - stereotype.addObjectAttribute("obj1", attrClass); - stereotype.addObjectAttribute("obj2", "attrClass"); - CEnum enumType = model.createEnum("XYEnum", Arrays.asList("X", "Y")); - stereotype.addEnumAttribute("enum", enumType); - - CClass cl = model.createClass(mcl, "C"); - cl.addStereotypeInstance(stereotype); - - assertEquals(null, cl.getTaggedValue("isBoolean")); - assertEquals(null, cl.getTaggedValue("intVal")); - assertEquals(null, cl.getTaggedValue("longVal")); - assertEquals(null, cl.getTaggedValue("doubleVal")); - assertEquals(null, cl.getTaggedValue("floatVal")); - assertEquals(null, cl.getTaggedValue("string")); - assertEquals(null, cl.getTaggedValue("char")); - assertEquals(null, cl.getTaggedValue("byte")); - assertEquals(null, cl.getTaggedValue("short")); - assertEquals(null, cl.getTaggedValue("obj1")); - assertEquals(null, cl.getTaggedValue("obj2")); - assertEquals(null, cl.getTaggedValue("enum")); - } - - - @Test - public void testEnumTypeTaggedValues() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CStereotype stereotype = model.createStereotype("ST"); - mcl.addStereotype(stereotype); - CClass cl = model.createClass(mcl, "C"); - cl.addStereotypeInstance(stereotype); - - List enumValues = Arrays.asList("A", "B", "C"); - CEnum enumObj = model.createEnum("ABCEnum", enumValues); - - - assertEquals(0, stereotype.getAttributes().size()); - stereotype.addEnumAttribute("l1", enumObj).setAttributeDefaultValue("l1", "A"); - stereotype.addEnumAttribute("l2", enumObj); - - assertEquals("A", cl.getTaggedValue("l1")); - assertEquals(null, cl.getTaggedValue("l2")); - - cl.setTaggedValue("l1", "B"); - cl.setTaggedValue("l2", "C"); - - assertEquals("B", cl.getTaggedValue("l1")); - assertEquals("C", cl.getTaggedValue("l2")); - - try { - cl.setTaggedValue("l1", "X"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value 'X' not element of enumeration", e.getMessage()); - } - } - - @Test - public void testAttributeTypeCheckForTaggedValues() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CStereotype stereotype = model.createStereotype("ST"); - mcl.addStereotype(stereotype); - stereotype.addBooleanAttribute("a"); - CClass cl = model.createClass(mcl, "C"); - cl.addStereotypeInstance(stereotype); - - - try { - cl.setTaggedValue("a", CodeableModels.createModel()); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value for attribute 'a' is not a known attribute type", e.getMessage()); - } - try { - cl.setTaggedValue("a", 1); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'a' does not match attribute type", e.getMessage()); - } - - stereotype.addIntAttribute("i"); - try { - cl.setTaggedValue("i", Boolean.TRUE); - } catch (CException e) { - assertEquals("value type for attribute 'i' does not match attribute type", e.getMessage()); - } - - stereotype.addShortAttribute("s"); - try { - cl.setTaggedValue("s", Boolean.TRUE); - } catch (CException e) { - assertEquals("value type for attribute 's' does not match attribute type", e.getMessage()); - } - - stereotype.addByteAttribute("b"); - try { - cl.setTaggedValue("b", Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'b' does not match attribute type", e.getMessage()); - } - - stereotype.addLongAttribute("l"); - try { - cl.setTaggedValue("l", Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'l' does not match attribute type", e.getMessage()); - } - - stereotype.addFloatAttribute("f"); - try { - cl.setTaggedValue("f", Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'f' does not match attribute type", e.getMessage()); - } - - stereotype.addDoubleAttribute("d"); - try { - cl.setTaggedValue("d", Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'd' does not match attribute type", e.getMessage()); - } - - CEnum enumType = model.createEnum("XYEnum", Arrays.asList("X", "Y")); - stereotype.addEnumAttribute("e", enumType); - try { - cl.setTaggedValue("e", Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'e' does not match attribute type", e.getMessage()); - } - - CClass attrClass = model.createClass(mcl, "AttrClass"); - CClass otherClass = model.createClass(mcl, "OtherClass"); - stereotype.addObjectAttribute("o", attrClass); - try { - cl.setTaggedValue("o", Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'o' does not match attribute type", e.getMessage()); - } - - try { - cl.setTaggedValue("o", otherClass); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("type classifier of object 'OtherClass' is not matching attribute type of attribute 'o'", e - .getMessage()); - } - - } - - @Test - public void testDeleteAttributesSetTaggedValue() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CStereotype stereotype = model.createStereotype("ST"); - mcl.addStereotype(stereotype); - CClass cl = model.createClass(mcl, "C"); - cl.addStereotypeInstance(stereotype); - - stereotype.addAttribute("isBoolean", true); - stereotype.addAttribute("intVal", 1); - - cl.setTaggedValue("isBoolean", true); - - stereotype.deleteAttribute("isBoolean"); - - cl.setTaggedValue("intVal", 1); - - try { - cl.setTaggedValue("isBoolean", false); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("tagged value 'isBoolean' unknown", e.getMessage()); - } - } - - @Test - public void testTaggedValuesInheritance() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CStereotype top1 = model.createStereotype( "Top1"); - mcl.addStereotype(top1); - CStereotype top2 = model.createStereotype( "Top2"); - CStereotype stereotypeA = model.createStereotype( "STA").addSuperclass("Top1").addSuperclass("Top2"); - CStereotype stereotypeSubA = model.createStereotype("SubA").addSuperclass("STA"); - - top1.addAttribute("int0", 0); - top2.addAttribute("int1", 1); - stereotypeA.addAttribute("int2", 2); - stereotypeSubA.addAttribute("int3", 3); - - CClass cl = model.createClass(mcl, "C"); - cl.addStereotypeInstance(stereotypeSubA); - - assertEquals(0, cl.getTaggedValue("int0")); - assertEquals(1, cl.getTaggedValue("int1")); - assertEquals(2, cl.getTaggedValue("int2")); - assertEquals(3, cl.getTaggedValue("int3")); - - assertEquals(0, cl.getTaggedValue(top1,"int0")); - assertEquals(1, cl.getTaggedValue(top2, "int1")); - assertEquals(2, cl.getTaggedValue(stereotypeA, "int2")); - assertEquals(3, cl.getTaggedValue(stereotypeSubA, "int3")); - - cl.setTaggedValue("int0", 10); - cl.setTaggedValue("int1", 11); - cl.setTaggedValue("int2", 12); - cl.setTaggedValue("int3", 13); - - assertEquals(10, cl.getTaggedValue("int0")); - assertEquals(11, cl.getTaggedValue("int1")); - assertEquals(12, cl.getTaggedValue("int2")); - assertEquals(13, cl.getTaggedValue("int3")); - - assertEquals(10, cl.getTaggedValue(top1,"int0")); - assertEquals(11, cl.getTaggedValue(top2, "int1")); - assertEquals(12, cl.getTaggedValue(stereotypeA, "int2")); - assertEquals(13, cl.getTaggedValue(stereotypeSubA, "int3")); - - cl.setTaggedValue(top1,"int0", 100); - cl.setTaggedValue(top2,"int1", 110); - cl.setTaggedValue(stereotypeA,"int2", 120); - cl.setTaggedValue(stereotypeSubA,"int3", 130); - - assertEquals(100, cl.getTaggedValue("int0")); - assertEquals(110, cl.getTaggedValue("int1")); - assertEquals(120, cl.getTaggedValue("int2")); - assertEquals(130, cl.getTaggedValue("int3")); - - assertEquals(100, cl.getTaggedValue(top1,"int0")); - assertEquals(110, cl.getTaggedValue(top2, "int1")); - assertEquals(120, cl.getTaggedValue(stereotypeA, "int2")); - assertEquals(130, cl.getTaggedValue(stereotypeSubA, "int3")); - } - - - @Test - public void testTaggedValuesInheritanceAfterDeleteSuperclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CStereotype top1 = model.createStereotype( "Top1"); - mcl.addStereotype(top1); - CStereotype top2 = model.createStereotype( "Top2"); - CStereotype stereotypeA = model.createStereotype( "STA").addSuperclass("Top1").addSuperclass("Top2"); - CStereotype stereotypeSubA = model.createStereotype("SubA").addSuperclass("STA"); - - top1.addAttribute("int0", 0); - top2.addAttribute("int1", 1); - stereotypeA.addAttribute("int2", 2); - stereotypeSubA.addAttribute("int3", 3); - - CClass cl = model.createClass(mcl, "C"); - cl.addStereotypeInstance(stereotypeSubA); - - ((CClassifier) stereotypeA).deleteSuperclass(top2); - - assertEquals(0, cl.getTaggedValue("int0")); - try { - cl.getTaggedValue("int1"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("tagged value 'int1' unknown", e.getMessage()); - } - assertEquals(2, cl.getTaggedValue("int2")); - assertEquals(3, cl.getTaggedValue("int3")); - - cl.setTaggedValue("int0", 10); - try { - cl.setTaggedValue("int1", 11); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("tagged value 'int1' unknown", e.getMessage()); - } - cl.setTaggedValue("int2", 12); - cl.setTaggedValue("int3", 13); - } - - @Test - public void testTaggedValuesInheritanceMultipleStereotypes() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CStereotype top1 = model.createStereotype( "Top1"); - mcl.addStereotype(top1); - CStereotype top2 = model.createStereotype( "Top2"); - CStereotype stereotypeA = model.createStereotype( "STA").addSuperclass("Top1").addSuperclass("Top2"); - CStereotype stereotypeSubA = model.createStereotype("SubA").addSuperclass("STA"); - CStereotype stereotypeB = model.createStereotype( "STB").addSuperclass("Top1").addSuperclass("Top2"); - CStereotype stereotypeSubB = model.createStereotype("SubB").addSuperclass("STB"); - CStereotype stereotypeC = model.createStereotype( "STC"); - mcl.addStereotype(stereotypeC); - CStereotype stereotypeSubC = model.createStereotype("SubC").addSuperclass("STC"); - CClass cl = model.createClass(mcl, "C"); - cl.addStereotypeInstance(stereotypeSubA); - cl.addStereotypeInstance(stereotypeSubB); - cl.addStereotypeInstance(stereotypeSubC); - - top1.addAttribute("int0", 0); - top2.addAttribute("int1", 1); - stereotypeA.addAttribute("int2", 2); - stereotypeSubA.addAttribute("int3", 3); - stereotypeB.addAttribute("int4", 4); - stereotypeSubB.addAttribute("int5", 5); - stereotypeC.addAttribute("int6", 6); - stereotypeSubC.addAttribute("int7", 7); - - assertEquals(0, cl.getTaggedValue("int0")); - assertEquals(1, cl.getTaggedValue("int1")); - assertEquals(2, cl.getTaggedValue("int2")); - assertEquals(3, cl.getTaggedValue("int3")); - assertEquals(4, cl.getTaggedValue("int4")); - assertEquals(5, cl.getTaggedValue("int5")); - assertEquals(6, cl.getTaggedValue("int6")); - assertEquals(7, cl.getTaggedValue("int7")); - - assertEquals(0, cl.getTaggedValue(top1,"int0")); - assertEquals(1, cl.getTaggedValue(top2, "int1")); - assertEquals(2, cl.getTaggedValue(stereotypeA, "int2")); - assertEquals(3, cl.getTaggedValue(stereotypeSubA, "int3")); - assertEquals(4, cl.getTaggedValue(stereotypeB, "int4")); - assertEquals(5, cl.getTaggedValue(stereotypeSubB, "int5")); - assertEquals(6, cl.getTaggedValue(stereotypeC, "int6")); - assertEquals(7, cl.getTaggedValue(stereotypeSubC, "int7")); - - cl.setTaggedValue("int0", 10); - cl.setTaggedValue("int1", 11); - cl.setTaggedValue("int2", 12); - cl.setTaggedValue("int3", 13); - cl.setTaggedValue("int4", 14); - cl.setTaggedValue("int5", 15); - cl.setTaggedValue("int6", 16); - cl.setTaggedValue("int7", 17); - - assertEquals(10, cl.getTaggedValue("int0")); - assertEquals(11, cl.getTaggedValue("int1")); - assertEquals(12, cl.getTaggedValue("int2")); - assertEquals(13, cl.getTaggedValue("int3")); - assertEquals(14, cl.getTaggedValue("int4")); - assertEquals(15, cl.getTaggedValue("int5")); - assertEquals(16, cl.getTaggedValue("int6")); - assertEquals(17, cl.getTaggedValue("int7")); - } - - @Test - public void testTaggedValuesSameNameInheritance() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CStereotype top1 = model.createStereotype( "Top1"); - mcl.addStereotype(top1); - CStereotype top2 = model.createStereotype( "Top2"); - CStereotype stereotypeA = model.createStereotype( "STA").addSuperclass("Top1").addSuperclass("Top2"); - CStereotype stereotypeSubA = model.createStereotype("SubA").addSuperclass("STA"); - - top1.addAttribute("int", 0); - top2.addAttribute("int", 1); - stereotypeA.addAttribute("int", 2); - stereotypeSubA.addAttribute("int", 3); - - CClass cl1 = model.createClass(mcl, "C1"); - cl1.addStereotypeInstance(stereotypeSubA); - CClass cl2 = model.createClass(mcl, "C2"); - cl2.addStereotypeInstance(stereotypeA); - CClass cl3 = model.createClass(mcl, "C3"); - cl3.addStereotypeInstance(top1); - - assertEquals(3, cl1.getTaggedValue("int")); - assertEquals(2, cl2.getTaggedValue("int")); - assertEquals(0, cl3.getTaggedValue("int")); - - assertEquals(3, cl1.getTaggedValue(stereotypeSubA, "int")); - assertEquals(2, cl1.getTaggedValue(stereotypeA, "int")); - assertEquals(1, cl1.getTaggedValue(top2, "int")); - assertEquals(0, cl1.getTaggedValue(top1, "int")); - assertEquals(2, cl2.getTaggedValue(stereotypeA, "int")); - assertEquals(1, cl2.getTaggedValue(top2, "int")); - assertEquals(0, cl2.getTaggedValue(top1, "int")); - assertEquals(0, cl3.getTaggedValue(top1,"int")); - - cl1.setTaggedValue("int", 10); - cl2.setTaggedValue("int", 11); - cl3.setTaggedValue("int", 12); - - assertEquals(10, cl1.getTaggedValue("int")); - assertEquals(11, cl2.getTaggedValue("int")); - assertEquals(12, cl3.getTaggedValue("int")); - - assertEquals(10, cl1.getTaggedValue(stereotypeSubA, "int")); - assertEquals(2, cl1.getTaggedValue(stereotypeA, "int")); - assertEquals(1, cl1.getTaggedValue(top2, "int")); - assertEquals(0, cl1.getTaggedValue(top1, "int")); - assertEquals(11, cl2.getTaggedValue(stereotypeA, "int")); - assertEquals(1, cl2.getTaggedValue(top2, "int")); - assertEquals(0, cl2.getTaggedValue(top1, "int")); - assertEquals(12, cl3.getTaggedValue(top1,"int")); - - cl1.setTaggedValue(stereotypeSubA,"int", 130); - cl1.setTaggedValue(top1,"int", 100); - cl1.setTaggedValue(top2,"int", 110); - cl1.setTaggedValue(stereotypeA,"int", 120); - - assertEquals(130, cl1.getTaggedValue("int")); - - assertEquals(100, cl1.getTaggedValue(top1,"int")); - assertEquals(110, cl1.getTaggedValue(top2, "int")); - assertEquals(120, cl1.getTaggedValue(stereotypeA, "int")); - assertEquals(130, cl1.getTaggedValue(stereotypeSubA, "int")); - } - - @Test - public void testSetAndDeleteTaggedValues() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CStereotype stereo = model.createStereotype("Stereo"); - mcl.addStereotype(stereo); - CClass cl = model.createClass(mcl, "C"); - cl.addStereotypeInstance("Stereo"); - stereo.addAttribute("isBoolean", true); - stereo.addAttribute("intVal", 1); - - cl.setTaggedValue("isBoolean", true); - cl.setTaggedValue("intVal", 1); - - assertEquals(true, cl.getTaggedValue("isBoolean")); - assertEquals(1, cl.getTaggedValue("intVal")); - - stereo.deleteAttribute("isBoolean"); - - assertEquals(1, cl.getTaggedValue("intVal")); - cl.setTaggedValue("intVal", 100); - assertEquals(100, cl.getTaggedValue("intVal")); - - try { - cl.setAttributeValue("intVal", 3); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("attribute 'intVal' unknown for object 'C'", e.getMessage()); - } - - try { - cl.setTaggedValue("isBoolean", false); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("tagged value 'isBoolean' unknown", e.getMessage()); - } - } - - @Test - public void testSetAndGetTaggedValuesWithStereotypeSpecified() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CStereotype stereo = model.createStereotype("Stereo"); - mcl.addStereotype(stereo); - CClass cl = model.createClass(mcl, "C"); - cl.addStereotypeInstance("Stereo"); - stereo.addAttribute("isBoolean", true); - stereo.addAttribute("intVal", 1); - - cl.setTaggedValue(stereo,"isBoolean", true); - cl.setTaggedValue(stereo,"intVal", 1); - - assertEquals(true, cl.getTaggedValue(stereo,"isBoolean")); - assertEquals(1, cl.getTaggedValue(stereo,"intVal")); - } - - @Test - public void testWrongStereotypeInTaggedValue() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CStereotype stereo = model.createStereotype("Stereo"); - CStereotype stereo2 = model.createStereotype("Stereo2"); - mcl.addStereotype(stereo); - mcl.addStereotype(stereo2); - CClass cl = model.createClass(mcl, "C"); - cl.addStereotypeInstance("Stereo"); - - stereo.addAttribute("isBoolean", true); - stereo2.addAttribute("isBoolean", true); - - cl.setTaggedValue(stereo, "isBoolean", false); - - try { - cl.setTaggedValue(stereo2, "isBoolean", false); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("stereotype 'Stereo2' is not a stereotype of element", e.getMessage()); - } - - assertEquals(false, cl.getTaggedValue(stereo, "isBoolean")); - - try { - cl.getTaggedValue(stereo2, "isBoolean"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("stereotype 'Stereo2' is not a stereotype of element", e.getMessage()); - } - } - - @Test - public void testUnknownTaggedValueOnStereotype() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CStereotype stereo = model.createStereotype("Stereo"); - mcl.addStereotype(stereo); - CClass cl = model.createClass(mcl, "C"); - cl.addStereotypeInstance("Stereo"); - try { - cl.setTaggedValue(stereo, "isBoolean", false); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("tagged value 'isBoolean' unknown for stereotype 'Stereo'", e.getMessage()); - } - - try { - cl.getTaggedValue(stereo, "isBoolean"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("tagged value 'isBoolean' unknown for stereotype 'Stereo'", e.getMessage()); - } - } - - @Test - public void testStereotypeLookup() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl = model.createMetaclass("MCL"); - CStereotype stereo = model.createStereotype("Stereo"); - CStereotype stereo2 = model.createStereotype("Stereo2"); - mcl.addStereotype(stereo); - mcl.addStereotype(stereo2); - CClass cl = model.createClass(mcl, "C"); - cl.addStereotypeInstance("Stereo"); - cl.addStereotypeInstance("Stereo2"); - - stereo.addAttribute("isBoolean", true); - stereo2.addAttribute("isBoolean", true); - - cl.setTaggedValue("isBoolean", false); - - assertEquals(false, cl.getTaggedValue("isBoolean")); - assertEquals(false, cl.getTaggedValue(stereo,"isBoolean")); - assertEquals(true, cl.getTaggedValue(stereo2,"isBoolean")); - } - -} diff --git a/src/codeableModels/tests/TestsTaggedValuesOnLinks.java b/src/codeableModels/tests/TestsTaggedValuesOnLinks.java deleted file mode 100644 index 836ca9f..0000000 --- a/src/codeableModels/tests/TestsTaggedValuesOnLinks.java +++ /dev/null @@ -1,766 +0,0 @@ -package codeableModels.tests; - -import codeableModels.*; -import org.junit.*; - -import java.util.*; - -import static org.junit.Assert.*; - -public class TestsTaggedValuesOnLinks { - - private CModel createModel() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociationEnd end1 = mcl1.createEnd("metaclass1", "1"), - end2 = mcl2.createEnd("metaclass2", "1"); - CAssociation association = model.createAssociation(end1, end2); - CStereotype stereotype = model.createStereotype("ST", association); - CClass cl1 = model.createClass(mcl1, "CL1"); - CClass cl2 = model.createClass(mcl2, "CL2"); - cl1.addLink(end2, cl2).addStereotypeInstance(stereotype); - return model; - } - - @Test - public void testTaggedValuesOnPrimitiveTypeAttributes() throws CException { - CModel model = createModel(); - CStereotype stereotype = model.getStereotype("ST"); - CClass cl1 = model.getClass("CL1"); - CLink link = cl1.getLinkObjects(cl1.getClassifier().getAssociationByRoleName("metaclass2")).get(0); - - stereotype.addAttribute("isBoolean", true); - stereotype.addAttribute("intVal", 1); - stereotype.addAttribute("longVal", (long) 100); - stereotype.addAttribute("doubleVal", 1.1); - stereotype.addAttribute("floatVal", (float) 1.1); - stereotype.addAttribute("string", "abc"); - stereotype.addAttribute("char", 'a'); - stereotype.addAttribute("byte", (byte) 1); - stereotype.addAttribute("short", (short) 2); - - assertEquals(true, link.getTaggedValue("isBoolean")); - assertEquals(1, link.getTaggedValue("intVal")); - assertEquals((long) 100, link.getTaggedValue("longVal")); - assertEquals(1.1, link.getTaggedValue("doubleVal")); - assertEquals((float) 1.1, link.getTaggedValue("floatVal")); - assertEquals("abc", link.getTaggedValue("string")); - assertEquals('a', link.getTaggedValue("char")); - assertEquals((byte) 1, link.getTaggedValue("byte")); - assertEquals((short) 2, link.getTaggedValue("short")); - - link.setTaggedValue("isBoolean", false); - link.setTaggedValue("intVal", 10); - link.setTaggedValue("longVal", (long) 1000); - link.setTaggedValue("doubleVal", 100.1); - link.setTaggedValue("floatVal", (float) 102.1); - link.setTaggedValue("string", ""); - link.setTaggedValue("char", 'x'); - link.setTaggedValue("byte", (byte) 15); - link.setTaggedValue("short", (short) 12); - - assertEquals(false, link.getTaggedValue("isBoolean")); - assertEquals(10, link.getTaggedValue("intVal")); - assertEquals((long) 1000, link.getTaggedValue("longVal")); - assertEquals(100.1, link.getTaggedValue("doubleVal")); - assertEquals((float) 102.1, link.getTaggedValue("floatVal")); - assertEquals("", link.getTaggedValue("string")); - assertEquals('x', link.getTaggedValue("char")); - assertEquals((byte) 15, link.getTaggedValue("byte")); - assertEquals((short) 12, link.getTaggedValue("short")); - } - - @Test - public void testTaggedValuesOnPrimitiveTypeAttributesCreateClassAfterAttributeSetting() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociationEnd end1 = mcl1.createEnd("metaclass1", "1"), - end2 = mcl2.createEnd("metaclass2", "1"); - CAssociation association = model.createAssociation(end1, end2); - CStereotype stereotype = model.createStereotype("ST", association); - - stereotype.addAttribute("isBoolean", true); - stereotype.addAttribute("intVal", 1); - stereotype.addAttribute("longVal", (long) 100); - stereotype.addAttribute("doubleVal", 1.1); - stereotype.addAttribute("floatVal", (float) 1.1); - stereotype.addAttribute("string", "abc"); - stereotype.addAttribute("char", 'a'); - stereotype.addAttribute("byte", (byte) 1); - stereotype.addAttribute("short", (short) 2); - - CClass cl1 = model.createClass(mcl1, "CL1"); - CClass cl2 = model.createClass(mcl2, "CL2"); - cl1.addLink(end2, cl2); - CLink link = cl1.getLinkObjects(end2).get(0); - link.addStereotypeInstance(stereotype); - - assertEquals(true, link.getTaggedValue("isBoolean")); - assertEquals(1, link.getTaggedValue("intVal")); - assertEquals((long) 100, link.getTaggedValue("longVal")); - assertEquals(1.1, link.getTaggedValue("doubleVal")); - assertEquals((float) 1.1, link.getTaggedValue("floatVal")); - assertEquals("abc", link.getTaggedValue("string")); - assertEquals('a', link.getTaggedValue("char")); - assertEquals((byte) 1, link.getTaggedValue("byte")); - assertEquals((short) 2, link.getTaggedValue("short")); - } - - @Test - public void testAttributeOfTaggedValueUnknown() throws CException { - CModel model = createModel(); - CClass cl1 = model.getClass("CL1"); - CLink link = cl1.getLinkObjects(cl1.getClassifier().getAssociationByRoleName("metaclass2")).get(0); - - try { - link.getTaggedValue("x"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("tagged value 'x' unknown", e.getMessage()); - } - - try { - link.setTaggedValue("x", 1); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("tagged value 'x' unknown", e.getMessage()); - } - } - - @Test - public void testObjectTypeTaggedValues() throws CException { - CModel model = createModel(); - CMetaclass mcl = model.getMetaclass("MCL1"); - CStereotype stereotype = model.getStereotype("ST"); - CClass cl1 = model.getClass("CL1"); - CLink link = cl1.getLinkObjects(cl1.getClassifier().getAssociationByRoleName("metaclass2")).get(0); - - CClass attrType = model.createClass(mcl, "AttrType"); - CObject attrValue = model.createObject(attrType, "attrValue"); - - stereotype.addAttribute("attrTypeObj", attrValue); - stereotype.getAttribute("attrTypeObj"); - - assertEquals(attrValue, link.getTaggedValue("attrTypeObj")); - assertEquals("attrValue", ((CObject) link.getTaggedValue("attrTypeObj")).getName()); - - CObject attrValue2 = model.createObject(attrType, "attrValue2"); - link.setTaggedValue("attrTypeObj", attrValue2); - - assertEquals(attrValue2, link.getTaggedValue("attrTypeObj")); - assertEquals("attrValue2", ((CObject) link.getTaggedValue("attrTypeObj")).getName()); - - CObject nonAttrValue = model.createObject(cl1, "nonAttrValue"); - - try { - link.setTaggedValue("attrTypeObj", nonAttrValue); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("type classifier of object 'nonAttrValue' is not matching attribute type of attribute " + - "'attrTypeObj'", e.getMessage()); - } - } - - @Test - public void testAddObjectAttributeGetSetTaggedValue() throws CException { - CModel model = createModel(); - CMetaclass mcl = model.getMetaclass("MCL1"); - CStereotype stereotype = model.getStereotype("ST"); - CClass cl1 = model.getClass("CL1"); - CLink link = cl1.getLinkObjects(cl1.getClassifier().getAssociationByRoleName("metaclass2")).get(0); - - CClass attrType = model.createClass(mcl, "AttrType"); - CObject attrValue = model.createObject(attrType, "attrValue"); - - stereotype.addObjectAttribute("attrTypeObj", attrType).addObjectAttribute("attrTypeObj2", attrType); - assertEquals(null, link.getTaggedValue("attrTypeObj")); - link.setTaggedValue("attrTypeObj", attrValue); - assertEquals(attrValue, link.getTaggedValue("attrTypeObj")); - } - - - @Test - public void testTaggedValuesOnAttributesWithNoDefaultValues() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociationEnd end1 = mcl1.createEnd("metaclass1", "1"), - end2 = mcl2.createEnd("metaclass2", "1"); - CAssociation association = model.createAssociation(end1, end2); - CStereotype stereotype = model.createStereotype("ST", association); - - stereotype.addBooleanAttribute("isBoolean"); - stereotype.addIntAttribute("intVal"); - stereotype.addLongAttribute("longVal"); - stereotype.addDoubleAttribute("doubleVal"); - stereotype.addFloatAttribute("floatVal"); - stereotype.addStringAttribute("string"); - stereotype.addCharAttribute("char"); - stereotype.addByteAttribute("byte"); - stereotype.addShortAttribute("short"); - CClass attrClass = model.createClass(mcl1, "attrClass"); - stereotype.addObjectAttribute("obj1", attrClass); - stereotype.addObjectAttribute("obj2", "attrClass"); - CEnum enumType = model.createEnum("XYEnum", Arrays.asList("X", "Y")); - stereotype.addEnumAttribute("enum", enumType); - - CClass cl1 = model.createClass(mcl1, "CL1"); - CClass cl2 = model.createClass(mcl2, "CL2"); - cl1.addLink(end2, cl2); - CLink link = cl1.getLinkObjects(end2).get(0); - link.addStereotypeInstance(stereotype); - - assertEquals(null, link.getTaggedValue("isBoolean")); - assertEquals(null, link.getTaggedValue("intVal")); - assertEquals(null, link.getTaggedValue("longVal")); - assertEquals(null, link.getTaggedValue("doubleVal")); - assertEquals(null, link.getTaggedValue("floatVal")); - assertEquals(null, link.getTaggedValue("string")); - assertEquals(null, link.getTaggedValue("char")); - assertEquals(null, link.getTaggedValue("byte")); - assertEquals(null, link.getTaggedValue("short")); - assertEquals(null, link.getTaggedValue("obj1")); - assertEquals(null, link.getTaggedValue("obj2")); - assertEquals(null, link.getTaggedValue("enum")); - } - - - @Test - public void testEnumTypeTaggedValues() throws CException { - CModel model = createModel(); - CStereotype stereotype = model.getStereotype("ST"); - CClass cl1 = model.getClass("CL1"); - CLink link = cl1.getLinkObjects(cl1.getClassifier().getAssociationByRoleName("metaclass2")).get(0); - - List enumValues = Arrays.asList("A", "B", "C"); - CEnum enumObj = model.createEnum("ABCEnum", enumValues); - - - assertEquals(0, stereotype.getAttributes().size()); - stereotype.addEnumAttribute("l1", enumObj).setAttributeDefaultValue("l1", "A"); - stereotype.addEnumAttribute("l2", enumObj); - - assertEquals("A", link.getTaggedValue("l1")); - assertEquals(null, link.getTaggedValue("l2")); - - link.setTaggedValue("l1", "B"); - link.setTaggedValue("l2", "C"); - - assertEquals("B", link.getTaggedValue("l1")); - assertEquals("C", link.getTaggedValue("l2")); - - try { - link.setTaggedValue("l1", "X"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value 'X' not element of enumeration", e.getMessage()); - } - } - - @Test - public void testAttributeTypeCheckForTaggedValues() throws CException { - CModel model = createModel(); - CStereotype stereotype = model.getStereotype("ST"); - CMetaclass mcl = model.getMetaclass("MCL1"); - CClass cl1 = model.getClass("CL1"); - CLink link = cl1.getLinkObjects(cl1.getClassifier().getAssociationByRoleName("metaclass2")).get(0); - stereotype.addBooleanAttribute("a"); - - try { - link.setTaggedValue("a", CodeableModels.createModel()); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value for attribute 'a' is not a known attribute type", e.getMessage()); - } - try { - link.setTaggedValue("a", 1); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'a' does not match attribute type", e.getMessage()); - } - - stereotype.addIntAttribute("i"); - try { - link.setTaggedValue("i", Boolean.TRUE); - } catch (CException e) { - assertEquals("value type for attribute 'i' does not match attribute type", e.getMessage()); - } - - stereotype.addShortAttribute("s"); - try { - link.setTaggedValue("s", Boolean.TRUE); - } catch (CException e) { - assertEquals("value type for attribute 's' does not match attribute type", e.getMessage()); - } - - stereotype.addByteAttribute("b"); - try { - link.setTaggedValue("b", Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'b' does not match attribute type", e.getMessage()); - } - - stereotype.addLongAttribute("l"); - try { - link.setTaggedValue("l", Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'l' does not match attribute type", e.getMessage()); - } - - stereotype.addFloatAttribute("f"); - try { - link.setTaggedValue("f", Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'f' does not match attribute type", e.getMessage()); - } - - stereotype.addDoubleAttribute("d"); - try { - link.setTaggedValue("d", Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'd' does not match attribute type", e.getMessage()); - } - - CEnum enumType = model.createEnum("XYEnum", Arrays.asList("X", "Y")); - stereotype.addEnumAttribute("e", enumType); - try { - link.setTaggedValue("e", Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'e' does not match attribute type", e.getMessage()); - } - - CClass attrClass = model.createClass(mcl, "AttrClass"); - CClass otherClass = model.createClass(mcl, "OtherClass"); - stereotype.addObjectAttribute("o", attrClass); - try { - link.setTaggedValue("o", Boolean.TRUE); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("value type for attribute 'o' does not match attribute type", e.getMessage()); - } - - try { - link.setTaggedValue("o", otherClass); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("type classifier of object 'OtherClass' is not matching attribute type of attribute 'o'", e - .getMessage()); - } - - } - - @Test - public void testDeleteAttributesSetTaggedValue() throws CException { - CModel model = createModel(); - CStereotype stereotype = model.getStereotype("ST"); - CClass cl1 = model.getClass("CL1"); - CLink link = cl1.getLinkObjects(cl1.getClassifier().getAssociationByRoleName("metaclass2")).get(0); - - stereotype.addAttribute("isBoolean", true); - stereotype.addAttribute("intVal", 1); - - link.setTaggedValue("isBoolean", true); - - stereotype.deleteAttribute("isBoolean"); - - link.setTaggedValue("intVal", 1); - - try { - link.setTaggedValue("isBoolean", false); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("tagged value 'isBoolean' unknown", e.getMessage()); - } - } - - @Test - public void testTaggedValuesInheritance() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociationEnd end1 = mcl1.createEnd("metaclass1", "1"), - end2 = mcl2.createEnd("metaclass2", "1"); - CAssociation association = model.createAssociation(end1, end2); - CStereotype top1 = model.createStereotype( "Top1", association); - CStereotype top2 = model.createStereotype( "Top2"); - CStereotype stereotypeA = model.createStereotype( "STA").addSuperclass("Top1").addSuperclass("Top2"); - CStereotype stereotypeSubA = model.createStereotype("SubA").addSuperclass("STA"); - - top1.addAttribute("int0", 0); - top2.addAttribute("int1", 1); - stereotypeA.addAttribute("int2", 2); - stereotypeSubA.addAttribute("int3", 3); - - CClass cl1 = model.createClass(mcl1, "CL1"); - CClass cl2 = model.createClass(mcl2, "CL2"); - cl1.addLink(end2, cl2); - CLink link = cl1.getLinkObjects(end2).get(0); - link.addStereotypeInstance(stereotypeSubA); - - assertEquals(0, link.getTaggedValue("int0")); - assertEquals(1, link.getTaggedValue("int1")); - assertEquals(2, link.getTaggedValue("int2")); - assertEquals(3, link.getTaggedValue("int3")); - - assertEquals(0, link.getTaggedValue(top1,"int0")); - assertEquals(1, link.getTaggedValue(top2, "int1")); - assertEquals(2, link.getTaggedValue(stereotypeA, "int2")); - assertEquals(3, link.getTaggedValue(stereotypeSubA, "int3")); - - link.setTaggedValue("int0", 10); - link.setTaggedValue("int1", 11); - link.setTaggedValue("int2", 12); - link.setTaggedValue("int3", 13); - - assertEquals(10, link.getTaggedValue("int0")); - assertEquals(11, link.getTaggedValue("int1")); - assertEquals(12, link.getTaggedValue("int2")); - assertEquals(13, link.getTaggedValue("int3")); - - assertEquals(10, link.getTaggedValue(top1,"int0")); - assertEquals(11, link.getTaggedValue(top2, "int1")); - assertEquals(12, link.getTaggedValue(stereotypeA, "int2")); - assertEquals(13, link.getTaggedValue(stereotypeSubA, "int3")); - - link.setTaggedValue(top1,"int0", 100); - link.setTaggedValue(top2,"int1", 110); - link.setTaggedValue(stereotypeA,"int2", 120); - link.setTaggedValue(stereotypeSubA,"int3", 130); - - assertEquals(100, link.getTaggedValue("int0")); - assertEquals(110, link.getTaggedValue("int1")); - assertEquals(120, link.getTaggedValue("int2")); - assertEquals(130, link.getTaggedValue("int3")); - - assertEquals(100, link.getTaggedValue(top1,"int0")); - assertEquals(110, link.getTaggedValue(top2, "int1")); - assertEquals(120, link.getTaggedValue(stereotypeA, "int2")); - assertEquals(130, link.getTaggedValue(stereotypeSubA, "int3")); - } - - - @Test - public void testTaggedValuesInheritanceAfterDeleteSuperclass() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociationEnd end1 = mcl1.createEnd("metaclass1", "1"), - end2 = mcl2.createEnd("metaclass2", "1"); - CAssociation association = model.createAssociation(end1, end2); - CStereotype top1 = model.createStereotype( "Top1", association); - CStereotype top2 = model.createStereotype( "Top2"); - CStereotype stereotypeA = model.createStereotype( "STA").addSuperclass("Top1").addSuperclass("Top2"); - CStereotype stereotypeSubA = model.createStereotype("SubA").addSuperclass("STA"); - - top1.addAttribute("int0", 0); - top2.addAttribute("int1", 1); - stereotypeA.addAttribute("int2", 2); - stereotypeSubA.addAttribute("int3", 3); - - CClass cl1 = model.createClass(mcl1, "CL1"); - CClass cl2 = model.createClass(mcl2, "CL2"); - cl1.addLink(end2, cl2); - CLink link = cl1.getLinkObjects(end2).get(0); - link.addStereotypeInstance(stereotypeSubA); - - ((CClassifier) stereotypeA).deleteSuperclass(top2); - - assertEquals(0, link.getTaggedValue("int0")); - try { - link.getTaggedValue("int1"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("tagged value 'int1' unknown", e.getMessage()); - } - assertEquals(2, link.getTaggedValue("int2")); - assertEquals(3, link.getTaggedValue("int3")); - - link.setTaggedValue("int0", 10); - try { - link.setTaggedValue("int1", 11); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("tagged value 'int1' unknown", e.getMessage()); - } - link.setTaggedValue("int2", 12); - link.setTaggedValue("int3", 13); - } - - @Test - public void testTaggedValuesInheritanceMultipleStereotypes() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociationEnd end1 = mcl1.createEnd("metaclass1", "1"), - end2 = mcl2.createEnd("metaclass2", "1"); - CAssociation association = model.createAssociation(end1, end2); - - CStereotype top1 = model.createStereotype( "Top1", association); - CStereotype top2 = model.createStereotype( "Top2"); - CStereotype stereotypeA = model.createStereotype( "STA").addSuperclass("Top1").addSuperclass("Top2"); - CStereotype stereotypeSubA = model.createStereotype("SubA").addSuperclass("STA"); - CStereotype stereotypeB = model.createStereotype( "STB").addSuperclass("Top1").addSuperclass("Top2"); - CStereotype stereotypeSubB = model.createStereotype("SubB").addSuperclass("STB"); - CStereotype stereotypeC = model.createStereotype( "STC", association); - CStereotype stereotypeSubC = model.createStereotype("SubC").addSuperclass("STC"); - CClass cl1 = model.createClass(mcl1, "CL1"); - CClass cl2 = model.createClass(mcl2, "CL2"); - cl1.addLink(end2, cl2); - CLink link = cl1.getLinkObjects(end2).get(0); - link.addStereotypeInstance(stereotypeSubA); - link.addStereotypeInstance(stereotypeSubB); - link.addStereotypeInstance(stereotypeSubC); - - top1.addAttribute("int0", 0); - top2.addAttribute("int1", 1); - stereotypeA.addAttribute("int2", 2); - stereotypeSubA.addAttribute("int3", 3); - stereotypeB.addAttribute("int4", 4); - stereotypeSubB.addAttribute("int5", 5); - stereotypeC.addAttribute("int6", 6); - stereotypeSubC.addAttribute("int7", 7); - - assertEquals(0, link.getTaggedValue("int0")); - assertEquals(1, link.getTaggedValue("int1")); - assertEquals(2, link.getTaggedValue("int2")); - assertEquals(3, link.getTaggedValue("int3")); - assertEquals(4, link.getTaggedValue("int4")); - assertEquals(5, link.getTaggedValue("int5")); - assertEquals(6, link.getTaggedValue("int6")); - assertEquals(7, link.getTaggedValue("int7")); - - assertEquals(0, link.getTaggedValue(top1,"int0")); - assertEquals(1, link.getTaggedValue(top2, "int1")); - assertEquals(2, link.getTaggedValue(stereotypeA, "int2")); - assertEquals(3, link.getTaggedValue(stereotypeSubA, "int3")); - assertEquals(4, link.getTaggedValue(stereotypeB, "int4")); - assertEquals(5, link.getTaggedValue(stereotypeSubB, "int5")); - assertEquals(6, link.getTaggedValue(stereotypeC, "int6")); - assertEquals(7, link.getTaggedValue(stereotypeSubC, "int7")); - - link.setTaggedValue("int0", 10); - link.setTaggedValue("int1", 11); - link.setTaggedValue("int2", 12); - link.setTaggedValue("int3", 13); - link.setTaggedValue("int4", 14); - link.setTaggedValue("int5", 15); - link.setTaggedValue("int6", 16); - link.setTaggedValue("int7", 17); - - assertEquals(10, link.getTaggedValue("int0")); - assertEquals(11, link.getTaggedValue("int1")); - assertEquals(12, link.getTaggedValue("int2")); - assertEquals(13, link.getTaggedValue("int3")); - assertEquals(14, link.getTaggedValue("int4")); - assertEquals(15, link.getTaggedValue("int5")); - assertEquals(16, link.getTaggedValue("int6")); - assertEquals(17, link.getTaggedValue("int7")); - } - - @Test - public void testTaggedValuesSameNameInheritance() throws CException { - CModel model = CodeableModels.createModel(); - CMetaclass mcl1 = model.createMetaclass("MCL1"); - CMetaclass mcl2 = model.createMetaclass("MCL2"); - CAssociationEnd end1 = mcl1.createEnd("metaclass1", "1"), - end2 = mcl2.createEnd("metaclass2", "1"); - CAssociation association = model.createAssociation(end1, end2); - CStereotype top1 = model.createStereotype( "Top1", association); - CStereotype top2 = model.createStereotype( "Top2"); - CStereotype stereotypeA = model.createStereotype( "STA").addSuperclass("Top1").addSuperclass("Top2"); - CStereotype stereotypeSubA = model.createStereotype("SubA").addSuperclass("STA"); - - top1.addAttribute("int", 0); - top2.addAttribute("int", 1); - stereotypeA.addAttribute("int", 2); - stereotypeSubA.addAttribute("int", 3); - - - CClass cl1a = model.createClass(mcl1, "CL1A"); - CClass cl2a = model.createClass(mcl2, "CL2A"); - CLink link1 = cl1a.addLink(end2, cl2a); - link1.addStereotypeInstance(stereotypeSubA); - - CClass cl1b = model.createClass(mcl1, "CL1B"); - CClass cl2b = model.createClass(mcl2, "CL2B"); - CLink link2 = cl1b.addLink(end2, cl2b); - link2.addStereotypeInstance(stereotypeA); - - CClass cl1c = model.createClass(mcl1, "CL1C"); - CClass cl2c = model.createClass(mcl2, "CL2C"); - CLink link3 = cl1c.addLink(end2, cl2c); - link3.addStereotypeInstance(top1); - - assertEquals(3, link1.getTaggedValue("int")); - assertEquals(2, link2.getTaggedValue("int")); - assertEquals(0, link3.getTaggedValue("int")); - - assertEquals(3, link1.getTaggedValue(stereotypeSubA, "int")); - assertEquals(2, link1.getTaggedValue(stereotypeA, "int")); - assertEquals(1, link1.getTaggedValue(top2, "int")); - assertEquals(0, link1.getTaggedValue(top1, "int")); - assertEquals(2, link2.getTaggedValue(stereotypeA, "int")); - assertEquals(1, link2.getTaggedValue(top2, "int")); - assertEquals(0, link2.getTaggedValue(top1, "int")); - assertEquals(0, link3.getTaggedValue(top1,"int")); - - link1.setTaggedValue("int", 10); - link2.setTaggedValue("int", 11); - link3.setTaggedValue("int", 12); - - assertEquals(10, link1.getTaggedValue("int")); - assertEquals(11, link2.getTaggedValue("int")); - assertEquals(12, link3.getTaggedValue("int")); - - assertEquals(10, link1.getTaggedValue(stereotypeSubA, "int")); - assertEquals(2, link1.getTaggedValue(stereotypeA, "int")); - assertEquals(1, link1.getTaggedValue(top2, "int")); - assertEquals(0, link1.getTaggedValue(top1, "int")); - assertEquals(11, link2.getTaggedValue(stereotypeA, "int")); - assertEquals(1, link2.getTaggedValue(top2, "int")); - assertEquals(0, link2.getTaggedValue(top1, "int")); - assertEquals(12, link3.getTaggedValue(top1,"int")); - - link1.setTaggedValue(stereotypeSubA,"int", 130); - link1.setTaggedValue(top1,"int", 100); - link1.setTaggedValue(top2,"int", 110); - link1.setTaggedValue(stereotypeA,"int", 120); - - assertEquals(130, link1.getTaggedValue("int")); - - assertEquals(100, link1.getTaggedValue(top1,"int")); - assertEquals(110, link1.getTaggedValue(top2, "int")); - assertEquals(120, link1.getTaggedValue(stereotypeA, "int")); - assertEquals(130, link1.getTaggedValue(stereotypeSubA, "int")); - } - - @Test - public void testSetAndDeleteTaggedValues() throws CException { - CModel model = createModel(); - CStereotype stereotype = model.getStereotype("ST"); - CClass cl1 = model.getClass("CL1"); - CLink link = cl1.getLinkObjects(cl1.getClassifier().getAssociationByRoleName("metaclass2")).get(0); - stereotype.addAttribute("isBoolean", true); - stereotype.addAttribute("intVal", 1); - - link.setTaggedValue("isBoolean", true); - link.setTaggedValue("intVal", 1); - - assertEquals(true, link.getTaggedValue("isBoolean")); - assertEquals(1, link.getTaggedValue("intVal")); - - stereotype.deleteAttribute("isBoolean"); - - assertEquals(1, link.getTaggedValue("intVal")); - link.setTaggedValue("intVal", 100); - assertEquals(100, link.getTaggedValue("intVal")); - - try { - link.setTaggedValue("isBoolean", false); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("tagged value 'isBoolean' unknown", e.getMessage()); - } - } - - @Test - public void testSetAndGetTaggedValuesWithStereotypeSpecified() throws CException { - CModel model = createModel(); - CStereotype stereo = model.getStereotype("ST"); - CClass cl1 = model.getClass("CL1"); - CLink link = cl1.getLinkObjects(cl1.getClassifier().getAssociationByRoleName("metaclass2")).get(0); - stereo.addAttribute("isBoolean", true); - stereo.addAttribute("intVal", 1); - - link.setTaggedValue(stereo,"isBoolean", true); - link.setTaggedValue(stereo,"intVal", 1); - - assertEquals(true, link.getTaggedValue(stereo,"isBoolean")); - assertEquals(1, link.getTaggedValue(stereo,"intVal")); - } - - @Test - public void testWrongStereotypeInTaggedValue() throws CException { - CModel model = createModel(); - CClass cl1 = model.getClass("CL1"); - CAssociation association = cl1.getClassifier().getAssociationByRoleName("metaclass2"); - CStereotype stereo = model.createStereotype("Stereo", association); - CStereotype stereo2 = model.createStereotype("Stereo2", association); - CLink link = cl1.getLinkObjects(cl1.getClassifier().getAssociationByRoleName("metaclass2")).get(0); - link.addStereotypeInstance(stereo); - - stereo.addAttribute("isBoolean", true); - stereo2.addAttribute("isBoolean", true); - - link.setTaggedValue(stereo, "isBoolean", false); - - try { - link.setTaggedValue(stereo2, "isBoolean", false); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("stereotype 'Stereo2' is not a stereotype of element", e.getMessage()); - } - - assertEquals(false, link.getTaggedValue(stereo, "isBoolean")); - - try { - link.getTaggedValue(stereo2, "isBoolean"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("stereotype 'Stereo2' is not a stereotype of element", e.getMessage()); - } - } - - @Test - public void testUnknownTaggedValueOnStereotype() throws CException { - CModel model = createModel(); - CStereotype stereo = model.getStereotype("ST"); - CClass cl1 = model.getClass("CL1"); - CLink link = cl1.getLinkObjects(cl1.getClassifier().getAssociationByRoleName("metaclass2")).get(0); - try { - link.setTaggedValue(stereo, "isBoolean", false); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("tagged value 'isBoolean' unknown for stereotype 'ST'", e.getMessage()); - } - - try { - link.getTaggedValue(stereo, "isBoolean"); - fail("exception not thrown"); - } catch (CException e) { - assertEquals("tagged value 'isBoolean' unknown for stereotype 'ST'", e.getMessage()); - } - } - - @Test - public void testStereotypeLookup() throws CException { - CModel model = createModel(); - CClass cl1 = model.getClass("CL1"); - CAssociation association = cl1.getClassifier().getAssociationByRoleName("metaclass2"); - CStereotype stereo = model.createStereotype("Stereo", association); - CStereotype stereo2 = model.createStereotype("Stereo2", association); - CLink link = cl1.getLinkObjects(cl1.getClassifier().getAssociationByRoleName("metaclass2")).get(0); - link.addStereotypeInstance(stereo); - link.addStereotypeInstance(stereo2); - - stereo.addAttribute("isBoolean", true); - stereo2.addAttribute("isBoolean", true); - - link.setTaggedValue("isBoolean", false); - - assertEquals(false, link.getTaggedValue("isBoolean")); - assertEquals(false, link.getTaggedValue(stereo,"isBoolean")); - assertEquals(true, link.getTaggedValue(stereo2,"isBoolean")); - } - - - - -} diff --git a/tests/testCommons.py b/tests/testCommons.py new file mode 100644 index 0000000..ee7904e --- /dev/null +++ b/tests/testCommons.py @@ -0,0 +1,8 @@ +def exceptionExpected_(): + raise AssertionError("exception not raised") + +def neq_(a, b, msg=None): + """Shorthand for 'assert a != b, "%r == %r" % (a, b) + """ + if a == b: + raise AssertionError(msg or "%r == %r" % (a, b)) \ No newline at end of file diff --git a/tests/test_associations_ends_descriptor.py b/tests/test_associations_ends_descriptor.py new file mode 100644 index 0000000..92a88a1 --- /dev/null +++ b/tests/test_associations_ends_descriptor.py @@ -0,0 +1,273 @@ +import sys +sys.path.append("..") + +import re + +import nose +from nose.tools import ok_, eq_ +from testCommons import neq_, exceptionExpected_ +from parameterized import parameterized + +from codeableModels import CMetaclass, CClass, CObject, CAttribute, CException, CEnum, CStereotype + +class TestAssociationsEndsDescriptor(): + + def setUp(self): + self.mcl = CMetaclass("MCL") + self.c1 = CClass(self.mcl, "C1") + self.c2 = CClass(self.mcl, "C2") + self.c3 = CClass(self.mcl, "C3") + self.c4 = CClass(self.mcl, "C4") + self.c5 = CClass(self.mcl, "C5") + + def testEndsStringMalformed(self): + try: + a1 = self.c1.association(self.c2, '') + exceptionExpected_() + except CException as e: + eq_("association descriptor malformed: ''", e.value) + try: + a1 = self.c1.association(self.c2, '->->') + exceptionExpected_() + except CException as e: + eq_("malformed multiplicity: ''", e.value) + try: + a1 = self.c1.association(self.c2, 'a->b') + exceptionExpected_() + except CException as e: + eq_("malformed multiplicity: 'a'", e.value) + try: + a1 = self.c1.association(self.c2, '[]->[]') + exceptionExpected_() + except CException as e: + eq_("malformed multiplicity: '[]'", e.value) + try: + a1 = self.c1.association(self.c2, '[]1->[]*') + exceptionExpected_() + except CException as e: + eq_("malformed multiplicity: '[]1'", e.value) + try: + a1 = self.c1.association(self.c2, '::1->1') + exceptionExpected_() + except CException as e: + eq_("malformed multiplicity: ':1'", e.value) + try: + a1 = self.c1.association(self.c2, '1->1:') + exceptionExpected_() + except CException as e: + eq_("association descriptor malformed: ''", e.value) + + def testEndsStringAssociation(self): + a1 = self.c1.association(self.c2, '[a]1->[b]*') + eq_(a1.roleName, "b") + eq_(a1.sourceRoleName, "a") + eq_(a1.multiplicity, "*") + eq_(a1.sourceMultiplicity, "1") + eq_(a1.composition, False) + eq_(a1.aggregation, False) + + a1 = self.c1.association(self.c2, ' [a b] 1 -> [ b c_()-] * ') + eq_(a1.roleName, " b c_()-") + eq_(a1.sourceRoleName, "a b") + eq_(a1.multiplicity, "*") + eq_(a1.sourceMultiplicity, "1") + eq_(a1.composition, False) + eq_(a1.aggregation, False) + + a1 = self.c1.association(self.c2, '1..3-> 4..* ') + eq_(a1.roleName, None) + eq_(a1.sourceRoleName, None) + eq_(a1.multiplicity, "4..*") + eq_(a1.sourceMultiplicity, "1..3") + eq_(a1.composition, False) + eq_(a1.aggregation, False) + + a1 = self.c1.association(self.c2, '[ax] -> [bx]') + eq_(a1.roleName, "bx") + eq_(a1.sourceRoleName, "ax") + eq_(a1.multiplicity, "*") + eq_(a1.sourceMultiplicity, "1") + eq_(a1.composition, False) + eq_(a1.aggregation, False) + + def testEndsStringAggregation(self): + a1 = self.c1.association(self.c2, '[a]1<>-[b]*') + eq_(a1.roleName, "b") + eq_(a1.sourceRoleName, "a") + eq_(a1.multiplicity, "*") + eq_(a1.sourceMultiplicity, "1") + eq_(a1.composition, False) + eq_(a1.aggregation, True) + + a1 = self.c1.association(self.c2, ' [a b] 1 <>- [ b c_()-] * ') + eq_(a1.roleName, " b c_()-") + eq_(a1.sourceRoleName, "a b") + eq_(a1.multiplicity, "*") + eq_(a1.sourceMultiplicity, "1") + eq_(a1.composition, False) + eq_(a1.aggregation, True) + + a1 = self.c1.association(self.c2, '1..3<>- 4..* ') + eq_(a1.roleName, None) + eq_(a1.sourceRoleName, None) + eq_(a1.multiplicity, "4..*") + eq_(a1.sourceMultiplicity, "1..3") + eq_(a1.composition, False) + eq_(a1.aggregation, True) + + a1 = self.c1.association(self.c2, '[ax] <>- [bx]') + eq_(a1.roleName, "bx") + eq_(a1.sourceRoleName, "ax") + eq_(a1.multiplicity, "*") + eq_(a1.sourceMultiplicity, "1") + eq_(a1.composition, False) + eq_(a1.aggregation, True) + + def testEndsStringComposition(self): + a1 = self.c1.association(self.c2, '[a]1<*>-[b]*') + eq_(a1.roleName, "b") + eq_(a1.sourceRoleName, "a") + eq_(a1.multiplicity, "*") + eq_(a1.sourceMultiplicity, "1") + eq_(a1.composition, True) + eq_(a1.aggregation, False) + + a1 = self.c1.association(self.c2, ' [a b] 1 <*>- [ b c_()-] * ') + eq_(a1.roleName, " b c_()-") + eq_(a1.sourceRoleName, "a b") + eq_(a1.multiplicity, "*") + eq_(a1.sourceMultiplicity, "1") + eq_(a1.composition, True) + eq_(a1.aggregation, False) + + a1 = self.c1.association(self.c2, '1..3<*>- 4..* ') + eq_(a1.roleName, None) + eq_(a1.sourceRoleName, None) + eq_(a1.multiplicity, "4..*") + eq_(a1.sourceMultiplicity, "1..3") + eq_(a1.composition, True) + eq_(a1.aggregation, False) + + a1 = self.c1.association(self.c2, '[ax] <*>- [bx]') + eq_(a1.roleName, "bx") + eq_(a1.sourceRoleName, "ax") + eq_(a1.multiplicity, "*") + eq_(a1.sourceMultiplicity, "1") + eq_(a1.composition, True) + eq_(a1.aggregation, False) + + + def testEndsStringAssociationWithName(self): + a1 = self.c1.association(self.c2, ' assoc a : [a]1->[b]*') + eq_(a1.roleName, "b") + eq_(a1.sourceRoleName, "a") + eq_(a1.multiplicity, "*") + eq_(a1.sourceMultiplicity, "1") + eq_(a1.composition, False) + eq_(a1.aggregation, False) + eq_(a1.name, "assoc a") + + a1 = self.c1.association(self.c2, 'a: [a b] 1 -> [ b c_()-] * ') + eq_(a1.roleName, " b c_()-") + eq_(a1.sourceRoleName, "a b") + eq_(a1.multiplicity, "*") + eq_(a1.sourceMultiplicity, "1") + eq_(a1.composition, False) + eq_(a1.aggregation, False) + eq_(a1.name, "a") + + a1 = self.c1.association(self.c2, '"legal_name":1..3-> 4..* ') + eq_(a1.roleName, None) + eq_(a1.sourceRoleName, None) + eq_(a1.multiplicity, "4..*") + eq_(a1.sourceMultiplicity, "1..3") + eq_(a1.composition, False) + eq_(a1.aggregation, False) + eq_(a1.name, '"legal_name"') + + a1 = self.c1.association(self.c2, '[ax] -> [bx]:[ax] -> [bx]') + eq_(a1.roleName, "bx") + eq_(a1.sourceRoleName, "ax") + eq_(a1.multiplicity, "*") + eq_(a1.sourceMultiplicity, "1") + eq_(a1.composition, False) + eq_(a1.aggregation, False) + eq_(a1.name, '[ax] -> [bx]') + + + def testEndsStringAggregationWithName(self): + a1 = self.c1.association(self.c2, ' assoc a : [a]1<>-[b]*') + eq_(a1.roleName, "b") + eq_(a1.sourceRoleName, "a") + eq_(a1.multiplicity, "*") + eq_(a1.sourceMultiplicity, "1") + eq_(a1.composition, False) + eq_(a1.aggregation, True) + eq_(a1.name, "assoc a") + + a1 = self.c1.association(self.c2, ': [a b] 1 <>- [ b c_()-] * ') + eq_(a1.roleName, " b c_()-") + eq_(a1.sourceRoleName, "a b") + eq_(a1.multiplicity, "*") + eq_(a1.sourceMultiplicity, "1") + eq_(a1.composition, False) + eq_(a1.name, "") + + a1 = self.c1.association(self.c2, '"legal_name":1..3<>- 4..* ') + eq_(a1.roleName, None) + eq_(a1.sourceRoleName, None) + eq_(a1.multiplicity, "4..*") + eq_(a1.sourceMultiplicity, "1..3") + eq_(a1.composition, False) + eq_(a1.aggregation, True) + eq_(a1.name, '"legal_name"') + + a1 = self.c1.association(self.c2, '[ax] <>- [bx]:[ax] <>- [bx]') + eq_(a1.roleName, "bx") + eq_(a1.sourceRoleName, "ax") + eq_(a1.multiplicity, "*") + eq_(a1.sourceMultiplicity, "1") + eq_(a1.composition, False) + eq_(a1.aggregation, True) + eq_(a1.name, '[ax] <>- [bx]') + + + def testEndsStringCompositionWithName(self): + a1 = self.c1.association(self.c2, ' assoc a : [a]1<*>-[b]*') + eq_(a1.roleName, "b") + eq_(a1.sourceRoleName, "a") + eq_(a1.multiplicity, "*") + eq_(a1.sourceMultiplicity, "1") + eq_(a1.composition, True) + eq_(a1.aggregation, False) + eq_(a1.name, "assoc a") + + a1 = self.c1.association(self.c2, 'a: [a b] 1 <*>- [ b c_()-] * ') + eq_(a1.roleName, " b c_()-") + eq_(a1.sourceRoleName, "a b") + eq_(a1.multiplicity, "*") + eq_(a1.sourceMultiplicity, "1") + eq_(a1.composition, True) + eq_(a1.aggregation, False) + eq_(a1.name, "a") + + a1 = self.c1.association(self.c2, '"legal_name":1..3<*>- 4..* ') + eq_(a1.roleName, None) + eq_(a1.sourceRoleName, None) + eq_(a1.multiplicity, "4..*") + eq_(a1.sourceMultiplicity, "1..3") + eq_(a1.composition, True) + eq_(a1.aggregation, False) + eq_(a1.name, '"legal_name"') + + a1 = self.c1.association(self.c2, '[ax] <*>- [bx]:[ax] <*>- [bx]') + eq_(a1.roleName, "bx") + eq_(a1.sourceRoleName, "ax") + eq_(a1.multiplicity, "*") + eq_(a1.sourceMultiplicity, "1") + eq_(a1.composition, True) + eq_(a1.aggregation, False) + eq_(a1.name, '[ax] <*>- [bx]') + +if __name__ == "__main__": + nose.main() \ No newline at end of file diff --git a/tests/test_bundles.py b/tests/test_bundles.py new file mode 100644 index 0000000..5bbd406 --- /dev/null +++ b/tests/test_bundles.py @@ -0,0 +1,140 @@ +import sys +sys.path.append("..") + +import re + +import nose +from nose.tools import ok_, eq_ +from testCommons import neq_, exceptionExpected_ + + +from codeableModels import CBundle, CMetaclass, CClass, CObject, CAttribute, CException, CEnum, CLayer, CPackage + +class TestBundles(): + def setUp(self): + self.mcl = CMetaclass("MCL") + self.b1 = CBundle("B1") + + def testBundleNameFail(self): + try: + CBundle(self.mcl) + exceptionExpected_() + except CException as e: + ok_(e.value.startswith("is not a name string: '")) + ok_(e.value.endswith(" MCL'")) + + def testGetElementsWrongKwArg(self): + try: + self.b1.getElements(x = CBundle) + exceptionExpected_() + except CException as e: + eq_(e.value, "unknown argument to getElements: 'x'") + + def testGetElementWrongKwArg(self): + try: + self.b1.getElement(x = CBundle) + exceptionExpected_() + except CException as e: + eq_(e.value, "unknown argument to getElements: 'x'") + + def testPackageAndLayerSubclasses(self): + layer1 = CLayer("L1") + layer2 = CLayer("L2", subLayer = layer1) + package1 = CPackage("P1") + c1 = CClass(self.mcl, "C1") + c2 = CClass(self.mcl, "C2") + c3 = CClass(self.mcl, "C3") + layer1.elements = [c1, c2] + layer2.elements = [c3] + package1.elements = [layer1, layer2] + layer1.elements + layer2.elements + eq_(set(package1.elements), set([layer1, layer2, c1, c2, c3])) + eq_(set(layer1.elements), set([c1, c2])) + eq_(set(layer2.elements), set([c3])) + eq_(set(c1.bundles), set([layer1, package1])) + eq_(set(c2.bundles), set([layer1, package1])) + eq_(set(c3.bundles), set([layer2, package1])) + + def testLayerSubLayers(self): + layer1 = CLayer("L1") + eq_(layer1.superLayer, None) + eq_(layer1.subLayer, None) + + layer1 = CLayer("L1", subLayer = None) + eq_(layer1.superLayer, None) + eq_(layer1.subLayer, None) + + layer2 = CLayer("L2", subLayer = layer1) + eq_(layer1.superLayer, layer2) + eq_(layer1.subLayer, None) + eq_(layer2.superLayer, None) + eq_(layer2.subLayer, layer1) + + layer3 = CLayer("L3", subLayer = layer1) + eq_(layer1.superLayer, layer3) + eq_(layer1.subLayer, None) + eq_(layer2.superLayer, None) + eq_(layer2.subLayer, None) + eq_(layer3.superLayer, None) + eq_(layer3.subLayer, layer1) + + layer3.subLayer = layer2 + eq_(layer1.superLayer, None) + eq_(layer1.subLayer, None) + eq_(layer2.superLayer, layer3) + eq_(layer2.subLayer, None) + eq_(layer3.superLayer, None) + eq_(layer3.subLayer, layer2) + + layer2.subLayer = layer1 + eq_(layer1.superLayer, layer2) + eq_(layer1.subLayer, None) + eq_(layer2.superLayer, layer3) + eq_(layer2.subLayer, layer1) + eq_(layer3.superLayer, None) + eq_(layer3.subLayer, layer2) + + + def testLayerSuperLayers(self): + layer1 = CLayer("L1") + eq_(layer1.superLayer, None) + eq_(layer1.subLayer, None) + + layer1 = CLayer("L1", superLayer = None) + eq_(layer1.superLayer, None) + eq_(layer1.subLayer, None) + + layer2 = CLayer("L2", superLayer = layer1) + eq_(layer1.superLayer, None) + eq_(layer1.subLayer, layer2) + eq_(layer2.superLayer, layer1) + eq_(layer2.subLayer, None) + + layer3 = CLayer("L3", superLayer = layer1) + eq_(layer1.superLayer, None) + eq_(layer1.subLayer, layer3) + eq_(layer2.superLayer, None) + eq_(layer2.subLayer, None) + eq_(layer3.superLayer, layer1) + eq_(layer3.subLayer, None) + + layer3.superLayer = layer2 + eq_(layer1.superLayer, None) + eq_(layer1.subLayer, None) + eq_(layer2.superLayer, None) + eq_(layer2.subLayer, layer3) + eq_(layer3.superLayer, layer2) + eq_(layer3.subLayer, None) + + layer2.superLayer = layer1 + eq_(layer1.superLayer, None) + eq_(layer1.subLayer, layer2) + eq_(layer2.superLayer, layer1) + eq_(layer2.subLayer, layer3) + eq_(layer3.superLayer, layer2) + eq_(layer3.subLayer, None) + +if __name__ == "__main__": + nose.main() + + + diff --git a/tests/test_bundles_of_bundles.py b/tests/test_bundles_of_bundles.py new file mode 100644 index 0000000..4801860 --- /dev/null +++ b/tests/test_bundles_of_bundles.py @@ -0,0 +1,266 @@ +import sys +sys.path.append("..") + +import re + +import nose +from nose.tools import ok_, eq_ +from testCommons import neq_, exceptionExpected_ + + +from codeableModels import CBundle, CMetaclass, CClass, CObject, CAttribute, CException, CEnum + +class TestBundlesOfBundles(): + def setUp(self): + self.mcl = CMetaclass("MCL") + self.b1 = CBundle("B1") + self.b2 = CBundle("B2") + + def testBundleDefinedBundles(self): + eq_(set(self.b1.getElements()), set()) + b1 = CBundle("B1", bundles = self.b1) + eq_(set(self.b1.getElements()), set([b1])) + b2 = CBundle("B2", bundles = [self.b1]) + b3 = CBundle("B3", bundles = [self.b1, self.b2]) + mcl = CMetaclass("MCL", bundles = self.b1) + eq_(set(self.b1.getElements(type = CBundle)), set([b1, b2, b3])) + eq_(set(self.b1.elements), set([b1, b2, b3, mcl])) + eq_(set(self.b2.getElements(type = CBundle)), set([b3])) + eq_(set(self.b2.elements), set([b3])) + + def testBundleDefinedByBundleList(self): + b1 = CBundle("B1") + b2 = CBundle("B2") + b3 = CBundle("P3") + eq_(set(self.b1.getElements(type = CBundle)), set()) + ba = CBundle("PA", elements = [b1, b2, b3]) + eq_(set(ba.elements), set([b1, b2, b3])) + self.mcl.bundles = ba + eq_(set(ba.elements), set([b1, b2, b3, self.mcl])) + eq_(set(ba.getElements(type = CBundle)), set([b1, b2, b3])) + bb = CBundle("PB") + bb.elements = [b2, b3] + eq_(set(bb.getElements(type = CBundle)), set([b2, b3])) + eq_(set(b1.bundles), set([ba])) + eq_(set(b2.bundles), set([ba, bb])) + eq_(set(b3.bundles), set([ba, bb])) + eq_(set(ba.bundles), set()) + eq_(set(bb.bundles), set()) + eq_(set(b1.elements), set()) + eq_(set(b2.elements), set()) + eq_(set(b3.elements), set()) + + def testGetBundlesByName(self): + eq_(set(self.b1.getElements(name = "B1")), set()) + b1 = CBundle("B1", bundles = self.b1) + m = CMetaclass("B1", bundles = self.b1) + eq_(self.b1.getElements(type=CMetaclass), [m]) + eq_(set(self.b1.getElements(name = "B1", type = CBundle)), set([b1])) + b2 = CBundle("B1", bundles = self.b1) + eq_(set(self.b1.getElements(name = "B1", type = CBundle)), set([b1, b2])) + ok_(b1 != b2) + b3 = CBundle("B1", bundles = self.b1) + eq_(set(self.b1.getElements(name = "B1", type = CBundle)), set([b1, b2, b3])) + eq_(self.b1.getElement(name = "B1", type = CBundle), b1) + + def testgetBundleElementsByName(self): + eq_(set(self.b1.getElements(name = "B1")), set()) + b1 = CBundle("B1", bundles = self.b1) + eq_(set(self.b1.getElements(name = "B1")), set([b1])) + m = CMetaclass("B1", bundles = self.b1) + eq_(set(self.b1.getElements(name = "B1")), set([m, b1])) + b2 = CBundle("B1", bundles = self.b1) + eq_(set(self.b1.getElements(name = "B1")), set([m, b1, b2])) + ok_(b1 != b2) + b3 = CBundle("B1", bundles = self.b1) + eq_(set(self.b1.getElements(name = "B1")), set([m, b1, b2, b3])) + eq_(self.b1.getElement(name = "B1"), b1) + + def testBundleDefinedBundleChange(self): + b1 = CBundle("B1", bundles = self.b1) + b2 = CBundle("B2", bundles = self.b1) + b3 = CBundle("P3", bundles = self.b1) + mcl = CMetaclass("MCL", bundles = self.b1) + b = CBundle() + b2.bundles = b + b3.bundles = None + self.mcl.bundles = b + eq_(set(self.b1.elements), set([mcl, b1])) + eq_(set(self.b1.getElements(type = CBundle)), set([b1])) + eq_(set(b.elements), set([b2, self.mcl])) + eq_(set(b.getElements(type = CBundle)), set([b2])) + eq_(b1.bundles, [self.b1]) + eq_(b2.bundles, [b]) + eq_(b3.bundles, []) + + def testBundleDeleteBundle(self): + b1 = CBundle("B1", bundles = self.b1) + b2 = CBundle("B2", bundles = self.b1) + b3 = CBundle("P3", bundles = self.b1) + self.b1.delete() + eq_(set(self.b1.elements), set()) + eq_(b1.getElements(type = CBundle), []) + eq_(b1.elements, []) + eq_(b2.getElements(type = CBundle), []) + eq_(b3.getElements(type = CBundle), []) + + def testCreationOfUnnamedBundleInBundle(self): + b1 = CBundle() + b2 = CBundle() + b3 = CBundle("x") + mcl = CMetaclass() + self.b1.elements = [b1, b2, b3, mcl] + eq_(set(self.b1.getElements(type = CBundle)), set([b1, b2, b3])) + eq_(self.b1.getElement(name = None, type = CBundle), b1) + eq_(set(self.b1.getElements(name = None, type = CBundle)), set([b1, b2])) + eq_(set(self.b1.getElements(name = None)), set([b1, b2, mcl])) + + def testRemoveBundleFromBundle(self): + b1 = CBundle("B1") + b2 = CBundle("B2") + ba = CBundle("A", bundles = b1) + try: + b1.remove(None) + exceptionExpected_() + except CException as e: + eq_("'None' is not an element of the bundle", e.value) + try: + b1.remove(CEnum("A")) + exceptionExpected_() + except CException as e: + eq_("'A' is not an element of the bundle", e.value) + try: + b2.remove(ba) + exceptionExpected_() + except CException as e: + eq_("'A' is not an element of the bundle", e.value) + b1.remove(ba) + eq_(set(b1.getElements(type = CBundle)), set()) + + mcl1 = CMetaclass("MCL") + cl1 = CClass(mcl1, "CL") + + ba = CBundle("PA", bundles = b1) + bb = CBundle("PB", bundles = b1) + bc = CBundle("PC", bundles = b1, elements = [mcl1, cl1]) + + b1.remove(ba) + try: + b1.remove(CBundle("PB", bundles = b2)) + exceptionExpected_() + except CException as e: + eq_("'PB' is not an element of the bundle", e.value) + try: + b1.remove(ba) + exceptionExpected_() + except CException as e: + eq_("'PA' is not an element of the bundle", e.value) + + eq_(set(b1.getElements(type = CBundle)), set([bb, bc])) + b1.remove(bc) + eq_(set(b1.getElements(type = CBundle)), set([bb])) + + eq_(bc.getElements(type = CBundle), []) + eq_(bc.getElements(type = CBundle), []) + eq_(bc.elements, [mcl1, cl1]) + + def testDeleteBundleFromBundle(self): + b1 = CBundle("B1") + ba = CBundle("A", bundles = b1) + ba.delete() + eq_(set(b1.getElements(type = CBundle)), set()) + + mcl1 = CMetaclass("MCL") + cl1 = CClass(mcl1, "CL") + + ba = CBundle("PA", bundles = b1) + bb = CBundle("PB", bundles = b1) + bc = CBundle("PC", bundles = b1, elements = [mcl1, cl1]) + + ba.delete() + eq_(set(b1.getElements(type = CBundle)), set([bb, bc])) + bc.delete() + eq_(set(b1.getElements(type = CBundle)), set([bb])) + + eq_(bc.getElements(type = CBundle), []) + eq_(bc.getElements(type = CBundle), []) + eq_(bc.elements, []) + + def testRemoveBundleFromTwoBundles(self): + b1 = CBundle("B1") + b2 = CBundle("B2") + ba = CBundle("ba", bundles = [b1, b2]) + b1.remove(ba) + eq_(set(b1.getElements(type=CBundle)), set()) + eq_(set(b2.getElements(type=CBundle)), set([ba])) + eq_(set(ba.bundles), set([b2])) + + def testDeleteBundleFromTwoBundles(self): + b1 = CBundle("B1") + b2 = CBundle("B2") + ba = CBundle("ba", bundles = [b1, b2]) + b1.delete() + eq_(set(b1.getElements(type=CBundle)), set()) + eq_(set(b2.getElements(type=CBundle)), set([ba])) + eq_(set(ba.bundles), set([b2])) + + def testDeleteBundleHavingTwoBundles(self): + b1 = CBundle("B1") + b2 = CBundle("B2") + ba = CBundle("ba", bundles = [b1, b2]) + bb = CBundle("bb", bundles = [b2]) + ba.delete() + eq_(set(b1.getElements(type=CBundle)), set()) + eq_(set(b2.getElements(type=CBundle)), set([bb])) + eq_(set(ba.bundles), set()) + eq_(set(bb.bundles), set([b2])) + + def testDeleteTopLevelBundle(self): + b1 = CBundle("B1") + ba = CBundle("A", bundles = b1) + b1.delete() + eq_(b1.getElements(type = CBundle), []) + eq_(ba.getElements(type = CBundle), []) + + b1 = CBundle("B1") + mcl1 = CMetaclass("MCL") + cl1 = CClass(mcl1, "CL") + + ba = CBundle("BA", bundles = b1) + bb = CBundle("BB", bundles = b1) + bc = CBundle("BC", bundles = b1, elements = [mcl1, cl1]) + + b1.delete() + eq_(b1.getElements(type = CBundle), []) + eq_(b1.elements, []) + eq_(ba.getElements(type = CBundle), []) + eq_(bb.getElements(type = CBundle), []) + eq_(bc.getElements(type = CBundle), []) + eq_(bc.elements, [mcl1, cl1]) + eq_(bc.getElements(type = CBundle), []) + eq_(mcl1.classes, [cl1]) + eq_(mcl1.bundles, [bc]) + eq_(cl1.metaclass, mcl1) + eq_(cl1.bundles, [bc]) + + + def testBundleThatIsDeleted(self): + b1 = CBundle("B1") + b1.delete() + try: + CBundle("A", bundles = b1) + exceptionExpected_() + except CException as e: + eq_(e.value, "cannot access named element that has been deleted") + + + def testSetBundleToNone(self): + p = CBundle("A", bundles = None) + eq_(p.getElements(type = CBundle), []) + eq_(p.name, "A") + +if __name__ == "__main__": + nose.main() + + + diff --git a/tests/test_bundles_of_classes.py b/tests/test_bundles_of_classes.py new file mode 100644 index 0000000..0868e3b --- /dev/null +++ b/tests/test_bundles_of_classes.py @@ -0,0 +1,322 @@ +import sys +sys.path.append("..") + +import re + +import nose +from nose.tools import ok_, eq_ +from testCommons import neq_, exceptionExpected_ +from parameterized import parameterized + +from codeableModels import CBundle, CMetaclass, CClass, CObject, CAttribute, CException, CEnum + +class TestBundlesOfClasses(): + def setUp(self): + self.mcl = CMetaclass("MCL", attributes = {"i" : 1}) + self.b1 = CBundle("B1") + self.b2 = CBundle("B2") + + def testClassNameFail(self): + try: + CClass(self.mcl, self.mcl) + exceptionExpected_() + except CException as e: + ok_(e.value.startswith("is not a name string: '")) + ok_(e.value.endswith(" MCL'")) + + def testClassDefinedBundles(self): + eq_(set(self.b1.getElements()), set()) + cl1 = CClass(self.mcl, "Class1", bundles = self.b1) + eq_(set(self.b1.getElements()), set([cl1])) + cl2 = CClass(self.mcl, "Class2", bundles = [self.b1]) + cl3 = CClass(self.mcl, "Class3", bundles = [self.b1, self.b2]) + mcl = CMetaclass("MCL", bundles = self.b1) + eq_(set(self.b1.getElements(type = CClass)), set([cl1, cl2, cl3])) + eq_(set(self.b1.elements), set([cl1, cl2, cl3, mcl])) + eq_(set(self.b2.getElements(type = CClass)), set([cl3])) + eq_(set(self.b2.elements), set([cl3])) + + + def testBundleDefinedClasses(self): + cl1 = CClass(self.mcl, "Class1") + cl2 = CClass(self.mcl, "Class2") + cl3 = CClass(self.mcl, "Class3") + eq_(set(self.b1.getElements(type = CClass)), set()) + b1 = CBundle("B1", elements = [cl1, cl2, cl3]) + eq_(set(b1.elements), set([cl1, cl2, cl3])) + self.mcl.bundles = b1 + eq_(set(b1.elements), set([cl1, cl2, cl3, self.mcl])) + eq_(set(b1.getElements(type = CClass)), set([cl1, cl2, cl3])) + b2 = CBundle("B2") + b2.elements = [cl2, cl3] + eq_(set(b2.getElements(type = CClass)), set([cl2, cl3])) + eq_(set(cl1.bundles), set([b1])) + eq_(set(cl2.bundles), set([b1, b2])) + eq_(set(cl3.bundles), set([b1, b2])) + + def testGetClassesByName(self): + eq_(set(self.b1.getElements(type=CClass, name ="CL1")), set()) + c1 = CClass(self.mcl, "CL1", bundles = self.b1) + m = CMetaclass("CL1", bundles = self.b1) + eq_(self.b1.getElements(type=CMetaclass), [m]) + eq_(set(self.b1.getElements(type=CClass, name = "CL1")), set([c1])) + c2 = CClass(self.mcl, "CL1", bundles = self.b1) + eq_(set(self.b1.getElements(type=CClass, name = "CL1")), set([c1, c2])) + ok_(c1 != c2) + c3 = CClass(self.mcl, "CL1", bundles = self.b1) + eq_(set(self.b1.getElements(type=CClass, name = "CL1")), set([c1, c2, c3])) + eq_(self.b1.getElement(type=CClass, name = "CL1"), c1) + + def testGetClassElementsByName(self): + eq_(set(self.b1.getElements(name = "CL1")), set()) + c1 = CClass(self.mcl, "CL1", bundles = self.b1) + eq_(set(self.b1.getElements(name = "CL1")), set([c1])) + m = CMetaclass("CL1", bundles = self.b1) + eq_(set(self.b1.getElements(name = "CL1")), set([m, c1])) + c2 = CClass(self.mcl, "CL1", bundles = self.b1) + eq_(set(self.b1.getElements(name = "CL1")), set([m, c1, c2])) + ok_(c1 != c2) + c3 = CClass(self.mcl, "CL1", bundles = self.b1) + eq_(set(self.b1.getElements(name = "CL1")), set([m, c1, c2, c3])) + eq_(self.b1.getElement(name = "CL1"), c1) + + def testClassDefinedBundleChange(self): + cl1 = CClass(self.mcl, "Class1", bundles = self.b1) + cl2 = CClass(self.mcl, "Class2", bundles = self.b1) + cl3 = CClass(self.mcl, "Class3", bundles = self.b1) + mcl = CMetaclass("MCL", bundles = self.b1) + b = CBundle() + cl2.bundles = b + cl3.bundles = None + self.mcl.bundles = b + eq_(set(self.b1.elements), set([mcl, cl1])) + eq_(set(self.b1.getElements(type = CClass)), set([cl1])) + eq_(set(b.elements), set([cl2, self.mcl])) + eq_(set(b.getElements(type = CClass)), set([cl2])) + eq_(cl1.bundles, [self.b1]) + eq_(cl2.bundles, [b]) + eq_(cl3.bundles, []) + + def testBundleDeleteClass(self): + cl1 = CClass(self.mcl, "Class1", bundles = self.b1) + cl2 = CClass(self.mcl, "Class2", bundles = self.b1) + cl3 = CClass(self.mcl, "Class3", bundles = self.b1) + self.b1.delete() + eq_(set(self.b1.elements), set()) + eq_(cl1.bundles, []) + eq_(cl1.metaclass, self.mcl) + eq_(cl2.bundles, []) + eq_(cl3.bundles, []) + + def testCreationOfUnnamedClassInBundle(self): + c1 = CClass(self.mcl) + c2 = CClass(self.mcl) + c3 = CClass(self.mcl, "x") + mcl = CMetaclass() + self.b1.elements = [c1, c2, c3, mcl] + eq_(set(self.b1.getElements(type=CClass)), set([c1, c2, c3])) + eq_(self.b1.getElement(type=CClass, name = None), c1) + eq_(set(self.b1.getElements(type=CClass, name = None)), set([c1, c2])) + eq_(set(self.b1.getElements(name = None)), set([c1, c2, mcl])) + + def testRemoveClassFromBundle(self): + b1 = CBundle("B1") + b2 = CBundle("B2") + cl1 = CClass(self.mcl, "CL1", bundles = b1) + try: + b1.remove(None) + exceptionExpected_() + except CException as e: + eq_("'None' is not an element of the bundle", e.value) + try: + b1.remove(CEnum("A")) + exceptionExpected_() + except CException as e: + eq_("'A' is not an element of the bundle", e.value) + try: + b2.remove(cl1) + exceptionExpected_() + except CException as e: + eq_("'CL1' is not an element of the bundle", e.value) + b1.remove(cl1) + eq_(set(b1.getElements(type=CClass)), set()) + + cl1 = CClass(self.mcl, "CL1", bundles = b1) + cl2 = CClass(self.mcl, "CL2", bundles = b1) + cl3 = CClass(self.mcl, "CL3", superclasses = cl2, attributes = {"i" : 1}, bundles = b1) + cl3.setValue("i", 7) + o = CObject(cl3, bundles = b1) + + b1.remove(cl1) + try: + b1.remove(CClass(CMetaclass("MCL", bundles = b2), "CL2", bundles = b2)) + exceptionExpected_() + except CException as e: + eq_("'CL2' is not an element of the bundle", e.value) + try: + b1.remove(cl1) + exceptionExpected_() + except CException as e: + eq_("'CL1' is not an element of the bundle", e.value) + + eq_(set(b1.getElements(type=CClass)), set([cl2, cl3])) + b1.remove(cl3) + eq_(set(b1.getElements(type=CClass)), set([cl2])) + + eq_(cl3.superclasses, [cl2]) + eq_(cl2.subclasses, [cl3]) + eq_(cl3.attributeNames, ["i"]) + eq_(cl3.metaclass, self.mcl) + eq_(cl3.objects, [o]) + eq_(cl3.name, "CL3") + eq_(cl3.bundles, []) + eq_(b1.getElements(type=CObject), [o]) + eq_(cl3.getValue("i"), 7) + + def testDeleteClassFromBundle(self): + b1 = CBundle("B1") + cl1 = CClass(self.mcl, "CL1", bundles = b1) + cl1.delete() + eq_(set(b1.getElements(type=CClass)), set()) + + cl1 = CClass(self.mcl, "CL1", bundles = b1) + cl2 = CClass(self.mcl, "CL2", bundles = b1) + cl3 = CClass(self.mcl, "CL3", superclasses = cl2, attributes = {"i" : 1}, bundles = b1) + cl3.setValue("i", 7) + CObject(cl3, bundles = b1) + cl1.delete() + eq_(set(b1.getElements(type=CClass)), set([cl2, cl3])) + cl3.delete() + eq_(set(b1.getElements(type=CClass)), set([cl2])) + + eq_(cl3.superclasses, []) + eq_(cl2.subclasses, []) + eq_(cl3.attributes, []) + eq_(cl3.attributeNames, []) + eq_(cl3.metaclass, None) + eq_(cl3.objects, []) + eq_(cl3.name, None) + eq_(cl3.bundles, []) + eq_(b1.getElements(type=CObject), []) + try: + cl3.getValue("i") + exceptionExpected_() + except CException as e: + eq_("can't get value 'i' on deleted object", e.value) + + + def testRemoveBundleFromTwoBundles(self): + b1 = CBundle("B1") + b2 = CBundle("B2") + c1 = CClass(self.mcl, "c1", bundles = [b1, b2]) + b1.remove(c1) + eq_(set(b1.getElements(type=CClass)), set()) + eq_(set(b2.getElements(type=CClass)), set([c1])) + eq_(set(c1.bundles), set([b2])) + + def testDeleteBundleFromTwoBundles(self): + b1 = CBundle("B1") + b2 = CBundle("B2") + c1 = CClass(self.mcl, "c1", bundles = [b1, b2]) + b1.delete() + eq_(set(b1.getElements(type=CClass)), set()) + eq_(set(b2.getElements(type=CClass)), set([c1])) + eq_(set(c1.bundles), set([b2])) + + def testDeleteClassHavingTwoBundles(self): + b1 = CBundle("B1") + b2 = CBundle("B2") + c1 = CClass(self.mcl, "c1", bundles = [b1, b2]) + c2 = CClass(self.mcl, "c2", bundles = [b2]) + c1.delete() + eq_(set(b1.getElements(type=CClass)), set()) + eq_(set(b2.getElements(type=CClass)), set([c2])) + eq_(set(c1.bundles), set()) + eq_(set(c2.bundles), set([b2])) + + def testDeleteClassThatIsAnAttributeType(self): + b1 = CBundle("B1") + cl1 = CClass(self.mcl, "CL1", bundles = b1) + cl2 = CClass(self.mcl, "CL2", bundles = b1) + cl3 = CClass(self.mcl, "CL3", bundles = b1) + o3 = CObject(cl3, "O3") + + ea1 = CAttribute(type = cl3, default = o3) + c = CClass(self.mcl, "C", bundles = b1, attributes = {"o" : ea1}) + o = CObject(c) + cl1.delete() + cl3.delete() + try: + ea1.default + exceptionExpected_() + except CException as e: + eq_("cannot access named element that has been deleted", e.value) + try: + ea1.type + exceptionExpected_() + except CException as e: + eq_("cannot access named element that has been deleted", e.value) + try: + ea1.default = "3" + exceptionExpected_() + except CException as e: + eq_("cannot access named element that has been deleted", e.value) + try: + ea1.type = cl1 + exceptionExpected_() + except CException as e: + eq_("cannot access named element that has been deleted", e.value) + try: + ea1.type = cl2 + exceptionExpected_() + except CException as e: + eq_("default value '' incompatible with attribute's type 'CL2'", e.value) + try: + o.setValue("o", CObject(cl2)) + exceptionExpected_() + except CException as e: + eq_("cannot access named element that has been deleted", e.value) + try: + o.getValue("o") + exceptionExpected_() + except CException as e: + eq_("cannot access named element that has been deleted", e.value) + + + def testBundleThatIsDeleted(self): + b1 = CBundle("B1") + b2 = CBundle("B2") + b1.delete() + try: + CClass(self.mcl, "CL1", bundles = b1) + exceptionExpected_() + except CException as e: + eq_(e.value, "cannot access named element that has been deleted") + + + def testSetBundleToNone(self): + c = CClass(self.mcl, "C", bundles = None) + eq_(c.bundles, []) + eq_(c.name, "C") + + def testBundleElementsThatAreDeleted(self): + c = CClass(self.mcl, "C") + c.delete() + try: + CBundle("B1", elements = [c]) + exceptionExpected_() + except CException as e: + eq_(e.value, "cannot access named element that has been deleted") + + def testBundleElementsThatAreNone(self): + try: + CBundle("B1", elements = [None]) + exceptionExpected_() + except CException as e: + eq_(e.value, "'None' cannot be an element of bundle") + +if __name__ == "__main__": + nose.main() + + + diff --git a/tests/test_bundles_of_enums.py b/tests/test_bundles_of_enums.py new file mode 100644 index 0000000..8443c0a --- /dev/null +++ b/tests/test_bundles_of_enums.py @@ -0,0 +1,322 @@ +import sys +sys.path.append("..") + +import re + +import nose +from nose.tools import ok_, eq_ +from testCommons import neq_, exceptionExpected_ +from parameterized import parameterized + +from codeableModels import CBundle, CMetaclass, CClass, CObject, CAttribute, CException, CEnum + +class TestBundlesOfEnums(): + def setUp(self): + self.mcl = CMetaclass("MCL") + self.b1 = CBundle("B1") + self.b2 = CBundle("B2") + + def testEnumNameFail(self): + try: + CEnum(self.mcl) + exceptionExpected_() + except CException as e: + ok_(e.value.startswith("is not a name string: '")) + ok_(e.value.endswith(" MCL'")) + + def testEnumDefinedBundles(self): + eq_(set(self.b1.getElements()), set()) + e1 = CEnum("E1", values = ["A", "B", "C"], bundles = self.b1) + eq_(set(self.b1.getElements()), set([e1])) + e2 = CEnum("E2", values = ["A", "B", "C"], bundles = [self.b1]) + e3 = CEnum("E3", values = ["A", "B", "C"], bundles = [self.b1, self.b2]) + mcl = CMetaclass("MCL", bundles = self.b1) + eq_(set(self.b1.getElements(type=CEnum)), set([e1, e2, e3])) + eq_(set(self.b1.elements), set([e1, e2, e3, mcl])) + eq_(set(self.b2.getElements(type = CEnum)), set([e3])) + eq_(set(self.b2.elements), set([e3])) + + def testBundleDefinedEnums(self): + e1 = CEnum("E1", values = ["A", "B", "C"]) + e2 = CEnum("E2", values = ["A", "B", "C"]) + e3 = CEnum("E3", values = ["A", "B", "C"]) + eq_(set(self.b1.getElements(type=CEnum)), set()) + b1 = CBundle("B1", elements = [e1, e2, e3]) + eq_(set(b1.elements), set([e1, e2, e3])) + self.mcl.bundles = b1 + eq_(set(b1.elements), set([e1, e2, e3, self.mcl])) + eq_(set(b1.getElements(type=CEnum)), set([e1, e2, e3])) + b2 = CBundle("B2") + b2.elements = [e2, e3] + eq_(set(b2.getElements(type=CEnum)), set([e2, e3])) + eq_(set(e1.bundles), set([b1])) + eq_(set(e2.bundles), set([b1, b2])) + eq_(set(e3.bundles), set([b1, b2])) + def testGetEnumsByName(self): + eq_(set(self.b1.getElements(type=CEnum, name = "E1")), set()) + e1 = CEnum("E1", bundles = self.b1) + m = CMetaclass("E1", bundles = self.b1) + eq_(self.b1.getElements(type = CMetaclass), [m]) + eq_(set(self.b1.getElements(type=CEnum, name = "E1")), set([e1])) + e2 = CEnum("E1", bundles = self.b1) + eq_(set(self.b1.getElements(type=CEnum, name = "E1")), set([e1, e2])) + ok_(e1 != e2) + e3 = CEnum("E1", bundles = self.b1) + eq_(set(self.b1.getElements(type=CEnum, name = "E1")), set([e1, e2, e3])) + eq_(self.b1.getElement(type=CEnum, name ="E1"), e1) + + def testgetEnumElementsByName(self): + eq_(set(self.b1.getElements(name = "E1")), set()) + e1 = CEnum("E1", bundles = self.b1) + eq_(set(self.b1.getElements(name = "E1")), set([e1])) + m = CMetaclass("E1", bundles = self.b1) + eq_(set(self.b1.getElements(name = "E1")), set([m, e1])) + e2 = CEnum("E1", bundles = self.b1) + eq_(set(self.b1.getElements(name = "E1")), set([m, e1, e2])) + ok_(e1 != e2) + e3 = CEnum("E1", bundles = self.b1) + eq_(set(self.b1.getElements(name = "E1")), set([m, e1, e2, e3])) + eq_(self.b1.getElement(name = "E1"), e1) + + def testEnumDefinedBundleChange(self): + e1 = CEnum("E1", bundles = self.b1) + e2 = CEnum("E2", bundles = self.b1) + e3 = CEnum("E3", bundles = self.b1) + mcl = CMetaclass("MCL", bundles = self.b1) + b = CBundle() + e2.bundles = b + e3.bundles = None + self.mcl.bundles = b + eq_(set(self.b1.elements), set([mcl, e1])) + eq_(set(self.b1.getElements(type=CEnum)), set([e1])) + eq_(set(b.elements), set([e2, self.mcl])) + eq_(set(b.getElements(type=CEnum)), set([e2])) + eq_(e1.bundles, [self.b1]) + eq_(e2.bundles, [b]) + eq_(e3.bundles, []) + + def testBundleDeleteEnum(self): + e1 = CEnum("E1", bundles = self.b1) + e2 = CEnum("E2", bundles = self.b1) + e3 = CEnum("E3", bundles = self.b1) + self.b1.delete() + eq_(set(self.b1.elements), set()) + eq_(e1.bundles, []) + eq_(e1.name, "E1") + eq_(e2.bundles, []) + eq_(e3.bundles, []) + + def testCreationOfUnnamedEnumInBundle(self): + e1 = CEnum() + e2 = CEnum() + e3 = CEnum("x") + mcl = CMetaclass() + self.b1.elements = [e1, e2, e3, mcl] + eq_(set(self.b1.getElements(type=CEnum)), set([e1, e2, e3])) + eq_(self.b1.getElement(type=CEnum, name = None), e1) + eq_(set(self.b1.getElements(type=CEnum, name = None)), set([e1, e2])) + eq_(set(self.b1.getElements(name = None)), set([e1, e2, mcl])) + + def testRemoveEnumFromBundle(self): + b1 = CBundle("B1") + b2 = CBundle("B2") + e1 = CEnum("E1", bundles = b1) + try: + b1.remove(None) + exceptionExpected_() + except CException as e: + eq_("'None' is not an element of the bundle", e.value) + try: + b1.remove(CEnum("A")) + exceptionExpected_() + except CException as e: + eq_("'A' is not an element of the bundle", e.value) + try: + b2.remove(e1) + exceptionExpected_() + except CException as e: + eq_("'E1' is not an element of the bundle", e.value) + b1.remove(e1) + eq_(set(b1.getElements(type = CEnum)), set()) + + e1 = CEnum("E1", bundles = b1) + e2 = CEnum("E2", bundles = b1) + e3 = CEnum("E3", values = ["1", "2"], bundles = b1) + + b1.remove(e1) + try: + b1.remove(CEnum("E2", bundles = b2)) + exceptionExpected_() + except CException as e: + eq_("'E2' is not an element of the bundle", e.value) + try: + b1.remove(e1) + exceptionExpected_() + except CException as e: + eq_("'E1' is not an element of the bundle", e.value) + + eq_(set(b1.getElements(type = CEnum)), set([e2, e3])) + b1.remove(e3) + eq_(set(b1.getElements(type = CEnum)), set([e2])) + + eq_(e3.name, "E3") + eq_(e3.bundles, []) + eq_(e3.values, ["1", "2"]) + + def testDeleteEnumFromBundle(self): + b1 = CBundle("B1") + e1 = CEnum("E1", bundles = b1) + e1.delete() + eq_(set(b1.getElements(type = CEnum)), set()) + + e1 = CEnum("E1", bundles = b1) + e2 = CEnum("E2", bundles = b1) + e3 = CEnum("E3", values = ["1", "2"], bundles = b1) + ea1 = CAttribute(type = e3, default = "1") + ea2 = CAttribute(type = e3) + cl = CClass(self.mcl, attributes = {"letters1": ea1, "letters2": ea2}) + o = CObject(cl, "o") + + e1.delete() + eq_(set(b1.getElements(type = CEnum)), set([e2, e3])) + e3.delete() + eq_(set(b1.getElements(type = CEnum)), set([e2])) + + eq_(e3.name, None) + eq_(e3.bundles, []) + eq_(e3.values, []) + eq_(set(cl.attributes), {ea1, ea2}) + try: + ea1.default + exceptionExpected_() + except CException as e: + eq_("cannot access named element that has been deleted", e.value) + try: + ea1.type + exceptionExpected_() + except CException as e: + eq_("cannot access named element that has been deleted", e.value) + try: + ea1.default = "3" + exceptionExpected_() + except CException as e: + eq_("cannot access named element that has been deleted", e.value) + try: + ea1.type = e1 + exceptionExpected_() + except CException as e: + eq_("cannot access named element that has been deleted", e.value) + try: + ea1.type = e2 + exceptionExpected_() + except CException as e: + eq_("default value '1' incompatible with attribute's type 'E2'", e.value) + try: + o.setValue("letters1", "1") + exceptionExpected_() + except CException as e: + eq_("cannot access named element that has been deleted", e.value) + try: + o.getValue("letters1") + exceptionExpected_() + except CException as e: + eq_("cannot access named element that has been deleted", e.value) + + + def testRemoveBundleFromTwoBundles(self): + b1 = CBundle("B1") + b2 = CBundle("B2") + e1 = CEnum("e1", bundles = [b1, b2]) + b1.remove(e1) + eq_(set(b1.getElements(type=CEnum)), set()) + eq_(set(b2.getElements(type=CEnum)), set([e1])) + eq_(set(e1.bundles), set([b2])) + + def testDeleteBundleFromTwoBundles(self): + b1 = CBundle("B1") + b2 = CBundle("B2") + e1 = CEnum("e1", bundles = [b1, b2]) + b1.delete() + eq_(set(b1.getElements(type=CEnum)), set()) + eq_(set(b2.getElements(type=CEnum)), set([e1])) + eq_(set(e1.bundles), set([b2])) + + def testDeleteEnumHavingTwoBundles(self): + b1 = CBundle("B1") + b2 = CBundle("B2") + e1 = CEnum("e1", bundles = [b1, b2]) + e2 = CEnum("e2", bundles = [b2]) + e1.delete() + eq_(set(b1.getElements(type=CEnum)), set()) + eq_(set(b2.getElements(type=CEnum)), set([e2])) + eq_(set(e1.bundles), set()) + eq_(set(e2.bundles), set([b2])) + + def testDeleteEnumThatIsAnAttributeType(self): + b1 = CBundle("B1") + b2 = CBundle("B2") + e1 = CEnum("E1", bundles = b1) + e2 = CEnum("E2", bundles = b1) + e3 = CEnum("E3", values = ["1", "2"], bundles = b1) + ea1 = CAttribute(type = e3, default = "1") + ea2 = CAttribute(type = e3) + cl = CClass(self.mcl, attributes = {"letters1": ea1, "letters2": ea2}) + o = CObject(cl, "o") + e1.delete() + e3.delete() + try: + ea1.default + exceptionExpected_() + except CException as e: + eq_("cannot access named element that has been deleted", e.value) + try: + ea1.type + exceptionExpected_() + except CException as e: + eq_("cannot access named element that has been deleted", e.value) + try: + ea1.default = "3" + exceptionExpected_() + except CException as e: + eq_("cannot access named element that has been deleted", e.value) + try: + ea1.type = e1 + exceptionExpected_() + except CException as e: + eq_("cannot access named element that has been deleted", e.value) + try: + ea1.type = e2 + exceptionExpected_() + except CException as e: + eq_("default value '1' incompatible with attribute's type 'E2'", e.value) + try: + o.setValue("letters1", "1") + exceptionExpected_() + except CException as e: + eq_("cannot access named element that has been deleted", e.value) + try: + o.getValue("letters1") + exceptionExpected_() + except CException as e: + eq_("cannot access named element that has been deleted", e.value) + + def testBundleThatIsDeleted(self): + b1 = CBundle("B1") + b1.delete() + try: + CEnum("E1", bundles = b1) + exceptionExpected_() + except CException as e: + eq_(e.value, "cannot access named element that has been deleted") + + + def testSetBundleToNone(self): + c = CEnum("E1", bundles = None) + eq_(c.bundles, []) + eq_(c.name, "E1") + +if __name__ == "__main__": + nose.main() + + + diff --git a/tests/test_bundles_of_metaclasses.py b/tests/test_bundles_of_metaclasses.py new file mode 100644 index 0000000..a2b82a3 --- /dev/null +++ b/tests/test_bundles_of_metaclasses.py @@ -0,0 +1,298 @@ +import sys +sys.path.append("..") + +import re + +import nose +from nose.tools import ok_, eq_ +from testCommons import neq_, exceptionExpected_ +from parameterized import parameterized + +from codeableModels import CBundle, CMetaclass, CClass, CObject, CAttribute, CException, CEnum + +class TestBundlesOfMetaclasses(): + def setUp(self): + self.b1 = CBundle("B1") + self.b2 = CBundle("B2") + + def testMetaclassNameFail(self): + try: + CMetaclass(self.b1) + exceptionExpected_() + except CException as e: + ok_(e.value.startswith("is not a name string: '")) + ok_(e.value.endswith(" B1'")) + + def testMetaclassDefinedBundles(self): + eq_(set(self.b1.getElements()), set()) + m1 = CMetaclass("M1", bundles = self.b1) + eq_(set(self.b1.getElements()), set([m1])) + m2 = CMetaclass("M2", bundles = [self.b1]) + m3 = CMetaclass("M3", bundles = [self.b1, self.b2]) + cl = CClass(m1, "C", bundles = [self.b1, self.b2]) + eq_(set(self.b1.getElements(type=CMetaclass)), set([m1, m2, m3])) + eq_(set(self.b1.elements), set([m1, m2, m3, cl])) + eq_(set(self.b2.getElements(type = CMetaclass)), set([m3])) + eq_(set(self.b2.elements), set([m3, cl])) + + + def testBundleDefinedMetaclasses(self): + m1 = CMetaclass("M1") + m2 = CMetaclass("M2") + m3 = CMetaclass("M3") + eq_(set(self.b1.getElements(type=CMetaclass)), set()) + b1 = CBundle("B1", elements = [m1, m2, m3]) + eq_(set(b1.elements), set([m1, m2, m3])) + cl = CClass(m1, "C", bundles = b1) + eq_(set(b1.elements), set([m1, m2, m3, cl])) + eq_(set(b1.getElements(type=CMetaclass)), set([m1, m2, m3])) + b2 = CBundle("B2") + b2.elements = [m2, m3] + eq_(set(b2.getElements(type=CMetaclass)), set([m2, m3])) + eq_(set(m1.bundles), set([b1])) + eq_(set(m2.bundles), set([b1, b2])) + eq_(set(m3.bundles), set([b1, b2])) + + def testGetMetaclassesByName(self): + eq_(set(self.b1.getElements(type=CMetaclass, name = "m1")), set()) + m1 = CMetaclass("M1", bundles = self.b1) + c1 = CClass(m1, "C1", bundles = self.b1) + eq_(self.b1.getElements(type=CClass), [c1]) + eq_(set(self.b1.getElements(type=CMetaclass, name = "M1")), set([m1])) + m2 = CMetaclass("M1", bundles = self.b1) + eq_(set(self.b1.getElements(type=CMetaclass, name = "M1")), set([m1, m2])) + ok_(m1 != m2) + m3 = CMetaclass("M1", bundles = self.b1) + eq_(set(self.b1.getElements(type=CMetaclass, name = "M1")), set([m1, m2, m3])) + eq_(self.b1.getElement(type=CMetaclass, name = "M1"), m1) + + def testGetMetaclassElementsByName(self): + eq_(set(self.b1.getElements(name = "M1")), set()) + m1 = CMetaclass("M1", bundles = self.b1) + eq_(set(self.b1.getElements(name = "M1")), set([m1])) + c1 = CClass(m1, "M1", bundles = self.b1) + eq_(set(self.b1.getElements(name = "M1")), set([m1, c1])) + m2 = CMetaclass("M1", bundles = self.b1) + eq_(set(self.b1.getElements(name = "M1")), set([m1, c1, m2])) + ok_(m1 != m2) + m3 = CMetaclass("M1", bundles = self.b1) + eq_(set(self.b1.getElements(name = "M1")), set([m1, c1, m2, m3])) + eq_(self.b1.getElement(name = "M1"), m1) + + def testMetaclassDefinedBundleChange(self): + m1 = CMetaclass("M1", bundles = self.b1) + m2 = CMetaclass("M2", bundles = self.b1) + m3 = CMetaclass("M3", bundles = self.b1) + cl1 = CClass(m1, "C1", bundles = self.b1) + cl2 = CClass(m1, "C2", bundles = self.b1) + b = CBundle() + m2.bundles = b + m3.bundles = None + cl2.bundles = b + eq_(set(self.b1.elements), set([cl1, m1])) + eq_(set(self.b1.getElements(type=CMetaclass)), set([m1])) + eq_(set(b.elements), set([m2, cl2])) + eq_(set(b.getElements(type=CMetaclass)), set([m2])) + eq_(m1.bundles, [self.b1]) + eq_(m2.bundles, [b]) + eq_(m3.bundles, []) + + def testBundleDeleteMetaclass(self): + m1 = CMetaclass("M1", bundles = self.b1) + c = CClass(m1) + eq_(m1.classes, [c]) + m2 = CMetaclass("M2", bundles = self.b1) + m3 = CMetaclass("M3", bundles = self.b1) + self.b1.delete() + eq_(set(self.b1.elements), set()) + eq_(m1.bundles, []) + eq_(m1.classes, [c]) + eq_(m2.bundles, []) + eq_(m3.bundles, []) + + def testCreationOfUnnamedMetaclassInBundle(self): + m1 = CMetaclass() + m2 = CMetaclass() + m3 = CMetaclass("x") + cl = CClass(m1) + self.b1.elements = [m1, m2, m3, cl] + eq_(set(self.b1.getElements(type = CMetaclass)), set([m1, m2, m3])) + eq_(self.b1.getElement(name = None), m1) + eq_(set(self.b1.getElements(type = CMetaclass, name = None)), set([m1, m2])) + eq_(set(self.b1.getElements(name = None)), set([m1, m2, cl])) + + def testRemoveMetaclassFromBundle(self): + b1 = CBundle("B1") + b2 = CBundle("B2") + m1 = CMetaclass("M1", bundles = b1) + try: + b1.remove(None) + exceptionExpected_() + except CException as e: + eq_("'None' is not an element of the bundle", e.value) + try: + b1.remove(CEnum("A")) + exceptionExpected_() + except CException as e: + eq_("'A' is not an element of the bundle", e.value) + try: + b2.remove(m1) + exceptionExpected_() + except CException as e: + eq_("'M1' is not an element of the bundle", e.value) + b1.remove(m1) + eq_(set(b1.getElements(type=CMetaclass)), set()) + + m1 = CMetaclass("M1", bundles = b1) + m2 = CMetaclass("M1", bundles = b1) + m3 = CMetaclass("M1", superclasses = m2, attributes = {"i" : 1}, bundles = b1) + c = CClass(m3, bundles = b1) + + b1.remove(m1) + try: + b1.remove(CMetaclass("M2", bundles = b2)) + exceptionExpected_() + except CException as e: + eq_("'M2' is not an element of the bundle", e.value) + try: + b1.remove(m1) + exceptionExpected_() + except CException as e: + eq_("'M1' is not an element of the bundle", e.value) + + eq_(set(b1.getElements(type=CMetaclass)), set([m2, m3])) + b1.remove(m3) + eq_(set(b1.getElements(type=CMetaclass)), set([m2])) + + eq_(m3.superclasses, [m2]) + eq_(m2.subclasses, [m3]) + eq_(m3.attributeNames, ["i"]) + eq_(m3.classes, [c]) + eq_(m3.name, "M1") + eq_(m3.bundles, []) + eq_(b1.getElements(type=CClass), [c]) + + def testDeleteMetaclassFromBundle(self): + b1 = CBundle("B1") + b2 = CBundle("B2") + m1 = CMetaclass("M1", bundles = b1) + m1.delete() + eq_(set(b1.getElements(type=CMetaclass)), set()) + + m1 = CMetaclass("M1", bundles = b1) + m2 = CMetaclass("M1", bundles = b1) + m3 = CMetaclass("M1", superclasses = m2, attributes = {"i" : 1}, bundles = b1) + CClass(m3, bundles = b1) + m1.delete() + eq_(set(b1.getElements(type=CMetaclass)), set([m2, m3])) + m3.delete() + eq_(set(b1.getElements(type=CMetaclass)), set([m2])) + + eq_(m3.superclasses, []) + eq_(m2.subclasses, []) + eq_(m3.attributes, []) + eq_(m3.attributeNames, []) + eq_(m3.classes, []) + eq_(m3.name, None) + eq_(m3.bundles, []) + eq_(b1.getElements(type=CClass), []) + + + def testRemoveBundleFromTwoBundles(self): + b1 = CBundle("B1") + b2 = CBundle("B2") + m1 = CMetaclass("m1", bundles = [b1, b2]) + b1.remove(m1) + eq_(set(b1.getElements(type=CMetaclass)), set()) + eq_(set(b2.getElements(type=CMetaclass)), set([m1])) + eq_(set(m1.bundles), set([b2])) + + def testDeleteBundleFromTwoBundles(self): + b1 = CBundle("B1") + b2 = CBundle("B2") + m1 = CMetaclass("m1", bundles = [b1, b2]) + b1.delete() + eq_(set(b1.getElements(type=CMetaclass)), set()) + eq_(set(b2.getElements(type=CMetaclass)), set([m1])) + eq_(set(m1.bundles), set([b2])) + + def testDeleteMetaclassHavingTwoBundles(self): + b1 = CBundle("B1") + b2 = CBundle("B2") + m1 = CMetaclass("m1", bundles = [b1, b2]) + m2 = CMetaclass("m2", bundles = [b2]) + m1.delete() + eq_(set(b1.getElements(type=CMetaclass)), set()) + eq_(set(b2.getElements(type=CMetaclass)), set([m2])) + eq_(set(m1.bundles), set()) + eq_(set(m2.bundles), set([b2])) + + def testDeleteClassThatIsAnAttributeType(self): + b1 = CBundle("B1") + mcl = CMetaclass("MCL") + cl1 = CClass(mcl, "CL1", bundles = b1) + cl2 = CClass(mcl, "CL2", bundles = b1) + cl3 = CClass(mcl, "CL3", bundles = b1) + o3 = CObject(cl3, "O3") + + ea1 = CAttribute(type = cl3, default = o3) + m = CMetaclass("M", bundles = b1, attributes = {"o" : ea1}) + c = CClass(m) + cl1.delete() + cl3.delete() + try: + ea1.default + exceptionExpected_() + except CException as e: + eq_("cannot access named element that has been deleted", e.value) + try: + ea1.type + exceptionExpected_() + except CException as e: + eq_("cannot access named element that has been deleted", e.value) + try: + ea1.default = "3" + exceptionExpected_() + except CException as e: + eq_("cannot access named element that has been deleted", e.value) + try: + ea1.type = cl1 + exceptionExpected_() + except CException as e: + eq_("cannot access named element that has been deleted", e.value) + try: + ea1.type = cl2 + exceptionExpected_() + except CException as e: + eq_("default value '' incompatible with attribute's type 'CL2'", e.value) + try: + c.setValue("o", CObject(cl2)) + exceptionExpected_() + except CException as e: + eq_("cannot access named element that has been deleted", e.value) + try: + c.getValue("o") + exceptionExpected_() + except CException as e: + eq_("cannot access named element that has been deleted", e.value) + + def testBundleThatIsDeleted(self): + b1 = CBundle("B1") + b1.delete() + try: + CMetaclass("M", bundles = b1) + exceptionExpected_() + except CException as e: + eq_(e.value, "cannot access named element that has been deleted") + + + def testSetBundleToNone(self): + c = CMetaclass("M", bundles = None) + eq_(c.bundles, []) + eq_(c.name, "M") + +if __name__ == "__main__": + nose.main() + + + diff --git a/tests/test_bundles_of_objects.py b/tests/test_bundles_of_objects.py new file mode 100644 index 0000000..9e9115c --- /dev/null +++ b/tests/test_bundles_of_objects.py @@ -0,0 +1,249 @@ +import sys +sys.path.append("..") + +import re + +import nose +from nose.tools import ok_, eq_ +from testCommons import neq_, exceptionExpected_ +from parameterized import parameterized + +from codeableModels import CBundle, CMetaclass, CClass, CObject, CAttribute, CException, CEnum + +class TestBundlesOfClasses(): + def setUp(self): + self.mcl = CMetaclass("MCL") + self.cl = CClass(self.mcl, "C", attributes = {"i" : 1}) + self.b1 = CBundle("B1") + self.b2 = CBundle("B2") + + def testObjectNameFail(self): + try: + CObject(self.cl, self.b1) + exceptionExpected_() + except CException as e: + ok_(e.value.startswith("is not a name string: '")) + ok_(e.value.endswith(" B1'")) + + + def testObjectDefinedBundles(self): + eq_(set(self.b1.getElements(type=CObject)), set()) + o1 = CObject(self.cl, "O1", bundles = self.b1) + eq_(set(self.b1.getElements(type=CObject)), set([o1])) + o2 = CObject(self.cl, "O2", bundles = [self.b1]) + o3 = CObject(self.cl, "O3", bundles = [self.b1, self.b2]) + mcl = CMetaclass("MCL", bundles = self.b1) + eq_(set(self.b1.getElements(type=CObject)), set([o1, o2, o3])) + eq_(set(self.b1.elements), set([o1, o2, o3, mcl])) + eq_(set(self.b2.getElements(type = CObject)), set([o3])) + eq_(set(self.b2.elements), set([o3])) + + def testBundleDefinedObjects(self): + o1 = CObject(self.cl, "O1") + o2 = CObject(self.cl, "O2") + o3 = CObject(self.cl, "O3") + eq_(set(self.b1.getElements(type=CObject)), set()) + b1 = CBundle("B1", elements = [o1, o2, o3]) + eq_(set(b1.elements), set([o1, o2, o3])) + self.mcl.bundles = b1 + eq_(set(b1.elements), set([o1, o2, o3, self.mcl])) + eq_(set(b1.getElements(type=CObject)), set([o1, o2, o3])) + b2 = CBundle("B2") + b2.elements = [o2, o3] + eq_(set(b2.getElements(type=CObject)), set([o2, o3])) + eq_(set(o1.bundles), set([b1])) + eq_(set(o2.bundles), set([b1, b2])) + eq_(set(o3.bundles), set([b1, b2])) + + + def testGetObjectsByName(self): + eq_(set(self.b1.getElements(type=CObject, name = "O1")), set()) + o1 = CObject(self.cl, "O1", bundles = self.b1) + m = CMetaclass("O1", bundles = self.b1) + eq_(self.b1.getElements(type=CMetaclass), [m]) + eq_(set(self.b1.getElements(type=CObject, name = "O1")), set([o1])) + o2 = CObject(self.cl, "O1", bundles = self.b1) + eq_(set(self.b1.getElements(type=CObject, name = "O1")), set([o1, o2])) + ok_(o1 != o2) + o3 = CObject(self.cl, "O1", bundles = self.b1) + eq_(set(self.b1.getElements(type=CObject, name = "O1")), set([o1, o2, o3])) + eq_(self.b1.getElement(type=CObject, name = "O1"), o1) + + def testGetObjectElementsByName(self): + eq_(set(self.b1.getElements(type=CObject, name = "O1")), set()) + o1 = CObject(self.cl, "O1", bundles = self.b1) + eq_(set(self.b1.getElements(type=CObject, name = "O1")), set([o1])) + m = CMetaclass("O1", bundles = self.b1) + eq_(set(self.b1.getElements(name = "O1")), set([m, o1])) + o2 = CObject(self.cl, "O1", bundles = self.b1) + eq_(set(self.b1.getElements(name = "O1")), set([m, o1, o2])) + ok_(o1 != o2) + o3 = CObject(self.cl, "O1", bundles = self.b1) + eq_(set(self.b1.getElements(name = "O1")), set([m, o1, o2, o3])) + eq_(self.b1.getElement(type=CObject, name = "O1"), o1) + + def testObjectDefinedBundleChange(self): + o1 = CObject(self.cl, "O1", bundles = self.b1) + o2 = CObject(self.cl, "O2", bundles = self.b1) + o3 = CObject(self.cl, "O3", bundles = self.b1) + mcl = CMetaclass("MCL", bundles = self.b1) + b = CBundle() + o2.bundles = b + o3.bundles = None + self.mcl.bundles = b + eq_(set(self.b1.elements), set([mcl, o1])) + eq_(set(self.b1.getElements(type=CObject)), set([o1])) + eq_(set(b.elements), set([o2, self.mcl])) + eq_(set(b.getElements(type=CObject)), set([o2])) + eq_(o1.bundles, [self.b1]) + eq_(o2.bundles, [b]) + eq_(o3.bundles, []) + + def testBundleDeleteObject(self): + o1 = CObject(self.cl, "O1", bundles = self.b1) + o2 = CObject(self.cl, "O2", bundles = self.b1) + o3 = CObject(self.cl, "O3", bundles = self.b1) + self.b1.delete() + eq_(set(self.b1.elements), set()) + eq_(o1.bundles, []) + eq_(o1.classifier, self.cl) + eq_(o2.bundles, []) + eq_(o3.bundles, []) + + def testCreationOfUnnamedObjectInBundle(self): + o1 = CObject(self.cl) + o2 = CObject(self.cl) + o3 = CObject(self.cl, "x") + mcl = CMetaclass() + self.b1.elements = [o1, o2, o3, mcl] + eq_(set(self.b1.getElements(type=CObject)), set([o1, o2, o3])) + eq_(self.b1.getElement(type=CObject, name = None), o1) + eq_(set(self.b1.getElements(type=CObject, name = None)), set([o1, o2])) + eq_(self.b1.getElement(name = None), o1) + eq_(set(self.b1.getElements(name = None)), set([o1, o2, mcl])) + + def testRemoveObjectFromBundle(self): + b1 = CBundle("B1") + b2 = CBundle("B2") + o = CObject(self.cl, "O", bundles = b1) + try: + b1.remove(None) + exceptionExpected_() + except CException as e: + eq_("'None' is not an element of the bundle", e.value) + try: + b1.remove(CEnum("A")) + exceptionExpected_() + except CException as e: + eq_("'A' is not an element of the bundle", e.value) + try: + b2.remove(o) + exceptionExpected_() + except CException as e: + eq_("'O' is not an element of the bundle", e.value) + b1.remove(o) + eq_(set(b1.getElements(type=CObject)), set()) + + o1 = CObject(self.cl, "O1", bundles = b1) + o2 = CObject(self.cl, "O2", bundles = b1) + o3 = CObject(self.cl, "O3", bundles = b1) + o3.setValue("i", 7) + + b1.remove(o1) + try: + b1.remove(CObject(CClass(self.mcl), "Obj2", bundles = b2)) + exceptionExpected_() + except CException as e: + eq_("'Obj2' is not an element of the bundle", e.value) + try: + b1.remove(o1) + exceptionExpected_() + except CException as e: + eq_("'O1' is not an element of the bundle", e.value) + + eq_(set(b1.getElements(type=CObject)), set([o2, o3])) + b1.remove(o3) + eq_(b1.getElements(type=CObject), [o2]) + + eq_(o3.classifier, self.cl) + eq_(set(self.cl.objects), set([o, o1, o2, o3])) + eq_(o3.getValue("i"), 7) + eq_(o3.name, "O3") + eq_(o3.bundles, []) + + def testDeleteObjectFromBundle(self): + b1 = CBundle("B1") + o = CObject(self.cl, "O1", bundles = b1) + o.delete() + eq_(set(b1.getElements(type=CObject)), set()) + + o1 = CObject(self.cl, "O1", bundles = b1) + o2 = CObject(self.cl, "O2", bundles = b1) + o3 = CObject(self.cl, "O3", bundles = b1) + o3.setValue("i", 7) + + o1.delete() + eq_(set(b1.getElements(type=CObject)), set([o2, o3])) + o3.delete() + eq_(set(b1.getElements(type=CObject)), set([o2])) + + eq_(o3.classifier, None) + try: + o3.getValue("i") + exceptionExpected_() + except CException as e: + eq_("can't get value 'i' on deleted object", e.value) + eq_(o3.name, None) + eq_(o3.bundles, []) + + + def testRemoveBundleFromTwoBundles(self): + b1 = CBundle("B1") + b2 = CBundle("B2") + o1 = CObject(self.cl, "o", bundles = [b1, b2]) + b1.remove(o1) + eq_(set(b1.getElements(type=CObject)), set()) + eq_(set(b2.getElements(type=CObject)), set([o1])) + eq_(set(o1.bundles), set([b2])) + + def testDeleteBundleFromTwoBundles(self): + b1 = CBundle("B1") + b2 = CBundle("B2") + o1 = CObject(self.cl, "o1", bundles = [b1, b2]) + b1.delete() + eq_(set(b1.getElements(type=CObject)), set()) + eq_(set(b2.getElements(type=CObject)), set([o1])) + eq_(set(o1.bundles), set([b2])) + + def testDeleteObjectHavingTwoBundles(self): + b1 = CBundle("B1") + b2 = CBundle("B2") + o1 = CObject(self.cl, "o1", bundles = [b1, b2]) + o2 = CObject(self.cl, "o2", bundles = [b2]) + o1.delete() + eq_(set(b1.getElements(type=CObject)), set()) + eq_(set(b2.getElements(type=CObject)), set([o2])) + eq_(set(o1.bundles), set()) + eq_(set(o2.bundles), set([b2])) + + + def testBundleThatIsDeleted(self): + b1 = CBundle("B1") + b1.delete() + try: + CObject(self.cl, "O1", bundles = b1) + exceptionExpected_() + except CException as e: + eq_(e.value, "cannot access named element that has been deleted") + + + def testSetBundleToNone(self): + o = CObject(self.cl, "O1", bundles = None) + eq_(o.bundles, []) + eq_(o.name, "O1") + +if __name__ == "__main__": + nose.main() + + + diff --git a/tests/test_bundles_of_stereotypes.py b/tests/test_bundles_of_stereotypes.py new file mode 100644 index 0000000..734146b --- /dev/null +++ b/tests/test_bundles_of_stereotypes.py @@ -0,0 +1,343 @@ +import sys +sys.path.append("..") + +import re + +import nose +from nose.tools import ok_, eq_ +from testCommons import neq_, exceptionExpected_ +from parameterized import parameterized + +from codeableModels import CBundle, CStereotype, CMetaclass, CClass, CObject, CAttribute, CException, CEnum + +class TestBundlesOfStereotypes(): + def setUp(self): + self.mcl = CMetaclass("MCL") + self.b1 = CBundle("B1") + self.b2 = CBundle("B2") + self.m1 = CMetaclass("M1") + self.m2 = CMetaclass("M2") + self.a = self.m1.association(self.m2, name = "A", multiplicity = "1", roleName = "m1", + sourceMultiplicity = "*", sourceRoleName = "m2") + + + def testStereotypeNameFail(self): + try: + CStereotype(self.b1) + exceptionExpected_() + except CException as e: + ok_(e.value.startswith("is not a name string: '")) + ok_(e.value.endswith(" B1'")) + + def testStereotypeDefinedBundles(self): + eq_(set(self.b1.getElements(type=CStereotype)), set()) + s1 = CStereotype("s1", bundles = self.b1) + eq_(set(self.b1.getElements(type=CStereotype)), set([s1])) + s2 = CStereotype("s2", bundles = [self.b1]) + s3 = CStereotype("s3", bundles = [self.b1, self.b2]) + cl = CClass(self.mcl, "C", bundles = self.b1) + eq_(set(self.b1.getElements(type=CStereotype)), set([s1, s2, s3])) + eq_(set(self.b1.elements), set([s1, s2, s3, cl])) + eq_(set(self.b2.getElements(type = CStereotype)), set([s3])) + eq_(set(self.b2.elements), set([s3])) + + def testBundleDefinedStereotype(self): + s1 = CStereotype("s1") + s2 = CStereotype("s2") + s3 = CStereotype("s3") + eq_(set(self.b1.getElements(type=CStereotype)), set()) + b1 = CBundle("B1", elements = [s1, s2, s3]) + eq_(set(b1.elements), set([s1, s2, s3])) + cl = CClass(self.mcl, "C", bundles = b1) + eq_(set(b1.elements), set([s1, s2, s3, cl])) + eq_(set(b1.getElements(type=CStereotype)), set([s1, s2, s3])) + b2 = CBundle("B2") + b2.elements = [s2, s3] + eq_(set(b2.getElements(type=CStereotype)), set([s2, s3])) + eq_(set(s1.bundles), set([b1])) + eq_(set(s2.bundles), set([b1, b2])) + eq_(set(s3.bundles), set([b1, b2])) + + + def testGetStereotypesByName(self): + eq_(set(self.b1.getElements(type=CStereotype, name = "s1")), set()) + s1 = CStereotype("s1", bundles = self.b1) + c1 = CClass(self.mcl, "C1", bundles = self.b1) + eq_(self.b1.getElements(type=CClass), [c1]) + eq_(set(self.b1.getElements(type=CStereotype, name = "s1")), set([s1])) + s2 = CStereotype("s1", bundles = self.b1) + eq_(set(self.b1.getElements(type=CStereotype, name = "s1")), set([s1, s2])) + ok_(s1 != s2) + s3 = CStereotype("s1", bundles = self.b1) + eq_(set(self.b1.getElements(type=CStereotype, name = "s1")), set([s1, s2, s3])) + eq_(self.b1.getElement(type=CStereotype, name = "s1"), s1) + + def testGetStereotypeElementsByName(self): + eq_(set(self.b1.getElements(name = "s1")), set()) + s1 = CStereotype("s1", bundles = self.b1) + eq_(set(self.b1.getElements(name = "s1")), set([s1])) + c1 = CClass(self.mcl, "s1", bundles = self.b1) + eq_(set(self.b1.getElements(name = "s1")), set([s1, c1])) + s2 = CStereotype("s1", bundles = self.b1) + eq_(set(self.b1.getElements(name = "s1")), set([s1, c1, s2])) + ok_(s1 != s2) + s3 = CStereotype("s1", bundles = self.b1) + eq_(set(self.b1.getElements(name = "s1")), set([s1, c1, s2, s3])) + eq_(self.b1.getElement(name = "s1"), s1) + + def testStereotypeDefinedBundleChange(self): + s1 = CStereotype("s1", bundles = self.b1) + s2 = CStereotype("s2", bundles = self.b1) + s3 = CStereotype("s3", bundles = self.b1) + cl1 = CClass(self.mcl, "C1", bundles = self.b1) + cl2 = CClass(self.mcl, "C2", bundles = self.b1) + b = CBundle() + s2.bundles = b + s3.bundles = None + cl2.bundles = b + eq_(set(self.b1.elements), set([cl1, s1])) + eq_(set(self.b1.getElements(type=CStereotype)), set([s1])) + eq_(set(b.elements), set([s2, cl2])) + eq_(set(b.getElements(type=CStereotype)), set([s2])) + eq_(s1.bundles, [self.b1]) + eq_(s2.bundles, [b]) + eq_(s3.bundles, []) + + def testBundleDeleteStereotypeMetaclass(self): + s1 = CStereotype("s1", bundles = self.b1, extended = self.mcl) + eq_(s1.extended, [self.mcl]) + s2 = CStereotype("s2", bundles = self.b1) + s3 = CStereotype("s3", bundles = self.b1) + self.b1.delete() + eq_(set(self.b1.elements), set()) + eq_(s1.bundles, []) + eq_(s1.extended, [self.mcl]) + eq_(s2.bundles, []) + eq_(s3.bundles, []) + + def testBundleDeleteStereotypeAssociation(self): + s1 = CStereotype("s1", bundles = self.b1, extended = self.a) + eq_(s1.extended, [self.a]) + s2 = CStereotype("s2", bundles = self.b1) + s3 = CStereotype("s3", bundles = self.b1) + self.b1.delete() + eq_(set(self.b1.elements), set()) + eq_(s1.bundles, []) + eq_(s1.extended, [self.a]) + eq_(s2.bundles, []) + eq_(s3.bundles, []) + + def testCreationOfUnnamedStereotypeInBundle(self): + cl = CClass(self.mcl) + s1 = CStereotype() + s2 = CStereotype() + s3 = CStereotype("x") + self.b1.elements = [s1, s2, s3, cl] + eq_(set(self.b1.getElements(type=CStereotype)), set([s1, s2, s3])) + eq_(self.b1.getElement(name = None), s1) + eq_(set(self.b1.getElements(type = CStereotype, name = None)), set([s1, s2])) + eq_(self.b1.getElement(name = None), s1) + eq_(set(self.b1.getElements(name = None)), set([s1, s2, cl])) + + def testRemoveStereotypeFromBundle(self): + b1 = CBundle("B1") + b2 = CBundle("B2") + s1 = CStereotype("s1", bundles = b1) + try: + b1.remove(None) + exceptionExpected_() + except CException as e: + eq_("'None' is not an element of the bundle", e.value) + try: + b1.remove(CEnum("A")) + exceptionExpected_() + except CException as e: + eq_("'A' is not an element of the bundle", e.value) + try: + b2.remove(s1) + exceptionExpected_() + except CException as e: + eq_("'s1' is not an element of the bundle", e.value) + b1.remove(s1) + eq_(set(b1.getElements(type=CStereotype)), set()) + + s1 = CStereotype("s1", bundles = b1) + s2 = CStereotype("s1", bundles = b1) + s3 = CStereotype("s1", superclasses = s2, attributes = {"i" : 1}, bundles = b1, extended = self.mcl) + s4 = CStereotype("s1", superclasses = s2, attributes = {"i" : 1}, bundles = b1, extended = self.a) + + b1.remove(s1) + try: + b1.remove(CStereotype("s2", bundles = b2)) + exceptionExpected_() + except CException as e: + eq_("'s2' is not an element of the bundle", e.value) + try: + b1.remove(s1) + exceptionExpected_() + except CException as e: + eq_("'s1' is not an element of the bundle", e.value) + + eq_(set(b1.getElements(type=CStereotype)), set([s2, s3, s4])) + b1.remove(s3) + b1.remove(s4) + eq_(set(b1.getElements(type=CStereotype)), set([s2])) + + eq_(s3.superclasses, [s2]) + eq_(s2.subclasses, [s3, s4]) + eq_(s3.attributeNames, ["i"]) + eq_(s3.extended, [self.mcl]) + eq_(s3.name, "s1") + eq_(s3.bundles, []) + eq_(s4.superclasses, [s2]) + eq_(s4.attributeNames, ["i"]) + eq_(s4.extended, [self.a]) + eq_(s4.name, "s1") + eq_(s4.bundles, []) + + + def testDeleteStereotypeFromBundle(self): + b1 = CBundle("B1") + s1 = CStereotype("s1", bundles = b1) + s1.delete() + eq_(set(b1.getElements(type=CStereotype)), set()) + + s1 = CStereotype("s1", bundles = b1) + s2 = CStereotype("s1", bundles = b1) + s3 = CStereotype("s1", superclasses = s2, attributes = {"i" : 1}, bundles = b1, extended = self.mcl) + s4 = CStereotype("s1", superclasses = s2, attributes = {"i" : 1}, bundles = b1, extended = self.a) + + s1.delete() + eq_(set(b1.getElements(type=CStereotype)), set([s2, s3, s4])) + s3.delete() + s4.delete() + eq_(set(b1.getElements(type=CStereotype)), set([s2])) + + eq_(s3.superclasses, []) + eq_(s2.subclasses, []) + eq_(s3.attributes, []) + eq_(s3.attributeNames, []) + eq_(s3.extended, []) + eq_(s3.name, None) + eq_(s3.bundles, []) + + eq_(s4.superclasses, []) + eq_(s4.attributes, []) + eq_(s4.attributeNames, []) + eq_(s4.extended, []) + eq_(s4.name, None) + eq_(s4.bundles, []) + + def testRemoveBundleFromTwoBundles(self): + b1 = CBundle("B1") + b2 = CBundle("B2") + s1 = CStereotype("s1", bundles = [b1, b2]) + b1.remove(s1) + eq_(set(b1.getElements(type=CStereotype)), set()) + eq_(set(b2.getElements(type=CStereotype)), set([s1])) + eq_(set(s1.bundles), set([b2])) + + def testDeleteBundleFromTwoBundles(self): + b1 = CBundle("B1") + b2 = CBundle("B2") + s1 = CStereotype("s1", bundles = [b1, b2]) + b1.delete() + eq_(set(b1.getElements(type=CStereotype)), set()) + eq_(set(b2.getElements(type=CStereotype)), set([s1])) + eq_(set(s1.bundles), set([b2])) + + def testDeleteStereotypeHavingTwoBundles(self): + b1 = CBundle("B1") + b2 = CBundle("B2") + s1 = CStereotype("s1", bundles = [b1, b2]) + s2 = CStereotype("s2", bundles = [b2]) + s1.delete() + eq_(set(b1.getElements(type=CStereotype)), set()) + eq_(set(b2.getElements(type=CStereotype)), set([s2])) + eq_(set(s1.bundles), set()) + eq_(set(s2.bundles), set([b2])) + + + + def testStereotypeRemoveSterotypeOrMetaclass(self): + mcl = CMetaclass("MCL1") + s1 = CStereotype("S1", extended = [mcl]) + s2 = CStereotype("S2", extended = [mcl]) + s3 = CStereotype("S3", extended = [mcl]) + s4 = CStereotype("S4", extended = [mcl]) + self.b1.elements = [mcl, s1, s2, s3, s4] + eq_(set(self.b1.getElements(type=CStereotype)), set([s1, s2, s3, s4])) + s2.delete() + eq_(set(self.b1.getElements(type=CStereotype)), set([s1, s3, s4])) + eq_(set(s2.extended), set()) + eq_(set(s1.extended), set([mcl])) + mcl.delete() + eq_(set(mcl.stereotypes), set()) + eq_(set(s1.extended), set()) + eq_(set(self.b1.getElements(type=CStereotype)), set([s1, s3, s4])) + + def testDoubleAssignmentStereotypeExtensionMetaclass(self): + try: + CStereotype("S1", bundles = self.b1, extended = [self.mcl, self.mcl]) + exceptionExpected_() + except CException as e: + eq_("'MCL' is already extended by stereotype 'S1'", e.value) + s1 = self.b1.getElement(type=CStereotype, name = "S1") + eq_(s1.name, "S1") + eq_(set(self.b1.getElements(type=CStereotype)), set([s1])) + eq_(s1.bundles, [self.b1]) + eq_(self.mcl.stereotypes, [s1]) + + def testDoubleAssignmentStereotypeExtensionAssociation(self): + try: + CStereotype("S1", bundles = self.b1, extended = [self.a, self.a]) + exceptionExpected_() + except CException as e: + eq_("'A' is already extended by stereotype 'S1'", e.value) + s1 = self.b1.getElement(type=CStereotype, name = "S1") + eq_(s1.name, "S1") + eq_(set(self.b1.getElements(type=CStereotype)), set([s1])) + eq_(s1.bundles, [self.b1]) + eq_(self.a.stereotypes, [s1]) + + def testDoubleAssignmentMetaclassStereotype(self): + try: + s1 = CStereotype("S1", bundles = self.b1) + self.mcl.stereotypes = [s1, s1] + exceptionExpected_() + except CException as e: + eq_("'S1' is already a stereotype of 'MCL'", e.value) + s1 = self.b1.getElement(type=CStereotype, name = "S1") + eq_(s1.name, "S1") + eq_(set(self.b1.getElements(type=CStereotype)), set([s1])) + + def testDoubleAssignmentAssociationStereotype(self): + try: + s1 = CStereotype("S1", bundles = self.b1) + self.a.stereotypes = [s1, s1] + exceptionExpected_() + except CException as e: + eq_("'S1' is already a stereotype of 'A'", e.value) + s1 = self.b1.getElement(type=CStereotype, name = "S1") + eq_(s1.name, "S1") + eq_(set(self.b1.getElements(type=CStereotype)), set([s1])) + + def testBundleThatIsDeleted(self): + b1 = CBundle("B1") + b1.delete() + try: + CStereotype("S1", bundles = b1) + exceptionExpected_() + except CException as e: + eq_(e.value, "cannot access named element that has been deleted") + + def testSetBundleToNone(self): + s = CStereotype("S1", bundles = None) + eq_(s.bundles, []) + eq_(s.name, "S1") + +if __name__ == "__main__": + nose.main() + + + diff --git a/tests/test_class.py b/tests/test_class.py new file mode 100644 index 0000000..fe69d23 --- /dev/null +++ b/tests/test_class.py @@ -0,0 +1,253 @@ +import sys +sys.path.append("..") + +import re + +import nose +from nose.tools import ok_, eq_ +from testCommons import neq_, exceptionExpected_ +from parameterized import parameterized + +from codeableModels import CMetaclass, CClass, CObject, CAttribute, CException, CEnum, CBundle + +class TestClass(): + def setUp(self): + self.mcl = CMetaclass("MCL", attributes = {"i" : 1}) + + def testCreationOfOneClass(self): + eq_(self.mcl.classes, []) + cl = CClass(self.mcl, "CL") + cl2 = self.mcl.classes[0] + eq_(cl2.name, "CL") + eq_(cl, cl2) + eq_(cl2.metaclass, self.mcl) + + def testCreateClassWrongArgTypes(self): + try: + CClass("MCL", "CL") + exceptionExpected_() + except CException as e: + eq_("'MCL' is not a metaclass", e.value) + try: + cl = CClass(self.mcl, "TC") + CClass(cl, "CL") + exceptionExpected_() + except CException as e: + eq_("'TC' is not a metaclass", e.value) + + def testCreationOf3Classes(self): + c1 = CClass(self.mcl, "CL1") + c2 = CClass(self.mcl, "CL2") + c3 = CClass(self.mcl, "CL3") + eq_(set(self.mcl.classes), set([c1, c2, c3])) + + def testCreationOfUnnamedClass(self): + c1 = CClass(self.mcl) + c2 = CClass(self.mcl) + c3 = CClass(self.mcl, "x") + eq_(set(self.mcl.classes), set([c1, c2, c3])) + eq_(c1.name, None) + eq_(c2.name, None) + eq_(c3.name, "x") + + def testGetObjectsByName(self): + c1 = CClass(self.mcl) + eq_(set(c1.getObjects("o1")), set()) + o1 = CObject(c1, "o1") + eq_(c1.objects, [o1]) + eq_(set(c1.getObjects("o1")), set([o1])) + o2 = CObject(c1, "o1") + eq_(set(c1.getObjects("o1")), set([o1, o2])) + ok_(o1 != o2) + o3 = CObject(c1, "o1") + eq_(set(c1.getObjects("o1")), set([o1, o2, o3])) + eq_(c1.getObject("o1"), o1) + + def testDeleteClass(self): + cl1 = CClass(self.mcl, "CL1") + cl1.delete() + eq_(set(self.mcl.classes), set()) + + cl1 = CClass(self.mcl, "CL1") + cl2 = CClass(self.mcl, "CL2") + cl3 = CClass(self.mcl, "CL3", superclasses = cl2, attributes = {"i" : 1}) + cl3.setValue("i", 7) + CObject(cl3) + + cl1.delete() + eq_(set(self.mcl.classes), set([cl2, cl3])) + cl3.delete() + eq_(set(self.mcl.classes), set([cl2])) + + eq_(cl3.superclasses, []) + eq_(cl2.subclasses, []) + eq_(cl3.attributes, []) + eq_(cl3.attributeNames, []) + eq_(cl3.metaclass, None) + eq_(cl3.objects, []) + eq_(cl3.name, None) + eq_(cl3.bundles, []) + try: + cl3.getValue("i") + exceptionExpected_() + except CException as e: + eq_("can't get value 'i' on deleted object", e.value) + + def testDeleteClassInstanceRelation(self): + cl1 = CClass(self.mcl, "CL1") + cl2 = CClass(self.mcl, "CL2") + obj1 = CObject(cl1, "O1") + obj2 = CObject(cl1, "O2") + obj3 = CObject(cl2, "O3") + + cl1.delete() + + eq_(obj1.classifier, None) + eq_(obj2.classifier, None) + eq_(obj3.classifier, cl2) + eq_(set(cl2.objects), set([obj3])) + eq_(cl1.objects, []) + + def testMetaclassChange(self): + m1 = CMetaclass("M1") + m2 = CMetaclass("M2") + c1 = CClass(m1, "C1") + c1.metaclass = m2 + eq_(c1.metaclass, m2) + eq_(m1.classes, []) + eq_(m2.classes, [c1]) + + def testMetaclassChangeNoneInput(self): + m1 = CMetaclass("M1") + m2 = CMetaclass("M2") + c1 = CClass(m1, "C1") + try: + c1.metaclass = None + exceptionExpected_() + except CException as e: + eq_("'None' is not a metaclass", e.value) + + def testMetaclassChangeWrongInputType(self): + m1 = CMetaclass("M1") + m2 = CMetaclass("M2") + c1 = CClass(m1, "C1") + try: + c1.metaclass = CClass(m1) + exceptionExpected_() + except CException as e: + ok_(e.value.endswith("' is not a metaclass")) + + def testMetaclassIsDeletedInConstructor(self): + m1 = CMetaclass("M1") + m1.delete() + try: + CClass(m1, "C1") + exceptionExpected_() + except CException as e: + ok_(e.value.endswith("cannot access named element that has been deleted")) + + def testMetaclassIsDeletedInMetaclassMethod(self): + m1 = CMetaclass("M1") + m2 = CMetaclass("M2") + c1 = CClass(m2, "C1") + m1.delete() + try: + c1.metaclass = m1 + exceptionExpected_() + except CException as e: + ok_(e.value.endswith("cannot access named element that has been deleted")) + + def testMetaclassIsNoneInConstructor(self): + try: + CClass(None, "C1") + exceptionExpected_() + except CException as e: + ok_(e.value.endswith("'None' is not a metaclass")) + + def testGetClassObject(self): + cl = CClass(self.mcl, "CX") + eq_(cl.classObject.name, cl.name) + eq_(cl.classObject.classifier, self.mcl) + eq_(cl.classObject._classObjectClass, cl) + + + def testGetConnectedElements_WrongKeywordArg(self): + c1 = CClass(self.mcl, "c1") + try: + c1.getConnectedElements(a = "c1") + exceptionExpected_() + except CException as e: + eq_(e.value, "unknown keyword argument 'a', should be one of: ['addBundles', 'processBundles', 'stopElementsInclusive', 'stopElementsExclusive']") + + def testGetConnectedElementsEmpty(self): + c1 = CClass(self.mcl, "c1") + eq_(set(c1.getConnectedElements()), set([c1])) + + mcl = CMetaclass("MCL") + c1 = CClass(mcl, "c1") + c2 = CClass(mcl, "c2", superclasses = c1) + c3 = CClass(mcl, "c3", superclasses = c2) + c4 = CClass(mcl, "c4", superclasses = c2) + c5 = CClass(mcl, "c5", superclasses = c4) + c6 = CClass(mcl, "c6") + a1 = c1.association(c6) + c7 = CClass(mcl, "c7") + c8 = CClass(mcl, "c8") + a3 = c8.association(c7) + c9 = CClass(mcl, "c9") + a4 = c7.association(c9) + c10 = CClass(mcl, "c10") + c11 = CClass(mcl, "c11", superclasses = c10) + c12 = CClass(mcl, "c12") + c13 = CClass(mcl, "c13") + c12.association(c11) + bsub = CBundle("bsub", elements = [c13]) + b1 = CBundle("b1", elements = [c1, c2, c3, bsub, c7]) + b2 = CBundle("b2", elements = [c7, c10, c11, c12]) + + allTestElts = [c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, b1, b2, bsub] + + @parameterized.expand([ + (allTestElts, {"processBundles": True}, set([c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13])), + (allTestElts, {"processBundles": True, "addBundles": True}, set([c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, b1, b2, bsub])), + ([c1], {"processBundles": True}, set([c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13])), + ([c1], {"processBundles": True, "addBundles": True}, set([c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, b1, b2, bsub])), + ([c7], {}, set([c7, c8, c9])), + ([c7], {"addBundles": True}, set([c7, c8, c9, b1, b2])), + ]) + def testGetConnectedElements(self, testElements, kwargsDict, connectedElementsResult): + for elt in testElements: + eq_(set(elt.getConnectedElements(**kwargsDict)), connectedElementsResult) + + def testGetConnectedElements_StopElementsInclusiveWrongTypes(self): + c1 = CClass(self.mcl, "c1") + try: + c1.getConnectedElements(stopElementsInclusive = "c1") + exceptionExpected_() + except CException as e: + eq_(e.value, "expected one element or a list of stop elements, but got: 'c1'") + try: + c1.getConnectedElements(stopElementsInclusive = ["c1"]) + exceptionExpected_() + except CException as e: + eq_(e.value, "expected one element or a list of stop elements, but got: '['c1']' with element of wrong type: 'c1'") + + @parameterized.expand([ + ([c1], {"stopElementsExclusive" : [c1]}, set()), + ([c1], {"stopElementsInclusive" : [c3, c6]}, set([c1, c2, c3, c4, c5, c6])), + ([c1], {"stopElementsExclusive" : [c3, c6]}, set([c1, c2, c4, c5])), + ([c1], {"stopElementsInclusive" : [c3, c6], "stopElementsExclusive" : [c3]}, set([c1, c2, c4, c5, c6])), + ([c7], {"stopElementsInclusive" : [b2], "stopElementsExclusive" : [b1], "processBundles": True, "addBundles": True}, set([c7, b2, c8, c9])), + ([c7], {"stopElementsExclusive" : [b1, b2], "processBundles": True, "addBundles": True}, set([c7, c8, c9])), + ]) + def testGetConnectedElements_StopElementsInclusive(self, testElements, kwargsDict, connectedElementsResult): + for elt in testElements: + eq_(set(elt.getConnectedElements(**kwargsDict)), connectedElementsResult) + + + +if __name__ == "__main__": + nose.main() + + + diff --git a/tests/test_class_associations.py b/tests/test_class_associations.py new file mode 100644 index 0000000..9631550 --- /dev/null +++ b/tests/test_class_associations.py @@ -0,0 +1,202 @@ +import sys +sys.path.append("..") + +import re + +import nose +from nose.tools import ok_, eq_ +from testCommons import neq_, exceptionExpected_ +from parameterized import parameterized + +from codeableModels import CMetaclass, CClass, CObject, CAttribute, CException, CEnum, CStereotype + +class TestClassAssociations(): + + def setUp(self): + self.mcl = CMetaclass("MCL") + self.c1 = CClass(self.mcl, "C1") + self.c2 = CClass(self.mcl, "C2") + self.c3 = CClass(self.mcl, "C3") + self.c4 = CClass(self.mcl, "C4") + self.c5 = CClass(self.mcl, "C5") + + def getAllAssociationsOfMetaclass(self): + associations = [] + for c in self.mcl.allClasses: + for a in c.allAssociations: + if not a in associations: + associations.append(a) + return associations + + def testAssociationCreation(self): + a1 = self.c1.association(self.c2, multiplicity = "1", roleName = "t", + sourceMultiplicity = "*", sourceRoleName = "i") + a2 = self.c1.association(self.c2, "[o]*->[s]1") + a3 = self.c1.association(self.c3, "[a] 0..1 <*>- [n]*") + a4 = self.c1.association(self.c3, multiplicity = "*", roleName = "e", + sourceMultiplicity = "0..1", sourceRoleName = "a", composition = True) + a5 = self.c4.association(self.c3, multiplicity = "*", roleName = "n", + sourceMultiplicity = "0..1", sourceRoleName = "a", aggregation = True) + a6 = self.c3.association(self.c2, '[a] 3 <>- [e]*') + + eq_(len(self.getAllAssociationsOfMetaclass()), 6) + + eq_(self.c1.associations[0].roleName, "t") + eq_(a5.roleName, "n") + eq_(a2.roleName, "s") + eq_(a1.multiplicity, "1") + eq_(a1.sourceMultiplicity, "*") + eq_(a4.sourceMultiplicity, "0..1") + eq_(a6.sourceMultiplicity, "3") + + eq_(a1.composition, False) + eq_(a1.aggregation, False) + eq_(a3.composition, True) + eq_(a3.aggregation, False) + eq_(a5.composition, False) + eq_(a5.aggregation, True) + + a1.aggregation = True + eq_(a1.composition, False) + eq_(a1.aggregation, True) + a1.composition = True + eq_(a1.composition, True) + eq_(a1.aggregation, False) + + def testMixedAssociationTypes(self): + m1 = CMetaclass("M1") + s1 = CStereotype("S1") + try: + a1 = self.c1.association(s1, multiplicity = "1", roleName = "t", + sourceMultiplicity = "*", sourceRoleName = "i") + exceptionExpected_() + except CException as e: + eq_("class 'C1' is not compatible with association target 'S1'", e.value) + + try: + a1 = self.c1.association(m1, multiplicity = "1", roleName = "t", + sourceMultiplicity = "*", sourceRoleName = "i") + exceptionExpected_() + except CException as e: + eq_("class 'C1' is not compatible with association target 'M1'", e.value) + + def testGetAssociationByRoleName(self): + a1 = self.c1.association(self.c2, multiplicity = "1", roleName = "t", + sourceMultiplicity = "*", sourceRoleName = "i") + a2 = self.c1.association(self.c2, multiplicity = "1", roleName = "s", + sourceMultiplicity = "*", sourceRoleName = "o") + a3 = self.c1.association(self.c3, multiplicity = "*", roleName = "n", + sourceMultiplicity = "0..1", sourceRoleName = "a", composition = True) + + a_2 = next(a for a in self.c1.associations if a.roleName == "s") + eq_(a_2.multiplicity, "1") + eq_(a_2.sourceRoleName, "o") + eq_(a_2.sourceMultiplicity, "*") + + def testGetAssociationByName(self): + a1 = self.c1.association(self.c2, name = "n1", multiplicity = "1", roleName = "t", + sourceMultiplicity = "*", sourceRoleName = "i") + a2 = self.c1.association(self.c2, name = "n2", multiplicity = "1", roleName = "s", + sourceMultiplicity = "*", sourceRoleName = "o") + a3 = self.c1.association(self.c3, "n3: [a] 0..1 <*>- [n] *") + + a_2 = next(a for a in self.c1.associations if a.name == "n2") + eq_(a_2.multiplicity, "1") + eq_(a_2.sourceRoleName, "o") + eq_(a_2.sourceMultiplicity, "*") + + a_3 = next(a for a in self.c1.associations if a.name == "n3") + eq_(a_3.multiplicity, "*") + eq_(a_3.roleName, "n") + eq_(a_3.sourceMultiplicity, "0..1") + eq_(a_3.sourceRoleName, "a") + eq_(a_3.composition, True) + + def testGetAssociations(self): + a1 = self.c1.association(self.c2, multiplicity = "1", roleName = "t", + sourceMultiplicity = "*", sourceRoleName = "i") + a2 = self.c1.association(self.c2, multiplicity = "1", roleName = "s", + sourceMultiplicity = "*", sourceRoleName = "o") + a3 = self.c1.association(self.c3, multiplicity = "*", roleName = "n", + sourceMultiplicity = "0..1", sourceRoleName = "a", composition = True) + a4 = self.c1.association(self.c3, multiplicity = "*", roleName = "e", + sourceMultiplicity = "0..1", sourceRoleName = "a", composition = True) + a5 = self.c4.association(self.c3, multiplicity = "*", roleName = "n", + sourceMultiplicity = "0..1", sourceRoleName = "a", aggregation = True) + a6 = self.c3.association(self.c2, multiplicity = "*", roleName = "e", + sourceMultiplicity = "3", sourceRoleName = "a", aggregation = True) + eq_(self.c1.associations, [a1, a2, a3, a4]) + eq_(self.c2.associations, [a1, a2, a6]) + eq_(self.c3.associations, [a3, a4, a5, a6]) + eq_(self.c4.associations, [a5]) + eq_(self.c5.associations, []) + + def testDeleteAssociations(self): + a1 = self.c1.association(self.c2, multiplicity = "1", roleName = "t", + sourceMultiplicity = "*", sourceRoleName = "i") + a2 = self.c1.association(self.c2, multiplicity = "1", roleName = "s", + sourceMultiplicity = "*", sourceRoleName = "o") + a3 = self.c1.association(self.c3, multiplicity = "*", roleName = "n", + sourceMultiplicity = "0..1", sourceRoleName = "a", composition = True) + a4 = self.c1.association(self.c3, multiplicity = "*", roleName = "e", + sourceMultiplicity = "0..1", sourceRoleName = "a", composition = True) + a5 = self.c4.association(self.c3, multiplicity = "*", roleName = "n", + sourceMultiplicity = "0..1", sourceRoleName = "a", aggregation = True) + a6 = self.c3.association(self.c2, multiplicity = "*", roleName = "e", + sourceMultiplicity = "3", sourceRoleName = "a", aggregation = True) + a7 = self.c1.association(self.c1, multiplicity = "*", roleName = "x", + sourceMultiplicity = "3", sourceRoleName = "y") + + eq_(len(self.getAllAssociationsOfMetaclass()), 7) + + a2.delete() + a4.delete() + + eq_(len(self.getAllAssociationsOfMetaclass()), 5) + + eq_(self.c1.associations, [a1, a3, a7]) + eq_(self.c2.associations, [a1, a6]) + eq_(self.c3.associations, [a3, a5, a6]) + eq_(self.c4.associations, [a5]) + eq_(self.c5.associations, []) + + + def testDeleteClassAndGetAssociations(self): + a1 = self.c1.association(self.c2, multiplicity = "1", roleName = "t", + sourceMultiplicity = "*", sourceRoleName = "i") + a2 = self.c1.association(self.c2, multiplicity = "1", roleName = "s", + sourceMultiplicity = "*", sourceRoleName = "o") + a3 = self.c1.association(self.c3, multiplicity = "*", roleName = "n", + sourceMultiplicity = "0..1", sourceRoleName = "a", composition = True) + a4 = self.c1.association(self.c3, multiplicity = "*", roleName = "e", + sourceMultiplicity = "0..1", sourceRoleName = "a", composition = True) + a5 = self.c4.association(self.c3, multiplicity = "*", roleName = "n", + sourceMultiplicity = "0..1", sourceRoleName = "a", aggregation = True) + a6 = self.c3.association(self.c2, multiplicity = "*", roleName = "e", + sourceMultiplicity = "3", sourceRoleName = "a", aggregation = True) + a7 = self.c1.association(self.c1, multiplicity = "*", roleName = "x", + sourceMultiplicity = "3", sourceRoleName = "y") + + eq_(len(self.getAllAssociationsOfMetaclass()), 7) + + self.c1.delete() + + eq_(len(self.getAllAssociationsOfMetaclass()), 2) + + eq_(self.c1.associations, []) + eq_(self.c2.associations, [a6]) + eq_(self.c3.associations, [a5, a6]) + eq_(self.c4.associations, [a5]) + eq_(self.c5.associations, []) + + + def testAllAssociations(self): + s = CClass(self.mcl, "S") + d = CClass(self.mcl, "D", superclasses = s) + a = s.association(d, "is next: [prior s] * -> [next d] *") + eq_(d.allAssociations, [a]) + eq_(s.allAssociations, [a]) + + +if __name__ == "__main__": + nose.main() \ No newline at end of file diff --git a/tests/test_class_attribute_values.py b/tests/test_class_attribute_values.py new file mode 100644 index 0000000..da9971f --- /dev/null +++ b/tests/test_class_attribute_values.py @@ -0,0 +1,551 @@ +import sys +sys.path.append("..") + +import re + +import nose +from nose.tools import ok_, eq_ +from testCommons import neq_, exceptionExpected_ +from parameterized import parameterized + +from codeableModels import CMetaclass, CClass, CObject, CAttribute, CException, CEnum + +class TestClassAttributeValues(): + def setUp(self): + self.mcl = CMetaclass("MCL") + + def testValuesOnPrimitiveTypeAttributes(self): + mcl = CMetaclass("M", attributes = { + "isBoolean": True, + "intVal": 1, + "floatVal": 1.1, + "string": "abc"}) + cl = CClass(mcl, "C") + + eq_(cl.getValue("isBoolean"), True) + eq_(cl.getValue("intVal"), 1) + eq_(cl.getValue("floatVal"), 1.1) + eq_(cl.getValue("string"), "abc") + + cl.setValue("isBoolean", False) + cl.setValue("intVal", 2) + cl.setValue("floatVal", 2.1) + cl.setValue("string", "y") + + eq_(cl.getValue("isBoolean"), False) + eq_(cl.getValue("intVal"), 2) + eq_(cl.getValue("floatVal"), 2.1) + eq_(cl.getValue("string"), "y") + + def testAttributeOfValueUnknown(self): + cl = CClass(self.mcl, "C") + try: + cl.getValue("x") + exceptionExpected_() + except CException as e: + eq_(e.value, "attribute 'x' unknown for object 'C'") + + self.mcl.attributes = {"isBoolean": True, "intVal": 1} + try: + cl.setValue("x", 1) + exceptionExpected_() + except CException as e: + eq_(e.value, "attribute 'x' unknown for object 'C'") + + def testIntegersAsFloats(self): + cl = CMetaclass("C", attributes = { + "floatVal": float}) + o = CClass(cl, "C") + o.setValue("floatVal", 15) + eq_(o.getValue("floatVal"), 15) + + def testAttributeDefinedAfterInstance(self): + cl = CMetaclass("C") + o = CClass(cl, "C") + cl.attributes = {"floatVal": float} + o.setValue("floatVal", 15) + eq_(o.getValue("floatVal"), 15) + + def testObjectTypeAttributeValues(self): + attrType = CClass(self.mcl, "AttrType") + attrValue = CObject(attrType, "attrValue") + self.mcl.attributes = {"attrTypeObj" : attrValue} + objAttr = self.mcl.getAttribute("attrTypeObj") + eq_(objAttr.type, attrType) + cl = CClass(self.mcl, "C") + eq_(cl.getValue("attrTypeObj"), attrValue) + + nonAttrValue = CObject(CClass(self.mcl), "nonAttrValue") + try: + cl.setValue("attrTypeObj", nonAttrValue) + exceptionExpected_() + except CException as e: + eq_(e.value, "type of object 'nonAttrValue' is not matching type of attribute 'attrTypeObj'") + + def testAddObjectAttributeGetSetValue(self): + attrType = CClass(self.mcl, "AttrType") + attrValue = CObject(attrType, "attrValue") + cl = CClass(self.mcl) + self.mcl.attributes = { + "attrTypeObj1" : attrType, "attrTypeObj2" : attrValue + } + eq_(cl.getValue("attrTypeObj1"), None) + eq_(cl.getValue("attrTypeObj2"), attrValue) + + def testObjectAttributeOfSuperclassType(self): + attrSuperType = CClass(self.mcl, "AttrSuperType") + attrType = CClass(self.mcl, "AttrType", superclasses = attrSuperType) + attrValue = CObject(attrType, "attrValue") + cl = CClass(self.mcl) + self.mcl.attributes = { + "attrTypeObj1" : attrSuperType, "attrTypeObj2" : attrValue + } + cl.setValue("attrTypeObj1", attrValue) + cl.setValue("attrTypeObj2", attrValue) + eq_(cl.getValue("attrTypeObj1"), attrValue) + eq_(cl.getValue("attrTypeObj2"), attrValue) + + def testValuesOnAttributesWithNoDefaultValues(self): + attrType = CClass(self.mcl, "AttrType") + enumType = CEnum("EnumT", values = ["A", "B", "C"]) + mcl = CMetaclass("M", attributes = { + "b": bool, + "i": int, + "f": float, + "s": str, + "C": attrType, + "e": enumType}) + cl = CClass(mcl, "C") + for n in ["b", "i", "f", "s", "C", "e"]: + eq_(cl.getValue(n), None) + + def testEnumTypeAttributeValues(self): + enumType = CEnum("EnumT", values = ["A", "B", "C"]) + mcl = CMetaclass(attributes = { + "e1": enumType, + "e2": enumType}) + e2 = mcl.getAttribute("e2") + e2.default = "A" + cl = CClass(mcl, "C") + eq_(cl.getValue("e1"), None) + eq_(cl.getValue("e2"), "A") + cl.setValue("e1", "B") + cl.setValue("e2", "C") + eq_(cl.getValue("e1"), "B") + eq_(cl.getValue("e2"), "C") + try: + cl.setValue("e1", "X") + exceptionExpected_() + except CException as e: + eq_(e.value, "value 'X' is not element of enumeration") + + def testDefaultInitAfterInstanceCreation(self): + enumType = CEnum("EnumT", values = ["A", "B", "C"]) + mcl = CMetaclass(attributes = { + "e1": enumType, + "e2": enumType}) + cl = CClass(mcl, "C") + e2 = mcl.getAttribute("e2") + e2.default = "A" + eq_(cl.getValue("e1"), None) + eq_(cl.getValue("e2"), "A") + + def testAttributeValueTypeCheckBool1(self): + self.mcl.attributes = {"t": bool} + cl = CClass(self.mcl, "C") + try: + cl.setValue("t", self.mcl) + exceptionExpected_() + except CException as e: + eq_(f"value for attribute 't' is not a known attribute type", e.value) + + def testAttributeValueTypeCheckBool2(self): + self.mcl.attributes = {"t": bool} + cl = CClass(self.mcl, "C") + try: + cl.setValue("t", 1) + exceptionExpected_() + except CException as e: + eq_(f"value type for attribute 't' does not match attribute type", e.value) + + def testAttributeValueTypeCheckInt(self): + self.mcl.attributes = {"t": int} + cl = CClass(self.mcl, "C") + try: + cl.setValue("t", True) + exceptionExpected_() + except CException as e: + eq_(f"value type for attribute 't' does not match attribute type", e.value) + + def testAttributeValueTypeCheckFloat(self): + self.mcl.attributes = {"t": float} + cl = CClass(self.mcl, "C") + try: + cl.setValue("t", True) + exceptionExpected_() + except CException as e: + eq_(f"value type for attribute 't' does not match attribute type", e.value) + + def testAttributeValueTypeCheckStr(self): + self.mcl.attributes = {"t": str} + cl = CClass(self.mcl, "C") + try: + cl.setValue("t", True) + exceptionExpected_() + except CException as e: + eq_(f"value type for attribute 't' does not match attribute type", e.value) + + def testAttributeValueTypeCheckObject(self): + attrType = CMetaclass("AttrType") + self.mcl.attributes = {"t": attrType} + cl = CClass(self.mcl, "C") + try: + cl.setValue("t", True) + exceptionExpected_() + except CException as e: + eq_(f"value type for attribute 't' does not match attribute type", e.value) + + def testAttributeValueTypeCheckEnum(self): + enumType = CEnum("EnumT", values = ["A", "B", "C"]) + self.mcl.attributes = {"t": enumType} + cl = CClass(self.mcl, "C") + try: + cl.setValue("t", True) + exceptionExpected_() + except CException as e: + eq_(f"value type for attribute 't' does not match attribute type", e.value) + + def testAttributeDeleted(self): + mcl = CMetaclass(attributes = { + "isBoolean": True, + "intVal": 15}) + cl = CClass(mcl, "C") + eq_(cl.getValue("intVal"), 15) + mcl.attributes = { + "isBoolean": False} + eq_(cl.getValue("isBoolean"), True) + try: + cl.getValue("intVal") + exceptionExpected_() + except CException as e: + eq_(e.value, "attribute 'intVal' unknown for object 'C'") + try: + cl.setValue("intVal", 1) + exceptionExpected_() + except CException as e: + eq_(e.value, "attribute 'intVal' unknown for object 'C'") + + def testAttributeDeletedNoDefault(self): + mcl = CMetaclass( attributes = { + "isBoolean": bool, + "intVal": int}) + mcl.attributes = {"isBoolean": bool} + cl = CClass(mcl, "C") + eq_(cl.getValue("isBoolean"), None) + try: + cl.getValue("intVal") + exceptionExpected_() + except CException as e: + eq_(e.value, "attribute 'intVal' unknown for object 'C'") + try: + cl.setValue("intVal", 1) + exceptionExpected_() + except CException as e: + eq_(e.value, "attribute 'intVal' unknown for object 'C'") + + def testAttributesOverwrite(self): + mcl = CMetaclass(attributes = { + "isBoolean": True, + "intVal": 15}) + cl = CClass(mcl, "C") + eq_(cl.getValue("intVal"), 15) + try: + cl.getValue("floatVal") + exceptionExpected_() + except CException as e: + eq_(e.value, "attribute 'floatVal' unknown for object 'C'") + cl.setValue("intVal", 18) + mcl.attributes = { + "isBoolean": False, + "intVal": 19, + "floatVal": 25.1} + eq_(cl.getValue("isBoolean"), True) + eq_(cl.getValue("floatVal"), 25.1) + eq_(cl.getValue("intVal"), 18) + cl.setValue("floatVal", 1.2) + eq_(cl.getValue("floatVal"), 1.2) + + def testAttributesOverwriteNoDefaults(self): + mcl = CMetaclass(attributes = { + "isBoolean": bool, + "intVal": int}) + cl = CClass(mcl, "C") + eq_(cl.getValue("isBoolean"), None) + cl.setValue("isBoolean", False) + mcl.attributes = { + "isBoolean": bool, + "intVal": int, + "floatVal": float} + eq_(cl.getValue("isBoolean"), False) + eq_(cl.getValue("floatVal"), None) + eq_(cl.getValue("intVal"), None) + cl.setValue("floatVal", 1.2) + eq_(cl.getValue("floatVal"), 1.2) + + def testAttributesDeletedOnSubclass(self): + mcl = CMetaclass("M", attributes = { + "isBoolean": True, + "intVal": 1}) + mcl2 = CMetaclass("M2", attributes = { + "isBoolean": False}, superclasses = mcl) + + cl = CClass(mcl2, "C") + + eq_(cl.getValue("isBoolean"), False) + eq_(cl.getValue("isBoolean", mcl), True) + eq_(cl.getValue("isBoolean", mcl2), False) + + mcl2.attributes = {} + + eq_(cl.getValue("isBoolean"), True) + eq_(cl.getValue("intVal"), 1) + eq_(cl.getValue("isBoolean", mcl), True) + try: + cl.getValue("isBoolean", mcl2) + exceptionExpected_() + except CException as e: + eq_(e.value, "attribute 'isBoolean' unknown for 'M2'") + + def testAttributesDeletedOnSubclassNoDefaults(self): + mcl = CMetaclass("M", attributes = { + "isBoolean": bool, + "intVal": int}) + mcl2 = CMetaclass("M2", attributes = { + "isBoolean": bool}, superclasses = mcl) + + cl = CClass(mcl2, "C") + + eq_(cl.getValue("isBoolean"), None) + eq_(cl.getValue("isBoolean", mcl), None) + eq_(cl.getValue("isBoolean", mcl2), None) + + mcl2.attributes = {} + + eq_(cl.getValue("isBoolean"), None) + eq_(cl.getValue("intVal"), None) + eq_(cl.getValue("isBoolean", mcl), None) + try: + cl.getValue("isBoolean", mcl2) + exceptionExpected_() + except CException as e: + eq_(e.value, "attribute 'isBoolean' unknown for 'M2'") + + + def testAttributeValuesInheritance(self): + t1 = CMetaclass("T1") + t2 = CMetaclass("T2") + c = CMetaclass("C", superclasses = [t1, t2]) + sc = CMetaclass("C", superclasses = c) + + t1.attributes = {"i0" : 0} + t2.attributes = {"i1" : 1} + c.attributes = {"i2" : 2} + sc.attributes = {"i3" : 3} + + cl = CClass(sc, "C") + + for name, value in {"i0" : 0, "i1" : 1, "i2" : 2, "i3" : 3}.items(): + eq_(cl.getValue(name), value) + + eq_(cl.getValue("i0", t1), 0) + eq_(cl.getValue("i1", t2), 1) + eq_(cl.getValue("i2", c), 2) + eq_(cl.getValue("i3", sc), 3) + + for name, value in {"i0" : 10, "i1" : 11, "i2" : 12, "i3" : 13}.items(): + cl.setValue(name, value) + + for name, value in {"i0" : 10, "i1" : 11, "i2" : 12, "i3" : 13}.items(): + eq_(cl.getValue(name), value) + + eq_(cl.getValue("i0", t1), 10) + eq_(cl.getValue("i1", t2), 11) + eq_(cl.getValue("i2", c), 12) + eq_(cl.getValue("i3", sc), 13) + + def testAttributeValuesInheritanceAfterDeleteSuperclass(self): + t1 = CMetaclass("T1") + t2 = CMetaclass("T2") + c = CMetaclass("C", superclasses = [t1, t2]) + sc = CMetaclass("C", superclasses = c) + + t1.attributes = {"i0" : 0} + t2.attributes = {"i1" : 1} + c.attributes = {"i2" : 2} + sc.attributes = {"i3" : 3} + + cl = CClass(sc, "C") + + t2.delete() + + for name, value in {"i0" : 0, "i2" : 2, "i3" : 3}.items(): + eq_(cl.getValue(name), value) + try: + cl.getValue("i1") + exceptionExpected_() + except CException as e: + eq_(e.value, "attribute 'i1' unknown for object 'C'") + + eq_(cl.getValue("i0", t1), 0) + try: + cl.getValue("i1", t2) + exceptionExpected_() + except CException as e: + eq_(e.value, "attribute 'i1' unknown for ''") + eq_(cl.getValue("i2", c), 2) + eq_(cl.getValue("i3", sc), 3) + + for name, value in {"i0" : 10, "i2" : 12, "i3" : 13}.items(): + cl.setValue(name, value) + try: + cl.setValue("i1", 11) + exceptionExpected_() + except CException as e: + eq_(e.value, "attribute 'i1' unknown for object 'C'") + + for name, value in {"i0" : 10, "i2" : 12, "i3" : 13}.items(): + eq_(cl.getValue(name), value) + try: + cl.getValue("i1") + exceptionExpected_() + except CException as e: + eq_(e.value, "attribute 'i1' unknown for object 'C'") + + eq_(cl.getValue("i0", t1), 10) + try: + cl.getValue("i1", t2) + exceptionExpected_() + except CException as e: + eq_(e.value, "attribute 'i1' unknown for ''") + eq_(cl.getValue("i2", c), 12) + eq_(cl.getValue("i3", sc), 13) + + + def testAttributeValuesSameNameInheritance(self): + t1 = CMetaclass("T1") + t2 = CMetaclass("T2") + c = CMetaclass("C", superclasses = [t1, t2]) + sc = CMetaclass("C", superclasses = c) + + t1.attributes = {"i" : 0} + t2.attributes = {"i" : 1} + c.attributes = {"i" : 2} + sc.attributes = {"i" : 3} + + cl1 = CClass(sc) + cl2 = CClass(c) + cl3 = CClass(t1) + + eq_(cl1.getValue("i"), 3) + eq_(cl2.getValue("i"), 2) + eq_(cl3.getValue("i"), 0) + + eq_(cl1.getValue("i", sc), 3) + eq_(cl1.getValue("i", c), 2) + eq_(cl1.getValue("i", t2), 1) + eq_(cl1.getValue("i", t1), 0) + eq_(cl2.getValue("i", c), 2) + eq_(cl2.getValue("i", t2), 1) + eq_(cl2.getValue("i", t1), 0) + eq_(cl3.getValue("i", t1), 0) + + cl1.setValue("i", 10) + cl2.setValue("i", 11) + cl3.setValue("i", 12) + + eq_(cl1.getValue("i"), 10) + eq_(cl2.getValue("i"), 11) + eq_(cl3.getValue("i"), 12) + + eq_(cl1.getValue("i", sc), 10) + eq_(cl1.getValue("i", c), 2) + eq_(cl1.getValue("i", t2), 1) + eq_(cl1.getValue("i", t1), 0) + eq_(cl2.getValue("i", c), 11) + eq_(cl2.getValue("i", t2), 1) + eq_(cl2.getValue("i", t1), 0) + eq_(cl3.getValue("i", t1), 12) + + cl1.setValue("i", 130, sc) + cl1.setValue("i", 100, t1) + cl1.setValue("i", 110, t2) + cl1.setValue("i", 120, c) + + eq_(cl1.getValue("i"), 130) + + eq_(cl1.getValue("i", sc), 130) + eq_(cl1.getValue("i", c), 120) + eq_(cl1.getValue("i", t2), 110) + eq_(cl1.getValue("i", t1), 100) + + + def testValuesMultipleInheritance(self): + t1 = CMetaclass("T1") + t2 = CMetaclass("T2") + sta = CMetaclass("STA", superclasses = [t1, t2]) + suba = CMetaclass("SubA", superclasses = [sta]) + stb = CMetaclass("STB", superclasses = [t1, t2]) + subb = CMetaclass("SubB", superclasses = [stb]) + stc = CMetaclass("STC") + subc = CMetaclass("SubC", superclasses = [stc]) + + mcl = CMetaclass("M", superclasses = [suba, subb, subc]) + cl = CClass(mcl, "C") + + t1.attributes = {"i0" : 0} + t2.attributes = {"i1" : 1} + sta.attributes = {"i2" : 2} + suba.attributes = {"i3" : 3} + stb.attributes = {"i4" : 4} + subb.attributes = {"i5" : 5} + stc.attributes = {"i6" : 6} + subc.attributes = {"i7" : 7} + + eq_(cl.getValue("i0"), 0) + eq_(cl.getValue("i1"), 1) + eq_(cl.getValue("i2"), 2) + eq_(cl.getValue("i3"), 3) + eq_(cl.getValue("i4"), 4) + eq_(cl.getValue("i5"), 5) + eq_(cl.getValue("i6"), 6) + eq_(cl.getValue("i7"), 7) + + eq_(cl.getValue("i0", t1), 0) + eq_(cl.getValue("i1", t2), 1) + eq_(cl.getValue("i2", sta), 2) + eq_(cl.getValue("i3", suba), 3) + eq_(cl.getValue("i4", stb), 4) + eq_(cl.getValue("i5", subb), 5) + eq_(cl.getValue("i6", stc), 6) + eq_(cl.getValue("i7", subc), 7) + + cl.setValue("i0", 10) + cl.setValue("i1", 11) + cl.setValue("i2", 12) + cl.setValue("i3", 13) + cl.setValue("i4", 14) + cl.setValue("i5", 15) + cl.setValue("i6", 16) + cl.setValue("i7", 17) + + eq_(cl.getValue("i0"), 10) + eq_(cl.getValue("i1"), 11) + eq_(cl.getValue("i2"), 12) + eq_(cl.getValue("i3"), 13) + eq_(cl.getValue("i4"), 14) + eq_(cl.getValue("i5"), 15) + eq_(cl.getValue("i6"), 16) + eq_(cl.getValue("i7"), 17) + +if __name__ == "__main__": + nose.main() \ No newline at end of file diff --git a/tests/test_class_attributes.py b/tests/test_class_attributes.py new file mode 100644 index 0000000..1152461 --- /dev/null +++ b/tests/test_class_attributes.py @@ -0,0 +1,351 @@ +import sys +sys.path.append("..") + +import nose +from nose.tools import ok_, eq_ +from testCommons import neq_, exceptionExpected_ +from parameterized import parameterized + +import re + +from codeableModels import CMetaclass, CClass, CObject, CAttribute, CException, CEnum + +class TestClassAttributes(): + def setUp(self): + self.mcl = CMetaclass("MCL") + self.cl = CClass(self.mcl, "CL") + + def testPrimitiveEmptyInput(self): + cl = CClass(self.mcl, "C", attributes = {}) + eq_(len(cl.attributes), 0) + eq_(len(cl.attributeNames), 0) + + def testPrimitiveNoneInput(self): + cl = CClass(self.mcl, "C", attributes = None) + eq_(len(cl.attributes), 0) + eq_(len(cl.attributeNames), 0) + + def testPrimitiveTypeAttributes(self): + cl = CClass(self.mcl, "C", attributes = { + "isBoolean": True, + "intVal": 1, + "floatVal": 1.1, + "string": "abc"}) + eq_(len(cl.attributes), 4) + eq_(len(cl.attributeNames), 4) + + ok_(set(["isBoolean", "intVal", "floatVal", "string"]).issubset(cl.attributeNames)) + + a1 = cl.getAttribute("isBoolean") + a2 = cl.getAttribute("intVal") + a3 = cl.getAttribute("floatVal") + a4 = cl.getAttribute("string") + ok_(set([a1, a2, a3, a4]).issubset(cl.attributes)) + eq_(None, cl.getAttribute("X")) + + eq_(a1.type, bool) + eq_(a2.type, int) + eq_(a3.type, float) + eq_(a4.type, str) + + d1 = a1.default + d2 = a2.default + d3 = a3.default + d4 = a4.default + + ok_(isinstance(d1, bool)) + ok_(isinstance(d2, int)) + ok_(isinstance(d3, float)) + ok_(isinstance(d4, str)) + + eq_(d1, True) + eq_(d2, 1) + eq_(d3, 1.1) + eq_(d4, "abc") + + def testAttributeGetNameAndClassifier(self): + cl = CClass(self.mcl, "C", attributes = {"isBoolean": True}) + a = cl.getAttribute("isBoolean") + eq_(a.name, "isBoolean") + eq_(a.classifier, cl) + cl.delete() + eq_(a.name, None) + eq_(a.classifier, None) + + def testPrimitiveAttributesNoDefault(self): + self.cl.attributes = {"a": bool, "b": int, "c": str, "d": float} + a1 = self.cl.getAttribute("a") + a2 = self.cl.getAttribute("b") + a3 = self.cl.getAttribute("c") + a4 = self.cl.getAttribute("d") + ok_(set([a1, a2, a3, a4]).issubset(self.cl.attributes)) + eq_(a1.default, None) + eq_(a1.type, bool) + eq_(a2.default, None) + eq_(a2.type, int) + eq_(a3.default, None) + eq_(a3.type, str) + eq_(a4.default, None) + eq_(a4.type, float) + + def testGetAttributeNotFound(self): + eq_(self.cl.getAttribute("x"), None) + self.cl.attributes = {"a": bool, "b": int, "c": str, "d": float} + eq_(self.cl.getAttribute("x"), None) + + def testTypeAndDefaultOnAttribute(self): + CAttribute(default = "", type = str) + CAttribute(type = str, default = "") + try: + CAttribute(default = 1, type = str) + exceptionExpected_() + except CException as e: + eq_("default value '1' incompatible with attribute's type ''", e.value) + try: + CAttribute(type = str, default = 1) + exceptionExpected_() + except CException as e: + eq_("default value '1' incompatible with attribute's type ''", e.value) + a5 = CAttribute(type = int) + eq_(a5.default, None) + + def testSameNamedArgumentsCAttributes(self): + a1 = CAttribute(default = "") + a2 = CAttribute(type = str) + n1 = "a" + self.cl.attributes = {n1: a1, "a": a2} + eq_(set(self.cl.attributes), {a2}) + eq_(self.cl.attributeNames, ["a"]) + + def testSameNamedArgumentsDefaults(self): + n1 = "a" + self.cl.attributes = {n1: "", "a": 1} + ok_(len(self.cl.attributes), 1) + eq_(self.cl.getAttribute("a").default, 1) + eq_(self.cl.attributeNames, ["a"]) + + def testObjectTypeAttribute(self): + attrType = CClass(self.mcl, "AttrType") + attrValue = CObject(attrType, "attrValue") + self.cl.attributes = {"attrTypeObj" : attrValue} + objAttr = self.cl.getAttribute("attrTypeObj") + attributes = self.cl.attributes + eq_(set(attributes), {objAttr}) + + boolAttr = CAttribute(default = True) + self.cl.attributes = {"attrTypeObj" : objAttr, "isBoolean" : boolAttr} + attributes = self.cl.attributes + eq_(set(attributes), {objAttr, boolAttr}) + eq_(self.cl.attributeNames, ["attrTypeObj", "isBoolean"]) + objAttr = self.cl.getAttribute("attrTypeObj") + eq_(objAttr.type, attrType) + default = objAttr.default + ok_(isinstance(default, CObject)) + eq_(default, attrValue) + + self.cl.attributes = {"attrTypeObj" : attrValue, "isBoolean" : boolAttr} + eq_(self.cl.attributeNames, ["attrTypeObj", "isBoolean"]) + # using the CObject in attributes causes a new CAttribute to be created != objAttr + neq_(self.cl.getAttribute("attrTypeObj"), objAttr) + + def testEnumGetValues(self): + enumValues = ["A", "B", "C"] + enumObj = CEnum("ABCEnum", values = enumValues) + eq_(["A", "B", "C"], enumObj.values) + ok_("A" in enumObj.values) + ok_(not("X" in enumObj.values)) + enumValues = [1, 2, 3] + enumObj = CEnum("123Enum", values = enumValues) + eq_([1,2,3], enumObj.values) + + def testEnumEmpty(self): + enumObj = CEnum("ABCEnum", values = []) + eq_(enumObj.values, []) + enumObj = CEnum("ABCEnum", values = None) + eq_(enumObj.values, []) + + def testEnumNoList(self): + enumValues = {"A", "B", "C"} + try: + CEnum("ABCEnum", values = enumValues) + exceptionExpected_() + except CException as e: + ok_(re.match("^an enum needs to be initialized with a list of values, but got:([ {}'CAB,]+)$", e.value)) + + def testEnumName(self): + enumValues = ["A", "B", "C"] + enumObj = CEnum("ABCEnum", values = enumValues) + eq_("ABCEnum", enumObj.name) + + def testDefineEnumTypeAttribute(self): + enumValues = ["A", "B", "C"] + enumObj = CEnum("ABCEnum", values = enumValues) + CAttribute(type = enumObj, default = "A") + CAttribute(default = "A", type = enumObj) + try: + CAttribute(type = enumObj, default = "X") + exceptionExpected_() + except CException as e: + eq_("default value 'X' incompatible with attribute's type 'ABCEnum'", e.value) + try: + CAttribute(default = "X", type = enumObj) + exceptionExpected_() + except CException as e: + eq_("default value 'X' incompatible with attribute's type 'ABCEnum'", e.value) + + def testUseEnumTypeAttribute(self): + enumValues = ["A", "B", "C"] + enumObj = CEnum("ABCEnum", values = enumValues) + ea1 = CAttribute(type = enumObj, default = "A") + ea2 = CAttribute(type = enumObj) + self.cl.attributes = {"letters1": ea1, "letters2": ea2} + eq_(set(self.cl.attributes), {ea1, ea2}) + ok_(isinstance(ea1.type, CEnum)) + + self.cl.attributes = {"letters1": ea1, "isBool": True, "letters2": ea2} + boolAttr = self.cl.getAttribute("isBool") + l1 = self.cl.getAttribute("letters1") + eq_(set(self.cl.attributes), {l1, ea2, boolAttr}) + eq_(l1.default, "A") + eq_(ea2.default, None) + + def testUnknownAttributeType(self): + try: + self.cl.attributes = {"x": CEnum, "b" : bool} + exceptionExpected_() + except CException as e: + ok_(re.match("^(unknown attribute type: '')$", e.value)) + + + def testSetAttributeDefaultValue(self): + enumObj = CEnum("ABCEnum", values = ["A", "B", "C"]) + self.cl.attributes = {"letters": enumObj, "b" : bool} + l = self.cl.getAttribute("letters") + b = self.cl.getAttribute("b") + eq_(l.default, None) + eq_(b.default, None) + l.default = "B" + b.default = False + eq_(l.default, "B") + eq_(b.default, False) + eq_(l.type, enumObj) + eq_(b.type, bool) + + def testCClassVsCObject(self): + cl_a = CClass(self.mcl, "A") + cl_b = CClass(self.mcl, "B") + obj_b = CObject(cl_b, "obj_b") + + self.cl.attributes = {"a": cl_a, "b": obj_b} + a = self.cl.getAttribute("a") + b = self.cl.getAttribute("b") + eq_(a.type, cl_a) + eq_(a.default, None) + eq_(b.type, cl_b) + eq_(b.default, obj_b) + + testMetaclass = CMetaclass("A") + testEnum = CEnum("AEnum", values = [1,2]) + testClass = CClass(testMetaclass, "CL") + + @parameterized.expand([ + (bool, testMetaclass), + (bool, 1.1), + (int, testMetaclass), + (int, "abc"), + (float, "1"), + (float, testMetaclass), + (str, 1), + (str, testMetaclass), + (testEnum, "1"), + (testEnum, testMetaclass), + (testClass, "1"), + (testClass, testMetaclass)]) + def testAttributeTypeCheck(self, type, wrongDefault): + self.cl.attributes = {"a": type} + attr = self.cl.getAttribute("a") + try: + attr.default = wrongDefault + exceptionExpected_() + except CException as e: + eq_(f"default value '{wrongDefault!s}' incompatible with attribute's type '{type!s}'", e.value) + + def test_DeleteAttributes(self): + self.cl.attributes = { + "isBoolean": True, + "intVal": 1, + "floatVal": 1.1, + "string": "abc"} + eq_(len(set(self.cl.attributes)), 4) + self.cl.attributes = {} + eq_(set(self.cl.attributes), set()) + self.cl.attributes = { + "isBoolean": True, + "intVal": 1, + "floatVal": 1.1, + "string": "abc"} + eq_(len(set(self.cl.attributes)), 4) + self.cl.attributes = {} + eq_(set(self.cl.attributes), set()) + + def testTypeObjectAttributeClassIsDeletedInConstructor(self): + attrCl = CClass(self.mcl, "AC") + attrCl.delete() + try: + CClass(self.mcl, "C", attributes = {"ac": attrCl}) + exceptionExpected_() + except CException as e: + eq_(e.value, "cannot access named element that has been deleted") + + def testTypeObjectAttributeClassIsDeletedInTypeMethod(self): + attrCl = CClass(self.mcl, "AC") + attrCl.delete() + try: + a = CAttribute() + a.type = attrCl + exceptionExpected_() + except CException as e: + eq_(e.value, "cannot access named element that has been deleted") + + def testSetCAttributeMethodToNone(self): + # setting the type/default to their default value (None) should work + a = CAttribute() + a.type = None + a.default = None + eq_(a.type, None) + eq_(a.default, None) + + def testTypeObjectAttributeClassIsNone(self): + c = CClass(self.mcl, "C", attributes = {"ac": None}) + ac = c.getAttribute("ac") + eq_(ac.default, None) + eq_(ac.type, None) + + def testDefaultObjectAttributeIsDeletedInConstructor(self): + attrCl = CClass(self.mcl, "AC") + defaultObj = CObject(attrCl) + defaultObj.delete() + try: + CClass(self.mcl, "C", attributes = {"ac": defaultObj}) + exceptionExpected_() + except CException as e: + eq_(e.value, "cannot access named element that has been deleted") + + def testDefaultObjectAttributeIsDeletedInDefaultMethod(self): + attrCl = CClass(self.mcl, "AC") + defaultObj = CObject(attrCl) + defaultObj.delete() + try: + a = CAttribute() + a.default = defaultObj + exceptionExpected_() + except CException as e: + eq_(e.value, "cannot access named element that has been deleted") + + +if __name__ == "__main__": + nose.main() + + + + diff --git a/tests/test_class_inheritance.py b/tests/test_class_inheritance.py new file mode 100644 index 0000000..ae7a430 --- /dev/null +++ b/tests/test_class_inheritance.py @@ -0,0 +1,395 @@ +import sys +sys.path.append("..") + +import re + +import nose +from nose.tools import ok_, eq_ +from testCommons import neq_, exceptionExpected_ +from parameterized import parameterized + +from codeableModels import CMetaclass, CStereotype, CClass, CObject, CAttribute, CException, CEnum + +class TestClassInheritance(): + def setUp(self): + self.mcl = CMetaclass("MCL") + + def testClassNoInheritance(self): + t = CClass(self.mcl, "T") + eq_(set(t.superclasses), set()) + eq_(set(t.subclasses), set()) + eq_(set(t.allSuperclasses), set()) + eq_(set(t.allSubclasses), set()) + + def testClassSuperclassesEmptyInput(self): + m1 = CClass(self.mcl, "M1", superclasses = []) + eq_(set(m1.superclasses), set()) + eq_(set(m1.subclasses), set()) + + def testClassSuperclassesNoneInput(self): + m1 = CClass(self.mcl, "M1", superclasses = None) + eq_(set(m1.superclasses), set()) + eq_(set(m1.subclasses), set()) + + def testClassSimpleInheritance(self): + t = CClass(self.mcl, "T") + m1 = CClass(self.mcl, "M1", superclasses = t) + m2 = CClass(self.mcl, "M2", superclasses = t) + b1 = CClass(self.mcl, "B1", superclasses = m1) + b2 = CClass(self.mcl, "B2", superclasses = m1) + b3 = CClass(self.mcl, "B3", superclasses = t) + + eq_(set(t.superclasses), set()) + eq_(set(t.subclasses), set([m1, m2, b3])) + eq_(set(t.allSuperclasses), set()) + eq_(set(t.allSubclasses), set([m1, m2, b1, b2, b3])) + + eq_(set(m1.superclasses), set([t])) + eq_(set(m1.subclasses), set([b1, b2])) + eq_(set(m1.allSuperclasses), set([t])) + eq_(set(m1.allSubclasses), set([b1, b2])) + + eq_(set(m2.superclasses), set([t])) + eq_(set(m2.subclasses), set()) + eq_(set(m2.allSuperclasses), set([t])) + eq_(set(m2.allSubclasses), set()) + + eq_(set(b1.superclasses), set([m1])) + eq_(set(b1.subclasses), set()) + eq_(set(b1.allSuperclasses), set([t, m1])) + eq_(set(b1.allSubclasses), set()) + + eq_(set(b2.superclasses), set([m1])) + eq_(set(b2.subclasses), set()) + eq_(set(b2.allSuperclasses), set([t, m1])) + eq_(set(b2.allSubclasses), set()) + + eq_(set(b3.superclasses), set([t])) + eq_(set(b3.subclasses), set()) + eq_(set(b3.allSuperclasses), set([t])) + eq_(set(b3.allSubclasses), set()) + + def testClassInheritanceDoubleAssignment(self): + t = CClass(self.mcl, "T") + try: + CClass(self.mcl, "C1", superclasses = [t, t]) + exceptionExpected_() + except CException as e: + eq_("'T' is already a superclass of 'C1'", e.value) + c1 = self.mcl.getClass("C1") + eq_(c1.metaclass, self.mcl) + eq_(c1.name, "C1") + eq_(t.name, "T") + eq_(set(c1.superclasses), set([t])) + + def testClassInheritanceDeleteTopClass(self): + t = CClass(self.mcl, "T") + m1 = CClass(self.mcl, "M1", superclasses = [t]) + m2 = CClass(self.mcl, "M2", superclasses = [t]) + b1 = CClass(self.mcl, "B1", superclasses = [m1]) + b2 = CClass(self.mcl, "B2", superclasses = [m1]) + b3 = CClass(self.mcl, "B3", superclasses = [t]) + + t.delete() + + eq_(t.name, None) + eq_(set(t.superclasses), set()) + eq_(set(t.subclasses), set()) + eq_(set(t.allSuperclasses), set()) + eq_(set(t.allSubclasses), set()) + + eq_(set(m1.superclasses), set()) + eq_(set(m1.subclasses), set([b1, b2])) + eq_(set(m1.allSuperclasses), set()) + eq_(set(m1.allSubclasses), set([b1, b2])) + + eq_(set(m2.superclasses), set()) + eq_(set(m2.subclasses), set()) + eq_(set(m2.allSuperclasses), set()) + eq_(set(m2.allSubclasses), set()) + + eq_(set(b1.superclasses), set([m1])) + eq_(set(b1.subclasses), set()) + eq_(set(b1.allSuperclasses), set([m1])) + eq_(set(b1.allSubclasses), set()) + + eq_(set(b2.superclasses), set([m1])) + eq_(set(b2.subclasses), set()) + eq_(set(b2.allSuperclasses), set([m1])) + eq_(set(b2.allSubclasses), set()) + + eq_(set(b3.superclasses), set()) + eq_(set(b3.subclasses), set()) + eq_(set(b3.allSuperclasses), set()) + eq_(set(b3.allSubclasses), set()) + + def testClassInheritanceDeleteInnerClass(self): + t = CClass(self.mcl, "T") + m1 = CClass(self.mcl, "M1", superclasses = [t]) + m2 = CClass(self.mcl, "M2", superclasses = [t]) + b1 = CClass(self.mcl, "B1", superclasses = [m1]) + b2 = CClass(self.mcl, "B2", superclasses = [m1]) + b3 = CClass(self.mcl, "B3", superclasses = [t]) + + m1.delete() + + eq_(set(t.superclasses), set()) + eq_(set(t.subclasses), set([m2, b3])) + eq_(set(t.allSuperclasses), set()) + eq_(set(t.allSubclasses), set([m2, b3])) + + eq_(set(m1.superclasses), set()) + eq_(set(m1.subclasses), set()) + eq_(set(m1.allSuperclasses), set()) + eq_(set(m1.allSubclasses), set()) + + eq_(set(m2.superclasses), set([t])) + eq_(set(m2.subclasses), set()) + eq_(set(m2.allSuperclasses), set([t])) + eq_(set(m2.allSubclasses), set()) + + eq_(set(b1.superclasses), set()) + eq_(set(b1.subclasses), set()) + eq_(set(b1.allSuperclasses), set()) + eq_(set(b1.allSubclasses), set()) + + eq_(set(b2.superclasses), set()) + eq_(set(b2.subclasses), set()) + eq_(set(b2.allSuperclasses), set()) + eq_(set(b2.allSubclasses), set()) + + eq_(set(b3.superclasses), set([t])) + eq_(set(b3.subclasses), set()) + eq_(set(b3.allSuperclasses), set([t])) + eq_(set(b3.allSubclasses), set()) + + def testClassSuperclassesReassignment(self): + t = CClass(self.mcl, "T") + m1 = CClass(self.mcl, "M1", superclasses = [t]) + m2 = CClass(self.mcl, "M2", superclasses = [t]) + b1 = CClass(self.mcl, "B1", superclasses = [m1]) + b2 = CClass(self.mcl, "B2", superclasses = [m1]) + b3 = CClass(self.mcl, "B3", superclasses = [t]) + + m1.superclasses = [] + b1.superclasses = [] + b2.superclasses = [] + + eq_(set(t.superclasses), set()) + eq_(set(t.subclasses), set([m2, b3])) + eq_(set(t.allSuperclasses), set()) + eq_(set(t.allSubclasses), set([m2, b3])) + + eq_(set(m1.superclasses), set()) + eq_(set(m1.subclasses), set()) + eq_(set(m1.allSuperclasses), set()) + eq_(set(m1.allSubclasses), set()) + + eq_(set(m2.superclasses), set([t])) + eq_(set(m2.subclasses), set()) + eq_(set(m2.allSuperclasses), set([t])) + eq_(set(m2.allSubclasses), set()) + + eq_(set(b1.superclasses), set()) + eq_(set(b1.subclasses), set()) + eq_(set(b1.allSuperclasses), set()) + eq_(set(b1.allSubclasses), set()) + + eq_(set(b2.superclasses), set()) + eq_(set(b2.subclasses), set()) + eq_(set(b2.allSuperclasses), set()) + eq_(set(b2.allSubclasses), set()) + + eq_(set(b3.superclasses), set([t])) + eq_(set(b3.subclasses), set()) + eq_(set(b3.allSuperclasses), set([t])) + eq_(set(b3.allSubclasses), set()) + + def testClassMultipleInheritance(self): + t1 = CClass(self.mcl, "T1") + t2 = CClass(self.mcl, "T2") + t3 = CClass(self.mcl, "T3") + m1 = CClass(self.mcl, "M1", superclasses = [t1, t3]) + m2 = CClass(self.mcl, "M2", superclasses = [t2, t3]) + b1 = CClass(self.mcl, "B1", superclasses = [m1]) + b2 = CClass(self.mcl, "B2", superclasses = [m1, m2]) + b3 = CClass(self.mcl, "B3", superclasses = [m2, m1]) + + eq_(set(t1.superclasses), set()) + eq_(set(t1.subclasses), set([m1])) + eq_(set(t1.allSuperclasses), set()) + eq_(set(t1.allSubclasses), set([m1, b1, b2, b3])) + + eq_(set(t2.superclasses), set()) + eq_(set(t2.subclasses), set([m2])) + eq_(set(t2.allSuperclasses), set()) + eq_(set(t2.allSubclasses), set([m2, b3, b2])) + + eq_(set(t3.superclasses), set()) + eq_(set(t3.subclasses), set([m2, m1])) + eq_(set(t3.allSuperclasses), set()) + eq_(set(t3.allSubclasses), set([m2, m1, b1, b2, b3])) + + eq_(set(m1.superclasses), set([t1, t3])) + eq_(set(m1.subclasses), set([b1, b2, b3])) + eq_(set(m1.allSuperclasses), set([t1, t3])) + eq_(set(m1.allSubclasses), set([b1, b2, b3])) + + eq_(set(m2.superclasses), set([t2, t3])) + eq_(set(m2.subclasses), set([b2, b3])) + eq_(set(m2.allSuperclasses), set([t2, t3])) + eq_(set(m2.allSubclasses), set([b2, b3])) + + eq_(set(b1.superclasses), set([m1])) + eq_(set(b1.subclasses), set()) + eq_(set(b1.allSuperclasses), set([m1, t1, t3])) + eq_(set(b1.allSubclasses), set()) + + eq_(set(b2.superclasses), set([m1, m2])) + eq_(set(b2.subclasses), set()) + eq_(set(b2.allSuperclasses), set([m1, m2, t1, t2, t3])) + eq_(set(b2.allSubclasses), set()) + + eq_(set(b3.superclasses), set([m1, m2])) + eq_(set(b3.subclasses), set()) + eq_(set(b3.allSuperclasses), set([m1, m2, t1, t2, t3])) + eq_(set(b3.allSubclasses), set()) + + def testClassAsWrongTypeOfSuperclass(self): + t = CClass(self.mcl, "C") + try: + CMetaclass("M", superclasses = [t]) + exceptionExpected_() + except CException as e: + ok_(re.match("^cannot add superclass 'C' to 'M': not of type([ $", e.value)) + try: + CStereotype("S", superclasses = [t]) + exceptionExpected_() + except CException as e: + ok_(re.match("^cannot add superclass 'C' to 'S': not of type([ $", e.value)) + + def testClassPathNoInheritance(self): + t = CClass(self.mcl) + o = CObject(t) + eq_(set(o.classPath), set([t])) + + def testClassPathSimpleInheritance(self): + t = CClass(self.mcl, "T") + m1 = CClass(self.mcl, "M1", superclasses = [t]) + m2 = CClass(self.mcl, "M2", superclasses = [t]) + b1 = CClass(self.mcl, "B1", superclasses = [m1]) + b2 = CClass(self.mcl, "B2", superclasses = [m1]) + b3 = CClass(self.mcl, "B3", superclasses = [t]) + eq_(CObject(b1).classPath, [b1, m1, t]) + eq_(CObject(b2).classPath, [b2, m1, t]) + eq_(CObject(b3).classPath, [b3, t]) + eq_(CObject(m1).classPath, [m1, t]) + eq_(CObject(m2).classPath, [m2, t]) + eq_(CObject(t).classPath, [t]) + + def testClassPathMultipleInheritance(self): + t = CClass(self.mcl, "T") + m1 = CClass(self.mcl, "M1", superclasses = [t]) + m2 = CClass(self.mcl, "M2", superclasses = [t]) + b1 = CClass(self.mcl, "B1", superclasses = [m1, m2]) + b2 = CClass(self.mcl, "B2", superclasses = [t, m1]) + b3 = CClass(self.mcl, "B3", superclasses = [t, m1, m2]) + eq_(CObject(b1).classPath, [b1, m1, t, m2]) + eq_(CObject(b2).classPath, [b2, t, m1]) + eq_(CObject(b3).classPath, [b3, t, m1, m2]) + eq_(CObject(m1).classPath, [m1, t]) + eq_(CObject(m2).classPath, [m2, t]) + eq_(CObject(t).classPath, [t]) + + def testClassInstanceOf(self): + a = CClass(self.mcl) + b = CClass(self.mcl, superclasses = [a]) + c = CClass(self.mcl) + o = CObject(b, "o") + + eq_(o.instanceOf(a), True) + eq_(o.instanceOf(b), True) + eq_(o.instanceOf(c), False) + + try: + o.instanceOf(o) + exceptionExpected_() + except CException as e: + eq_("'o' is not a class", e.value) + + o.delete() + eq_(o.instanceOf(a), False) + + def testClassGetAllInstances(self): + t = CClass(self.mcl, "T") + m1 = CClass(self.mcl, "M1", superclasses = [t]) + m2 = CClass(self.mcl, "M2", superclasses = [t]) + b1 = CClass(self.mcl, "B1", superclasses = [m1, m2]) + to1 = CObject(t) + to2 = CObject(t) + m1o1 = CObject(m1) + m1o2 = CObject(m1) + m2o = CObject(m2) + b1o1 = CObject(b1) + b1o2 = CObject(b1) + + eq_(set(t.objects), set([to1, to2])) + eq_(set(t.allObjects), set([to1, to2, m1o1, m1o2, b1o1, b1o2, m2o])) + eq_(set(m1.objects), set([m1o1, m1o2])) + eq_(set(m1.allObjects), set([m1o1, m1o2, b1o1, b1o2])) + eq_(set(m2.objects), set([m2o])) + eq_(set(m2.allObjects), set([m2o, b1o1, b1o2])) + eq_(set(b1.objects), set([b1o1, b1o2])) + eq_(set(b1.allObjects), set([b1o1, b1o2])) + + def testClassHasSuperclassHasSubclass(self): + c1 = CClass(self.mcl, "C1") + c2 = CClass(self.mcl, "C2", superclasses = [c1]) + c3 = CClass(self.mcl, "C3", superclasses = [c2]) + c4 = CClass(self.mcl, "C4", superclasses = [c2]) + c5 = CClass(self.mcl, "C5", superclasses = []) + + eq_(c1.hasSuperclass(c2), False) + eq_(c5.hasSuperclass(c2), False) + eq_(c1.hasSuperclass(None), False) + eq_(c5.hasSuperclass(None), False) + eq_(c2.hasSuperclass(c1), True) + eq_(c3.hasSuperclass(c2), True) + eq_(c3.hasSuperclass(c2), True) + eq_(c4.hasSuperclass(c2), True) + eq_(c3.hasSubclass(c2), False) + eq_(c3.hasSubclass(None), False) + eq_(c5.hasSubclass(c2), False) + eq_(c5.hasSubclass(None), False) + eq_(c1.hasSubclass(c3), True) + eq_(c1.hasSubclass(c2), True) + + def testClassUnknownNonPositionalArgument(self): + t = CClass(self.mcl, "T") + try: + CClass(self.mcl, "ST", superclass = t) + exceptionExpected_() + except CException as e: + eq_("unknown keyword argument 'superclass', should be one of: ['stereotypeInstances', 'attributes', 'superclasses', 'bundles']", e.value) + + def testSuperclassesThatAreDeleted(self): + c1 = CClass(self.mcl, "C1") + c1.delete() + try: + CClass(self.mcl, superclasses = [c1]) + exceptionExpected_() + except CException as e: + eq_(e.value, "cannot access named element that has been deleted") + + def testSuperclassesThatAreNone(self): + try: + CClass(self.mcl, "C", superclasses = [None]) + exceptionExpected_() + except CException as e: + ok_(e.value.startswith("cannot add superclass 'None' to 'C': not of type")) + +if __name__ == "__main__": + nose.main() + + + diff --git a/tests/test_class_links.py b/tests/test_class_links.py new file mode 100644 index 0000000..e1e242c --- /dev/null +++ b/tests/test_class_links.py @@ -0,0 +1,1166 @@ +import sys +sys.path.append("..") + +import re + +import nose +from nose.tools import ok_, eq_ +from testCommons import neq_, exceptionExpected_ +from parameterized import parameterized + +from codeableModels import CMetaclass, CClass, CObject, CAttribute, CException, CEnum, setLinks, addLinks, deleteLinks + +class TestClassLinks(): + def setUp(self): + self.m1 = CMetaclass("M1") + self.m2 = CMetaclass("M2") + self.mcl = CMetaclass("MCL") + + def testLinkMethodsWrongKeywordArgs(self): + c1 = CClass(self.m1, "C1") + try: + addLinks({c1:c1}, associationX = None) + exceptionExpected_() + except CException as e: + eq_(e.value, "unknown keywords argument") + try: + c1.addLinks(c1, associationX = None) + exceptionExpected_() + except CException as e: + eq_(e.value, "unknown keywords argument") + try: + setLinks({c1: c1}, associationX = None) + exceptionExpected_() + except CException as e: + eq_(e.value, "unknown keywords argument") + try: + c1.deleteLinks(c1, associationX = None) + exceptionExpected_() + except CException as e: + eq_(e.value, "unknown keywords argument") + try: + deleteLinks({c1: c1}, associationX = None) + exceptionExpected_() + except CException as e: + eq_(e.value, "unknown keywords argument") + try: + c1.getLinks(associationX = None) + exceptionExpected_() + except CException as e: + eq_(e.value, "unknown keywords argument") + try: + deleteLinks({c1:c1}, stereotypeInstances = None) + exceptionExpected_() + except CException as e: + eq_(e.value, "unknown keywords argument") + + def testSetOneToOneLink(self): + self.m1.association(self.m2, name = "l", multiplicity = "1") + + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m2, "c2") + c3 = CClass(self.m2, "c3") + + eq_(c1.links, []) + + setLinks({c1: c2}) + eq_(c1.links, [c2]) + eq_(c2.links, [c1]) + + setLinks({c1: c3}) + eq_(c1.links, [c3]) + eq_(c2.links, []) + eq_(c3.links, [c1]) + + def testAddOneToOneLink(self): + self.m1.association(self.m2, "l: 1 -> [target] 0..1") + + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m2, "c2") + c3 = CClass(self.m2, "c3") + + eq_(c1.links, []) + + addLinks({c1: c3}) + eq_(c1.links, [c3]) + eq_(c3.links, [c1]) + + setLinks({c1: []}, roleName = "target") + eq_(c1.links, []) + + c1.addLinks(c2) + eq_(c1.links, [c2]) + eq_(c2.links, [c1]) + + try: + addLinks({c1: c3}) + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'c1' have wrong multiplicity '2': should be '0..1'") + eq_(c1.links, [c2]) + eq_(c2.links, [c1]) + eq_(c3.links, []) + + def testWrongTypesAddLinks(self): + self.m1.association(self.m2, name = "l", multiplicity = "1") + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m2, "c2") + try: + addLinks({c1: self.mcl}) + exceptionExpected_() + except CException as e: + eq_(e.value, "link target 'MCL' is neither an object nor a class") + try: + c1.addLinks([c2, self.mcl]) + exceptionExpected_() + except CException as e: + eq_(e.value, "link target 'MCL' is neither an object nor a class") + + def testWrongTypesSetLinks(self): + self.m1.association(self.m2, name = "l", multiplicity = "1") + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m2, "c2") + try: + setLinks({c1: self.mcl}) + exceptionExpected_() + except CException as e: + eq_(e.value, "link target 'MCL' is neither an object nor a class") + try: + setLinks({c1: [c2, self.mcl]}) + exceptionExpected_() + except CException as e: + eq_(e.value, "link target 'MCL' is neither an object nor a class") + try: + setLinks({c1: [c2, None]}) + exceptionExpected_() + except CException as e: + eq_(e.value, "link target 'None' is neither an object nor a class") + try: + setLinks({self.mcl: c2}) + exceptionExpected_() + except CException as e: + eq_(e.value, "link source 'MCL' is neither an object nor a class") + try: + setLinks({None: c2}) + exceptionExpected_() + except CException as e: + eq_(e.value, "link should not contain an empty source") + + def testWrongFormatSetLinks(self): + self.m1.association(self.m2, name = "l", multiplicity = "1") + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m2, "c2") + try: + setLinks([c1, c2]) + exceptionExpected_() + except CException as e: + eq_(e.value, "link definitions should be of the form {: , ..., : }") + + def testRemoveOneToOneLink(self): + a = self.m1.association(self.m2, "l: 1 -> [c2] 0..1") + + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m2, "c2") + c3 = CClass(self.m1, "c3") + c4 = CClass(self.m2, "c4") + + links = setLinks({c1: c2, c3: c4}) + eq_(c1.links, [c2]) + eq_(c2.links, [c1]) + eq_(c3.links, [c4]) + eq_(c4.links, [c3]) + eq_(c1.linkObjects, [links[0]]) + eq_(c2.linkObjects, [links[0]]) + eq_(c3.linkObjects, [links[1]]) + eq_(c4.linkObjects, [links[1]]) + + try: + links = setLinks({c1: None}) + exceptionExpected_() + except CException as e: + eq_(e.value, "matching association not found for source 'c1' and targets '[]'") + + setLinks({c1: None}, association = a) + eq_(c1.links, []) + eq_(c2.links, []) + eq_(c3.links, [c4]) + eq_(c4.links, [c3]) + eq_(c1.linkObjects, []) + eq_(c2.linkObjects, []) + eq_(c3.linkObjects, [links[1]]) + eq_(c4.linkObjects, [links[1]]) + + def testSetLinksOneToNLink(self): + self.m1.association(self.m2, name = "l") + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m2, "c2") + c3 = CClass(self.m2, "c3") + + setLinks({c1: [c2, c3]}) + eq_(c1.links, [c2, c3]) + eq_(c2.links, [c1]) + eq_(c3.links, [c1]) + setLinks({c1: c2}) + eq_(c1.links, [c2]) + eq_(c2.links, [c1]) + eq_(c3.links, []) + setLinks({c3: c1, c2: c1}) + eq_(c1.links, [c3, c2]) + eq_(c2.links, [c1]) + eq_(c3.links, [c1]) + + def testAddLinksOneToNLink(self): + self.m1.association(self.m2, name = "l") + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m2, "c2") + c3 = CClass(self.m2, "c3") + c4 = CClass(self.m2, "c4") + c5 = CClass(self.m2, "c5") + c6 = CClass(self.m2, "c6") + + addLinks({c1: [c2, c3]}) + eq_(c1.links, [c2, c3]) + eq_(c2.links, [c1]) + eq_(c3.links, [c1]) + addLinks({c1: c4}) + eq_(c1.links, [c2, c3, c4]) + eq_(c2.links, [c1]) + eq_(c3.links, [c1]) + eq_(c4.links, [c1]) + c1.addLinks([c5, c6]) + eq_(c1.links, [c2, c3, c4, c5, c6]) + eq_(c2.links, [c1]) + eq_(c3.links, [c1]) + eq_(c4.links, [c1]) + eq_(c5.links, [c1]) + eq_(c6.links, [c1]) + + def testRemoveOneToNLink(self): + a = self.m1.association(self.m2, name = "l", multiplicity = "*") + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m2, "c2") + c3 = CClass(self.m2, "c3") + setLinks({c1: [c2, c3]}) + setLinks({c1: c2}) + eq_(c1.links, [c2]) + eq_(c2.links, [c1]) + eq_(c3.links, []) + try: + setLinks({c1: []}) + exceptionExpected_() + except CException as e: + eq_(e.value, "matching association not found for source 'c1' and targets '[]'") + setLinks({c1: []}, association = a) + eq_(c1.links, []) + eq_(c2.links, []) + eq_(c3.links, []) + + def testNToNLink(self): + a = self.m1.association(self.m2, name = "l", sourceMultiplicity = "*") + c1a = CClass(self.m1, "c1a") + c1b = CClass(self.m1, "c1b") + c1c = CClass(self.m1, "c1c") + c2a = CClass(self.m2, "c2a") + c2b = CClass(self.m2, "c2b") + + setLinks({c1a: [c2a, c2b], c1b: [c2a], c1c: [c2b]}) + + eq_(c1a.links, [c2a, c2b]) + eq_(c1b.links, [c2a]) + eq_(c1c.links, [c2b]) + eq_(c2a.links, [c1a, c1b]) + eq_(c2b.links, [c1a, c1c]) + + setLinks({c2a: [c1a, c1b]}) + try: + setLinks({c2b: []}) + exceptionExpected_() + except CException as e: + eq_(e.value, "matching association not found for source 'c2b' and targets '[]'") + setLinks({c2b: []}, association = a) + + + eq_(c1a.links, [c2a]) + eq_(c1b.links, [c2a]) + eq_(c1c.links, []) + eq_(c2a.links, [c1a, c1b]) + eq_(c2b.links, []) + + def testRemoveNToNLink(self): + self.m1.association(self.m2, name = "l", sourceMultiplicity = "*", multiplicity = "*") + c1 = CClass(self.m1, "c2") + c2 = CClass(self.m2, "c2") + c3 = CClass(self.m2, "c3") + c4 = CClass(self.m1, "c4") + setLinks({c1: [c2, c3], c4: c2}) + setLinks({c1: c2, c4: [c3, c2]}) + eq_(c1.links, [c2]) + eq_(c2.links, [c1, c4]) + eq_(c3.links, [c4]) + eq_(c4.links, [c3, c2]) + + def testNToNSetSelfLink(self): + self.m1.association(self.m1, name = "a", sourceMultiplicity = "*", multiplicity = "*", sourceRoleName = "super", roleName = "sub") + + top = CClass(self.m1, "Top") + mid1 = CClass(self.m1, "Mid1") + mid2 = CClass(self.m1, "Mid2") + mid3 = CClass(self.m1, "Mid3") + bottom1 = CClass(self.m1, "Bottom1") + bottom2 = CClass(self.m1, "Bottom2") + + setLinks({top: [mid1, mid2, mid3]}, roleName = "sub") + mid1.addLinks([bottom1, bottom2], roleName = "sub") + + eq_(top.links, [mid1, mid2, mid3]) + eq_(mid1.links, [top, bottom1, bottom2]) + eq_(mid2.links, [top]) + eq_(mid3.links, [top]) + eq_(bottom1.links, [mid1]) + eq_(bottom2.links, [mid1]) + + eq_(top.getLinks(roleName = "sub"), [mid1, mid2, mid3]) + eq_(mid1.getLinks(roleName = "sub"), [bottom1, bottom2]) + eq_(mid2.getLinks(roleName = "sub"), []) + eq_(mid3.getLinks(roleName = "sub"), []) + eq_(bottom1.getLinks(roleName = "sub"), []) + eq_(bottom2.getLinks(roleName = "sub"), []) + + eq_(top.getLinks(roleName = "super"), []) + eq_(mid1.getLinks(roleName = "super"), [top]) + eq_(mid2.getLinks(roleName = "super"), [top]) + eq_(mid3.getLinks(roleName = "super"), [top]) + eq_(bottom1.getLinks(roleName = "super"), [mid1]) + eq_(bottom2.getLinks(roleName = "super"), [mid1]) + + def testNToNSetSelfLinkDeleteLinks(self): + self.m1.association(self.m1, name = "a", sourceMultiplicity = "*", multiplicity = "*", sourceRoleName = "super", roleName = "sub") + + top = CClass(self.m1, "Top") + mid1 = CClass(self.m1, "Mid1") + mid2 = CClass(self.m1, "Mid2") + mid3 = CClass(self.m1, "Mid3") + bottom1 = CClass(self.m1, "Bottom1") + bottom2 = CClass(self.m1, "Bottom2") + + setLinks({top: [mid1, mid2, mid3], mid1: [bottom1, bottom2]}, roleName = "sub") + # delete links + setLinks({top: []}, roleName = "sub") + eq_(top.links, []) + eq_(mid1.links, [bottom1, bottom2]) + # change links + setLinks({mid1: top, mid3: top, bottom1: mid1, bottom2: mid1}, roleName = "super") + + eq_(top.links, [mid1, mid3]) + eq_(mid1.links, [top, bottom1, bottom2]) + eq_(mid2.links, []) + eq_(mid3.links, [top]) + eq_(bottom1.links, [mid1]) + eq_(bottom2.links, [mid1]) + + eq_(top.getLinks(roleName = "sub"), [mid1, mid3]) + eq_(mid1.getLinks(roleName = "sub"), [bottom1, bottom2]) + eq_(mid2.getLinks(roleName = "sub"), []) + eq_(mid3.getLinks(roleName = "sub"), []) + eq_(bottom1.getLinks(roleName = "sub"), []) + eq_(bottom2.getLinks(roleName = "sub"), []) + + eq_(top.getLinks(roleName = "super"), []) + eq_(mid1.getLinks(roleName = "super"), [top]) + eq_(mid2.getLinks(roleName = "super"), []) + eq_(mid3.getLinks(roleName = "super"), [top]) + eq_(bottom1.getLinks(roleName = "super"), [mid1]) + eq_(bottom2.getLinks(roleName = "super"), [mid1]) + + def testIncompatibleClassifier(self): + self.m1.association(self.m2, name = "l", multiplicity = "*") + cl = CClass(self.mcl, "CLX") + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m2, "c2") + c3 = CObject(cl, "c3") + try: + setLinks({c1: [c2, c3]}) + exceptionExpected_() + except CException as e: + eq_(e.value, "link target 'c3' is an object, but source is an class") + try: + setLinks({c1: c3}) + exceptionExpected_() + except CException as e: + eq_(e.value, "link target 'c3' is an object, but source is an class") + + def testDuplicateAssignment(self): + a = self.m1.association(self.m2, "l: *->*") + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m2, "c2") + try: + setLinks({c1: [c2, c2]}) + exceptionExpected_() + except CException as e: + eq_(e.value, "trying to link the same link twice 'c1 -> c2'' twice for the same association") + eq_(c1.getLinks(), []) + eq_(c2.getLinks(), []) + + b = self.m1.association(self.m2, "l: *->*") + c1.addLinks(c2, association = a) + c1.addLinks(c2, association = b) + eq_(c1.getLinks(), [c2, c2]) + eq_(c2.getLinks(), [c1, c1]) + + def testNonExistingRoleName(self): + self.m1.association(self.m1, roleName = "next", sourceRoleName = "prior", + sourceMultiplicity = "1", multiplicity = "1") + c1 = CClass(self.m1, "c1") + try: + setLinks({c1 : c1}, roleName = "target") + exceptionExpected_() + except CException as e: + eq_(e.value, "matching association not found for source 'c1' and targets '['c1']'") + + def testLinkAssociationAmbiguous(self): + self.m1.association(self.m2, name = "a1", roleName = "c2", multiplicity = "*") + self.m1.association(self.m2, name = "a2", roleName = "c2", multiplicity = "*") + + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m2, "c2") + try: + setLinks({c1:c2}) + exceptionExpected_() + except CException as e: + eq_(e.value, "link specification ambiguous, multiple matching associations found for source 'c1' and targets '['c2']'") + try: + setLinks({c1:c2}, roleName = "c2") + exceptionExpected_() + except CException as e: + eq_(e.value, "link specification ambiguous, multiple matching associations found for source 'c1' and targets '['c2']'") + + def testLinkAndGetLinksByAssociation(self): + a1 = self.m1.association(self.m2, name = "a1", multiplicity = "*") + a2 = self.m1.association(self.m2, name = "a2", multiplicity = "*") + + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m2, "c2") + c3 = CClass(self.m2, "c3") + + setLinks({c1:c2}, association = a1) + setLinks({c1:[c2, c3]}, association = a2) + + eq_(c1.getLinks(), [c2, c2, c3]) + eq_(c1.links, [c2, c2, c3]) + + eq_(c1.getLinks(association = a1), [c2]) + eq_(c1.getLinks(association = a2), [c2, c3]) + + def testLinkWithInheritanceInClassifierTargets(self): + subCl = CMetaclass(superclasses = self.m2) + a1 = self.m1.association(subCl, name = "a1", multiplicity = "*") + a2 = self.m1.association(self.m2, name = "a2", multiplicity = "*") + + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m1, "c2") + c_sub_1 = CClass(subCl, "c_sub_1") + c_sub_2 = CClass(subCl, "c_sub_2") + c_super_1 = CClass(self.m2, "c_super_1") + c_super_2 = CClass(self.m2, "c_super_2") + try: + # ambiguous, list works for both associations + setLinks({c1: [c_sub_1, c_sub_2]}) + exceptionExpected_() + except CException as e: + eq_(e.value, "link specification ambiguous, multiple matching associations found for source 'c1' and targets '['c_sub_1', 'c_sub_2']'") + setLinks({c1: [c_sub_1, c_sub_2]}, association = a1) + setLinks({c1: [c_sub_1]}, association = a2) + setLinks({c2: [c_super_1, c_super_2]}) + + eq_(c1.links, [c_sub_1, c_sub_2, c_sub_1]) + eq_(c1.getLinks(), [c_sub_1, c_sub_2, c_sub_1]) + eq_(c2.getLinks(), [c_super_1, c_super_2]) + eq_(c1.getLinks(association = a1), [c_sub_1, c_sub_2]) + eq_(c1.getLinks(association = a2), [c_sub_1]) + eq_(c2.getLinks(association = a1), []) + eq_(c2.getLinks(association = a2), [c_super_1, c_super_2]) + + # this mixed list is applicable only for a2 + setLinks({c2: [c_sub_1, c_super_1]}) + eq_(c2.getLinks(association = a1), []) + eq_(c2.getLinks(association = a2), [c_sub_1, c_super_1]) + + def testLinkWithInheritanceInClassifierTargets_UsingRoleNames(self): + subCl = CMetaclass(superclasses = self.m2) + a1 = self.m1.association(subCl, "a1: * -> [subCl] *") + a2 = self.m1.association(self.m2, "a2: * -> [c2] *") + + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m1, "c2") + c_sub_1 = CClass(subCl, "c_sub_1") + c_sub_2 = CClass(subCl, "c_sub_2") + c_super_1 = CClass(self.m2, "c_super_1") + c_super_2 = CClass(self.m2, "c_super_2") + setLinks({c1: [c_sub_1, c_sub_2]}, roleName = "subCl") + setLinks({c1: [c_sub_1]}, roleName = "c2") + setLinks({c2: [c_super_1, c_super_2]}) + + eq_(c1.links, [c_sub_1, c_sub_2, c_sub_1]) + eq_(c1.getLinks(), [c_sub_1, c_sub_2, c_sub_1]) + eq_(c2.getLinks(), [c_super_1, c_super_2]) + eq_(c1.getLinks(association = a1), [c_sub_1, c_sub_2]) + eq_(c1.getLinks(association = a2), [c_sub_1]) + eq_(c2.getLinks(association = a1), []) + eq_(c2.getLinks(association = a2), [c_super_1, c_super_2]) + eq_(c1.getLinks(roleName = "subCl"), [c_sub_1, c_sub_2]) + eq_(c1.getLinks(roleName = "c2"), [c_sub_1]) + eq_(c2.getLinks(roleName = "subCl"), []) + eq_(c2.getLinks(roleName = "c2"), [c_super_1, c_super_2]) + + def testLinkDeleteAssociation(self): + a = self.m1.association(self.m2, name = "l", sourceMultiplicity = "*", multiplicity = "*") + c1 = CClass(self.m1, "c2") + c2 = CClass(self.m2, "c2") + c3 = CClass(self.m2, "c3") + c4 = CClass(self.m1, "c4") + setLinks({c1: [c2, c3]}) + setLinks({c4: [c2]}) + setLinks({c1: [c2]}) + setLinks({c4: [c3, c2]}) + a.delete() + eq_(c1.links, []) + eq_(c2.links, []) + eq_(c3.links, []) + eq_(c4.links, []) + try: + setLinks({c1: [c2, c3]}) + exceptionExpected_() + except CException as e: + eq_(e.value, "matching association not found for source 'c2' and targets '['c2', 'c3']'") + + def testOneToOneLinkMultiplicity(self): + a = self.m1.association(self.m2, name = "l", multiplicity = "1", sourceMultiplicity = "1..1") + + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m2, "c2") + c3 = CClass(self.m2, "c3") + c4 = CClass(self.m1, "c4") + + try: + setLinks({c1: []}, association = a) + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'c1' have wrong multiplicity '0': should be '1'") + try: + setLinks({c1: [c2, c3]}) + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'c1' have wrong multiplicity '2': should be '1'") + + try: + setLinks({c2: []}, association = a) + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'c2' have wrong multiplicity '0': should be '1..1'") + try: + setLinks({c2: [c1, c4]}) + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'c2' have wrong multiplicity '2': should be '1..1'") + + def testOneToNLinkMultiplicity(self): + a = self.m1.association(self.m2, name = "l", sourceMultiplicity = "1", multiplicity = "1..*") + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m2, "c2") + c3 = CClass(self.m2, "c3") + c4 = CClass(self.m1, "c4") + + try: + setLinks({c1: []}, association = a) + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'c1' have wrong multiplicity '0': should be '1..*'") + + setLinks({c1: [c2, c3]}) + eq_(c1.getLinks(association = a), [c2, c3]) + + try: + setLinks({c2: []}, association = a) + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'c2' have wrong multiplicity '0': should be '1'") + try: + setLinks({c2: [c1, c4]}) + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'c2' have wrong multiplicity '2': should be '1'") + + + def testSpecificNToNLinkMultiplicity(self): + a = self.m1.association(self.m2, name = "l", sourceMultiplicity = "1..2", multiplicity = "2") + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m2, "c2") + c3 = CClass(self.m2, "c3") + c4 = CClass(self.m1, "c4") + c5 = CClass(self.m1, "c5") + c6 = CClass(self.m2, "c6") + + try: + setLinks({c1: []}, association = a) + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'c1' have wrong multiplicity '0': should be '2'") + try: + setLinks({c1: [c2]}, association = a) + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'c1' have wrong multiplicity '1': should be '2'") + try: + setLinks({c1: [c2, c3, c6]}, association = a) + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'c1' have wrong multiplicity '3': should be '2'") + + setLinks({c1: [c2, c3]}) + eq_(c1.getLinks(association = a), [c2, c3]) + setLinks({c2: [c1, c4], c1: c3, c4: c3}) + eq_(c2.getLinks(association = a), [c1, c4]) + + try: + setLinks({c2: []}, association = a) + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'c2' have wrong multiplicity '0': should be '1..2'") + try: + setLinks({c2: [c1, c4, c5]}) + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'c2' have wrong multiplicity '3': should be '1..2'") + + def testGetLinkObjects(self): + c1Sub = CMetaclass("C1Sub", superclasses = self.m1) + c2Sub = CMetaclass("C2Sub", superclasses = self.m2) + a1 = self.m1.association(self.m2, roleName = "c2", sourceRoleName = "c1", + sourceMultiplicity = "*", multiplicity = "*") + a2 = self.m1.association(self.m1, roleName = "next", sourceRoleName = "prior", + sourceMultiplicity = "1", multiplicity = "0..1") + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m2, "c2") + c1Sub = CClass(c1Sub, "c1Sub") + c2Sub = CClass(c2Sub, "c2Sub") + + linkObjects1 = setLinks({c1: c2}) + eq_(linkObjects1, c1.linkObjects) + link1 = c1.linkObjects[0] + link2 = [o for o in c1.linkObjects if o.association == a1][0] + eq_(link1, link2) + eq_(link1.association, a1) + eq_(link1.source, c1) + eq_(link1.target, c2) + + linkObjects2 = setLinks({c1: c2Sub}) + eq_(linkObjects2, c1.linkObjects) + eq_(len(c1.linkObjects), 1) + link1 = c1.linkObjects[0] + link2 = [o for o in c1.linkObjects if o.association == a1][0] + eq_(link1, link2) + eq_(link1.association, a1) + eq_(link1.source, c1) + eq_(link1.target, c2Sub) + + linkObjects3 = setLinks({c1: c2}) + eq_(linkObjects3, c1.linkObjects) + eq_(len(c1.linkObjects), 1) + link1 = c1.linkObjects[0] + link2 = [o for o in c1.linkObjects if o.association == a1][0] + eq_(link1, link2) + eq_(link1.association, a1) + eq_(link1.source, c1) + eq_(link1.target, c2) + + linkObjects4 = setLinks({c1: c1}, roleName = "next") + eq_(linkObjects3 + linkObjects4, c1.linkObjects) + eq_(len(c1.linkObjects), 2) + link1 = c1.linkObjects[1] + link2 = [o for o in c1.linkObjects if o.association == a2][0] + eq_(link1, link2) + eq_(link1.association, a2) + eq_(link1.source, c1) + eq_(link1.target, c1) + + linkObjects5 = setLinks({c1: c1Sub}, roleName = "next") + eq_(linkObjects3 + linkObjects5, c1.linkObjects) + eq_(len(c1.linkObjects), 2) + link1 = c1.linkObjects[1] + link2 = [o for o in c1.linkObjects if o.association == a2][0] + eq_(link1, link2) + eq_(link1.association, a2) + eq_(link1.source, c1) + eq_(link1.target, c1Sub) + + setLinks({c1: c1}, roleName = "next") + eq_(len(c1.linkObjects), 2) + link1 = c1.linkObjects[1] + link2 = [o for o in c1.linkObjects if o.association == a2][0] + eq_(link1, link2) + eq_(link1.association, a2) + eq_(link1.source, c1) + eq_(link1.target, c1) + + setLinks({c1: []}, association = a1) + setLinks({c1: []}, association = a2) + eq_(len(c1.linkObjects), 0) + + setLinks({c1Sub: c1}, roleName = "next") + eq_(len(c1Sub.linkObjects), 1) + link1 = c1Sub.linkObjects[0] + link2 = [o for o in c1Sub.linkObjects if o.association == a2][0] + eq_(link1, link2) + eq_(link1.association, a2) + eq_(link1.source, c1Sub) + eq_(link1.target, c1) + + def testGetLinkObjectsSelfLink(self): + a1 = self.m1.association(self.m1, roleName = "to", sourceRoleName = "from", + sourceMultiplicity = "*", multiplicity = "*") + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m1, "c2") + c3 = CClass(self.m1, "c3") + c4 = CClass(self.m1, "c4") + + setLinks({c1: [c2, c3, c1]}) + addLinks({c4: [c1, c3]}) + link1 = c1.linkObjects[0] + link2 = [o for o in c1.linkObjects if o.association == a1][0] + link3 = [o for o in c1.linkObjects if o.roleName == "to"][0] + link4 = [o for o in c1.linkObjects if o.sourceRoleName == "from"][0] + eq_(link1, link2) + eq_(link1, link3) + eq_(link1, link4) + eq_(link1.association, a1) + eq_(link1.source, c1) + eq_(link1.target, c2) + + eq_(len(c1.linkObjects), 4) + eq_(len(c2.linkObjects), 1) + eq_(len(c3.linkObjects), 2) + eq_(len(c4.linkObjects), 2) + + def testAddLinks(self): + a1 = self.m1.association(self.m2, "1 -> [role1] *") + a2 = self.m1.association(self.m2, "* -> [role2] *") + a3 = self.m1.association(self.m2, "1 -> [role3] 1") + + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m2, "c2") + c3 = CClass(self.m2, "c3") + c4 = CClass(self.m2, "c4") + + addLinks({c1: c2}, roleName = "role1") + eq_(c1.getLinks(roleName = "role1"), [c2]) + addLinks({c1: [c3, c4]}, roleName = "role1") + c1.getLinks(roleName = "role1") + eq_(c1.getLinks(roleName = "role1"), [c2, c3, c4]) + + c1.addLinks(c2, roleName = "role2") + eq_(c1.getLinks(roleName = "role2"), [c2]) + c1.addLinks([c3, c4], roleName = "role2") + c1.getLinks(roleName = "role2") + eq_(c1.getLinks(roleName = "role2"), [c2, c3, c4]) + + c1.addLinks(c2, roleName = "role3") + eq_(c1.getLinks(roleName = "role3"), [c2]) + try: + addLinks({c1: [c3, c4]}, roleName = "role3") + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'c1' have wrong multiplicity '3': should be '1'") + + try: + addLinks({c1: [c3]}, roleName = "role3") + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'c1' have wrong multiplicity '2': should be '1'") + eq_(c1.getLinks(roleName = "role3"), [c2]) + + + def testLinkSourceMultiplicity(self): + a1 = self.m1.association(self.m2, "[sourceRole1] 1 -> [role1] *") + a2 = self.m1.association(self.m2, "[sourceRole2] 1 -> [role2] 1") + + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m1, "c2") + c3 = CClass(self.m2, "c3") + c4 = CClass(self.m2, "c4") + c5 = CClass(self.m2, "c5") + + setLinks({c1: c3}, roleName = "role1") + setLinks({c2: c3}, roleName = "role1") + + eq_(c3.getLinks(roleName = "sourceRole1"), [c2]) + + def testAddLinksSourceMultiplicity(self): + a1 = self.m1.association(self.m2, "[sourceRole1] 1 -> [role1] *") + a2 = self.m1.association(self.m2, "[sourceRole2] 1 -> [role2] 1") + + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m1, "c2") + c3 = CClass(self.m2, "c3") + c4 = CClass(self.m2, "c4") + c5 = CClass(self.m2, "c5") + c6 = CClass(self.m2, "c6") + + addLinks({c2: c3}, roleName = "role1") + addLinks({c2: c4}, roleName = "role1") + + eq_(c3.getLinks(roleName = "sourceRole1"), [c2]) + + addLinks({c2: c5}, roleName = "role1") + eq_(c2.getLinks(roleName = "role1"), [c3, c4, c5]) + + try: + addLinks({c1: [c4]}, roleName = "role1") + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'c4' have wrong multiplicity '2': should be '1'") + + addLinks({c1: c6}, roleName = "role2") + eq_(c1.getLinks(roleName = "role2"), [c6]) + try: + addLinks({c1: [c3, c4]}, roleName = "role2") + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'c1' have wrong multiplicity '3': should be '1'") + eq_(c1.getLinks(roleName = "role2"), [c6]) + + def testSetLinksMultipleLinksInDefinition(self): + a1 = self.m1.association(self.m2, "[sourceRole1] * -> [role1] *") + + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m1, "c2") + c3 = CClass(self.m1, "c3") + c4 = CClass(self.m2, "c4") + c5 = CClass(self.m2, "c5") + + setLinks({c1: c4, c2: [c4], c5: [c1, c2, c3]}) + eq_(c1.getLinks(), [c4, c5]) + eq_(c2.getLinks(), [c4, c5]) + eq_(c3.getLinks(), [c5]) + eq_(c4.getLinks(), [c1, c2]) + eq_(c5.getLinks(), [c1, c2, c3]) + + def testAddLinksMultipleLinksInDefinition(self): + a1 = self.m1.association(self.m2, "[sourceRole1] * -> [role1] *") + + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m1, "c2") + c3 = CClass(self.m1, "c3") + c4 = CClass(self.m2, "c4") + c5 = CClass(self.m2, "c5") + + addLinks({c1: c4, c2: [c4], c5: [c1, c2, c3]}) + eq_(c1.getLinks(), [c4, c5]) + eq_(c2.getLinks(), [c4, c5]) + eq_(c3.getLinks(), [c5]) + eq_(c4.getLinks(), [c1, c2]) + eq_(c5.getLinks(), [c1, c2, c3]) + + + def testWrongTypesDeleteLinks(self): + self.m1.association(self.m2, name = "l", multiplicity = "1") + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m2, "c2") + try: + deleteLinks(c1) + exceptionExpected_() + except CException as e: + eq_(e.value, "link definitions should be of the form {: , ..., : }") + try: + deleteLinks({c1: self.mcl}) + exceptionExpected_() + except CException as e: + eq_(e.value, "link target 'MCL' is neither an object nor a class") + try: + deleteLinks({c1: [c2, self.mcl]}) + exceptionExpected_() + except CException as e: + eq_(e.value, "link target 'MCL' is neither an object nor a class") + try: + deleteLinks({c1: [c2, None]}) + exceptionExpected_() + except CException as e: + eq_(e.value, "link target 'None' is neither an object nor a class") + try: + deleteLinks({self.mcl: c2}) + exceptionExpected_() + except CException as e: + eq_(e.value, "link source 'MCL' is neither an object nor a class") + try: + deleteLinks({None: c2}) + exceptionExpected_() + except CException as e: + eq_(e.value, "link should not contain an empty source") + + def testDeleteOneToOneLink(self): + a = self.m1.association(self.m2, "l: 1 -> [c2] 0..1") + + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m2, "c2") + c3 = CClass(self.m1, "c3") + c4 = CClass(self.m2, "c4") + + links = addLinks({c1: c2, c3: c4}) + c1.deleteLinks(c2) + eq_(c1.links, []) + eq_(c2.links, []) + eq_(c3.links, [c4]) + eq_(c4.links, [c3]) + eq_(c1.linkObjects, []) + eq_(c2.linkObjects, []) + eq_(c3.linkObjects, [links[1]]) + eq_(c4.linkObjects, [links[1]]) + deleteLinks({c3: c4}) + eq_(c1.links, []) + eq_(c2.links, []) + eq_(c3.links, []) + eq_(c4.links, []) + eq_(c1.linkObjects, []) + eq_(c2.linkObjects, []) + eq_(c3.linkObjects, []) + eq_(c4.linkObjects, []) + + def testDeleteOneToOneLinkWrongMultiplicity(self): + a = self.m1.association(self.m2, "l: 1 -> [c2] 1") + + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m2, "c2") + addLinks({c1: c2}) + try: + c1.deleteLinks(c2) + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'c1' have wrong multiplicity '0': should be '1'") + eq_(c1.links, [c2]) + eq_(c2.links, [c1]) + + def testDeleteOneToNLinks(self): + self.m1.association(self.m2, "l: 0..1 -> *") + + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m1, "c2") + c3 = CClass(self.m2, "c3") + c4 = CClass(self.m2, "c4") + c5 = CClass(self.m2, "c5") + + addLinks({c1: [c3, c4], c2: [c5]}) + c4.deleteLinks([c1]) + eq_(c1.links, [c3]) + eq_(c2.links, [c5]) + eq_(c3.links, [c1]) + eq_(c4.links, []) + eq_(c5.links, [c2]) + + c4.addLinks([c2]) + eq_(c2.links, [c5, c4]) + deleteLinks({c1: c3, c2: c2.links}) + eq_(c1.links, []) + eq_(c2.links, []) + eq_(c3.links, []) + eq_(c4.links, []) + eq_(c5.links, []) + + def testDeleteOneToNLinksWrongMultiplicity(self): + a = self.m1.association(self.m2, "l: 1 -> *") + + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m1, "c2") + c3 = CClass(self.m2, "c3") + c4 = CClass(self.m2, "c4") + c5 = CClass(self.m2, "c5") + + addLinks({c1: [c3, c4], c2: [c5]}) + + try: + c4.deleteLinks([c1]) + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'c4' have wrong multiplicity '0': should be '1'") + + + def testDeleteNToNLinks(self): + self.m1.association(self.m2, "l: * -> *") + + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m1, "c2") + c3 = CClass(self.m2, "c3") + c4 = CClass(self.m2, "c4") + c5 = CClass(self.m2, "c5") + c6 = CClass(self.m2, "c6") + + addLinks({c1: [c3, c4], c2: [c4, c5]}) + c4.deleteLinks([c1, c2]) + eq_(c1.links, [c3]) + eq_(c2.links, [c5]) + eq_(c3.links, [c1]) + eq_(c4.links, []) + eq_(c5.links, [c2]) + + addLinks({c4: [c1, c2], c6: [c2, c1]}) + deleteLinks({c1: c6, c2: [c4, c5]}) + eq_(c1.links, [c3, c4]) + eq_(c2.links, [c6]) + eq_(c3.links, [c1]) + eq_(c4.links, [c1]) + eq_(c5.links, []) + eq_(c6.links, [c2]) + + def testDeleteLinkNoMatchingLink(self): + a = self.m1.association(self.m2, "l: 0..1 -> *") + + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m1, "c2") + c3 = CClass(self.m2, "c3") + c4 = CClass(self.m2, "c4") + c5 = CClass(self.m2, "c5") + + addLinks({c1: [c3, c4], c2: [c5]}, association = a) + + try: + deleteLinks({c1:c5}) + exceptionExpected_() + except CException as e: + eq_(e.value, "no link found for 'c1 -> c5' in delete links") + + b = self.m1.association(self.m2, "l: 0..1 -> *") + try: + deleteLinks({c1:c5}) + exceptionExpected_() + except CException as e: + eq_(e.value, "no link found for 'c1 -> c5' in delete links") + + try: + c4.deleteLinks([c1], association = b) + exceptionExpected_() + except CException as e: + eq_(e.value, "no link found for 'c4 -> c1' in delete links for given association") + + def testDeleteLinkSelectByAssociation(self): + a = self.m1.association(self.m2, "a: * -> *") + b = self.m1.association(self.m2, "b: * -> *") + + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m1, "c2") + c3 = CClass(self.m2, "c3") + c4 = CClass(self.m2, "c4") + + addLinks({c1: [c3], c2: [c3, c4]}, association = b) + deleteLinks({c2: c3}) + eq_(c1.links, [c3]) + eq_(c2.links, [c4]) + eq_(c3.links, [c1]) + eq_(c4.links, [c2]) + addLinks({c1: [c3], c2: [c3, c4]}, association = a) + + try: + deleteLinks({c1: c3}) + exceptionExpected_() + except CException as e: + eq_(e.value, "link definition in delete links ambiguous for link 'c1->c3': found multiple matches") + + deleteLinks({c1: c3, c2: c4}, association = b) + eq_(c1.links, [c3]) + eq_(c2.links, [c3, c4]) + eq_(c3.links, [c1, c2]) + eq_(c4.links, [c2]) + for o in [c1, c2, c3, c4]: + for lo in o.linkObjects: + eq_(lo.association, a) + + c1.addLinks(c3, association = b) + try: + c1.deleteLinks(c3) + exceptionExpected_() + except CException as e: + eq_(e.value, "link definition in delete links ambiguous for link 'c1->c3': found multiple matches") + + eq_(c1.links, [c3, c3]) + eq_(c2.links, [c3, c4]) + eq_(c3.links, [c1, c2, c1]) + eq_(c4.links, [c2]) + + c1.deleteLinks(c3, association = a) + eq_(c1.links, [c3]) + eq_(c2.links, [c3, c4]) + eq_(c3.links, [c2, c1]) + eq_(c4.links, [c2]) + + + def testDeleteLinkSelectByRoleName(self): + a = self.m1.association(self.m2, "a: [sourceA] * -> [targetA] *") + b = self.m1.association(self.m2, "b: [sourceB] * -> [targetB] *") + + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m1, "c2") + c3 = CClass(self.m2, "c3") + c4 = CClass(self.m2, "c4") + + addLinks({c1: [c3], c2: [c3, c4]}, roleName = "targetB") + deleteLinks({c2: c3}) + eq_(c1.links, [c3]) + eq_(c2.links, [c4]) + eq_(c3.links, [c1]) + eq_(c4.links, [c2]) + addLinks({c1: [c3], c2: [c3, c4]}, roleName = "targetA") + + deleteLinks({c1: c3, c2: c4}, roleName = "targetB") + eq_(c1.links, [c3]) + eq_(c2.links, [c3, c4]) + eq_(c3.links, [c1, c2]) + eq_(c4.links, [c2]) + for o in [c1, c2, c3, c4]: + for lo in o.linkObjects: + eq_(lo.association, a) + + addLinks({c1: [c3], c2: [c3, c4]}, roleName = "targetB") + c3.deleteLinks([c1, c2], roleName = "sourceB") + deleteLinks({c4: c2}, roleName = "sourceB") + + eq_(c1.links, [c3]) + eq_(c2.links, [c3, c4]) + eq_(c3.links, [c1, c2]) + eq_(c4.links, [c2]) + for o in [c1, c2, c3, c4]: + for lo in o.linkObjects: + eq_(lo.association, a) + + def testDeleteLinksWrongRoleName(self): + a = self.m1.association(self.m2, "a: [sourceA] * -> [targetA] *") + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m2, "c2") + c1.addLinks(c2) + try: + c1.deleteLinks(c2, roleName = "target") + exceptionExpected_() + except CException as e: + eq_(e.value, "no link found for 'c1 -> c2' in delete links for given role name 'target'") + + def testDeleteLinksWrongAssociation(self): + a = self.m1.association(self.m2, "a: [sourceA] * -> [targetA] *") + c1 = CClass(self.m1, "c1") + c2 = CClass(self.m2, "c2") + c1.addLinks(c2) + try: + c1.deleteLinks(c2, association = c1) + exceptionExpected_() + except CException as e: + eq_(e.value, "'c1' is not a association") + b = self.m1.association(self.m2, "b: [sourceB] * -> [targetB] *") + try: + c1.deleteLinks(c2, association = b) + exceptionExpected_() + except CException as e: + eq_(e.value, "no link found for 'c1 -> c2' in delete links for given association") + try: + c1.deleteLinks(c2, association = b, roleName = "x") + exceptionExpected_() + except CException as e: + eq_(e.value, "no link found for 'c1 -> c2' in delete links for given role name 'x' and for given association") + + +if __name__ == "__main__": + nose.main() \ No newline at end of file diff --git a/tests/test_metaclass.py b/tests/test_metaclass.py new file mode 100644 index 0000000..cdf6972 --- /dev/null +++ b/tests/test_metaclass.py @@ -0,0 +1,197 @@ +import sys +sys.path.append("..") + +import re + +import nose +from nose.tools import ok_, eq_ +from testCommons import neq_, exceptionExpected_ +from parameterized import parameterized + +from codeableModels import CBundle, CMetaclass, CClass, CObject, CAttribute, CException, CEnum, CStereotype + +class TestMetaclass(): + def testCreationOfOneMetaclass(self): + mcl = CMetaclass("MCL") + eq_(mcl.name, "MCL") + cl = CClass(mcl, "C") + eq_(cl.metaclass, mcl) + + def testCreationOfUnnamedMetaclass(self): + mcl = CMetaclass() + eq_(mcl.name, None) + + def testDeleteMetaclass(self): + m = CMetaclass("M1") + cl = CClass(m, "C") + m.delete() + eq_(cl.metaclass, None) + + m1 = CMetaclass("M1") + m2 = CMetaclass("M2") + m3 = CMetaclass("M3", superclasses = m2, attributes = {"i" : 1}, stereotypes = CStereotype("S")) + cl = CClass(m3, "C") + m1.delete() + eq_(cl.metaclass, m3) + m3.delete() + eq_(cl.metaclass, None) + + eq_(m3.superclasses, []) + eq_(m2.subclasses, []) + eq_(m3.attributes, []) + eq_(m3.attributeNames, []) + eq_(m3.stereotypes, []) + eq_(m3.classes, []) + eq_(m3.name, None) + eq_(m3.bundles, []) + + def testDeleteMetaclassClassesRelation(self): + m1 = CMetaclass() + m2 = CMetaclass() + cl1 = CClass(m1) + cl2 = CClass(m1) + cl3 = CClass(m2) + + m1.delete() + + eq_(cl1.metaclass, None) + eq_(cl2.metaclass, None) + eq_(cl3.metaclass, m2) + eq_(set(m2.classes), set([cl3])) + eq_(m1.classes, []) + + def testGetClassesByName(self): + m1 = CMetaclass() + eq_(set(m1.getClasses("CL1")), set()) + c1 = CClass(m1, "CL1") + eq_(m1.classes, [c1]) + eq_(set(m1.getClasses("CL1")), set([c1])) + c2 = CClass(m1, "CL1") + eq_(set(m1.getClasses("CL1")), set([c1, c2])) + ok_(c1 != c2) + c3 = CClass(m1, "CL1") + eq_(set(m1.getClasses("CL1")), set([c1, c2, c3])) + eq_(m1.getClass("CL1"), c1) + + def testGetStereotypesByName(self): + m1 = CMetaclass() + eq_(set(m1.getStereotypes("S1")), set()) + s1 = CStereotype("S1", extended = m1) + eq_(m1.stereotypes, [s1]) + eq_(set(m1.getStereotypes("S1")), set([s1])) + s2 = CStereotype("S1", extended = m1) + eq_(set(m1.getStereotypes("S1")), set([s1, s2])) + ok_(s1 != s2) + s3 = CStereotype("S1", extended = m1) + eq_(set(m1.getStereotypes("S1")), set([s1, s2, s3])) + eq_(m1.getStereotype("S1"), s1) + + def testStereotypesThatAreDeleted(self): + s1 = CStereotype("S1") + s1.delete() + try: + CMetaclass(stereotypes = [s1]) + exceptionExpected_() + except CException as e: + eq_(e.value, "cannot access named element that has been deleted") + + def testStereotypesThatAreNone(self): + try: + CMetaclass(stereotypes = [None]) + exceptionExpected_() + except CException as e: + eq_(e.value, "'None' is not a stereotype") + + def testGetConnectedElements_WrongKeywordArg(self): + m1 = CMetaclass("m1") + try: + m1.getConnectedElements(a = "m1") + exceptionExpected_() + except CException as e: + eq_(e.value, "unknown keyword argument 'a', should be one of: ['addStereotypes', 'processStereotypes', 'addBundles', 'processBundles', 'stopElementsInclusive', 'stopElementsExclusive']") + + + def testGetConnectedElementsEmpty(self): + m1 = CMetaclass("m1") + eq_(set(m1.getConnectedElements()), set([m1])) + + m1 = CMetaclass("m1") + m2 = CMetaclass("m2", superclasses = m1) + m3 = CMetaclass("m3", superclasses = m2) + m4 = CMetaclass("m4", superclasses = m2) + m5 = CMetaclass("m5", superclasses = m4) + m6 = CMetaclass("m6") + a1 = m1.association(m6) + m7 = CMetaclass("m7") + m8 = CMetaclass("m8") + a3 = m8.association(m7) + m9 = CMetaclass("m9") + a4 = m7.association(m9) + m10 = CMetaclass("m10") + m11 = CMetaclass("m11", superclasses = m10) + m12 = CMetaclass("m12") + m13 = CMetaclass("m13") + m12.association(m11) + bsub = CBundle("bsub", elements = [m13]) + b1 = CBundle("b1", elements = [m1, m2, m3, bsub, m7]) + b2 = CBundle("b2", elements = [m7, m10, m11, m12]) + + m14 = CMetaclass("m14") + s1 = CStereotype("s1") + s2 = CStereotype("s2", extended = [m7, m14], superclasses = s1) + + allTestElts = [m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, b1, b2, bsub, m14, s1, s2] + + @parameterized.expand([ + (allTestElts, {"processStereotypes": True, "processBundles": True}, set([m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, m14])), + (allTestElts, {"processStereotypes": True, "processBundles": True, "addStereotypes": True, "addBundles": True}, set([m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, m14, b1, b2, bsub, s1, s2])), + (allTestElts, {"processStereotypes": True, "processBundles": True, "addBundles": True}, set([m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, m14, b1, b2, bsub])), + (allTestElts, {"processStereotypes": True, "processBundles": True, "addStereotypes": True}, set([m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, m14, s1, s2])), + ([m1], {"processBundles": True}, set([m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13])), + ([m1], {"processBundles": True, "addStereotypes": True, "addBundles": True}, set([m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, b1, b2, bsub, s1, s2])), + ([m1], {"processBundles": True, "addBundles": True}, set([m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, b1, b2, bsub])), + ([m1], {"processBundles": True, "addStereotypes": True}, set([m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, s1, s2])), + ([m7], {"processStereotypes": True}, set([m7, m8, m9, m14])), + ([m7], {"processStereotypes": True, "addStereotypes": True, "addBundles": True}, set([m7, m8, m9, m14, b1, b2, s1, s2])), + ([m7], {"processStereotypes": True, "addBundles": True}, set([m7, m8, m9, m14, b1, b2])), + ([m7], {"processStereotypes": True, "addStereotypes": True}, set([m7, m8, m9, m14, s1, s2])), + ([m7], {}, set([m7, m8, m9])), + ([m7], {"addStereotypes": True, "addBundles": True}, set([m7, m8, m9, b1, b2, s1, s2])), + ([m7], {"addBundles": True}, set([m7, m8, m9, b1, b2])), + ([m7], {"addStereotypes": True}, set([m7, m8, m9, s1, s2]))]) + def testGetConnectedElements(self, testElements, kwargsDict, connectedElementsResult): + for elt in testElements: + eq_(set(elt.getConnectedElements(**kwargsDict)), connectedElementsResult) + + def testGetConnectedElements_StopElementsInclusiveWrongTypes(self): + m1 = CMetaclass("m1") + try: + m1.getConnectedElements(stopElementsInclusive = "m1") + exceptionExpected_() + except CException as e: + eq_(e.value, "expected one element or a list of stop elements, but got: 'm1'") + try: + m1.getConnectedElements(stopElementsInclusive = ["m1"]) + exceptionExpected_() + except CException as e: + eq_(e.value, "expected one element or a list of stop elements, but got: '['m1']' with element of wrong type: 'm1'") + + @parameterized.expand([ + ([m1], {"stopElementsExclusive" : [m1]}, set()), + ([m1], {"stopElementsInclusive" : [m3, m6]}, set([m1, m2, m3, m4, m5, m6])), + ([m1], {"stopElementsExclusive" : [m3, m6]}, set([m1, m2, m4, m5])), + ([m1], {"stopElementsInclusive" : [m3, m6], "stopElementsExclusive" : [m3]}, set([m1, m2, m4, m5, m6])), + ([m7], {"stopElementsInclusive" : [b2, s2], "stopElementsExclusive" : [b1], "processStereotypes": True, "processBundles": True, "addStereotypes": True, "addBundles": True}, set([m7, b2, s2, m8, m9])), + ([m7], {"stopElementsExclusive" : [b1, b2, s2], "processStereotypes": True, "processBundles": True, "addStereotypes": True, "addBundles": True}, set([m7, m8, m9])), + ([b2], {"stopElementsInclusive" : [b1, m8, m9], "stopElementsExclusive" : [s1, s2], "processStereotypes": True, "processBundles": True, "addStereotypes": True, "addBundles": True}, set([m7, m10, m11, m12, m8, m9, b1, b2])), + ([b2], {"stopElementsInclusive" : [b1, m8, m9], "stopElementsExclusive" : [s1, s2], "processStereotypes": True, "processBundles": True}, set([m7, m10, m11, m12, m8, m9])), + ([s1], {"stopElementsInclusive" : [m14, m7], "processStereotypes": True, "processBundles": True, "addStereotypes": True, "addBundles": True}, set([s1, s2, m7, m14])), + ([s1], {"stopElementsInclusive" : [m14, m7], "processStereotypes": True, "processBundles": True}, set([m7, m14])) + ]) + def testGetConnectedElements_StopElementsInclusive(self, testElements, kwargsDict, connectedElementsResult): + for elt in testElements: + eq_(set(elt.getConnectedElements(**kwargsDict)), connectedElementsResult) + + +if __name__ == "__main__": + nose.main() \ No newline at end of file diff --git a/tests/test_metaclass_associations.py b/tests/test_metaclass_associations.py new file mode 100644 index 0000000..9892d54 --- /dev/null +++ b/tests/test_metaclass_associations.py @@ -0,0 +1,201 @@ +import sys +sys.path.append("..") + +import re + +import nose +from nose.tools import ok_, eq_ +from testCommons import neq_, exceptionExpected_ +from parameterized import parameterized + +from codeableModels import CMetaclass, CClass, CObject, CAttribute, CException, CEnum, CBundle, CStereotype + +class TestMetaclassAssociations(): + def setUp(self): + self.metaclassBundle = CBundle("P") + self.m1 = CMetaclass("M1", bundles = self.metaclassBundle) + self.m2 = CMetaclass("M2", bundles = self.metaclassBundle) + self.m3 = CMetaclass("M3", bundles = self.metaclassBundle) + self.m4 = CMetaclass("M4", bundles = self.metaclassBundle) + self.m5 = CMetaclass("M5", bundles = self.metaclassBundle) + + def getAllAssociationsInBundle(self): + associations = [] + for c in self.metaclassBundle.getElements(type=CMetaclass): + for a in c.allAssociations: + if not a in associations: + associations.append(a) + return associations + + def testAssociationCreation(self): + a1 = self.m1.association(self.m2, multiplicity = "1", roleName = "t", + sourceMultiplicity = "*", sourceRoleName = "i") + a2 = self.m1.association(self.m2, "[o]*->[s]1") + a3 = self.m1.association(self.m3, "[a] 0..1 <*>- [n]*") + a4 = self.m1.association(self.m3, multiplicity = "*", roleName = "e", + sourceMultiplicity = "0..1", sourceRoleName = "a", composition = True) + a5 = self.m4.association(self.m3, multiplicity = "*", roleName = "n", + sourceMultiplicity = "0..1", sourceRoleName = "a", aggregation = True) + a6 = self.m3.association(self.m2, '[a] 3 <>- [e]*') + + eq_(len(self.getAllAssociationsInBundle()), 6) + + eq_(self.m1.associations[0].roleName, "t") + eq_(a5.roleName, "n") + eq_(a2.roleName, "s") + eq_(a1.multiplicity, "1") + eq_(a1.sourceMultiplicity, "*") + eq_(a4.sourceMultiplicity, "0..1") + eq_(a6.sourceMultiplicity, "3") + + eq_(a1.composition, False) + eq_(a1.aggregation, False) + eq_(a3.composition, True) + eq_(a3.aggregation, False) + eq_(a5.composition, False) + eq_(a5.aggregation, True) + + a1.aggregation = True + eq_(a1.composition, False) + eq_(a1.aggregation, True) + a1.composition = True + eq_(a1.composition, True) + eq_(a1.aggregation, False) + + def testMixedAssociationTypes(self): + c1 = CClass(self.m1, "C1") + s1 = CStereotype("S1") + try: + a1 = self.m1.association(s1, multiplicity = "1", roleName = "t", + sourceMultiplicity = "*", sourceRoleName = "i") + exceptionExpected_() + except CException as e: + eq_("metaclass 'M1' is not compatible with association target 'S1'", e.value) + + try: + a1 = self.m1.association(c1, multiplicity = "1", roleName = "t", + sourceMultiplicity = "*", sourceRoleName = "i") + exceptionExpected_() + except CException as e: + eq_("metaclass 'M1' is not compatible with association target 'C1'", e.value) + + def testGetAssociationByRoleName(self): + a1 = self.m1.association(self.m2, multiplicity = "1", roleName = "t", + sourceMultiplicity = "*", sourceRoleName = "i") + a2 = self.m1.association(self.m2, multiplicity = "1", roleName = "s", + sourceMultiplicity = "*", sourceRoleName = "o") + a3 = self.m1.association(self.m3, multiplicity = "*", roleName = "n", + sourceMultiplicity = "0..1", sourceRoleName = "a", composition = True) + + a_2 = next(a for a in self.m1.associations if a.roleName == "s") + eq_(a_2.multiplicity, "1") + eq_(a_2.sourceRoleName, "o") + eq_(a_2.sourceMultiplicity, "*") + + def testGetAssociationByName(self): + a1 = self.m1.association(self.m2, name = "n1", multiplicity = "1", roleName = "t", + sourceMultiplicity = "*", sourceRoleName = "i") + a2 = self.m1.association(self.m2, name = "n2", multiplicity = "1", roleName = "s", + sourceMultiplicity = "*", sourceRoleName = "o") + a3 = self.m1.association(self.m3, "n3: [a] 0..1 <*>- [n] *") + + a_2 = next(a for a in self.m1.associations if a.name == "n2") + eq_(a_2.multiplicity, "1") + eq_(a_2.sourceRoleName, "o") + eq_(a_2.sourceMultiplicity, "*") + + a_3 = next(a for a in self.m1.associations if a.name == "n3") + eq_(a_3.multiplicity, "*") + eq_(a_3.roleName, "n") + eq_(a_3.sourceMultiplicity, "0..1") + eq_(a_3.sourceRoleName, "a") + eq_(a_3.composition, True) + + + def testGetAssociations(self): + a1 = self.m1.association(self.m2, multiplicity = "1", roleName = "t", + sourceMultiplicity = "*", sourceRoleName = "i") + a2 = self.m1.association(self.m2, multiplicity = "1", roleName = "s", + sourceMultiplicity = "*", sourceRoleName = "o") + a3 = self.m1.association(self.m3, multiplicity = "*", roleName = "n", + sourceMultiplicity = "0..1", sourceRoleName = "a", composition = True) + a4 = self.m1.association(self.m3, multiplicity = "*", roleName = "e", + sourceMultiplicity = "0..1", sourceRoleName = "a", composition = True) + a5 = self.m4.association(self.m3, multiplicity = "*", roleName = "n", + sourceMultiplicity = "0..1", sourceRoleName = "a", aggregation = True) + a6 = self.m3.association(self.m2, multiplicity = "*", roleName = "e", + sourceMultiplicity = "3", sourceRoleName = "a", aggregation = True) + eq_(self.m1.associations, [a1, a2, a3, a4]) + eq_(self.m2.associations, [a1, a2, a6]) + eq_(self.m3.associations, [a3, a4, a5, a6]) + eq_(self.m4.associations, [a5]) + eq_(self.m5.associations, []) + + def testDeleteAssociations(self): + a1 = self.m1.association(self.m2, multiplicity = "1", roleName = "t", + sourceMultiplicity = "*", sourceRoleName = "i") + a2 = self.m1.association(self.m2, multiplicity = "1", roleName = "s", + sourceMultiplicity = "*", sourceRoleName = "o") + a3 = self.m1.association(self.m3, multiplicity = "*", roleName = "n", + sourceMultiplicity = "0..1", sourceRoleName = "a", composition = True) + a4 = self.m1.association(self.m3, multiplicity = "*", roleName = "e", + sourceMultiplicity = "0..1", sourceRoleName = "a", composition = True) + a5 = self.m4.association(self.m3, multiplicity = "*", roleName = "n", + sourceMultiplicity = "0..1", sourceRoleName = "a", aggregation = True) + a6 = self.m3.association(self.m2, multiplicity = "*", roleName = "e", + sourceMultiplicity = "3", sourceRoleName = "a", aggregation = True) + a7 = self.m1.association(self.m1, multiplicity = "*", roleName = "x", + sourceMultiplicity = "3", sourceRoleName = "y") + + eq_(len(self.getAllAssociationsInBundle()), 7) + + a2.delete() + a4.delete() + + eq_(len(self.getAllAssociationsInBundle()), 5) + + eq_(self.m1.associations, [a1, a3, a7]) + eq_(self.m2.associations, [a1, a6]) + eq_(self.m3.associations, [a3, a5, a6]) + eq_(self.m4.associations, [a5]) + eq_(self.m5.associations, []) + + + def testDeleteClassAndGetAssociations(self): + a1 = self.m1.association(self.m2, multiplicity = "1", roleName = "t", + sourceMultiplicity = "*", sourceRoleName = "i") + a2 = self.m1.association(self.m2, multiplicity = "1", roleName = "s", + sourceMultiplicity = "*", sourceRoleName = "o") + a3 = self.m1.association(self.m3, multiplicity = "*", roleName = "n", + sourceMultiplicity = "0..1", sourceRoleName = "a", composition = True) + a4 = self.m1.association(self.m3, multiplicity = "*", roleName = "e", + sourceMultiplicity = "0..1", sourceRoleName = "a", composition = True) + a5 = self.m4.association(self.m3, multiplicity = "*", roleName = "n", + sourceMultiplicity = "0..1", sourceRoleName = "a", aggregation = True) + a6 = self.m3.association(self.m2, multiplicity = "*", roleName = "e", + sourceMultiplicity = "3", sourceRoleName = "a", aggregation = True) + a7 = self.m1.association(self.m1, multiplicity = "*", roleName = "x", + sourceMultiplicity = "3", sourceRoleName = "y") + + eq_(len(self.getAllAssociationsInBundle()), 7) + + self.m1.delete() + + eq_(len(self.getAllAssociationsInBundle()), 2) + + eq_(self.m1.associations, []) + eq_(self.m2.associations, [a6]) + eq_(self.m3.associations, [a5, a6]) + eq_(self.m4.associations, [a5]) + eq_(self.m5.associations, []) + + def testAllAssociations(self): + s = CMetaclass("S") + d = CMetaclass("D", superclasses = s) + a = s.association(d, "is next: [prior s] * -> [next d] *") + eq_(d.allAssociations, [a]) + eq_(s.allAssociations, [a]) + + +if __name__ == "__main__": + nose.main() \ No newline at end of file diff --git a/tests/test_metaclass_attributes.py b/tests/test_metaclass_attributes.py new file mode 100644 index 0000000..f0b586d --- /dev/null +++ b/tests/test_metaclass_attributes.py @@ -0,0 +1,258 @@ +import sys +sys.path.append("..") + +import nose +from nose.tools import ok_, eq_ +from testCommons import neq_, exceptionExpected_ +from parameterized import parameterized + +import re + +from codeableModels import CMetaclass, CClass, CObject, CAttribute, CException, CEnum + + +class TestMetaClassAttributes(): + def setUp(self): + self.mcl = CMetaclass("MCL") + + def testPrimitiveEmptyInput(self): + cl = CMetaclass("MCL", attributes = {}) + eq_(len(cl.attributes), 0) + eq_(len(cl.attributeNames), 0) + + def testPrimitiveNoneInput(self): + cl = CMetaclass("MCL", attributes = None) + eq_(len(cl.attributes), 0) + eq_(len(cl.attributeNames), 0) + + def testPrimitiveTypeAttributes(self): + cl = CMetaclass("MCL", attributes = { + "isBoolean": True, + "intVal": 1, + "floatVal": 1.1, + "string": "abc"}) + eq_(len(cl.attributes), 4) + eq_(len(cl.attributeNames), 4) + + ok_(set(["isBoolean", "intVal", "floatVal", "string"]).issubset(cl.attributeNames)) + + a1 = cl.getAttribute("isBoolean") + a2 = cl.getAttribute("intVal") + a3 = cl.getAttribute("floatVal") + a4 = cl.getAttribute("string") + ok_(set([a1, a2, a3, a4]).issubset(cl.attributes)) + eq_(None, cl.getAttribute("X")) + + eq_(a1.type, bool) + eq_(a2.type, int) + eq_(a3.type, float) + eq_(a4.type, str) + + d1 = a1.default + d2 = a2.default + d3 = a3.default + d4 = a4.default + + ok_(isinstance(d1, bool)) + ok_(isinstance(d2, int)) + ok_(isinstance(d3, float)) + ok_(isinstance(d4, str)) + + eq_(d1, True) + eq_(d2, 1) + eq_(d3, 1.1) + eq_(d4, "abc") + + def testAttributeGetNameAndClassifier(self): + m = CMetaclass("M", attributes = {"isBoolean": True}) + a = m.getAttribute("isBoolean") + eq_(a.name, "isBoolean") + eq_(a.classifier, m) + m.delete() + eq_(a.name, None) + eq_(a.classifier, None) + + def testPrimitiveAttributesNoDefault(self): + self.mcl.attributes = {"a": bool, "b": int, "c": str, "d": float} + a1 = self.mcl.getAttribute("a") + a2 = self.mcl.getAttribute("b") + a3 = self.mcl.getAttribute("c") + a4 = self.mcl.getAttribute("d") + ok_(set([a1, a2, a3, a4]).issubset(self.mcl.attributes)) + eq_(a1.default, None) + eq_(a1.type, bool) + eq_(a2.default, None) + eq_(a2.type, int) + eq_(a3.default, None) + eq_(a3.type, str) + eq_(a4.default, None) + eq_(a4.type, float) + + def testGetAttributeNotFound(self): + eq_(self.mcl.getAttribute("x"), None) + self.mcl.attributes = {"a": bool, "b": int, "c": str, "d": float} + eq_(self.mcl.getAttribute("x"), None) + + def testSameNamedArgumentsCAttributes(self): + a1 = CAttribute(default = "") + a2 = CAttribute(type = str) + n1 = "a" + self.mcl.attributes = {n1: a1, "a": a2} + eq_(set(self.mcl.attributes), {a2}) + eq_(self.mcl.attributeNames, ["a"]) + + def testSameNamedArgumentsDefaults(self): + n1 = "a" + self.mcl.attributes = {n1: "", "a": 1} + ok_(len(self.mcl.attributes), 1) + eq_(self.mcl.getAttribute("a").default, 1) + eq_(self.mcl.attributeNames, ["a"]) + + def testObjectTypeAttribute(self): + attrType = CClass(self.mcl, "AttrType") + attrValue = CObject(attrType, "attrValue") + self.mcl.attributes = {"attrTypeObj" : attrValue} + objAttr = self.mcl.getAttribute("attrTypeObj") + attributes = self.mcl.attributes + eq_(set(attributes), {objAttr}) + + boolAttr = CAttribute(default = True) + self.mcl.attributes = {"attrTypeObj" : objAttr, "isBoolean" : boolAttr} + attributes = self.mcl.attributes + eq_(set(attributes), {objAttr, boolAttr}) + eq_(self.mcl.attributeNames, ["attrTypeObj", "isBoolean"]) + objAttr = self.mcl.getAttribute("attrTypeObj") + eq_(objAttr.type, attrType) + default = objAttr.default + ok_(isinstance(default, CObject)) + eq_(default, attrValue) + + self.mcl.attributes = {"attrTypeObj" : attrValue, "isBoolean" : boolAttr} + eq_(self.mcl.attributeNames, ["attrTypeObj", "isBoolean"]) + # using the CObject in attributes causes a new CAttribute to be created != objAttr + neq_(self.mcl.getAttribute("attrTypeObj"), objAttr) + + def testUseEnumTypeAttribute(self): + enumValues = ["A", "B", "C"] + enumObj = CEnum("ABCEnum", values = enumValues) + ea1 = CAttribute(type = enumObj, default = "A") + ea2 = CAttribute(type = enumObj) + self.mcl.attributes = {"letters1": ea1, "letters2": ea2} + eq_(set(self.mcl.attributes), {ea1, ea2}) + ok_(isinstance(ea1.type, CEnum)) + + self.mcl.attributes = {"letters1": ea1, "isBool": True, "letters2": ea2} + boolAttr = self.mcl.getAttribute("isBool") + l1 = self.mcl.getAttribute("letters1") + eq_(set(self.mcl.attributes), {l1, ea2, boolAttr}) + eq_(l1.default, "A") + eq_(ea2.default, None) + + def testUnknownAttributeType(self): + try: + self.mcl.attributes = {"x": CEnum, "b" : bool} + exceptionExpected_() + except CException as e: + ok_(re.match("^(unknown attribute type: '')$", e.value)) + + + def testSetAttributeDefaultValue(self): + enumObj = CEnum("ABCEnum", values = ["A", "B", "C"]) + self.mcl.attributes = {"letters": enumObj, "b" : bool} + l = self.mcl.getAttribute("letters") + b = self.mcl.getAttribute("b") + eq_(l.default, None) + eq_(b.default, None) + l.default = "B" + b.default = False + eq_(l.default, "B") + eq_(b.default, False) + eq_(l.type, enumObj) + eq_(b.type, bool) + + def testCClassVsCObject(self): + cl_a = CClass(self.mcl, "A") + cl_b = CClass(self.mcl, "B") + obj_b = CObject(cl_b, "obj_b") + + self.mcl.attributes = {"a": cl_a, "b": obj_b} + a = self.mcl.getAttribute("a") + b = self.mcl.getAttribute("b") + eq_(a.type, cl_a) + eq_(a.default, None) + eq_(b.type, cl_b) + eq_(b.default, obj_b) + + testMetaclass = CMetaclass("A") + testEnum = CEnum("AEnum", values = [1,2]) + testClass = CClass(testMetaclass, "CL") + + @parameterized.expand([ + (bool, testMetaclass), + (bool, 1.1), + (int, testMetaclass), + (int, "abc"), + (float, "1"), + (float, testMetaclass), + (str, 1), + (str, testMetaclass), + (testEnum, "1"), + (testEnum, testMetaclass), + (testClass, "1"), + (testClass, testMetaclass)]) + def testAttributeTypeCheck(self, type, wrongDefault): + self.mcl.attributes = {"a": type} + attr = self.mcl.getAttribute("a") + try: + attr.default = wrongDefault + exceptionExpected_() + except CException as e: + eq_(f"default value '{wrongDefault!s}' incompatible with attribute's type '{type!s}'", e.value) + + def test_DeleteAttributes(self): + self.mcl.attributes = { + "isBoolean": True, + "intVal": 1, + "floatVal": 1.1, + "string": "abc"} + eq_(len(set(self.mcl.attributes)), 4) + self.mcl.attributes = {} + eq_(set(self.mcl.attributes), set()) + self.mcl.attributes = { + "isBoolean": True, + "intVal": 1, + "floatVal": 1.1, + "string": "abc"} + eq_(len(set(self.mcl.attributes)), 4) + self.mcl.attributes = {} + eq_(set(self.mcl.attributes), set()) + + def testTypeObjectAttributeClassIsDeletedInConstructor(self): + attrCl = CClass(self.mcl, "AC") + attrCl.delete() + try: + CMetaclass("M", attributes = {"ac": attrCl}) + exceptionExpected_() + except CException as e: + eq_(e.value, "cannot access named element that has been deleted") + + def testTypeObjectAttributeClassIsNone(self): + m = CMetaclass("M", attributes = {"ac": None}) + ac = m.getAttribute("ac") + eq_(ac.default, None) + eq_(ac.type, None) + + def testDefaultObjectAttributeIsDeletedInConstructor(self): + attrCl = CClass(self.mcl, "AC") + defaultObj = CObject(attrCl) + defaultObj.delete() + try: + CMetaclass("M", attributes = {"ac": defaultObj}) + exceptionExpected_() + except CException as e: + eq_(e.value, "cannot access named element that has been deleted") + +if __name__ == "__main__": + nose.main() + + diff --git a/tests/test_metaclass_inheritance.py b/tests/test_metaclass_inheritance.py new file mode 100644 index 0000000..453ad93 --- /dev/null +++ b/tests/test_metaclass_inheritance.py @@ -0,0 +1,391 @@ +import sys +sys.path.append("..") + +import re + +import nose +from nose.tools import ok_, eq_ +from testCommons import neq_, exceptionExpected_ +from parameterized import parameterized + +from codeableModels import CMetaclass, CStereotype, CClass, CObject, CAttribute, CException, CEnum + +class TestMetaclassInheritance(): + def testMetaclassNoInheritance(self): + t = CMetaclass("T") + eq_(set(t.superclasses), set()) + eq_(set(t.subclasses), set()) + eq_(set(t.allSuperclasses), set()) + eq_(set(t.allSubclasses), set()) + + def testMetaclassSuperclassesEmptyInput(self): + m1 = CMetaclass("M1", superclasses = []) + eq_(set(m1.superclasses), set()) + eq_(set(m1.subclasses), set()) + + def testMetaclassSuperclassesNoneInput(self): + m1 = CMetaclass("M1", superclasses = None) + eq_(set(m1.superclasses), set()) + eq_(set(m1.subclasses), set()) + + def testMetaclassSimpleInheritance(self): + t = CMetaclass("T") + m1 = CMetaclass("M1", superclasses = t) + m2 = CMetaclass("M2", superclasses = t) + b1 = CMetaclass("B1", superclasses = m1) + b2 = CMetaclass("B2", superclasses = m1) + b3 = CMetaclass("B3", superclasses = t) + + eq_(set(t.superclasses), set()) + eq_(set(t.subclasses), set([m1, m2, b3])) + eq_(set(t.allSuperclasses), set()) + eq_(set(t.allSubclasses), set([m1, m2, b1, b2, b3])) + + eq_(set(m1.superclasses), set([t])) + eq_(set(m1.subclasses), set([b1, b2])) + eq_(set(m1.allSuperclasses), set([t])) + eq_(set(m1.allSubclasses), set([b1, b2])) + + eq_(set(m2.superclasses), set([t])) + eq_(set(m2.subclasses), set()) + eq_(set(m2.allSuperclasses), set([t])) + eq_(set(m2.allSubclasses), set()) + + eq_(set(b1.superclasses), set([m1])) + eq_(set(b1.subclasses), set()) + eq_(set(b1.allSuperclasses), set([t, m1])) + eq_(set(b1.allSubclasses), set()) + + eq_(set(b2.superclasses), set([m1])) + eq_(set(b2.subclasses), set()) + eq_(set(b2.allSuperclasses), set([t, m1])) + eq_(set(b2.allSubclasses), set()) + + eq_(set(b3.superclasses), set([t])) + eq_(set(b3.subclasses), set()) + eq_(set(b3.allSuperclasses), set([t])) + eq_(set(b3.allSubclasses), set()) + + def testMetaclassInheritanceDoubleAssignment(self): + t = CMetaclass("T") + m1 = CMetaclass("M1") + try: + m1.superclasses = [t, t] + exceptionExpected_() + except CException as e: + eq_("'T' is already a superclass of 'M1'", e.value) + eq_(m1.name, "M1") + eq_(t.name, "T") + eq_(set(m1.superclasses), set([t])) + + def testMetaclassInheritanceDeleteTopClass(self): + t = CMetaclass("T") + m1 = CMetaclass("M1", superclasses = [t]) + m2 = CMetaclass("M2", superclasses = [t]) + b1 = CMetaclass("B1", superclasses = [m1]) + b2 = CMetaclass("B2", superclasses = [m1]) + b3 = CMetaclass("B3", superclasses = [t]) + + t.delete() + + eq_(t.name, None) + eq_(set(t.superclasses), set()) + eq_(set(t.subclasses), set()) + eq_(set(t.allSuperclasses), set()) + eq_(set(t.allSubclasses), set()) + + eq_(set(m1.superclasses), set()) + eq_(set(m1.subclasses), set([b1, b2])) + eq_(set(m1.allSuperclasses), set()) + eq_(set(m1.allSubclasses), set([b1, b2])) + + eq_(set(m2.superclasses), set()) + eq_(set(m2.subclasses), set()) + eq_(set(m2.allSuperclasses), set()) + eq_(set(m2.allSubclasses), set()) + + eq_(set(b1.superclasses), set([m1])) + eq_(set(b1.subclasses), set()) + eq_(set(b1.allSuperclasses), set([m1])) + eq_(set(b1.allSubclasses), set()) + + eq_(set(b2.superclasses), set([m1])) + eq_(set(b2.subclasses), set()) + eq_(set(b2.allSuperclasses), set([m1])) + eq_(set(b2.allSubclasses), set()) + + eq_(set(b3.superclasses), set()) + eq_(set(b3.subclasses), set()) + eq_(set(b3.allSuperclasses), set()) + eq_(set(b3.allSubclasses), set()) + + def testMetaclassInheritanceDeleteInnerClass(self): + t = CMetaclass("T") + m1 = CMetaclass("M1", superclasses = [t]) + m2 = CMetaclass("M2", superclasses = [t]) + b1 = CMetaclass("B1", superclasses = [m1]) + b2 = CMetaclass("B2", superclasses = [m1]) + b3 = CMetaclass("B3", superclasses = [t]) + + m1.delete() + + eq_(set(t.superclasses), set()) + eq_(set(t.subclasses), set([m2, b3])) + eq_(set(t.allSuperclasses), set()) + eq_(set(t.allSubclasses), set([m2, b3])) + + eq_(set(m1.superclasses), set()) + eq_(set(m1.subclasses), set()) + eq_(set(m1.allSuperclasses), set()) + eq_(set(m1.allSubclasses), set()) + + eq_(set(m2.superclasses), set([t])) + eq_(set(m2.subclasses), set()) + eq_(set(m2.allSuperclasses), set([t])) + eq_(set(m2.allSubclasses), set()) + + eq_(set(b1.superclasses), set()) + eq_(set(b1.subclasses), set()) + eq_(set(b1.allSuperclasses), set()) + eq_(set(b1.allSubclasses), set()) + + eq_(set(b2.superclasses), set()) + eq_(set(b2.subclasses), set()) + eq_(set(b2.allSuperclasses), set()) + eq_(set(b2.allSubclasses), set()) + + eq_(set(b3.superclasses), set([t])) + eq_(set(b3.subclasses), set()) + eq_(set(b3.allSuperclasses), set([t])) + eq_(set(b3.allSubclasses), set()) + + def testMetaclassSuperclassesReassignment(self): + t = CMetaclass("T") + m1 = CMetaclass("M1", superclasses = [t]) + m2 = CMetaclass("M2", superclasses = [t]) + b1 = CMetaclass("B1", superclasses = [m1]) + b2 = CMetaclass("B2", superclasses = [m1]) + b3 = CMetaclass("B3", superclasses = [t]) + + m1.superclasses = [] + b1.superclasses = [] + b2.superclasses = [] + + eq_(set(t.superclasses), set()) + eq_(set(t.subclasses), set([m2, b3])) + eq_(set(t.allSuperclasses), set()) + eq_(set(t.allSubclasses), set([m2, b3])) + + eq_(set(m1.superclasses), set()) + eq_(set(m1.subclasses), set()) + eq_(set(m1.allSuperclasses), set()) + eq_(set(m1.allSubclasses), set()) + + eq_(set(m2.superclasses), set([t])) + eq_(set(m2.subclasses), set()) + eq_(set(m2.allSuperclasses), set([t])) + eq_(set(m2.allSubclasses), set()) + + eq_(set(b1.superclasses), set()) + eq_(set(b1.subclasses), set()) + eq_(set(b1.allSuperclasses), set()) + eq_(set(b1.allSubclasses), set()) + + eq_(set(b2.superclasses), set()) + eq_(set(b2.subclasses), set()) + eq_(set(b2.allSuperclasses), set()) + eq_(set(b2.allSubclasses), set()) + + eq_(set(b3.superclasses), set([t])) + eq_(set(b3.subclasses), set()) + eq_(set(b3.allSuperclasses), set([t])) + eq_(set(b3.allSubclasses), set()) + + def testMetaclassMultipleInheritance(self): + t1 = CMetaclass("T1") + t2 = CMetaclass("T2") + t3 = CMetaclass("T3") + m1 = CMetaclass("M1", superclasses = [t1, t3]) + m2 = CMetaclass("M2", superclasses = [t2, t3]) + b1 = CMetaclass("B1", superclasses = [m1]) + b2 = CMetaclass("B2", superclasses = [m1, m2]) + b3 = CMetaclass("B3", superclasses = [m2, m1]) + + eq_(set(t1.superclasses), set()) + eq_(set(t1.subclasses), set([m1])) + eq_(set(t1.allSuperclasses), set()) + eq_(set(t1.allSubclasses), set([m1, b1, b2, b3])) + + eq_(set(t2.superclasses), set()) + eq_(set(t2.subclasses), set([m2])) + eq_(set(t2.allSuperclasses), set()) + eq_(set(t2.allSubclasses), set([m2, b3, b2])) + + eq_(set(t3.superclasses), set()) + eq_(set(t3.subclasses), set([m2, m1])) + eq_(set(t3.allSuperclasses), set()) + eq_(set(t3.allSubclasses), set([m2, m1, b1, b2, b3])) + + eq_(set(m1.superclasses), set([t1, t3])) + eq_(set(m1.subclasses), set([b1, b2, b3])) + eq_(set(m1.allSuperclasses), set([t1, t3])) + eq_(set(m1.allSubclasses), set([b1, b2, b3])) + + eq_(set(m2.superclasses), set([t2, t3])) + eq_(set(m2.subclasses), set([b2, b3])) + eq_(set(m2.allSuperclasses), set([t2, t3])) + eq_(set(m2.allSubclasses), set([b2, b3])) + + eq_(set(b1.superclasses), set([m1])) + eq_(set(b1.subclasses), set()) + eq_(set(b1.allSuperclasses), set([m1, t1, t3])) + eq_(set(b1.allSubclasses), set()) + + eq_(set(b2.superclasses), set([m1, m2])) + eq_(set(b2.subclasses), set()) + eq_(set(b2.allSuperclasses), set([m1, m2, t1, t2, t3])) + eq_(set(b2.allSubclasses), set()) + + eq_(set(b3.superclasses), set([m1, m2])) + eq_(set(b3.subclasses), set()) + eq_(set(b3.allSuperclasses), set([m1, m2, t1, t2, t3])) + eq_(set(b3.allSubclasses), set()) + + def testMetaclassAsWrongTypeOfSuperclass(self): + t = CMetaclass("M") + try: + CClass(t, "C", superclasses = [t]) + exceptionExpected_() + except CException as e: + ok_(re.match("^cannot add superclass 'M' to 'C': not of type([ $", e.value)) + try: + CStereotype("S", superclasses = [t]) + exceptionExpected_() + except CException as e: + ok_(re.match("^cannot add superclass 'M' to 'S': not of type([ $", e.value)) + + def testMetaclassPathNoInheritance(self): + t = CMetaclass() + o = CClass(t) + eq_(set(o.classPath), set([t])) + + def testMetaclassPathSimpleInheritance(self): + t = CMetaclass("T") + m1 = CMetaclass("M1", superclasses = [t]) + m2 = CMetaclass("M2", superclasses = [t]) + b1 = CMetaclass("B1", superclasses = [m1]) + b2 = CMetaclass("B2", superclasses = [m1]) + b3 = CMetaclass("B3", superclasses = [t]) + eq_(CClass(b1).classPath, [b1, m1, t]) + eq_(CClass(b2).classPath, [b2, m1, t]) + eq_(CClass(b3).classPath, [b3, t]) + eq_(CClass(m1).classPath, [m1, t]) + eq_(CClass(m2).classPath, [m2, t]) + eq_(CClass(t).classPath, [t]) + + def testMetaclassPathMultipleInheritance(self): + t = CMetaclass("T") + m1 = CMetaclass("M1", superclasses = [t]) + m2 = CMetaclass("M2", superclasses = [t]) + b1 = CMetaclass("B1", superclasses = [m1, m2]) + b2 = CMetaclass("B2", superclasses = [t, m1]) + b3 = CMetaclass("B3", superclasses = [t, m1, m2]) + eq_(CClass(b1).classPath, [b1, m1, t, m2]) + eq_(CClass(b2).classPath, [b2, t, m1]) + eq_(CClass(b3).classPath, [b3, t, m1, m2]) + eq_(CClass(m1).classPath, [m1, t]) + eq_(CClass(m2).classPath, [m2, t]) + eq_(CClass(t).classPath, [t]) + + def testMetaclassInstanceOf(self): + a = CMetaclass() + b = CMetaclass(superclasses = [a]) + c = CMetaclass() + cl = CClass(b, "C") + + eq_(cl.instanceOf(a), True) + eq_(cl.instanceOf(b), True) + eq_(cl.instanceOf(c), False) + + try: + cl.instanceOf(cl) + exceptionExpected_() + except CException as e: + eq_("'C' is not a metaclass", e.value) + + cl.delete() + eq_(cl.instanceOf(a), False) + + def testMetaclassGetAllInstances(self): + t = CMetaclass("T") + m1 = CMetaclass("M1", superclasses = [t]) + m2 = CMetaclass("M2", superclasses = [t]) + b1 = CMetaclass("B1", superclasses = [m1, m2]) + to1 = CClass(t) + to2 = CClass(t) + m1o1 = CClass(m1) + m1o2 = CClass(m1) + m2o = CClass(m2) + b1o1 = CClass(b1) + b1o2 = CClass(b1) + + eq_(set(t.classes), set([to1, to2])) + eq_(set(t.allClasses), set([to1, to2, m1o1, m1o2, b1o1, b1o2, m2o])) + eq_(set(m1.classes), set([m1o1, m1o2])) + eq_(set(m1.allClasses), set([m1o1, m1o2, b1o1, b1o2])) + eq_(set(m2.classes), set([m2o])) + eq_(set(m2.allClasses), set([m2o, b1o1, b1o2])) + eq_(set(b1.classes), set([b1o1, b1o2])) + eq_(set(b1.allClasses), set([b1o1, b1o2])) + + def testMetaclassHasSuperclassHasSubclass(self): + m1 = CMetaclass("M1") + m2 = CMetaclass("M2", superclasses = [m1]) + m3 = CMetaclass("M3", superclasses = [m2]) + m4 = CMetaclass("M4", superclasses = [m2]) + m5 = CMetaclass("M5", superclasses = []) + + eq_(m1.hasSuperclass(m2), False) + eq_(m5.hasSuperclass(m2), False) + eq_(m1.hasSuperclass(None), False) + eq_(m5.hasSuperclass(None), False) + eq_(m2.hasSuperclass(m1), True) + eq_(m3.hasSuperclass(m2), True) + eq_(m3.hasSuperclass(m2), True) + eq_(m4.hasSuperclass(m2), True) + eq_(m3.hasSubclass(m2), False) + eq_(m3.hasSubclass(None), False) + eq_(m5.hasSubclass(m2), False) + eq_(m5.hasSubclass(None), False) + eq_(m1.hasSubclass(m3), True) + eq_(m1.hasSubclass(m2), True) + + def testMetaclassUnknownNonPositionalArgument(self): + t = CMetaclass("T") + try: + CMetaclass("ST", superclass = t) + exceptionExpected_() + except CException as e: + eq_("unknown keyword argument 'superclass', should be one of: ['stereotypes', 'attributes', 'superclasses', 'bundles']", e.value) + + def testSuperMetaclassesThatAreDeleted(self): + m1 = CMetaclass("M1") + m1.delete() + try: + CMetaclass(superclasses = [m1]) + exceptionExpected_() + except CException as e: + eq_(e.value, "cannot access named element that has been deleted") + + def testSuperMetaclassesThatAreNone(self): + try: + CMetaclass("M", superclasses = [None]) + exceptionExpected_() + except CException as e: + ok_(e.value.startswith("cannot add superclass 'None' to 'M': not of type")) + +if __name__ == "__main__": + nose.main() + + + diff --git a/tests/test_object.py b/tests/test_object.py new file mode 100644 index 0000000..825d021 --- /dev/null +++ b/tests/test_object.py @@ -0,0 +1,252 @@ +import sys +sys.path.append("..") + +import re + +import nose +from nose.tools import ok_, eq_ +from testCommons import neq_, exceptionExpected_ +from parameterized import parameterized + +from codeableModels import CMetaclass, CClass, CObject, CAttribute, CException, CEnum, CBundle, addLinks + +class TestObject(): + def setUp(self): + self.mcl = CMetaclass("MCL") + self.cl = CClass(self.mcl, "CL", attributes = {"i" : 1}) + + def testCreationOfOneObject(self): + eq_(self.cl.objects, []) + o1 = CObject(self.cl, "o") + o2 = self.cl.objects[0] + eq_(o1.name, "o") + eq_(o1, o2) + eq_(o2.classifier, self.cl) + + def testCreateObjectWrongArgTypes(self): + try: + CObject("CL", "o1") + exceptionExpected_() + except CException as e: + eq_("'CL' is not a class", e.value) + try: + CObject(self.mcl, "o1") + exceptionExpected_() + except CException as e: + eq_("'MCL' is not a class", e.value) + + def testCreationOf3Objects(self): + o1 = CObject(self.cl, "o1") + o2 = CObject(self.cl, "o2") + o3 = CObject(self.cl, "o3") + eq_(set(self.cl.objects), set([o1, o2, o3])) + + def testCreationOfUnnamedObject(self): + o1 = CObject(self.cl) + o2 = CObject(self.cl) + o3 = CObject(self.cl, "x") + eq_(set(self.cl.objects), set([o1, o2, o3])) + eq_(o1.name, None) + eq_(o2.name, None) + eq_(o3.name, "x") + + def testDeleteObject(self): + o = CObject(self.cl, "o") + o.delete() + eq_(set(self.cl.objects), set()) + + o1 = CObject(self.cl, "o1") + o2 = CObject(self.cl, "o2") + o3 = CObject(self.cl, "o3") + o3.setValue("i", 7) + + o1.delete() + eq_(set(self.cl.objects), set([o2, o3])) + o3.delete() + eq_(set(self.cl.objects), set([o2])) + + eq_(o3.classifier, None) + eq_(set(self.cl.objects), set([o2])) + eq_(o3.name, None) + eq_(o3.bundles, []) + try: + o3.getValue("i") + exceptionExpected_() + except CException as e: + eq_("can't get value 'i' on deleted object", e.value) + + def testClassInstanceRelation(self): + cl1 = CClass(self.mcl, "CL1") + cl2 = CClass(self.mcl, "CL2") + eq_(set(cl1.objects), set()) + + o1 = CObject(cl1, "O1") + o2 = CObject(cl1, "O2") + o3 = CObject(cl2, "O3") + eq_(set(cl1.objects), set([o1, o2])) + eq_(set(cl2.objects), set([o3])) + eq_(o1.classifier, cl1) + eq_(o2.classifier, cl1) + eq_(o3.classifier, cl2) + + def testClassInstanceRelationDeletionFromObject(self): + cl1 = CClass(self.mcl, "CL1") + o1 = CObject(cl1, "O1") + o1.delete() + eq_(set(cl1.objects), set()) + + o1 = CObject(cl1, "O1") + o2 = CObject(cl1, "O2") + o3 = CObject(cl1, "O3") + + eq_(set(cl1.objects), set([o1, o2, o3])) + o1.delete() + eq_(set(cl1.objects), set([o2, o3])) + o3.delete() + eq_(set(cl1.objects), set([o2])) + eq_(o1.classifier, None) + eq_(o2.classifier, cl1) + eq_(o3.classifier, None) + o2.delete() + eq_(set(cl1.objects), set()) + eq_(o1.classifier, None) + eq_(o2.classifier, None) + eq_(o3.classifier, None) + + def testClassifierChange(self): + cl1 = CClass(self.mcl, "CL1") + cl2 = CClass(self.mcl, "CL2") + o1 = CObject(cl1, "O1") + o1.classifier = cl2 + eq_(o1.classifier, cl2) + eq_(cl1.objects, []) + eq_(cl2.objects, [o1]) + + def testClassifierChangeNullInput(self): + cl1 = CClass(self.mcl, "CL1") + o1 = CObject(cl1, "O1") + try: + o1.classifier = None + exceptionExpected_() + except CException as e: + eq_("'None' is not a class", e.value) + + def testClassifierChangeWrongInputType(self): + cl1 = CClass(self.mcl, "CL1") + o1 = CObject(cl1, "O1") + try: + o1.classifier = self.mcl + exceptionExpected_() + except CException as e: + ok_(e.value.endswith("' is not a class")) + + + def testClassIsDeletedInConstructor(self): + c1 = CClass(self.mcl, "CL1") + c1.delete() + try: + CObject(c1, "O1") + exceptionExpected_() + except CException as e: + ok_(e.value.endswith("cannot access named element that has been deleted")) + + def testClassIsDeletedInClassifierMethod(self): + c1 = CClass(self.mcl, "CL1") + c2 = CClass(self.mcl, "CL2") + o1 = CObject(c2, "O1") + c1.delete() + try: + o1.classifier = c1 + exceptionExpected_() + except CException as e: + ok_(e.value.endswith("cannot access named element that has been deleted")) + + def testClassIsNoneInConstructor(self): + try: + CObject(None, "O1") + exceptionExpected_() + except CException as e: + ok_(e.value.endswith("'None' is not a class")) + + + + def testGetConnectedElements_WrongKeywordArg(self): + o1 = CObject(self.cl, "o1") + try: + o1.getConnectedElements(a = "o1") + exceptionExpected_() + except CException as e: + eq_(e.value, "unknown keyword argument 'a', should be one of: ['addBundles', 'processBundles', 'stopElementsInclusive', 'stopElementsExclusive']") + + def testGetConnectedElementsEmpty(self): + o1 = CObject(self.cl, "o1") + eq_(set(o1.getConnectedElements()), set([o1])) + + mcl = CMetaclass("MCL") + cl1 = CClass(mcl, "C") + cl2 = CClass(mcl, "C") + cl1.association(cl2, "*->*") + o1 = CObject(cl1, "o1") + o2 = CObject(cl2, "o2") + o3 = CObject(cl2, "o3") + o4 = CObject(cl2, "o4") + o5 = CObject(cl2, "o5") + addLinks({o1: [o2, o3, o4, o5]}) + o6 = CObject(cl2, "o6") + addLinks({o6: o1}) + o7 = CObject(cl1, "o7") + o8 = CObject(cl2, "o8") + o9 = CObject(cl2, "o9") + addLinks({o7: [o8, o9]}) + o10 = CObject(cl1, "o10") + o11 = CObject(cl2, "o11") + addLinks({o10: o11}) + o12 = CObject(cl1, "o12") + o13 = CObject(cl2, "o13") + addLinks({o12: o11}) + bsub = CBundle("bsub", elements = [o13]) + b1 = CBundle("b1", elements = [o1, o2, o3, bsub, o7]) + b2 = CBundle("b2", elements = [o7, o10, o11, o12]) + + allTestElts = [o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, o11, o12, o13, b1, b2, bsub] + + @parameterized.expand([ + (allTestElts, {"processBundles": True}, set([o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, o11, o12, o13])), + (allTestElts, {"processBundles": True, "addBundles": True}, set([o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, o11, o12, o13, b1, b2, bsub])), + ([o1], {"processBundles": True}, set([o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, o11, o12, o13])), + ([o1], {"processBundles": True, "addBundles": True}, set([o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, o11, o12, o13, b1, b2, bsub])), + ([o7], {}, set([o7, o8, o9])), + ([o7], {"addBundles": True}, set([o7, o8, o9, b1, b2])), + ]) + def testGetConnectedElements(self, testElements, kwargsDict, connectedElementsResult): + for elt in testElements: + eq_(set(elt.getConnectedElements(**kwargsDict)), connectedElementsResult) + + def testGetConnectedElements_StopElementsInclusiveWrongTypes(self): + o1 = CObject(self.cl, "o1") + try: + o1.getConnectedElements(stopElementsInclusive = "o1") + exceptionExpected_() + except CException as e: + eq_(e.value, "expected one element or a list of stop elements, but got: 'o1'") + try: + o1.getConnectedElements(stopElementsInclusive = ["o1"]) + exceptionExpected_() + except CException as e: + eq_(e.value, "expected one element or a list of stop elements, but got: '['o1']' with element of wrong type: 'o1'") + + @parameterized.expand([ + ([o1], {"stopElementsExclusive" : [o1]}, set()), + ([o1], {"stopElementsInclusive" : [o3, o6]}, set([o1, o2, o3, o4, o5, o6])), + ([o1], {"stopElementsExclusive" : [o3, o6]}, set([o1, o2, o4, o5])), + ([o1], {"stopElementsInclusive" : [o3, o6], "stopElementsExclusive" : [o3]}, set([o1, o2, o4, o5, o6])), + ([o7], {"stopElementsInclusive" : [b2], "stopElementsExclusive" : [b1], "processBundles": True, "addBundles": True}, set([o7, b2, o8, o9])), + ([o7], {"stopElementsExclusive" : [b1, b2], "processBundles": True, "addBundles": True}, set([o7, o8, o9])), + ]) + def testGetConnectedElements_StopElementsInclusive(self, testElements, kwargsDict, connectedElementsResult): + for elt in testElements: + eq_(set(elt.getConnectedElements(**kwargsDict)), connectedElementsResult) + + +if __name__ == "__main__": + nose.main() \ No newline at end of file diff --git a/tests/test_object_attribute_values.py b/tests/test_object_attribute_values.py new file mode 100644 index 0000000..68997a9 --- /dev/null +++ b/tests/test_object_attribute_values.py @@ -0,0 +1,552 @@ +import sys +sys.path.append("..") + +import re + +import nose +from nose.tools import ok_, eq_ +from testCommons import neq_, exceptionExpected_ +from parameterized import parameterized + +from codeableModels import CMetaclass, CClass, CObject, CAttribute, CException, CEnum + +class TestObjectAttributeValues(): + def setUp(self): + self.mcl = CMetaclass("MCL") + self.cl = CClass(self.mcl, "CL") + + def testValuesOnPrimitiveTypeAttributes(self): + cl = CClass(self.mcl, "C", attributes = { + "isBoolean": True, + "intVal": 1, + "floatVal": 1.1, + "string": "abc"}) + o = CObject(cl, "o") + + eq_(o.getValue("isBoolean"), True) + eq_(o.getValue("intVal"), 1) + eq_(o.getValue("floatVal"), 1.1) + eq_(o.getValue("string"), "abc") + + o.setValue("isBoolean", False) + o.setValue("intVal", 2) + o.setValue("floatVal", 2.1) + o.setValue("string", "y") + + eq_(o.getValue("isBoolean"), False) + eq_(o.getValue("intVal"), 2) + eq_(o.getValue("floatVal"), 2.1) + eq_(o.getValue("string"), "y") + + def testAttributeOfValueUnknown(self): + o = CObject(self.cl, "o") + try: + o.getValue("x") + exceptionExpected_() + except CException as e: + eq_(e.value, "attribute 'x' unknown for object 'o'") + + self.cl.attributes = {"isBoolean": True, "intVal": 1} + try: + o.setValue("x", 1) + exceptionExpected_() + except CException as e: + eq_(e.value, "attribute 'x' unknown for object 'o'") + + def testIntegersAsFloats(self): + cl = CClass(self.mcl, "C", attributes = { + "floatVal": float}) + o = CObject(cl, "o") + o.setValue("floatVal", 15) + eq_(o.getValue("floatVal"), 15) + + def testAttributeDefinedAfterInstance(self): + cl = CClass(self.mcl, "C") + o = CObject(cl, "o") + cl.attributes = {"floatVal": float} + o.setValue("floatVal", 15) + eq_(o.getValue("floatVal"), 15) + + def testObjectTypeAttributeValues(self): + attrType = CClass(self.mcl, "AttrType") + attrValue = CObject(attrType, "attrValue") + self.cl.attributes = {"attrTypeObj" : attrValue} + objAttr = self.cl.getAttribute("attrTypeObj") + eq_(objAttr.type, attrType) + o = CObject(self.cl, "o") + eq_(o.getValue("attrTypeObj"), attrValue) + + nonAttrValue = CObject(self.cl, "nonAttrValue") + try: + o.setValue("attrTypeObj", nonAttrValue) + exceptionExpected_() + except CException as e: + eq_(e.value, "type of object 'nonAttrValue' is not matching type of attribute 'attrTypeObj'") + + def testAddObjectAttributeGetSetValue(self): + attrType = CClass(self.mcl, "AttrType") + attrValue = CObject(attrType, "attrValue") + o1 = CObject(self.cl, "o1") + self.cl.attributes = { + "attrTypeObj1" : attrType, "attrTypeObj2" : attrValue + } + eq_(o1.getValue("attrTypeObj1"), None) + eq_(o1.getValue("attrTypeObj2"), attrValue) + + def testObjectAttributeOfSuperclassType(self): + attrSuperType = CClass(self.mcl, "AttrSuperType") + attrType = CClass(self.mcl, "AttrType", superclasses = attrSuperType) + attrValue = CObject(attrType, "attrValue") + o = CObject(self.cl, "o1") + self.cl.attributes = { + "attrTypeObj1" : attrSuperType, "attrTypeObj2" : attrValue + } + o.setValue("attrTypeObj1", attrValue) + o.setValue("attrTypeObj2", attrValue) + eq_(o.getValue("attrTypeObj1"), attrValue) + eq_(o.getValue("attrTypeObj2"), attrValue) + + def testValuesOnAttributesWithNoDefaultValues(self): + attrType = CClass(self.mcl, "AttrType") + enumType = CEnum("EnumT", values = ["A", "B", "C"]) + cl = CClass(self.mcl, "C", attributes = { + "b": bool, + "i": int, + "f": float, + "s": str, + "o": attrType, + "e": enumType}) + o = CObject(cl, "o") + for n in ["b", "i", "f", "s", "o", "e"]: + eq_(o.getValue(n), None) + + def testEnumTypeAttributeValues(self): + enumType = CEnum("EnumT", values = ["A", "B", "C"]) + cl = CClass(self.mcl, "C", attributes = { + "e1": enumType, + "e2": enumType}) + e2 = cl.getAttribute("e2") + e2.default = "A" + o = CObject(cl, "o") + eq_(o.getValue("e1"), None) + eq_(o.getValue("e2"), "A") + o.setValue("e1", "B") + o.setValue("e2", "C") + eq_(o.getValue("e1"), "B") + eq_(o.getValue("e2"), "C") + try: + o.setValue("e1", "X") + exceptionExpected_() + except CException as e: + eq_(e.value, "value 'X' is not element of enumeration") + + def testDefaultInitAfterInstanceCreation(self): + enumType = CEnum("EnumT", values = ["A", "B", "C"]) + cl = CClass(self.mcl, "C", attributes = { + "e1": enumType, + "e2": enumType}) + o = CObject(cl, "o") + e2 = cl.getAttribute("e2") + e2.default = "A" + eq_(o.getValue("e1"), None) + eq_(o.getValue("e2"), "A") + + def testAttributeValueTypeCheckBool1(self): + self.cl.attributes = {"t": bool} + o = CObject(self.cl, "o") + try: + o.setValue("t", self.mcl) + exceptionExpected_() + except CException as e: + eq_(f"value for attribute 't' is not a known attribute type", e.value) + + def testAttributeValueTypeCheckBool2(self): + self.cl.attributes = {"t": bool} + o = CObject(self.cl, "o") + try: + o.setValue("t", 1) + exceptionExpected_() + except CException as e: + eq_(f"value type for attribute 't' does not match attribute type", e.value) + + def testAttributeValueTypeCheckInt(self): + self.cl.attributes = {"t": int} + o = CObject(self.cl, "o") + try: + o.setValue("t", True) + exceptionExpected_() + except CException as e: + eq_(f"value type for attribute 't' does not match attribute type", e.value) + + def testAttributeValueTypeCheckFloat(self): + self.cl.attributes = {"t": float} + o = CObject(self.cl, "o") + try: + o.setValue("t", True) + exceptionExpected_() + except CException as e: + eq_(f"value type for attribute 't' does not match attribute type", e.value) + + def testAttributeValueTypeCheckStr(self): + self.cl.attributes = {"t": str} + o = CObject(self.cl, "o") + try: + o.setValue("t", True) + exceptionExpected_() + except CException as e: + eq_(f"value type for attribute 't' does not match attribute type", e.value) + + def testAttributeValueTypeCheckObject(self): + attrType = CClass(self.mcl, "AttrType") + self.cl.attributes = {"t": attrType} + o = CObject(self.cl, "o") + try: + o.setValue("t", True) + exceptionExpected_() + except CException as e: + eq_(f"value type for attribute 't' does not match attribute type", e.value) + + def testAttributeValueTypeCheckEnum(self): + enumType = CEnum("EnumT", values = ["A", "B", "C"]) + self.cl.attributes = {"t": enumType} + o = CObject(self.cl, "o") + try: + o.setValue("t", True) + exceptionExpected_() + except CException as e: + eq_(f"value type for attribute 't' does not match attribute type", e.value) + + def testAttributeDeleted(self): + cl = CClass(self.mcl, "C", attributes = { + "isBoolean": True, + "intVal": 15}) + o = CObject(cl, "o") + eq_(o.getValue("intVal"), 15) + cl.attributes = { + "isBoolean": False} + eq_(o.getValue("isBoolean"), True) + try: + o.getValue("intVal") + exceptionExpected_() + except CException as e: + eq_(e.value, "attribute 'intVal' unknown for object 'o'") + try: + o.setValue("intVal", 1) + exceptionExpected_() + except CException as e: + eq_(e.value, "attribute 'intVal' unknown for object 'o'") + + def testAttributeDeletedNoDefault(self): + cl = CClass(self.mcl, "C", attributes = { + "isBoolean": bool, + "intVal": int}) + cl.attributes = {"isBoolean": bool} + o = CObject(cl, "o") + eq_(o.getValue("isBoolean"), None) + try: + o.getValue("intVal") + exceptionExpected_() + except CException as e: + eq_(e.value, "attribute 'intVal' unknown for object 'o'") + try: + o.setValue("intVal", 1) + exceptionExpected_() + except CException as e: + eq_(e.value, "attribute 'intVal' unknown for object 'o'") + + def testAttributesOverwrite(self): + cl = CClass(self.mcl, "C", attributes = { + "isBoolean": True, + "intVal": 15}) + o = CObject(cl, "o") + eq_(o.getValue("intVal"), 15) + try: + o.getValue("floatVal") + exceptionExpected_() + except CException as e: + eq_(e.value, "attribute 'floatVal' unknown for object 'o'") + o.setValue("intVal", 18) + cl.attributes = { + "isBoolean": False, + "intVal": 19, + "floatVal": 25.1} + eq_(o.getValue("isBoolean"), True) + eq_(o.getValue("floatVal"), 25.1) + eq_(o.getValue("intVal"), 18) + o.setValue("floatVal", 1.2) + eq_(o.getValue("floatVal"), 1.2) + + def testAttributesOverwriteNoDefaults(self): + cl = CClass(self.mcl, "C", attributes = { + "isBoolean": bool, + "intVal": int}) + o = CObject(cl, "o") + eq_(o.getValue("isBoolean"), None) + o.setValue("isBoolean", False) + cl.attributes = { + "isBoolean": bool, + "intVal": int, + "floatVal": float} + eq_(o.getValue("isBoolean"), False) + eq_(o.getValue("floatVal"), None) + eq_(o.getValue("intVal"), None) + o.setValue("floatVal", 1.2) + eq_(o.getValue("floatVal"), 1.2) + + def testAttributesDeletedOnSubclass(self): + cl = CClass(self.mcl, "C", attributes = { + "isBoolean": True, + "intVal": 1}) + cl2 = CClass(self.mcl, "C2", attributes = { + "isBoolean": False}, superclasses = cl) + + o = CObject(cl2, "o") + + eq_(o.getValue("isBoolean"), False) + eq_(o.getValue("isBoolean", cl), True) + eq_(o.getValue("isBoolean", cl2), False) + + cl2.attributes = {} + + eq_(o.getValue("isBoolean"), True) + eq_(o.getValue("intVal"), 1) + eq_(o.getValue("isBoolean", cl), True) + try: + o.getValue("isBoolean", cl2) + exceptionExpected_() + except CException as e: + eq_(e.value, "attribute 'isBoolean' unknown for 'C2'") + + def testAttributesDeletedOnSubclassNoDefaults(self): + cl = CClass(self.mcl, "C", attributes = { + "isBoolean": bool, + "intVal": int}) + cl2 = CClass(self.mcl, "C2", attributes = { + "isBoolean": bool}, superclasses = cl) + + o = CObject(cl2, "o") + + eq_(o.getValue("isBoolean"), None) + eq_(o.getValue("isBoolean", cl), None) + eq_(o.getValue("isBoolean", cl2), None) + + cl2.attributes = {} + + eq_(o.getValue("isBoolean"), None) + eq_(o.getValue("intVal"), None) + eq_(o.getValue("isBoolean", cl), None) + try: + o.getValue("isBoolean", cl2) + exceptionExpected_() + except CException as e: + eq_(e.value, "attribute 'isBoolean' unknown for 'C2'") + + + def testAttributeValuesInheritance(self): + t1 = CClass(self.mcl, "T1") + t2 = CClass(self.mcl, "T2") + c = CClass(self.mcl, "C", superclasses = [t1, t2]) + sc = CClass(self.mcl, "C", superclasses = c) + + t1.attributes = {"i0" : 0} + t2.attributes = {"i1" : 1} + c.attributes = {"i2" : 2} + sc.attributes = {"i3" : 3} + + o = CObject(sc, "o") + + for name, value in {"i0" : 0, "i1" : 1, "i2" : 2, "i3" : 3}.items(): + eq_(o.getValue(name), value) + + eq_(o.getValue("i0", t1), 0) + eq_(o.getValue("i1", t2), 1) + eq_(o.getValue("i2", c), 2) + eq_(o.getValue("i3", sc), 3) + + for name, value in {"i0" : 10, "i1" : 11, "i2" : 12, "i3" : 13}.items(): + o.setValue(name, value) + + for name, value in {"i0" : 10, "i1" : 11, "i2" : 12, "i3" : 13}.items(): + eq_(o.getValue(name), value) + + eq_(o.getValue("i0", t1), 10) + eq_(o.getValue("i1", t2), 11) + eq_(o.getValue("i2", c), 12) + eq_(o.getValue("i3", sc), 13) + + def testAttributeValuesInheritanceAfterDeleteSuperclass(self): + t1 = CClass(self.mcl, "T1") + t2 = CClass(self.mcl, "T2") + c = CClass(self.mcl, "C", superclasses = [t1, t2]) + sc = CClass(self.mcl, "C", superclasses = c) + + t1.attributes = {"i0" : 0} + t2.attributes = {"i1" : 1} + c.attributes = {"i2" : 2} + sc.attributes = {"i3" : 3} + + o = CObject(sc, "o") + + t2.delete() + + for name, value in {"i0" : 0, "i2" : 2, "i3" : 3}.items(): + eq_(o.getValue(name), value) + try: + o.getValue("i1") + exceptionExpected_() + except CException as e: + eq_(e.value, "attribute 'i1' unknown for object 'o'") + + eq_(o.getValue("i0", t1), 0) + try: + o.getValue("i1", t2) + exceptionExpected_() + except CException as e: + eq_(e.value, "attribute 'i1' unknown for ''") + eq_(o.getValue("i2", c), 2) + eq_(o.getValue("i3", sc), 3) + + for name, value in {"i0" : 10, "i2" : 12, "i3" : 13}.items(): + o.setValue(name, value) + try: + o.setValue("i1", 11) + exceptionExpected_() + except CException as e: + eq_(e.value, "attribute 'i1' unknown for object 'o'") + + for name, value in {"i0" : 10, "i2" : 12, "i3" : 13}.items(): + eq_(o.getValue(name), value) + try: + o.getValue("i1") + exceptionExpected_() + except CException as e: + eq_(e.value, "attribute 'i1' unknown for object 'o'") + + eq_(o.getValue("i0", t1), 10) + try: + o.getValue("i1", t2) + exceptionExpected_() + except CException as e: + eq_(e.value, "attribute 'i1' unknown for ''") + eq_(o.getValue("i2", c), 12) + eq_(o.getValue("i3", sc), 13) + + + def testAttributeValuesSameNameInheritance(self): + t1 = CClass(self.mcl, "T1") + t2 = CClass(self.mcl, "T2") + c = CClass(self.mcl, "C", superclasses = [t1, t2]) + sc = CClass(self.mcl, "C", superclasses = c) + + t1.attributes = {"i" : 0} + t2.attributes = {"i" : 1} + c.attributes = {"i" : 2} + sc.attributes = {"i" : 3} + + o1 = CObject(sc) + o2 = CObject(c) + o3 = CObject(t1) + + eq_(o1.getValue("i"), 3) + eq_(o2.getValue("i"), 2) + eq_(o3.getValue("i"), 0) + + eq_(o1.getValue("i", sc), 3) + eq_(o1.getValue("i", c), 2) + eq_(o1.getValue("i", t2), 1) + eq_(o1.getValue("i", t1), 0) + eq_(o2.getValue("i", c), 2) + eq_(o2.getValue("i", t2), 1) + eq_(o2.getValue("i", t1), 0) + eq_(o3.getValue("i", t1), 0) + + o1.setValue("i", 10) + o2.setValue("i", 11) + o3.setValue("i", 12) + + eq_(o1.getValue("i"), 10) + eq_(o2.getValue("i"), 11) + eq_(o3.getValue("i"), 12) + + eq_(o1.getValue("i", sc), 10) + eq_(o1.getValue("i", c), 2) + eq_(o1.getValue("i", t2), 1) + eq_(o1.getValue("i", t1), 0) + eq_(o2.getValue("i", c), 11) + eq_(o2.getValue("i", t2), 1) + eq_(o2.getValue("i", t1), 0) + eq_(o3.getValue("i", t1), 12) + + o1.setValue("i", 130, sc) + o1.setValue("i", 100, t1) + o1.setValue("i", 110, t2) + o1.setValue("i", 120, c) + + eq_(o1.getValue("i"), 130) + + eq_(o1.getValue("i", sc), 130) + eq_(o1.getValue("i", c), 120) + eq_(o1.getValue("i", t2), 110) + eq_(o1.getValue("i", t1), 100) + + def testValuesMultipleInheritance(self): + t1 = CClass(self.mcl, "T1") + t2 = CClass(self.mcl, "T2") + sta = CClass(self.mcl, "STA", superclasses = [t1, t2]) + suba = CClass(self.mcl, "SubA", superclasses = [sta]) + stb = CClass(self.mcl, "STB", superclasses = [t1, t2]) + subb = CClass(self.mcl, "SubB", superclasses = [stb]) + stc = CClass(self.mcl, "STC") + subc = CClass(self.mcl, "SubC", superclasses = [stc]) + + cl = CClass(self.mcl, "M", superclasses = [suba, subb, subc]) + o = CObject(cl) + + t1.attributes = {"i0" : 0} + t2.attributes = {"i1" : 1} + sta.attributes = {"i2" : 2} + suba.attributes = {"i3" : 3} + stb.attributes = {"i4" : 4} + subb.attributes = {"i5" : 5} + stc.attributes = {"i6" : 6} + subc.attributes = {"i7" : 7} + + eq_(o.getValue("i0"), 0) + eq_(o.getValue("i1"), 1) + eq_(o.getValue("i2"), 2) + eq_(o.getValue("i3"), 3) + eq_(o.getValue("i4"), 4) + eq_(o.getValue("i5"), 5) + eq_(o.getValue("i6"), 6) + eq_(o.getValue("i7"), 7) + + eq_(o.getValue("i0", t1), 0) + eq_(o.getValue("i1", t2), 1) + eq_(o.getValue("i2", sta), 2) + eq_(o.getValue("i3", suba), 3) + eq_(o.getValue("i4", stb), 4) + eq_(o.getValue("i5", subb), 5) + eq_(o.getValue("i6", stc), 6) + eq_(o.getValue("i7", subc), 7) + + o.setValue("i0", 10) + o.setValue("i1", 11) + o.setValue("i2", 12) + o.setValue("i3", 13) + o.setValue("i4", 14) + o.setValue("i5", 15) + o.setValue("i6", 16) + o.setValue("i7", 17) + + eq_(o.getValue("i0"), 10) + eq_(o.getValue("i1"), 11) + eq_(o.getValue("i2"), 12) + eq_(o.getValue("i3"), 13) + eq_(o.getValue("i4"), 14) + eq_(o.getValue("i5"), 15) + eq_(o.getValue("i6"), 16) + eq_(o.getValue("i7"), 17) + + +if __name__ == "__main__": + nose.main() \ No newline at end of file diff --git a/tests/test_object_links.py b/tests/test_object_links.py new file mode 100644 index 0000000..4241211 --- /dev/null +++ b/tests/test_object_links.py @@ -0,0 +1,1163 @@ +import sys +sys.path.append("..") + +import re + +import nose +from nose.tools import ok_, eq_ +from testCommons import neq_, exceptionExpected_ +from parameterized import parameterized + +from codeableModels import CMetaclass, CClass, CObject, CAttribute, CException, CEnum, setLinks, addLinks, deleteLinks + +class TestObjectLinks(): + def setUp(self): + self.mcl = CMetaclass("MCL") + self.c1 = CClass(self.mcl, "C1") + self.c2 = CClass(self.mcl, "C2") + + def testLinkMethodsWrongKeywordArgs(self): + o1 = CObject(self.c1, "o1") + try: + addLinks({o1:o1}, associationX = None) + exceptionExpected_() + except CException as e: + eq_(e.value, "unknown keywords argument") + try: + o1.addLinks(o1, associationX = None) + exceptionExpected_() + except CException as e: + eq_(e.value, "unknown keywords argument") + try: + setLinks({o1: o1}, associationX = None) + exceptionExpected_() + except CException as e: + eq_(e.value, "unknown keywords argument") + try: + deleteLinks({o1:o1}, associationX = None) + exceptionExpected_() + except CException as e: + eq_(e.value, "unknown keywords argument") + try: + o1.deleteLinks(o1, associationX = None) + exceptionExpected_() + except CException as e: + eq_(e.value, "unknown keywords argument") + try: + o1.getLinks(associationX = None) + exceptionExpected_() + except CException as e: + eq_(e.value, "unknown keywords argument") + try: + deleteLinks({o1:o1}, stereotypeInstances = None) + exceptionExpected_() + except CException as e: + eq_(e.value, "unknown keywords argument") + + def testSetOneToOneLink(self): + self.c1.association(self.c2, name = "l", multiplicity = "1") + + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c2, "o2") + o3 = CObject(self.c2, "o3") + + eq_(o1.links, []) + + setLinks({o1: o2}) + eq_(o1.links, [o2]) + eq_(o2.links, [o1]) + + setLinks({o1: o3}) + eq_(o1.links, [o3]) + eq_(o2.links, []) + eq_(o3.links, [o1]) + + def testAddOneToOneLink(self): + self.c1.association(self.c2, "l: 1 -> [target] 0..1") + + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c2, "o2") + o3 = CObject(self.c2, "o3") + + eq_(o1.links, []) + + addLinks({o1: o3}) + eq_(o1.links, [o3]) + eq_(o3.links, [o1]) + + setLinks({o1: []}, roleName = "target") + eq_(o1.links, []) + + o1.addLinks(o2) + eq_(o1.links, [o2]) + eq_(o2.links, [o1]) + + try: + addLinks({o1: o3}) + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'o1' have wrong multiplicity '2': should be '0..1'") + eq_(o1.links, [o2]) + eq_(o2.links, [o1]) + eq_(o3.links, []) + + def testWrongTypesAddLinks(self): + self.c1.association(self.c2, name = "l", multiplicity = "1") + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c2, "o2") + try: + addLinks({o1: self.mcl}) + exceptionExpected_() + except CException as e: + eq_(e.value, "link target 'MCL' is neither an object nor a class") + try: + o1.addLinks([o2, self.mcl]) + exceptionExpected_() + except CException as e: + eq_(e.value, "link target 'MCL' is neither an object nor a class") + + def testWrongTypesSetLinks(self): + self.c1.association(self.c2, name = "l", multiplicity = "1") + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c2, "o2") + try: + setLinks({o1: self.mcl}) + exceptionExpected_() + except CException as e: + eq_(e.value, "link target 'MCL' is neither an object nor a class") + try: + setLinks({o1: [o2, self.mcl]}) + exceptionExpected_() + except CException as e: + eq_(e.value, "link target 'MCL' is neither an object nor a class") + try: + setLinks({o1: [o2, None]}) + exceptionExpected_() + except CException as e: + eq_(e.value, "link target 'None' is neither an object nor a class") + try: + setLinks({self.mcl: o2}) + exceptionExpected_() + except CException as e: + eq_(e.value, "link source 'MCL' is neither an object nor a class") + try: + setLinks({None: o2}) + exceptionExpected_() + except CException as e: + eq_(e.value, "link should not contain an empty source") + + def testWrongFormatSetLinks(self): + self.c1.association(self.c2, name = "l", multiplicity = "1") + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c2, "o2") + try: + setLinks([o1, o2]) + exceptionExpected_() + except CException as e: + eq_(e.value, "link definitions should be of the form {: , ..., : }") + + def testRemoveOneToOneLink(self): + a = self.c1.association(self.c2, "l: 1 -> [c2] 0..1") + + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c2, "o2") + o3 = CObject(self.c1, "o3") + o4 = CObject(self.c2, "o4") + + links = setLinks({o1: o2, o3: o4}) + eq_(o1.links, [o2]) + eq_(o2.links, [o1]) + eq_(o3.links, [o4]) + eq_(o4.links, [o3]) + eq_(o1.linkObjects, [links[0]]) + eq_(o2.linkObjects, [links[0]]) + eq_(o3.linkObjects, [links[1]]) + eq_(o4.linkObjects, [links[1]]) + + try: + links = setLinks({o1: None}) + exceptionExpected_() + except CException as e: + eq_(e.value, "matching association not found for source 'o1' and targets '[]'") + + setLinks({o1: None}, association = a) + eq_(o1.links, []) + eq_(o2.links, []) + eq_(o3.links, [o4]) + eq_(o4.links, [o3]) + eq_(o1.linkObjects, []) + eq_(o2.linkObjects, []) + eq_(o3.linkObjects, [links[1]]) + eq_(o4.linkObjects, [links[1]]) + + def testSetLinksOneToNLink(self): + self.c1.association(self.c2, name = "l") + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c2, "o2") + o3 = CObject(self.c2, "o3") + + setLinks({o1: [o2, o3]}) + eq_(o1.links, [o2, o3]) + eq_(o2.links, [o1]) + eq_(o3.links, [o1]) + setLinks({o1: o2}) + eq_(o1.links, [o2]) + eq_(o2.links, [o1]) + eq_(o3.links, []) + setLinks({o3: o1, o2: o1}) + eq_(o1.links, [o3, o2]) + eq_(o2.links, [o1]) + eq_(o3.links, [o1]) + + def testAddLinksOneToNLink(self): + self.c1.association(self.c2, name = "l") + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c2, "o2") + o3 = CObject(self.c2, "o3") + o4 = CObject(self.c2, "o4") + o5 = CObject(self.c2, "o5") + o6 = CObject(self.c2, "o6") + + addLinks({o1: [o2, o3]}) + eq_(o1.links, [o2, o3]) + eq_(o2.links, [o1]) + eq_(o3.links, [o1]) + addLinks({o1: o4}) + eq_(o1.links, [o2, o3, o4]) + eq_(o2.links, [o1]) + eq_(o3.links, [o1]) + eq_(o4.links, [o1]) + o1.addLinks([o5, o6]) + eq_(o1.links, [o2, o3, o4, o5, o6]) + eq_(o2.links, [o1]) + eq_(o3.links, [o1]) + eq_(o4.links, [o1]) + eq_(o5.links, [o1]) + eq_(o6.links, [o1]) + + def testRemoveOneToNLink(self): + a = self.c1.association(self.c2, name = "l", multiplicity = "*") + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c2, "o2") + o3 = CObject(self.c2, "o3") + setLinks({o1: [o2, o3]}) + setLinks({o1: o2}) + eq_(o1.links, [o2]) + eq_(o2.links, [o1]) + eq_(o3.links, []) + try: + setLinks({o1: []}) + exceptionExpected_() + except CException as e: + eq_(e.value, "matching association not found for source 'o1' and targets '[]'") + setLinks({o1: []}, association = a) + eq_(o1.links, []) + eq_(o2.links, []) + eq_(o3.links, []) + + def testNToNLink(self): + a = self.c1.association(self.c2, name = "l", sourceMultiplicity = "*") + o1a = CObject(self.c1, "o1a") + o1b = CObject(self.c1, "o1b") + o1c = CObject(self.c1, "o1c") + o2a = CObject(self.c2, "o2a") + o2b = CObject(self.c2, "o2b") + + setLinks({o1a: [o2a, o2b], o1b: [o2a], o1c: [o2b]}) + + eq_(o1a.links, [o2a, o2b]) + eq_(o1b.links, [o2a]) + eq_(o1c.links, [o2b]) + eq_(o2a.links, [o1a, o1b]) + eq_(o2b.links, [o1a, o1c]) + + setLinks({o2a: [o1a, o1b]}) + try: + setLinks({o2b: []}) + exceptionExpected_() + except CException as e: + eq_(e.value, "matching association not found for source 'o2b' and targets '[]'") + setLinks({o2b: []}, association = a) + + eq_(o1a.links, [o2a]) + eq_(o1b.links, [o2a]) + eq_(o1c.links, []) + eq_(o2a.links, [o1a, o1b]) + eq_(o2b.links, []) + + def testRemoveNToNLink(self): + self.c1.association(self.c2, name = "l", sourceMultiplicity = "*", multiplicity = "*") + o1 = CObject(self.c1, "o2") + o2 = CObject(self.c2, "o2") + o3 = CObject(self.c2, "o3") + o4 = CObject(self.c1, "o4") + setLinks({o1: [o2, o3], o4: o2}) + setLinks({o1: o2, o4: [o3, o2]}) + eq_(o1.links, [o2]) + eq_(o2.links, [o1, o4]) + eq_(o3.links, [o4]) + eq_(o4.links, [o3, o2]) + + def testNToNSetSelfLink(self): + self.c1.association(self.c1, name = "a", sourceMultiplicity = "*", multiplicity = "*", sourceRoleName = "super", roleName = "sub") + + top = CObject(self.c1, "Top") + mid1 = CObject(self.c1, "Mid1") + mid2 = CObject(self.c1, "Mid2") + mid3 = CObject(self.c1, "Mid3") + bottom1 = CObject(self.c1, "Bottom1") + bottom2 = CObject(self.c1, "Bottom2") + + setLinks({top: [mid1, mid2, mid3]}, roleName = "sub") + addLinks({mid1: [bottom1, bottom2]}, roleName = "sub") + + eq_(top.links, [mid1, mid2, mid3]) + eq_(mid1.links, [top, bottom1, bottom2]) + eq_(mid2.links, [top]) + eq_(mid3.links, [top]) + eq_(bottom1.links, [mid1]) + eq_(bottom2.links, [mid1]) + + eq_(top.getLinks(roleName = "sub"), [mid1, mid2, mid3]) + eq_(mid1.getLinks(roleName = "sub"), [bottom1, bottom2]) + eq_(mid2.getLinks(roleName = "sub"), []) + eq_(mid3.getLinks(roleName = "sub"), []) + eq_(bottom1.getLinks(roleName = "sub"), []) + eq_(bottom2.getLinks(roleName = "sub"), []) + + eq_(top.getLinks(roleName = "super"), []) + eq_(mid1.getLinks(roleName = "super"), [top]) + eq_(mid2.getLinks(roleName = "super"), [top]) + eq_(mid3.getLinks(roleName = "super"), [top]) + eq_(bottom1.getLinks(roleName = "super"), [mid1]) + eq_(bottom2.getLinks(roleName = "super"), [mid1]) + + def testNToNSetSelfLinkDeleteLinks(self): + self.c1.association(self.c1, name = "a", sourceMultiplicity = "*", multiplicity = "*", sourceRoleName = "super", roleName = "sub") + + top = CObject(self.c1, "Top") + mid1 = CObject(self.c1, "Mid1") + mid2 = CObject(self.c1, "Mid2") + mid3 = CObject(self.c1, "Mid3") + bottom1 = CObject(self.c1, "Bottom1") + bottom2 = CObject(self.c1, "Bottom2") + + setLinks({top: [mid1, mid2, mid3], mid1: [bottom1, bottom2]}, roleName = "sub") + # delete links + setLinks({top: []}, roleName = "sub") + eq_(top.links, []) + eq_(mid1.links, [bottom1, bottom2]) + # change links + setLinks({mid1: top, mid3: top, bottom1: mid1, bottom2: mid1}, roleName = "super") + + eq_(top.links, [mid1, mid3]) + eq_(mid1.links, [top, bottom1, bottom2]) + eq_(mid2.links, []) + eq_(mid3.links, [top]) + eq_(bottom1.links, [mid1]) + eq_(bottom2.links, [mid1]) + + eq_(top.getLinks(roleName = "sub"), [mid1, mid3]) + eq_(mid1.getLinks(roleName = "sub"), [bottom1, bottom2]) + eq_(mid2.getLinks(roleName = "sub"), []) + eq_(mid3.getLinks(roleName = "sub"), []) + eq_(bottom1.getLinks(roleName = "sub"), []) + eq_(bottom2.getLinks(roleName = "sub"), []) + + eq_(top.getLinks(roleName = "super"), []) + eq_(mid1.getLinks(roleName = "super"), [top]) + eq_(mid2.getLinks(roleName = "super"), []) + eq_(mid3.getLinks(roleName = "super"), [top]) + eq_(bottom1.getLinks(roleName = "super"), [mid1]) + eq_(bottom2.getLinks(roleName = "super"), [mid1]) + + def testIncompatibleClassifier(self): + self.c1.association(self.c2, name = "l", multiplicity = "*") + cl = CClass(self.mcl, "CLX") + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c2, "o2") + o3 = CObject(cl, "o3") + try: + setLinks({o1: [o2, o3]}) + exceptionExpected_() + except CException as e: + eq_(e.value, "object 'o3' has an incompatible classifier") + try: + setLinks({o1: o3}) + exceptionExpected_() + except CException as e: + eq_(e.value, "matching association not found for source 'o1' and targets '['o3']'") + + def testDuplicateAssignment(self): + a = self.c1.association(self.c2, "l: *->*") + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c2, "o2") + try: + setLinks({o1: [o2, o2]}) + exceptionExpected_() + except CException as e: + eq_(e.value, "trying to link the same link twice 'o1 -> o2'' twice for the same association") + eq_(o1.getLinks(), []) + eq_(o2.getLinks(), []) + b = self.c1.association(self.c2, "l: *->*") + o1.addLinks(o2, association = a) + o1.addLinks(o2, association = b) + eq_(o1.getLinks(), [o2, o2]) + eq_(o2.getLinks(), [o1, o1]) + + def testNonExistingRoleName(self): + self.c1.association(self.c1, roleName = "next", sourceRoleName = "prior", + sourceMultiplicity = "1", multiplicity = "1") + o1 = CObject(self.c1, "o1") + try: + setLinks({o1 : o1}, roleName = "target") + exceptionExpected_() + except CException as e: + eq_(e.value, "matching association not found for source 'o1' and targets '['o1']'") + + def testLinkAssociationAmbiguous(self): + self.c1.association(self.c2, name = "a1", roleName = "c2", multiplicity = "*") + self.c1.association(self.c2, name = "a2", roleName = "c2", multiplicity = "*") + + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c2, "o2") + try: + setLinks({o1:o2}) + exceptionExpected_() + except CException as e: + eq_(e.value, "link specification ambiguous, multiple matching associations found for source 'o1' and targets '['o2']'") + try: + setLinks({o1:o2}, roleName = "c2") + exceptionExpected_() + except CException as e: + eq_(e.value, "link specification ambiguous, multiple matching associations found for source 'o1' and targets '['o2']'") + + def testLinkAndGetLinksByAssociation(self): + a1 = self.c1.association(self.c2, name = "a1", multiplicity = "*") + a2 = self.c1.association(self.c2, name = "a2", multiplicity = "*") + + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c2, "o2") + o3 = CObject(self.c2, "o3") + + setLinks({o1:o2}, association = a1) + setLinks({o1:[o2, o3]}, association = a2) + + eq_(o1.getLinks(), [o2, o2, o3]) + eq_(o1.links, [o2, o2, o3]) + + eq_(o1.getLinks(association = a1), [o2]) + eq_(o1.getLinks(association = a2), [o2, o3]) + + def testLinkWithInheritanceInClassifierTargets(self): + subCl = CClass(self.mcl, superclasses = self.c2) + a1 = self.c1.association(subCl, name = "a1", multiplicity = "*") + a2 = self.c1.association(self.c2, name = "a2", multiplicity = "*") + + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c1, "o2") + o_sub_1 = CObject(subCl, "o_sub_1") + o_sub_2 = CObject(subCl, "o_sub_2") + o_super_1 = CObject(self.c2, "o_super_1") + o_super_2 = CObject(self.c2, "o_super_2") + try: + # ambiguous, list works for both associations + setLinks({o1: [o_sub_1, o_sub_2]}) + exceptionExpected_() + except CException as e: + eq_(e.value, "link specification ambiguous, multiple matching associations found for source 'o1' and targets '['o_sub_1', 'o_sub_2']'") + setLinks({o1: [o_sub_1, o_sub_2]}, association = a1) + setLinks({o1: [o_sub_1]}, association = a2) + setLinks({o2: [o_super_1, o_super_2]}) + + eq_(o1.links, [o_sub_1, o_sub_2, o_sub_1]) + eq_(o1.getLinks(), [o_sub_1, o_sub_2, o_sub_1]) + eq_(o2.getLinks(), [o_super_1, o_super_2]) + eq_(o1.getLinks(association = a1), [o_sub_1, o_sub_2]) + eq_(o1.getLinks(association = a2), [o_sub_1]) + eq_(o2.getLinks(association = a1), []) + eq_(o2.getLinks(association = a2), [o_super_1, o_super_2]) + + # this mixed list is applicable only for a2 + setLinks({o2: [o_sub_1, o_super_1]}) + eq_(o2.getLinks(association = a1), []) + eq_(o2.getLinks(association = a2), [o_sub_1, o_super_1]) + + def testLinkWithInheritanceInClassifierTargets_UsingRoleNames(self): + subCl = CClass(self.mcl, superclasses = self.c2) + a1 = self.c1.association(subCl, "a1: * -> [subCl] *") + a2 = self.c1.association(self.c2, "a2: * -> [c2] *") + + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c1, "o2") + o_sub_1 = CObject(subCl, "o_sub_1") + o_sub_2 = CObject(subCl, "o_sub_2") + o_super_1 = CObject(self.c2, "o_super_1") + o_super_2 = CObject(self.c2, "o_super_2") + setLinks({o1: [o_sub_1, o_sub_2]}, roleName = "subCl") + setLinks({o1: [o_sub_1]}, roleName = "c2") + setLinks({o2: [o_super_1, o_super_2]}) + + eq_(o1.links, [o_sub_1, o_sub_2, o_sub_1]) + eq_(o1.getLinks(), [o_sub_1, o_sub_2, o_sub_1]) + eq_(o2.getLinks(), [o_super_1, o_super_2]) + eq_(o1.getLinks(association = a1), [o_sub_1, o_sub_2]) + eq_(o1.getLinks(association = a2), [o_sub_1]) + eq_(o2.getLinks(association = a1), []) + eq_(o2.getLinks(association = a2), [o_super_1, o_super_2]) + eq_(o1.getLinks(roleName = "subCl"), [o_sub_1, o_sub_2]) + eq_(o1.getLinks(roleName = "c2"), [o_sub_1]) + eq_(o2.getLinks(roleName = "subCl"), []) + eq_(o2.getLinks(roleName = "c2"), [o_super_1, o_super_2]) + + def testLinkDeleteAssociation(self): + a = self.c1.association(self.c2, name = "l", sourceMultiplicity = "*", multiplicity = "*") + o1 = CObject(self.c1, "o2") + o2 = CObject(self.c2, "o2") + o3 = CObject(self.c2, "o3") + o4 = CObject(self.c1, "o4") + setLinks({o1: [o2, o3]}) + setLinks({o4: [o2]}) + setLinks({o1: [o2]}) + setLinks({o4: [o3, o2]}) + a.delete() + eq_(o1.links, []) + eq_(o2.links, []) + eq_(o3.links, []) + eq_(o4.links, []) + try: + setLinks({o1: [o2, o3]}) + exceptionExpected_() + except CException as e: + eq_(e.value, "matching association not found for source 'o2' and targets '['o2', 'o3']'") + + def testOneToOneLinkMultiplicity(self): + a = self.c1.association(self.c2, name = "l", multiplicity = "1", sourceMultiplicity = "1..1") + + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c2, "o2") + o3 = CObject(self.c2, "o3") + o4 = CObject(self.c1, "o4") + + try: + setLinks({o1: []}, association = a) + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'o1' have wrong multiplicity '0': should be '1'") + try: + setLinks({o1: [o2, o3]}) + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'o1' have wrong multiplicity '2': should be '1'") + + try: + setLinks({o2: []}, association = a) + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'o2' have wrong multiplicity '0': should be '1..1'") + try: + setLinks({o2: [o1, o4]}) + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'o2' have wrong multiplicity '2': should be '1..1'") + + def testOneToNLinkMultiplicity(self): + a = self.c1.association(self.c2, name = "l", sourceMultiplicity = "1", multiplicity = "1..*") + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c2, "o2") + o3 = CObject(self.c2, "o3") + o4 = CObject(self.c1, "o4") + + try: + setLinks({o1: []}, association = a) + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'o1' have wrong multiplicity '0': should be '1..*'") + + setLinks({o1: [o2, o3]}) + eq_(o1.getLinks(association = a), [o2, o3]) + + try: + setLinks({o2: []}, association = a) + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'o2' have wrong multiplicity '0': should be '1'") + try: + setLinks({o2: [o1, o4]}) + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'o2' have wrong multiplicity '2': should be '1'") + + + def testSpecificNToNLinkMultiplicity(self): + a = self.c1.association(self.c2, name = "l", sourceMultiplicity = "1..2", multiplicity = "2") + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c2, "o2") + o3 = CObject(self.c2, "o3") + o4 = CObject(self.c1, "o4") + o5 = CObject(self.c1, "o5") + o6 = CObject(self.c2, "o6") + + try: + setLinks({o1: []}, association = a) + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'o1' have wrong multiplicity '0': should be '2'") + try: + setLinks({o1: [o2]}, association = a) + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'o1' have wrong multiplicity '1': should be '2'") + try: + setLinks({o1: [o2, o3, o6]}, association = a) + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'o1' have wrong multiplicity '3': should be '2'") + + setLinks({o1: [o2, o3]}) + eq_(o1.getLinks(association = a), [o2, o3]) + setLinks({o2: [o1, o4], o1: o3, o4: o3}) + eq_(o2.getLinks(association = a), [o1, o4]) + + try: + setLinks({o2: []}, association = a) + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'o2' have wrong multiplicity '0': should be '1..2'") + try: + setLinks({o2: [o1, o4, o5]}) + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'o2' have wrong multiplicity '3': should be '1..2'") + + def testGetLinkObjects(self): + c1Sub = CClass(self.mcl, "C1Sub", superclasses = self.c1) + c2Sub = CClass(self.mcl, "C2Sub", superclasses = self.c2) + a1 = self.c1.association(self.c2, roleName = "c2", sourceRoleName = "c1", + sourceMultiplicity = "*", multiplicity = "*") + a2 = self.c1.association(self.c1, roleName = "next", sourceRoleName = "prior", + sourceMultiplicity = "1", multiplicity = "0..1") + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c2, "o2") + o1Sub = CObject(c1Sub, "o1Sub") + o2Sub = CObject(c2Sub, "o2Sub") + + linkObjects1 = setLinks({o1: o2}) + eq_(linkObjects1, o1.linkObjects) + link1 = o1.linkObjects[0] + link2 = [o for o in o1.linkObjects if o.association == a1][0] + eq_(link1, link2) + eq_(link1.association, a1) + eq_(link1.source, o1) + eq_(link1.target, o2) + + linkObjects2 = setLinks({o1: o2Sub}) + eq_(linkObjects2, o1.linkObjects) + eq_(len(o1.linkObjects), 1) + link1 = o1.linkObjects[0] + link2 = [o for o in o1.linkObjects if o.association == a1][0] + eq_(link1, link2) + eq_(link1.association, a1) + eq_(link1.source, o1) + eq_(link1.target, o2Sub) + + linkObjects3 = setLinks({o1: o2}) + eq_(linkObjects3, o1.linkObjects) + eq_(len(o1.linkObjects), 1) + link1 = o1.linkObjects[0] + link2 = [o for o in o1.linkObjects if o.association == a1][0] + eq_(link1, link2) + eq_(link1.association, a1) + eq_(link1.source, o1) + eq_(link1.target, o2) + + linkObjects4 = setLinks({o1: o1}, roleName = "next") + eq_(linkObjects3 + linkObjects4, o1.linkObjects) + eq_(len(o1.linkObjects), 2) + link1 = o1.linkObjects[1] + link2 = [o for o in o1.linkObjects if o.association == a2][0] + eq_(link1, link2) + eq_(link1.association, a2) + eq_(link1.source, o1) + eq_(link1.target, o1) + + linkObjects5 = setLinks({o1: o1Sub}, roleName = "next") + eq_(linkObjects3 + linkObjects5, o1.linkObjects) + eq_(len(o1.linkObjects), 2) + link1 = o1.linkObjects[1] + link2 = [o for o in o1.linkObjects if o.association == a2][0] + eq_(link1, link2) + eq_(link1.association, a2) + eq_(link1.source, o1) + eq_(link1.target, o1Sub) + + setLinks({o1: o1}, roleName = "next") + eq_(len(o1.linkObjects), 2) + link1 = o1.linkObjects[1] + link2 = [o for o in o1.linkObjects if o.association == a2][0] + eq_(link1, link2) + eq_(link1.association, a2) + eq_(link1.source, o1) + eq_(link1.target, o1) + + setLinks({o1: []}, association = a1) + setLinks({o1: []}, association = a2) + eq_(len(o1.linkObjects), 0) + + setLinks({o1Sub: o1}, roleName = "next") + eq_(len(o1Sub.linkObjects), 1) + link1 = o1Sub.linkObjects[0] + link2 = [o for o in o1Sub.linkObjects if o.association == a2][0] + eq_(link1, link2) + eq_(link1.association, a2) + eq_(link1.source, o1Sub) + eq_(link1.target, o1) + + def testGetLinkObjectsSelfLink(self): + a1 = self.c1.association(self.c1, roleName = "to", sourceRoleName = "from", + sourceMultiplicity = "*", multiplicity = "*") + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c1, "o2") + o3 = CObject(self.c1, "o3") + o4 = CObject(self.c1, "o4") + + setLinks({o1: [o2, o3, o1]}) + o4.addLinks([o1, o3]) + link1 = o1.linkObjects[0] + link2 = [o for o in o1.linkObjects if o.association == a1][0] + link3 = [o for o in o1.linkObjects if o.roleName == "to"][0] + link4 = [o for o in o1.linkObjects if o.sourceRoleName == "from"][0] + eq_(link1, link2) + eq_(link1, link3) + eq_(link1, link4) + eq_(link1.association, a1) + eq_(link1.source, o1) + eq_(link1.target, o2) + + eq_(len(o1.linkObjects), 4) + eq_(len(o2.linkObjects), 1) + eq_(len(o3.linkObjects), 2) + eq_(len(o4.linkObjects), 2) + + def testAddLinks(self): + a1 = self.c1.association(self.c2, "1 -> [role1] *") + a2 = self.c1.association(self.c2, "* -> [role2] *") + a3 = self.c1.association(self.c2, "1 -> [role3] 1") + + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c2, "o2") + o3 = CObject(self.c2, "o3") + o4 = CObject(self.c2, "o4") + + addLinks({o1: o2}, roleName = "role1") + eq_(o1.getLinks(roleName = "role1"), [o2]) + addLinks({o1: [o3, o4]}, roleName = "role1") + o1.getLinks(roleName = "role1") + eq_(o1.getLinks(roleName = "role1"), [o2, o3, o4]) + + o1.addLinks(o2, roleName = "role2") + eq_(o1.getLinks(roleName = "role2"), [o2]) + o1.addLinks([o3, o4], roleName = "role2") + o1.getLinks(roleName = "role2") + eq_(o1.getLinks(roleName = "role2"), [o2, o3, o4]) + + addLinks({o1: o2}, roleName = "role3") + eq_(o1.getLinks(roleName = "role3"), [o2]) + try: + addLinks({o1: [o3, o4]}, roleName = "role3") + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'o1' have wrong multiplicity '3': should be '1'") + + try: + addLinks({o1: [o3]}, roleName = "role3") + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'o1' have wrong multiplicity '2': should be '1'") + eq_(o1.getLinks(roleName = "role3"), [o2]) + + + def testLinkSourceMultiplicity(self): + a1 = self.c1.association(self.c2, "[sourceRole1] 1 -> [role1] *") + a2 = self.c1.association(self.c2, "[sourceRole2] 1 -> [role2] 1") + + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c1, "o2") + o3 = CObject(self.c2, "o3") + o4 = CObject(self.c2, "o4") + o5 = CObject(self.c2, "o5") + + setLinks({o1: o3}, roleName = "role1") + setLinks({o2: o3}, roleName = "role1") + + eq_(o3.getLinks(roleName = "sourceRole1"), [o2]) + + def testAddLinksSourceMultiplicity(self): + a1 = self.c1.association(self.c2, "[sourceRole1] 1 -> [role1] *") + a2 = self.c1.association(self.c2, "[sourceRole2] 1 -> [role2] 1") + + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c1, "o2") + o3 = CObject(self.c2, "o3") + o4 = CObject(self.c2, "o4") + o5 = CObject(self.c2, "o5") + o6 = CObject(self.c2, "o6") + + addLinks({o2: o3}, roleName = "role1") + addLinks({o2: o4}, roleName = "role1") + + eq_(o3.getLinks(roleName = "sourceRole1"), [o2]) + + addLinks({o2: o5}, roleName = "role1") + eq_(o2.getLinks(roleName = "role1"), [o3, o4, o5]) + + try: + addLinks({o1: [o4]}, roleName = "role1") + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'o4' have wrong multiplicity '2': should be '1'") + + addLinks({o1: o6}, roleName = "role2") + eq_(o1.getLinks(roleName = "role2"), [o6]) + try: + addLinks({o1: [o3, o4]}, roleName = "role2") + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'o1' have wrong multiplicity '3': should be '1'") + eq_(o1.getLinks(roleName = "role2"), [o6]) + + + def testSetLinksMultipleLinksInDefinition(self): + a1 = self.c1.association(self.c2, "[sourceRole1] * -> [role1] *") + + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c1, "o2") + o3 = CObject(self.c1, "o3") + o4 = CObject(self.c2, "o4") + o5 = CObject(self.c2, "o5") + + setLinks({o1: o4, o2: [o4], o5: [o1, o2, o3]}) + eq_(o1.getLinks(), [o4, o5]) + eq_(o2.getLinks(), [o4, o5]) + eq_(o3.getLinks(), [o5]) + eq_(o4.getLinks(), [o1, o2]) + eq_(o5.getLinks(), [o1, o2, o3]) + + def testAddLinksMultipleLinksInDefinition(self): + a1 = self.c1.association(self.c2, "[sourceRole1] * -> [role1] *") + + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c1, "o2") + o3 = CObject(self.c1, "o3") + o4 = CObject(self.c2, "o4") + o5 = CObject(self.c2, "o5") + + addLinks({o1: o4, o2: [o4], o5: [o1, o2, o3]}) + eq_(o1.getLinks(), [o4, o5]) + eq_(o2.getLinks(), [o4, o5]) + eq_(o3.getLinks(), [o5]) + eq_(o4.getLinks(), [o1, o2]) + eq_(o5.getLinks(), [o1, o2, o3]) + + def testWrongTypesDeleteLinks(self): + self.c1.association(self.c2, name = "l", multiplicity = "1") + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c2, "o2") + try: + deleteLinks(o1) + exceptionExpected_() + except CException as e: + eq_(e.value, "link definitions should be of the form {: , ..., : }") + try: + deleteLinks({o1: self.mcl}) + exceptionExpected_() + except CException as e: + eq_(e.value, "link target 'MCL' is neither an object nor a class") + try: + deleteLinks({o1: [o2, self.mcl]}) + exceptionExpected_() + except CException as e: + eq_(e.value, "link target 'MCL' is neither an object nor a class") + try: + deleteLinks({o1: [o2, None]}) + exceptionExpected_() + except CException as e: + eq_(e.value, "link target 'None' is neither an object nor a class") + try: + deleteLinks({self.mcl: o2}) + exceptionExpected_() + except CException as e: + eq_(e.value, "link source 'MCL' is neither an object nor a class") + try: + deleteLinks({None: o2}) + exceptionExpected_() + except CException as e: + eq_(e.value, "link should not contain an empty source") + + def testDeleteOneToOneLink(self): + a = self.c1.association(self.c2, "l: 1 -> [c2] 0..1") + + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c2, "o2") + o3 = CObject(self.c1, "o3") + o4 = CObject(self.c2, "o4") + + links = addLinks({o1: o2, o3: o4}) + o1.deleteLinks(o2) + eq_(o1.links, []) + eq_(o2.links, []) + eq_(o3.links, [o4]) + eq_(o4.links, [o3]) + eq_(o1.linkObjects, []) + eq_(o2.linkObjects, []) + eq_(o3.linkObjects, [links[1]]) + eq_(o4.linkObjects, [links[1]]) + deleteLinks({o3: o4}) + eq_(o1.links, []) + eq_(o2.links, []) + eq_(o3.links, []) + eq_(o4.links, []) + eq_(o1.linkObjects, []) + eq_(o2.linkObjects, []) + eq_(o3.linkObjects, []) + eq_(o4.linkObjects, []) + + def testDeleteOneToOneLinkWrongMultiplicity(self): + a = self.c1.association(self.c2, "l: 1 -> [c2] 1") + + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c2, "o2") + addLinks({o1: o2}) + try: + o1.deleteLinks(o2) + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'o1' have wrong multiplicity '0': should be '1'") + eq_(o1.links, [o2]) + eq_(o2.links, [o1]) + + def testDeleteOneToNLinks(self): + self.c1.association(self.c2, "l: 0..1 -> *") + + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c1, "o2") + o3 = CObject(self.c2, "o3") + o4 = CObject(self.c2, "o4") + o5 = CObject(self.c2, "o5") + + addLinks({o1: [o3, o4], o2: [o5]}) + o4.deleteLinks([o1]) + eq_(o1.links, [o3]) + eq_(o2.links, [o5]) + eq_(o3.links, [o1]) + eq_(o4.links, []) + eq_(o5.links, [o2]) + + o4.addLinks([o2]) + eq_(o2.links, [o5, o4]) + deleteLinks({o1: o3, o2: o2.links}) + eq_(o1.links, []) + eq_(o2.links, []) + eq_(o3.links, []) + eq_(o4.links, []) + eq_(o5.links, []) + + def testDeleteOneToNLinksWrongMultiplicity(self): + a = self.c1.association(self.c2, "l: 1 -> *") + + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c1, "o2") + o3 = CObject(self.c2, "o3") + o4 = CObject(self.c2, "o4") + o5 = CObject(self.c2, "o5") + + addLinks({o1: [o3, o4], o2: [o5]}) + + try: + o4.deleteLinks([o1]) + exceptionExpected_() + except CException as e: + eq_(e.value, "links of object 'o4' have wrong multiplicity '0': should be '1'") + + + def testDeleteNToNLinks(self): + self.c1.association(self.c2, "l: * -> *") + + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c1, "o2") + o3 = CObject(self.c2, "o3") + o4 = CObject(self.c2, "o4") + o5 = CObject(self.c2, "o5") + o6 = CObject(self.c2, "o6") + + addLinks({o1: [o3, o4], o2: [o4, o5]}) + o4.deleteLinks([o1, o2]) + eq_(o1.links, [o3]) + eq_(o2.links, [o5]) + eq_(o3.links, [o1]) + eq_(o4.links, []) + eq_(o5.links, [o2]) + + addLinks({o4: [o1, o2], o6: [o2, o1]}) + deleteLinks({o1: o6, o2: [o4, o5]}) + eq_(o1.links, [o3, o4]) + eq_(o2.links, [o6]) + eq_(o3.links, [o1]) + eq_(o4.links, [o1]) + eq_(o5.links, []) + eq_(o6.links, [o2]) + + def testDeleteLinkNoMatchingLink(self): + a = self.c1.association(self.c2, "l: 0..1 -> *") + + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c1, "o2") + o3 = CObject(self.c2, "o3") + o4 = CObject(self.c2, "o4") + o5 = CObject(self.c2, "o5") + + addLinks({o1: [o3, o4], o2: [o5]}, association = a) + + try: + deleteLinks({o1:o5}) + exceptionExpected_() + except CException as e: + eq_(e.value, "no link found for 'o1 -> o5' in delete links") + + b = self.c1.association(self.c2, "l: 0..1 -> *") + try: + deleteLinks({o1:o5}) + exceptionExpected_() + except CException as e: + eq_(e.value, "no link found for 'o1 -> o5' in delete links") + + try: + o4.deleteLinks([o1], association = b) + exceptionExpected_() + except CException as e: + eq_(e.value, "no link found for 'o4 -> o1' in delete links for given association") + + def testDeleteLinkSelectByAssociation(self): + a = self.c1.association(self.c2, "a: * -> *") + b = self.c1.association(self.c2, "b: * -> *") + + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c1, "o2") + o3 = CObject(self.c2, "o3") + o4 = CObject(self.c2, "o4") + + addLinks({o1: [o3], o2: [o3, o4]}, association = b) + deleteLinks({o2: o3}) + eq_(o1.links, [o3]) + eq_(o2.links, [o4]) + eq_(o3.links, [o1]) + eq_(o4.links, [o2]) + addLinks({o1: [o3], o2: [o3, o4]}, association = a) + + try: + deleteLinks({o1: o3}) + exceptionExpected_() + except CException as e: + eq_(e.value, "link definition in delete links ambiguous for link 'o1->o3': found multiple matches") + + deleteLinks({o1: o3, o2: o4}, association = b) + eq_(o1.links, [o3]) + eq_(o2.links, [o3, o4]) + eq_(o3.links, [o1, o2]) + eq_(o4.links, [o2]) + for o in [o1, o2, o3, o4]: + for lo in o.linkObjects: + eq_(lo.association, a) + + o1.addLinks(o3, association = b) + try: + o1.deleteLinks(o3) + exceptionExpected_() + except CException as e: + eq_(e.value, "link definition in delete links ambiguous for link 'o1->o3': found multiple matches") + + eq_(o1.links, [o3, o3]) + eq_(o2.links, [o3, o4]) + eq_(o3.links, [o1, o2, o1]) + eq_(o4.links, [o2]) + + o1.deleteLinks(o3, association = a) + eq_(o1.links, [o3]) + eq_(o2.links, [o3, o4]) + eq_(o3.links, [o2, o1]) + eq_(o4.links, [o2]) + + + def testDeleteLinkSelectByRoleName(self): + a = self.c1.association(self.c2, "a: [sourceA] * -> [targetA] *") + b = self.c1.association(self.c2, "b: [sourceB] * -> [targetB] *") + + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c1, "o2") + o3 = CObject(self.c2, "o3") + o4 = CObject(self.c2, "o4") + + addLinks({o1: [o3], o2: [o3, o4]}, roleName = "targetB") + deleteLinks({o2: o3}) + eq_(o1.links, [o3]) + eq_(o2.links, [o4]) + eq_(o3.links, [o1]) + eq_(o4.links, [o2]) + addLinks({o1: [o3], o2: [o3, o4]}, roleName = "targetA") + + deleteLinks({o1: o3, o2: o4}, roleName = "targetB") + eq_(o1.links, [o3]) + eq_(o2.links, [o3, o4]) + eq_(o3.links, [o1, o2]) + eq_(o4.links, [o2]) + for o in [o1, o2, o3, o4]: + for lo in o.linkObjects: + eq_(lo.association, a) + + addLinks({o1: [o3], o2: [o3, o4]}, roleName = "targetB") + o3.deleteLinks([o1, o2], roleName = "sourceB") + deleteLinks({o4: o2}, roleName = "sourceB") + + eq_(o1.links, [o3]) + eq_(o2.links, [o3, o4]) + eq_(o3.links, [o1, o2]) + eq_(o4.links, [o2]) + for o in [o1, o2, o3, o4]: + for lo in o.linkObjects: + eq_(lo.association, a) + + def testDeleteLinksWrongRoleName(self): + a = self.c1.association(self.c2, "a: [sourceA] * -> [targetA] *") + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c2, "o2") + o1.addLinks(o2) + try: + o1.deleteLinks(o2, roleName = "target") + exceptionExpected_() + except CException as e: + eq_(e.value, "no link found for 'o1 -> o2' in delete links for given role name 'target'") + + def testDeleteLinksWrongAssociation(self): + a = self.c1.association(self.c2, "a: [sourceA] * -> [targetA] *") + o1 = CObject(self.c1, "o1") + o2 = CObject(self.c2, "o2") + o1.addLinks(o2) + try: + o1.deleteLinks(o2, association = o1) + exceptionExpected_() + except CException as e: + eq_(e.value, "'o1' is not a association") + b = self.c1.association(self.c2, "b: [sourceB] * -> [targetB] *") + try: + o1.deleteLinks(o2, association = b) + exceptionExpected_() + except CException as e: + eq_(e.value, "no link found for 'o1 -> o2' in delete links for given association") + try: + o1.deleteLinks(o2, association = b, roleName = "x") + exceptionExpected_() + except CException as e: + eq_(e.value, "no link found for 'o1 -> o2' in delete links for given role name 'x' and for given association") + +if __name__ == "__main__": + nose.main() \ No newline at end of file diff --git a/tests/test_stereotype_associations.py b/tests/test_stereotype_associations.py new file mode 100644 index 0000000..d54b9e0 --- /dev/null +++ b/tests/test_stereotype_associations.py @@ -0,0 +1,200 @@ +import sys +sys.path.append("..") + +import re + +import nose +from nose.tools import ok_, eq_ +from testCommons import neq_, exceptionExpected_ +from parameterized import parameterized + +from codeableModels import CMetaclass, CStereotype, CClass, CObject, CAttribute, CException, CEnum, CBundle + +class TestStereotypeAssociations(): + + def setUp(self): + self.stereotypeBundle = CBundle("Elements") + self.s1 = CStereotype("S1", bundles = self.stereotypeBundle) + self.s2 = CStereotype("S2", bundles = self.stereotypeBundle) + self.s3 = CStereotype("S3", bundles = self.stereotypeBundle) + self.s4 = CStereotype("S4", bundles = self.stereotypeBundle) + self.s5 = CStereotype("S5", bundles = self.stereotypeBundle) + + def getAllAssociationsInBundle(self): + associations = [] + for c in self.stereotypeBundle.getElements(type=CStereotype): + for a in c.allAssociations: + if not a in associations: + associations.append(a) + return associations + + def testAssociationCreation(self): + a1 = self.s1.association(self.s2, multiplicity = "1", roleName = "t", + sourceMultiplicity = "*", sourceRoleName = "i") + a2 = self.s1.association(self.s2, "[o]*->[s]1") + a3 = self.s1.association(self.s3, "[a] 0..1 <*>- [n]*") + a4 = self.s1.association(self.s3, multiplicity = "*", roleName = "e", + sourceMultiplicity = "0..1", sourceRoleName = "a", composition = True) + a5 = self.s4.association(self.s3, multiplicity = "*", roleName = "n", + sourceMultiplicity = "0..1", sourceRoleName = "a", aggregation = True) + a6 = self.s3.association(self.s2, '[a] 0..3 <>- [e]*') + + eq_(len(self.getAllAssociationsInBundle()), 6) + + eq_(self.s1.associations[0].roleName, "t") + eq_(a5.roleName, "n") + eq_(a2.roleName, "s") + eq_(a1.multiplicity, "1") + eq_(a1.sourceMultiplicity, "*") + eq_(a4.sourceMultiplicity, "0..1") + eq_(a6.sourceMultiplicity, "0..3") + + eq_(a1.composition, False) + eq_(a1.aggregation, False) + eq_(a3.composition, True) + eq_(a3.aggregation, False) + eq_(a5.composition, False) + eq_(a5.aggregation, True) + + a1.aggregation = True + eq_(a1.composition, False) + eq_(a1.aggregation, True) + a1.composition = True + eq_(a1.composition, True) + eq_(a1.aggregation, False) + + def testMixedAssociationTypes(self): + m1 = CMetaclass("M1") + c1 = CClass(m1, "C1") + try: + a1 = self.s1.association(c1, multiplicity = "1", roleName = "t", + sourceMultiplicity = "*", sourceRoleName = "i") + exceptionExpected_() + except CException as e: + eq_("stereotype 'S1' is not compatible with association target 'C1'", e.value) + + try: + a1 = self.s1.association(m1, multiplicity = "1", roleName = "t", + sourceMultiplicity = "*", sourceRoleName = "i") + exceptionExpected_() + except CException as e: + eq_("stereotype 'S1' is not compatible with association target 'M1'", e.value) + + def testGetAssociationByRoleName(self): + a1 = self.s1.association(self.s2, multiplicity = "1", roleName = "t", + sourceMultiplicity = "*", sourceRoleName = "i") + a2 = self.s1.association(self.s2, multiplicity = "1", roleName = "s", + sourceMultiplicity = "*", sourceRoleName = "o") + a3 = self.s1.association(self.s3, multiplicity = "*", roleName = "n", + sourceMultiplicity = "0..1", sourceRoleName = "a", composition = True) + + a_2 = next(a for a in self.s1.associations if a.roleName == "s") + eq_(a_2.multiplicity, "1") + eq_(a_2.sourceRoleName, "o") + eq_(a_2.sourceMultiplicity, "*") + + def testGetAssociationByName(self): + a1 = self.s1.association(self.s2, name = "n1", multiplicity = "1", roleName = "t", + sourceMultiplicity = "*", sourceRoleName = "i") + a2 = self.s1.association(self.s2, name = "n2", multiplicity = "1", roleName = "s", + sourceMultiplicity = "*", sourceRoleName = "o") + a3 = self.s1.association(self.s3, "n3: [a] 0..1 <*>- [n] *") + + a_2 = next(a for a in self.s1.associations if a.name == "n2") + eq_(a_2.multiplicity, "1") + eq_(a_2.sourceRoleName, "o") + eq_(a_2.sourceMultiplicity, "*") + + a_3 = next(a for a in self.s1.associations if a.name == "n3") + eq_(a_3.multiplicity, "*") + eq_(a_3.roleName, "n") + eq_(a_3.sourceMultiplicity, "0..1") + eq_(a_3.sourceRoleName, "a") + eq_(a_3.composition, True) + + def testGetAssociations(self): + a1 = self.s1.association(self.s2, multiplicity = "1", roleName = "t", + sourceMultiplicity = "*", sourceRoleName = "i") + a2 = self.s1.association(self.s2, multiplicity = "1", roleName = "s", + sourceMultiplicity = "*", sourceRoleName = "o") + a3 = self.s1.association(self.s3, multiplicity = "*", roleName = "n", + sourceMultiplicity = "0..1", sourceRoleName = "a", composition = True) + a4 = self.s1.association(self.s3, multiplicity = "*", roleName = "e", + sourceMultiplicity = "0..1", sourceRoleName = "a", composition = True) + a5 = self.s4.association(self.s3, multiplicity = "*", roleName = "n", + sourceMultiplicity = "0..1", sourceRoleName = "a", aggregation = True) + a6 = self.s3.association(self.s2, multiplicity = "*", roleName = "e", + sourceMultiplicity = "1..3", sourceRoleName = "a", aggregation = True) + eq_(self.s1.associations, [a1, a2, a3, a4]) + eq_(self.s2.associations, [a1, a2, a6]) + eq_(self.s3.associations, [a3, a4, a5, a6]) + eq_(self.s4.associations, [a5]) + eq_(self.s5.associations, []) + + def testDeleteAssociations(self): + a1 = self.s1.association(self.s2, multiplicity = "1", roleName = "t", + sourceMultiplicity = "*", sourceRoleName = "i") + a2 = self.s1.association(self.s2, multiplicity = "1", roleName = "s", + sourceMultiplicity = "*", sourceRoleName = "o") + a3 = self.s1.association(self.s3, multiplicity = "*", roleName = "n", + sourceMultiplicity = "0..1", sourceRoleName = "a", composition = True) + a4 = self.s1.association(self.s3, multiplicity = "*", roleName = "e", + sourceMultiplicity = "0..1", sourceRoleName = "a", composition = True) + a5 = self.s4.association(self.s3, multiplicity = "*", roleName = "n", + sourceMultiplicity = "0..1", sourceRoleName = "a", aggregation = True) + a6 = self.s3.association(self.s2, multiplicity = "*", roleName = "e", + sourceMultiplicity = "0..3", sourceRoleName = "a", aggregation = True) + a7 = self.s1.association(self.s1, multiplicity = "*", roleName = "x", + sourceMultiplicity = "1..3", sourceRoleName = "y") + + eq_(len(self.getAllAssociationsInBundle()), 7) + + a2.delete() + a4.delete() + + eq_(len(self.getAllAssociationsInBundle()), 5) + + eq_(self.s1.associations, [a1, a3, a7]) + eq_(self.s2.associations, [a1, a6]) + eq_(self.s3.associations, [a3, a5, a6]) + eq_(self.s4.associations, [a5]) + eq_(self.s5.associations, []) + + + def testDeleteClassAndGetAssociations(self): + a1 = self.s1.association(self.s2, multiplicity = "1", roleName = "t", + sourceMultiplicity = "*", sourceRoleName = "i") + a2 = self.s1.association(self.s2, multiplicity = "1", roleName = "s", + sourceMultiplicity = "*", sourceRoleName = "o") + a3 = self.s1.association(self.s3, multiplicity = "*", roleName = "n", + sourceMultiplicity = "0..1", sourceRoleName = "a", composition = True) + a4 = self.s1.association(self.s3, multiplicity = "*", roleName = "e", + sourceMultiplicity = "0..1", sourceRoleName = "a", composition = True) + a5 = self.s4.association(self.s3, multiplicity = "*", roleName = "n", + sourceMultiplicity = "0..1", sourceRoleName = "a", aggregation = True) + a6 = self.s3.association(self.s2, multiplicity = "*", roleName = "e", + sourceMultiplicity = "0..3", sourceRoleName = "a", aggregation = True) + a7 = self.s1.association(self.s1, multiplicity = "*", roleName = "x", + sourceMultiplicity = "1..3", sourceRoleName = "y") + + eq_(len(self.getAllAssociationsInBundle()), 7) + + self.s1.delete() + + eq_(len(self.getAllAssociationsInBundle()), 2) + + eq_(self.s1.associations, []) + eq_(self.s2.associations, [a6]) + eq_(self.s3.associations, [a5, a6]) + eq_(self.s4.associations, [a5]) + eq_(self.s5.associations, []) + + def testAllAssociations(self): + s = CStereotype("S") + d = CStereotype("D", superclasses = s) + a = s.association(d, "is next: [prior s] * -> [next d] *") + eq_(d.allAssociations, [a]) + eq_(s.allAssociations, [a]) + +if __name__ == "__main__": + nose.main() \ No newline at end of file diff --git a/tests/test_stereotype_attributes.py b/tests/test_stereotype_attributes.py new file mode 100644 index 0000000..3bb84b3 --- /dev/null +++ b/tests/test_stereotype_attributes.py @@ -0,0 +1,260 @@ +import sys +sys.path.append("..") + +import nose +from nose.tools import ok_, eq_ +from testCommons import neq_, exceptionExpected_ +from parameterized import parameterized + +import re + +from codeableModels import CMetaclass, CClass, CObject, CAttribute, CException, CEnum, CStereotype + +class TestMetaClassAttributes(): + def setUp(self): + self.mcl = CMetaclass("MCL") + self.stereotype = CStereotype("S", extended = self.mcl) + + def testPrimitiveEmptyInput(self): + cl = CStereotype("S", extended = self.mcl, attributes = {}) + eq_(len(cl.attributes), 0) + eq_(len(cl.attributeNames), 0) + + def testPrimitiveNoneInput(self): + cl = CStereotype("S", extended = self.mcl, attributes = None) + eq_(len(cl.attributes), 0) + eq_(len(cl.attributeNames), 0) + + def testPrimitiveTypeAttributes(self): + cl = CStereotype("S", extended = self.mcl, attributes = { + "isBoolean": True, + "intVal": 1, + "floatVal": 1.1, + "string": "abc"}) + eq_(len(cl.attributes), 4) + eq_(len(cl.attributeNames), 4) + + ok_(set(["isBoolean", "intVal", "floatVal", "string"]).issubset(cl.attributeNames)) + + a1 = cl.getAttribute("isBoolean") + a2 = cl.getAttribute("intVal") + a3 = cl.getAttribute("floatVal") + a4 = cl.getAttribute("string") + ok_(set([a1, a2, a3, a4]).issubset(cl.attributes)) + eq_(None, cl.getAttribute("X")) + + eq_(a1.type, bool) + eq_(a2.type, int) + eq_(a3.type, float) + eq_(a4.type, str) + + d1 = a1.default + d2 = a2.default + d3 = a3.default + d4 = a4.default + + ok_(isinstance(d1, bool)) + ok_(isinstance(d2, int)) + ok_(isinstance(d3, float)) + ok_(isinstance(d4, str)) + + eq_(d1, True) + eq_(d2, 1) + eq_(d3, 1.1) + eq_(d4, "abc") + + def testAttributeGetNameAndClassifier(self): + cl = CStereotype("S", attributes = {"isBoolean": True}) + a = cl.getAttribute("isBoolean") + eq_(a.name, "isBoolean") + eq_(a.classifier, cl) + cl.delete() + eq_(a.name, None) + eq_(a.classifier, None) + + def testPrimitiveAttributesNoDefault(self): + self.stereotype.attributes = {"a": bool, "b": int, "c": str, "d": float} + a1 = self.stereotype.getAttribute("a") + a2 = self.stereotype.getAttribute("b") + a3 = self.stereotype.getAttribute("c") + a4 = self.stereotype.getAttribute("d") + ok_(set([a1, a2, a3, a4]).issubset(self.stereotype.attributes)) + eq_(a1.default, None) + eq_(a1.type, bool) + eq_(a2.default, None) + eq_(a2.type, int) + eq_(a3.default, None) + eq_(a3.type, str) + eq_(a4.default, None) + eq_(a4.type, float) + + def testGetAttributeNotFound(self): + eq_(self.stereotype.getAttribute("x"), None) + self.stereotype.attributes = {"a": bool, "b": int, "c": str, "d": float} + eq_(self.stereotype.getAttribute("x"), None) + + def testSameNamedArgumentsCAttributes(self): + a1 = CAttribute(default = "") + a2 = CAttribute(type = str) + n1 = "a" + self.stereotype.attributes = {n1: a1, "a": a2} + eq_(set(self.stereotype.attributes), {a2}) + eq_(self.stereotype.attributeNames, ["a"]) + + def testSameNamedArgumentsDefaults(self): + n1 = "a" + self.stereotype.attributes = {n1: "", "a": 1} + ok_(len(self.stereotype.attributes), 1) + eq_(self.stereotype.getAttribute("a").default, 1) + eq_(self.stereotype.attributeNames, ["a"]) + + def testObjectTypeAttribute(self): + attrType = CClass(self.mcl, "AttrType") + attrValue = CObject(attrType, "attrValue") + self.stereotype.attributes = {"attrTypeObj" : attrValue} + objAttr = self.stereotype.getAttribute("attrTypeObj") + attributes = self.stereotype.attributes + eq_(set(attributes), {objAttr}) + + boolAttr = CAttribute(default = True) + self.stereotype.attributes = {"attrTypeObj" : objAttr, "isBoolean" : boolAttr} + attributes = self.stereotype.attributes + eq_(set(attributes), {objAttr, boolAttr}) + eq_(self.stereotype.attributeNames, ["attrTypeObj", "isBoolean"]) + objAttr = self.stereotype.getAttribute("attrTypeObj") + eq_(objAttr.type, attrType) + default = objAttr.default + ok_(isinstance(default, CObject)) + eq_(default, attrValue) + + self.stereotype.attributes = {"attrTypeObj" : attrValue, "isBoolean" : boolAttr} + eq_(self.stereotype.attributeNames, ["attrTypeObj", "isBoolean"]) + # using the CObject in attributes causes a new CAttribute to be created != objAttr + neq_(self.stereotype.getAttribute("attrTypeObj"), objAttr) + + def testUseEnumTypeAttribute(self): + enumValues = ["A", "B", "C"] + enumObj = CEnum("ABCEnum", values = enumValues) + ea1 = CAttribute(type = enumObj, default = "A") + ea2 = CAttribute(type = enumObj) + self.stereotype.attributes = {"letters1": ea1, "letters2": ea2} + eq_(set(self.stereotype.attributes), {ea1, ea2}) + ok_(isinstance(ea1.type, CEnum)) + + self.stereotype.attributes = {"letters1": ea1, "isBool": True, "letters2": ea2} + boolAttr = self.stereotype.getAttribute("isBool") + l1 = self.stereotype.getAttribute("letters1") + eq_(set(self.stereotype.attributes), {l1, ea2, boolAttr}) + eq_(l1.default, "A") + eq_(ea2.default, None) + + def testUnknownAttributeType(self): + try: + self.stereotype.attributes = {"x": CEnum, "b" : bool} + exceptionExpected_() + except CException as e: + ok_(re.match("^(unknown attribute type: '')$", e.value)) + + + def testSetAttributeDefaultValue(self): + enumObj = CEnum("ABCEnum", values = ["A", "B", "C"]) + self.stereotype.attributes = {"letters": enumObj, "b" : bool} + l = self.stereotype.getAttribute("letters") + b = self.stereotype.getAttribute("b") + eq_(l.default, None) + eq_(b.default, None) + l.default = "B" + b.default = False + eq_(l.default, "B") + eq_(b.default, False) + eq_(l.type, enumObj) + eq_(b.type, bool) + + def testCClassVsCObject(self): + cl_a = CClass(self.mcl, "A") + cl_b = CClass(self.mcl, "B") + obj_b = CObject(cl_b, "obj_b") + + self.stereotype.attributes = {"a": cl_a, "b": obj_b} + a = self.stereotype.getAttribute("a") + b = self.stereotype.getAttribute("b") + eq_(a.type, cl_a) + eq_(a.default, None) + eq_(b.type, cl_b) + eq_(b.default, obj_b) + + testMetaclass = CMetaclass("A") + testEnum = CEnum("AEnum", values = [1,2]) + testClass = CClass(testMetaclass, "CL") + + @parameterized.expand([ + (bool, testMetaclass), + (bool, 1.1), + (int, testMetaclass), + (int, "abc"), + (float, "1"), + (float, testMetaclass), + (str, 1), + (str, testMetaclass), + (testEnum, "1"), + (testEnum, testMetaclass), + (testClass, "1"), + (testClass, testMetaclass)]) + def testAttributeTypeCheck(self, type, wrongDefault): + self.stereotype.attributes = {"a": type} + attr = self.stereotype.getAttribute("a") + try: + attr.default = wrongDefault + exceptionExpected_() + except CException as e: + eq_(f"default value '{wrongDefault!s}' incompatible with attribute's type '{type!s}'", e.value) + + def test_DeleteAttributes(self): + self.stereotype.attributes = { + "isBoolean": True, + "intVal": 1, + "floatVal": 1.1, + "string": "abc"} + eq_(len(set(self.stereotype.attributes)), 4) + self.stereotype.attributes = {} + eq_(set(self.stereotype.attributes), set()) + self.stereotype.attributes = { + "isBoolean": True, + "intVal": 1, + "floatVal": 1.1, + "string": "abc"} + eq_(len(set(self.stereotype.attributes)), 4) + self.stereotype.attributes = {} + eq_(set(self.stereotype.attributes), set()) + + + def testTypeObjectAttributeClassIsDeletedInConstructor(self): + attrCl = CClass(self.mcl, "AC") + attrCl.delete() + try: + CStereotype("S", attributes = {"ac": attrCl}) + exceptionExpected_() + except CException as e: + eq_(e.value, "cannot access named element that has been deleted") + + def testTypeObjectAttributeClassIsNone(self): + s1 = CStereotype("S", attributes = {"ac": None}) + ac = s1.getAttribute("ac") + eq_(ac.default, None) + eq_(ac.type, None) + + def testDefaultObjectAttributeIsDeletedInConstructor(self): + attrCl = CClass(self.mcl, "AC") + defaultObj = CObject(attrCl) + defaultObj.delete() + try: + CStereotype("S", attributes = {"ac": defaultObj}) + exceptionExpected_() + except CException as e: + eq_(e.value, "cannot access named element that has been deleted") + + +if __name__ == "__main__": + nose.main() + + diff --git a/tests/test_stereotype_inheritance.py b/tests/test_stereotype_inheritance.py new file mode 100644 index 0000000..fc5a1e7 --- /dev/null +++ b/tests/test_stereotype_inheritance.py @@ -0,0 +1,388 @@ +import sys +sys.path.append("..") + +import re + +import nose +from nose.tools import ok_, eq_ +from testCommons import neq_, exceptionExpected_ +from parameterized import parameterized + +from codeableModels import CMetaclass, CStereotype, CClass, CObject, CAttribute, CException, CEnum + +class TestStereotypeInheritance(): + def setUp(self): + self.mcl = CMetaclass("MCL") + + def testStereotypeNoInheritance(self): + t = CStereotype("T") + eq_(set(t.superclasses), set()) + eq_(set(t.subclasses), set()) + eq_(set(t.allSuperclasses), set()) + eq_(set(t.allSubclasses), set()) + + def testStereotypeSuperclassesEmptyInput(self): + m1 = CStereotype("M1", superclasses = []) + eq_(set(m1.superclasses), set()) + eq_(set(m1.subclasses), set()) + + def testStereotypeSuperclassesNoneInput(self): + m1 = CStereotype("M1", superclasses = None) + eq_(set(m1.superclasses), set()) + eq_(set(m1.subclasses), set()) + + def testStereotypeSimpleInheritance(self): + t = CStereotype("T") + m1 = CStereotype("M1", superclasses = t) + m2 = CStereotype("M2", superclasses = t) + b1 = CStereotype("B1", superclasses = m1) + b2 = CStereotype("B2", superclasses = m1) + b3 = CStereotype("B3", superclasses = t) + + eq_(set(t.superclasses), set()) + eq_(set(t.subclasses), set([m1, m2, b3])) + eq_(set(t.allSuperclasses), set()) + eq_(set(t.allSubclasses), set([m1, m2, b1, b2, b3])) + + eq_(set(m1.superclasses), set([t])) + eq_(set(m1.subclasses), set([b1, b2])) + eq_(set(m1.allSuperclasses), set([t])) + eq_(set(m1.allSubclasses), set([b1, b2])) + + eq_(set(m2.superclasses), set([t])) + eq_(set(m2.subclasses), set()) + eq_(set(m2.allSuperclasses), set([t])) + eq_(set(m2.allSubclasses), set()) + + eq_(set(b1.superclasses), set([m1])) + eq_(set(b1.subclasses), set()) + eq_(set(b1.allSuperclasses), set([t, m1])) + eq_(set(b1.allSubclasses), set()) + + eq_(set(b2.superclasses), set([m1])) + eq_(set(b2.subclasses), set()) + eq_(set(b2.allSuperclasses), set([t, m1])) + eq_(set(b2.allSubclasses), set()) + + eq_(set(b3.superclasses), set([t])) + eq_(set(b3.subclasses), set()) + eq_(set(b3.allSuperclasses), set([t])) + eq_(set(b3.allSubclasses), set()) + + def testStereotypeInheritanceDoubleAssignment(self): + m = CMetaclass("M") + t = CStereotype("T") + try: + CStereotype("S1", extended = m, superclasses = [t, t]) + exceptionExpected_() + except CException as e: + eq_("'T' is already a superclass of 'S1'", e.value) + s1 = m.getStereotype("S1") + eq_(s1.name, "S1") + eq_(set(s1.superclasses), set([t])) + + def testStereotypeInheritanceDeleteTopClass(self): + t = CStereotype("T") + m1 = CStereotype("M1", superclasses = [t]) + m2 = CStereotype("M2", superclasses = [t]) + b1 = CStereotype("B1", superclasses = [m1]) + b2 = CStereotype("B2", superclasses = [m1]) + b3 = CStereotype("B3", superclasses = [t]) + + t.delete() + + eq_(t.name, None) + eq_(set(t.superclasses), set()) + eq_(set(t.subclasses), set()) + eq_(set(t.allSuperclasses), set()) + eq_(set(t.allSubclasses), set()) + + eq_(set(m1.superclasses), set()) + eq_(set(m1.subclasses), set([b1, b2])) + eq_(set(m1.allSuperclasses), set()) + eq_(set(m1.allSubclasses), set([b1, b2])) + + eq_(set(m2.superclasses), set()) + eq_(set(m2.subclasses), set()) + eq_(set(m2.allSuperclasses), set()) + eq_(set(m2.allSubclasses), set()) + + eq_(set(b1.superclasses), set([m1])) + eq_(set(b1.subclasses), set()) + eq_(set(b1.allSuperclasses), set([m1])) + eq_(set(b1.allSubclasses), set()) + + eq_(set(b2.superclasses), set([m1])) + eq_(set(b2.subclasses), set()) + eq_(set(b2.allSuperclasses), set([m1])) + eq_(set(b2.allSubclasses), set()) + + eq_(set(b3.superclasses), set()) + eq_(set(b3.subclasses), set()) + eq_(set(b3.allSuperclasses), set()) + eq_(set(b3.allSubclasses), set()) + + def testStereotypeInheritanceDeleteInnerClass(self): + t = CStereotype("T") + m1 = CStereotype("M1", superclasses = [t]) + m2 = CStereotype("M2", superclasses = [t]) + b1 = CStereotype("B1", superclasses = [m1]) + b2 = CStereotype("B2", superclasses = [m1]) + b3 = CStereotype("B3", superclasses = [t]) + + m1.delete() + + eq_(set(t.superclasses), set()) + eq_(set(t.subclasses), set([m2, b3])) + eq_(set(t.allSuperclasses), set()) + eq_(set(t.allSubclasses), set([m2, b3])) + + eq_(set(m1.superclasses), set()) + eq_(set(m1.subclasses), set()) + eq_(set(m1.allSuperclasses), set()) + eq_(set(m1.allSubclasses), set()) + + eq_(set(m2.superclasses), set([t])) + eq_(set(m2.subclasses), set()) + eq_(set(m2.allSuperclasses), set([t])) + eq_(set(m2.allSubclasses), set()) + + eq_(set(b1.superclasses), set()) + eq_(set(b1.subclasses), set()) + eq_(set(b1.allSuperclasses), set()) + eq_(set(b1.allSubclasses), set()) + + eq_(set(b2.superclasses), set()) + eq_(set(b2.subclasses), set()) + eq_(set(b2.allSuperclasses), set()) + eq_(set(b2.allSubclasses), set()) + + eq_(set(b3.superclasses), set([t])) + eq_(set(b3.subclasses), set()) + eq_(set(b3.allSuperclasses), set([t])) + eq_(set(b3.allSubclasses), set()) + + def testStereotypeSuperclassesReassignment(self): + t = CStereotype("T") + m1 = CStereotype("M1", superclasses = [t]) + m2 = CStereotype("M2", superclasses = [t]) + b1 = CStereotype("B1", superclasses = [m1]) + b2 = CStereotype("B2", superclasses = [m1]) + b3 = CStereotype("B3", superclasses = [t]) + + m1.superclasses = [] + b1.superclasses = [] + b2.superclasses = [] + + eq_(set(t.superclasses), set()) + eq_(set(t.subclasses), set([m2, b3])) + eq_(set(t.allSuperclasses), set()) + eq_(set(t.allSubclasses), set([m2, b3])) + + eq_(set(m1.superclasses), set()) + eq_(set(m1.subclasses), set()) + eq_(set(m1.allSuperclasses), set()) + eq_(set(m1.allSubclasses), set()) + + eq_(set(m2.superclasses), set([t])) + eq_(set(m2.subclasses), set()) + eq_(set(m2.allSuperclasses), set([t])) + eq_(set(m2.allSubclasses), set()) + + eq_(set(b1.superclasses), set()) + eq_(set(b1.subclasses), set()) + eq_(set(b1.allSuperclasses), set()) + eq_(set(b1.allSubclasses), set()) + + eq_(set(b2.superclasses), set()) + eq_(set(b2.subclasses), set()) + eq_(set(b2.allSuperclasses), set()) + eq_(set(b2.allSubclasses), set()) + + eq_(set(b3.superclasses), set([t])) + eq_(set(b3.subclasses), set()) + eq_(set(b3.allSuperclasses), set([t])) + eq_(set(b3.allSubclasses), set()) + + def testStereotypeMultipleInheritance(self): + t1 = CStereotype("T1") + t2 = CStereotype("T2") + t3 = CStereotype("T3") + m1 = CStereotype("M1", superclasses = [t1, t3]) + m2 = CStereotype("M2", superclasses = [t2, t3]) + b1 = CStereotype("B1", superclasses = [m1]) + b2 = CStereotype("B2", superclasses = [m1, m2]) + b3 = CStereotype("B3", superclasses = [m2, m1]) + + eq_(set(t1.superclasses), set()) + eq_(set(t1.subclasses), set([m1])) + eq_(set(t1.allSuperclasses), set()) + eq_(set(t1.allSubclasses), set([m1, b1, b2, b3])) + + eq_(set(t2.superclasses), set()) + eq_(set(t2.subclasses), set([m2])) + eq_(set(t2.allSuperclasses), set()) + eq_(set(t2.allSubclasses), set([m2, b3, b2])) + + eq_(set(t3.superclasses), set()) + eq_(set(t3.subclasses), set([m2, m1])) + eq_(set(t3.allSuperclasses), set()) + eq_(set(t3.allSubclasses), set([m2, m1, b1, b2, b3])) + + eq_(set(m1.superclasses), set([t1, t3])) + eq_(set(m1.subclasses), set([b1, b2, b3])) + eq_(set(m1.allSuperclasses), set([t1, t3])) + eq_(set(m1.allSubclasses), set([b1, b2, b3])) + + eq_(set(m2.superclasses), set([t2, t3])) + eq_(set(m2.subclasses), set([b2, b3])) + eq_(set(m2.allSuperclasses), set([t2, t3])) + eq_(set(m2.allSubclasses), set([b2, b3])) + + eq_(set(b1.superclasses), set([m1])) + eq_(set(b1.subclasses), set()) + eq_(set(b1.allSuperclasses), set([m1, t1, t3])) + eq_(set(b1.allSubclasses), set()) + + eq_(set(b2.superclasses), set([m1, m2])) + eq_(set(b2.subclasses), set()) + eq_(set(b2.allSuperclasses), set([m1, m2, t1, t2, t3])) + eq_(set(b2.allSubclasses), set()) + + eq_(set(b3.superclasses), set([m1, m2])) + eq_(set(b3.subclasses), set()) + eq_(set(b3.allSuperclasses), set([m1, m2, t1, t2, t3])) + eq_(set(b3.allSubclasses), set()) + + def testStereotypeAsWrongTypeOfSuperclass(self): + t = CStereotype("S") + try: + CMetaclass("M", superclasses = [t]) + exceptionExpected_() + except CException as e: + ok_(re.match("^cannot add superclass 'S' to 'M': not of type([ $", e.value)) + try: + CClass(CMetaclass(), "C", superclasses = [t]) + exceptionExpected_() + except CException as e: + ok_(re.match("^cannot add superclass 'S' to 'C': not of type([ $", e.value)) + + def testExtendedClassesOfInheritingStereotypes_SuperclassHasNone(self): + m1 = CMetaclass() + m2 = CMetaclass(superclasses = [m1]) + s1 = CStereotype() + s2 = CStereotype(superclasses = [s1]) + m2.stereotypes = s2 + eq_(len(s1.extended), 0) + eq_(set(m2.stereotypes), set([s2])) + + def testExtendedClassesOfInheritingStereotypes_SuperclassHasTheSame(self): + m1 = CMetaclass() + s1 = CStereotype(extended = [m1]) + s2 = CStereotype(superclasses = [s1], extended = [m1]) + eq_(set(s1.extended), set([m1])) + eq_(set(s2.extended), set([m1])) + eq_(set(m1.stereotypes), set([s2, s1])) + + def testExtendedClassesOfInheritingStereotypes_RemoveSuperclassStereotype(self): + m1 = CMetaclass() + s1 = CStereotype(extended = [m1]) + s2 = CStereotype(superclasses = [s1], extended = [m1]) + m1.stereotypes = s2 + eq_(set(s1.extended), set()) + eq_(set(s2.extended), set([m1])) + eq_(set(m1.stereotypes), set([s2])) + + def testExtendedClassesOfInheritingStereotypes_SuperclassIsSetToTheSame(self): + m1 = CMetaclass("M1") + s1 = CStereotype("S1") + s2 = CStereotype("S2", extended = [m1], superclasses = [s1]) + m1.stereotypes = [s2,s1] + eq_(set(s1.extended), set([m1])) + eq_(set(s2.extended), set([m1])) + eq_(set(m1.stereotypes), set([s2, s1])) + + def testExtendedClassesOfInheritingStereotypes_SuperclassHasMetaclassesSuperclass(self): + m1 = CMetaclass() + m2 = CMetaclass(superclasses = [m1]) + s1 = CStereotype(extended = [m1]) + s2 = CStereotype(superclasses = [s1], extended = [m2]) + eq_(set(s1.extended), set([m1])) + eq_(set(s2.extended), set([m2])) + eq_(set(m1.stereotypes), set([s1])) + eq_(set(m2.stereotypes), set([s2])) + + def testExtendedClassesOfInheritingStereotypes_SuperclassHasMetaclassesSuperclassIndirectly(self): + m1 = CMetaclass() + m2 = CMetaclass(superclasses = [m1]) + m3 = CMetaclass(superclasses = [m2]) + s1 = CStereotype(extended = [m1]) + s2 = CStereotype(superclasses = [s1], extended = [m3]) + eq_(set(s1.extended), set([m1])) + eq_(set(s2.extended), set([m3])) + eq_(set(m1.stereotypes), set([s1])) + eq_(set(m3.stereotypes), set([s2])) + + def testExtendedClassesOfInheritingStereotypes_SuperclassIsSetToMetaclassesSuperclassIndirectly(self): + m1 = CMetaclass() + m2 = CMetaclass(superclasses = [m1]) + m3 = CMetaclass(superclasses = [m2]) + s1 = CStereotype() + s2 = CStereotype(superclasses = [s1], extended = [m3]) + m1.stereotypes = s1 + eq_(set(s1.extended), set([m1])) + eq_(set(s2.extended), set([m3])) + eq_(set(m1.stereotypes), set([s1])) + eq_(set(m3.stereotypes), set([s2])) + + def testStereotypeHasSuperclassHasSubclass(self): + c1 = CStereotype("C1") + c2 = CStereotype("C2", superclasses = [c1]) + c3 = CStereotype("C3", superclasses = [c2]) + c4 = CStereotype("C4", superclasses = [c2]) + c5 = CStereotype("C5", superclasses = []) + + eq_(c1.hasSuperclass(c2), False) + eq_(c5.hasSuperclass(c2), False) + eq_(c1.hasSuperclass(None), False) + eq_(c5.hasSuperclass(None), False) + eq_(c2.hasSuperclass(c1), True) + eq_(c3.hasSuperclass(c2), True) + eq_(c3.hasSuperclass(c2), True) + eq_(c4.hasSuperclass(c2), True) + eq_(c3.hasSubclass(c2), False) + eq_(c3.hasSubclass(None), False) + eq_(c5.hasSubclass(c2), False) + eq_(c5.hasSubclass(None), False) + eq_(c1.hasSubclass(c3), True) + eq_(c1.hasSubclass(c2), True) + + def testStereotypeUnknownNonPositionalArgument(self): + t = CStereotype("T") + try: + CStereotype("ST", superclass = t) + exceptionExpected_() + except CException as e: + eq_("unknown keyword argument 'superclass', should be one of: ['extended', 'attributes', 'superclasses', 'bundles']", e.value) + + def testSuperStereotypesThatAreDeleted(self): + s1 = CStereotype("S1") + s1.delete() + try: + CStereotype(superclasses = [s1]) + exceptionExpected_() + except CException as e: + eq_(e.value, "cannot access named element that has been deleted") + + def testSuperStereotypesThatAreNone(self): + try: + CStereotype("S", superclasses = [None]) + exceptionExpected_() + except CException as e: + ok_(e.value.startswith("cannot add superclass 'None' to 'S': not of type")) + +if __name__ == "__main__": + nose.main() + + + diff --git a/tests/test_stereotype_instances_on_associations.py b/tests/test_stereotype_instances_on_associations.py new file mode 100644 index 0000000..e391c7f --- /dev/null +++ b/tests/test_stereotype_instances_on_associations.py @@ -0,0 +1,173 @@ +import sys +sys.path.append("..") + +import re + +import nose +from nose.tools import ok_, eq_ +from testCommons import neq_, exceptionExpected_ +from parameterized import parameterized + +from codeableModels import CMetaclass, CStereotype, CClass, CObject, CAttribute, CException, CEnum, setLinks, addLinks + +class TestStereotypeInstancesOnAssociations(): + def setUp(self): + self.m1 = CMetaclass("M1") + self.m2 = CMetaclass("M2") + self.a = self.m1.association(self.m2, name = "a", multiplicity = "*", roleName = "m1", + sourceMultiplicity = "1", sourceRoleName = "m2") + + def testStereotypeInstancesOnAssociation(self): + s1 = CStereotype("S1", extended = self.a) + s2 = CStereotype("S2", extended = self.a) + s3 = CStereotype("S3", extended = self.a) + + c1 = CClass(self.m1, "C1") + c2 = CClass(self.m2, "C2") + c3 = CClass(self.m2, "C3") + linkObjects = setLinks({c1: [c2, c3]}) + l1 = linkObjects[0] + + eq_(l1.stereotypeInstances, []) + eq_(s1.extendedInstances, []) + l1.stereotypeInstances = [s1] + eq_(s1.extendedInstances, [l1]) + eq_(l1.stereotypeInstances, [s1]) + l1.stereotypeInstances = [s1, s2, s3] + eq_(s1.extendedInstances, [l1]) + eq_(s2.extendedInstances, [l1]) + eq_(s3.extendedInstances, [l1]) + eq_(set(l1.stereotypeInstances), set([s1, s2, s3])) + l1.stereotypeInstances = s2 + eq_(l1.stereotypeInstances, [s2]) + eq_(s1.extendedInstances, []) + eq_(s2.extendedInstances, [l1]) + eq_(s3.extendedInstances, []) + + def testStereotypeInstancesDoubleAssignment(self): + s1 = CStereotype("S1", extended = self.a) + + c1 = CClass(self.m1, "C1") + c2 = CClass(self.m2, "C2") + l = setLinks({c1: c2})[0] + + try: + l.stereotypeInstances = [s1, s1] + exceptionExpected_() + except CException as e: + eq_(e.value, "'S1' is already a stereotype instance on link from 'C1' to 'C2'") + eq_(l.stereotypeInstances, [s1]) + + def testStereotypeInstancesNoneAssignment(self): + s1 = CStereotype("S1", extended = self.a) + + c1 = CClass(self.m1, "C1") + c2 = CClass(self.m2, "C2") + l = setLinks({c1:[c2]})[0] + + try: + l.stereotypeInstances = [None] + exceptionExpected_() + except CException as e: + eq_(e.value, "'None' is not a stereotype") + eq_(l.stereotypeInstances, []) + + def testStereotypeInstancesWrongTypeInAssignment(self): + s1 = CStereotype("S1", extended = self.a) + c1 = CClass(self.m1, "C1") + c2 = CClass(self.m2, "C2") + l = c1.addLinks(c2)[0] + try: + l.stereotypeInstances = self.a + exceptionExpected_() + except CException as e: + eq_(e.value, "a list or a stereotype is required as input") + eq_(l.stereotypeInstances, []) + + def testMultipleExtendedInstances(self): + s1 = CStereotype("S1", extended = self.a) + s2 = CStereotype("S2", extended = self.a) + c1 = CClass(self.m1, "C1") + c2 = CClass(self.m2, "C2") + c3 = CClass(self.m2, "C3") + c4 = CClass(self.m2, "C4") + linkObjects = setLinks({c1: [c2, c3, c4]}) + linkObjects[0].stereotypeInstances = [s1] + eq_(s1.extendedInstances, [linkObjects[0]]) + linkObjects[1].stereotypeInstances = [s1] + eq_(set(s1.extendedInstances), set([linkObjects[0], linkObjects[1]])) + linkObjects[2].stereotypeInstances = [s1, s2] + eq_(set(s1.extendedInstances), set([linkObjects[0], linkObjects[1], linkObjects[2]])) + eq_(set(s2.extendedInstances), set([linkObjects[2]])) + + def testDeleteStereotypeOfExtendedInstances(self): + s1 = CStereotype("S1", extended = self.a) + s1.delete() + c1 = CClass(self.m1, "C1") + c2 = CClass(self.m2, "C2") + l = setLinks({c1: c2})[0] + try: + l.stereotypeInstances = [s1] + exceptionExpected_() + except CException as e: + eq_(e.value, "cannot access named element that has been deleted") + + def testDeleteStereotypedElementInstance(self): + s1 = CStereotype("S1", extended = self.a) + c1 = CClass(self.m1, "C1") + c2 = CClass(self.m2, "C2") + l = setLinks({c1: c2}, stereotypeInstances = [s1])[0] + eq_(s1.extendedInstances, [l]) + eq_(l.stereotypeInstances, [s1]) + l.delete() + eq_(s1.extendedInstances, []) + eq_(l.stereotypeInstances, []) + + def testAddStereotypeInstanceWrongAssociation(self): + otherAssociation = self.m1.association(self.m2, name = "b", multiplicity = "*", roleName = "m1", + sourceMultiplicity = "1", sourceRoleName = "m2") + s1 = CStereotype("S1", extended = self.a) + c1 = CClass(self.m1, "C1") + c2 = CClass(self.m2, "C2") + l = setLinks({c1: c2}, association = otherAssociation)[0] + try: + l.stereotypeInstances = [s1] + exceptionExpected_() + except CException as e: + eq_(e.value, "stereotype 'S1' cannot be added to link from 'C1' to 'C2': no extension by this stereotype found") + + def testAddStereotypeOfInheritedMetaclass(self): + sub1 = CMetaclass("Sub1", superclasses = self.m1) + sub2 = CMetaclass("Sub2", superclasses = self.m2) + s = CStereotype("S1", extended = self.a) + c1 = CClass(sub1, "C1") + c2 = CClass(sub2, "C2") + l = addLinks({c1: c2}, stereotypeInstances = s)[0] + eq_(s.extendedInstances, [l]) + eq_(l.stereotypeInstances, [s]) + + def testAddStereotypeInstanceCorrectByInheritanceOfStereotype(self): + s1 = CStereotype("S1", extended = self.a) + s2 = CStereotype("S2", superclasses = s1) + c1 = CClass(self.m1, "C1") + c2 = CClass(self.m2, "C2") + l = c1.addLinks(c2, stereotypeInstances = [s2])[0] + eq_(s2.extendedInstances, [l]) + eq_(l.stereotypeInstances, [s2]) + + def testAllExtendedInstances(self): + s1 = CStereotype("S1", extended = self.a) + s2 = CStereotype("S2", superclasses = s1) + c1 = CClass(self.m1, "C1") + c2 = CClass(self.m2, "C2") + c3 = CClass(self.m2, "C3") + linkObjects = setLinks({c1: [c2, c3]}) + linkObjects[0].stereotypeInstances = s1 + linkObjects[1].stereotypeInstances = s2 + eq_(s1.extendedInstances, [linkObjects[0]]) + eq_(s2.extendedInstances, [linkObjects[1]]) + eq_(s1.allExtendedInstances, [linkObjects[0], linkObjects[1]]) + eq_(s2.allExtendedInstances, [linkObjects[1]]) + +if __name__ == "__main__": + nose.main() \ No newline at end of file diff --git a/tests/test_stereotype_instances_on_classes.py b/tests/test_stereotype_instances_on_classes.py new file mode 100644 index 0000000..af59bce --- /dev/null +++ b/tests/test_stereotype_instances_on_classes.py @@ -0,0 +1,169 @@ +import sys +sys.path.append("..") + +import re + +import nose +from nose.tools import ok_, eq_ +from testCommons import neq_, exceptionExpected_ +from parameterized import parameterized + +from codeableModels import CMetaclass, CStereotype, CClass, CObject, CAttribute, CException, CEnum + +class TestStereotypeInstancesOnClasses(): + def setUp(self): + self.mcl = CMetaclass("MCL") + + def testStereotypeInstancesOnClass(self): + s1 = CStereotype("S1", extended = self.mcl) + s2 = CStereotype("S2", extended = self.mcl) + s3 = CStereotype("S3", extended = self.mcl) + c = CClass(self.mcl, "C") + eq_(c.stereotypeInstances, []) + eq_(s1.extendedInstances, []) + c.stereotypeInstances = [s1] + eq_(s1.extendedInstances, [c]) + eq_(c.stereotypeInstances, [s1]) + c.stereotypeInstances = [s1, s2, s3] + eq_(s1.extendedInstances, [c]) + eq_(s2.extendedInstances, [c]) + eq_(s3.extendedInstances, [c]) + eq_(set(c.stereotypeInstances), set([s1, s2, s3])) + c.stereotypeInstances = s2 + eq_(c.stereotypeInstances, [s2]) + eq_(s1.extendedInstances, []) + eq_(s2.extendedInstances, [c]) + eq_(s3.extendedInstances, []) + + def testStereotypeInstancesDoubleAssignment(self): + s1 = CStereotype("S1", extended = self.mcl) + c = CClass(self.mcl, "C") + try: + c.stereotypeInstances = [s1, s1] + exceptionExpected_() + except CException as e: + eq_(e.value, "'S1' is already a stereotype instance on 'C'") + eq_(c.stereotypeInstances, [s1]) + + def testStereotypeInstancesNoneAssignment(self): + s1 = CStereotype("S1", extended = self.mcl) + c = CClass(self.mcl, "C") + try: + c.stereotypeInstances = [None] + exceptionExpected_() + except CException as e: + eq_(e.value, "'None' is not a stereotype") + eq_(c.stereotypeInstances, []) + + def testStereotypeInstancesWrongTypeInAssignment(self): + s1 = CStereotype("S1", extended = self.mcl) + c = CClass(self.mcl, "C") + try: + c.stereotypeInstances = self.mcl + exceptionExpected_() + except CException as e: + eq_(e.value, "a list or a stereotype is required as input") + eq_(c.stereotypeInstances, []) + + def testMultipleExtendedInstances(self): + s1 = CStereotype("S1", extended = self.mcl) + s2 = CStereotype("S2", extended = self.mcl) + c1 = CClass(self.mcl, "C1", stereotypeInstances = [s1]) + eq_(s1.extendedInstances, [c1]) + c2 = CClass(self.mcl, "C2", stereotypeInstances = s1) + eq_(set(s1.extendedInstances), set([c1, c2])) + c3 = CClass(self.mcl, "C3", stereotypeInstances = [s1, s2]) + eq_(set(s1.extendedInstances), set([c1, c2, c3])) + eq_(set(s2.extendedInstances), set([c3])) + + def testDeleteStereotypeOfExtendedInstances(self): + s1 = CStereotype("S1", extended = self.mcl) + s1.delete() + try: + CClass(self.mcl, "C1", stereotypeInstances = [s1]) + exceptionExpected_() + except CException as e: + eq_(e.value, "cannot access named element that has been deleted") + + def testDeleteStereotypedElementInstance(self): + s1 = CStereotype("S1", extended = self.mcl) + c = CClass(self.mcl, "C1", stereotypeInstances = [s1]) + c.delete() + eq_(s1.extendedInstances, []) + eq_(c.stereotypeInstances, []) + + def testAddStereotypeInstanceWrongMetaclass(self): + otherMCL = CMetaclass() + s1 = CStereotype("S1", extended = self.mcl) + try: + CClass(otherMCL, "C1", stereotypeInstances = [s1]) + exceptionExpected_() + except CException as e: + eq_(e.value, "stereotype 'S1' cannot be added to 'C1': no extension by this stereotype found") + + def testAddStereotypeInstanceMetaclassCorrectByInheritanceOfMetaclass(self): + mcl1 = CMetaclass("MCL1") + mcl2 = CMetaclass("MCL2", superclasses = mcl1) + s = CStereotype("S1", extended = mcl1) + c = CClass(mcl2, "CL", stereotypeInstances = s) + eq_(s.extendedInstances, [c]) + eq_(c.stereotypeInstances, [s]) + + def testApplyStereotypeInstancesWrongMetaclassInheritance(self): + mcl1 = CMetaclass("MCL1", superclasses = self.mcl) + mcl2 = CMetaclass("MCL2", superclasses = self.mcl) + s1 = CStereotype("S1", extended = mcl1) + s2 = CStereotype("S2", extended = mcl2) + superST = CStereotype("SuperST", extended = self.mcl) + + c = CClass(self.mcl, "CL") + + try: + c.stereotypeInstances = s1 + exceptionExpected_() + except CException as e: + eq_(e.value, "stereotype 'S1' cannot be added to 'CL': no extension by this stereotype found") + + c.stereotypeInstances = superST + + mcl1Class = CClass(mcl1, "Mcl1Class", stereotypeInstances = s1) + try: + mcl1Class.stereotypeInstances = [s1, s2] + exceptionExpected_() + except CException as e: + eq_(e.value, "stereotype 'S2' cannot be added to 'Mcl1Class': no extension by this stereotype found") + mcl1Class.stereotypeInstances = [s1, superST] + eq_(set(mcl1Class.stereotypeInstances), set([s1, superST])) + eq_(c.stereotypeInstances, [superST]) + + def testAddStereotypeInstanceMetaclassWrongInheritanceHierarchy(self): + mcl1 = CMetaclass("MCL1") + mcl2 = CMetaclass("MCL2", superclasses = mcl1) + s = CStereotype("S1", extended = mcl2) + c = CClass(mcl1, "CL") + try: + c.stereotypeInstances = [s] + exceptionExpected_() + except CException as e: + eq_(e.value, "stereotype 'S1' cannot be added to 'CL': no extension by this stereotype found") + + + def testAddStereotypeInstanceMetaclassCorrectByInheritanceOfStereotype(self): + s1 = CStereotype("S1", extended = self.mcl) + s2 = CStereotype("S2", superclasses = s1) + c = CClass(self.mcl, "CL", stereotypeInstances = s2) + eq_(s2.extendedInstances, [c]) + eq_(c.stereotypeInstances, [s2]) + + def testAllExtendedInstances(self): + s1 = CStereotype("S1", extended = self.mcl) + s2 = CStereotype("S2", superclasses = s1) + c1 = CClass(self.mcl, "C1", stereotypeInstances = s1) + c2 = CClass(self.mcl, "C2", stereotypeInstances = s2) + eq_(s1.extendedInstances, [c1]) + eq_(s2.extendedInstances, [c2]) + eq_(s1.allExtendedInstances, [c1, c2]) + eq_(s2.allExtendedInstances, [c2]) + +if __name__ == "__main__": + nose.main() \ No newline at end of file diff --git a/tests/test_stereotype_tag_values_on_classes.py b/tests/test_stereotype_tag_values_on_classes.py new file mode 100644 index 0000000..aa53d53 --- /dev/null +++ b/tests/test_stereotype_tag_values_on_classes.py @@ -0,0 +1,585 @@ +import sys +sys.path.append("..") + +import re + +import nose +from nose.tools import ok_, eq_ +from testCommons import neq_, exceptionExpected_ +from parameterized import parameterized + +from codeableModels import CMetaclass, CStereotype, CClass, CObject, CAttribute, CException, CEnum + +class TestStereotypeTagValuesOnClasses(): + def setUp(self): + self.mcl = CMetaclass("MCL") + self.st = CStereotype("ST") + self.mcl.stereotypes = self.st + self.cl = CClass(self.mcl, "C", stereotypeInstances = self.st) + + def testTaggedValuesOnPrimitiveTypeAttributes(self): + s = CStereotype("S", attributes = { + "isBoolean": True, + "intVal": 1, + "floatVal": 1.1, + "string": "abc"}) + self.mcl.stereotypes = s + cl = CClass(self.mcl, "C", stereotypeInstances = s) + + eq_(cl.getTaggedValue("isBoolean"), True) + eq_(cl.getTaggedValue("intVal"), 1) + eq_(cl.getTaggedValue("floatVal"), 1.1) + eq_(cl.getTaggedValue("string"), "abc") + + cl.setTaggedValue("isBoolean", False) + cl.setTaggedValue("intVal", 2) + cl.setTaggedValue("floatVal", 2.1) + cl.setTaggedValue("string", "y") + + eq_(cl.getTaggedValue("isBoolean"), False) + eq_(cl.getTaggedValue("intVal"), 2) + eq_(cl.getTaggedValue("floatVal"), 2.1) + eq_(cl.getTaggedValue("string"), "y") + + def testAttributeOfTaggedValueUnknown(self): + try: + self.cl.getTaggedValue("x") + exceptionExpected_() + except CException as e: + eq_(e.value, "tagged value 'x' unknown") + + self.mcl.attributes = {"isBoolean": True, "intVal": 1} + try: + self.cl.setTaggedValue("x", 1) + exceptionExpected_() + except CException as e: + eq_(e.value, "tagged value 'x' unknown") + + def testIntegersAsFloatTaggedValues(self): + self.st.attributes = {"floatVal": float} + self.cl.setTaggedValue("floatVal", 15) + eq_(self.cl.getTaggedValue("floatVal"), 15) + + def testObjectTypeAttributeTaggedValues(self): + attrType = CClass(self.mcl, "AttrType") + attrValue = CObject(attrType, "attrValue") + self.st.attributes = {"attrTypeObj" : attrValue} + objAttr = self.st.getAttribute("attrTypeObj") + eq_(objAttr.type, attrType) + eq_(self.cl.getTaggedValue("attrTypeObj"), attrValue) + + nonAttrValue = CObject(CClass(self.mcl), "nonAttrValue") + try: + self.cl.setTaggedValue("attrTypeObj", nonAttrValue) + exceptionExpected_() + except CException as e: + eq_(e.value, "type of object 'nonAttrValue' is not matching type of attribute 'attrTypeObj'") + + def testAddObjectAttributeGetSetTaggedValue(self): + attrType = CClass(self.mcl, "AttrType") + attrValue = CObject(attrType, "attrValue") + self.st.attributes = { + "attrTypeObj1" : attrType, "attrTypeObj2" : attrValue + } + eq_(self.cl.getTaggedValue("attrTypeObj1"), None) + eq_(self.cl.getTaggedValue("attrTypeObj2"), attrValue) + + def testObjectAttributeTaggedValueOfSuperclassType(self): + attrSuperType = CClass(self.mcl, "AttrSuperType") + attrType = CClass(self.mcl, "AttrType", superclasses = attrSuperType) + attrValue = CObject(attrType, "attrValue") + self.st.attributes = { + "attrTypeObj1" : attrSuperType, "attrTypeObj2" : attrValue + } + self.cl.setTaggedValue("attrTypeObj1", attrValue) + self.cl.setTaggedValue("attrTypeObj2", attrValue) + eq_(self.cl.getTaggedValue("attrTypeObj1"), attrValue) + eq_(self.cl.getTaggedValue("attrTypeObj2"), attrValue) + + def testTaggedValuesOnAttributesWithNoDefaultValues(self): + attrType = CClass(self.mcl, "AttrType") + enumType = CEnum("EnumT", values = ["A", "B", "C"]) + st = CStereotype("S", attributes = { + "b": bool, + "i": int, + "f": float, + "s": str, + "C": attrType, + "e": enumType}) + mcl = CMetaclass("M", stereotypes = st) + cl = CClass(mcl, "C", stereotypeInstances = st) + for n in ["b", "i", "f", "s", "C", "e"]: + eq_(cl.getTaggedValue(n), None) + + def testEnumTypeAttributeValues(self): + enumType = CEnum("EnumT", values = ["A", "B", "C"]) + self.st.attributes = { + "e1": enumType, + "e2": enumType} + e2 = self.st.getAttribute("e2") + e2.default = "A" + cl = CClass(self.mcl, "C", stereotypeInstances = self.st) + eq_(self.cl.getTaggedValue("e1"), None) + eq_(self.cl.getTaggedValue("e2"), "A") + self.cl.setTaggedValue("e1", "B") + self.cl.setTaggedValue("e2", "C") + eq_(self.cl.getTaggedValue("e1"), "B") + eq_(self.cl.getTaggedValue("e2"), "C") + try: + self.cl.setTaggedValue("e1", "X") + exceptionExpected_() + except CException as e: + eq_(e.value, "value 'X' is not element of enumeration") + + def testDefaultInitAfterInstanceCreation(self): + enumType = CEnum("EnumT", values = ["A", "B", "C"]) + self.st.attributes = { + "e1": enumType, + "e2": enumType} + e2 = self.st.getAttribute("e2") + e2.default = "A" + eq_(self.cl.getTaggedValue("e1"), None) + eq_(self.cl.getTaggedValue("e2"), "A") + + def testAttributeValueTypeCheckBool1(self): + self.st.attributes = {"t": bool} + cl = CClass(self.mcl, "C", stereotypeInstances = self.st) + try: + cl.setTaggedValue("t", self.mcl) + exceptionExpected_() + except CException as e: + eq_(f"value for attribute 't' is not a known attribute type", e.value) + + def testAttributeValueTypeCheckBool2(self): + self.st.attributes = {"t": bool} + cl = CClass(self.mcl, "C", stereotypeInstances = self.st) + try: + cl.setTaggedValue("t", 1) + exceptionExpected_() + except CException as e: + eq_(f"value type for attribute 't' does not match attribute type", e.value) + + def testAttributeValueTypeCheckInt(self): + self.st.attributes = {"t": int} + cl = CClass(self.mcl, "C", stereotypeInstances = self.st) + try: + cl.setTaggedValue("t", True) + exceptionExpected_() + except CException as e: + eq_(f"value type for attribute 't' does not match attribute type", e.value) + + def testAttributeValueTypeCheckFloat(self): + self.st.attributes = {"t": float} + cl = CClass(self.mcl, "C", stereotypeInstances = self.st) + try: + cl.setTaggedValue("t", True) + exceptionExpected_() + except CException as e: + eq_(f"value type for attribute 't' does not match attribute type", e.value) + + def testAttributeValueTypeCheckStr(self): + self.st.attributes = {"t": str} + cl = CClass(self.mcl, "C", stereotypeInstances = self.st) + try: + cl.setTaggedValue("t", True) + exceptionExpected_() + except CException as e: + eq_(f"value type for attribute 't' does not match attribute type", e.value) + + def testAttributeValueTypeCheckObject(self): + attrType = CMetaclass("AttrType") + self.st.attributes = {"t": attrType} + cl = CClass(self.mcl, "C", stereotypeInstances = self.st) + try: + cl.setTaggedValue("t", True) + exceptionExpected_() + except CException as e: + eq_(f"value type for attribute 't' does not match attribute type", e.value) + + def testAttributeValueTypeCheckEnum(self): + enumType = CEnum("EnumT", values = ["A", "B", "C"]) + self.st.attributes = {"t": enumType} + cl = CClass(self.mcl, "C", stereotypeInstances = self.st) + try: + cl.setTaggedValue("t", True) + exceptionExpected_() + except CException as e: + eq_(f"value type for attribute 't' does not match attribute type", e.value) + + def testAttributeDeleted(self): + self.st.attributes = { + "isBoolean": True, + "intVal": 15} + cl = CClass(self.mcl, "C", stereotypeInstances = self.st) + eq_(cl.getTaggedValue("intVal"), 15) + self.st.attributes = { + "isBoolean": False} + eq_(cl.getTaggedValue("isBoolean"), True) + try: + cl.getTaggedValue("intVal") + exceptionExpected_() + except CException as e: + eq_(e.value, "tagged value 'intVal' unknown") + try: + cl.setTaggedValue("intVal", 1) + exceptionExpected_() + except CException as e: + eq_(e.value, "tagged value 'intVal' unknown") + + def testAttributeDeletedNoDefault(self): + self.st.attributes = { + "isBoolean": bool, + "intVal": int} + self.st.attributes = {"isBoolean": bool} + eq_(self.cl.getTaggedValue("isBoolean"), None) + try: + self.cl.getTaggedValue("intVal") + exceptionExpected_() + except CException as e: + eq_(e.value, "tagged value 'intVal' unknown") + try: + self.cl.setTaggedValue("intVal", 1) + exceptionExpected_() + except CException as e: + eq_(e.value, "tagged value 'intVal' unknown") + + def testAttributesOverwrite(self): + self.st.attributes = { + "isBoolean": True, + "intVal": 15} + cl = CClass(self.mcl, "C", stereotypeInstances = self.st) + eq_(cl.getTaggedValue("intVal"), 15) + try: + cl.getTaggedValue("floatVal") + exceptionExpected_() + except CException as e: + eq_(e.value, "tagged value 'floatVal' unknown") + cl.setTaggedValue("intVal", 18) + self.st.attributes = { + "isBoolean": False, + "intVal": 19, + "floatVal": 25.1} + eq_(cl.getTaggedValue("isBoolean"), True) + eq_(cl.getTaggedValue("floatVal"), 25.1) + eq_(cl.getTaggedValue("intVal"), 18) + cl.setTaggedValue("floatVal", 1.2) + eq_(cl.getTaggedValue("floatVal"), 1.2) + + def testAttributesOverwriteNoDefaults(self): + self.st.attributes = { + "isBoolean": bool, + "intVal": int} + eq_(self.cl.getTaggedValue("isBoolean"), None) + self.cl.setTaggedValue("isBoolean", False) + self.st.attributes = { + "isBoolean": bool, + "intVal": int, + "floatVal": float} + eq_(self.cl.getTaggedValue("isBoolean"), False) + eq_(self.cl.getTaggedValue("floatVal"), None) + eq_(self.cl.getTaggedValue("intVal"), None) + self.cl.setTaggedValue("floatVal", 1.2) + eq_(self.cl.getTaggedValue("floatVal"), 1.2) + + def testAttributesDeletedOnSubclass(self): + self.st.attributes = { + "isBoolean": True, + "intVal": 1} + st2 = CStereotype("S2", attributes = { + "isBoolean": False}, superclasses = self.st) + mcl = CMetaclass("M", stereotypes = st2) + cl = CClass(mcl, "C", stereotypeInstances = st2) + + eq_(cl.getTaggedValue("isBoolean"), False) + eq_(cl.getTaggedValue("isBoolean", self.st), True) + eq_(cl.getTaggedValue("isBoolean", st2), False) + + st2.attributes = {} + + eq_(cl.getTaggedValue("isBoolean"), True) + eq_(cl.getTaggedValue("intVal"), 1) + eq_(cl.getTaggedValue("isBoolean", self.st), True) + try: + cl.getTaggedValue("isBoolean", st2) + exceptionExpected_() + except CException as e: + eq_(e.value, "tagged value 'isBoolean' unknown for stereotype 'S2'") + + def testAttributesDeletedOnSubclassNoDefaults(self): + self.st.attributes = { + "isBoolean": bool, + "intVal": int} + st2 = CStereotype("S2", attributes = { + "isBoolean": bool}, superclasses = self.st) + mcl = CMetaclass("M", stereotypes = st2) + cl = CClass(mcl, "C", stereotypeInstances = st2) + + eq_(cl.getTaggedValue("isBoolean"), None) + eq_(cl.getTaggedValue("isBoolean", self.st), None) + eq_(cl.getTaggedValue("isBoolean", st2), None) + + st2.attributes = {} + + eq_(cl.getTaggedValue("isBoolean"), None) + eq_(cl.getTaggedValue("intVal"), None) + eq_(cl.getTaggedValue("isBoolean", self.st), None) + try: + cl.getTaggedValue("isBoolean", st2) + exceptionExpected_() + except CException as e: + eq_(e.value, "tagged value 'isBoolean' unknown for stereotype 'S2'") + + + def testWrongStereotypeInTaggedValue(self): + self.st.attributes = { + "isBoolean": True} + st2 = CStereotype("S2", attributes = { + "isBoolean": True}) + mcl = CMetaclass("M", stereotypes = self.st) + cl = CClass(mcl, "C", stereotypeInstances = self.st) + + cl.setTaggedValue("isBoolean", False) + + try: + cl.setTaggedValue("isBoolean", False, st2) + except CException as e: + eq_(e.value, "stereotype 'S2' is not a stereotype of element") + + eq_(cl.getTaggedValue("isBoolean"), False) + + try: + cl.getTaggedValue("isBoolean", st2) + except CException as e: + eq_(e.value, "stereotype 'S2' is not a stereotype of element") + + + + def testAttributeValuesInheritance(self): + t1 = CStereotype("T1") + t2 = CStereotype("T2") + c = CStereotype("C", superclasses = [t1, t2]) + sc = CStereotype("C", superclasses = c) + + t1.attributes = {"i0" : 0} + t2.attributes = {"i1" : 1} + c.attributes = {"i2" : 2} + sc.attributes = {"i3" : 3} + + mcl = CMetaclass("M", stereotypes = sc) + cl = CClass(mcl, "C", stereotypeInstances = sc) + + for name, value in {"i0" : 0, "i1" : 1, "i2" : 2, "i3" : 3}.items(): + eq_(cl.getTaggedValue(name), value) + + eq_(cl.getTaggedValue("i0", t1), 0) + eq_(cl.getTaggedValue("i1", t2), 1) + eq_(cl.getTaggedValue("i2", c), 2) + eq_(cl.getTaggedValue("i3", sc), 3) + + for name, value in {"i0" : 10, "i1" : 11, "i2" : 12, "i3" : 13}.items(): + cl.setTaggedValue(name, value) + + for name, value in {"i0" : 10, "i1" : 11, "i2" : 12, "i3" : 13}.items(): + eq_(cl.getTaggedValue(name), value) + + eq_(cl.getTaggedValue("i0", t1), 10) + eq_(cl.getTaggedValue("i1", t2), 11) + eq_(cl.getTaggedValue("i2", c), 12) + eq_(cl.getTaggedValue("i3", sc), 13) + + def testAttributeValuesInheritanceAfterDeleteSuperclass(self): + t1 = CStereotype("T1") + t2 = CStereotype("T2") + c = CStereotype("C", superclasses = [t1, t2]) + sc = CStereotype("C", superclasses = c) + + t1.attributes = {"i0" : 0} + t2.attributes = {"i1" : 1} + c.attributes = {"i2" : 2} + sc.attributes = {"i3" : 3} + + mcl = CMetaclass("M", stereotypes = sc) + cl = CClass(mcl, "C", stereotypeInstances = sc) + + t2.delete() + + for name, value in {"i0" : 0, "i2" : 2, "i3" : 3}.items(): + eq_(cl.getTaggedValue(name), value) + try: + cl.getTaggedValue("i1") + exceptionExpected_() + except CException as e: + eq_(e.value, "tagged value 'i1' unknown") + + eq_(cl.getTaggedValue("i0", t1), 0) + try: + cl.getTaggedValue("i1", t2) + exceptionExpected_() + except CException as e: + eq_(e.value, "stereotype '' is not a stereotype of element") + eq_(cl.getTaggedValue("i2", c), 2) + eq_(cl.getTaggedValue("i3", sc), 3) + + for name, value in {"i0" : 10, "i2" : 12, "i3" : 13}.items(): + cl.setTaggedValue(name, value) + try: + cl.setTaggedValue("i1", 11) + exceptionExpected_() + except CException as e: + eq_(e.value, "tagged value 'i1' unknown") + + for name, value in {"i0" : 10, "i2" : 12, "i3" : 13}.items(): + eq_(cl.getTaggedValue(name), value) + try: + cl.getTaggedValue("i1") + exceptionExpected_() + except CException as e: + eq_(e.value, "tagged value 'i1' unknown") + + eq_(cl.getTaggedValue("i0", t1), 10) + try: + cl.getTaggedValue("i1", t2) + exceptionExpected_() + except CException as e: + eq_(e.value, "stereotype '' is not a stereotype of element") + eq_(cl.getTaggedValue("i2", c), 12) + eq_(cl.getTaggedValue("i3", sc), 13) + + + def testAttributeValuesSameNameInheritance(self): + t1 = CStereotype("T1") + t2 = CStereotype("T2") + c = CStereotype("C", superclasses = [t1, t2]) + sc = CStereotype("C", superclasses = c) + + t1.attributes = {"i" : 0} + t2.attributes = {"i" : 1} + c.attributes = {"i" : 2} + sc.attributes = {"i" : 3} + + mcl = CMetaclass("M", stereotypes = t1) + cl1 = CClass(mcl, "C", stereotypeInstances = sc) + cl2 = CClass(mcl, "C", stereotypeInstances = c) + cl3 = CClass(mcl, "C", stereotypeInstances = t1) + + eq_(cl1.getTaggedValue("i"), 3) + eq_(cl2.getTaggedValue("i"), 2) + eq_(cl3.getTaggedValue("i"), 0) + + eq_(cl1.getTaggedValue("i", sc), 3) + eq_(cl1.getTaggedValue("i", c), 2) + eq_(cl1.getTaggedValue("i", t2), 1) + eq_(cl1.getTaggedValue("i", t1), 0) + eq_(cl2.getTaggedValue("i", c), 2) + eq_(cl2.getTaggedValue("i", t2), 1) + eq_(cl2.getTaggedValue("i", t1), 0) + eq_(cl3.getTaggedValue("i", t1), 0) + + cl1.setTaggedValue("i", 10) + cl2.setTaggedValue("i", 11) + cl3.setTaggedValue("i", 12) + + eq_(cl1.getTaggedValue("i"), 10) + eq_(cl2.getTaggedValue("i"), 11) + eq_(cl3.getTaggedValue("i"), 12) + + eq_(cl1.getTaggedValue("i", sc), 10) + eq_(cl1.getTaggedValue("i", c), 2) + eq_(cl1.getTaggedValue("i", t2), 1) + eq_(cl1.getTaggedValue("i", t1), 0) + eq_(cl2.getTaggedValue("i", c), 11) + eq_(cl2.getTaggedValue("i", t2), 1) + eq_(cl2.getTaggedValue("i", t1), 0) + eq_(cl3.getTaggedValue("i", t1), 12) + + cl1.setTaggedValue("i", 130, sc) + cl1.setTaggedValue("i", 100, t1) + cl1.setTaggedValue("i", 110, t2) + cl1.setTaggedValue("i", 120, c) + + eq_(cl1.getTaggedValue("i"), 130) + + eq_(cl1.getTaggedValue("i", sc), 130) + eq_(cl1.getTaggedValue("i", c), 120) + eq_(cl1.getTaggedValue("i", t2), 110) + eq_(cl1.getTaggedValue("i", t1), 100) + + + def testTaggedValuesInheritanceMultipleStereotypes(self): + t1 = CStereotype("T1") + t2 = CStereotype("T2") + sta = CStereotype("STA", superclasses = [t1, t2]) + suba = CStereotype("SubA", superclasses = [sta]) + stb = CStereotype("STB", superclasses = [t1, t2]) + subb = CStereotype("SubB", superclasses = [stb]) + stc = CStereotype("STC") + subc = CStereotype("SubC", superclasses = [stc]) + + mcl = CMetaclass("M", stereotypes = [t1, stc]) + cl = CClass(mcl, "C", stereotypeInstances = [suba, subb, subc]) + + t1.attributes = {"i0" : 0} + t2.attributes = {"i1" : 1} + sta.attributes = {"i2" : 2} + suba.attributes = {"i3" : 3} + stb.attributes = {"i4" : 4} + subb.attributes = {"i5" : 5} + stc.attributes = {"i6" : 6} + subc.attributes = {"i7" : 7} + + eq_(cl.getTaggedValue("i0"), 0) + eq_(cl.getTaggedValue("i1"), 1) + eq_(cl.getTaggedValue("i2"), 2) + eq_(cl.getTaggedValue("i3"), 3) + eq_(cl.getTaggedValue("i4"), 4) + eq_(cl.getTaggedValue("i5"), 5) + eq_(cl.getTaggedValue("i6"), 6) + eq_(cl.getTaggedValue("i7"), 7) + + eq_(cl.getTaggedValue("i0", t1), 0) + eq_(cl.getTaggedValue("i1", t2), 1) + eq_(cl.getTaggedValue("i2", sta), 2) + eq_(cl.getTaggedValue("i3", suba), 3) + eq_(cl.getTaggedValue("i4", stb), 4) + eq_(cl.getTaggedValue("i5", subb), 5) + eq_(cl.getTaggedValue("i6", stc), 6) + eq_(cl.getTaggedValue("i7", subc), 7) + + cl.setTaggedValue("i0", 10) + cl.setTaggedValue("i1", 11) + cl.setTaggedValue("i2", 12) + cl.setTaggedValue("i3", 13) + cl.setTaggedValue("i4", 14) + cl.setTaggedValue("i5", 15) + cl.setTaggedValue("i6", 16) + cl.setTaggedValue("i7", 17) + + eq_(cl.getTaggedValue("i0"), 10) + eq_(cl.getTaggedValue("i1"), 11) + eq_(cl.getTaggedValue("i2"), 12) + eq_(cl.getTaggedValue("i3"), 13) + eq_(cl.getTaggedValue("i4"), 14) + eq_(cl.getTaggedValue("i5"), 15) + eq_(cl.getTaggedValue("i6"), 16) + eq_(cl.getTaggedValue("i7"), 17) + + cl.setTaggedValue("i0", 210, t1) + cl.setTaggedValue("i1", 211, t2) + cl.setTaggedValue("i2", 212, sta) + cl.setTaggedValue("i3", 213, suba) + cl.setTaggedValue("i4", 214, stb) + cl.setTaggedValue("i5", 215, subb) + cl.setTaggedValue("i6", 216, stc) + cl.setTaggedValue("i7", 217, subc) + + eq_(cl.getTaggedValue("i0"), 210) + eq_(cl.getTaggedValue("i1"), 211) + eq_(cl.getTaggedValue("i2"), 212) + eq_(cl.getTaggedValue("i3"), 213) + eq_(cl.getTaggedValue("i4"), 214) + eq_(cl.getTaggedValue("i5"), 215) + eq_(cl.getTaggedValue("i6"), 216) + eq_(cl.getTaggedValue("i7"), 217) + +if __name__ == "__main__": + nose.main() \ No newline at end of file diff --git a/tests/test_stereotype_tag_values_on_links.py b/tests/test_stereotype_tag_values_on_links.py new file mode 100644 index 0000000..b15b296 --- /dev/null +++ b/tests/test_stereotype_tag_values_on_links.py @@ -0,0 +1,587 @@ +import sys +sys.path.append("..") + +import re + +import nose +from nose.tools import ok_, eq_ +from testCommons import neq_, exceptionExpected_ +from parameterized import parameterized + +from codeableModels import CMetaclass, CStereotype, CClass, CObject, CAttribute, CException, CEnum, setLinks + +class TestStereotypeTagValuesOnLinks(): + def setUp(self): + self.st = CStereotype("ST") + self.m1 = CMetaclass("M1") + self.m2 = CMetaclass("M2") + self.a = self.m1.association(self.m2, name = "a", multiplicity = "*", roleName = "m1", + sourceMultiplicity = "1", sourceRoleName = "m2") + self.a.stereotypes = self.st + c1 = CClass(self.m1, "C1") + c2 = CClass(self.m2, "C2") + c3 = CClass(self.m2, "C3") + c4 = CClass(self.m2, "C4") + self.linkObjects = setLinks({c1: [c2, c3, c4]}) + self.l = self.linkObjects[0] + self.l2 = self.linkObjects[1] + self.l3 = self.linkObjects[2] + self.l.stereotypeInstances = self.st + + + def testTaggedValuesOnPrimitiveTypeAttributes(self): + s = CStereotype("S", attributes = { + "isBoolean": True, + "intVal": 1, + "floatVal": 1.1, + "string": "abc"}) + self.a.stereotypes = s + self.l.stereotypeInstances = s + + eq_(self.l.getTaggedValue("isBoolean"), True) + eq_(self.l.getTaggedValue("intVal"), 1) + eq_(self.l.getTaggedValue("floatVal"), 1.1) + eq_(self.l.getTaggedValue("string"), "abc") + + self.l.setTaggedValue("isBoolean", False) + self.l.setTaggedValue("intVal", 2) + self.l.setTaggedValue("floatVal", 2.1) + self.l.setTaggedValue("string", "y") + + eq_(self.l.getTaggedValue("isBoolean"), False) + eq_(self.l.getTaggedValue("intVal"), 2) + eq_(self.l.getTaggedValue("floatVal"), 2.1) + eq_(self.l.getTaggedValue("string"), "y") + + def testAttributeOfTaggedValueUnknown(self): + try: + self.l.getTaggedValue("x") + exceptionExpected_() + except CException as e: + eq_(e.value, "tagged value 'x' unknown") + + def testIntegersAsFloatTaggedValues(self): + self.st.attributes = {"floatVal": float} + self.l.setTaggedValue("floatVal", 15) + eq_(self.l.getTaggedValue("floatVal"), 15) + + def testObjectTypeAttributeTaggedValues(self): + attrType = CClass(self.m1, "AttrType") + attrValue = CObject(attrType, "attrValue") + self.st.attributes = {"attrTypeObj" : attrValue} + objAttr = self.st.getAttribute("attrTypeObj") + eq_(objAttr.type, attrType) + eq_(self.l.getTaggedValue("attrTypeObj"), attrValue) + + nonAttrValue = CObject(CClass(self.m1), "nonAttrValue") + try: + self.l.setTaggedValue("attrTypeObj", nonAttrValue) + exceptionExpected_() + except CException as e: + eq_(e.value, "type of object 'nonAttrValue' is not matching type of attribute 'attrTypeObj'") + + def testAddObjectAttributeGetSetTaggedValue(self): + attrType = CClass(self.m1, "AttrType") + attrValue = CObject(attrType, "attrValue") + self.st.attributes = { + "attrTypeObj1" : attrType, "attrTypeObj2" : attrValue + } + eq_(self.l.getTaggedValue("attrTypeObj1"), None) + eq_(self.l.getTaggedValue("attrTypeObj2"), attrValue) + + def testObjectAttributeTaggedValueOfSuperclassType(self): + attrSuperType = CClass(self.m1, "AttrSuperType") + attrType = CClass(self.m1, "AttrType", superclasses = attrSuperType) + attrValue = CObject(attrType, "attrValue") + self.st.attributes = { + "attrTypeObj1" : attrSuperType, "attrTypeObj2" : attrValue + } + self.l.setTaggedValue("attrTypeObj1", attrValue) + self.l.setTaggedValue("attrTypeObj2", attrValue) + eq_(self.l.getTaggedValue("attrTypeObj1"), attrValue) + eq_(self.l.getTaggedValue("attrTypeObj2"), attrValue) + + def testTaggedValuesOnAttributesWithNoDefaultValues(self): + attrType = CClass(self.m1, "AttrType") + enumType = CEnum("EnumT", values = ["A", "B", "C"]) + st = CStereotype("S", attributes = { + "b": bool, + "i": int, + "f": float, + "s": str, + "C": attrType, + "e": enumType}) + self.a.stereotypes = st + self.l.stereotypeInstances = st + for n in ["b", "i", "f", "s", "C", "e"]: + eq_(self.l.getTaggedValue(n), None) + + def testEnumTypeAttributeValues(self): + enumType = CEnum("EnumT", values = ["A", "B", "C"]) + self.st.attributes = { + "e1": enumType, + "e2": enumType} + e2 = self.st.getAttribute("e2") + e2.default = "A" + eq_(self.l.getTaggedValue("e1"), None) + eq_(self.l.getTaggedValue("e2"), "A") + self.l.setTaggedValue("e1", "B") + self.l.setTaggedValue("e2", "C") + eq_(self.l.getTaggedValue("e1"), "B") + eq_(self.l.getTaggedValue("e2"), "C") + try: + self.l.setTaggedValue("e1", "X") + exceptionExpected_() + except CException as e: + eq_(e.value, "value 'X' is not element of enumeration") + + def testDefaultInitAfterInstanceCreation(self): + enumType = CEnum("EnumT", values = ["A", "B", "C"]) + self.st.attributes = { + "e1": enumType, + "e2": enumType} + e2 = self.st.getAttribute("e2") + e2.default = "A" + eq_(self.l.getTaggedValue("e1"), None) + eq_(self.l.getTaggedValue("e2"), "A") + + def testAttributeValueTypeCheckBool1(self): + self.st.attributes = {"t": bool} + self.lstereotypeInstances = self.st + try: + self.l.setTaggedValue("t", self.m1) + exceptionExpected_() + except CException as e: + eq_(f"value for attribute 't' is not a known attribute type", e.value) + + def testAttributeValueTypeCheckBool2(self): + self.st.attributes = {"t": bool} + self.l.stereotypeInstances = self.st + try: + self.l.setTaggedValue("t", 1) + exceptionExpected_() + except CException as e: + eq_(f"value type for attribute 't' does not match attribute type", e.value) + + def testAttributeValueTypeCheckInt(self): + self.st.attributes = {"t": int} + self.l.stereotypeInstances = self.st + try: + self.l.setTaggedValue("t", True) + exceptionExpected_() + except CException as e: + eq_(f"value type for attribute 't' does not match attribute type", e.value) + + def testAttributeValueTypeCheckFloat(self): + self.st.attributes = {"t": float} + self.l.stereotypeInstances = self.st + try: + self.l.setTaggedValue("t", True) + exceptionExpected_() + except CException as e: + eq_(f"value type for attribute 't' does not match attribute type", e.value) + + def testAttributeValueTypeCheckStr(self): + self.st.attributes = {"t": str} + self.l.stereotypeInstances = self.st + try: + self.l.setTaggedValue("t", True) + exceptionExpected_() + except CException as e: + eq_(f"value type for attribute 't' does not match attribute type", e.value) + + def testAttributeValueTypeCheckObject(self): + attrType = CMetaclass("AttrType") + self.st.attributes = {"t": attrType} + self.l.stereotypeInstances = self.st + try: + self.l.setTaggedValue("t", True) + exceptionExpected_() + except CException as e: + eq_(f"value type for attribute 't' does not match attribute type", e.value) + + def testAttributeValueTypeCheckEnum(self): + enumType = CEnum("EnumT", values = ["A", "B", "C"]) + self.st.attributes = {"t": enumType} + self.l.stereotypeInstances = self.st + try: + self.l.setTaggedValue("t", True) + exceptionExpected_() + except CException as e: + eq_(f"value type for attribute 't' does not match attribute type", e.value) + + def testAttributeDeleted(self): + self.st.attributes = { + "isBoolean": True, + "intVal": 15} + self.l.stereotypeInstances = self.st + eq_(self.l.getTaggedValue("intVal"), 15) + self.st.attributes = { + "isBoolean": False} + eq_(self.l.getTaggedValue("isBoolean"), True) + try: + self.l.getTaggedValue("intVal") + exceptionExpected_() + except CException as e: + eq_(e.value, "tagged value 'intVal' unknown") + try: + self.l.setTaggedValue("intVal", 1) + exceptionExpected_() + except CException as e: + eq_(e.value, "tagged value 'intVal' unknown") + + def testAttributeDeletedNoDefault(self): + self.st.attributes = { + "isBoolean": bool, + "intVal": int} + self.st.attributes = {"isBoolean": bool} + eq_(self.l.getTaggedValue("isBoolean"), None) + try: + self.l.getTaggedValue("intVal") + exceptionExpected_() + except CException as e: + eq_(e.value, "tagged value 'intVal' unknown") + try: + self.l.setTaggedValue("intVal", 1) + exceptionExpected_() + except CException as e: + eq_(e.value, "tagged value 'intVal' unknown") + + def testAttributesOverwrite(self): + self.st.attributes = { + "isBoolean": True, + "intVal": 15} + self.l.stereotypeInstances = self.st + eq_(self.l.getTaggedValue("intVal"), 15) + try: + self.l.getTaggedValue("floatVal") + exceptionExpected_() + except CException as e: + eq_(e.value, "tagged value 'floatVal' unknown") + self.l.setTaggedValue("intVal", 18) + self.st.attributes = { + "isBoolean": False, + "intVal": 19, + "floatVal": 25.1} + eq_(self.l.getTaggedValue("isBoolean"), True) + eq_(self.l.getTaggedValue("floatVal"), 25.1) + eq_(self.l.getTaggedValue("intVal"), 18) + self.l.setTaggedValue("floatVal", 1.2) + eq_(self.l.getTaggedValue("floatVal"), 1.2) + + def testAttributesOverwriteNoDefaults(self): + self.st.attributes = { + "isBoolean": bool, + "intVal": int} + eq_(self.l.getTaggedValue("isBoolean"), None) + self.l.setTaggedValue("isBoolean", False) + self.st.attributes = { + "isBoolean": bool, + "intVal": int, + "floatVal": float} + eq_(self.l.getTaggedValue("isBoolean"), False) + eq_(self.l.getTaggedValue("floatVal"), None) + eq_(self.l.getTaggedValue("intVal"), None) + self.l.setTaggedValue("floatVal", 1.2) + eq_(self.l.getTaggedValue("floatVal"), 1.2) + + def testAttributesDeletedOnSubclass(self): + self.st.attributes = { + "isBoolean": True, + "intVal": 1} + st2 = CStereotype("S2", attributes = { + "isBoolean": False}, superclasses = self.st) + self.a.stereotypes = st2 + self.l.stereotypeInstances = st2 + + eq_(self.l.getTaggedValue("isBoolean"), False) + eq_(self.l.getTaggedValue("isBoolean", self.st), True) + eq_(self.l.getTaggedValue("isBoolean", st2), False) + + st2.attributes = {} + + eq_(self.l.getTaggedValue("isBoolean"), True) + eq_(self.l.getTaggedValue("intVal"), 1) + eq_(self.l.getTaggedValue("isBoolean", self.st), True) + try: + self.l.getTaggedValue("isBoolean", st2) + exceptionExpected_() + except CException as e: + eq_(e.value, "tagged value 'isBoolean' unknown for stereotype 'S2'") + + def testAttributesDeletedOnSubclassNoDefaults(self): + self.st.attributes = { + "isBoolean": bool, + "intVal": int} + st2 = CStereotype("S2", attributes = { + "isBoolean": bool}, superclasses = self.st) + self.a.stereotypes = st2 + self.l.stereotypeInstances = st2 + + eq_(self.l.getTaggedValue("isBoolean"), None) + eq_(self.l.getTaggedValue("isBoolean", self.st), None) + eq_(self.l.getTaggedValue("isBoolean", st2), None) + + st2.attributes = {} + + eq_(self.l.getTaggedValue("isBoolean"), None) + eq_(self.l.getTaggedValue("intVal"), None) + eq_(self.l.getTaggedValue("isBoolean", self.st), None) + try: + self.l.getTaggedValue("isBoolean", st2) + exceptionExpected_() + except CException as e: + eq_(e.value, "tagged value 'isBoolean' unknown for stereotype 'S2'") + + def testWrongStereotypeInTaggedValue(self): + self.st.attributes = { + "isBoolean": True} + st2 = CStereotype("S2", attributes = { + "isBoolean": True}) + self.a.stereotypes = st2 + self.l.stereotypeInstances = st2 + + self.l.setTaggedValue("isBoolean", False) + + try: + self.l.setTaggedValue("isBoolean", False, st2) + except CException as e: + eq_(e.value, "stereotype 'S2' is not a stereotype of element") + + eq_(self.l.getTaggedValue("isBoolean"), False) + + try: + self.l.getTaggedValue("isBoolean", st2) + except CException as e: + eq_(e.value, "stereotype 'S2' is not a stereotype of element") + + + def testAttributeValuesInheritance(self): + t1 = CStereotype("T1") + t2 = CStereotype("T2") + c = CStereotype("C", superclasses = [t1, t2]) + sc = CStereotype("C", superclasses = c) + + t1.attributes = {"i0" : 0} + t2.attributes = {"i1" : 1} + c.attributes = {"i2" : 2} + sc.attributes = {"i3" : 3} + + self.a.stereotypes = sc + self.l.stereotypeInstances = sc + + for name, value in {"i0" : 0, "i1" : 1, "i2" : 2, "i3" : 3}.items(): + eq_(self.l.getTaggedValue(name), value) + + eq_(self.l.getTaggedValue("i0", t1), 0) + eq_(self.l.getTaggedValue("i1", t2), 1) + eq_(self.l.getTaggedValue("i2", c), 2) + eq_(self.l.getTaggedValue("i3", sc), 3) + + for name, value in {"i0" : 10, "i1" : 11, "i2" : 12, "i3" : 13}.items(): + self.l.setTaggedValue(name, value) + + for name, value in {"i0" : 10, "i1" : 11, "i2" : 12, "i3" : 13}.items(): + eq_(self.l.getTaggedValue(name), value) + + eq_(self.l.getTaggedValue("i0", t1), 10) + eq_(self.l.getTaggedValue("i1", t2), 11) + eq_(self.l.getTaggedValue("i2", c), 12) + eq_(self.l.getTaggedValue("i3", sc), 13) + + def testAttributeValuesInheritanceAfterDeleteSuperclass(self): + t1 = CStereotype("T1") + t2 = CStereotype("T2") + c = CStereotype("C", superclasses = [t1, t2]) + sc = CStereotype("C", superclasses = c) + + t1.attributes = {"i0" : 0} + t2.attributes = {"i1" : 1} + c.attributes = {"i2" : 2} + sc.attributes = {"i3" : 3} + + self.a.stereotypes = sc + self.l.stereotypeInstances = sc + + t2.delete() + + for name, value in {"i0" : 0, "i2" : 2, "i3" : 3}.items(): + eq_(self.l.getTaggedValue(name), value) + try: + self.l.getTaggedValue("i1") + exceptionExpected_() + except CException as e: + eq_(e.value, "tagged value 'i1' unknown") + + eq_(self.l.getTaggedValue("i0", t1), 0) + try: + self.l.getTaggedValue("i1", t2) + exceptionExpected_() + except CException as e: + eq_(e.value, "stereotype '' is not a stereotype of element") + eq_(self.l.getTaggedValue("i2", c), 2) + eq_(self.l.getTaggedValue("i3", sc), 3) + + for name, value in {"i0" : 10, "i2" : 12, "i3" : 13}.items(): + self.l.setTaggedValue(name, value) + try: + self.l.setTaggedValue("i1", 11) + exceptionExpected_() + except CException as e: + eq_(e.value, "tagged value 'i1' unknown") + + for name, value in {"i0" : 10, "i2" : 12, "i3" : 13}.items(): + eq_(self.l.getTaggedValue(name), value) + try: + self.l.getTaggedValue("i1") + exceptionExpected_() + except CException as e: + eq_(e.value, "tagged value 'i1' unknown") + + eq_(self.l.getTaggedValue("i0", t1), 10) + try: + self.l.getTaggedValue("i1", t2) + exceptionExpected_() + except CException as e: + eq_(e.value, "stereotype '' is not a stereotype of element") + eq_(self.l.getTaggedValue("i2", c), 12) + eq_(self.l.getTaggedValue("i3", sc), 13) + + + def testAttributeValuesSameNameInheritance(self): + t1 = CStereotype("T1") + t2 = CStereotype("T2") + c = CStereotype("C", superclasses = [t1, t2]) + sc = CStereotype("C", superclasses = c) + + t1.attributes = {"i" : 0} + t2.attributes = {"i" : 1} + c.attributes = {"i" : 2} + sc.attributes = {"i" : 3} + + self.a.stereotypes = t1 + self.l.stereotypeInstances = sc + self.l2.stereotypeInstances = c + self.l3.stereotypeInstances = t1 + + eq_(self.l.getTaggedValue("i"), 3) + eq_(self.l2.getTaggedValue("i"), 2) + eq_(self.l3.getTaggedValue("i"), 0) + + eq_(self.l.getTaggedValue("i", sc), 3) + eq_(self.l.getTaggedValue("i", c), 2) + eq_(self.l.getTaggedValue("i", t2), 1) + eq_(self.l.getTaggedValue("i", t1), 0) + eq_(self.l2.getTaggedValue("i", c), 2) + eq_(self.l2.getTaggedValue("i", t2), 1) + eq_(self.l2.getTaggedValue("i", t1), 0) + eq_(self.l3.getTaggedValue("i", t1), 0) + + self.l.setTaggedValue("i", 10) + self.l2.setTaggedValue("i", 11) + self.l3.setTaggedValue("i", 12) + + eq_(self.l.getTaggedValue("i"), 10) + eq_(self.l2.getTaggedValue("i"), 11) + eq_(self.l3.getTaggedValue("i"), 12) + + eq_(self.l.getTaggedValue("i", sc), 10) + eq_(self.l.getTaggedValue("i", c), 2) + eq_(self.l.getTaggedValue("i", t2), 1) + eq_(self.l.getTaggedValue("i", t1), 0) + eq_(self.l2.getTaggedValue("i", c), 11) + eq_(self.l2.getTaggedValue("i", t2), 1) + eq_(self.l2.getTaggedValue("i", t1), 0) + eq_(self.l3.getTaggedValue("i", t1), 12) + + self.l.setTaggedValue("i", 130, sc) + self.l.setTaggedValue("i", 100, t1) + self.l.setTaggedValue("i", 110, t2) + self.l.setTaggedValue("i", 120, c) + + eq_(self.l.getTaggedValue("i"), 130) + + eq_(self.l.getTaggedValue("i", sc), 130) + eq_(self.l.getTaggedValue("i", c), 120) + eq_(self.l.getTaggedValue("i", t2), 110) + eq_(self.l.getTaggedValue("i", t1), 100) + + + def testTaggedValuesInheritanceMultipleStereotypes(self): + t1 = CStereotype("T1") + t2 = CStereotype("T2") + sta = CStereotype("STA", superclasses = [t1, t2]) + suba = CStereotype("SubA", superclasses = [sta]) + stb = CStereotype("STB", superclasses = [t1, t2]) + subb = CStereotype("SubB", superclasses = [stb]) + stc = CStereotype("STC") + subc = CStereotype("SubC", superclasses = [stc]) + + self.a.stereotypes = [t1, stc] + self.l.stereotypeInstances = [suba, subb, subc] + + t1.attributes = {"i0" : 0} + t2.attributes = {"i1" : 1} + sta.attributes = {"i2" : 2} + suba.attributes = {"i3" : 3} + stb.attributes = {"i4" : 4} + subb.attributes = {"i5" : 5} + stc.attributes = {"i6" : 6} + subc.attributes = {"i7" : 7} + + eq_(self.l.getTaggedValue("i0"), 0) + eq_(self.l.getTaggedValue("i1"), 1) + eq_(self.l.getTaggedValue("i2"), 2) + eq_(self.l.getTaggedValue("i3"), 3) + eq_(self.l.getTaggedValue("i4"), 4) + eq_(self.l.getTaggedValue("i5"), 5) + eq_(self.l.getTaggedValue("i6"), 6) + eq_(self.l.getTaggedValue("i7"), 7) + + eq_(self.l.getTaggedValue("i0", t1), 0) + eq_(self.l.getTaggedValue("i1", t2), 1) + eq_(self.l.getTaggedValue("i2", sta), 2) + eq_(self.l.getTaggedValue("i3", suba), 3) + eq_(self.l.getTaggedValue("i4", stb), 4) + eq_(self.l.getTaggedValue("i5", subb), 5) + eq_(self.l.getTaggedValue("i6", stc), 6) + eq_(self.l.getTaggedValue("i7", subc), 7) + + self.l.setTaggedValue("i0", 10) + self.l.setTaggedValue("i1", 11) + self.l.setTaggedValue("i2", 12) + self.l.setTaggedValue("i3", 13) + self.l.setTaggedValue("i4", 14) + self.l.setTaggedValue("i5", 15) + self.l.setTaggedValue("i6", 16) + self.l.setTaggedValue("i7", 17) + + eq_(self.l.getTaggedValue("i0"), 10) + eq_(self.l.getTaggedValue("i1"), 11) + eq_(self.l.getTaggedValue("i2"), 12) + eq_(self.l.getTaggedValue("i3"), 13) + eq_(self.l.getTaggedValue("i4"), 14) + eq_(self.l.getTaggedValue("i5"), 15) + eq_(self.l.getTaggedValue("i6"), 16) + eq_(self.l.getTaggedValue("i7"), 17) + + self.l.setTaggedValue("i0", 210, t1) + self.l.setTaggedValue("i1", 211, t2) + self.l.setTaggedValue("i2", 212, sta) + self.l.setTaggedValue("i3", 213, suba) + self.l.setTaggedValue("i4", 214, stb) + self.l.setTaggedValue("i5", 215, subb) + self.l.setTaggedValue("i6", 216, stc) + self.l.setTaggedValue("i7", 217, subc) + + eq_(self.l.getTaggedValue("i0"), 210) + eq_(self.l.getTaggedValue("i1"), 211) + eq_(self.l.getTaggedValue("i2"), 212) + eq_(self.l.getTaggedValue("i3"), 213) + eq_(self.l.getTaggedValue("i4"), 214) + eq_(self.l.getTaggedValue("i5"), 215) + eq_(self.l.getTaggedValue("i6"), 216) + eq_(self.l.getTaggedValue("i7"), 217) + +if __name__ == "__main__": + nose.main() \ No newline at end of file diff --git a/tests/test_stereotypes_on_associations.py b/tests/test_stereotypes_on_associations.py new file mode 100644 index 0000000..ae8c4ed --- /dev/null +++ b/tests/test_stereotypes_on_associations.py @@ -0,0 +1,210 @@ +import sys +sys.path.append("..") + +import re + +import nose +from nose.tools import ok_, eq_ +from testCommons import neq_, exceptionExpected_ +from parameterized import parameterized + +from codeableModels import CStereotype, CMetaclass, CBundle, CObject, CAttribute, CException, CEnum, CBundle, CAssociation + +class TestStereotypesOnAssociations(): + def setUp(self): + self.m1 = CMetaclass("M1") + self.m2 = CMetaclass("M2") + self.a = self.m1.association(self.m2, name = "A", multiplicity = "1", roleName = "m1", + sourceMultiplicity = "*", sourceRoleName = "m2") + + def testCreationOfOneStereotype(self): + s = CStereotype("S", extended = self.a) + eq_(s.name, "S") + eq_(self.a.stereotypes, [s]) + eq_(s.extended, [self.a]) + + def testWrongsTypesInListOfExtendedElementTypes(self): + try: + s = CStereotype("S", extended = [self.a, self.m1]) + exceptionExpected_() + except CException as e: + eq_("'M1' is not a association", e.value) + try: + s = CStereotype("S", extended = [self.a, CBundle("P")]) + exceptionExpected_() + except CException as e: + eq_("'P' is not a association", e.value) + try: + s = CStereotype("S", extended = [CBundle("P"), self.a]) + exceptionExpected_() + except CException as e: + eq_("unknown type of extend element: 'P'", e.value) + + def testCreationOf3Stereotypes(self): + s1 = CStereotype("S1") + s2 = CStereotype("S2") + s3 = CStereotype("S3") + self.a.stereotypes = [s1, s2, s3] + eq_(s1.extended, [self.a]) + eq_(s2.extended, [self.a]) + eq_(s3.extended, [self.a]) + eq_(set(self.a.stereotypes), set([s1, s2, s3])) + + def testCreationOfUnnamedStereotype(self): + s = CStereotype() + eq_(s.name, None) + eq_(self.a.stereotypes, []) + eq_(s.extended, []) + + def testDeleteStereotype(self): + s1 = CStereotype("S1") + s1.delete() + eq_(s1.name, None) + eq_(self.a.stereotypes, []) + s1 = CStereotype("S1", extended = self.a) + s1.delete() + eq_(s1.name, None) + eq_(self.a.stereotypes, []) + + s1 = CStereotype("S1", extended = self.a) + s2 = CStereotype("S2", extended = self.a) + s3 = CStereotype("s1", superclasses = s2, attributes = {"i" : 1}, extended = self.a) + + s1.delete() + eq_(set(self.a.stereotypes), set([s2, s3])) + s3.delete() + eq_(set(self.a.stereotypes), set([s2])) + + eq_(s3.superclasses, []) + eq_(s2.subclasses, []) + eq_(s3.attributes, []) + eq_(s3.attributeNames, []) + eq_(s3.extended, []) + eq_(s3.name, None) + eq_(s3.bundles, []) + + def testStereotypeExtensionAddRemove(self): + s1 = CStereotype("S1") + eq_(set(s1.extended), set()) + a1 = self.m1.association(self.m2, multiplicity = "1", roleName = "m1", + sourceMultiplicity = "*", sourceRoleName = "m2", stereotypes = [s1]) + eq_(set(s1.extended), set([a1])) + eq_(set(a1.stereotypes), set([s1])) + a2 = self.m1.association(self.m2, multiplicity = "1", roleName = "m1", + sourceMultiplicity = "*", sourceRoleName = "m2", stereotypes = s1) + eq_(set(s1.extended), set([a1, a2])) + eq_(set(a1.stereotypes), set([s1])) + eq_(set(a2.stereotypes), set([s1])) + s1.extended = [a2] + eq_(set(s1.extended), set([a2])) + eq_(set(a1.stereotypes), set()) + eq_(set(a2.stereotypes), set([s1])) + s2 = CStereotype("S2", extended = [a2]) + eq_(set(a2.stereotypes), set([s2, s1])) + eq_(set(s1.extended), set([a2])) + eq_(set(s2.extended), set([a2])) + a2.stereotypes = [] + eq_(set(a2.stereotypes), set()) + eq_(set(s1.extended), set()) + eq_(set(s2.extended), set()) + + def testStereotypeRemoveSterotypeOrAssociation(self): + a1 = self.m1.association(self.m2, multiplicity = "1", roleName = "m1", + sourceMultiplicity = "*", sourceRoleName = "m2") + s1 = CStereotype("S1", extended = [a1]) + s2 = CStereotype("S2", extended = [a1]) + s3 = CStereotype("S3", extended = [a1]) + s4 = CStereotype("S4", extended = [a1]) + eq_(set(a1.stereotypes), set([s1, s2, s3, s4])) + s2.delete() + eq_(set(a1.stereotypes), set([s1, s3, s4])) + eq_(set(s2.extended), set()) + eq_(set(s1.extended), set([a1])) + a1.delete() + eq_(set(a1.stereotypes), set()) + eq_(set(s1.extended), set()) + + def testStereotypesWrongType(self): + a1 = self.m1.association(self.m2, name = "a1", multiplicity = "1", roleName = "m1", + sourceMultiplicity = "*", sourceRoleName = "m2") + try: + a1.stereotypes = [a1] + exceptionExpected_() + except CException as e: + eq_("'a1' is not a stereotype", e.value) + + def testAssociationStereotypesNullInput(self): + s = CStereotype() + a1 = self.m1.association(self.m2, name = "a1", multiplicity = "1", roleName = "m1", + sourceMultiplicity = "*", sourceRoleName = "m2", stereotypes = None) + eq_(a1.stereotypes, []) + eq_(s.extended, []) + + def testAssociationStereotypesNonListInput(self): + s = CStereotype() + a1 = self.m1.association(self.m2, name = "a1", multiplicity = "1", roleName = "m1", + sourceMultiplicity = "*", sourceRoleName = "m2", stereotypes = s) + eq_(a1.stereotypes, [s]) + eq_(s.extended, [a1]) + + def testAssociationStereotypesNonListInputWrongType(self): + try: + a1 = self.m1.association(self.m2, name = "a1", multiplicity = "1", roleName = "m1", + sourceMultiplicity = "*", sourceRoleName = "m2") + a1.stereotypes = a1 + exceptionExpected_() + except CException as e: + eq_("a list or a stereotype is required as input", e.value) + + def testMetaclassStereotypesAppend(self): + s1 = CStereotype() + s2 = CStereotype() + a1 = self.m1.association(self.m2, name = "a1", multiplicity = "1", roleName = "m1", + sourceMultiplicity = "*", sourceRoleName = "m2", stereotypes = s1) + # should have no effect, as setter must be used + a1.stereotypes.append(s2) + eq_(a1.stereotypes, [s1]) + eq_(s1.extended, [a1]) + eq_(s2.extended, []) + + def testStereotypeExtendedNullInput(self): + a1 = self.m1.association(self.m2, name = "a1", multiplicity = "1", roleName = "m1", + sourceMultiplicity = "*", sourceRoleName = "m2") + s = CStereotype(extended = None) + eq_(a1.stereotypes, []) + eq_(s.extended, []) + + def testStereotypeExtendedNonListInput(self): + a1 = self.m1.association(self.m2, name = "a1", multiplicity = "1", roleName = "m1", + sourceMultiplicity = "*", sourceRoleName = "m2") + s = CStereotype(extended = a1) + eq_(a1.stereotypes, [s]) + eq_(s.extended, [a1]) + + def testStereotypeExtendedAppend(self): + a1 = self.m1.association(self.m2, name = "a1", multiplicity = "1", roleName = "m1", + sourceMultiplicity = "*", sourceRoleName = "m2") + a2 = self.m1.association(self.m2, name = "a2", multiplicity = "1", roleName = "m1", + sourceMultiplicity = "*", sourceRoleName = "m2") + s = CStereotype(extended = [a1]) + # should have no effect, as setter must be used + s.extended.append(a2) + eq_(a1.stereotypes, [s]) + eq_(a2.stereotypes, []) + eq_(s.extended, [a1]) + + def testExtendedAssociationThatIsDeleted(self): + a1 = self.m1.association(self.m2, name = "a1", multiplicity = "1", roleName = "m1", + sourceMultiplicity = "*", sourceRoleName = "m2") + a1.delete() + try: + CStereotype(extended = [a1]) + exceptionExpected_() + except CException as e: + eq_(e.value, "cannot access named element that has been deleted") + +if __name__ == "__main__": + nose.main() + + + diff --git a/tests/test_stereotypes_on_metaclasses.py b/tests/test_stereotypes_on_metaclasses.py new file mode 100644 index 0000000..de74080 --- /dev/null +++ b/tests/test_stereotypes_on_metaclasses.py @@ -0,0 +1,216 @@ +import sys +sys.path.append("..") + +import re + +import nose +from nose.tools import ok_, eq_ +from testCommons import neq_, exceptionExpected_ +from parameterized import parameterized + +from codeableModels import CStereotype, CMetaclass, CClass, CObject, CAttribute, CException, CEnum, CAssociation, CBundle + +class TestStereotypesOnMetaclasses(): + def setUp(self): + self.mcl = CMetaclass("MCL") + self.mcl = CMetaclass("MCL") + + def testCreationOfOneStereotype(self): + s = CStereotype("S", extended = self.mcl) + eq_(s.name, "S") + eq_(self.mcl.stereotypes, [s]) + eq_(s.extended, [self.mcl]) + + def testWrongsTypesInListOfExtendedElementTypes(self): + try: + s = CStereotype("S", extended = [self.mcl, self.mcl.association(self.mcl, name = "A")]) + exceptionExpected_() + except CException as e: + eq_("'A' is not a metaclass", e.value) + try: + s = CStereotype("S", extended = [self.mcl, CBundle("P")]) + exceptionExpected_() + except CException as e: + eq_("'P' is not a metaclass", e.value) + try: + s = CStereotype("S", extended = [CBundle("P"), self.mcl]) + exceptionExpected_() + except CException as e: + eq_("unknown type of extend element: 'P'", e.value) + + + def testCreationOf3Stereotypes(self): + s1 = CStereotype("S1") + s2 = CStereotype("S2") + s3 = CStereotype("S3") + self.mcl.stereotypes = [s1, s2, s3] + eq_(s1.extended, [self.mcl]) + eq_(s2.extended, [self.mcl]) + eq_(s3.extended, [self.mcl]) + eq_(set(self.mcl.stereotypes), set([s1, s2, s3])) + + def testCreationOfUnnamedStereotype(self): + s = CStereotype() + eq_(s.name, None) + eq_(self.mcl.stereotypes, []) + eq_(s.extended, []) + + def testDeleteStereotype(self): + s1 = CStereotype("S1") + s1.delete() + eq_(s1.name, None) + eq_(self.mcl.stereotypes, []) + s1 = CStereotype("S1", extended = self.mcl) + s1.delete() + eq_(s1.name, None) + eq_(self.mcl.stereotypes, []) + + s1 = CStereotype("S1", extended = self.mcl) + s2 = CStereotype("S2", extended = self.mcl) + s3 = CStereotype("s1", superclasses = s2, attributes = {"i" : 1}, extended = self.mcl) + + s1.delete() + eq_(set(self.mcl.stereotypes), set([s2, s3])) + s3.delete() + eq_(set(self.mcl.stereotypes), set([s2])) + + eq_(s3.superclasses, []) + eq_(s2.subclasses, []) + eq_(s3.attributes, []) + eq_(s3.attributeNames, []) + eq_(s3.extended, []) + eq_(s3.name, None) + eq_(s3.bundles, []) + + def testStereotypeExtensionAddRemove(self): + s1 = CStereotype("S1") + eq_(set(s1.extended), set()) + mcl1 = CMetaclass(stereotypes = [s1]) + eq_(set(s1.extended), set([mcl1])) + eq_(set(mcl1.stereotypes), set([s1])) + mcl2 = CMetaclass(stereotypes = s1) + eq_(set(s1.extended), set([mcl1, mcl2])) + eq_(set(mcl1.stereotypes), set([s1])) + eq_(set(mcl2.stereotypes), set([s1])) + s1.extended = [mcl2] + eq_(set(s1.extended), set([mcl2])) + eq_(set(mcl1.stereotypes), set()) + eq_(set(mcl2.stereotypes), set([s1])) + s2 = CStereotype("S2", extended = [mcl2]) + eq_(set(mcl2.stereotypes), set([s2, s1])) + eq_(set(s1.extended), set([mcl2])) + eq_(set(s2.extended), set([mcl2])) + mcl2.stereotypes = [] + eq_(set(mcl2.stereotypes), set()) + eq_(set(s1.extended), set()) + eq_(set(s2.extended), set()) + + def testStereotypeRemoveSterotypeOrMetaclass(self): + mcl = CMetaclass("MCL1") + s1 = CStereotype("S1", extended = [mcl]) + s2 = CStereotype("S2", extended = [mcl]) + s3 = CStereotype("S3", extended = [mcl]) + s4 = CStereotype("S4", extended = [mcl]) + eq_(set(mcl.stereotypes), set([s1, s2, s3, s4])) + s2.delete() + eq_(set(mcl.stereotypes), set([s1, s3, s4])) + eq_(set(s2.extended), set()) + eq_(set(s1.extended), set([mcl])) + mcl.delete() + eq_(set(mcl.stereotypes), set()) + eq_(set(s1.extended), set()) + + def testStereotypesWrongType(self): + try: + self.mcl.stereotypes = [self.mcl] + exceptionExpected_() + except CException as e: + eq_("'MCL' is not a stereotype", e.value) + + def testExtendedWrongType(self): + try: + cl = CClass(self.mcl) + CStereotype("S1", extended = [cl]) + exceptionExpected_() + except CException as e: + eq_("unknown type of extend element: ''", e.value) + + def testMetaclassStereotypesNullInput(self): + s = CStereotype() + m = CMetaclass(stereotypes = None) + eq_(m.stereotypes, []) + eq_(s.extended, []) + + def testMetaclassStereotypesNonListInput(self): + s = CStereotype() + m = CMetaclass(stereotypes = s) + eq_(m.stereotypes, [s]) + eq_(s.extended, [m]) + + def testMetaclassStereotypesNonListInputWrongType(self): + try: + CMetaclass(stereotypes = self.mcl) + exceptionExpected_() + except CException as e: + eq_("a list or a stereotype is required as input", e.value) + + def testMetaclassStereotypesAppend(self): + s1 = CStereotype() + s2 = CStereotype() + m = CMetaclass(stereotypes = [s1]) + # should have no effect, as setter must be used + m.stereotypes.append(s2) + eq_(m.stereotypes, [s1]) + eq_(s1.extended, [m]) + eq_(s2.extended, []) + + def testStereotypeExtendedNullInput(self): + m = CMetaclass() + s = CStereotype(extended = None) + eq_(m.stereotypes, []) + eq_(s.extended, []) + + def testStereotypeExtendedNonListInput(self): + m = CMetaclass() + s = CStereotype(extended = m) + eq_(m.stereotypes, [s]) + eq_(s.extended, [m]) + + def testStereotypeExtendedNonListInputWrongType(self): + try: + s = CStereotype(extended = CClass(self.mcl)) + exceptionExpected_() + except CException as e: + eq_("extended requires a list or a metaclass as input", e.value) + + def testStereotypeExtendedAppend(self): + m1 = CMetaclass() + m2 = CMetaclass() + s = CStereotype(extended = [m1]) + # should have no effect, as setter must be used + s.extended.append(m2) + eq_(m1.stereotypes, [s]) + eq_(m2.stereotypes, []) + eq_(s.extended, [m1]) + + def testExtendedMetaclassThatIsDeleted(self): + m1 = CMetaclass("M1") + m1.delete() + try: + CStereotype(extended = [m1]) + exceptionExpected_() + except CException as e: + eq_(e.value, "cannot access named element that has been deleted") + + def testExtendedMetaclassThatAreNone(self): + try: + CStereotype(extended = [None]) + exceptionExpected_() + except CException as e: + eq_(e.value, "unknown type of extend element: 'None'") + +if __name__ == "__main__": + nose.main() + + +