diff --git a/beat/editor/templates/algorithm.jinja2 b/beat/editor/templates/algorithm.jinja2 index 3665cae1f4b69974cc81a7e898fe1d5e2bb44ec4..dd2cb4ad3885175a8a847625a3c5c003d00f62df 100644 --- a/beat/editor/templates/algorithm.jinja2 +++ b/beat/editor/templates/algorithm.jinja2 @@ -1,22 +1,31 @@ # You may import any python packages that will be available in the environment you will run this algorithm in # Environments can change based on the experiment's settings -{% for ref, lib in uses.items() %}# Library "{{ lib }}" is available under "{{ ref }}" +{% for ref, lib in contents.uses.items() %}# Library "{{ lib }}" is available under "{{ ref }}" {% endfor %} class Algorithm: # initialise fields to store cross-input data (e.g. machines, aggregations, etc.) def __init__(self): pass -{% if has_parameters %} +{% if contents.parameters %} # do initial setup work with the given parameters for the algorithm def setup(self, parameters): + # Parameters available: + {% for p_name, param in contents.parameters.items() %}# Parameter "{{ p_name }}" with type "{{ param.type }}" + {% endfor %} # get a parameter like: - # self.param1 = parameters.get('param_1', self.param1) + # param1_value = parameters.get('param_1', self.param1_default) return True {% endif %} # this will be called each time the sync'd input has more data available to be processed def process(self, inputs, outputs): + # Groups available: + {% for group in contents.groups %}# Group {{ loop.index }}: + {% for iName, input in group.inputs.items() %}# Input "{{ iName }}" with type "{{ input.type }}" + {% endfor %}{% if 'outputs' in group %}{% for oName, output in group.outputs.items() %}# Output "{{ oName }}" with type "{{ output.type }}" + {% endfor %}{% endif %}{% endfor %} + # to check if there is more data waiting in the inputs # (if it is False, you have processed all the inputs and this "process" function won't be called again): # if inputs.hasMoreData(): diff --git a/beat/editor/utils.py b/beat/editor/utils.py index 84fedf4ddda50d5778f6c292f5767bac7365d046..d2fc57fe9aad5581d3e79ee303c892bd05d3491c 100644 --- a/beat/editor/utils.py +++ b/beat/editor/utils.py @@ -82,18 +82,13 @@ def generate_library(uses=None): return template.render(uses=uses) -def generate_algorithm(has_parameters=False, uses=None): +def generate_algorithm(contents): """Generates a valid BEAT algorithm from our stored template Parameters: - has_parameters (:py:class:`bool`, Optional): Whether the algorithm has - parameters or not (default: False) - - uses (:py:class:`dict`, Optional): A dict of libraries that the algorithm - uses. Keys are the value to reference the library, values are the - library being referenced. + contents (:py:class:`dict`): The algorithm's JSON metadata Returns: @@ -102,9 +97,8 @@ def generate_algorithm(has_parameters=False, uses=None): """ - uses = uses or {} template = ENV.get_template('algorithm.jinja2') - return template.render(uses=uses, has_parameters=has_parameters) + return template.render(contents=contents) TEMPLATE_FUNCTION = dict( diff --git a/conda/js/src/components/EntityTemplateGenerationButton.jsx b/conda/js/src/components/EntityTemplateGenerationButton.jsx index 217ccb57e5f1071fc7d4b3c04fe372870ff71d7d..9e9564ea0b49fc4abee9423481591998dda59b37 100644 --- a/conda/js/src/components/EntityTemplateGenerationButton.jsx +++ b/conda/js/src/components/EntityTemplateGenerationButton.jsx @@ -57,9 +57,7 @@ export default class EntityTemplateGenerationButton extends React.Component<Prop // find the used libraries if(!this.props.data.contents.parameters) throw new Error(`Bad alg object, no params field: ${ this.props.data.contents }`); - const hasParameters = Object.keys(this.props.data.contents.parameters).length > 0; - uses = copyObj(this.props.data.contents.uses); - res = generateAlgorithmTemplate(this.props.data.name, hasParameters, uses); + res = generateAlgorithmTemplate(this.props.data.name, this.props.data.contents); break; case('library'): // find the used libraries diff --git a/conda/js/src/helpers/api.js b/conda/js/src/helpers/api.js index 3afcb2ad3658045b3cf8f3055b4ceb46030910b9..86aa9ce27c2153e820b6129cbc6ff10d8f44b595 100644 --- a/conda/js/src/helpers/api.js +++ b/conda/js/src/helpers/api.js @@ -91,8 +91,8 @@ export const generateDatabaseTemplate = async (name: string, views: string[]) => generateTemplate('databases', {name, views}); }; -export const generateAlgorithmTemplate = async (name: string, hasParameters: boolean, uses: StringObject) => { - generateTemplate('algorithms', {name, has_parameters: hasParameters, uses}); +export const generateAlgorithmTemplate = async (name: string, contents: any) => { + generateTemplate('algorithms', {name, contents}); }; export const generateLibraryTemplate = async (name: string, uses: StringObject) => {