Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabling automated calculations through conditional properties #40

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

MayerBjoern
Copy link
Contributor

@MayerBjoern MayerBjoern commented Aug 23, 2022

This small features uses the infrastructure for conditions in a different way. The newly added condition is called calculate. It does not hide properties, the spoken logic essentially is: "If properties a and b are set, this property can be filled in automatically based on the values of a and b".

Let's look at a small and simplistic JSON example to explain this further:

"m": {
  "title": "Meters",
  "units": "m",
  "conditions": [
	{
	  "type": "calculate",
	  "property_names": ["s", "mps"],
	  "digits": 2,
	  "formula": "mps * s"
	}
  ],
  "type": "quantity"
},
"s": {
  "title": "Seconds",
  "units": "s",
  "conditions": [
	{
	  "type": "calculate",
	  "property_names": ["m", "mps"],
	  "digits": 2,
	  "formula": "m / mps"
	}
  ],
  "type": "quantity"
},
"mps": {
  "title": "Meters per second",
  "units": "m/s",
  "conditions": [
	{
	  "type": "calculate",
	  "property_names": ["m", "s"],
	  "digits": 2,
	  "formula": "m / s"
	}
  ],
  "type": "quantity"
},

Once the user fills in meters and seconds with values, meters per seconds gets automatically filled in. If i fill in mps and meters, seconds will be automatically calculated. This is useful for any sort of numerical data that is correlated but where initially it is not clear what values the user will provide/has measured.

property_names is renamed to the plural here, since it expects an array of other properties.
digits is simply the number of decimal places to round the result to
formula is obviously the calculation formula and essentially just a wrapper for math.js.evaluate

Linked property names in the formula will be replaced with their respective values before mathematical evaluation.

@FlorianRhiem FlorianRhiem self-assigned this Aug 24, 2022
@FlorianRhiem
Copy link
Member

Hey Björn, thank you for the pull request!

The documentation and the demo include calculations that cross-reference each other, but when testing the demo example, these do not seem to work consistently:

  • Starting with a blank form, when I enter 10 meters nothing happens, as expected, because both seconds and meters per second require a second input.
  • When I then enter 2 seconds, 5 meters per seconds is calculated correctly.
  • When I then update the meters per second to 10, nothing happens to meters and seconds and the values are invalid.
  • When I then update the meters to 5, seconds is updated to 0.5
  • When I then update the seconds to 2, nothing happens and the values are invalid again.

This might be avoidable if there is a clear order of evaluation, so that changes are consistent. If it cannot be avoided, then perhaps it might be better not to allow such dependency "loops".

When submitting the object form for the demo schema without entering a sample name, I get a broken form without the other fields, likely because their conditions aren't all fulfilled. I'm not sure if the conditions are a good place for something like this which is not related to conditional fields. Perhaps there should be a second property for more generic features like this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants