brighter-trading/markdown/StratWorkspaceManager.md

5.3 KiB

StratWorkspaceManager:

@startuml
start
:Initialize StratWorkspaceManager;
note right
- workspace = null
- blocksDefined = false
- MAX_TOP_LEVEL_BLOCKS = 10
end note

if (blocklyDiv exists?) then (yes)
    if (workspace exists?) then (yes)
        :Dispose of existing workspace;
    endif

    :Load Modules and Init Workspace;
    note right
    Calls _loadModulesAndInitWorkspace()
    end note

    :Initialize Blockly Workspace;
    note right
    - Set blocksDefined = true
    - Inject workspace into blocklyDiv
    end note
else (no)
    :Log error "blocklyDiv is not loaded.";
endif

:Adjust Workspace;
note right
Calls adjustWorkspace()
end note

stop
@enduml

initWorkspace:

@startuml
participant "StratWorkspaceManager" as SWM
participant "Document" as Doc
participant "Blockly"

SWM -> Doc: getElementById('blocklyDiv')
alt blocklyDiv exists
    SWM -> SWM: workspace.dispose() [if workspace exists]
    SWM -> SWM: _loadModulesAndInitWorkspace()
    SWM -> SWM: blocksDefined = true
    SWM -> Blockly: inject('blocklyDiv', options)
else
    SWM -> SWM: console.error("blocklyDiv is not loaded.")
end
@enduml

_loadModulesAndInitWorkspace:

@startuml
participant "StratWorkspaceManager" as SWM
participant "Document" as Doc
participant "Blockly"
@startuml
participant "StratWorkspaceManager" as SWM
participant "Custom Blocks Modules" as Modules
participant "Document" as Doc
participant "Blockly"

SWM -> SWM: Check blocksDefined
alt blocksDefined is false
    SWM -> Modules: Import and define modules
    SWM -> SWM: blocksDefined = true
else
    SWM -> SWM: Skip module loading
end

SWM -> Doc: getElementById('toolbox_advanced')
alt toolbox exists
    SWM -> Blockly: inject('blocklyDiv', options)
    SWM -> SWM: Workspace initialized
else
    SWM -> SWM: console.error("toolbox is not loaded.")
end
@enduml

SWM -> Doc: getElementById('blocklyDiv')
alt blocklyDiv exists
    SWM -> SWM: workspace.dispose() [if workspace exists]
    SWM -> SWM: _loadModulesAndInitWorkspace()
    SWM -> SWM: blocksDefined = true
    SWM -> Blockly: inject('blocklyDiv', options)
else
    SWM -> SWM: console.error("blocklyDiv is not loaded.")
end
@enduml

adjustWorkspace:

@startuml
participant "StratWorkspaceManager" as SWM
participant "Document" as Doc
participant "Blockly"

SWM -> Doc: getElementById('blocklyDiv')
alt blocklyDiv and workspace exist
    SWM -> Blockly: svgResize(workspace)
else
    SWM -> SWM: console.error("Cannot resize workspace.")
end
@enduml

compileStrategyJson:

@startuml
start
:Check if workspace is available;
if (workspace exists?) then (yes)
    :Get strategy name from name_box;
    :Generate strategy JSON;
    note right
    Calls _generateStrategyJsonFromWorkspace()
    end note
    if (strategyJson is null?) then (yes)
        :Log error "Failed to generate strategy JSON.";
        stop
    endif
    :Serialize workspace to XML;
    :Return JSON string containing strategy data;
else (no)
    :Log error "Workspace is not available.";
    stop
endif
@enduml


_generateStrategyJsonFromWorkspace:

@startuml
start
:Categorize orphaned blocks;
note right
Calls _categorizeOrphanedBlocks(workspace)
end note

:Calculate totalTopLevelBlocks;

if (totalTopLevelBlocks > MAX_TOP_LEVEL_BLOCKS?) then (yes)
    :Log error "Too many top-level blocks.";
    :Alert user;
    :Return null;
    stop
else (no)
    :Create strategy JSON;
    note right
    Calls _createStrategyJson(initializationBlocks, actionBlocks)
    end note
    :Return strategy JSON;
    stop
endif
@enduml

_categorizeOrphanedBlocks:

@startuml
start
:Get top-level blocks from workspace;

:Initialize initializationBlocks and actionBlocks arrays;

:Initialize index = 0;

while (index < total number of blocks?) is (yes)
    :Get block at index;
    if (block.type is initialization type?) then (yes)
        :Add block to initializationBlocks;
    else if (block.type is action type?) then (yes)
        :Add block to actionBlocks;
    else
        :Log warning "Block type not allowed as top-level block.";
    endif
    :Increment index;
endwhile

:Return { initializationBlocks, actionBlocks };
stop
@enduml




_createStrategyJson:

@startuml
start
:Initialize empty statements array;

:Initialize index = 0;

while (index < number of initializationBlocks?) is (yes)
    :Get block at index from initializationBlocks;
    :Convert block to JSON;
    :Add to statements array;
    :Increment index;
endwhile

:Reset index = 0;

while (index < number of actionBlocks?) is (yes)
    :Get block at index from actionBlocks;
    :Convert block to JSON;
    :Add to statements array;
    :Increment index;
endwhile

:Create strategy JSON with type 'strategy' and statements;
:Return strategy JSON;
stop
@enduml

_loadWorkspaceFromXml:

@startuml
participant "StratWorkspaceManager" as SWM
participant "Blockly"
participant "Workspace" as WS

SWM -> SWM: Check if workspace is initialized
alt workspace exists
    SWM -> Blockly: textToDom(workspaceXmlText)
    alt workspaceXml is valid
        SWM -> WS: clear()
        SWM -> Blockly: domToWorkspace(workspaceXml, workspace)
    else
        SWM -> SWM: console.error('Invalid workspace XML.')
        SWM -> User: alert('Invalid workspace data.')
    end
else
    SWM -> SWM: console.error('Workspace is not initialized.')
end
@enduml