Skip to content

Commit

Permalink
some tweaking
Browse files Browse the repository at this point in the history
  • Loading branch information
dromer committed Nov 10, 2024
1 parent 63b2b76 commit 956daea
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 41 deletions.
58 changes: 30 additions & 28 deletions hvcc/interpreters/pd2hv/HeavyGraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
import os
from typing import Optional, List

from .types import HvGraph
from .types import HvGraph, HvArgs
from .PdObject import PdObject
from .HeavyObject import HeavyObject
# from .HeavyObject import HeavyObject


class HeavyGraph(PdObject):
Expand All @@ -38,39 +38,41 @@ def __init__(
self.hv_json = HvGraph(**json.load(f))

# parse the heavy data structure to determine the outlet connection type
outlets = [o for o in self.hv_json.objects.values() if o.type == "outlet"]
sorted(outlets, key=lambda o: o.args.index)
self.__outlet_connection_types = [o.args.type for o in outlets]
if self.hv_json.objects:
outlets = [o for o in self.hv_json.objects.values() if o.type == "outlet"]
sorted(outlets, key=lambda o: o.args.index)
self.__outlet_connection_types = [o.args.type for o in outlets]
else:
self.__outlet_connection_types = []

# resolve the arguments
for i, a in enumerate(self.hv_json.args):
if i < len(self.obj_args):
arg_value = self.obj_args[i]
elif a.required:
self.add_error(f"Required argument \"{a.name}\" not found.")
continue
else:
arg_value = a.default
# # resolve the arguments
# for i, a in enumerate(self.hv_json.args):
# if i < len(self.obj_args):
# arg_value = self.obj_args[i]
# elif a.required:
# self.add_error(f"Required argument \"{a.name}\" not found.")
# continue
# else:
# arg_value = a.default

try:
arg_value = HeavyObject.force_arg_type(arg_value, a.value_type)
except Exception as e:
self.add_error(
f"Heavy {self.obj_type} cannot convert argument \"{a.name}\""
f" with value \"{arg_value}\" to type {a.value_type}: {e}")
# try:
# arg_value = HeavyObject.force_arg_type(arg_value, a.value_type)
# except Exception as e:
# self.add_error(
# f"Heavy {self.obj_type} cannot convert argument \"{a.name}\""
# f" with value \"{arg_value}\" to type {a.value_type}: {e}")

# resolve all arguments for each object in the graph
for o in self.hv_json.objects.values():
for k, v in o.args:
# TODO(mhroth): make resolution more robust
# if v == f"${a.name}":
# o.args[k] = arg_value
pass
# # resolve all arguments for each object in the graph
# for o in self.hv_json.objects.values():
# for k, v in o.args:
# # TODO(mhroth): make resolution more robust
# if v == f"${a.name}":
# o.args[k] = arg_value

# reset all arguments, as they have all been resolved
# any required arguments would break hv2ir as they will no longer
# be supplied (because they are resolved)
self.hv_json.args = []
self.hv_json.args = HvArgs()

def get_outlet_connection_type(self, outlet_index: int) -> str:
return self.__outlet_connection_types[outlet_index]
Expand Down
2 changes: 1 addition & 1 deletion hvcc/interpreters/pd2hv/PdGraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def to_hv(self, export_args: bool = False) -> HvGraph:
hvgraph = {
"type": "graph",
"imports": [],
"args": self.hv_args if export_args else [],
"args": self.hv_args if export_args else {},
"objects": {o.obj_id: o.to_hv() for o in self.__objs},
"connections": [c.to_hv() for c in self.__connections],
"properties": {
Expand Down
23 changes: 12 additions & 11 deletions hvcc/interpreters/pd2hv/types/HvGraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ class HvProperties(BaseModel):

class HvArgs(BaseModel):
extern: Optional[ExternType] = None
index: int
index: int = 0
name: Optional[str] = None
priority: Optional[int] = None
size: Optional[int] = None
values: Optional[List] = None
type: str
required: Optional[bool]
default: Optional[List]
value_type: Optional[str]
type: str = ""
required: Optional[bool] = None
default: Optional[List] = None
value_type: Optional[str] = None
length: Optional[int] = None


class HvObject(BaseModel):
Expand All @@ -50,13 +51,13 @@ class HvObject(BaseModel):

class HvGraph(BaseModel):
type: str
args: List[HvArgs]
connections: List[Connection]
imports: List[str]
objects: Dict[str, HvObject]
route_Ymaxs: HvObject
args: HvArgs
connections: Optional[List[Connection]] = []
imports: Optional[List[str]] = []
objects: Optional[Dict[str, HvObject]] = {}
route_Ymaxs: Optional[HvObject] = None
properties: HvProperties
annotations: Optional[Dict[str, str]]
annotations: Optional[Dict[str, str]] = None


# if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion hvcc/interpreters/pd2hv/types/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .HvGraph import HvGraph, HvObject # noqa
from .HvGraph import HvGraph, HvObject, HvArgs # noqa

0 comments on commit 956daea

Please sign in to comment.