What is the best way to store a formula in a database for a table driven formula? [closed]

So I am changing my code over to a table driven formula system to make it sustainable in the long term.

So basically I have 5 foreign keys that will get me to a single formula to use. However I’m not quite sure how I should store the formula. At the moment I am just using a char(100) and parsing it in Java on retrieval. This seems risky though.

My formulas are quite simple (.9 * inputBoxA) but it just gives me a bad feeling doing it this way. Is there a standard for this or will I just have to push on?

Edit:
Let me explain my situation a bit more. I have 5 foreign keys all based on user input. One other input is a rate that the user gives. So based on the 5 FKs I want to do a calculation on this rate.

The formula will usually be (inputRate), (.9*inputRate), etc. But it can get a bit more complex (.9*(.6*inputRate-.1)) but still nothing too bad.

I will be retrieving the formulas from my database with a JPA query and process it in Java so the user should never directly see the formulas. However in the future new formulas may be necessary so I need some standard for others to be able to enter them.

7

It is a matter of opinion. You might use postfix representation (à la RPN, or even some stack-oriented programming language), or some bytecode (specific to your expression language), or some s-expr syntax, if you want some more compact representation. JSON is probably a less compact textual representation but it is very widely used (and you’ve got many libraries to parse JSON); you could define your JSON format for your expression ASTs.

You could also store a compressed form of your formula’s representation in your database.

You might consider using some bytecode of some existing interpreter, e.g. Lua, Guile2, or even some JVM class with your own class loader, etc…

And perhaps you want to store closures, not expressions.

You may want to embed an interpreter inside your application.

You need to learn more about programming languages (e.g. read Scott’s book: Programming Language Pragmatics), compilers (read the dragon book) & parsing & interpreters & semantics ; if you know a bit of Scheme or Lisp (and learning it thru SICP is not a loss of your time) I strongly recommend Queinnec’s book Lisp In Small Pieces, giving a broad view of interpretation & compilation techniques.

MathML is a standard for representation of mathematics, notably formulas (using XML). But it is not compact and not easy to parse, so very probably overkill to represent expressions.

OpenDocument is a standard for spreadsheet & word processing documents, so contains a notation for expressions (apparently related to MathML).

You can use some DBMS (from sqlite to PostgresQL, MongoDB, MariaDB/MySQL) to store your formulas (as TEXT or BLOB in SQL sense), you’ll be able to store millions or billions of them (e.g. have some SQL TABLE containing them, one row per formula).

Don’t forget to document the format of your persistent data, and the form of the “expressions” and/or “closures” in it. Even your advanced users could need to know that format and use it in some other way without having to reverse-engineer it.

Perhaps you are re-inventing business rule management systems or expert systems (look into CLIPS & JESS) or some scripting language (then perhaps try to use an existing one, not invent your own) or spreadsheets …

I don’t see any risk in storing your formulas as text, provided you define and document very well the syntax of your formulas (e.g. using EBNF) and their semantics (e.g. an operational semantics); publish that documentation (advanced users would need to know about them). Parsing efficiently short formulas is not a real issue (the disk I/O or database access would probably be the bottleneck, more than the parsing of the formula) : you can write a simple recursive descent parser or use a parsing expression grammar. Evaluating a formula is more difficult (and requires a semantics, and some evaluation strategy). You’ll need some environment to deal with your variables and their binding! Read also about higher-order abstract syntax. Don’t forget to validate a formula before storing it.

You might even take a metaprogramming approach and translate these formalas into a routine (or a method, or a class, etc…) or something runnable…

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật