Service - Transform Units: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
= Overview = | = Overview = | ||
Transformations are used to convert | Transformations are used to convert any number of inputUnits into any number of outputUnits. | ||
= Repository = | = Repository = | ||
| Line 7: | Line 7: | ||
https://bitbucket.org/izara-supply-units/izara-supply-units-transform-units | https://bitbucket.org/izara-supply-units/izara-supply-units-transform-units | ||
= | == Calculating values == | ||
combinationEquation and calculateByProperties fields replace out special tags first then parse through an evaluation library, eg: | |||
* https://mathjs.org/ | |||
* https://www.npmjs.com/package/string-math | |||
* https://www.npmjs.com/package/exact-math | |||
* https://www.codeproject.com/Articles/12116/JavaScript-Mathematical-Expression-Evaluator | |||
Must make sure equation is validated/sanitized first, be careful of eval or new Function which can invoke arbritrary commands | |||
= | = Transform request = | ||
* transformConfig is primarily used as a default template, each unitTransform can adjust almost all details such as units inputted and outputted propertyValues | |||
* user can use the transformConfig inputUnits calculatedProperties to recalc inputUnit property values according to the outputUnits numberOfUnits | |||
* user can set to recalculate outputUnits calculatedProperties according to inputUnits property values | |||
* inputUnits array can have multiple elements with the same unitTypeId, this is a guide for frontend to allow multiple units of the same unitType but eg different fabrics to be inputs in the same transformation | |||
* outputUnit calculatedProperties will calculate the same for all outputUnit numberOfUnits, can be adjusted manually | |||
* outputUnits array can have multiple elements with the same unitTypeId for eg calcualting cost for sizes of different clothing) | |||
= | = transformConfigInputOutput = | ||
<syntaxhighlight lang="JavaScript"> | <syntaxhighlight lang="JavaScript"> | ||
{ | { | ||
inputUnits: [ | |||
{unitTypeId | { | ||
stageId: "xx", // optional stage that | unitTypeId: "zz", | ||
stageId: "xx", // optional, stage that applies stageAdjustments to input unit | |||
numberOfUnits: 1, // default number of inputUnits used to perform this transform | |||
}, // | calculatedProperties:{ // calculates changes to inputUnits unitTrackedProperties | ||
unitTypeTrackedPropertyId: { | |||
integerOnly: false, // default false, if true then calculated splits must be integers, will not process transformation if split per outputUnit are not integers | |||
{unitTypeId | quantityReductionSetting: "spread"|"clearPerUnit", // if not all quantityUnitTypeTrackedPropertyId are used up in transformation, do we reduce all numberOfUnits quantity equally, or clear quantity out one unit at a time | ||
stageId: "xx", // | valueUsePerOutputUnit: 1, | ||
calculatedProperties: { // calculates | }, | ||
// ... | |||
}, | |||
}, | |||
//.. | |||
], | |||
outputUnits: [ | |||
{ | |||
unitTypeId: "zz", | |||
stageId: "xx", // optional, stage that applies stageAdjustments to output unit | |||
numberOfUnits: 1, // default number of outputUnits to create, calculations are based on this and inputUnit numberOfUnits | |||
calculatedProperties: { // calculates value for unitTypeTrackedProperties added to outputUnits | |||
unitTypeTrackedPropertyId: { | |||
calculationType: "calculateValue", // can add other types later | calculationType: "calculateValue", // can add other types later | ||
combinationEquation: "[ | combinationEquation: "[calcPropTagA]+[calcPropTagB]", // optional, method of combining multiple calculateByProperties together, defaults to adding/sum | ||
calculateByProperties: | calculateByProperties: { | ||
calcPropTag: { // any string, used in combinationEquation | |||
calculation: "[valueUsePerOutputUnit:{inputUnitTypeId}:{unitTypeTrackedPropertyId}]*[unitTypeTrackedProperty:{inputUnitTypeId}:{unitTypeTrackedPropertyId}]", | |||
or | |||
defaultValue: "30", // not calculated, user can overwrite defaultValue | |||
round: "round", //optional: "round"|"ceil"|"floor"|.. | |||
}, | }, | ||
// .. | // .. | ||
} | |||
}, | }, | ||
//.. | //.. | ||
| Line 52: | Line 73: | ||
}, | }, | ||
//.. | //.. | ||
], | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
= | = objectSchemas = | ||
<syntaxhighlight lang="JavaScript"> | <syntaxhighlight lang="JavaScript"> | ||
{ | { | ||
objectType: "transformConfig", | |||
addOnDataStructure: [ | |||
{ | |||
type:"versionedData", | |||
versionedDataLabel: "transformConfigInputOutput", | |||
storageResourceTag : "xx", | |||
fieldNames: [ | |||
{ | |||
fieldName: "inputUnits", | |||
}, | |||
{ | |||
fieldName: "outputUnits", | |||
}, | |||
] | |||
}, | |||
], | |||
storageResources:{ | |||
"xxx":{ | |||
storageType: "graph", | |||
graphServerTag: "xx", | |||
} | |||
}, | |||
fieldNames: { | |||
transformConfigId: { | |||
type: "string", | |||
randomOnCreate: true | |||
}, | |||
{ | |||
fieldName: "inputUnits", | |||
type: "arrayMixed", | |||
requiredOnCreate: true | |||
validation: { | |||
itemValidation:{} // for validate item in array | |||
}, | }, | ||
}, | }, | ||
{ | |||
fieldName: "outputUnits", | |||
type: "arrayMixed", | |||
requiredOnCreate: true | |||
validation: { | |||
itemValidation:{} // for validate item in array | |||
{ | |||
}, | }, | ||
}, | }, | ||
} | }, | ||
} | identifiers: [ | ||
{ | |||
fieldName: "transformConfigId" | |||
}, | |||
] | |||
}, | |||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="JavaScript"> | <syntaxhighlight lang="JavaScript"> | ||
{ | { | ||
objectType: "unitTransform", | |||
storageResources:{ | |||
"xxx":{ | |||
storageType: "graph", | |||
graphServerTag: "xx", | |||
} | |||
}, | |||
fieldNames: { | |||
unitTransformId: { | |||
type: "string", | |||
randomOnCreate: true | |||
}, | |||
}, | }, | ||
} | identifiers: [ | ||
{ | |||
fieldName: "unitTransformId" | |||
}, | |||
] | |||
}, | |||
</syntaxhighlight> | </syntaxhighlight> | ||
= | = relationshipSchemas = | ||
<syntaxhighlight lang="JavaScript"> | <syntaxhighlight lang="JavaScript"> | ||
{ | [ | ||
{ | |||
"oldTransformUnitTrackedProperty": { | |||
storageResources:{ | |||
"xxx":{ | |||
storageType: "graph", | |||
graphServerTag: "xx", | |||
} | |||
}, | |||
links: [ | |||
} | { | ||
storageResourceTags: ["xxx"], | |||
from: { | |||
objType: { | |||
serviceTag: "transformUnits", | |||
objectType: "unitTransform" | |||
}, | |||
linkType: "one", | |||
}, | |||
to: { | |||
objType: { | |||
serviceTag: "unitType", | |||
objectType: "unitTrackedProperty" | |||
}, | |||
linkType: "many", | |||
} | |||
} | |||
] | |||
} | |||
} | |||
] | |||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="JavaScript"> | <syntaxhighlight lang="JavaScript"> | ||
{ | [ | ||
{ | |||
"newTransformUnitTrackedProperty": { | |||
storageResources:{ | |||
"xxx":{ | |||
storageType: "graph", | |||
graphServerTag: "xx", | |||
} | |||
}, | |||
fieldNames: { | |||
} | adjustmentValue: { | ||
type: "string", // so can adjust any type of property, will need cast type in logic | |||
requiredOnCreate: true, | |||
}, | |||
}, | |||
links: [ | |||
{ | |||
storageResourceTags: ["xxx"], | |||
from: { | |||
objType: { | |||
serviceTag: "transformUnits", | |||
objectType: "unitTransform" | |||
}, | |||
linkType: "one", | |||
}, | |||
to: { | |||
objType: { | |||
serviceTag: "unitType", | |||
objectType: "unitTrackedProperty" | |||
}, | |||
linkType: "many", | |||
} | |||
} | |||
] | |||
} | |||
} | |||
] | |||
</syntaxhighlight> | </syntaxhighlight> | ||
* | |||
* links unitTransform event to all before and after unitTrackedProperties that are adjusted by this transformation | |||
= Working documents = | = Working documents = | ||
Revision as of 12:18, 16 February 2025
Overview
Transformations are used to convert any number of inputUnits into any number of outputUnits.
Repository
https://bitbucket.org/izara-supply-units/izara-supply-units-transform-units
Calculating values
combinationEquation and calculateByProperties fields replace out special tags first then parse through an evaluation library, eg:
- https://mathjs.org/
- https://www.npmjs.com/package/string-math
- https://www.npmjs.com/package/exact-math
- https://www.codeproject.com/Articles/12116/JavaScript-Mathematical-Expression-Evaluator
Must make sure equation is validated/sanitized first, be careful of eval or new Function which can invoke arbritrary commands
Transform request
- transformConfig is primarily used as a default template, each unitTransform can adjust almost all details such as units inputted and outputted propertyValues
- user can use the transformConfig inputUnits calculatedProperties to recalc inputUnit property values according to the outputUnits numberOfUnits
- user can set to recalculate outputUnits calculatedProperties according to inputUnits property values
- inputUnits array can have multiple elements with the same unitTypeId, this is a guide for frontend to allow multiple units of the same unitType but eg different fabrics to be inputs in the same transformation
- outputUnit calculatedProperties will calculate the same for all outputUnit numberOfUnits, can be adjusted manually
- outputUnits array can have multiple elements with the same unitTypeId for eg calcualting cost for sizes of different clothing)
transformConfigInputOutput
{
inputUnits: [
{
unitTypeId: "zz",
stageId: "xx", // optional, stage that applies stageAdjustments to input unit
numberOfUnits: 1, // default number of inputUnits used to perform this transform
calculatedProperties:{ // calculates changes to inputUnits unitTrackedProperties
unitTypeTrackedPropertyId: {
integerOnly: false, // default false, if true then calculated splits must be integers, will not process transformation if split per outputUnit are not integers
quantityReductionSetting: "spread"|"clearPerUnit", // if not all quantityUnitTypeTrackedPropertyId are used up in transformation, do we reduce all numberOfUnits quantity equally, or clear quantity out one unit at a time
valueUsePerOutputUnit: 1,
},
// ...
},
},
//..
],
outputUnits: [
{
unitTypeId: "zz",
stageId: "xx", // optional, stage that applies stageAdjustments to output unit
numberOfUnits: 1, // default number of outputUnits to create, calculations are based on this and inputUnit numberOfUnits
calculatedProperties: { // calculates value for unitTypeTrackedProperties added to outputUnits
unitTypeTrackedPropertyId: {
calculationType: "calculateValue", // can add other types later
combinationEquation: "[calcPropTagA]+[calcPropTagB]", // optional, method of combining multiple calculateByProperties together, defaults to adding/sum
calculateByProperties: {
calcPropTag: { // any string, used in combinationEquation
calculation: "[valueUsePerOutputUnit:{inputUnitTypeId}:{unitTypeTrackedPropertyId}]*[unitTypeTrackedProperty:{inputUnitTypeId}:{unitTypeTrackedPropertyId}]",
or
defaultValue: "30", // not calculated, user can overwrite defaultValue
round: "round", //optional: "round"|"ceil"|"floor"|..
},
// ..
}
},
//..
}
},
//..
],
}
objectSchemas
{
objectType: "transformConfig",
addOnDataStructure: [
{
type:"versionedData",
versionedDataLabel: "transformConfigInputOutput",
storageResourceTag : "xx",
fieldNames: [
{
fieldName: "inputUnits",
},
{
fieldName: "outputUnits",
},
]
},
],
storageResources:{
"xxx":{
storageType: "graph",
graphServerTag: "xx",
}
},
fieldNames: {
transformConfigId: {
type: "string",
randomOnCreate: true
},
{
fieldName: "inputUnits",
type: "arrayMixed",
requiredOnCreate: true
validation: {
itemValidation:{} // for validate item in array
},
},
{
fieldName: "outputUnits",
type: "arrayMixed",
requiredOnCreate: true
validation: {
itemValidation:{} // for validate item in array
},
},
},
identifiers: [
{
fieldName: "transformConfigId"
},
]
},
{
objectType: "unitTransform",
storageResources:{
"xxx":{
storageType: "graph",
graphServerTag: "xx",
}
},
fieldNames: {
unitTransformId: {
type: "string",
randomOnCreate: true
},
},
identifiers: [
{
fieldName: "unitTransformId"
},
]
},
relationshipSchemas
[
{
"oldTransformUnitTrackedProperty": {
storageResources:{
"xxx":{
storageType: "graph",
graphServerTag: "xx",
}
},
links: [
{
storageResourceTags: ["xxx"],
from: {
objType: {
serviceTag: "transformUnits",
objectType: "unitTransform"
},
linkType: "one",
},
to: {
objType: {
serviceTag: "unitType",
objectType: "unitTrackedProperty"
},
linkType: "many",
}
}
]
}
}
]
[
{
"newTransformUnitTrackedProperty": {
storageResources:{
"xxx":{
storageType: "graph",
graphServerTag: "xx",
}
},
fieldNames: {
adjustmentValue: {
type: "string", // so can adjust any type of property, will need cast type in logic
requiredOnCreate: true,
},
},
links: [
{
storageResourceTags: ["xxx"],
from: {
objType: {
serviceTag: "transformUnits",
objectType: "unitTransform"
},
linkType: "one",
},
to: {
objType: {
serviceTag: "unitType",
objectType: "unitTrackedProperty"
},
linkType: "many",
}
}
]
}
}
]
- links unitTransform event to all before and after unitTrackedProperties that are adjusted by this transformation