Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(engine): Add AttributeWithXML processor type to WasmRuntimeExecutor #681

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

soma00333
Copy link
Contributor

@soma00333 soma00333 commented Dec 7, 2024

Overview

Add AttributeWithXML processor type to WasmRuntimeExecutor to handle TranXLinkChecker Python script.

What I've done

This pull request introduces several changes to the engine/runtime/action-wasm-processor module. The updates include adding support for processing XML attributes, modifying the WasmRuntimeExecutor to handle new processor types, and adding a new example workflow for XML processing.

Processor and Executor Changes:

  • Added new processor type AttributeWithXML to the ProcessorType enum in runtime_executor.rs.
  • Implemented the process_attribute_with_xml method in WasmRuntimeExecutor to handle XML attributes. This method reads XML content, parses it, and updates attributes accordingly.
  • Modified the execute_wasm_module method to accept an additional stdin parameter for XML content and handle it appropriately.

Example and Workflow Changes:

  • Added a new Python script tran_xlink_checker.py to process XML attributes and update them based on specific criteria.
  • Introduced a new example workflow example_tran_xlink_checker_wasm.yml to demonstrate the usage of the AttributeWithXML processor type, including the necessary configuration and nodes.

Dependency and Import Changes:

  • Updated imports in runtime_executor.rs to include necessary modules for handling URIs and XML content.

What I haven't done

  • Add integration tests

How I tested

Test command

export FOLDER_PATH="~/13229_nishitokyo-shi_city_2023_citygml_1_op"
yaml-include runtime/examples/plateau/testdata/workflow/example_tran_xlink_checker_wasm.yml >| sample.yml
cargo run --package reearth-flow-cli -- run --workflow sample.yml --var "cityGmlPath=${FOLDER_PATH}/udx/tran/53394452_tran_6697_op.gml" --var "cityCode='13229'" --var "codelistsPath=${FOLDER_PATH}/codelists" --var "schemasPath=${FOLDER_PATH}/schemas"

Test results

lod null: WasmRuntimeExecutor

2024-12-07T05:37:58.214Z  INFO action: reearth_flow_runner::log_event_handler: "echo with feature = \"{\\\"id\\\":\\\"1e722e5d-94df-4ca2-a888-14d37e801641\\\",\\\"attributes\\\":{\\\"udxDirs\\\":\\\"tran\\\",\\\"extension\\\":\\\"gml\\\",\\\"dirSchemas\\\":\\\"file:///Users/soma.utsumi/Downloads/eukarya/13229_nishitokyo-shi_city_2023_citygml_1_op/schemas\\\",\\\"package\\\":\\\"tran\\\",\\\"path\\\":\\\"file:///Users/soma.utsumi/Downloads/eukarya/13229_nishitokyo-shi_city_2023_citygml_1_op/udx/tran/53394452_tran_6697_op.gml\\\",\\\"dirRoot\\\":\\\"file:///Users/soma.utsumi/Downloads/eukarya/13229_nishitokyo-shi_city_2023_citygml_1_op\\\",\\\"name\\\":\\\"53394452_tran_6697_op.gml\\\",\\\"dirCodelists\\\":\\\"file:///Users/soma.utsumi/Downloads/eukarya/13229_nishitokyo-shi_city_2023_citygml_1_op/codelists\\\",\\\"admin\\\":\\\"\\\",\\\"fileIndex\\\":1,\\\"root\\\":\\\"13229_nishitokyo-shi_city_2023_citygml_1_op\\\",\\\"cityGmlPath\\\":\\\"file:///Users/soma.utsumi/Downloads/eukarya/13229_nishitokyo-shi_city_2023_citygml_1_op/udx/tran/53394452_tran_6697_op.gml\\\",\\\"area\\\":\\\"\\\"},\\\"geometry\\\":{\\\"epsg\\\":null,\\\"value\\\":\\\"none\\\"},\\\"metadata\\\":{\\\"featureId\\\":null,\\\"featureType\\\":null,\\\"lod\\\":null}}\""

lod null: PLATEAU.TranXLinkChecker

2024-12-07T05:36:02.627Z  INFO action{otel.name="EchoSink" otel.kind="Sink Node" workflow.id="ff2593bf-a36c-46c3-88ac-eba42dd8f59a" node.id="30a42dee-474c-4283-b34b-3c54e16bd2b6"}: reearth_flow_runner::log_event_handler: "\"EchoSink\" sink finish. elapsed = 109.437375ms"

lod2: WasmRuntimeExecutor

2024-12-07T05:25:45.701Z  INFO action: reearth_flow_runner::log_event_handler: "echo with feature = \"{\\\"id\\\":\\\"1b4e41f9-15de-44c5-a013-0f5cd71a9962\\\",\\\"attributes\\\":{\\\"cityGmlPath\\\":\\\"file:///Users/soma.utsumi/Downloads/eukarya/13229_nishitokyo-shi_city_2023_citygml_1_op/udx/tran/53394471_tran_6697_op.gml\\\",\\\"fileIndex\\\":1,\\\"package\\\":\\\"tran\\\",\\\"dirSchemas\\\":\\\"file:///Users/soma.utsumi/Downloads/eukarya/13229_nishitokyo-shi_city_2023_citygml_1_op/schemas\\\",\\\"featureType\\\":\\\"Road\\\",\\\"dirCodelists\\\":\\\"file:///Users/soma.utsumi/Downloads/eukarya/13229_nishitokyo-shi_city_2023_citygml_1_op/codelists\\\",\\\"name\\\":\\\"53394471_tran_6697_op.gml\\\",\\\"unreferencedSurfaceNum\\\":0,\\\"admin\\\":\\\"\\\",\\\"area\\\":\\\"\\\",\\\"glmId\\\":\\\"tran_300a8917-7334-4fa4-9297-c2e7fcc0e5c8\\\",\\\"udxDirs\\\":\\\"tran\\\",\\\"root\\\":\\\"13229_nishitokyo-shi_city_2023_citygml_1_op\\\",\\\"unreferencedIds\\\":[],\\\"dirRoot\\\":\\\"file:///Users/soma.utsumi/Downloads/eukarya/13229_nishitokyo-shi_city_2023_citygml_1_op\\\",\\\"extension\\\":\\\"gml\\\",\\\"path\\\":\\\"file:///Users/soma.utsumi/Downloads/eukarya/13229_nishitokyo-shi_city_2023_citygml_1_op/udx/tran/53394471_tran_6697_op.gml\\\",\\\"lod\\\":\\\"2\\\"},\\\"geometry\\\":{\\\"epsg\\\":null,\\\"value\\\":\\\"none\\\"},\\\"metadata\\\":{\\\"featureId\\\":null,\\\"featureType\\\":null,\\\"lod\\\":null}}\""

lod2: PLATEAU.TranXLinkChecker

2024-12-07T05:29:10.376Z  INFO action: reearth_flow_runner::log_event_handler: "echo with feature = \"{\\\"id\\\":\\\"8ec65279-b8a8-4f80-b36d-3fd486b28b1c\\\",\\\"attributes\\\":{\\\"lod\\\":\\\"2\\\",\\\"featureType\\\":\\\"Road\\\",\\\"unreferencedIds\\\":[],\\\"dirCodelists\\\":\\\"file:///Users/soma.utsumi/Downloads/eukarya/13229_nishitokyo-shi_city_2023_citygml_1_op/codelists\\\",\\\"dirRoot\\\":\\\"file:///Users/soma.utsumi/Downloads/eukarya/13229_nishitokyo-shi_city_2023_citygml_1_op\\\",\\\"cityGmlPath\\\":\\\"file:///Users/soma.utsumi/Downloads/eukarya/13229_nishitokyo-shi_city_2023_citygml_1_op/udx/tran/53394471_tran_6697_op.gml\\\",\\\"path\\\":\\\"file:///Users/soma.utsumi/Downloads/eukarya/13229_nishitokyo-shi_city_2023_citygml_1_op/udx/tran/53394471_tran_6697_op.gml\\\",\\\"dirSchemas\\\":\\\"file:///Users/soma.utsumi/Downloads/eukarya/13229_nishitokyo-shi_city_2023_citygml_1_op/schemas\\\",\\\"root\\\":\\\"13229_nishitokyo-shi_city_2023_citygml_1_op\\\",\\\"area\\\":\\\"\\\",\\\"package\\\":\\\"tran\\\",\\\"udxDirs\\\":\\\"tran\\\",\\\"gmlId\\\":\\\"tran_300a8917-7334-4fa4-9297-c2e7fcc0e5c8\\\",\\\"name\\\":\\\"53394471_tran_6697_op.gml\\\",\\\"unreferencedSurfaceNum\\\":0,\\\"admin\\\":\\\"\\\",\\\"fileIndex\\\":1,\\\"extension\\\":\\\"gml\\\"},\\\"geometry\\\":{\\\"epsg\\\":null,\\\"value\\\":\\\"none\\\"},\\\"metadata\\\":{\\\"featureId\\\":null,\\\"featureType\\\":null,\\\"lod\\\":null}}\""

lod3: WasmRuntimeExecutor

2024-12-07T05:46:17.598Z  INFO action: reearth_flow_runner::log_event_handler: "echo with feature = \"{\\\"id\\\":\\\"7d46e899-325a-43bc-9aaf-bddc22d210b9\\\",\\\"attributes\\\":{\\\"name\\\":\\\"53393549_tran_6697_op.gml\\\",\\\"dirRoot\\\":\\\"file:///Users/soma.utsumi/Downloads/eukarya/13103_minato-ku_city_2023_citygml_1_op\\\",\\\"unreferencedSurfaceNum\\\":0,\\\"extension\\\":\\\"gml\\\",\\\"package\\\":\\\"tran\\\",\\\"area\\\":\\\"\\\",\\\"glmId\\\":\\\"tran_cdb59e13-8c37-42c6-98ca-3c58b44e9d4f\\\",\\\"featureType\\\":\\\"Road\\\",\\\"root\\\":\\\"13103_minato-ku_city_2023_citygml_1_op\\\",\\\"lod\\\":\\\"3\\\",\\\"udxDirs\\\":\\\"tran\\\",\\\"admin\\\":\\\"\\\",\\\"path\\\":\\\"file:///Users/soma.utsumi/Downloads/eukarya/13103_minato-ku_city_2023_citygml_1_op/udx/tran/53393549_tran_6697_op.gml\\\",\\\"dirSchemas\\\":\\\"file:///Users/soma.utsumi/Downloads/eukarya/13103_minato-ku_city_2023_citygml_1_op/schemas\\\",\\\"dirCodelists\\\":\\\"file:///Users/soma.utsumi/Downloads/eukarya/13103_minato-ku_city_2023_citygml_1_op/codelists\\\",\\\"cityGmlPath\\\":\\\"file:///Users/soma.utsumi/Downloads/eukarya/13103_minato-ku_city_2023_citygml_1_op/udx/tran/53393549_tran_6697_op.gml\\\",\\\"fileIndex\\\":1,\\\"unreferencedIds\\\":[]},\\\"geometry\\\":{\\\"epsg\\\":null,\\\"value\\\":\\\"none\\\"},\\\"metadata\\\":{\\\"featureId\\\":null,\\\"featureType\\\":null,\\\"lod\\\":null}}\""

lod3: PLATEAU.TranXLinkChecker

2024-12-07T05:44:30.155Z  INFO action: reearth_flow_runner::log_event_handler: "echo with feature = \"{\\\"id\\\":\\\"0ddb032f-1140-45f1-8f07-1aca5292d3b3\\\",\\\"attributes\\\":{\\\"fileIndex\\\":1,\\\"gmlId\\\":\\\"tran_ed388584-3d60-495d-a1df-ad4b7cae8a7f\\\",\\\"featureType\\\":\\\"Road\\\",\\\"dirSchemas\\\":\\\"file:///Users/soma.utsumi/Downloads/eukarya/13103_minato-ku_city_2023_citygml_1_op/schemas\\\",\\\"path\\\":\\\"file:///Users/soma.utsumi/Downloads/eukarya/13103_minato-ku_city_2023_citygml_1_op/udx/tran/53393549_tran_6697_op.gml\\\",\\\"root\\\":\\\"13103_minato-ku_city_2023_citygml_1_op\\\",\\\"name\\\":\\\"53393549_tran_6697_op.gml\\\",\\\"cityGmlPath\\\":\\\"file:///Users/soma.utsumi/Downloads/eukarya/13103_minato-ku_city_2023_citygml_1_op/udx/tran/53393549_tran_6697_op.gml\\\",\\\"package\\\":\\\"tran\\\",\\\"area\\\":\\\"\\\",\\\"dirRoot\\\":\\\"file:///Users/soma.utsumi/Downloads/eukarya/13103_minato-ku_city_2023_citygml_1_op\\\",\\\"lod\\\":\\\"3\\\",\\\"unreferencedSurfaceNum\\\":0,\\\"unreferencedIds\\\":[],\\\"dirCodelists\\\":\\\"file:///Users/soma.utsumi/Downloads/eukarya/13103_minato-ku_city_2023_citygml_1_op/codelists\\\",\\\"udxDirs\\\":\\\"tran\\\",\\\"admin\\\":\\\"\\\",\\\"extension\\\":\\\"gml\\\"},\\\"geometry\\\":{\\\"epsg\\\":null,\\\"value\\\":\\\"none\\\"},\\\"metadata\\\":{\\\"featureId\\\":null,\\\"featureType\\\":null,\\\"lod\\\":null}}\""

Screenshot

Which point I want you to review particularly

Memo

Summary by CodeRabbit

Release Notes

  • New Features

    • Introduced support for processing XML attributes in the WASM processor with the new AttributeWithXML variant.
    • Added a Python script to process transportation-related XML data, enhancing the handling of CityGML features.
    • Created a new YAML workflow configuration for the "ExampleTranXLinkChecker," streamlining the execution of related tasks.
  • Bug Fixes

    • Improved error handling in the XML processing script to ensure robust output in case of parsing issues.
  • Documentation

    • Updated the schema to include the new AttributeWithXML processor type for better clarity on available processing options.

Copy link
Contributor

coderabbitai bot commented Dec 7, 2024

Walkthrough

The changes in this pull request enhance the functionality of the WASM processor by introducing a new enum variant AttributeWithXML to handle XML attributes. The WasmRuntimeExecutor is updated to process this new variant, allowing it to extract and validate XML data, specifically a cityGmlPath attribute. Additionally, a new Python script for processing transportation-related XML data is added, along with a YAML workflow configuration that utilizes these enhancements. The schema for the WasmRuntimeExecutor is also updated to reflect the new processing capabilities.

Changes

File Change Summary
engine/runtime/action-wasm-processor/src/runtime_executor.rs Added AttributeWithXML to ProcessorType enum; updated process and execute_wasm_module methods.
engine/runtime/examples/plateau/testdata/python/tran_xlink_checker.py Introduced tran_xlink_checker.py for processing XML data related to transportation features. Added process_attributes and main functions.
engine/runtime/examples/plateau/testdata/workflow/example_tran_xlink_checker_wasm.yml Created a new YAML workflow for ExampleTranXLinkChecker with defined nodes and parameters.
engine/schema/actions.json Added AttributeWithXML to ProcessorType in the WasmRuntimeExecutor schema.

Possibly related PRs

Suggested labels

worker

Suggested reviewers

  • miseyu

Poem

🐰 In the land of code, we hop and play,
With XML attributes, we pave the way.
A new variant added, to process with glee,
Enhancing our WASM, as bright as can be!
Through workflows we dance, with data in tow,
In the garden of features, watch our changes grow! 🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

netlify bot commented Dec 7, 2024

Deploy Preview for reearth-flow canceled.

Name Link
🔨 Latest commit 70cd75c
🔍 Latest deploy log https://app.netlify.com/sites/reearth-flow/deploys/6753e7bc3422f40008246c0a

@soma00333 soma00333 marked this pull request as ready for review December 7, 2024 06:43
@soma00333 soma00333 requested a review from miseyu December 7, 2024 06:43
@soma00333 soma00333 enabled auto-merge (squash) December 7, 2024 06:44
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Outside diff range and nitpick comments (6)
engine/runtime/examples/plateau/testdata/python/tran_xlink_checker.py (4)

75-77: Improve exception handling for better error reporting

Catching all exceptions with except Exception as e can mask unexpected errors. Consider catching specific exceptions, such as ET.ParseError for XML parsing errors and json.JSONDecodeError for JSON decoding errors. This provides clearer error messages and aids in debugging.


20-33: Refactor duplicated code into a helper function

The code blocks for processing tran:TrafficArea and tran:AuxiliaryTrafficArea are similar. Refactoring them into a helper function can reduce duplication and improve maintainability.

Apply this diff to refactor the code:

+def extract_surface_ids(road, area_type, lod_levels):
+    trf = {lod: [] for lod in lod_levels}
+    for x in road.findall(f'.//tran:{area_type}', XMLNS):
+        for lod in lod_levels:
+            xpath = f'./tran:lod{lod}MultiSurface/gml:MultiSurface/gml:surfaceMember/*[@gml:id]'
+            for p in x.findall(xpath, XMLNS):
+                trf[lod].append(p.get('{http://www.opengis.net/gml}id'))
+    return trf

 def process_attributes(attributes, xml_root):
     for road in xml_root.findall('*//tran:Road', XMLNS):
         gml_id = road.get('{http://www.opengis.net/gml}id')
-        lod2_trf = []
-        lod3_trf = []
-        for x in road.findall('.//tran:TrafficArea', XMLNS):
-            for p in x.findall('./tran:lod2MultiSurface/gml:MultiSurface/gml:surfaceMember/*[@gml:id]', XMLNS):
-                lod2_trf.append(p.get('{http://www.opengis.net/gml}id'))
-            for p in x.findall('./tran:lod3MultiSurface/gml:MultiSurface/gml:surfaceMember/*[@gml:id]', XMLNS):
-                lod3_trf.append(p.get('{http://www.opengis.net/gml}id'))
-        # similar code for AuxiliaryTrafficArea
+        lod_levels = ['2', '3']
+        lod_trf = extract_surface_ids(road, 'TrafficArea', lod_levels)
+        lod_aux = extract_surface_ids(road, 'AuxiliaryTrafficArea', lod_levels)
         # rest of the code using lod_trf and lod_aux

15-15: Add docstrings to functions for better code documentation

Including docstrings in the process_attributes and main functions will improve code readability and help others understand the purpose and usage of these functions.

Also applies to: 64-64


13-15: Remove unnecessary blank lines to enhance readability

There are multiple consecutive blank lines which can be reduced to improve code clarity.

Also applies to: 62-64, 79-81

engine/runtime/action-wasm-processor/src/runtime_executor.rs (2)

254-266: Ensure proper handling of stdin write operations

Writing to stdin_tx and immediately dropping it may cause write errors in some cases. Ensure that all write operations are flushed and handled correctly before dropping the transmitter.

Apply this diff to handle potential write errors:

 stdin_tx.write_all(stdin.as_bytes()).map_err(|e| {
     WasmProcessorError::RuntimeExecutor(format!("Failed to write to stdin: {}", e))
 })?;
+ stdin_tx.flush().map_err(|e| {
+     WasmProcessorError::RuntimeExecutor(format!("Failed to flush stdin: {}", e))
+ })?;
 drop(stdin_tx);

223-231: Provide more informative error messages when accessing storage

Error messages in storage resolution and content retrieval could be more descriptive to aid in debugging. Include context about what operation failed.

Apply this diff to improve error messages:

 let storage = ctx
     .storage_resolver
     .resolve(&uri)
-    .map_err(|e| WasmProcessorError::RuntimeExecutor(format!("{:?}", e)))?;
+    .map_err(|e| WasmProcessorError::RuntimeExecutor(format!("Failed to resolve storage for URI {}: {:?}", uri, e)))?;

 let content = storage
     .get_sync(uri.path().as_path())
-    .map_err(|e| WasmProcessorError::RuntimeExecutor(format!("{:?}", e)))?;
+    .map_err(|e| WasmProcessorError::RuntimeExecutor(format!("Failed to get content from storage at path {}: {:?}", uri.path(), e)))?;
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 7c535a6 and 70cd75c.

⛔ Files ignored due to path filters (1)
  • engine/docs/mdbook/src/action.md is excluded by !**/*.md
📒 Files selected for processing (4)
  • engine/runtime/action-wasm-processor/src/runtime_executor.rs (5 hunks)
  • engine/runtime/examples/plateau/testdata/python/tran_xlink_checker.py (1 hunks)
  • engine/runtime/examples/plateau/testdata/workflow/example_tran_xlink_checker_wasm.yml (1 hunks)
  • engine/schema/actions.json (1 hunks)
🔇 Additional comments (2)
engine/runtime/examples/plateau/testdata/workflow/example_tran_xlink_checker_wasm.yml (1)

6-14: Verify the completeness of the with parameters

The with section includes parameters like cityGmlPath, cityCode, etc., which are currently empty. Ensure that all necessary parameters are provided for the workflow to function correctly.

engine/schema/actions.json (1)

2809-2810: Addition of AttributeWithXML enum value is appropriate

The inclusion of "AttributeWithXML" in the ProcessorType enum enhances the schema to support XML attribute processing as intended.

Comment on lines +69 to +70
arg_input = sys.argv[1] # attributes
parsed_arg_input = json.loads(arg_input)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Check for missing command-line arguments to prevent IndexError

Accessing sys.argv[1] without verifying the number of arguments can lead to an IndexError if no arguments are provided. Add a check to ensure the required arguments are present before accessing them.

Apply this diff to check for command-line arguments:

def main():
    try:
+       if len(sys.argv) < 2:
+           raise ValueError("Attributes input not provided in command-line arguments.")
        stdin_input = sys.stdin.read()  # xml
        xml_root = ET.fromstring(stdin_input)

        arg_input = sys.argv[1]  # attributes
        # rest of the code...

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines +16 to +61
for road in xml_root.findall('*//tran:Road', XMLNS):
gml_id = road.get('{http://www.opengis.net/gml}id')
lod2_trf = []
lod3_trf = []
for x in road.findall('.//tran:TrafficArea', XMLNS):
for p in x.findall('./tran:lod2MultiSurface/gml:MultiSurface/gml:surfaceMember/*[@gml:id]', XMLNS):
lod2_trf.append(p.get('{http://www.opengis.net/gml}id'))
for p in x.findall('./tran:lod3MultiSurface/gml:MultiSurface/gml:surfaceMember/*[@gml:id]', XMLNS):
lod3_trf.append(p.get('{http://www.opengis.net/gml}id'))

lod2_aux = []
lod3_aux = []
for x in road.findall('.//tran:AuxiliaryTrafficArea', XMLNS):
for p in x.findall('./tran:lod2MultiSurface/gml:MultiSurface/gml:surfaceMember/*[@gml:id]', XMLNS):
lod2_aux.append(p.get('{http://www.opengis.net/gml}id'))
for p in x.findall('./tran:lod3MultiSurface/gml:MultiSurface/gml:surfaceMember/*[@gml:id]', XMLNS):
lod3_aux.append(p.get('{http://www.opengis.net/gml}id'))

lod2xlinks = []
lod3xlinks = []
for x in road.findall('./tran:lod2MultiSurface//*[@xlink:href]', XMLNS):
lod2xlinks.append(x.get('{http://www.w3.org/1999/xlink}href')[1:])
for x in road.findall('./tran:lod3MultiSurface//*[@xlink:href]', XMLNS):
lod3xlinks.append(x.get('{http://www.w3.org/1999/xlink}href')[1:])

lod2_unref = set(lod2_trf + lod2_aux) - set(lod2xlinks)
lod3_unref = set(lod3_trf + lod3_aux) - set(lod3xlinks)

if lod2_trf:
attributes.update({
"glmId": {"String": gml_id},
"featureType": {"String": "Road"},
"lod": {"String": "2"},
"unreferencedSurfaceNum": {"Number": len(lod2_unref)},
"unreferencedIds": {"Array": [{"String": str(id)} for id in lod2_unref]},
})

if lod3_trf:
attributes.update({
"glmId": {"String": gml_id},
"featureType": {"String": "Road"},
"lod": {"String": "3"},
"unreferencedSurfaceNum": {"Number": len(lod3_unref)},
"unreferencedIds": {"Array": [{"String": str(id)} for id in lod3_unref]},
})
return attributes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Avoid overwriting attributes when processing multiple roads

In the process_attributes function, the attributes dictionary is being updated inside a loop over roads. This could lead to earlier attribute values being overwritten if multiple tran:Road elements exist in the XML. Consider accumulating the results or processing each road separately to prevent data loss.

Apply this diff to collect attributes for all roads:

def process_attributes(attributes, xml_root):
+    results = []
     for road in xml_root.findall('*//tran:Road', XMLNS):
         gml_id = road.get('{http://www.opengis.net/gml}id')
         lod2_trf = []
         # existing code...
         if lod2_trf:
-            attributes.update({
+            road_attributes = {
                 "glmId": {"String": gml_id},
                 # other attributes...
-            })
+            }
+            results.append(road_attributes)
         # similarly for lod3_trf...
+    attributes['roads'] = results
     return attributes

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines +215 to +218
.get(&Attribute::new("cityGmlPath"))
.ok_or(WasmProcessorError::RuntimeExecutor(
"cityGmlPath key empty".to_string(),
))?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Properly extract string value from AttributeValue

Using city_gml_path.to_string() may not retrieve the actual string value if city_gml_path is an AttributeValue enum. Instead, match on the AttributeValue to extract the string.

Apply this diff to correctly extract the string:

 let city_gml_path_value = match city_gml_path {
     AttributeValue::String(s) => s.clone(),
     _ => {
         return Err(WasmProcessorError::RuntimeExecutor(
             "cityGmlPath attribute is not a string".to_string(),
         ))
     }
 };

 let uri = Uri::from_str(&city_gml_path_value).map_err(|err| {
     WasmProcessorError::RuntimeExecutor(format!("cityGmlPath is not a valid uri: {}", err))
 })?;

Committable suggestion skipped: line range outside the PR's diff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant