<p>Hi,</p>
<p>I'm porting a php application to GoLang. </p>
<p>I'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->create(
$rb->logicalAnd(
$rb['minNumPeople']->lessThanOrEqualTo($rb['actualNumPeople']),
$rb['maxNumPeople']->greaterThanOrEqualTo($rb['actualNumPeople'])
),
function() {
echo 'YAY!';
}
);
$context = new Context(array(
'minNumPeople' => 5,
'maxNumPeople' => 25,
'actualNumPeople' => function() {
return 6;
},
));
$rule->execute($context); // "Yay!"
</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 <= actualNumPeople() && maxNumPeople >= actualNumPeople() {
fmt.Println("Yay!")
}
}
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'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'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'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's silly to use code examples of rules engines. Of <em>course</em> it'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 "rule building blocks" 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 "func SayYayRule2" or "func newSayYayRule" 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 "func SayYayRule2" or "func newSayYayRule" 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's going to be the same issue whether it's stored in a file (SayYayRule.yml, SayYayRule2.yml) or as functions. Organization! :)</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传