Skip to content

Commit

Permalink
frontend: Add nodrag class on buttons and add icons
Browse files Browse the repository at this point in the history
wsock: add error tracebacks in debug mode
config: minor refactor of fem_analysis -> fea
  • Loading branch information
Krande committed Oct 10, 2024
1 parent 4209bdc commit 2fbc576
Show file tree
Hide file tree
Showing 21 changed files with 167 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/ada/api/spatial/assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def to_fem(
if isinstance(fem_format, str):
fem_format = FEATypes.from_str(fem_format)

scratch_dir = Config().fem_analysis_scratch_dir if scratch_dir is None else pathlib.Path(scratch_dir)
scratch_dir = Config().fea_scratch_dir if scratch_dir is None else pathlib.Path(scratch_dir)

write_to_fem(
self, name, fem_format, overwrite, fem_converter, scratch_dir, metadata, make_zip_file, model_data_only
Expand Down
5 changes: 4 additions & 1 deletion src/ada/comms/cli_async_ws_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@

async def start_async_server(host="localhost", port=8765, run_in_thread=False, log_level="DEBUG"):
logger.setLevel(log_level)
server = WebSocketAsyncServer(host, port)
is_debug = False
if log_level == "DEBUG":
is_debug = True
server = WebSocketAsyncServer(host, port, debug=is_debug)
if run_in_thread:
await server.run_in_background()
else:
Expand Down
1 change: 1 addition & 0 deletions src/ada/comms/fb_deserializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ def deserialize_value(fb_obj) -> ValueDC | None:
array_value_type=ParameterTypeDC(fb_obj.ArrayValueType()),
array_length=fb_obj.ArrayLength(),
array_type=ArrayTypeDC(fb_obj.ArrayType()),
array_any_length=fb_obj.ArrayAnyLength(),
)


Expand Down
1 change: 1 addition & 0 deletions src/ada/comms/fb_model_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ class ValueDC:
array_value_type: Optional[ParameterTypeDC] = None
array_length: int = None
array_type: Optional[ArrayTypeDC] = None
array_any_length: bool = None


@dataclass
Expand Down
2 changes: 2 additions & 0 deletions src/ada/comms/fb_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ def serialize_value(builder: flatbuffers.Builder, obj: Optional[ValueDC]) -> Opt
Value.AddArrayLength(builder, obj.array_length)
if obj.array_type is not None:
Value.AddArrayType(builder, obj.array_type.value)
if obj.array_any_length is not None:
Value.AddArrayAnyLength(builder, obj.array_any_length)
return Value.End(builder)


Expand Down
4 changes: 4 additions & 0 deletions src/ada/comms/msg_handling/default_on_message.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import traceback
from typing import TYPE_CHECKING

from ada.comms.fb_deserializer import deserialize_root_message
Expand Down Expand Up @@ -43,5 +44,8 @@ def default_on_message(server: WebSocketAsyncServer, client: ConnectedClient, me
on_error_reply(server, client, error_message=f"Unknown command type: {message.command_type}")

except Exception as e:
trace_str = traceback.format_exc()
logger.error(f"Error handling message: {e}")
if server.debug:
logger.error(trace_str)
on_error_reply(server, client, error_message=str(e))
17 changes: 16 additions & 1 deletion src/ada/comms/wsock/Value.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,16 @@ def ArrayType(self):
return self._tab.Get(flatbuffers.number_types.Int8Flags, o + self._tab.Pos)
return 0

# Value
def ArrayAnyLength(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(20))
if o != 0:
return bool(self._tab.Get(flatbuffers.number_types.BoolFlags, o + self._tab.Pos))
return False


def ValueStart(builder):
builder.StartObject(8)
builder.StartObject(9)


def Start(builder):
Expand Down Expand Up @@ -183,6 +190,14 @@ def AddArrayType(builder, arrayType):
ValueAddArrayType(builder, arrayType)


def ValueAddArrayAnyLength(builder, arrayAnyLength):
builder.PrependBoolSlot(8, arrayAnyLength, 0)


def AddArrayAnyLength(builder, arrayAnyLength):
ValueAddArrayAnyLength(builder, arrayAnyLength)


def ValueEnd(builder):
return builder.EndObject()

Expand Down
2 changes: 2 additions & 0 deletions src/ada/comms/wsock_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def __init__(
on_unsent_message: Optional[
Callable[[WebSocketAsyncServer, bytes, ConnectedClient, MessageDC, int], None]
] = retry_message_sending,
debug=False,
):
self.host = host
self.port = port
Expand All @@ -91,6 +92,7 @@ def __init__(
self.instance_id = random.randint(0, 2**31 - 1) # Generates a random int32 value
self.msg_queue = asyncio.Queue()
self.procedure_store = ProcedureStore()
self.debug = debug

async def handle_client(self, websocket: websockets.WebSocketServerProtocol, path: str):
client = await process_client(websocket, path)
Expand Down
2 changes: 1 addition & 1 deletion src/ada/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class Config:
],
),
ConfigSection(
"fem_analysis",
"fea",
[
ConfigEntry("execute_dir", str, None, False),
ConfigEntry(
Expand Down
2 changes: 1 addition & 1 deletion src/ada/fem/formats/mesh_io/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
def meshio_to_fem(assembly: Assembly, name: str, scratch_dir=None, metadata=None, model_data_only=False) -> None:
"""Convert Assembly information to FEM using Meshio"""
if scratch_dir is None:
scratch_dir = Config().fem_analysis_scratch_dir
scratch_dir = Config().fea_scratch_dir

mesh_format = metadata["fem_format"]

Expand Down
19 changes: 10 additions & 9 deletions src/ada/fem/formats/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ def analysis_dir(self):

@property
def execute_dir(self):
if Config().fem_analysis_execute_dir is None:
if Config().fea_execute_dir is None:
return self.analysis_dir
else:
return Config().fem_analysis_execute_dir / self.analysis_name
return Config().fea_execute_dir / self.analysis_name

@property
def analysis_name(self):
Expand Down Expand Up @@ -220,9 +220,10 @@ def get_exe_path(fea_type: FEATypes):
if exe_linux is None and bin_exe_linux.exists():
exe_linux = bin_exe_linux
exe_win = shutil.which(f"{exe_name}.exe")
fea_exe_paths = Config().fea_fem_exe_paths

if Config().fem_analysis_fem_exe_paths.get(exe_name, None) is not None:
exe_path = Config().fem_analysis_fem_exe_paths[exe_name]
if Config().fea_fem_exe_paths.get(exe_name, None) is not None:
exe_path = Config().fea_fem_exe_paths[exe_name]
elif exe_linux:
exe_path = exe_linux
elif exe_win:
Expand Down Expand Up @@ -320,7 +321,7 @@ def _lock_check(analysis_dir):

def folder_prep(scratch_dir, analysis_name, overwrite):
if scratch_dir is None:
scratch_dir = pathlib.Path(Config().fem_analysis_scratch_dir)
scratch_dir = pathlib.Path(Config().fea_scratch_dir)
else:
scratch_dir = pathlib.Path(scratch_dir)

Expand Down Expand Up @@ -359,9 +360,9 @@ def run_windows(exe: LocalExecute, run_cmd, stop_cmd=None, exit_after=True, bat_
with open(exe.execute_dir / stop_bat, "w") as d:
d.write(f"cd /d {exe.analysis_dir}\n{stop_cmd}")

if Config().fem_analysis_execute_dir is not None:
shutil.copy(exe.execute_dir / start_bat, Config().fem_analysis_execute_dir / start_bat)
shutil.copy(exe.execute_dir / stop_bat, Config().fem_analysis_execute_dir / stop_bat)
if Config().fea_execute_dir is not None:
shutil.copy(exe.execute_dir / start_bat, Config().fea_execute_dir / start_bat)
shutil.copy(exe.execute_dir / stop_bat, Config().fea_execute_dir / stop_bat)

# If the script should be running from batch files, then this can be used
if run_in_shell:
Expand Down Expand Up @@ -567,7 +568,7 @@ def default_fem_res_path(
from ada.fem.formats.general import FEATypes

if scratch_dir is None and analysis_dir is None:
scratch_dir = Config().fem_analysis_scratch_dir
scratch_dir = Config().fea_scratch_dir

base_path = scratch_dir / name / name if analysis_dir is None else analysis_dir / name
fem_format_map = {
Expand Down
28 changes: 18 additions & 10 deletions src/ada/procedural_modelling/load_procedures.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,30 @@ def arg_to_param(arg: ast.arg, default: ast.expr, decorator_config: dict) -> Par
array_type = ArrayTypeDC.SET
else:
raise NotImplementedError(f"Parameter type {arg_type} not implemented")
if array_value_type == ParameterTypeDC.FLOAT:
default_values = [ValueDC(float_value=x) for x in default_arg_value]
elif array_value_type == ParameterTypeDC.INTEGER:
default_values = [ValueDC(integer_value=x) for x in default_arg_value]
elif array_value_type == ParameterTypeDC.STRING:
default_values = [ValueDC(string_value=x) for x in default_arg_value]
elif array_value_type == ParameterTypeDC.BOOLEAN:
default_values = [ValueDC(boolean_value=x) for x in default_arg_value]
else:
raise NotImplementedError(f"Parameter type {arg_type} not implemented")

array_is_any_length = False
if len(value_types) > 1 and value_types[1] == "...":
array_is_any_length = True

default_values = None
if default_arg_value is not None:
if array_value_type == ParameterTypeDC.FLOAT:
default_values = [ValueDC(float_value=x) for x in default_arg_value]
elif array_value_type == ParameterTypeDC.INTEGER:
default_values = [ValueDC(integer_value=x) for x in default_arg_value]
elif array_value_type == ParameterTypeDC.STRING:
default_values = [ValueDC(string_value=x) for x in default_arg_value]
elif array_value_type == ParameterTypeDC.BOOLEAN:
default_values = [ValueDC(boolean_value=x) for x in default_arg_value]
else:
raise NotImplementedError(f"Parameter type {arg_type} not implemented")

default_value = ValueDC(
array_value=default_values,
array_length=len(value_types),
array_type=array_type,
array_value_type=array_value_type,
array_any_length=array_is_any_length,
)
else:
raise NotImplementedError(f"Parameter type {arg_type} not implemented")
Expand Down
3 changes: 2 additions & 1 deletion src/flatbuffers/schemas/commands.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ table Value {
boolean_value: bool; // Value when type is BOOLEAN
array_value: [Value]; // Array of values
array_value_type: ParameterType; // Array of numbers (e.g., [1.0, 2.0, 3.0])
array_length: int; // Length of the array
array_length: int; // Length of the array,
array_type: ArrayType; // Type of the array
array_any_length: bool; // Boolean to indicate if the array can have any length
}

// Define a Parameter table that uses a union type for value
Expand Down
26 changes: 23 additions & 3 deletions src/frontend/src/components/node_editor/NodeEditorComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@ const nodeTypes = {
procedure: ProcedureNode,
file_object: CustomFileObjectNode,
};
const update_icon = <svg width="24px" height="24px" viewBox="0 0 15 15" fill="none"
xmlns="http://www.w3.org/2000/svg">
<path
fillRule="evenodd"
clipRule="evenodd"
d="M1.90321 7.29677C1.90321 10.341 4.11041 12.4147 6.58893 12.8439C6.87255 12.893 7.06266 13.1627 7.01355 13.4464C6.96444 13.73 6.69471 13.9201 6.41109 13.871C3.49942 13.3668 0.86084 10.9127 0.86084 7.29677C0.860839 5.76009 1.55996 4.55245 2.37639 3.63377C2.96124 2.97568 3.63034 2.44135 4.16846 2.03202L2.53205 2.03202C2.25591 2.03202 2.03205 1.80816 2.03205 1.53202C2.03205 1.25588 2.25591 1.03202 2.53205 1.03202L5.53205 1.03202C5.80819 1.03202 6.03205 1.25588 6.03205 1.53202L6.03205 4.53202C6.03205 4.80816 5.80819 5.03202 5.53205 5.03202C5.25591 5.03202 5.03205 4.80816 5.03205 4.53202L5.03205 2.68645L5.03054 2.68759L5.03045 2.68766L5.03044 2.68767L5.03043 2.68767C4.45896 3.11868 3.76059 3.64538 3.15554 4.3262C2.44102 5.13021 1.90321 6.10154 1.90321 7.29677ZM13.0109 7.70321C13.0109 4.69115 10.8505 2.6296 8.40384 2.17029C8.12093 2.11718 7.93465 1.84479 7.98776 1.56188C8.04087 1.27898 8.31326 1.0927 8.59616 1.14581C11.4704 1.68541 14.0532 4.12605 14.0532 7.70321C14.0532 9.23988 13.3541 10.4475 12.5377 11.3662C11.9528 12.0243 11.2837 12.5586 10.7456 12.968L12.3821 12.968C12.6582 12.968 12.8821 13.1918 12.8821 13.468C12.8821 13.7441 12.6582 13.968 12.3821 13.968L9.38205 13.968C9.10591 13.968 8.88205 13.7441 8.88205 13.468L8.88205 10.468C8.88205 10.1918 9.10591 9.96796 9.38205 9.96796C9.65819 9.96796 9.88205 10.1918 9.88205 10.468L9.88205 12.3135L9.88362 12.3123C10.4551 11.8813 11.1535 11.3546 11.7585 10.6738C12.4731 9.86976 13.0109 8.89844 13.0109 7.70321Z"
fill="#ffffff"
/>
</svg>
const popout_icon = <svg fill="#ffffff" width="24px" height="24px" viewBox="0 0 36 36" version="1.1"
preserveAspectRatio="xMidYMid meet" xmlns="http://www.w3.org/2000/svg">
<title>pop-out-line</title>
<path className="clr-i-outline clr-i-outline-path-1"
d="M27,33H5a2,2,0,0,1-2-2V9A2,2,0,0,1,5,7H15V9H5V31H27V21h2V31A2,2,0,0,1,27,33Z"></path>
<path className="clr-i-outline clr-i-outline-path-2"
d="M18,3a1,1,0,0,0,0,2H29.59L15.74,18.85a1,1,0,1,0,1.41,1.41L31,6.41V18a1,1,0,0,0,2,0V3Z"></path>
<rect x="0" y="0" width="36" height="36" fillOpacity="0"/>
</svg>

const NodeEditorComponent: React.FC = () => {
// Access Zustand state and actions using hooks
Expand Down Expand Up @@ -40,13 +58,15 @@ const NodeEditorComponent: React.FC = () => {
{/* Header Area */}
<div className="node-editor-header node-editor-drag-handle bg-gray-800 text-white px-4 py-2 cursor-move">
<div className={"flex flex-row"}>
<div className={"flex"}>Node Editor</div>
<div className={"flex p-1"}>Node Editor</div>
<button
className={"flex relative bg-blue-700 hover:bg-blue-700/50 text-white px-4 ml-2 rounded"}
className={"flex relative bg-blue-700 hover:bg-blue-700/50 text-white p-1 ml-2 rounded"}
onClick={() => request_list_of_nodes()}
>
Update
{update_icon}
</button>
<button
className={"flex relative bg-blue-700 hover:bg-blue-700/50 text-white p-1 ml-2 rounded"}>{popout_icon}</button>
</div>
</div>
{/* Content Area */}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function CustomFileObjectNode(props: { id: string, data: Record<string, string |

<div className={"flex justify-center items-center text-xs p-1"}>
<button
className="bg-blue-500 hover:bg-blue-700 text-white p-1 rounded"
className="nodrag bg-blue-500 hover:bg-blue-700 text-white p-1 rounded"
onClick={() => {
view_file_object_from_server(props.data.fileobject as FileObject);
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,29 +70,54 @@ export function ParameterItem({
</div>
);
case ParameterType.ARRAY:
const values = [];
const arrayValueType = paramDefaultValue?.arrayValueType();
for (let i = 0; i < paramDefaultValue?.arrayLength(); i++) {
const value = paramDefaultValue.arrayValue(i);
if (value) {
values.push(
arrayValueType === ParameterType.FLOAT ? value.floatValue() : value.stringValue()
);
if (param.defaultValue()?.arrayAnyLength() === false) {
const values = [];
for (let i = 0; i < paramDefaultValue?.arrayLength(); i++) {
const value = paramDefaultValue.arrayValue(i);
if (value) {
values.push(
arrayValueType === ParameterType.FLOAT ? value.floatValue() : value.stringValue()
);
}
}
}
return (
<div className="flex flex-row items-center w-24 ml-1" id={paramId}>
{values.slice(0, 3).map((val, idx) => (
return (
<div className="flex flex-row items-center w-24 ml-1" id={paramId}>
{values.slice(0, 3).map((val, idx) => (
<input
key={idx}
type="number"
defaultValue={typeof val === 'number' ? val.toFixed(3) : ''}
className="nodrag flex m-auto text-gray-800 border text-xs border-gray-400 rounded max-w-8"
/>
))}
<span className="flex flex-1 pl-2 text-xs text-gray-300">{paramName}</span>
</div>
);
} else {
let placeholder = '';
if (arrayValueType === ParameterType.FLOAT)
placeholder = '1.0;2.0;3.0';
else if (arrayValueType === ParameterType.INTEGER)
placeholder = '1;2;3';
else {
console.error(`Unknown array value type: ${arrayValueType}`);
}

return (
<div className="flex flex-row items-center ml-1" id={paramId}>
<input
key={idx}
type="number"
defaultValue={typeof val === 'number' ? val.toFixed(3) : ''}
className="nodrag flex m-auto text-gray-800 border text-xs border-gray-400 rounded max-w-8"
type="text"
pattern="[0-9]+(\.[0-9]+)?(;\s*[0-9]+(\.[0-9]+)?)*"
defaultValue={paramDefaultValue?.stringValue()}
placeholder={placeholder}
className="nodrag m-auto ml-0 mr-0 text-gray-800 text-xs w-24 border border-gray-400 rounded"
/>
))}
<span className="flex flex-1 pl-2 text-xs text-gray-300">{paramName}</span>
</div>
);
<span className="pl-2 text-xs text-gray-300">{paramName}</span>
</div>
);
}

default:
console.error(`Unknown parameter type: ${param.type()}`);
return null;
Expand Down
Loading

0 comments on commit 2fbc576

Please sign in to comment.