# Advanced Expression Templates

## Overview

In addition to the simple examples shown on the previous pages, more advanced expression templates can also be used. On this page we show some examples of expression constraints with multiple replacement slots, multiple cardinality constraints and slot co-dependency constraints.

## Multiple Replacement Slots

In many situations, it is useful for an expression template to contain more than one replacement slot. Below are some examples.

#### Example 1

The following expression template uses three replacement slots to generate an expression that represents a type of procedure. The first slot (named "Procedure") is a placeholder for the focus concept, while the second slot (named "BodySite") is a placeholder for the [| Procedure site - Direct|](http://snomed.info/id/405813007) , and the third slot (named "Method") is a placeholder for the [| Method|](http://snomed.info/id/260686004) .

{% @snomed-syntax-highlighter/snomed-syntax-highlighter lines="4" content="\[\[+ (< 71388002 |Procedure| ) @Procedure]]: { 405813007 |Procedure site - direct| = \[\[+ (< 91723000 |Anatomical structure|) @BodySite]], 260686004 |Method| = \[\[+ (< 129264002 |Action (qualifier value)| ) @Method]] }" %}

If the slots are populated with the values:

* Procedure = [387713003 | Surgical procedure|](http://snomed.info/id/387713003)
* BodySite = [66754008 | Appendix structure|](http://snomed.info/id/66754008)
* Method = [129304002 | Excision - action|](http://snomed.info/id/129304002)

then the following expression would be generated.

{% @snomed-syntax-highlighter/snomed-syntax-highlighter lines="5" content="387713003 |Surgical procedure|: { 405813007 |Procedure site - direct| = 66754008 |Appendix structure| , 260686004 |Method| = 129304002 |Excision - action| }" processed="true" %}

#### Example 2

Another example of an expression template with multiple slots is shown below. This expression template is used to generate expressions that represent the family history of a patient. The template contains two slots - the first slot (named "Finding") is a placeholder for the [| Clinical finding|](http://snomed.info/id/404684003) known to be present in the family member, while the second slot (named "Relationship") is a placeholder for the [| Subject relationship context|](http://snomed.info/id/408732007) of this [| Clinical finding|](http://snomed.info/id/404684003) .

{% @snomed-syntax-highlighter/snomed-syntax-highlighter lines="6" content="243796009 |Situation with explicit context|: { 246090004 |Associated finding| = \[\[+id (< 404684003 |Clinical finding|) @Finding]], 408731000 |Temporal context| = 410511007 |Current or past (actual)| , 408729009 |Finding context| = 410515003 |Known present| , 408732007 |Subject relationship context| = \[\[+id (<< 444148008 |Person in family of subject| ) @Relationship]] }" %}

If the following input data is provided (in which each row represents a separate expression):

| Finding                                                                  | Relationship                                                                      |
| ------------------------------------------------------------------------ | --------------------------------------------------------------------------------- |
| [93870000 \| Liver cancer\|](http://snomed.info/id/93870000)             | [444244000 \| Maternal grandmother of subject\|](http://snomed.info/id/444244000) |
| [57809008 \| Myocardial disease\|](http://snomed.info/id/57809008)       | [444292000 \| Paternal grandfather of subject\|](http://snomed.info/id/444292000) |
| [46635009 \| Diabetes mellitus type 1\|](http://snomed.info/id/46635009) | [444301002 \| Mother of subject\|](http://snomed.info/id/444301002)               |

Then the following family history expressions would be generated.

{% @snomed-syntax-highlighter/snomed-syntax-highlighter lines="7" content="243796009 |Situation with explicit context| : { 246090004 |Associated finding| = 93870000 |Liver cancer| , 408731000 |Temporal context| = 410511007 |Current or past (actual)| , 408729009 |Finding context| = 410515003 |Known present| , 408732007 |Subject relationship context| = 444244000 |Maternal grandmother of subject| }" processed="true" %}

{% @snomed-syntax-highlighter/snomed-syntax-highlighter lines="7" content="243796009 |Situation with explicit context| : { 246090004 |Associated finding| = 57809008 |Myocardial disease| , 408731000 |Temporal context| = 410511007 |Current or past (actual)| , 408729009 |Finding context| = 410515003 |Known present| , 408732007 |Subject relationship context| = 444292000 |Paternal grandfather of subject| }" processed="true" %}

{% @snomed-syntax-highlighter/snomed-syntax-highlighter lines="6" content="243796009 |Situation with explicit context| : { 246090004 |Associated finding| = 46635009 |Diabetes mellitus type 1| , 408731000 |Temporal context| = 410511007 |Current or past (actual)| , 408729009 |Finding context| = 410515003 |Known present| , 408732007 |Subject relationship context| = 444301002 |Mother of subject| }" processed="true" %}

## Multiple Cardinality Constraints

Expression templates may also use more than one cardinality constraint to indicate the repeatability of different parts of the expression. Below are some examples.

#### Example 1

The following expression template uses three replacement slots to generate an expression that represents a type of procedure. The first slot (named "Procedure") is a placeholder for the focus concept, while the second slot (named "BodySite") is a placeholder for the [| Procedure site - Direct|](http://snomed.info/id/405813007) , and the third slot (named "Method") is a placeholder for the [| Method|](http://snomed.info/id/260686004) .

{% @snomed-syntax-highlighter/snomed-syntax-highlighter lines="4" content="\[\[1..1]] \[\[+ (< 71388002 |Procedure| ) @Procedure]] : \[\[ 1..2 @SMgroup]] { \[\[1..1]] 405813007 |Procedure site - direct| = \[\[+ (< 91723000 |Anatomical structure| ) @BodySite]], \[\[1..1]] 260686004 |Method| = \[\[+ (< 129264002 |Action (qualifier value)| ) @Method]] }" processed="true" %}

The first cardinality constraint (i.e. 1..1 ) indicates that exactly one value should be populated in the *Procedure* slot. The second cardinality constraint (i.e. 1..2 ) indicates that it is valid to include either 1 or 2 relationship groups in the resulting expression. The last two cardinality constraints, that appear inside the relationship group, (i.e. 1..1 ) indicate that in each instance of a relationship group, exactly one [| Procedure site - direct|](http://snomed.info/id/405813007) value and exactly one [| Method|](http://snomed.info/id/260686004) value should be used.

The following input data satisfies these cardinality constraints.

| Procedure                        | SMgroup | BodySite                           | Method                             |
| -------------------------------- | ------- | ---------------------------------- | ---------------------------------- |
| 387713003 \|Surgical procedure\| | 1       | 28273000 \|Bile duct structure\|   | 281615006 \|Exploration - action\| |
|                                  | 2       | 28231008 \|Gallbladder structure\| | 129304002 \|Excision - action\|    |
| 387713003 \|Surgical procedure\| | 1       | 66754008 \|Appendix structure\|    | 129304002 \|Excision - action\|    |

If the slots are populated with the values above, then the following expressions would be generated

{% @snomed-syntax-highlighter/snomed-syntax-highlighter lines="9" content="387713003 |Surgical procedure| : { 405813007 |Procedure site - direct| = 28273000 |Bile duct structure| , 260686004 |Method| = 281615006 |Exploration - action| }, { 405813007 |Procedure site - direct| = 28231008 |Gallbladder structure| , 260686004 |Method| = 129304002 |Excision - action| }" processed="true" %}

{% @snomed-syntax-highlighter/snomed-syntax-highlighter lines="4" content="387713003 |Surgical procedure| : { 405813007 |Procedure site - direct| = 66754008 |Appendix structure| , 260686004 |Method| = 129304002 |Excision - action| }" processed="true" %}

#### **Example 2**

Another example of an expression template with multiple cardinality constraints is shown below. This expression template is used to generate expressions that represent a clinical finding with explicit context.

{% @snomed-syntax-highlighter/snomed-syntax-highlighter lines="9" content="\[\[1..1]] \[\[+id (<< 413350009 |Finding with explicit context| ) @Condition]]: \[\[ 1..2 @AFgroup ]] { \[\[1..1]] 246090004 |Associated finding| = ( \[\[+id (<< 404684003 |Clinical finding| ) @Finding]]: \[\[0..1 @SSgroup]] { \[\[0..1]] 246112005 |Severity| = \[\[+id (< 272141005 |Severities| ) @Severity]], \[\[0..1]] 363698007 |Finding site| = \[\[+id (< 91723000 |Anatomical structure| ) @Site]] } ), \[\[1..1]] 408732007 |Subject relationship context| = \[\[+id (< 444148008 |Person in family of subject| ) @Relationship]], \[\[1..1]] 408731000 |Temporal context| = \[\[+id (< 410510008 |Temporal context value| ) @Time]], \[\[1..1]] 408729009 |Finding context| = \[\[+id (< 410514004 |Finding context value| ) @Context]] }" processed="true" %}

The first cardinality constraint (i.e. 1..1 ) indicates that exactly one value should be populated in the *Condition* slot. The second cardinality constraint (i.e. 1..2 ) indicates that it is valid to include either 1 or 2 relationship groups (named "AFgroup") in the resulting expression. Each *AFgroup* relationship group must have exactly one [| Associated finding|](http://snomed.info/id/246090004) , exactly one [| Subject relationship context|](http://snomed.info/id/408732007) , exactly one [| Temporal context|](http://snomed.info/id/408731000) and exactly one [| Finding context|](http://snomed.info/id/408729009) . The value of the [| Associated finding|](http://snomed.info/id/246090004) in each *AFgroup* is an expression, may optionally be refined using a single relationship group named "SSgroup". Each *SSgroup* optionally has one [| Severity|](http://snomed.info/id/246112005) and optionally has one [| Finding site|](http://snomed.info/id/363698007) . Based on these cardinality constraints, the input data shown in [Table 8.6-1](https://confluence.ihtsdotools.org/display/DOCSTS/8.6.+Advanced+Expression+Templates#Table-valid-input-example-2) would be valid.

Table 8.6-1: Valid Input Data for Example 2

| Condition                                         | AF group | Finding                               | Severity             | Site                          | Relationship                                  | Time                                    | Context                     |
| ------------------------------------------------- | -------- | ------------------------------------- | -------------------- | ----------------------------- | --------------------------------------------- | --------------------------------------- | --------------------------- |
| 243796009 \|Situation with explicit context\|     | 1        | 56265001 \|Heart disease\|            | 24484000 \|Severe\|  |                               | 444292000 \|Paternal grandfather of subject\| | 410512000 \|Current or specified time\| | 410515003 \|Known present\| |
|                                                   | 2        | 22298006 \|Myocardial infarction\|    |                      |                               | 444292000 \|Paternal grandfather of subject\| | 410589000 \|All times past\|            | 410516002 \|Known absent\|  |
| 57177007 \|Family history with explicit context\| | 1        | 363346000 \|Cancer\|                  | 6736007 \|Moderate\| | 76752008 \|Breast structure\| | 444244000 \|Maternal grandmother of subject\| | 410512000 \|Current or specified time\| | 410515003 \|Known present\| |
| 160303001 \|FH: Diabetes mellitus\|               | 1        | 46635009 \|Diabetes mellitus type 1\| |                      |                               | 444301002 \|Mother of subject\|               | 410512000 \|Current or specified time\| | 410515003 \|Known present\| |

If the slots are populated with the values above, then the following expressions would be generated.

{% @snomed-syntax-highlighter/snomed-syntax-highlighter lines="13" content="243796009 |Situation with explicit context| : { 246090004 |Associated finding| = ( 56265001 |Heart disease| : { 246112005 |Severity| = 24484000 |Severe| }), 408732007 |Subject relationship context| = 444292000 |Paternal grandfather of subject| , 408731000 |Temporal context| = 410511007 |Current or past (actual)| , 408729009 |Finding context| = 410515003 |Known present| }, { 246090004 |Associated finding| = 22298006 |Myocardial infarction| , 408732007 |Subject relationship context| = 444292000 |Paternal grandfather of subject| , 408731000 |Temporal context| = 410589000 |All times past| , 408729009 |Finding context| = 410516002 |Known absent| }" %}

{% @snomed-syntax-highlighter/snomed-syntax-highlighter lines="10" content="57177007 |Family history with explicit context| : { 246090004 |Associated finding| = ( 363346000 |Cancer| : { 246112005 |Severity| = 6736007 |Moderate| , 363698007 |Finding site| = 76752008 |Breast structure| } ), 408732007 |Subject relationship context| = 444244000 |Maternal grandmother of subject| , 40873100 |Temporal context| = 410511007 |Current or past (actual)| , 408729009 |Finding context| = 410515003 |Known present| }" %}

{% @snomed-syntax-highlighter/snomed-syntax-highlighter lines="6" content="160303001 |FH: Diabetes mellitus| : { 246090004 |Associated finding| = 46635009 |Diabetes mellitus type 1| , 40873100 |Temporal context| = 410511007 |Current or past (actual)| , 408729009 |Finding context| = 410515003 |Known present| , 408732007 |Subject relationship context| = 444301002 |Mother of subject| }" %}

Please note that when part of an expression is repeated, connectors (e.g. a comma) must be added between the parts during processing. Similarly, when part of an expression is absent then clean up is required (e.g. to remove commas and brackets). For more information on these processing steps, please refer to [7.2. Template Processing](https://github.com/SNOMED-Documents/snomed-termplate-syntax-specification/blob/main/8%20expression-template-examples/7.2.-Template-Processing_45517676.html).

<a href="https://docs.google.com/forms/d/e/1FAIpQLScTmbZIf0UEQwYDkY27EEWBkaiYkHSbR0_9DmFrMLXoQLyL7Q/viewform?usp=pp_url&#x26;entry.1767247133=SNOMED+Template+Syntax+Specification&#x26;entry.670899847=Advanced%20Expression%20Templates" class="button primary">Provide Feedback</a>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.snomed.org/snomed-ct-specifications/snomed-ct-template-syntax-specification/8-expression-template-examples/8.6-advanced-expression-templates.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
