-
Notifications
You must be signed in to change notification settings - Fork 2
/
NestedPolicyEngineExample.cs
97 lines (89 loc) · 4.21 KB
/
NestedPolicyEngineExample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using Atrea.PolicyEngine.Builders;
using Atrea.PolicyEngine.Examples.Mocks.Domain;
using Atrea.PolicyEngine.Policies.Input;
namespace Atrea.PolicyEngine.Examples.Examples;
public class NestedPolicyEngineExample : BaseExample
{
public override void Run()
{
var canadianFrenchTranslationEngine = BuildCanadianFrenchTranslationEngine();
var englishTranslationEngine = BuildEnglishTranslationEngine();
var numericTranslationEngine = BuildNumericTranslationEngine();
var aggregateTranslationEngine = PolicyEngineBuilder<TranslatableItem>
.Configure()
// Only process items which have not yet been translated.
.WithInputPolicies(NotYetTranslated)
.WithProcessors(
// Use the Canadian French, English, and Numeric translation engines to
// perform translations.
canadianFrenchTranslationEngine,
englishTranslationEngine,
numericTranslationEngine
)
// No output policies needed, since each individual engine handles its own
// post-processing steps.
.WithOutputPolicies()
.Build();
var translatableItem = new TranslatableItem();
aggregateTranslationEngine.Process(translatableItem);
}
private static IPolicyEngine<TranslatableItem> BuildCanadianFrenchTranslationEngine() =>
PolicyEngineBuilder<TranslatableItem>
.Configure()
.WithInputPolicies(
// Only process items which have not yet been translated and are translations
// that are either from Canadian French XOR to Canadian French.
NotYetTranslated,
FromCanadianFrench.Xor(ToCanadianFrench)
).WithProcessors(
// Use the GoogleTranslator, MicrosoftTranslator, and CacheTranslator to perform
// translations.
GoogleTranslator,
MicrosoftTranslator,
CacheTranslator
).WithOutputPolicies(
// After processing, publish the translation, mark the item as translated, and
// send a translation success email to the user who requested it.
PublishTranslation,
MarkItemTranslated,
SendTranslationSuccessEmail
)
.Build();
private static IPolicyEngine<TranslatableItem> BuildEnglishTranslationEngine() =>
PolicyEngineBuilder<TranslatableItem>
.Configure()
.WithInputPolicies(
// Only process items which have not yet been translated and are translations
// that are from US English AND to UK English, OR are from UK English AND to US English.
NotYetTranslated,
FromUsEnglish.And(ToUkEnglish).Or(FromUkEnglish.And(ToUsEnglish))
).WithProcessors(
// Use the SingleWordTranslator and DictionaryTranslator to perform translations.
SingleWordTranslator,
DictionaryTranslator
).WithOutputPolicies(
// After processing, publish the translation, mark the item as translated, and
// send a translation success email to the user who requested it.
PublishTranslation,
MarkItemTranslated
)
.Build();
private static IPolicyEngine<TranslatableItem> BuildNumericTranslationEngine() =>
PolicyEngineBuilder<TranslatableItem>
.Configure()
.WithInputPolicies(
// Only process items which have not yet been translated and are translations which
// contain numeric text.
NotYetTranslated,
ContainsNumericText
).WithProcessors(
// Use the SingleWordTranslator to perform translations.
SingleWordTranslator
).WithOutputPolicies(
// After processing, publish the translation, mark the item as translated, and
// send a translation success email to the user who requested it.
PublishTranslation,
MarkItemTranslated
)
.Build();
}