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

105
LINES

< > BotCompany Repo | #1027443 // Scenarios DB [use with frontend #1027518]

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

Uses 911K of libraries. Click here for Pure Java version (12429L/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<UndoableWithAnswer> undoList;
14  
15  
  Scenario findConceptWithName(S name) {
16  
    ret conceptWhereCI Scenario(+name);
17  
  }
18  
  
19  
  S answer(S s) {
20  
    ret dm_q(() -> {
21  
      print("answering: " + s);
22  
      new Matches m;
23  
24  
      // undo actions
25  
      
26  
      if "undo" ret singleUndo();
27  
      if "anything to undo"
28  
        ret empty(undoList) ? "Nothing to undo" : n2(undoList, "undoable action");
29  
      if "clear undos" ret "OK" with clearUndos();
30  
31  
      // scenario actions
32  
      
33  
      if "how many scenarios"
34  
        ret str(countConcepts(Scenario));
35  
      if "delete scenario *" {
36  
        S name = $1;
37  
        setField(selected := null);
38  
        Scenario sc = findConceptWithName(name);
39  
        if (sc == null)
40  
 ret format("Scenario * not found", name);
41  
        unregisterConcept(sc);
42  
        addUndo(new UndoDeleteConcept(sc));
43  
        ret format("Scenario * deleted", name);
44  
      }
45  
      if "new scenario *" {
46  
        S name = $1;
47  
        Scenario sc = conceptWhereCI Scenario(+name);
48  
        if (sc != null)
49  
          ret format("Scenario * exists", name);
50  
        setField(selected := uniqCI Scenario(+name));
51  
        addUndo(new UndoCreateConcept(selected.id));
52  
        ret format("Scenario * created", name);
53  
      }
54  
      if "list scenarios"
55  
        ret or2(joinWithComma(collect name(list(Scenario))), "No scenarios defined");
56  
      if "rename scenario * to *" {
57  
        S name1 = $1, name2 = $2;
58  
        if (!eqic(name1, name2)
59  
          && conceptWhereCI Scenario(name := name2) != null)
60  
          ret format("A scenario named * exists", name2);
61  
        Scenario sc = findConceptWithName(name1);
62  
        if (sc == null)
63  
          ret format("Scenario * not found", name1);
64  
        addUndo(new UndoSetConceptField(sc.id, 'name, name1, name2));
65  
        cset(sc, name := name2);
66  
        ret format("Scenario * renamed to *", name1, name2);
67  
      }
68  
69  
      null;
70  
    });
71  
  }
72  
73  
  void addUndo(UndoableWithAnswer undo) {
74  
    if (undo == null) ret;
75  
    undoList.add(undo);
76  
    change();
77  
    print("Undo added: " + undo);
78  
  }
79  
80  
  afterVisualize {
81  
    addComponent(crud.buttons, jbutton("Talk to me", rThread talkToMe));
82  
  }
83  
84  
  void talkToMe enter { dm_showConversationPopupForModule(); }
85  
86  
  void clearUndos q {
87  
    if (empty(undoList)) ret;
88  
    undoList.clear();
89  
    change;
90  
    print("Undo list cleared");
91  
  }
92  
93  
  S singleUndo() {
94  
    UndoableWithAnswer undo = popLast(undoList);
95  
    if (undo == null) ret "Nothing to undo";
96  
    change();
97  
    ret or2(undo.undo(), "Last action undone");
98  
  }
99  
100  
  // API
101  
102  
  UndoableWithAnswer lastUndo() {
103  
    ret dm_q(() -> last(undoList));
104  
  }
105  
}

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: #1027443
Snippet name: Scenarios DB [use with frontend #1027518]
Eternal ID of this version: #1027443/42
Text MD5: 5e5c3900cb875da4a38b605e4cb3d340
Transpilation MD5: c9ba4a68f26186b628fdc64563ea8847
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:38:23
Source code size: 2862 bytes / 105 lines
Pitched / IR pitched: No / No
Views / Downloads: 253 / 1076
Version history: 41 change(s)
Referenced in: [show references]