Logical Expressions rules in relational datamodel

◇◆丶佛笑我妖孽 提交于 2019-12-01 12:37:21

Such an expression is a tree. Your example, expressed as a tree, is

=
   c
   +
      a
      *
         b
         d

The leaves of the tree are the primitive symbols a, b, ..., while the roots of the (sub)trees are the operators.

A simple relational database structure to represent such trees is

node(id, operator, left_component_id, right_component_id, primitive)

where, in case of a subtree, the operator and the foreign keys for the component nodes will be filled, whereas in case of a primitive, the last attribute will be filled.

If the arity, i.e. the count of arguments, of your operators is high or even unlimited, the schema will get more complicated. You'll need a separate table

argument(id, node_id, position, component_id)

that carries the arguments of the referenced node.

These schemes give you the full power of relational databases. E.g., you can query how many of your expressions have "a" as first argument. On the other hand, a simple expression will get scattered across a lot of database records that way. If you don't need the mechanisms of the database to inspect the inner structure of your expressions, you could just store the entire expression, as a string, in a single record.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!