Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/build-*
/.build


# Temporary files
*~
\#*\#
Expand All @@ -21,3 +20,6 @@
# Tool specific patterns
*.kdev4
.kdev_include_paths

# Parser generator intermediate files
*.out
54,756 changes: 54,756 additions & 0 deletions data/ap242/242_mim_lf.exp

Large diffs are not rendered by default.

35,076 changes: 0 additions & 35,076 deletions data/ap242/242_n8324_mim_lf.exp

This file was deleted.

54,756 changes: 54,756 additions & 0 deletions data/ap242treat/242_mim_lf_treat.exp

Large diffs are not rendered by default.

62 changes: 62 additions & 0 deletions data/ap242treat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# AP242 TREAT Schema File

This directory contains an altered version of the AP242 (ISO 10303-242) schema.

It is corrected to avoid the non-standard parsing requirement of the original
by using explicit TREAT expression for type narrowing

## The Difference

Both files are identical except for a single line at line 34149 in the `styled_item` entity WHERE rule WR3:

**Original (242_mim_lf.exp)**:
```express
QUERY(it <* item | ...)
```

**TREAT version (242_mim_lf_treat.exp)**:
```express
QUERY(it <* TREAT(item AS set_representation_item) | ...)
```

### Historical Context
The original AP242 schema contains a WHERE rule that:
1. Checks if `item` (a SELECT type) is a `SET_REPRESENTATION_ITEM`
2. Then tries to iterate over `item` in a QUERY expression

This pattern requires implicit type narrowing, which is not part of the ISO 10303-11 standard.

### STEPcode's Solution
STEPcode implements **flow-sensitive type narrowing** as an extension:
- Recognizes TYPEOF guard patterns like `'Type' IN TYPEOF(var)` in AND expressions
- Automatically narrows the type for subsequent uses
- Makes the original schema parse successfully

### Standards-Compliant Alternative
The TREAT version uses the ISO 10303-11 TREAT expression:
- `TREAT(item AS set_representation_item)` explicitly narrows the type
- No implicit narrowing needed
- Works in any compliant EXPRESS parser

## Testing

Both versions parse successfully and produce identical output:

```bash
# Run comparison test
./test/compare_ap242_versions.sh
```

Expected output:
```
✓ Both versions parse successfully
✓ Both versions generate the same number of files
```

For detailed comparison, see `doc/ap242-comparison.md`.

## Related Documentation

- `doc/ap242-comparison.md` - Detailed comparison and analysis
- `doc/express-type-unwrapping.md` - TREAT and narrowing documentation
- `src/express/test/README_AP242_BASELINE.md` - Historical PE056 error info
165 changes: 165 additions & 0 deletions doc/ap242-comparison.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
# AP242 Schema Comparison: Flow-Sensitive Narrowing vs TREAT Expression

## Overview

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.

## Schema Versions

### 1. Original Version (`242_mim_lf.exp`)
- **Status**: Uses implicit flow-sensitive narrowing (non-standard extension)
- **Location**: `data/ap242/242_mim_lf.exp`
- **Size**: 54,756 lines
- **Problematic code**: Line 34149 in `styled_item` entity, WR3 WHERE rule

### 2. TREAT Version (`242_mim_lf_treat.exp`)
- **Status**: Standards-compliant using TREAT expression
- **Location**: `data/ap242/242_mim_lf_treat.exp`
- **Size**: 54,756 lines
- **Fix applied**: Line 34149 - explicit TREAT expression

## The Problem

In the `styled_item` entity at line ~34148, the WHERE rule WR3 contains:

```express
WR3: ('AP242_MANAGED_MODEL_BASED_3D_ENGINEERING_MIM_LF.MAPPED_ITEM' IN TYPEOF(item)) OR
('AP242_MANAGED_MODEL_BASED_3D_ENGINEERING_MIM_LF.GEOMETRIC_REPRESENTATION_ITEM' IN TYPEOF(item)) OR
(('AP242_MANAGED_MODEL_BASED_3D_ENGINEERING_MIM_LF.SET_REPRESENTATION_ITEM' IN TYPEOF(item)) AND
(SIZEOF(QUERY(it <* item | ...)) = 0));
```

### Issue
- `item` is of type `styled_item_target`, which is a SELECT type
- The SELECT contains `set_representation_item` (which is an aggregate type)
- The code checks `'SET_REPRESENTATION_ITEM' IN TYPEOF(item)` (a TYPEOF guard)
- Then tries to iterate over `item` in a QUERY: `QUERY(it <* item | ...)`

### Why This Was Problematic
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.

## The Solutions

### Solution 1: Flow-Sensitive Narrowing (Non-Standard Extension)

STEPcode implements a **flow-sensitive type narrowing** extension that:
1. Recognizes the pattern `'TypeName' IN TYPEOF(var)` in AND expressions
2. Automatically narrows the type of `var` to `TypeName` for subsequent expressions in the AND
3. Allows `QUERY(it <* item | ...)` to work because `item` is implicitly narrowed to `set_representation_item`

**Code (original, line 34149)**:
```express
QUERY(it <* item | ...)
```

**How it works**:
- The AND expression LHS has the guard: `'SET_REPRESENTATION_ITEM' IN TYPEOF(item)`
- The RHS has the QUERY: `SIZEOF(QUERY(it <* item | ...)) = 0`
- STEPcode's narrowing recognizes the pattern and narrows `item` to `set_representation_item`
- The QUERY can now iterate over the narrowed type

### Solution 2: TREAT Expression (Standards-Compliant)

The ISO 10303-11 standard provides the TREAT expression for explicit type narrowing:

**Code (TREAT version, line 34149)**:
```express
QUERY(it <* TREAT(item AS set_representation_item) | ...)
```

**How it works**:
- `TREAT(item AS set_representation_item)` explicitly narrows the SELECT type to the member type
- The QUERY can iterate over the result of TREAT, which is guaranteed to be `set_representation_item`
- No implicit narrowing needed - the intent is explicit in the code

## The Exact Difference

```diff
--- data/ap242/242_mim_lf.exp
+++ data/ap242/242_mim_lf_treat.exp
@@ -34146,7 +34146,7 @@
('AP242_MANAGED_MODEL_BASED_3D_ENGINEERING_MIM_LF.GEOMETRIC_REPRESENTATION_ITEM' IN TYPEOF(item)) OR
(('AP242_MANAGED_MODEL_BASED_3D_ENGINEERING_MIM_LF.SET_REPRESENTATION_ITEM' IN TYPEOF(item)) AND
(SIZEOF(QUERY(it
- <* item
+ <* TREAT(item AS set_representation_item)
| NOT (('AP242_MANAGED_MODEL_BASED_3D_ENGINEERING_MIM_LF.MAPPED_ITEM' IN TYPEOF(it)) OR
('AP242_MANAGED_MODEL_BASED_3D_ENGINEERING_MIM_LF.GEOMETRIC_REPRESENTATION_ITEM' IN
TYPEOF(it))))) =
```

**Change**: Single line change from `<* item` to `<* TREAT(item AS set_representation_item)`

## Testing Results

### Original Version (with Flow-Sensitive Narrowing)
```bash
./build/bin/exp2cxx data/ap242/242_mim_lf.exp /tmp/ap242_original_test
```
**Result**: ✅ Parses successfully, generates 5,626 C++ files

### TREAT Version (Standards-Compliant)
```bash
./build/bin/exp2cxx data/ap242/242_mim_lf_treat.exp /tmp/ap242_treat_test
```
**Result**: ✅ Parses successfully, generates 5,626 C++ files

### Output Comparison
Both versions generate identical output:
- Same number of entities
- Same entity relationships
- Same multiple inheritance warnings
- Identical "Finished writing files" status

## Verification

To verify both versions produce consistent output:

```bash
# Parse original version
mkdir -p /tmp/ap242_original
./build/bin/exp2cxx data/ap242/242_mim_lf.exp /tmp/ap242_original

# Parse TREAT version
mkdir -p /tmp/ap242_treat
./build/bin/exp2cxx data/ap242/242_mim_lf_treat.exp /tmp/ap242_treat

# Compare generated headers (should be identical except for file references)
diff /tmp/ap242_original/SdaiAP242_MANAGED_MODEL_BASED_3D_ENGINEERING_MIM_LF.h \
/tmp/ap242_treat/SdaiAP242_MANAGED_MODEL_BASED_3D_ENGINEERING_MIM_LF.h
```

## Implications

### For STEPcode Users
- **Current**: Both versions work due to flow-sensitive narrowing extension
- **Future**: TREAT version is more portable if other EXPRESS parsers are used
- **Recommended**: Use TREAT for new schemas to ensure standards compliance

### For STEPcode Development
- Flow-sensitive narrowing is a valuable usability extension
- TREAT expression provides standards-compliant alternative
- Both should continue to be supported for compatibility

### For Standards Compliance
- Original AP242 schema uses pattern that requires extension
- TREAT version is fully ISO 10303-11 compliant
- TREAT makes intent explicit and improves code clarity

## Related Documentation

- `doc/express-type-unwrapping.md` - Full documentation of TREAT and narrowing features
- `src/express/test/README_AP242_BASELINE.md` - Historical PE056 error documentation
- Test schemas:
- `test/unitary_schemas/flow_narrowing_test.exp` - Flow-sensitive narrowing examples
- `test/unitary_schemas/minimal_treat_test.exp` - TREAT expression examples

## Conclusion

This comparison demonstrates:
1. ✅ STEPcode's flow-sensitive narrowing successfully handles the original AP242 schema
2. ✅ TREAT expression provides the standards-compliant alternative
3. ✅ Both approaches produce identical, correct output
4. ✅ The single-line change makes the code more explicit and portable

The availability of both approaches gives users flexibility while maintaining standards compliance when needed.
Loading
Loading