Rules Engine for Golang?

xuanbao · · 1816 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi,</p> <p>I&#39;m porting a php application to GoLang. </p> <p>I&#39;m using the rules engine from <a href="https://github.com/bobthecow/Ruler">https://github.com/bobthecow/Ruler</a> . You may have also seen <a href="http://hoa-project.net/En/Literature/Hack/Ruler.html">http://hoa-project.net/En/Literature/Hack/Ruler.html</a> .</p> <p>Is there something similar for GoLang?</p> <p>Thanks</p> <hr/>**评论:**<br/><br/>oleksandr123: <pre><p>You can take a look at this one: <a href="https://github.com/oleksandr/conditions">https://github.com/oleksandr/conditions</a></p></pre>no1youknowz: <pre><p>Awesome, thanks for replying! I have bookmarked this and will take a look!</p></pre>jreid42: <pre><p>Maybe I am missing the point of Ruler, but the example from the main page:</p> <pre><code>$rb = new RuleBuilder; $rule = $rb-&gt;create( $rb-&gt;logicalAnd( $rb[&#39;minNumPeople&#39;]-&gt;lessThanOrEqualTo($rb[&#39;actualNumPeople&#39;]), $rb[&#39;maxNumPeople&#39;]-&gt;greaterThanOrEqualTo($rb[&#39;actualNumPeople&#39;]) ), function() { echo &#39;YAY!&#39;; } ); $context = new Context(array( &#39;minNumPeople&#39; =&gt; 5, &#39;maxNumPeople&#39; =&gt; 25, &#39;actualNumPeople&#39; =&gt; function() { return 6; }, )); $rule-&gt;execute($context); // &#34;Yay!&#34; </code></pre> <p>Seems very complex why not just code it in go directly?</p> <p>Same example in golang (did not run it):</p> <pre><code>func getActualNumPeople() int { return 6 } func SayYayRule(minNumPeople int, maxNumPeople int, actualNumPeople func()int) { if minNumPeople &lt;= actualNumPeople() &amp;&amp; maxNumPeople &gt;= actualNumPeople() { fmt.Println(&#34;Yay!&#34;) } } SayYayRule(5, 25, getActualNumPeople) </code></pre></pre>ulfurinn: <pre><blockquote> <p>Seems very complex why not just code it in go directly?</p> </blockquote> <p>The kind of examples that truly require the use of a rule engine tend to be too complex to be good examples. That&#39;s why you always end up with some contrived nonsense.</p></pre>no1youknowz: <pre><p>Yes, 100% true. Unfortunately <a href="/u/jreid42" rel="nofollow">/u/jreid42</a> missed the point completely.</p></pre>jreid42: <pre><p>Yeah I still don&#39;t really get the point of it. Even if I make the situation 100x more complex I still feel that it will be more expressive in code.</p> <p>Could I see an example of something more complex? Genuinely curious.</p></pre>willglynn: <pre><p>The main reason to use a rules engine is when you can predict the <em>kinds</em> of logic that you&#39;re going to have to execute, but not the logic itself.</p> <p>An e-commerce platform might use a rules engine for promotion codes. The platform would define the environment necessary for the rules engine to operate (i.e. hooks to get information about an order, the customer, the date, …) and invoke it at the appropriate time. However, the platform would also provide an interface allowing administrators to define their own rules without needing to recompile and redeploy.</p> <p>This is why it&#39;s silly to use code examples of rules engines. Of <em>course</em> it&#39;s more verbose and less expressive than coding the rule directly. The key is that rules are a data structure, which means you can construct rules at runtime.</p></pre>jreid42: <pre><blockquote> <p>the platform would also provide an interface allowing administrators to define their own rules without needing to recompile and redeploy</p> </blockquote> <p>Thanks this is what I was missing from my thought process</p></pre>no1youknowz: <pre><p>Thanks. Up-voted :)</p></pre>jerklin: <pre><p>In my personal experience, the big advantage of something like the ruler rule builder is being able to store the rule configuration in a database or yaml file and then generating the rule dynamically. This makes it much easier to provide &#34;rule building blocks&#34; in a GUI and let an end user manipulate the structure.</p> <p>The code in your example may be faster to write but over time can create issues when business requirements change and you end up with a bunch of &#34;func SayYayRule2&#34; or &#34;func newSayYayRule&#34; code for slight variations of the original rule.</p></pre>jreid42: <pre><blockquote> <p>is being able to store the rule configuration in a database or yaml file and then generating the rule dynamically</p> </blockquote> <p>Yeah, this is what I was missing. This is the only use case that makes sense for this sort of thing (IMO)</p> <blockquote> <p>The code in your example may be faster to write but over time can create issues when business requirements change and you end up with a bunch of &#34;func SayYayRule2&#34; or &#34;func newSayYayRule&#34; code for slight variations of the original rule.</p> </blockquote> <p>If you have slight variations of rules that are running at the same time it&#39;s going to be the same issue whether it&#39;s stored in a file (SayYayRule.yml, SayYayRule2.yml) or as functions. Organization! :)</p></pre>

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

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