Skip to content

Commit

Permalink
Moved tool functions and conversion function invocation to ToolsManag…
Browse files Browse the repository at this point in the history
…er and FunctionRegistry as part of #40
  • Loading branch information
barnettwilliam committed Mar 12, 2024
1 parent cb2e9a4 commit 8678cf2
Show file tree
Hide file tree
Showing 3 changed files with 317 additions and 349 deletions.
74 changes: 71 additions & 3 deletions platform/src/FunctionRegistry.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import {arrayEquals} from "./Utility.js"
import {arrayEquals, jsonRequestConversion, jsonRequest} from "./Utility.js"

class FunctionRegistry {

register = [];
toolsManager;

/**
* @param {ToolsManager} theToolsManager - the tools manager
*/
constructor(theToolsManager){
this.toolsManager = theToolsManager;
}

registerFunction( inputParameterTypes, outputParameterType, functionId ){

Expand All @@ -14,7 +22,7 @@ register = [];
this.register.push( entry );
}

lookupFunction(inputParameterTypes, outputParameterType){
find(inputParameterTypes, outputParameterType){

let foundEntry;

Expand All @@ -33,7 +41,7 @@ register = [];
}


lookupFunctionsPartialMatch(inputParameterTypes, outputParameterType){
findPartial(inputParameterTypes, outputParameterType){

let foundEntries;

Expand All @@ -51,6 +59,66 @@ register = [];
}
}

/**
* Resolves the id of an action function to the function itself
*
* @param {string} functionId
*/
resolve(functionId){
return this.toolsManager.getActionFunction(functionId)
}

/**
* Requests the conversion function from the remote tool service
*
* @param {Object} parameters
* @param {ActionFunction} converstionFunction
* @param {String} parameterName name of the parameter
* @returns Promise for the translated data
*/
requestTranslation(parameters, conversionFunction, parameterName){

let parametersJson = JSON.stringify(parameters);

return jsonRequestConversion(conversionFunction.getPath(), parametersJson, parameterName);
}

/**
*
* @param {string} functionId url of the function to call
* @param {Object} parameters object containing the parameters request data
* @returns
*/
call(functionId, parameters ){

let actionFunction = this.toolsManager.getActionFunction(functionId);
let parametersJson = JSON.stringify(parameters);

let requestPromise = jsonRequest(actionFunction.getPath(), parametersJson)

return requestPromise;
}

/**
* Prepares the input parameters and requests the type translation for the given function id
*
* @param {string} functionId the id of the action function
* @param {Object} typeValuesMap an object mapping action functions paramter types as keys to input values
* @param {string} parameterName name of the parameter
* @returns Promise for the translated data
*
*/
callConversion( functionId, typeValuesMap, parameterName ){
let conversionRequestData = {};
let conversionFunction = this.toolsManager.getActionFunction(functionId);

// Populate parameters for the conversion request
for( const param of conversionFunction.getParameters() ){
conversionRequestData[param.name] = typeValuesMap[param.type];
}

return this.requestTranslation(conversionRequestData, conversionFunction, parameterName);
}
}

export { FunctionRegistry };
Loading

0 comments on commit 8678cf2

Please sign in to comment.