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
13 changes: 11 additions & 2 deletions src/vss_tools/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# SPDX-License-Identifier: MPL-2.0
import re
from enum import Enum
from typing import Any, TypeAlias
from typing import Any, Literal, TypeAlias

import jsonschema
from pydantic import (
Expand All @@ -33,7 +33,15 @@
resolve_datatype,
)

EXPORT_EXCLUDE_ATTRIBUTES = ["delete", "instantiate", "fqn", "arraysize", "aggregate", "is_instance"]
EXPORT_EXCLUDE_ATTRIBUTES = [
"delete",
"instantiate",
"fqn",
"arraysize",
"aggregate",
"is_instance",
"instances_relation",
]


class ModelException(Exception):
Expand Down Expand Up @@ -139,6 +147,7 @@ def ensure_description(self) -> Self:

class VSSDataBranch(VSSData):
instances: Any = None
instances_relation: Literal["child", "sibling"] = "child"
aggregate: bool = False
is_instance: bool = False

Expand Down
18 changes: 16 additions & 2 deletions src/vss_tools/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ class NoVSSDataException(Exception):
pass


class InvalidInstanceNodeException(Exception):
pass


class VSSNode(Node): # type: ignore[misc]
"""
Our Anytree node class
Expand Down Expand Up @@ -207,6 +211,10 @@ def expand_instances(self) -> None:
n_instance_nodes += len(instance_nodes)
iterations += 1
for instance_node in instance_nodes:
data = instance_node.get_vss_data()
if not isinstance(data, VSSDataBranch):
raise InvalidInstanceNodeException(instance_node.get_fqn())

log.debug(f"'{instance_node.get_fqn()}', expanding...")
# Copy the reference node for creating instances
instance_node_copy = deepcopy(instance_node)
Expand All @@ -233,6 +241,13 @@ def expand_instances(self) -> None:
# Roots to attach generated nodes
# Initialized with the instance node itself
roots = [instance_node]

if data.instances_relation == "sibling":
if instance_node.parent is None:
instance_node.data.instances = [] # type: ignore
continue
roots = [instance_node.parent]

log.debug(f"Roots: {[r.get_fqn() for r in roots]}")

# We want to keep track of generated nodes
Expand All @@ -241,7 +256,7 @@ def expand_instances(self) -> None:
generated_instance_nodes = []
# On every iteration, we get back new nodes to attach new instances to (roots)
# as well as the nodes that have been generated
for instance in instance_node.data.instances: # type: ignore
for instance in data.instances:
roots, generated = expand_instance(roots, instance_node_copy, instance)
generated_instance_nodes.extend(generated)

Expand Down Expand Up @@ -483,7 +498,6 @@ def expand_instance(
# Instances could be again a list of strings
# We want to harmonize that
# The info however is used to decide what new root points to return
requested_instances = []
if isinstance(instance, list):
requested_instances = instance
else:
Expand Down
35 changes: 35 additions & 0 deletions tests/vspec/test_instances/expected.apigear/A.module.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,38 @@ interfaces:
- name: C
type: int32
description: This description will also exist multiple times.
- name: A_Y
properties:
- name: Count
type: int32
description: d
- name: A_X_B
properties:
- name: Count
type: int32
description: d
- name: A_X_C
properties:
- name: Count
type: int32
description: d
- name: A_X_B1
properties:
- name: Value
type: bool
description: d
- name: A_X_B2
properties:
- name: Value
type: bool
description: d
- name: A_X_C1
properties:
- name: Value
type: bool
description: d
- name: A_X_C2
properties:
- name: Value
type: bool
description: d
23 changes: 23 additions & 0 deletions tests/vspec/test_instances/expected.csv
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,26 @@ A.B.Row2.Left,branch,,,,,,This description will be duplicated.,This comment will
A.B.Row2.Left.C,sensor,int8,,km,,,This description will also exist multiple times.,As well as this comment.,,
A.B.Row2.Right,branch,,,,,,This description will be duplicated.,This comment will be duplicated,,
A.B.Row2.Right.C,sensor,int8,,km,,,This description will also exist multiple times.,As well as this comment.,,
A.Y,branch,,,,,,d,,,
A.Y.Count,attribute,uint8,,,,,d,,,
A.X,branch,,,,,,d,,,
A.X.B,branch,,,,,,d,,,
A.X.B.Count,attribute,uint8,,,,,d,,,
A.X.C,branch,,,,,,d,,,
A.X.C.Count,attribute,uint8,,,,,d,,,
A.X.B1,branch,,,,,,d,,,
A.X.B1.Value,sensor,boolean,,,,,d,,,
A.X.B2,branch,,,,,,d,,,
A.X.B2.Value,sensor,boolean,,,,,d,,,
A.X.C1,branch,,,,,,d,,,
A.X.C1.Value,sensor,boolean,,,,,d,,,
A.X.C2,branch,,,,,,d,,,
A.X.C2.Value,sensor,boolean,,,,,d,,,
A.Y1,branch,,,,,,d,,,
A.Y1.Z,branch,,,,,,d,,,
A.Y1.Z.Z1,branch,,,,,,d,,,
A.Y1.Z.Z2,branch,,,,,,d,,,
A.Y2,branch,,,,,,d,,,
A.Y2.Z,branch,,,,,,d,,,
A.Y2.Z.Z1,branch,,,,,,d,,,
A.Y2.Z.Z2,branch,,,,,,d,,,
106 changes: 106 additions & 0 deletions tests/vspec/test_instances/expected.ddsidl
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,110 @@ octet value;

};

module Y
{
struct Count
{
octet value;
//const string type ="attribute";
//const string description="d";
};
};

module X
{
module B
{
struct Count
{
octet value;
//const string type ="attribute";
//const string description="d";
};
};

module C
{
struct Count
{
octet value;
//const string type ="attribute";
//const string description="d";
};
};

module B1
{
struct Value
{
boolean value;
//const string type ="sensor";
//const string description="d";
};
};

module B2
{
struct Value
{
boolean value;
//const string type ="sensor";
//const string description="d";
};
};

module C1
{
struct Value
{
boolean value;
//const string type ="sensor";
//const string description="d";
};
};

module C2
{
struct Value
{
boolean value;
//const string type ="sensor";
//const string description="d";
};
};

};

module Y1
{
module Z
{
module Z1
{
};

module Z2
{
};

};

};

module Y2
{
module Z
{
module Z1
{
};

module Z2
{
};

};

};

};
99 changes: 99 additions & 0 deletions tests/vspec/test_instances/expected.franca
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,104 @@ const SignalSpec[] signal_spec = [
description: "This description will also exist multiple times.",
datatype: "int8",
unit: "km"
},
{ name: "A.Y",
type: "branch",
description: "d"
},
{ name: "A.Y.Count",
type: "attribute",
description: "d",
datatype: "uint8"
},
{ name: "A.X",
type: "branch",
description: "d"
},
{ name: "A.X.B",
type: "branch",
description: "d"
},
{ name: "A.X.B.Count",
type: "attribute",
description: "d",
datatype: "uint8"
},
{ name: "A.X.C",
type: "branch",
description: "d"
},
{ name: "A.X.C.Count",
type: "attribute",
description: "d",
datatype: "uint8"
},
{ name: "A.X.B1",
type: "branch",
description: "d"
},
{ name: "A.X.B1.Value",
type: "sensor",
description: "d",
datatype: "boolean"
},
{ name: "A.X.B2",
type: "branch",
description: "d"
},
{ name: "A.X.B2.Value",
type: "sensor",
description: "d",
datatype: "boolean"
},
{ name: "A.X.C1",
type: "branch",
description: "d"
},
{ name: "A.X.C1.Value",
type: "sensor",
description: "d",
datatype: "boolean"
},
{ name: "A.X.C2",
type: "branch",
description: "d"
},
{ name: "A.X.C2.Value",
type: "sensor",
description: "d",
datatype: "boolean"
},
{ name: "A.Y1",
type: "branch",
description: "d"
},
{ name: "A.Y1.Z",
type: "branch",
description: "d"
},
{ name: "A.Y1.Z.Z1",
type: "branch",
description: "d"
},
{ name: "A.Y1.Z.Z2",
type: "branch",
description: "d"
},
{ name: "A.Y2",
type: "branch",
description: "d"
},
{ name: "A.Y2.Z",
type: "branch",
description: "d"
},
{ name: "A.Y2.Z.Z1",
type: "branch",
description: "d"
},
{ name: "A.Y2.Z.Z2",
type: "branch",
description: "d"
}
]
Loading
Loading