sciapp / sampledb

Sample and Measurement Metadata Database
https://scientific-it-systems.iffgit.fz-juelich.de/SampleDB/
MIT License
21 stars 11 forks source link

Enabling automated calculations through conditional properties #40

Open MayerBjoern opened 2 years ago

MayerBjoern commented 2 years ago

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 commented 2 years ago

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:

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?