1
- // Copyright 2007-2008 The Apache Software Foundation.
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4
- // this file except in compliance with the License. You may obtain a copy of the
5
- // License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software distributed
10
- // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11
- // CONDITIONS OF ANY KIND, either express or implied. See the License for the
12
- // specific language governing permissions and limitations under the License.
13
- namespace Magnum . RulesEngine . Specs . FluentRuleSyntax
14
- {
15
- using System . Diagnostics ;
16
- using Conditions ;
17
- using Consequences ;
18
- using DSL ;
19
- using Events ;
20
- using NUnit . Framework ;
21
- using Visualizers ;
22
-
23
- [ TestFixture ]
24
- public class When_declaring_a_rule_using_the_domain_specific_language
25
- {
26
- [ SetUp ]
27
- public void Setup ( )
28
- {
29
- _engine = new MagnumRulesEngine ( ) ;
30
- }
31
-
32
- [ TearDown ]
33
- public void Teardown ( )
34
- {
35
- RulesEngineDebugVisualizer . TestShowVisualizer ( _engine ) ;
36
- }
37
-
38
- private RulesEngine _engine ;
39
-
40
- [ Test ]
41
- public void Should_be_able_to_add_pure_expressions_as_well_for_ease ( )
42
- {
43
- RuleSet ruleSet = Rule . Declare < OrderSubmitted > ( rule =>
44
- {
45
- rule . Then ( x => Trace . WriteLine ( "Order Amount: " + x . Object . Amount ) ) ;
46
-
47
- rule . When ( x => x . Amount > 100.00m )
48
- . Then ( x => Trace . WriteLine ( "Order is over the limit" ) )
49
- . Then ( x => x . RequestApproval ( ) ) ;
50
- } ) ;
51
-
52
- _engine . Add ( ruleSet ) ;
53
-
54
- using ( StatefulSession session = _engine . CreateSession ( ) )
55
- {
56
- session . Assert ( new OrderSubmitted { Amount = 123.45m } ) ;
57
-
58
- session . Run ( ) ;
59
- }
60
- }
61
-
62
- [ Test ]
63
- public void Should_be_able_to_add_them_into_a_set_of_rules ( )
64
- {
65
- RuleSet ruleSet = Rule . Declare < OrderSubmitted > ( rule =>
66
- {
67
- rule . Then < LogOrderDetails > ( ) ;
68
-
69
- rule . When < OrderAmountIsOverLimit > ( )
70
- . Then < RequestOrderApproval > ( ) ;
71
- } ) ;
72
-
73
- _engine . Add ( ruleSet ) ;
74
-
75
- using ( StatefulSession session = _engine . CreateSession ( ) )
76
- {
77
- session . Assert ( new OrderSubmitted { Amount = 123.45m } ) ;
78
-
79
- session . Run ( ) ;
80
- }
81
- }
82
- }
83
-
84
- public static class Extensions
85
- {
86
- public static void RequestApproval ( this RuleContext < OrderSubmitted > order )
87
- {
88
- }
89
- }
1
+ // Copyright 2007-2008 The Apache Software Foundation.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4
+ // this file except in compliance with the License. You may obtain a copy of the
5
+ // License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software distributed
10
+ // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
12
+ // specific language governing permissions and limitations under the License.
13
+ namespace Magnum . RulesEngine . Specs . FluentRuleSyntax
14
+ {
15
+ using System . Diagnostics ;
16
+ using Conditions ;
17
+ using Consequences ;
18
+ using DSL ;
19
+ using Events ;
20
+ using NUnit . Framework ;
21
+ using Visualizers ;
22
+
23
+ [ TestFixture ]
24
+ public class When_declaring_a_rule_using_the_domain_specific_language
25
+ {
26
+ [ SetUp ]
27
+ public void Setup ( )
28
+ {
29
+ _engine = new MagnumRulesEngine ( ) ;
30
+ }
31
+
32
+ [ TearDown ]
33
+ public void Teardown ( )
34
+ {
35
+ RulesEngineDebugVisualizer . TestShowVisualizer ( _engine ) ;
36
+ }
37
+
38
+ private RulesEngine _engine ;
39
+
40
+ [ Test ]
41
+ public void Should_be_able_to_add_pure_expressions_as_well_for_ease ( )
42
+ {
43
+ RuleSet ruleSet = Rule . Declare < OrderSubmitted > ( rule =>
44
+ {
45
+ rule . Then ( x => Trace . WriteLine ( "Order Amount: " + x . Object . Amount ) ) ;
46
+
47
+ rule . When ( x => x . Amount > 100.00m )
48
+ . Then ( x => Trace . WriteLine ( "Order is over the limit" ) )
49
+ . Then ( x => x . RequestApproval ( ) ) ;
50
+
51
+ rule . When ( x => x . Amount > 1000m )
52
+ . Then ( x => x . RequestClearance ( ) ) ;
53
+ } ) ;
54
+
55
+ // RuleSet ruleSet2 = Rule.Declare<OrderSubmitted>(rule =>
56
+ // {
57
+ // rule.When(x => x.Amount > 100.00m)
58
+ // .Then(x => Trace.WriteLine("BING"));
59
+ // });
60
+
61
+
62
+ _engine . Add ( ruleSet ) ;
63
+ // _engine.Add(ruleSet2);
64
+
65
+ using ( StatefulSession session = _engine . CreateSession ( ) )
66
+ {
67
+ session . Assert ( new OrderSubmitted { Amount = 123.45m } ) ;
68
+
69
+ session . Run ( ) ;
70
+ }
71
+ }
72
+
73
+ [ Test ]
74
+ public void Should_be_able_to_add_them_into_a_set_of_rules ( )
75
+ {
76
+ RuleSet ruleSet = Rule . Declare < OrderSubmitted > ( rule =>
77
+ {
78
+ rule . Then < LogOrderDetails > ( ) ;
79
+
80
+ rule . When < OrderAmountIsOverLimit > ( )
81
+ . Then < RequestOrderApproval > ( ) ;
82
+ } ) ;
83
+
84
+ _engine . Add ( ruleSet ) ;
85
+
86
+ using ( StatefulSession session = _engine . CreateSession ( ) )
87
+ {
88
+ session . Assert ( new OrderSubmitted { Amount = 123.45m } ) ;
89
+
90
+ session . Run ( ) ;
91
+ }
92
+ }
93
+ }
94
+
95
+ public static class Extensions
96
+ {
97
+ public static void RequestApproval ( this RuleContext < OrderSubmitted > order )
98
+ {
99
+ }
100
+ public static void RequestClearance ( this RuleContext < OrderSubmitted > order )
101
+ {
102
+ }
103
+ }
90
104
}
0 commit comments