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

202
LINES

< > BotCompany Repo | #1002871 // Edit

JavaX source code - run with: x30.jar

1  
!759
2  
3  
static Map<S, S> theories; // lower case name -> text
4  
static new MultiMap<S, S> signMap;
5  
6  
p {
7  
  theories2 = new PersistentMap("theories2");
8  
  if (theories2.isEmpty()) {
9  
    Map<S, S> theoriesOld = new PersistentMap("theories");
10  
    for (S name : keys(theoriesOld))
11  
      theories.put(name.toLowerCase(), theoriesOld.get(name));
12  
  }
13  
  load("signMap");
14  
}
15  
16  
static synchronized L<S> getTheoryNames() {
17  
  ret new ArrayList(theories.keySet());
18  
}
19  
20  
static synchronized boolean hasTheory(S name) {
21  
  ret theories.containsKey(name.toLowerCase());
22  
}
23  
24  
static synchronized S getTheoryOpt(S name) {
25  
  ret theories.get(name.toLowerCase());
26  
}
27  
28  
static synchronized S getTheory(S name) {
29  
  S text = getTheoryOpt(name);
30  
  if (text == null) fail("Theory * not found", name);
31  
  ret text;
32  
}
33  
34  
static synchronized L<S> getTheoriesSignedBy(S user) {
35  
  ret signMap.reverseGet(user);
36  
}
37  
38  
static synchronized void addTheory(S name, S text) {
39  
  if (hasTheory(name)) fail("Theory * exists", name);
40  
  theories.put(name, text);
41  
}
42  
43  
synchronized answer {
44  
  //if (!attn()) ret null;
45  
  
46  
  if "count theories"
47  
    ret l(theories);
48  
    
49  
  if "list theories"
50  
    ret structure(keys(theories));
51  
    
52  
  if "show theory *" exceptionToUser {
53  
    ret showTheories(litlist(m.unq(0)));
54  
  }
55  
  
56  
  if (matchStart("show theories", s, m)) exceptionToUser {
57  
    ret showTheories(codeTokensOnly(javaTok(m.rest())));
58  
  }
59  
  
60  
  if "modify theory * *" exceptionToUser {
61  
    S name = m.unq(0), text = m.unq(1);
62  
    ret saveTheory(name, text);
63  
  }
64  
  
65  
  if "add theory * *" exceptionToUser {
66  
    S name = m.unq(0), text = m.unq(1);
67  
    addTheory(name, text);
68  
    //boolean parses = doesTheoryParse(name);
69  
    //ret format(parses ? "OK, added theory *" : "Added theory *. Warning: Doesn't parse", name);
70  
    ret format("OK, added theory *", name);
71  
  }
72  
  
73  
  if "sign theory *" exceptionToUser {
74  
    S user = getUserName();
75  
    if (empty(user)) ret "go to slack please";
76  
    S name = m.unq(0);
77  
    if (!hasTheory(name)) ret "Theory not found";
78  
    L<S> users = signMap.get(name);
79  
    if (users.contains(user))
80  
      ret "You have already signed this theory!";
81  
    signMap.put(name, user);
82  
    save("signMap");
83  
    ret "OK, signed! All signers: " + structure(signMap.get(name));
84  
  }
85  
  
86  
  if "unsign theory *" exceptionToUser {
87  
    S user = getUserName();
88  
    if (empty(user)) ret "go to slack please";
89  
    S name = m.unq(0);
90  
    L<S> users = signMap.get(name);
91  
    if (!users.contains(user)) {
92  
      if (!hasTheory(name)) ret "Theory not found";
93  
      ret "You have not signed this theory.";
94  
    }
95  
    signMap.remove(name, user);
96  
    save("signMap");
97  
    ret "OK, unsigned! Remaining signers: " + structure(signMap.get(name));
98  
  }
99  
  
100  
  if "my signed theories" exceptionToUser {
101  
    S user = getUserName();
102  
    if (empty(user)) ret "go to slack please";
103  
    ret structure(signMap.reverseGet(user));
104  
  }
105  
106  
  if "theories signed by *" exceptionToUser {
107  
    S user = m.unq(0);
108  
    ret structure(signMap.reverseGet(user));
109  
  }
110  
  
111  
  if (match("theories page", s) || match("theories url", s))
112  
    ret getBotURL();
113  
114  
  if (!master()) ret null;
115  
  
116  
  if "delete theory *" exceptionToUser {
117  
    S name = toLowerCase(m.unq(0));
118  
    if (hasTheory(name)) {
119  
      S text = theories.get(name);
120  
      logMap("deleted", "name", name, "text", text, "time", now(), "signers", signMap.get(name));
121  
      logMap("deleted", "name", name, "signers", signMap.get(name));
122  
      theories.remove(name);
123  
      
124  
      signMap.remove(name);
125  
      save("signMap");
126  
      
127  
      ret format("Theory * backed up & deleted", name);
128  
    } else
129  
      ret format("Theory * not found", name);
130  
  }
131  
  
132  
  if "rename theory * to *" exceptionToUser {
133  
    S name = toLowerCase(m.unq(0)), newName = toLowerCase(m.unq(1));
134  
    if (hasTheory(newName))
135  
      ret format("A theory named * already exists", newName);
136  
    if (hasTheory(name)) {
137  
      S text = theories.get(name);
138  
      theories.put(newName, text);
139  
      theories.remove(name);
140  
141  
      // update signMap
142  
      L<S> signers = signMap.get(name);
143  
      signMap.addAll(newName, signers);
144  
      signMap.remove(name);
145  
      save("signMap");
146  
      
147  
      ret format("Theory * renamed to *", name, newName);
148  
    } else
149  
      ret format("Theory * not found", name);
150  
  }
151  
}
152  
153  
static S showTheories(L<S> theoryNames) {
154  
  new StringBuilder buf;
155  
  for (S name: theoryNames) {
156  
    if (!hasTheory(name))
157  
      buf.append(format("Theory * not found\n", name));
158  
    else {
159  
      //boolean parses = doesTheoryParse(name);
160  
      buf.append(quote(name) /* + " (" + (parses ? "PARSES" : "DOES NOT PARSE") + ")\n"*/ + "\n" + slackSnippet(theories.get(name)) + "\n\n");
161  
    }
162  
  }
163  
  ret str(buf).trim();
164  
}
165  
166  
static S html() {
167  
  new L data;
168  
  for (S name : theories.keySet()) {
169  
    S text = unnull(theories.get(name));
170  
    boolean multi = false;
171  
    Exception error = null;
172  
    try {
173  
      multi = nlIsMultipleStatements(text);
174  
    } catch (Exception e) {
175  
      error = e;
176  
    }
177  
    Map map = litmap(
178  
      "Name", htmlencode(name),
179  
      "Theory", pre(htmlencode(minitrim(rtrim(text)))),
180  
      "Signed by", htmlencode(structure(signMap.get(name))),
181  
      "Multiple rules?", multi);
182  
    if (error != null)
183  
      map.put("Parse Error", str(error));
184  
    data.add(map);
185  
  }
186  
  ret h3("Theories!") + htmlTable(data, false);
187  
}
188  
189  
// preserves formatting of first line
190  
static S minitrim(S s) {
191  
  L<S> l = toLines(s);
192  
  while (!empty(l) && empty(trim(first(l))))
193  
    l.remove(0);
194  
  ret autoUnindent(fromLines(l));
195  
}
196  
197  
static S saveTheory(S name, S text) {
198  
  boolean has = hasTheory(name);
199  
  logMap(has ? "modified" : "added", "name", name, "text", text, "time", now());
200  
  theories.put(name, text);
201  
  ret format((has ? "OK, modified theory *" : "OK, added theory *"), name);
202  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1002871
Snippet name: Edit
Eternal ID of this version: #1002871/1
Text MD5: 0006c6971c10ecb627be0a538abf87b4
Author: stefan
Category: eleu / nl / prolog
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-03-10 18:48:53
Source code size: 5887 bytes / 202 lines
Pitched / IR pitched: No / No
Views / Downloads: 466 / 430
Referenced in: [show references]