Commit 4711be1b authored by Jaden DIEFENBAUGH's avatar Jaden DIEFENBAUGH

[js][alg] much more intelligent algorithm templating, closes #118

parent ff6103b4
# 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():
......
......@@ -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(
......
......@@ -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
......
......@@ -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) => {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment