Skip to content

Commit

Permalink
Merge 4554227 into 77c1b13
Browse files Browse the repository at this point in the history
  • Loading branch information
capsulecorplab committed Aug 10, 2018
2 parents 77c1b13 + 4554227 commit 06d0966
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 58 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
This project is intended to provide an object-oriented programming (OOP) paradigm for practicing Model-Based Systems Engineering (MBSE).

[![Build Status](https://travis-ci.com/spacedecentral/SysML.py.svg?branch=dev)](https://travis-ci.com/spacedecentral/SysML.py)
[![Coverage Status](https://coveralls.io/repos/github/spacedecentral/SysML.py/badge.svg?branch=dev)](https://coveralls.io/github/spacedecentral/SysML.py?branch=dev)
[![Coverage Status](https://coveralls.io/repos/github/spacedecentral/SysML.py/badge.svg)](https://coveralls.io/github/spacedecentral/SysML.py?branch=dev)

## Package Contents

Expand Down
69 changes: 20 additions & 49 deletions sysml/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,19 @@ class ModelElement(ABC):

_id_no = 0

def __init__(self):
def __init__(self, name=None):
"""UUID"""
self._uuid = str(uuid.uuid1())

"""Name"""
if name is None:
self.__class__._id_no += 1
self._name = self.__class__.__name__.lower() + str(self.__class__._id_no)
elif type(name) is str:
self._name = name
else:
raise TypeError("'{}' must be a string".format(str(name)))

def __repr__(self):
return ''.join(["\xab" + _stereotype + "\xbb\n" for _stereotype in self._stereotype]) + "{}".format(self.name)

Expand All @@ -37,8 +46,6 @@ def _generateKey(name):
"""Takes a modeler-defined name and returns a formatted string for use as a key within the namespace of a parent model element"""
if type(name) is str:
return name[0].lower() + name[1:].replace(' ','')
else:
raise TypeError("'{}' is must be a string".format(str(name)))

class Block(ModelElement):
"""This class defines a block
Expand Down Expand Up @@ -69,30 +76,21 @@ def __init__(self, name=None, typeName=None, parts=None, references=None, values
"""Note: Block() class is intended for internal use by Model() class"""

"""Construct ModelElement"""
super().__init__()
super().__init__(name)

"""Stereotype"""
self._stereotype = [self.__class__.__name__.lower()]
if stereotype is None:
pass
elif type(stereotype) is str and stereotype not in self._stereotype:
self._stereotype.append(stereotype)
elif type(stereotype) is set:
elif type(stereotype) is list:
for stereotype in stereotype:
if type(stereotype) is str:
self._stereotype.add(stereotype)
self._stereotype.append(stereotype)
else:
raise TypeError("'{}' must be a string or set of strings".format(str(stereotype)))

"""Name"""
if name is None:
self.__class__._id_no += 1
self._name = self.__class__.__name__.lower() + str(self.__class__._id_no)
elif type(name) is str:
self._name = name
else:
raise TypeError("'{}' must be a string".format(str(name)))

"""Part Property"""
self._parts = {}
if parts is None:
Expand Down Expand Up @@ -256,20 +254,11 @@ def __init__(self, name=None, txt=None, id=None):
"""Note: Requirement() class is intended for internal use by Model() class"""

"""Construct ModelElement"""
super().__init__()
super().__init__(name)

"""Stereotype"""
self._stereotype = [self.__class__.__name__.lower()]

"""Name"""
if name is None:
self.__class__._id_no += 1
self._name = self.__class__.__name__.lower() + str(self.__class__._id_no)
elif type(name) is str:
self._name = name
else:
raise TypeError("'{}' must be a string".format(str(name)))

"""Text"""
if txt is None:
self.txt = ''
Expand Down Expand Up @@ -302,7 +291,7 @@ class ConstraintBlock(ModelElement):
def __init__(self):

"""Construct ModelElement"""
super().__init__()
super().__init__(name)

class Dependency(ModelElement):
"""This class defines a dependency"""
Expand Down Expand Up @@ -332,7 +321,7 @@ def __init__(self, supplier, client, stereotype):
raise Exception("'{}' is not a valid dependency".format(str(stereotype)))

"""Construct ModelElement"""
super().__init__()
super().__init__(self._name)

@property
def name(self):
Expand All @@ -357,20 +346,11 @@ class Package(ModelElement):
def __init__(self, name=None, elements=None):

"""Construct ModelElement"""
super().__init__()
super().__init__(name)

"""Stereotype"""
self._stereotype = [self.__class__.__name__.lower()]

"""Name"""
if name is None:
self.__class__._id_no += 1
self._name = self.__class__.__name__.lower() + str(self.__class__._id_no)
elif type(name) is str:
self._name = name
else:
raise TypeError("'{}' must be a string".format(str(name)))

"""Elements"""
self._elements = {}
if elements is None:
Expand Down Expand Up @@ -431,36 +411,27 @@ class StateMachine(ModelElement):
def __init__(self):

"""Construct ModelElement"""
super().__init__()
super().__init__(name)

class Activity(ModelElement):
"""This class defines a activity"""

def __init__(self):

"""Construct ModelElement"""
super().__init__()
super().__init__(name)

class Interaction(ModelElement):
"""This class defines an interaction"""

def __init__(self, name=None, elements=None):

"""Construct ModelElement"""
super().__init__()
super().__init__(name)

"""Stereotype"""
self._stereotype = self.__class__.__name__.lower()

"""Name"""
if name is None:
self.__class__._id_no += 1
self._name = self.__class__.__name__ + str(self.__class__._id_no)
elif type(name) is str:
self._name = name
else:
raise TypeError("'{}' must be a string".format(str(name)))

"""Elements"""
if elements is None:
elements = {}
Expand Down
26 changes: 18 additions & 8 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,10 @@ def test_block(model):
model['Structure'].add()
assert "add() missing 1 required positional argument: 'name'" in str(info.value)

model['Structure'].add(sysml.Block('Constitution-class Starship', stereotype='starship'))
model['Structure'].add(sysml.Block('Constitution-class Starship'))
starship_block = model['Structure']['Constitution-class Starship']

assert starship_block.stereotype == ['block', 'starship']

assert repr(model['Structure']['Constitution-class Starship']) == "\xabblock\xbb\n\xabstarship\xbb\nConstitution-class Starship"
assert repr(model['Structure']['Constitution-class Starship']) == "\xabblock\xbb\nConstitution-class Starship"
assert repr(type(model['Structure']['Constitution-class Starship'])) == "<class 'sysml.element.Block'>"
assert uuid.UUID(model['Structure']['Constitution-class Starship'].uuid, version=1)
assert model['Structure']['Constitution-class Starship'].multiplicity == 1
Expand All @@ -91,22 +89,34 @@ def test_block(model):
model['Structure']['Constitution-class Starship'].uuid = "47"
assert "can't set attribute" in str(info.value)

def test_block_stereotype():
"""Test block stereotype"""
enterprise = sysml.Block('NCC-1701',stereotype='constitutionClass')
enterpriseD = sysml.Block('NCC-1701-D',stereotype=['galaxyClass','flagship'])

assert enterprise.stereotype == ['block', 'constitutionClass']
assert enterpriseD.stereotype == ['block', 'galaxyClass', 'flagship']

with pytest.raises(TypeError) as info:
enterpriseD = sysml.Block('NCC-1701-D',stereotype=47)
assert "must be a string or set of strings" in str(info.value)

# @pytest.mark.skip('WIP')
def test_block_partProperty(model):
"""Add block elements as parts to parent blocks using add_part() method
Parts added to a block element are dictionary-callable via the 'parts' attribute"""

with pytest.raises(TypeError) as info:
model['Structure']['Constitution-class Starship'].add_part()
assert "add_part() missing 1 required positional argument: 'name'" in str(info.value)

starship_block = model['Structure']['Constitution-class Starship']

starship_block.add_part(sysml.Block('Primary Hull'))
starship_block.add_part(sysml.Block('Engineering Hull'))
starship_block.add_part(sysml.Block('Cloaking device'))

with pytest.raises(TypeError) as info:
model['Structure']['Constitution-class Starship'].add_part()
assert "add_part() missing 1 required positional argument: 'name'" in str(info.value)

cloaking_device = starship_block.parts['Cloaking device']

assert repr(model['Structure']['Constitution-class Starship'].parts['Primary Hull']) == "\xabblock\xbb\nPrimary Hull"
Expand Down

0 comments on commit 06d0966

Please sign in to comment.