Green-Software-Foundation / if

Impact Framework
https://if.greensoftware.foundation/
MIT License
139 stars 40 forks source link

Make "inline" arithmetic work in manifests #970

Open jmcook1186 opened 4 weeks ago

jmcook1186 commented 4 weeks ago

What

Where a number is passed in a manifest, a sum should also work, e.g.

The following should be allowed:

some-param: 30*2
duration: 60*60*24*7*4

and also the following, using other parameters in the manifest:

some-param: other-param*3

Why

This reduces the number of plugins required in pipelines because simple conversions that would otherwise need dedicated instances of our generic builtins can be done inline.

Context

Part of UX improvements

Prerequisites/resources n/a

SoW (scope of work)

Acceptance criteria

Given (Setup): Inline arithmetic is enabled for manifests

When (Action): I run the following manifest

name: coefficient-demo
description: successful path
tags:
initialize:
  plugins:
    coefficient:
      method: Coefficient
      path: "builtin"
      global-config:
        input-parameter: carbon * 3
        coefficient: 3*arbitrary-constant
        output-parameter: "carbon-product"
tree:
  children:
    child:
      pipeline:
        compute:
          - coefficient
      inputs:
        - timestamp: 2023-08-06T00:00
          duration: 3600
          carbon: 3
          arbitrary-constant: 2

Then (Assertion): I get the following output:

name: coefficient-demo
description: successful path
tags:
initialize:
  plugins:
    coefficient:
      method: Coefficient
      path: "builtin"
      global-config:
        input-parameter: carbon * 3
        coefficient: 3*arbitrary-constant
        output-parameter: "carbon-product"
tree:
  children:
    child:
      pipeline:
        compute:
          - coefficient
      inputs:
        - timestamp: 2023-08-06T00:00
          duration: 3600
          carbon: 3
          arbitrary-constant: 2
      outputs:
        - timestamp: 2023-08-06T00:00
          duration: 3600
          carbon: 3
          arbitrary-constant: 2
          carbon-product: 54 // because we are doing (3 * 3) * (3 + 2) 
manushak commented 3 weeks ago

@jmcook1186, should the inline arithmetic feature work only for multiplication, or should it also allow division, addition, and subtraction? For example, if we have coefficient: 3*arbitrary-constant, it will be hard to distinguish which part is the parameter's name and which part is the math operation (I mean this part: arbitrary-constant).

jmcook1186 commented 3 weeks ago

oh that's a good point.

Maybe we can include a keyword and require names to be wrapped in ' ' in order for inline arithmetic to work e.g. e.g. the = operator is used to tell IF to evaluate the following phrase:

coefficient: = 3 * 'arbitrary-constant'

Wyt? Do you have any alternative suggestions?

@manushak

manushak commented 3 weeks ago

yeah, that's a good idea, but in the case of duration: 60*60*24*7*4 I don't think it will look like natural. Anyway, I don't have any suggestions right now, so I'll add =

zanete commented 3 days ago

@jmcook1186 please add the feature request in relation to this issue as part of the description

zanete commented 5 hours ago

need a release in if-core before creating a PR, @narekhovhannisyan please help with that 🙏