Skip to content

Commit befb85f

Browse files
Copilotstarseeker
andcommitted
Add TREAT-based AP242 schema variant for standards compliance comparison (#15)
* Initial plan * Add documentation and clarify TYPEis_aggregate as shallow check Co-authored-by: starseeker <238416+starseeker@users.noreply.github.com> * Add test for aggregate unwrapping with typedef chains Co-authored-by: starseeker <238416+starseeker@users.noreply.github.com> * Add TREAT-based corrected AP242 schema and comparison documentation Co-authored-by: starseeker <238416+starseeker@users.noreply.github.com> * Add AP242 schema README and update documentation with comparison references Co-authored-by: starseeker <238416+starseeker@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: starseeker <238416+starseeker@users.noreply.github.com>
1 parent aa79dbf commit befb85f

7 files changed

Lines changed: 55520 additions & 0 deletions

File tree

data/ap242/242_mim_lf_treat.exp

Lines changed: 54756 additions & 0 deletions
Large diffs are not rendered by default.

data/ap242/README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# AP242 Schema Files
2+
3+
This directory contains two versions of the AP242 (ISO 10303-242) schema.
4+
5+
## Files
6+
7+
### `242_mim_lf.exp` (Original)
8+
- **Status**: Original ISO schema with non-standard parsing requirement
9+
- **Size**: 54,756 lines
10+
- **Parsing**: Requires STEPcode's flow-sensitive type narrowing extension
11+
- **Standards**: Contains pattern that needs implicit type narrowing (non-standard)
12+
13+
### `242_mim_lf_treat.exp` (Standards-Compliant)
14+
- **Status**: Corrected version using TREAT expression
15+
- **Size**: 54,756 lines
16+
- **Parsing**: Fully ISO 10303-11 compliant
17+
- **Standards**: Uses explicit TREAT expression for type narrowing
18+
19+
## The Difference
20+
21+
Both files are identical except for a single line at line 34149 in the `styled_item` entity WHERE rule WR3:
22+
23+
**Original (242_mim_lf.exp)**:
24+
```express
25+
QUERY(it <* item | ...)
26+
```
27+
28+
**TREAT version (242_mim_lf_treat.exp)**:
29+
```express
30+
QUERY(it <* TREAT(item AS set_representation_item) | ...)
31+
```
32+
33+
## Why Two Versions?
34+
35+
### Historical Context
36+
The original AP242 schema contains a WHERE rule that:
37+
1. Checks if `item` (a SELECT type) is a `SET_REPRESENTATION_ITEM`
38+
2. Then tries to iterate over `item` in a QUERY expression
39+
40+
This pattern requires implicit type narrowing, which is not part of the ISO 10303-11 standard.
41+
42+
### STEPcode's Solution
43+
STEPcode implements **flow-sensitive type narrowing** as an extension:
44+
- Recognizes TYPEOF guard patterns like `'Type' IN TYPEOF(var)` in AND expressions
45+
- Automatically narrows the type for subsequent uses
46+
- Makes the original schema parse successfully
47+
48+
### Standards-Compliant Alternative
49+
The TREAT version uses the ISO 10303-11 TREAT expression:
50+
- `TREAT(item AS set_representation_item)` explicitly narrows the type
51+
- No implicit narrowing needed
52+
- Works in any compliant EXPRESS parser
53+
54+
## Testing
55+
56+
Both versions parse successfully and produce identical output:
57+
58+
```bash
59+
# Run comparison test
60+
./test/compare_ap242_versions.sh
61+
```
62+
63+
Expected output:
64+
```
65+
✓ Both versions parse successfully
66+
✓ Both versions generate the same number of files
67+
```
68+
69+
For detailed comparison, see `doc/ap242-comparison.md`.
70+
71+
## Which Version to Use?
72+
73+
- **For STEPcode**: Both work equally well
74+
- **For portability**: Use the TREAT version (`242_mim_lf_treat.exp`)
75+
- **For ISO compliance**: Use the TREAT version (`242_mim_lf_treat.exp`)
76+
- **For historical reference**: The original is preserved as `242_mim_lf.exp`
77+
78+
## Related Documentation
79+
80+
- `doc/ap242-comparison.md` - Detailed comparison and analysis
81+
- `doc/express-type-unwrapping.md` - TREAT and narrowing documentation
82+
- `src/express/test/README_AP242_BASELINE.md` - Historical PE056 error info

doc/ap242-comparison.md

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
# AP242 Schema Comparison: Flow-Sensitive Narrowing vs TREAT Expression
2+
3+
## Overview
4+
5+
This document compares two versions of the AP242 schema to demonstrate the difference between STEPcode's non-standard flow-sensitive narrowing extension and the ISO 10303-11 compliant TREAT expression.
6+
7+
## Schema Versions
8+
9+
### 1. Original Version (`242_mim_lf.exp`)
10+
- **Status**: Uses implicit flow-sensitive narrowing (non-standard extension)
11+
- **Location**: `data/ap242/242_mim_lf.exp`
12+
- **Size**: 54,756 lines
13+
- **Problematic code**: Line 34149 in `styled_item` entity, WR3 WHERE rule
14+
15+
### 2. TREAT Version (`242_mim_lf_treat.exp`)
16+
- **Status**: Standards-compliant using TREAT expression
17+
- **Location**: `data/ap242/242_mim_lf_treat.exp`
18+
- **Size**: 54,756 lines
19+
- **Fix applied**: Line 34149 - explicit TREAT expression
20+
21+
## The Problem
22+
23+
In the `styled_item` entity at line ~34148, the WHERE rule WR3 contains:
24+
25+
```express
26+
WR3: ('AP242_MANAGED_MODEL_BASED_3D_ENGINEERING_MIM_LF.MAPPED_ITEM' IN TYPEOF(item)) OR
27+
('AP242_MANAGED_MODEL_BASED_3D_ENGINEERING_MIM_LF.GEOMETRIC_REPRESENTATION_ITEM' IN TYPEOF(item)) OR
28+
(('AP242_MANAGED_MODEL_BASED_3D_ENGINEERING_MIM_LF.SET_REPRESENTATION_ITEM' IN TYPEOF(item)) AND
29+
(SIZEOF(QUERY(it <* item | ...)) = 0));
30+
```
31+
32+
### Issue
33+
- `item` is of type `styled_item_target`, which is a SELECT type
34+
- The SELECT contains `set_representation_item` (which is an aggregate type)
35+
- The code checks `'SET_REPRESENTATION_ITEM' IN TYPEOF(item)` (a TYPEOF guard)
36+
- Then tries to iterate over `item` in a QUERY: `QUERY(it <* item | ...)`
37+
38+
### Why This Was Problematic
39+
According to strict ISO 10303-11 standards, you cannot iterate over a SELECT type directly in a QUERY expression, even if there's a preceding TYPEOF guard. The guard and the QUERY are separate expressions in an AND clause.
40+
41+
## The Solutions
42+
43+
### Solution 1: Flow-Sensitive Narrowing (Non-Standard Extension)
44+
45+
STEPcode implements a **flow-sensitive type narrowing** extension that:
46+
1. Recognizes the pattern `'TypeName' IN TYPEOF(var)` in AND expressions
47+
2. Automatically narrows the type of `var` to `TypeName` for subsequent expressions in the AND
48+
3. Allows `QUERY(it <* item | ...)` to work because `item` is implicitly narrowed to `set_representation_item`
49+
50+
**Code (original, line 34149)**:
51+
```express
52+
QUERY(it <* item | ...)
53+
```
54+
55+
**How it works**:
56+
- The AND expression LHS has the guard: `'SET_REPRESENTATION_ITEM' IN TYPEOF(item)`
57+
- The RHS has the QUERY: `SIZEOF(QUERY(it <* item | ...)) = 0`
58+
- STEPcode's narrowing recognizes the pattern and narrows `item` to `set_representation_item`
59+
- The QUERY can now iterate over the narrowed type
60+
61+
### Solution 2: TREAT Expression (Standards-Compliant)
62+
63+
The ISO 10303-11 standard provides the TREAT expression for explicit type narrowing:
64+
65+
**Code (TREAT version, line 34149)**:
66+
```express
67+
QUERY(it <* TREAT(item AS set_representation_item) | ...)
68+
```
69+
70+
**How it works**:
71+
- `TREAT(item AS set_representation_item)` explicitly narrows the SELECT type to the member type
72+
- The QUERY can iterate over the result of TREAT, which is guaranteed to be `set_representation_item`
73+
- No implicit narrowing needed - the intent is explicit in the code
74+
75+
## The Exact Difference
76+
77+
```diff
78+
--- data/ap242/242_mim_lf.exp
79+
+++ data/ap242/242_mim_lf_treat.exp
80+
@@ -34146,7 +34146,7 @@
81+
('AP242_MANAGED_MODEL_BASED_3D_ENGINEERING_MIM_LF.GEOMETRIC_REPRESENTATION_ITEM' IN TYPEOF(item)) OR
82+
(('AP242_MANAGED_MODEL_BASED_3D_ENGINEERING_MIM_LF.SET_REPRESENTATION_ITEM' IN TYPEOF(item)) AND
83+
(SIZEOF(QUERY(it
84+
- <* item
85+
+ <* TREAT(item AS set_representation_item)
86+
| NOT (('AP242_MANAGED_MODEL_BASED_3D_ENGINEERING_MIM_LF.MAPPED_ITEM' IN TYPEOF(it)) OR
87+
('AP242_MANAGED_MODEL_BASED_3D_ENGINEERING_MIM_LF.GEOMETRIC_REPRESENTATION_ITEM' IN
88+
TYPEOF(it))))) =
89+
```
90+
91+
**Change**: Single line change from `<* item` to `<* TREAT(item AS set_representation_item)`
92+
93+
## Testing Results
94+
95+
### Original Version (with Flow-Sensitive Narrowing)
96+
```bash
97+
./build/bin/exp2cxx data/ap242/242_mim_lf.exp /tmp/ap242_original_test
98+
```
99+
**Result**: ✅ Parses successfully, generates 5,626 C++ files
100+
101+
### TREAT Version (Standards-Compliant)
102+
```bash
103+
./build/bin/exp2cxx data/ap242/242_mim_lf_treat.exp /tmp/ap242_treat_test
104+
```
105+
**Result**: ✅ Parses successfully, generates 5,626 C++ files
106+
107+
### Output Comparison
108+
Both versions generate identical output:
109+
- Same number of entities
110+
- Same entity relationships
111+
- Same multiple inheritance warnings
112+
- Identical "Finished writing files" status
113+
114+
## Verification
115+
116+
To verify both versions produce consistent output:
117+
118+
```bash
119+
# Parse original version
120+
mkdir -p /tmp/ap242_original
121+
./build/bin/exp2cxx data/ap242/242_mim_lf.exp /tmp/ap242_original
122+
123+
# Parse TREAT version
124+
mkdir -p /tmp/ap242_treat
125+
./build/bin/exp2cxx data/ap242/242_mim_lf_treat.exp /tmp/ap242_treat
126+
127+
# Compare generated headers (should be identical except for file references)
128+
diff /tmp/ap242_original/SdaiAP242_MANAGED_MODEL_BASED_3D_ENGINEERING_MIM_LF.h \
129+
/tmp/ap242_treat/SdaiAP242_MANAGED_MODEL_BASED_3D_ENGINEERING_MIM_LF.h
130+
```
131+
132+
## Implications
133+
134+
### For STEPcode Users
135+
- **Current**: Both versions work due to flow-sensitive narrowing extension
136+
- **Future**: TREAT version is more portable if other EXPRESS parsers are used
137+
- **Recommended**: Use TREAT for new schemas to ensure standards compliance
138+
139+
### For STEPcode Development
140+
- Flow-sensitive narrowing is a valuable usability extension
141+
- TREAT expression provides standards-compliant alternative
142+
- Both should continue to be supported for compatibility
143+
144+
### For Standards Compliance
145+
- Original AP242 schema uses pattern that requires extension
146+
- TREAT version is fully ISO 10303-11 compliant
147+
- TREAT makes intent explicit and improves code clarity
148+
149+
## Related Documentation
150+
151+
- `doc/express-type-unwrapping.md` - Full documentation of TREAT and narrowing features
152+
- `src/express/test/README_AP242_BASELINE.md` - Historical PE056 error documentation
153+
- Test schemas:
154+
- `test/unitary_schemas/flow_narrowing_test.exp` - Flow-sensitive narrowing examples
155+
- `test/unitary_schemas/minimal_treat_test.exp` - TREAT expression examples
156+
157+
## Conclusion
158+
159+
This comparison demonstrates:
160+
1. ✅ STEPcode's flow-sensitive narrowing successfully handles the original AP242 schema
161+
2. ✅ TREAT expression provides the standards-compliant alternative
162+
3. ✅ Both approaches produce identical, correct output
163+
4. ✅ The single-line change makes the code more explicit and portable
164+
165+
The availability of both approaches gives users flexibility while maintaining standards compliance when needed.

0 commit comments

Comments
 (0)