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

119
LINES

< > BotCompany Repo | #1001348 // Identity Manager (LIVE)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (1915L/13K/42K).

1  
!747
2  
3  
m {
4  
  static new Map<S, State> states;
5  
  static int nMaxHistory = 2;
6  
  
7  
  static class State {
8  
    S id;
9  
    S machineID;
10  
    S dataID;
11  
    long since;
12  
    S status;
13  
    State previous;
14  
  }
15  
  
16  
  p {
17  
    readLocally("states");
18  
    makeAndroid3("Identity Manager.");
19  
  }
20  
  
21  
  static synchronized S answer(S s, L<S> history) {
22  
    new Matches m;
23  
    
24  
    if (match3("* is now on machine * with data *", s, m)) {
25  
      S programID = formatSnippetID(unquote(m.m[0]));
26  
      S machineID = unquote(m.m[1]);
27  
      S dataID = formatSnippetID(unquote(m.m[2]));
28  
      new State state;
29  
      state.since = now();
30  
      state.machineID = machineID;
31  
      state.dataID = dataID;
32  
      state.status = "residing";
33  
      setState(programID, state);
34  
      return "OK";
35  
    }
36  
    
37  
    if (match3("* is now in state *", s, m)) {
38  
      S programID = formatSnippetID(unquote(m.m[0]));
39  
      new State state;
40  
      state.since = now();
41  
      State last = states.get(programID);
42  
      if (last != null) {
43  
        state.machineID = last.machineID;
44  
        state.dataID = last.dataID;
45  
      }
46  
      state.status = unquote(m.m[1]);
47  
      setState(programID, state);
48  
      return "OK";
49  
    }
50  
    
51  
    if (match3("* is now in state * with data * to machine *", s, m)) {
52  
      S programID = formatSnippetID(unquote(m.m[0]));
53  
      new State state;
54  
      state.since = now();
55  
      State last = states.get(programID);
56  
      state.machineID = unquote(m.m[3]);
57  
      state.status = unquote(m.m[1]);
58  
      state.dataID = formatSnippetID(unquote(m.m[2]));
59  
      setState(programID, state);
60  
      return "OK";
61  
    }
62  
    
63  
    if (match3("get state of *", s, m))
64  
      return structure(states.get(formatSnippetID(unquote(m.m[0]))));
65  
      
66  
    if (match3("what machine is * supposed to be on ", s, m)) {
67  
      State state = states.get(formatSnippetID(unquote(m.m[0])));
68  
      ret state == null ? "Sorry, no information on bot" : format3("this one: *", state.machineID);
69  
    }
70  
      
71  
    if (match3("list programs", s, m))
72  
      return structure(states.keySet());
73  
      
74  
    if (match3("who is moving to *?", s, m)) {
75  
      S machineID = unquote(m.m[0]);
76  
      return structure(ids(forStateAndMachine("moving", machineID)));
77  
    }
78  
    
79  
    if (match3("who is moving to * (full state)?", s, m)) {
80  
      S machineID = unquote(m.m[0]);
81  
      return structure(forStateAndMachine("moving", machineID));
82  
    }
83  
    
84  
    ret standardQuery(s, "states");
85  
  }
86  
  
87  
  static L<State> forStateAndMachine(S status, S machineID) {
88  
    new L<State> l;
89  
    for (State state : states.values()) {
90  
      if (eq(state.status, status) && eq(state.machineID, machineID))
91  
        l.add(state);
92  
    }
93  
    ret l;
94  
  }
95  
    
96  
  static L<S> ids(L<State> states) {
97  
    new L<S> ids;
98  
    for (State state : states)
99  
      ids.add(state.id);
100  
    ret ids;
101  
  }
102  
  
103  
  static void clearHistory(State state) {
104  
    for (int n = 0; n < nMaxHistory; n++) {
105  
      state = state.previous;
106  
      if (state == null)
107  
        return;
108  
    }
109  
    state.previous = null;
110  
  }
111  
112  
  static synchronized void setState(S programID, State state) {
113  
    state.id = programID;
114  
    state.previous = states.get(programID);
115  
    clearHistory(state);
116  
    states.put(programID, state);
117  
    saveLocally("states");
118  
  }
119  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1001348
Snippet name: Identity Manager (LIVE)
Eternal ID of this version: #1001348/1
Text MD5: 4aff58d7c01663ee5a7be77de8c38485
Transpilation MD5: 93eb0d5594f484081d126565b0e79adf
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-11-13 18:06:53
Source code size: 3330 bytes / 119 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 719 / 1000
Referenced in: [show references]