Skip to content

Commit

Permalink
removed parameters for modeler-defined names in Package class
Browse files Browse the repository at this point in the history
  • Loading branch information
capsulecorplab committed Nov 9, 2018
1 parent 2311573 commit 7d2d384
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 63 deletions.
57 changes: 30 additions & 27 deletions sysml/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,14 +412,14 @@ def __init__(self, client, supplier):


class Package(ModelElement):
"""A package is a container for a set of model elements, of which may be
other packages.
"""A Package is a container for a set of model elements, of which may
consist of other packages.
Parameters
----------
name : string, default None
elements : dict, default None
elements : list, default None
"""

Expand All @@ -429,25 +429,21 @@ def __init__(self, name='', elements=None):
self._elements = dict()
if elements is None:
pass
elif type(elements) is dict:
for key, element in elements.items():
if type(key) is str and isinstance(element, ModelElement):
self._elements[key] = element
elif type(key) is not str:
raise TypeError
elif not isinstance(element, ModelElement):
elif type(elements) is list:
for element in elements:
if element in self._elements:
pass
elif isinstance(element, ModelElement):
self.add(element)
else:
raise TypeError
else:
raise TypeError

def __getitem__(self, elementName):
"Returns elementName-specified model element"
"Returns model element specified by its name"
return self._elements[elementName]

def __setitem__(self, elementName, element):
"Sets elementName-specified model element"
self._setElement(elementName, element)

@property
def name(self):
return self._name
Expand All @@ -456,27 +452,34 @@ def name(self):
def elements(self):
return self._elements

def add(self, elementName, element):
def add(self, element):
"""Adds a model element to package"""
self._setElement(elementName, element)
if isinstance(element, ModelElement):
i = 0
while element not in self.elements.values():
if isinstance(element, Dependency):
i += 1
elementName = "".join([
element.__class__.__name__[0].lower(),
element.__class__.__name__[1:],
str(i)
])
else:
elementName = element.name
if elementName not in self._elements.keys():
self._elements[elementName] = element
else:
raise TypeError

def remove(self, elementName):
def remove(self, element):
"""Removes a model element from package"""
self._elements.pop(elementName)
self._elements.pop(element.name)

def RTM(self):
"""Generates a requirements traceability matrix for model elements
contained and referenced within package"""
pass

def _setElement(self, elementName, element):
if type(elementName) is str and isinstance(element, ModelElement):
self._elements[elementName] = element
elif type(elementName) is not str:
raise TypeError
elif not isinstance(element, ModelElement):
raise TypeError


class StateMachine(ModelElement):
"""This class defines a state"""
Expand Down
73 changes: 37 additions & 36 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ def test_block(model):
"""Add block elements to package objects using built-in add_part()
method"""

starship_block = sysml.Block('Constitution-class Starship')
starship_block = sysml.Block('constitution-class Starship')

assert repr(starship_block) == "\xabblock\xbb Constitution-class Starship"
assert repr(starship_block) == "\xabblock\xbb constitution-class Starship"
assert repr(type(starship_block)) == "<class 'sysml.element.Block'>"
assert uuid.UUID(starship_block.uuid, version=1)
assert starship_block.multiplicity == 1
Expand Down Expand Up @@ -125,32 +125,33 @@ def test_package(model):

assert holodeck.stereotype == "\xabpackage\xbb"

model.add('holodeck', holodeck)
model.add(holodeck)

assert repr(model['holodeck']) == "\xabpackage\xbb Holodeck"
assert repr(type(model['holodeck'])) == package_type
assert uuid.UUID(model['holodeck'].uuid, version=1)
assert repr(model.elements) == "{'holodeck': \xabpackage\xbb Holodeck}"
assert model['Holodeck'] is holodeck
assert repr(model['Holodeck']) == "\xabpackage\xbb Holodeck"
assert repr(type(model['Holodeck'])) == package_type
assert uuid.UUID(model['Holodeck'].uuid, version=1)
assert repr(model.elements) == "{'Holodeck': \xabpackage\xbb Holodeck}"

model.remove('holodeck')
model.remove(holodeck)

with pytest.raises(KeyError) as info:
model['holodeck']
assert "holodeck" in str(info.value)
model['Holodeck']

starship_block = sysml.Block('Constitution-class Starship')
starship_block = sysml.Block('constitution-class starship')

structure = sysml.Package('structure', {'starship': starship_block})
structure = sysml.Package('structure', [starship_block])

model.add('structure', structure)
model.add(structure)

with pytest.raises(TypeError) as info:
model['structure'].add()
assert "missing 1 required positional argument" in str(info.value)

# model['structure'].add(starship_block)

assert starship_block is model['structure']['starship']
assert structure is model['structure']
assert starship_block is model['structure']['constitution-class starship']

assert repr(model['structure']) == "\xabpackage\xbb structure"
assert repr(type(model['structure'])) == package_type
Expand Down Expand Up @@ -188,7 +189,7 @@ def test_block_partProperty(model):
Parts added to a block element are dictionary-callable via the 'parts'
attribute"""

starship_block = model['structure']['starship']
starship_block = model['structure']['constitution-class starship']

starship_block.add_part('saucer section', sysml.Block('Primary Hull'))
starship_block['engineering'] = sysml.Block('Engineering Hull')
Expand Down Expand Up @@ -262,7 +263,7 @@ def test_block_partProperty_withMultiplicity(model):
add_part() method"""
# notes: need to redesign multiplicity constructor and setter

starship_block = model['structure']['starship']
starship_block = model['structure']['constitution-class starship']

starship_block.add_part('nacelle', sysml.Block('Nacelle', multiplicity=2))
starship_block.add_part('pylons', sysml.Block('Pylon', multiplicity=2))
Expand Down Expand Up @@ -319,24 +320,24 @@ def test_requirements(model):
its constructor arguments
"""

model['requirements'] = sysml.Package('Requirements')
model.add(sysml.Package('Requirements'))

package_type = "<class 'sysml.element.Package'>"

assert repr(model['requirements']) == "\xabpackage\xbb Requirements"
assert repr(type(model['requirements'])) == package_type
assert repr(model['Requirements']) == "\xabpackage\xbb Requirements"
assert repr(type(model['Requirements'])) == package_type

assert uuid.UUID(model['requirements'].uuid, version=1)
assert uuid.UUID(model['Requirements'].uuid, version=1)

with pytest.raises(AttributeError) as info:
model['requirements'].uuid = 47
model['Requirements'].uuid = 47
assert "can't set attribute" in str(info.value)
with pytest.raises(AttributeError) as info:
model['requirements'].uuid = "47"
model['Requirements'].uuid = "47"
assert "can't set attribute" in str(info.value)

# with pytest.raises(TypeError) as info:
# model['requirements'].add(sysml.Requirement())
# model['Requirements'].add(sysml.Requirement())
# assert "missing 2 required positional argument" in str(info.value)

top_lvl_req = sysml.Requirement(
Expand All @@ -348,11 +349,11 @@ def test_requirements(model):
'Functional',
"""A constitution-class starship shall be able to travel at warp 8 or
higher""")
model['requirements']['top-level'] = top_lvl_req
model['requirements']['functional'] = functional_req
model['Requirements'].add(top_lvl_req)
model['Requirements'].add(functional_req)

assert top_lvl_req is model['requirements']['top-level']
assert functional_req is model['requirements']['functional']
assert top_lvl_req is model['Requirements']['Top-level']
assert functional_req is model['Requirements']['Functional']
assert repr(top_lvl_req) == "\xabrequirement\xbb Top-level"
assert repr(functional_req) == "\xabrequirement\xbb Functional"

Expand All @@ -378,15 +379,15 @@ def test_derive_requirement(model):
requirements"""

# with pytest.raises(TypeError) as info:
# model['requirements'].add(sysml.Dependency())
# model['Requirements'].add(sysml.Dependency())
# assert "missing 3 required positional arguments" in str(info.value)

client = model['requirements']['functional']
supplier = model['requirements']['top-level']
client = model['Requirements']['Functional']
supplier = model['Requirements']['Top-level']
deriveReqt = sysml.DeriveReqt(client, supplier)
model['requirements']['deriveReqt1'] = deriveReqt
model['Requirements'].add(deriveReqt)

assert deriveReqt is model['requirements']['deriveReqt1']
assert deriveReqt is model['Requirements']['deriveReqt1']

assert repr(deriveReqt.client) == "\xabrequirement\xbb Functional"
assert repr(deriveReqt.supplier) == "\xabrequirement\xbb Top-level"
Expand Down Expand Up @@ -425,16 +426,16 @@ def test_satisfy_requirement(model):
"""Define a dependency relationship, of stereotype «satisfy», between a
requirement and block"""

starship_block = model['structure']['starship']
starship_block = model['structure']['constitution-class starship']

warpdrive = sysml.Block('Class-7 Warp Drive')
starship_block.add_part('warpdrive', warpdrive)

reqt1 = model['requirements']['functional']
reqt1 = model['Requirements']['Functional']
satisfy = sysml.Satisfy(warpdrive, reqt1)
model['requirements'].add('satisfy1', satisfy)
model['Requirements'].add(satisfy)

assert satisfy is model['requirements']['satisfy1']
assert satisfy is model['Requirements']['satisfy1']

assert repr(warpdrive) == "\xabblock\xbb Class-7 Warp Drive"
assert repr(reqt1) == "\xabrequirement\xbb Functional"
Expand Down

0 comments on commit 7d2d384

Please sign in to comment.