Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

131
LINES

< > BotCompany Repo | #1027557 // Scenarios DB [backup v1]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 911K of libraries. Click here for Pure Java version (12423L/65K).

1  
!7
2  
3  
concept Scenario {
4  
  S name;
5  
  LS conditions;
6  
  LS actions;
7  
8  
  toString { ret "Scenario " + quote(name); }
9  
}
10  
11  
cmodule Scenarios > DynCRUD<Scenario> {
12  
  transient Scenario selected;
13  
  new L<Undo> undoList;
14  
15  
  abstract class Undo { abstract S undo(); }
16  
  
17  
  record UndoCreateConcept(long conceptID) > Undo {
18  
    toString { ret "Create " + getConcept(conceptID); }
19  
    
20  
    S undo() {
21  
      Concept c = getConcept(conceptID);
22  
      S result = "Deleted " + c + " again";
23  
      deleteConcept(conceptID);
24  
      ret result;
25  
    }
26  
  }
27  
28  
  record UndoDeleteConcept(Concept concept) > Undo {
29  
    toString { ret "Delete " + concept; }
30  
    
31  
    S undo() {
32  
      registerConcept(concept);
33  
      ret "Restored " + concept;
34  
    }
35  
  }
36  
37  
  record UndoSetConceptField(long conceptID, S field, O value, O newValue) > Undo {
38  
    toString { ret "Set " + field + " of " + getConcept(conceptID) + " to " + newValue; }
39  
    
40  
    S undo() {
41  
      Concept c = getConcept(conceptID);
42  
      S result = "Reset " + field + " of " + c + " to " + value;
43  
      cset(c, field, value);
44  
      ret result;
45  
    }
46  
  }
47  
48  
  Scenario findConceptWithName(S name) {
49  
    ret conceptWhereCI Scenario(+name);
50  
  }
51  
  
52  
  S answer(S s) {
53  
    ret dm_q(() -> {
54  
      print("answering: " + s);
55  
      new Matches m;
56  
      if "undo" ret singleUndo();
57  
      if "anything to undo"
58  
        ret empty(undoList) ? "Nothing to undo" : n2(undoList, "undoable action");
59  
      if "how many scenarios"
60  
        ret str(countConcepts(Scenario));
61  
      if "delete scenario *" {
62  
        S name = $1;
63  
        setField(selected := null);
64  
        Scenario sc = findConceptWithName(name);
65  
        if (sc == null)
66  
 ret format("Scenario * not found", name);
67  
        unregisterConcept(sc);
68  
        addUndo(new UndoDeleteConcept(sc));
69  
        ret format("Scenario * deleted", name);
70  
      }
71  
      if "new scenario *" {
72  
        S name = $1;
73  
        Scenario sc = conceptWhereCI Scenario(+name);
74  
        if (sc != null)
75  
          ret format("Scenario * exists", name);
76  
        setField(selected := uniqCI Scenario(+name));
77  
        addUndo(new UndoCreateConcept(selected.id));
78  
        ret format("Scenario * created", name);
79  
      }
80  
      if "list scenarios"
81  
        ret or2(joinWithComma(collect name(list(Scenario))), "No scenarios defined");
82  
      if "rename scenario * to *" {
83  
        S name1 = $1, name2 = $2;
84  
        if (!eqic(name1, name2)
85  
          && conceptWhereCI Scenario(name := name2) != null)
86  
          ret format("A scenario named * exists", name2);
87  
        Scenario sc = findConceptWithName(name1);
88  
        if (sc == null)
89  
          ret format("Scenario * not found", name1);
90  
        addUndo(new UndoSetConceptField(sc.id, 'name, name1, name2));
91  
        cset(sc, name := name2);
92  
        ret format("Scenario * renamed to *", name1, name2);
93  
      }
94  
95  
      null;
96  
    });
97  
  }
98  
99  
  void addUndo(Undo undo) {
100  
    if (undo == null) ret;
101  
    undoList.add(undo);
102  
    change();
103  
    print("Undo added: " + undo);
104  
  }
105  
106  
  afterVisualize {
107  
    addComponent(crud.buttons, jbutton("Talk to me", rThread talkToMe));
108  
  }
109  
110  
  void talkToMe enter { dm_showConversationPopupForModule(); }
111  
112  
  void clearUndos q {
113  
    if (empty(undoList)) ret;
114  
    undoList.clear();
115  
    change;
116  
    print("Undo list cleared");
117  
  }
118  
119  
  S singleUndo() {
120  
    Undo undo = popLast(undoList);
121  
    if (undo == null) ret "Nothing to undo";
122  
    change();
123  
    ret or2(undo.undo(), "Last action undone");
124  
  }
125  
126  
  // API
127  
128  
  Undo lastUndo() {
129  
    ret dm_q(() -> last(undoList));
130  
  }
131  
}

Author comment

Began life as a copy of #1027443

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1027557
Snippet name: Scenarios DB [backup v1]
Eternal ID of this version: #1027557/1
Text MD5: a329ddf7d732ca9a81e97dce13248725
Transpilation MD5: c2ea18628812f53776571337635023be
Author: stefan
Category: javax / scenarios
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-03-23 21:33:59
Source code size: 3595 bytes / 131 lines
Pitched / IR pitched: No / No
Views / Downloads: 159 / 237
Referenced in: [show references]