Skip to content

Commit

Permalink
feat: additional template examples for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
micha91 committed Jun 26, 2024
1 parent 6e19b6b commit 167e921
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 86 deletions.
41 changes: 41 additions & 0 deletions tests/data/jinja_templates/class.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{#
Copyright DB InfraGO AG and contributors
SPDX-License-Identifier: Apache-2.0
#}

{% from 'common_macros.html.j2' import show_other_attributes, linked_name, linked_name_with_icon, description, display_property_label %}

{% macro properties_table(props, obj) %}
{% for property in props %}
{{ display_property_label(obj, property) | safe }}
<div style="margin-left: 20px;">
{% if property.kind != "UNSET" %}
<p>KIND: {{ property.kind }}</p>
{% endif %}
{{ description(property) | safe}}
</div>
<hr>
{% endfor %}
{% endmacro %}

<p>Parent: <a href="{{ object.parent | make_href }}">{{ object.parent.name }}</a></p>
{% if object.description %}
<p>{{ object.description }}</p>
{% else %}
<p style="color:red">No description available.</p>
{% endif %}

{% set props = [] %}
{% if object.super %}
{% set props = props | list + object.super.properties | list %}
{% endif %}
{% set props = props + object.owned_properties | list %}

<span style="font-weight: bold;font-size: 12pt;line-height: 1.5;">Properties</span>
<br>
{% if props %}
<p>The object owns the properties listed below; We use the following format to describe property: name : type [min .. max (instances of type)] or [ fixed number]; if no multiplicity is shown assume its 1 (single instance).</p>
{{ properties_table(props, object) | safe }}
{% else %}
<p style="color:red">No properties are owned by this object.</p>
{% endif %}
68 changes: 0 additions & 68 deletions tests/data/jinja_templates/classes.html.j2

This file was deleted.

87 changes: 70 additions & 17 deletions tests/data/jinja_templates/common_macros.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
SPDX-License-Identifier: Apache-2.0
#}

{% macro first_upper(text) %}
{{ text[0] | upper}}{{text[1:] }}
{% endmacro %}

{% macro show_compliance_to_modeling_rules(object) %}
{% if "validation" in object.__dir__() %}
<h2>Compliance to modeling rules</h2>
Expand Down Expand Up @@ -79,8 +83,7 @@
{% for rel in rels %}
<li>
<a href="{{ rel.type | make_href }}">{{rel.type.long_name }}</a>
{{ rel.target.__class__.__name__ }}
<a href="{{ rel.target | make_href }}">{{ rel.target.name }}</a>
{{ rel.target.__class__.__name__ }} {{ linked_name_with_icon(rel.target) | safe}}</a>
</li>
{% endfor %}
</ul>
Expand All @@ -90,13 +93,22 @@
{% endmacro %}

{% macro render_requirements_table(reqs) %}
<table>
{% for req in reqs %}
<div style="margin-top: 1em; margin-bottom: 1em;">
<div><b>ID:</b> <a href="{{ req | make_href }}">{{ req.uuid }}</a></div>
<div>{{ req.text if req.text else req.long_name }}</div>
<div>{{req_rels(req.relations) | safe}}</div>
<tr>
<td>
<a href="{{ req | make_href }}">
{{ req.identifier if req.identifier else "REQ-"+req.uuid[-5:] | upper }}
</a>
</td>
<td>
{{ req.text if req.text else req.long_name }}
{{req_rels(req.relations) | safe}}
</td>
</div>
{% endfor %}
</tr>
{% endfor %}
</table>
{% endmacro %}

{% macro render_reqs_by_type(reqs, types) %}
Expand All @@ -121,6 +133,19 @@

{%- macro linked_name(object) -%}<a href="{{ object | make_href }}">{{ object.name | trim }}</a>{%- endmacro -%}

{% macro draw_icon(obj, size) %}
{% set icon = obj._get_icon("datauri_svg", size=size) %}
{% if icon %}
<img src="{{ icon | safe }}" height="{{size}}" width="{{size}}" style="display:inline-block"/>
{% endif %}
{% endmacro %}

{% macro linked_name_with_icon(obj) %}
<a href="hlink://{{obj.uuid}}">
{{ obj.name }}
</a>
{% endmacro %}

{%- macro display_traceability(object, complain=False) -%}
{%- set realized_attrs = [] -%}
{%- set realizing_attrs = [] -%}
Expand All @@ -135,35 +160,37 @@
{%- set realizing_objects = object[realizing_attrsfirst] if realizing_attrs else None -%}

<h2>Traceability</h2>
<p>The figure below provides an overview of objects that "{{ object.name}}" realizes but also those that realize "{{ object.name }}"</p>
{{ object.realization_view.as_svg | safe }}
{% if realized_objects %}
<p>{{ object.name }} realizes the following objects:<p>
<ul>
{%- for obj in realized_objects -%}<li>{{ typed_name(obj) | safe}}</li>{%- endfor -%}
{%- for obj in realized_objects -%}<li>{{ linked_name_with_icon(obj) | safe}}</li>{%- endfor -%}
</ul>
{%- elif complain -%}
<p style="color: red;">{{ object.name }} doesn't seem to realize any object</p>
{%- else -%}
<p style="color: red;">{{ first_upper(object.name) }} doesn't seem to realize any object</p>
{%- endif -%}
{% if realizing_objects %}
<p>{{ object.name }} is realized by the following objects:</p>
<ul>
{%- for obj in realizing_objects -%}<li>{{ typed_name(obj) | safe}}</li>{%- endfor -%}
{%- for obj in realizing_objects -%}<li>{{ linked_name_with_icon(obj) | safe}}</li>{%- endfor -%}
</ul>
{%- elif complain -%}
<p style="color: red;">{{ object.name }} doesn't seem to be realized by any object</p>
{%- else -%}
<p style="color: red;">{{ first_upper(object.name) }} doesn't seem to be realized by any object</p>
{% endif %}
{%- endmacro -%}

{% macro display_property_values(object) %}
{% if object.property_value_groups %}
<h2>Other properties</h2>
<p>The following properties were additionally assigned to {{ object.name }}</p>
<h2>Other properties (PVMT)</h2>
<p>The following properties were additionally assigned to {{ object.name }} via PVMT extension</p>
{% for group in object.property_value_groups %}
<p><b>{{ group.name | safe}}</b></p>
<table>
<thead>
<tr>
<td>Property</td>
<td>Value</td>
<th><b>Property</b></th>
<th><b>Value</b></th>
</tr>
</thead>
<tbody>
Expand All @@ -178,3 +205,29 @@
{% endfor %}
{% endif %}
{% endmacro %}

{% macro display_property_label(object, property) %}
<p>
<b>{{ property.name }}</b> :
{% set type = None %}
{% if "type" in property.__dir__() %}
{% set type = property.type %}
{% elif "abstract_type" in property.__dir__() %}
{% set type = property.abstract_type %}
{% endif %}
{% if type %}
{{ linked_name_with_icon(type) | safe }}
{% else %}
<span style="color:red">No type defined</span>
{% endif %}
{% if property.min_card.value == property.max_card.value and property.max_card.value == "1" %}
{% elif property.min_card.value == property.max_card.value %}
[ {{ property.min_card.value }} ]
{% else %}
[ {{ property.min_card.value }} .. {{ property.max_card.value }} ]
{% endif %}
{% if property.parent != object %}
(inherited from {{ linked_name_with_icon(property.parent) | safe }})
{% endif %}
</p>
{% endmacro %}
37 changes: 37 additions & 0 deletions tests/data/jinja_templates/exchange_item.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{#
Copyright DB InfraGO AG and contributors
SPDX-License-Identifier: Apache-2.0
#}

{% from 'common_macros.html.j2' import show_other_attributes, description, typed_name, linked_name, linked_name_with_icon, display_property_label %}

{% macro render_usage(object) %}
<span style="font-weight: bold;font-size: 12pt;line-height: 1.5;">Usage</span>
{% if object.exchanges %}
<p>The exchange item "{{ object.name }}" is produced and used across the model in the following cases:</p>
<ul>
{%- for exchange in object.exchanges -%}
{%- set src = exchange.source.owner.owner if exchange.source.owner.owner else None -%}
{%- set tgt = exchange.target.owner.owner if exchange.target.owner.owner else None -%}
<li>
{{ linked_name_with_icon(exchange) | safe }}
, produced by {{ linked_name_with_icon(exchange.source.owner) | safe }} of {{ linked_name_with_icon(src) | safe if src else "Unassigned" }}
&nbsp;and consumed by {{ linked_name_with_icon(exchange.target.owner) | safe }} of {{ linked_name_with_icon(tgt) | safe if tgt else "Unassigned" }}</li>
{%- endfor -%}
</ul>
{% else %}
<p style="color: red;">The exchange item "{{ object.name }}" is not used in any exchanges.</p>
{% endif %}
{% endmacro %}

{{ description(object) | safe }}

{% if object.elements %}
<span style="font-weight: bold;font-size: 12pt;line-height: 1.5;">Composition</span>
<p>The exchange item "{{ object.name }}" is composed of the following elements:</p>
<ul>
{%- for element in object.elements -%}
<li>{{ display_property_label(object, element) | safe }}</li>
{%- endfor -%}
</ul>
{% endif %}
24 changes: 24 additions & 0 deletions tests/data/jinja_templates/functional_exchange.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{#
Copyright DB InfraGO AG and contributors
SPDX-License-Identifier: Apache-2.0
#}

{% from 'common_macros.html.j2' import show_other_attributes, description, typed_name, linked_name, linked_name_with_icon, display_property_label %}

{%- set source_function = object.source.owner -%}
{%- set target_function = object.target.owner -%}
{%- set source = source_function.owner -%}
{%- set target = target_function.owner -%}
The {{ linked_name(source) | safe }} shall provide {{ linked_name_with_icon(object) | safe }} to {{ linked_name_with_icon(target) | safe }} so that the {{ linked_name_with_icon(target) | safe }} could {{ linked_name_with_icon(target_function)|safe }}.
{% if object.exchange_items %}
{% if object.exchange_items | length > 1 %}
<p>{{ object.name }} is further specified via the following Exchange Items:</p>
<ul>
{% for item in object.exchange_items %}
<li>{{ linked_name_with_icon(item) | safe }}</li>
{% endfor %}
</ul>
{% else %}
<p>This interaction is further specified via {{ linked_name_with_icon(object.exchange_items[0]) | safe}} Exchange Item</p>
{% endif %}
{% endif %}
2 changes: 2 additions & 0 deletions tests/test_documents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0
2 changes: 1 addition & 1 deletion tests/test_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -1605,7 +1605,7 @@ def test_add_jinja_to_description(self, model: capellambse.MelodyModel):
{
"jinja_as_description": {
"template_folder": "tests/data/jinja_templates",
"template_path": "classes.html.j2",
"template_path": "class.html.j2",
}
},
[],
Expand Down

0 comments on commit 167e921

Please sign in to comment.