From ab9239c03e8205c20d7173afaa37f8939fdd5db9 Mon Sep 17 00:00:00 2001 From: Martin Lehmann Date: Fri, 26 Jan 2024 14:43:02 +0100 Subject: [PATCH] More work --- capella_model_explorer/demo.py | 161 +++++++++++++++++++-------------- pyproject.toml | 8 +- 2 files changed, 102 insertions(+), 67 deletions(-) diff --git a/capella_model_explorer/demo.py b/capella_model_explorer/demo.py index 436dd12..416c169 100644 --- a/capella_model_explorer/demo.py +++ b/capella_model_explorer/demo.py @@ -1,76 +1,105 @@ -import streamlit as st +import capellambse import pandas as pd +import streamlit as st from jinja2.environment import Environment -import capellambse - env = Environment() -model = capellambse.MelodyModel( - path="automated-train", - entrypoint="/automated-train.aird", -) -viewpoint = "capabilities" +@st.cache_resource +def load_model() -> capellambse.MelodyModel: + return capellambse.MelodyModel( + path="automated-train", + entrypoint="/automated-train.aird", + ) + + +model = load_model() with st.sidebar: st.write("# Sidebar!") - viewpoint = st.selectbox("Viewpoint:", ["Capabilities", "Functions", "Interfaces"]) - - -sys_caps = model.sa.all_capabilities - -st.write(""" -# Caps -""") -sys_caps_idx = {cap.name: cap for cap in sys_caps} -selected_name = st.selectbox("Select System Capability", options=sys_caps_idx.keys()) - -cap = sys_caps_idx[selected_name] - -st.image(cap.context_diagram.as_svg) - -cap_real_entities = set([entity.owner for entity in cap.involved_functions if entity.owner]) -table = pd.DataFrame([ - { - "name": obj.name, - "functions": "; ".join(["" + fnc.name for fnc in cap.involved_functions if fnc.owner == obj]) - } for obj in cap_real_entities - ]) - -st.dataframe(table.style.set_properties(**{"white-space": "pre-wrap"})) - -table_template = """ -To realize this capability the System and involved actors are required to perform the following functions:

- - - - - -{% for owner, fncs in cap.involved_functions | rejectattr("owner","none") | groupby("owner.name") %} - - - +
Involved EntityRequired function
{{owner}} - {% for fnc in fncs %} - - {{fnc.name}} + viewpoint = st.radio( + "Viewpoint:", ["Capabilities", "Functions", "Interfaces"] + ) + + +if viewpoint == "Capabilities": + with st.sidebar: + sys_caps = model.sa.all_capabilities + sys_caps_idx = {cap.name: cap for cap in sys_caps} + selected_name = st.selectbox( + "Select System Capability", options=sys_caps_idx.keys() + ) + cap = sys_caps_idx[selected_name] + st.write(f"# {cap.name}") + + st.write("## Description") + st.write(cap.description, unsafe_allow_html=True) + + st.write("## Context Diagram") + st.write( + """\ + The image below provides an overview over the actors immediately + involved in the Capability. + """ + ) + st.image(cap.context_diagram.as_svg) + + st.write("## Required Functions") + cap_real_entities = { + entity.owner for entity in cap.involved_functions if entity.owner + } + table = pd.DataFrame( + [ + { + "name": obj.name, + "functions": "; ".join( + fnc.name + for fnc in cap.involved_functions + if fnc.owner == obj + ), + } + for obj in cap_real_entities + ] + ) + + st.dataframe( + table.style.set_properties(**{"white-space": "pre-wrap"}), + hide_index=True, + ) + +elif viewpoint == "Functions": + table_template = """ + To realize this capability the System and involved actors are required to perform the following functions:

+ + + + + + {% for owner, fncs in cap.involved_functions | rejectattr("owner","none") | groupby("owner.name") %} + + + + {% endfor %} - - -{% endfor %} - -
Involved EntityRequired function
{{owner}} + {% for fnc in fncs %} + - {{fnc.name}} + {% endfor %} +
-""" -template = env.from_string(table_template) -st.markdown(template.render(cap=obj), unsafe_allow_html=True) - -fncs = model.sa.all_functions - -st.write(""" -# Functions -""") -sys_fncs = {fnc.name: fnc for fnc in fncs} -selected_fnc_name = st.selectbox("Select Function", options=sys_fncs.keys()) -fnc_obj = sys_fncs[selected_fnc_name] - -card = f"""{fnc_obj.context_diagram.as_svg}""" -st.markdown(card, unsafe_allow_html=True) \ No newline at end of file +
+ """ + template = env.from_string(table_template) + st.markdown(template.render(cap=obj), unsafe_allow_html=True) + + fncs = model.sa.all_functions + + st.write("# Functions") + sys_fncs = {fnc.name: fnc for fnc in fncs} + selected_fnc_name = st.selectbox( + "Select Function", options=sys_fncs.keys() + ) + fnc_obj = sys_fncs[selected_fnc_name] + + card = f"""{fnc_obj.context_diagram.as_svg}""" + st.markdown(card, unsafe_allow_html=True) diff --git a/pyproject.toml b/pyproject.toml index f81eb90..28aaf5e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,7 +27,13 @@ classifiers = [ "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", ] -dependencies = [] +dependencies = [ + "capellambse", + "capellambse-context-diagrams", + "jinja2", + "pandas", + "streamlit", +] [project.urls] Homepage = "https://github.com/DSD-DBS/capella-model-explorer"