A failed attempt at creating a Rules Engine DSL.

blov · · 851 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Recently for a Go project I needed something similar to <a href="https://www.drools.org/">https://www.drools.org/</a>. I looked around and found a few with the general idea of rules matching: <a href="https://golanglibs.com/search?q=rules+engine">https://golanglibs.com/search?q=rules+engine</a>. But didn&#39;t find anything which would allow us to express the complex rules logic we had. Naturally, I thought of creating my own DSL, but that didn&#39;t really fit the time frame.</p> <p>After a bit of head banging, I decided to use the text/template package as a base to hack out a psuedo rules dsl. It worked out OK! But for a really complex business logic( am not at liberty to share it), the code became illegible and required a steep learning curve to understand it. This is not good for a business rules logic DSL since it needs to be understood by people with varied technical skills.</p> <p>The resultant work is hosted at: <a href="https://github.com/myntra/roulette">https://github.com/myntra/roulette</a> . </p> <p>A typical rules file: <a href="https://github.com/myntra/roulette/blob/master/testrules/rules_advanced.xml">https://github.com/myntra/roulette/blob/master/testrules/rules_advanced.xml</a></p> <p>More rule files: <a href="https://github.com/myntra/roulette/tree/master/testrules">https://github.com/myntra/roulette/tree/master/testrules</a>.</p> <p>Ultimately we decided to not use the roulette package and wrote the rules logic in pure Go instead(with a simple json file). It seems to me a full-fledged DSL like Drools : <a href="https://docs.jboss.org/drools/release/5.6.0.Final/drools-expert-docs/html_single/">https://docs.jboss.org/drools/release/5.6.0.Final/drools-expert-docs/html_single/</a> can&#39;t be avoided. But before I go deep diving into ASTs, parsers and lexers, I thought I would go get feedback from <a href="/r/golang">r/golang</a>, of ways to solve this problem or any existing work which I can reference to. Thanks in advance.</p> <p>[edit: typos]</p> <hr/>**评论:**<br/><br/>PaluMacil: <pre><p>I work with someone who likes to talk about how we shouldn&#39;t use Salesforce because a rules engine would be so easy to write. If management ever believes him, it better be him assigned to it and not me...</p></pre>3bodyproblemsolver: <pre><p>Haha. Don&#39;t see how writing rules engine can be easy to develop. Maybe for trivial use cases.</p></pre>hgfischer: <pre><p>Maybe you can check Drolls source code to have ideas on how you can implement it.</p> <p>I am afraid you will not be able to avoid going into AST, parsers, etc.</p> <p>Or maybe you can do something using existing Lua or JavaScript parsers made in pure Go.</p></pre>3bodyproblemsolver: <pre><p>Thats the path which looks likely . Drools has a ebnf file which I will port to Go.</p> <p><a href="https://github.com/kiegroup/drools/blob/5.6.x/drools-docs/drools-expert-docs/src/main/docbook/en-US/Chapter-LanguageReference/drl.ebnf" rel="nofollow">https://github.com/kiegroup/drools/blob/5.6.x/drools-docs/drools-expert-docs/src/main/docbook/en-US/Chapter-LanguageReference/drl.ebnf</a></p></pre>3bodyproblemsolver: <pre><p>As an inspiration, I have been looking into <a href="https://github.com/mattn/anko" rel="nofollow">https://github.com/mattn/anko</a></p></pre>comrade_donkey: <pre><p>There&#39;s a couple of theoretical points to keep in mind:</p> <ul> <li>A &#34;rule&#34; is a <a href="https://en.wikipedia.org/wiki/Declarative_programming" rel="nofollow">declarative program</a>.</li> <li>A rule language is translated into an automaton (e.g. a <a href="https://en.wikipedia.org/wiki/Finite-state_machine" rel="nofollow">finite state machine</a> or <a href="https://en.wikipedia.org/wiki/Pushdown_automaton" rel="nofollow">pushdown automaton</a>).</li> </ul> <p>A naïve unbounded rule language may become turing complete, at which point its halting is impossible to prove i.e. it may run forever (and that&#39;s bad for hosted execution). So keep the power of the automaton bounded.</p> <p>Furthermore, you don&#39;t need to define a new language for your rule engine. You can embed your semantics into an existing format (e.g. JSON or XML) and compile your automaton from that.</p></pre>3bodyproblemsolver: <pre><p>That&#39;s what I have done now. Though it works, the code is incomprehensible to the point of being faulty.</p></pre>WikiTextBot: <pre><p><strong>Declarative programming</strong></p> <p>In computer science, declarative programming is a programming paradigm—a style of building the structure and elements of computer programs—that expresses the logic of a computation without describing its control flow.</p> <p>Many languages that apply this style attempt to minimize or eliminate side effects by describing what the program must accomplish in terms of the problem domain, rather than describe how to accomplish it as a sequence of the programming language primitives (the how being left up to the language&#39;s implementation). This is in contrast with imperative programming, which implements algorithms in explicit steps.</p> <p>Declarative programming often considers programs as theories of a formal logic, and computations as deductions in that logic space.</p> <hr/> <p><strong>Finite-state machine</strong></p> <p>A finite-state machine (FSM) or finite-state automaton (FSA, plural: automata), finite automaton, or simply a state machine, is a mathematical model of computation. It is an abstract machine that can be in exactly one of a finite number of states at any given time. The FSM can change from one state to another in response to some external inputs; the change from one state to another is called a transition. An FSM is defined by a list of its states, its initial state, and the conditions for each transition.</p> <hr/> <p><strong>Pushdown automaton</strong></p> <p>In computer science, a pushdown automaton (PDA) is a type of automaton that employs a stack.</p> <p>Pushdown automata are used in theories about what can be computed by machines. They are more capable than finite-state machines but less capable than Turing machines. Deterministic pushdown automata can recognize all deterministic context-free languages while nondeterministic ones can recognize all context-free languages, with the former often used in parser design.</p> <hr/> <p><sup>[</sup> <a href="https://www.reddit.com/message/compose?to=kittens_from_space" rel="nofollow"><sup>PM</sup></a> <sup>|</sup> <a href="https://reddit.com/message/compose?to=WikiTextBot&amp;message=Excludeme&amp;subject=Excludeme" rel="nofollow"><sup>Exclude</sup> <sup>me</sup></a> <sup>|</sup> <a href="https://np.reddit.com/r/golang/about/banned" rel="nofollow"><sup>Exclude</sup> <sup>from</sup> <sup>subreddit</sup></a> <sup>|</sup> <a href="https://np.reddit.com/r/WikiTextBot/wiki/index" rel="nofollow"><sup>FAQ</sup> <sup>/</sup> <sup>Information</sup></a> <sup>|</sup> <a href="https://github.com/kittenswolf/WikiTextBot" rel="nofollow"><sup>Source</sup></a> <sup>]</sup> <sup>Downvote</sup> <sup>to</sup> <sup>remove</sup> <sup>|</sup> <sup>v0.24</sup></p></pre>tvmaly: <pre><p>Prolog is a perfect system for this. I have not seen any interface to SWI Prolog for Go, but there is a C api, so it would be possible.</p> <p>There is also golog a prolog interpreter in Go, but I do not think it is ISO yet</p> <p><a href="https://github.com/mndrix/golog" rel="nofollow">https://github.com/mndrix/golog</a></p></pre>3bodyproblemsolver: <pre><p>Actually, I did check this out. Didn&#39;t find the API user friendly so moved on. But I am going to look at it again and see if anything can be done about that. Thanks for the reminder!</p></pre>loganjspears: <pre><p>Check out <a href="https://github.com/Knetic/govaluate" rel="nofollow">https://github.com/Knetic/govaluate</a></p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

851 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传