Libraryless. Click here for Pure Java version (20104L/149K).
1 | !7 |
2 | |
3 | concept Rule {
|
4 | S globalID = aGlobalIDUnlessLoading(); |
5 | S text; |
6 | S comments; // used by rule engine |
7 | S notes; // for humans only |
8 | |
9 | toString {
|
10 | ret globalID + ": " + escapeNewLines(rtrim(text)); |
11 | } |
12 | |
13 | void change {
|
14 | super.change(); |
15 | vmBus_send('gazelleRuleChanged, this);
|
16 | } |
17 | |
18 | void delete {
|
19 | vmBus_send('gazelleRuleDeleted, this);
|
20 | super.delete(); |
21 | } |
22 | } |
23 | |
24 | // Bug when making cmodule |
25 | module Rules > DynCRUD<Rule> {
|
26 | transient CalculatedConceptFieldIndex<Rule, S> byPurpose; |
27 | |
28 | enhanceFrame { internalFrameIcon(f, gazelle_frameIcon()); }
|
29 | |
30 | start {
|
31 | dbIndexing(Rule, 'globalID); |
32 | byPurpose = new CalculatedConceptFieldIndex<Rule, S>(Rule, r -> gazelle_purposeFromComments(r.comments)); |
33 | |
34 | addCountToName(); |
35 | crud.multiLineFields = litset('text, 'comments);
|
36 | crud.dontDuplicateFields = litset('globalID);
|
37 | crud.renderer = func(Rule r) -> Map { litorderedmap("Rule ID" := r.globalID, text := joinWithSpace(tlft(r.text)), comments := escapeNewLines(r.comments)) };
|
38 | crud.latestFirst = true; |
39 | onConceptsChange(r { vmBus_sendMessage('gazelleRulesChanged, this) });
|
40 | conceptsObject().onAllChanged.add(r {
|
41 | vmBus_sendMessage('gazelleRulesGlobalChange, this)
|
42 | }); |
43 | |
44 | // do some logic stuff here for now |
45 | dm_vmBus_onMessage_q('gazelleRuleCreated, voidfunc(O mod, O rule) {
|
46 | S ruleID = getString globalID(rule); |
47 | print("wiring statement conditions for new rule: " + ruleID);
|
48 | dm_gazelle_wireStatementConditions(ruleID); |
49 | }); |
50 | } |
51 | |
52 | // API |
53 | |
54 | Pair<S, Bool> addRule(S when, S then) {
|
55 | ret addRule(when + "\n=> " + then); |
56 | } |
57 | |
58 | Pair<S, Bool> addRule(S text) {
|
59 | ret addRuleWithComment(text, null); |
60 | } |
61 | |
62 | Pair<S, Bool> addRuleWithComment(S text, S comment) {
|
63 | Pair<Rule, Bool> p = uniq2_sync(Rule, |
64 | text := rtrim(text), |
65 | comments := nullIfEmpty(rtrim(comment))); |
66 | if (p.b) vmBus_sendMessage('gazelleRuleCreated, this, p.a);
|
67 | ret pair(p.a.globalID, p.b); |
68 | } |
69 | |
70 | PairS textAndCommentForRule(S ruleID) {
|
71 | Rule r = conceptWhere Rule(globalID := ruleID); |
72 | ret r == null ? null : pair(r.text, r.comments); |
73 | } |
74 | |
75 | void setRuleText(S ruleID, S text) {
|
76 | cset(conceptWhere Rule(globalID := ruleID), +text); |
77 | } |
78 | |
79 | void setComments(S ruleID, S comments) {
|
80 | cset(conceptWhere Rule(globalID := ruleID), +comments); |
81 | } |
82 | |
83 | void addComments(S ruleID, S comments) {
|
84 | Rule r = conceptWhere Rule(globalID := ruleID); |
85 | if (r != null) |
86 | cset(r, comments := lines_rtrim(uniquifyList(concatLists(lines(r.comments), tlft(comments))))); |
87 | } |
88 | |
89 | void removeComment(S ruleID, S comment) {
|
90 | Rule r = conceptWhere Rule(globalID := ruleID); |
91 | if (r == null) ret; |
92 | cset(r, comments := lines_rtrim(listWithout(lines(r.comments), comment))); |
93 | } |
94 | |
95 | S latestRuleID() {
|
96 | ret getString globalID(last(concepts())); |
97 | } |
98 | |
99 | void deleteRule(S ruleID) {
|
100 | deleteConceptsWhere Rule(globalID := ruleID); |
101 | } |
102 | |
103 | void activateRule(S ruleID) {
|
104 | dm_activateOSAndModule(); // TODO: wait |
105 | if (crud.table == null) ret; |
106 | Rule r = conceptWhere Rule(globalID := ruleID); |
107 | if (r == null) ret; |
108 | selectRowIfNotNegative(crud.table, crud.indexOfConcept(r)); |
109 | } |
110 | |
111 | // experimental |
112 | L<Rule> rulesForPurpose(S purpose) {
|
113 | ret byPurpose.getAll(purpose); |
114 | } |
115 | |
116 | LS rulesWithText(S text) {
|
117 | ret collect globalID(conceptsWhere Rule(+text)); |
118 | } |
119 | } |
Began life as a copy of #1019783
download show line numbers debug dex old transpilations
Travelled to 7 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1021352 |
| Snippet name: | Gazelle: Rules [Dyn Module] |
| Eternal ID of this version: | #1021352/50 |
| Text MD5: | f25997742a76c607f256db92c6db1f98 |
| Transpilation MD5: | b09ec3aab6f5fab1994d49a06abb0d89 |
| Author: | stefan |
| Category: | javax / a.i. |
| Type: | JavaX source code (Dynamic Module) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2019-03-22 15:10:39 |
| Source code size: | 3492 bytes / 119 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 734 / 7399 |
| Version history: | 49 change(s) |
| Referenced in: | [show references] |