Skip to content

Commit

Permalink
Fixes #86
Browse files Browse the repository at this point in the history
  • Loading branch information
Semprini committed Jul 6, 2024
1 parent 0162a58 commit 193fa03
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 87 deletions.
32 changes: 18 additions & 14 deletions mdg/parse/sparx_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
UMLPackage,
UMLAssociationType,
SearchTypes,
UMLStatuses,
)


Expand All @@ -47,7 +48,7 @@ def parse_uml() -> Tuple[UMLPackage, List[UMLInstance]]:
stmt = sqlalchemy.select(TPackage).where(TPackage.ea_guid == settings['root_package'])
else:
stmt = sqlalchemy.select(TPackage).where(TPackage.name == settings['root_package'])
root_tpackage: TPackage = session.execute(stmt).scalars().first()
root_tpackage: Optional[TPackage] = session.execute(stmt).scalars().first()
if root_tpackage is None:
raise ValueError("Root package element not found. Settings has:{}".format(settings['root_package']))
logger.debug(f"Root Object: {root_tpackage.package_id}: {root_tpackage.name}")
Expand All @@ -58,7 +59,7 @@ def parse_uml() -> Tuple[UMLPackage, List[UMLInstance]]:
stmt = sqlalchemy.select(TPackage).where(TPackage.ea_guid == settings['model_package'])
else:
stmt = sqlalchemy.select(TPackage).where(TPackage.name == settings['model_package'])
model_tpackage: TPackage = session.execute(stmt).scalars().first()
model_tpackage: Optional[TPackage] = session.execute(stmt).scalars().first()
if model_tpackage is None:
raise ValueError("Model package element not found. Settings has:{}".format(settings['model_package']))
logger.debug(f"Model Object: {model_tpackage.package_id}: {model_tpackage.name}")
Expand All @@ -79,7 +80,7 @@ def parse_uml() -> Tuple[UMLPackage, List[UMLInstance]]:
stmt = sqlalchemy.select(TPackage).where(TPackage.ea_guid == settings['test_package'])
else:
stmt = sqlalchemy.select(TPackage).where(TPackage.name == settings['test_package'])
test_tpackage: TPackage = session.execute(stmt).scalars().first()
test_tpackage: Optional[TPackage] = session.execute(stmt).scalars().first()
if test_tpackage is None:
raise ValueError("Test package element not found. Settings has:{}".format(settings['test_package']))
test_package = package_parse(session, test_tpackage, None)
Expand Down Expand Up @@ -173,7 +174,7 @@ def package_parse_associations(session, package: UMLPackage):
stmt = sqlalchemy.select(TConnector).where(TConnector.start_object_id == cls.id)

for connector in session.execute(stmt).scalars().all():
dest = package.root_package.find_by_id(connector.end_object_id, SearchTypes.CLASS)
dest = package.root_package.find_class_by_id(connector.end_object_id)
if dest is None:
logger.warn(f"Cannot find associated class from {cls}. Association Id:{connector.id}, Destination Id: {connector.end_object_id}")

Expand All @@ -191,19 +192,20 @@ def package_parse_associations(session, package: UMLPackage):
# Find enumeration attributes of class and link to attribute
for attr in cls.attributes:
if attr.classification_id is not None:
attr.classification = package.root_package.find_by_id(attr.classification_id, SearchTypes.ENUM)
attr.classification = package.root_package.find_enum_by_id(attr.classification_id)
if attr.classification is None:
logger.warn("Cannot find expected classification for {} of attribute {}. Id={}".format(attr.dest_type, attr.name, attr.classification_id))

for ins in package.instances:
stmt = sqlalchemy.select(TConnector).where(TConnector.start_object_id == ins.id)

for connector in session.execute(stmt).scalars().all():
dest = package.root_package.find_by_id(connector.end_object_id, SearchTypes.INSTANCE)
dest = package.root_package.find_instance_by_id(connector.end_object_id)

association = association_parse(session, connector, package, ins, dest)
if association is not None:
package.associations.append(association)
if dest:
association = association_parse(session, connector, package, ins, dest)
if association is not None:
package.associations.append(association)

for package_child in package.children:
package_parse_associations(session, package_child)
Expand Down Expand Up @@ -272,7 +274,7 @@ def instance_parse(session, package: UMLPackage, tobject: TObject):

ins.stereotype = tobject.stereotype
ins.documentation = tobject.note
ins.status = tobject.status
ins.status = UMLStatuses[tobject.status]

# We need to link this instance to the class it is an instance of
ins.classification_id = tobject.classifier
Expand Down Expand Up @@ -332,8 +334,8 @@ def association_parse(session, tconnector: TConnector, package: UMLPackage, sour
association.source_stereotypes = [tconnector.sourcestereotype,]
if tconnector.deststereotype is not None:
association.destination_stereotypes = [tconnector.deststereotype,]

logging.debug(f"Created {association.cardinality.name} {association.association_type.name} from {association.source_name} to {association.destination_name}")
logging.debug(f"Created {association.cardinality} {association.association_type.name} from {association.source_name} to {association.destination_name}")
return association


Expand Down Expand Up @@ -368,8 +370,8 @@ def class_parse(session, package: UMLPackage, tobject: TObject):
cls.is_abstract = False

cls.alias = tobject.alias
cls.status = tobject.status
cls.phase = tobject.phase
cls.status = UMLStatuses[tobject.status]
cls.phase = Decimal(tobject.phase)
if tobject.note is not None:
cls.documentation = tobject.note
else:
Expand Down Expand Up @@ -397,6 +399,8 @@ def attr_parse(session, parent: UMLClass, tattribute: TAttribute):
attr = UMLAttribute(parent, tattribute.name, tattribute.id)

# attr.visibility = element.get('visibility')
if tattribute.derived == "1":
attr.is_derived = True

type_elem = tattribute.type
if type_elem is not None:
Expand Down
149 changes: 77 additions & 72 deletions mdg/parse/sparx_db_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@
Text,
text,
)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm.decl_api import declarative_base as _declarative_base

Base: _declarative_base = declarative_base()
metadata = Base.metadata
from sqlalchemy.orm import (
DeclarativeBase,
mapped_column,
Mapped,
relationship,
)

class Base(DeclarativeBase):
pass


class TObject(Base):
Expand All @@ -28,16 +33,16 @@ class TObject(Base):
Index('ix_object_pkgidpdata1class', 'package_id', 'pdata1', 'classifier')
)

object_id = Column(Integer, primary_key=True, server_default=text("nextval(('object_id_seq'::text)::regclass)"))
object_id = mapped_column(Integer, primary_key=True, server_default=text("nextval(('object_id_seq'::text)::regclass)"))
object_type = Column(String(255), index=True)
diagram_id = Column(Integer, server_default=text("0"))
name = Column(String(255), index=True)
alias = Column(String(255))
author = Column(String(255))
version = Column(String(50), server_default=text("'1.0'::character varying"))
note = Column(Text)
name: Mapped[str] = mapped_column(String(255), index=True)
alias: Mapped[str] = mapped_column(String(255))
author: Mapped[str] = mapped_column(String(255))
version: Mapped[str] = mapped_column(String(50), server_default=text("'1.0'::character varying"))
note: Mapped[str] = mapped_column(Text)
package_id = Column(Integer, index=True, server_default=text("0"))
stereotype = Column(String(255))
stereotype: Mapped[str] = mapped_column(String(255))
ntype = Column(Integer, index=True, server_default=text("0"))
complexity = Column(String(50), server_default=text("'2'::character varying"))
effort = Column(Integer, server_default=text("0"))
Expand All @@ -49,8 +54,8 @@ class TObject(Base):
bordercolor = Column(Integer, server_default=text("0"))
createddate = Column(DateTime, server_default=text("now()"))
modifieddate = Column(DateTime, server_default=text("now()"))
status = Column(String(50))
abstract = Column(CHAR(1))
status: Mapped[str] = mapped_column(String(50))
abstract: Mapped[str] = mapped_column(CHAR(1))
tagged = Column(Integer, server_default=text("0"))
pdata1 = Column(String(255), index=True)
pdata2 = Column(Text, index=True)
Expand All @@ -65,12 +70,12 @@ class TObject(Base):
genfile = Column(String(255))
header1 = Column(Text)
header2 = Column(Text)
phase = Column(String(50))
scope = Column(String(25))
phase: Mapped[str] = mapped_column(String(50))
scope: Mapped[str] = mapped_column(String(25))
genoption = Column(Text)
genlinks = Column(Text, index=True)
classifier = Column(Integer, index=True)
ea_guid = Column(String(40), unique=True)
classifier: Mapped[int] = mapped_column(Integer, index=True)
ea_guid: Mapped[str] = mapped_column(String(40), unique=True)
parentid = Column(Integer, index=True)
runstate = Column(Text)
classifier_guid = Column(String(40), index=True)
Expand All @@ -90,8 +95,8 @@ class TObject(Base):
class TPackage(Base):
__tablename__ = 't_package'

package_id = Column(Integer, primary_key=True, server_default=text("nextval(('package_id_seq'::text)::regclass)"))
name = Column(String(255), index=True)
package_id: Mapped[int] = mapped_column(Integer, primary_key=True, server_default=text("nextval(('package_id_seq'::text)::regclass)"))
name: Mapped[str] = mapped_column(String(255), index=True)
parent_id = Column(Integer, index=True, server_default=text("0"))
createddate = Column(DateTime, server_default=text("now()"))
modifieddate = Column(DateTime, server_default=text("now()"))
Expand Down Expand Up @@ -139,33 +144,33 @@ class TXref(Base):
class TAttribute(Base):
__tablename__ = 't_attribute'

object_id = Column(Integer, index=True, server_default=text("0"))
name = Column(String(255), index=True)
scope = Column(String(50))
stereotype = Column(String(50))
containment = Column(String(50))
object_id: Mapped[int] = mapped_column(Integer, index=True, server_default=text("0"))
name: Mapped[str] = mapped_column(String(255), index=True)
scope: Mapped[str] = mapped_column(String(50))
stereotype: Mapped[str] = mapped_column(String(50))
containment: Mapped[str] = mapped_column(String(50))
isstatic = Column(Integer, server_default=text("0"))
iscollection = Column(Integer, server_default=text("0"))
isordered = Column(Integer, server_default=text("0"))
allowduplicates = Column(Integer, server_default=text("0"))
lowerbound = Column(String(50))
upperbound = Column(String(50))
container = Column(String(50))
notes = Column(Text)
derived = Column(CHAR(1))
id = Column(Integer, primary_key=True, server_default=text("nextval(('id_seq'::text)::regclass)"))
notes: Mapped[str] = mapped_column(Text)
derived: Mapped[str] = mapped_column(CHAR(1))
id: Mapped[int] = mapped_column(Integer, primary_key=True, server_default=text("nextval(('id_seq'::text)::regclass)"))
pos = Column(Integer)
genoption = Column(Text)
length = Column(Integer)
precision = Column(Integer)
scale = Column(Integer)
const = Column(Integer)
style = Column(String(255))
classifier = Column(String(50), index=True)
Default = Column(Text)
type = Column(String(255), index=True)
ea_guid = Column(String(50), unique=True)
styleex = Column(Text)
length: Mapped[int] = mapped_column(Integer)
precision: Mapped[int] = mapped_column(Integer)
scale: Mapped[int] = mapped_column(Integer)
const: Mapped[int] = mapped_column(Integer)
style: Mapped[str] = mapped_column(String(255))
classifier: Mapped[str] = mapped_column(String(50), index=True)
Default: Mapped[str] = mapped_column(Text)
type: Mapped[str] = mapped_column(String(255), index=True)
ea_guid: Mapped[str] = mapped_column(String(50), unique=True)
styleex: Mapped[str] = mapped_column(Text)


class TConnector(Base):
Expand All @@ -175,28 +180,28 @@ class TConnector(Base):
Index('ix_connector_endobjidconnid', 'end_object_id', 'connector_id')
)

connector_id = Column(Integer, primary_key=True, server_default=text("nextval(('connector_id_seq'::text)::regclass)"))
connector_id: Mapped[int] = mapped_column(Integer, primary_key=True, server_default=text("nextval(('connector_id_seq'::text)::regclass)"))
name = Column(String(255))
direction = Column(String(50))
notes = Column(Text)
connector_type = Column(String(50), index=True)
subtype = Column(String(50), index=True)
sourcecard = Column(String(50))
sourceaccess = Column(String(50))
sourceelement = Column(String(50))
destcard = Column(String(50))
destaccess = Column(String(50))
destelement = Column(String(50))
sourcerole = Column(String(255))
sourceroletype = Column(String(50))
sourcerolenote = Column(Text)
sourcecontainment = Column(String(50))
notes: Mapped[str] = mapped_column(Text)
connector_type: Mapped[str] = mapped_column(String(50), index=True)
subtype: Mapped[str] = mapped_column(String(50), index=True)
sourcecard: Mapped[str] = mapped_column(String(50))
sourceaccess: Mapped[str] = mapped_column(String(50))
sourceelement: Mapped[str] = mapped_column(String(50))
destcard: Mapped[str] = mapped_column(String(50))
destaccess: Mapped[str] = mapped_column(String(50))
destelement: Mapped[str] = mapped_column(String(50))
sourcerole: Mapped[str] = mapped_column(String(255))
sourceroletype: Mapped[str] = mapped_column(String(50))
sourcerolenote: Mapped[str] = mapped_column(Text)
sourcecontainment: Mapped[str] = mapped_column(String(50))
sourceisaggregate = Column(Integer, server_default=text("0"))
sourceisordered = Column(Integer, server_default=text("0"))
sourcequalifier = Column(String(50))
destrole = Column(String(255))
destroletype = Column(String(50))
destrolenote = Column(Text)
destrole: Mapped[str] = mapped_column(String(255))
destroletype: Mapped[str] = mapped_column(String(50))
destrolenote: Mapped[str] = mapped_column(Text)
destcontainment = Column(String(50))
destisaggregate = Column(Integer, server_default=text("0"))
destisordered = Column(Integer, server_default=text("0"))
Expand All @@ -221,7 +226,7 @@ class TConnector(Base):
routestyle = Column(Integer, server_default=text("0"))
isbold = Column(Integer, server_default=text("0"))
linecolor = Column(Integer, server_default=text("0"))
stereotype = Column(String(50))
stereotype: Mapped[str] = mapped_column(String(50))
virtualinheritance = Column(CHAR(1))
linkaccess = Column(String(50))
pdata1 = Column(String(255), index=True)
Expand All @@ -230,30 +235,30 @@ class TConnector(Base):
pdata4 = Column(String(255))
pdata5 = Column(Text, index=True)
diagramid = Column(Integer, index=True, server_default=text("0"))
ea_guid = Column(String(40), unique=True)
ea_guid: Mapped[str] = mapped_column(String(40), unique=True)
sourceconstraint = Column(String(255))
destconstraint = Column(String(255))
sourceisnavigable = Column(Integer, server_default=text("0"))
destisnavigable = Column(Integer, server_default=text("0"))
isroot = Column(Integer, server_default=text("0"))
isleaf = Column(Integer, server_default=text("0"))
isspec = Column(Integer, server_default=text("0"))
sourcechangeable = Column(String(12))
destchangeable = Column(String(12))
sourcets = Column(String(12))
destts = Column(String(12))
stateflags = Column(Text)
actionflags = Column(String(255))
isroot: Mapped[int] = mapped_column(Integer, server_default=text("0"))
isleaf: Mapped[int] = mapped_column(Integer, server_default=text("0"))
isspec: Mapped[int] = mapped_column(Integer, server_default=text("0"))
sourcechangeable: Mapped[str] = mapped_column(String(12))
destchangeable: Mapped[str] = mapped_column(String(12))
sourcets: Mapped[str] = mapped_column(String(12))
destts: Mapped[str] = mapped_column(String(12))
stateflags: Mapped[str] = mapped_column(Text)
actionflags: Mapped[str] = mapped_column(String(255))
issignal = Column(Integer, server_default=text("0"))
isstimulus = Column(Integer, server_default=text("0"))
dispatchaction = Column(String(255))
target2 = Column(Integer)
styleex = Column(Text, index=True)
sourcestereotype = Column(String(255))
deststereotype = Column(String(255))
sourcestyle = Column(Text)
deststyle = Column(Text)
eventflags = Column(String(255))
target2: Mapped[str] = mapped_column(Integer)
styleex: Mapped[str] = mapped_column(Text, index=True)
sourcestereotype: Mapped[str] = mapped_column(String(255))
deststereotype: Mapped[str] = mapped_column(String(255))
sourcestyle: Mapped[str] = mapped_column(Text)
deststyle: Mapped[str] = mapped_column(Text)
eventflags: Mapped[str] = mapped_column(String(255))


class TAttributeconstraint(Base):
Expand Down
Loading

0 comments on commit 193fa03

Please sign in to comment.