This is kind of a design question. Lets say that I have a CoreData model with 2 entities - Item and Formula.
Item has 3 numeric attributes "X", "Y" and "Z", and a to-one relation to a "Formula" entity.
Formula has one string attribute containing expressions like "(X*Y*Z)**(1.0/3)" or "Pi * X**3 / 3.0" etc. Any simple arithmetic using constant numbers, standard operators (addition, subtraction, multiplication, division, power, parentheses) and the "X" "Y" and "Z" symbols.
Now my task is very expected --- how to set up a new attribute to the "Item" entity, called "value" which will be calculated by plugging the X Y and Z values into the related "Formula", and evaluating the expression.
Considerations: 1. There may be millions of "Item" entities, and hundreds of "Formula". 2. I have control over the format in which Formula strings are created --- I can have people type in "$X+$Y" instead of "X+Y" if that eases things. 3. I will need to further calculate statistics on the "Value" attributed across subsets of items quickly (sums, medians stdDeves, averages etc.)
My questions: 1. generally how to go about it. Add a real numeric "Value" attribute to cache calculated results, or a calculated property that re-calculates when read? 2. How to use NSExpression to plug values instead of variable symbols like "X" "Y" "Z". 3. Can I somehow pre-create an NSExpression and cache it as another attribute of "Formula", and use it later instead of parsing and evaluating the formula for each item? How can one store a parsed NSExpression in CoreData?
I know this is a big question with many sub-questions. any hint will be appreciated!