From 0239c2250c71b518da08fd27795ab9a59ea1b417 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 18 Apr 2020 12:15:44 +0200 Subject: [PATCH] Dependency Filters --- .../FunctionLibraries/DependenciesFilter.js | 74 +++++++++++++++++++ .../FunctionLibraries/SessionFunctions.js | 7 +- DesignSpace/Workspace/Workspace.js | 3 +- 3 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 DesignSpace/Workspace/FunctionLibraries/DependenciesFilter.js diff --git a/DesignSpace/Workspace/FunctionLibraries/DependenciesFilter.js b/DesignSpace/Workspace/FunctionLibraries/DependenciesFilter.js new file mode 100644 index 000000000..2f88bf30d --- /dev/null +++ b/DesignSpace/Workspace/FunctionLibraries/DependenciesFilter.js @@ -0,0 +1,74 @@ +function newDependenciesFilter () { + thisObject = { + createFilter: createFilter + } + + return thisObject + + function createFilter (node) { + let filters = new Map() + recursiveFilter(node) + + return Array.from(filters.keys()) + + function recursiveFilter (node) { + filter(node.code) + + let nodeDefinition = APP_SCHEMA_MAP.get(node.type) + if (nodeDefinition !== undefined) { + if (nodeDefinition.properties !== undefined) { + let previousPropertyName // Since there are cases where there are many properties with the same name,because they can hold nodes of different types but only one at the time, we have to avoind counting each property of those as individual children. + for (let i = 0; i < nodeDefinition.properties.length; i++) { + let property = nodeDefinition.properties[i] + + switch (property.type) { + case 'node': { + if (property.name !== previousPropertyName) { + if (node[property.name] !== undefined) { + recursiveFilter(node[property.name]) + } + previousPropertyName = property.name + } + } + break + case 'array': { + let nodePropertyArray = node[property.name] + if (nodePropertyArray !== undefined) { + for (let j = 0; j < nodePropertyArray.length; j++) { + let nodeProperty = nodePropertyArray[j] + recursiveFilter(nodeProperty) + } + } + } + break + } + } + } + } + } + + function filter (code) { + if (code === undefined) { return } + + let instructionsArray = code.split(' ') + for (let i = 0; i < instructionsArray.length; i++) { + let instruction = instructionsArray[i] + if (instruction.indexOf('chart') >= 0) { + let parts = instruction.split('.') + let timeFrame = parts[1] + let product = parts[2] + let productParts = product.split(',') + product = productParts[0] + productParts = product.split(')') + product = productParts[0] + + // Example: chart.at01hs.popularSMA.sma200 - chart.at01hs.popularSMA.sma100 < 10 + if (timeFrame !== 'atAnyTimeFrame') { + timeFrame = timeFrame.substring(2, 4) + '-' + timeFrame.substring(4, 7) + } + filters.set(timeFrame + '-' + product, true) + } + } + } + } +} diff --git a/DesignSpace/Workspace/FunctionLibraries/SessionFunctions.js b/DesignSpace/Workspace/FunctionLibraries/SessionFunctions.js index 13e33b381..c29f97884 100644 --- a/DesignSpace/Workspace/FunctionLibraries/SessionFunctions.js +++ b/DesignSpace/Workspace/FunctionLibraries/SessionFunctions.js @@ -6,7 +6,7 @@ function newSessionFunctions () { return thisObject - function runSession (node, functionLibraryProtocolNode, callBackFunction) { + function runSession (node, functionLibraryProtocolNode, functionLibraryDependenciesFilter, callBackFunction) { /* We can not run a sessionif its parent process is not running. Less if it does not have a parent. */ if (node.payload.parentNode === undefined) { node.payload.uiObject.setErrorMessage('Session needs a Process Instance parent to be able to run.') @@ -59,10 +59,13 @@ function newSessionFunctions () { let session = functionLibraryProtocolNode.getProtocolNode(node, false, true, true, false, false, lightingPath) + let dependencyFilter = functionLibraryDependenciesFilter.createFilter(node.payload.referenceParent) + /* Raise event to run the session */ let event = { session: JSON.stringify(session), - tradingSystem: JSON.stringify(tradingSystem) + tradingSystem: JSON.stringify(tradingSystem), + dependencyFilter: JSON.stringify(dependencyFilter) } eventsServerClient.raiseEvent(key, 'Run Session', event) diff --git a/DesignSpace/Workspace/Workspace.js b/DesignSpace/Workspace/Workspace.js index 5bb9a0a88..dedea1c5b 100644 --- a/DesignSpace/Workspace/Workspace.js +++ b/DesignSpace/Workspace/Workspace.js @@ -60,6 +60,7 @@ function newWorkspace () { let functionLibrarySuperScripts = newSuperScriptsFunctions() let functionLibraryCCXTFunctions = newCCXTFunctions() let functionLibraryWebhookFunctions = newWebhookFunctions() + let functionLibraryDependenciesFilter = newDependenciesFilter() thisObject.nodeChildren = newNodeChildren() @@ -420,7 +421,7 @@ function newWorkspace () { break case 'Run Session': { - functionLibrarySessionFunctions.runSession(payload.node, functionLibraryProtocolNode, callBackFunction) + functionLibrarySessionFunctions.runSession(payload.node, functionLibraryProtocolNode, functionLibraryDependenciesFilter, callBackFunction) } break case 'Stop Session':