-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathadvanced_local_ids.py
More file actions
194 lines (153 loc) · 6.87 KB
/
Copy pathadvanced_local_ids.py
File metadata and controls
194 lines (153 loc) · 6.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env python3
"""Advanced Local ID Operations - Vista SDK Python.
This example demonstrates advanced Local ID operations:
- Building complex Local IDs with multiple components
- Error handling during parsing
- Working with custom tags
- Local ID validation and inspection
"""
from vista_sdk.codebook_names import CodebookName
from vista_sdk.gmod_path import GmodPath
from vista_sdk.local_id_builder import LocalIdBuilder
from vista_sdk.local_id_builder_parsing import LocalIdBuilderParsing
from vista_sdk.vis import VIS
from vista_sdk.vis_version import VisVersion
def main() -> None: # noqa : C901
"""Demonstrate advanced Local ID operations."""
print("=== Advanced Local ID Operations ===\n")
# Initialize VIS
vis = VIS()
version = VisVersion.v3_4a
codebooks = vis.get_codebooks(version)
# 1. Building complex Local IDs
print("1. Building Complex Local IDs...")
# Primary and secondary items
primary_path = GmodPath.parse("411.1/C101.31-2", arg=version)
secondary_path = GmodPath.parse("411.1/C101.63/S206", arg=version)
complex_local_id = (
LocalIdBuilder.create(version)
.with_primary_item(primary_path)
.with_secondary_item(secondary_path)
.with_metadata_tag(codebooks.create_tag(CodebookName.Quantity, "temperature"))
.with_metadata_tag(codebooks.create_tag(CodebookName.Content, "exhaust.gas"))
.with_metadata_tag(codebooks.create_tag(CodebookName.State, "high"))
.with_metadata_tag(codebooks.create_tag(CodebookName.Position, "inlet"))
.build()
)
print(f" Complex Local ID: {complex_local_id}")
print(f" Has secondary item: {complex_local_id.secondary_item is not None}")
print(f" Number of metadata tags: {len(complex_local_id.metadata_tags)}")
# 2. Working with custom tags
print("\n2. Working with Custom Tags...")
# Create custom tags (prefix with ~)
custom_quantity = codebooks.try_create_tag(
CodebookName.Quantity, "custom_temperature"
)
custom_position = codebooks.try_create_tag(CodebookName.Position, "custom_location")
if custom_quantity and custom_position:
custom_local_id = (
LocalIdBuilder.create(version)
.with_primary_item(primary_path)
.with_metadata_tag(custom_quantity)
.with_metadata_tag(custom_position)
.build()
)
print(f" Custom Local ID: {custom_local_id}")
print(f" Quantity tag is custom: {custom_local_id.quantity.is_custom}") # type: ignore
print(f" Position tag is custom: {custom_local_id.position.is_custom}") # type: ignore
# 3. Error handling and validation
print("\n3. Error Handling and Validation...")
invalid_local_ids = [
"/dnv-v2/vis-3-4a/invalid-path/meta/qty-temperature",
"/invalid-naming-rule/vis-3-4a/411.1/meta/qty-temperature",
"/dnv-v2/vis-3-4a/411.1/meta/qty-invalid_quantity",
# Invalid position order
"/dnv-v2/vis-3-4a/411.1/meta/qty-temperature/pos-1-centre",
]
for invalid_id in invalid_local_ids:
print(f"\n Testing invalid Local ID: {invalid_id}")
try:
success, errors, result = LocalIdBuilder.try_parse(invalid_id)
if not success or result is None:
print(" ✗ Parsing failed as expected")
if errors.has_errors:
print(" Errors found:")
for error_type, message in errors:
print(f" - {error_type}: {message}")
else:
print(f" ⚠ Unexpectedly parsed successfully: {result}")
except Exception as e:
print(f" ✗ Exception during parsing: {e}")
# 4. Local ID inspection and analysis
print("\n4. Local ID Inspection and Analysis...")
valid_local_ids = [
"/dnv-v2/vis-3-4a/411.1/C101.31-2/meta/qty-temperature/cnt-exhaust.gas/pos-inlet",
"/dnv-v2/vis-3-4a/652.31/S90.3/S61/sec/652.1i-1P/meta/cnt-sea.water/state-opened",
"/dnv-v2/vis-3-4a/1021.1i-6P/H123/meta/qty-volume/cnt-cargo/pos~percentage",
]
for local_id_str in valid_local_ids:
print(f"\n Analyzing: {local_id_str}")
try:
parser = LocalIdBuilderParsing()
local_id = parser.parse(local_id_str).build()
print(" ✓ Successfully parsed and built")
print(f" ✓ VIS Version: {local_id.vis_version}")
print(f" ✓ Has custom tags: {local_id.has_custom_tag}")
print(f" ✓ Primary item: {local_id.primary_item}")
if local_id.secondary_item:
print(f" ✓ Secondary item: {local_id.secondary_item}")
# Analyze each metadata tag
print(" ✓ Metadata tags:")
for tag in local_id.metadata_tags:
print(
f" - {tag.name.name}: {tag.value} (custom: {tag.is_custom})"
)
except Exception as e:
print(f" ✗ Failed to parse: {e}")
# 5. Builder pattern variations
print("\n5. Builder Pattern Variations...")
# Using try_with methods for safe building
builder = LocalIdBuilder.create(version)
# Try adding components safely
success, path_for_builder = GmodPath.try_parse("411.1/C101.31", version)
if success and path_for_builder:
builder = builder.with_primary_item(path_for_builder)
# Try with valid tags
temp_tag = codebooks.try_create_tag(CodebookName.Quantity, "temperature")
if temp_tag:
builder = builder.with_metadata_tag(temp_tag)
exhaust_tag = codebooks.try_create_tag(CodebookName.Content, "exhaust.gas")
if exhaust_tag:
builder = builder.with_metadata_tag(exhaust_tag)
# Try with potentially invalid values (this should fail)
invalid_tag = codebooks.try_create_tag(
CodebookName.Position, "invalid-position-format"
)
if invalid_tag:
builder = builder.with_metadata_tag(invalid_tag)
try:
safe_local_id = builder.build()
print(f" Safe building result: {safe_local_id}")
except Exception as e:
print(f" Safe building failed: {e}")
# 6. Verbose mode demonstration
print("\n6. Verbose Mode...")
verbose_local_id = (
LocalIdBuilder.create(version)
.with_verbose_mode(True)
.with_primary_item(primary_path)
.with_metadata_tag(codebooks.create_tag(CodebookName.Quantity, "temperature"))
.build()
)
regular_local_id = (
LocalIdBuilder.create(version)
.with_verbose_mode(False)
.with_primary_item(primary_path)
.with_metadata_tag(codebooks.create_tag(CodebookName.Quantity, "temperature"))
.build()
)
print(f" Verbose mode: {verbose_local_id}")
print(f" Regular mode: {regular_local_id}")
print("\n=== Advanced operations completed! ===")
if __name__ == "__main__":
main()