Warning : session_start(): open(/var/lib/php/sessions/sess_a3scd87qd147j5ci69flm9v0u8, O_RDWR) failed: No space left on device (28) in /var/www/tb-usercake/models/config.php on line 51
Warning : session_start(): Failed to read session data: files (path: /var/lib/php/sessions) in /var/www/tb-usercake/models/config.php on line 51
import java.util.*;
import java.util.zip.*;
import java.util.List;
import java.util.regex.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.table.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.lang.ref.*;
import java.lang.management.*;
import java.security.*;
import java.security.spec.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.math.*;
import java.text.*;
import java.text.NumberFormat;
import javax.net.ssl.*;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import javax.swing.undo.UndoManager;
import javax.imageio.metadata.*;
import javax.imageio.stream.*;
import java.text.SimpleDateFormat;
import org.apache.commons.io.input.ReversedLinesFileReader;
import java.awt.datatransfer.StringSelection;
import javax.swing.border.*;
import javax.swing.event.AncestorListener;
import javax.swing.event.AncestorEvent;
import javax.swing.Timer;
import java.nio.file.Files;
import java.nio.file.Path;
class main {
static class TelegramFactsBot extends AbstractTelegramThinkBot {
boolean debug;
boolean imagineMode;
boolean collectCheckingLog = true;
LinkedHashSet imaginedFacts = new LinkedHashSet();
List checkingLog;
long processingTime;
List activeTempRules = new ArrayList();
int evalTimeout = 20000;
boolean useIterate; // full iterate mode for normal rule checking
Object standardGrouper; // grouping function for all input
String ruleFired, lastRuleFired;
TelegramFactsBot() {
setMaxSizeOfMRUCache(parse3_cachedPattern_cache, 10000);
setMaxSizeOfMRUCache(parse3_cachedInput_cache, 10000);
}
void thinkAbout(String s) {
long _startTime_1 = sysNow();
thinkAbout_impl(s);
done2_always("Processing Time For Message", _startTime_1);
processingTime = lastTiming();
}
void thinkAbout_impl(final String input) {
final Thinking t = new Thinking();
final Map tl = getMCThreadLocals(ai_staticVars());
/*Thread thread = startThread("Think!", r {
setMCThreadLocals(tl);*/
t.thinkAbout(input);
/*});*/
int n = 0;
do {
while (l(t.output) > n)
postMessage(t.output.get(n++));
//joinThread(thread, 50);
} while (!t.done);
activeTempRules.clear();
lastRuleFired = ruleFired;
//print("lastRuleFired := " + lastRuleFired);
/*if (lastRuleFired != null) {
lastRuleFired = null;
print("lastRuleFired := null");
}*/
}
void _postMessage(String s) { postMessage(s); }
class Thinking {
long cancelTime = now()+20000;
Map ruleScores = new HashMap();
LinkedHashSet rewrittenInputs = new LinkedHashSet();
LinkedHashSet newRewrittenInputs = new LinkedHashSet();
volatile boolean posted, done;
List rules = new ArrayList();
PlanInMotion plan = new PlanInMotion();
HashSet outputSet = new HashSet();
List output = synchroList();
void postMessage(String s) {
//_postMessage(s);
if (outputSet.add(s))
output.add(s);
posted = true;
}
void done() { done = true; }
void thinkAbout(String input) {
try {
thinkAbout_impl(input);
} finally {
done();
}
}
void thinkAbout_impl(final String input0) {
if (ruleFired != null) {
ruleFired = null;
//print("ruleFired := null");
}
final String input1 = unixLineBreaks(input0);
plan.add(new Runnable() { public void run() { try {
if (!eq(input1, "!initiative")) return;
List rules = mL_parsedNLRulesWithoutIDs("Initiative rules");
for (IfThen rule : shuffledIterator(rules)) {
Pair p = nlLogic_extractFirstCondition(rule.in);
if (p == null) continue;
if (nlLogic_isFunc(p.a, "initiative")) {
postMessage(nlLogic_text(((Func) p.a).arg));
IfThen temp = new IfThen(p.b, rule.out);
temp.globalID = aGlobalID();
print("Have temp rule: " + sfu(temp));
activeTempRules.add(temp);
{ done(); return; }
}
}
{ done(); return; }
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "!initiative"; } public String _shortenedSourceCode() { return "if (!eq(input1, \"!initiative\")) return;\r\n List rules = mL_pars..."; }});
plan.add(new Runnable() { public void run() { try {
Matches mm = new Matches();
if (!startsWith_trim(input1, "!step ", mm)) return;
// TODO
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "!step"; } public String _shortenedSourceCode() { return "new Matches mm;\r\n if (!startsWith_trim(input1, \"!step \", mm)) return;\r..."; }});
runPlanWithCheck(plan, new F0() { Boolean get() { try { return !done; } catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "ret !done;"; }});
if (done) return;
stdCommands(input1);
String input2 = input1;
final List options = new ArrayList();
if (startsWith(input2, "[")) {
addAll(options, leadingSquareBracketOptions(input2));
input2 = dropLeadingSquareBracketStuff(input2);
}
final String input = callFOrKeep(standardGrouper, input2);
ai_tg_collectRuleFeedback(ruleScores, null);
newRewrittenInputs.add(input);
final Map msg = telegram_msg();
final NLLogicChecker_v3 c = new NLLogicChecker_v3() {
boolean checkExpression_impl(Exp e, Matching m) {
if (e instanceof Literal) {
// TEXT CONDITIONS
String text = nlLogic_text(e);
if (eq(text, "authorized"))
return telegram_amIAuthorized();
else if(eq(text, "imagineMode"))
return imagineMode;
}
return super.checkExpression_impl(e, m);
}
};
c.followingUpOn = lastRuleFired;
if (collectCheckingLog) {
checkingLog = new ArrayList();
nlLogic_collectCheckingLog(c, checkingLog);
}
Pair, List> rulesAndFacts = getRulesAndFacts();
c.facts = reversed(rulesAndFacts.b);
rules = concatLists_conservative(activeTempRules, reversed(rulesAndFacts.a)); // latest rules first!
final Set toDrop = asHashSet(matchAll_first("$x is not a general chat rule", c.facts));
rules = filter(rules , new F1() { Boolean get(IfThen r) { try { return !toDrop.contains(r.globalID)
&& ai_ruleAccessesInput(r); } catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "!toDrop.contains(r.globalID)\r\n && ai_ruleAccessesInput(r)"; }});
c.rules = rules;
while (!posted && now() < cancelTime && nempty(newRewrittenInputs)) {
final String s = first(newRewrittenInputs);
rewrittenInputs.add(s);
newRewrittenInputs.remove(s);
c.input = s;
c.recentHistory = recentHistory;
c.useIterate = useIterate;
final List battleSpace = new ArrayList();
nlLogic_fillBattleSpace(c, battleSpace, debug);
print("Have " + n2(battleSpace, "possible rule")
+ (empty(battleSpace) ? "" : ": " + joinWithComma(map(new F1() { String get(RuleWithParams r) { try { return r.ruleID() + "/" + toInt(ruleScores.get(r.ruleID())); } catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "r.ruleID() + \"/\" + toInt(ruleScores.get(r.ruleID()))"; }}, battleSpace))));
boolean all = options.contains("all");
if (!all) nlLogic_battleItOut(battleSpace, c.facts);
List winners = all ? battleSpace : nlLogic_highestScore(battleSpace, ruleScores);
final String msgGlobalID = getString(msg, "globalID");
//print("msgGlobalID=" + msgGlobalID);
for (RuleWithParams r : winners) {
print("Firing rule " + r.rule.globalID);
// Save rule fire
ruleFired = r.rule.globalID;
if (nempty(msgGlobalID) && nempty(r.rule.globalID)) {
String fact = format("Rule * fired on message * at *", r.rule.globalID, msgGlobalID, localDateWithMilliseconds());
if (nempty(r.matches))
fact += " with vars " + dropPrefix("lhm", struct(r.matches));
print("Saving rule fire. Mech mode: " + mechMode());
print(addToMechList("Telegram Rule Fires", print(fact)));
}
executeRule(c, r, this);
}
if (collectCheckingLog)
print("Checking log length: " + l(checkingLog));
}
}
} // end class Thinking
void executeRule(NLLogicChecker_v3 c, RuleWithParams r, Thinking t) {
// Execute rule
for (Exp e : nlLogic_unrollAnd(r.rule.out)) {
e = c.apply(e, r.matches);
if (e instanceof Func) {
String name = ((Func) e).name, arg = nlLogic_text(((Func) e).arg);
if (eqOneOf(name, "output", "say"))
t.postMessage(arg);
else if (eq(name, "fact")) {
String fact = arg;
if (imagineMode) {
if (!containsNL(imaginedFacts, fact)) {
imaginedFacts.add(fact);
t.postMessage("Storing imaginary fact: " + fact);
}
} else if (!ai_isQuestion_2(fact) && ai_storeActualFact(fact))
t.postMessage("Storing fact: " + fact);
} else if (eq(name, "storeRule")) {
String rule = arg;
if (!telegram_authorizedToStoreRule(rule)) continue;
rule = nlLogic_addGlobalID(rule);
if (mech_changed(appendToMechList_noUniq("NL Logic Examples", "\n" + rule)))
t.postMessage("Rule stored");
} else if (eq(name, "imagineMode")) {
imagineMode = match("true", arg);
if (!imagineMode) imaginedFacts.clear();
} else if (eq(name, "input")) {
String x = arg;
if (!t.rewrittenInputs.contains(x) && t.newRewrittenInputs.add(x))
print("New rewritten input: " + x);
} else if (c.checkHelper(((Func) e), ai_matchingWrapping(r.matches))) {
} else
{ print("Skipping rule with unknown RHS func: " + ((Func) e).name); return; }
} else
{ print("Skipping rule with unknown RHS element: " + e); return; }
}
}
Pair, List> getRulesAndFacts() {
Pair, List> p = ai_activeRulesAndFacts(imagineMode ? asList(imaginedFacts) : null);
if (debug)
print("getRulesAndFacts: " + n2(p.a, "rule"));
return p;
}
void stdCommands(String s) {
s = trim(s);
final Matches m = new Matches();
String procedure = nlLookup(mechMap("Telegram Procedures"), s, m);
if (procedure != null) {
procedure = expandDollarRefsToMatches(procedure, m, true);
print(">> " + procedure);
postMessage(strOrNull(javaEvalOrInterpret(procedure)));
}
print("s=" + s);
String language = "english";
if (swic_trim(s, "!german ", m)) {
language = "german";
s = m.rest();
print("s=" + s);
}
ai_setLanguage(language);
if (swic_trim(s, "!say ", m))
postMessage(m.rest());
if (eq(s, "!gac"))
postMessage(random_gac36k());
if (swic_trim(s, "!parse ", m))
postMessage(ai_renderCulledParseTree(ai_parseToTreeWithGuessing(m.rest())));
if (swic_trim(s, "!simplify ", m))
postMessage(lines(ai_parseTree_simplifiedTexts(ai_parseToTreeWithGuessing(m.rest()))));
if (swicOneOf_trim(s, m, "!factsFrom ", "!factsFrom\n")) {
String x = m.rest();
ai_deriveFacts_debug = leadingSquareBracketOptions(x).contains("debug");
AutoCloseable __0 = tempSet(NLLogicChecker_v2.class, "staticVerbose" , ai_deriveFacts_debug); try {
final String _x = dropActuallyLeadingSquareBracketStuff(x);
postMessage(or2(evalWithTimeout_text(evalTimeout, new F0() { Object get() { try {
return lines(ai_factsFromNewFacts(tlft(_x)));
} catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "ret lines(ai_factsFromNewFacts(tlft(_x)));"; }}), "No new facts found"));
} finally { _close(__0); }}
if (swic_trim(s, "!askBack ", m)) {
NLLogicChecker_v2 c = nlLogicCheckerWithRulesAndFacts(getRulesAndFacts());
c.input = m.rest();
askBack(c);
}
if (swic_trim(s, "!askBackAboutFact ", m)) {
NLLogicChecker_v2 c = nlLogicCheckerWithRulesAndFacts(getRulesAndFacts());
c.facts = listPlus_inFront(c.facts, m.rest());
askBack(c);
}
/*if (telegram_amIAuthorized())*/ {
if (telegram_amIFullyAuthorized()) {
if (eq(s, "!reload")) {
if (!hasFunctionNamed(mc(), "dm_reloadMe"))
postMessage("Not a module");
else {
postMessage("OK, reloading.");
doAfter(1000, "dm_reloadMe");
return;
}
}
boolean freshMe = swic_trim(s, "!fresh ", m);
if (freshMe || eq(s, "!fresh")) { dm_refreshTranspiler(); if (!freshMe) postMessage("OK"); }
if (swic_trim(s, "!eval ", m))
postMessage(pcallOrExceptionText(new F0() { Object get() { try { return strOrNull(javaEvalOrInterpret(m.rest())); } catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "ret strOrNull(javaEvalOrInterpret(m.rest()));"; }}));
if (freshMe || swic_trim(s, "!real-eval ", m))
postMessage(evalWithTimeout_text(evalTimeout, new F0() { Object get() { try {
return javaEval(m.rest());
} catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "ret javaEval(m.rest());"; }}));
}
if (swic_trim(s, "!rule ", m)) {
String rule = m.rest();
if (!telegram_authorizedToStoreRule(rule)) return;
List rules = trimAll(ai_unparsedTalkRules());
if (!contains(rules, rule)) { // TODO: ignore global IDs
rule = nlLogic_addGlobalID(rule);
appendToMechList_noUniq("NL Logic Examples", "\n" + rule);
postMessage("Rule saved as " + leadingSquareBracketOptions_id(rule) + ". Have " + n2(l(rules)+1, "rule") + ".");
}
}
if (swic_trim(s, "!fact ", m)) {
String fact = m.rest();
List facts = mL("Random facts");
if (!contains(facts, fact)) {
appendToMechList_noUniq("Random facts", fact);
postMessage("Fact saved. Have " + n2(l(facts)+1, "fact") + ".");
}
}
if (eqic(s, "!deriveFacts"))
postMessage("Got " + n2(ai_applyFactToFactRules(), "fact") + ". Total: " + l(mL("Derived facts")));
if (eqic(s, "!groupFacts"))
if (standardGrouper != null) {
appendToMechList("Derived facts", map(standardGrouper, ai_facts()));
postMessage("OK");
}
if (eqic(s, "!imaginaryFacts")) {
Pair, List> rulesAndFacts = getRulesAndFacts();
List facts = reversed(rulesAndFacts.b);
List rules = concatLists_conservative(activeTempRules, reversed(rulesAndFacts.a)); // latest rules first!
final Set toDrop = asHashSet(matchAll_first("$x is not a general chat rule", facts));
rules = filter(rules , new F1() { Boolean get(IfThen r) { try { return !toDrop.contains(r.globalID)
&& !ai_ruleAccessesInput(r); } catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "!toDrop.contains(r.globalID)\r\n && !ai_ruleAccessesInput(r)"; }});
print("Deriving from " + n2(rules, "rule") + " and " + n2(facts, "fact"));
ai_deriveFacts_debug = true;
Set newFacts = ai_deriveFacts(rules, facts);
newFacts = setMinusSet(newFacts, facts);
imaginedFacts.addAll(newFacts);
postMessage("Got " + n2(newFacts, "imaginary fact") + ". Total: " + l(imaginedFacts));
}
// end of authorized functions
}
}
void askBack(NLLogicChecker_v2 c) {
List fails = nlLogic_sortedRuleFails(nlLogic_ruleFails(c, c.rules));
print("Have " + n2(fails, "fail"));
//pnl(fails);
postMessage(lines(nlLogic_unresolvedFactsToQuestions(fails)));
}
int numRules() { return l(first(getRulesAndFacts())); }
JComponent visualize() {
return withCenteredButtons(super.visualize(),
"Checking Log", new Runnable() { public void run() { try { showText("Checking Log", lines(checkingLog)) ;
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "showText(\"Checking Log\", lines(checkingLog))"; }});
}
}
static void dm_reloadMe() {
dm_reloadModule(dm_currentModule());
}
static Map> parse3_cachedPattern_cache = synchronizedMRUCache(1000);
static synchronized List parse3_cachedPattern(String s) {
List tok = parse3_cachedPattern_cache.get(s);
if (tok == null) parse3_cachedPattern_cache.put(s, tok = parse3(s));
return tok;
}
static Map> parse3_cachedInput_cache = synchronizedMRUCache(1000);
static List parse3_cachedInput(String s) {
List tok = parse3_cachedInput_cache.get(s);
if (tok == null) parse3_cachedInput_cache.put(s, tok = parse3(s));
return tok;
}
static boolean setMaxSizeOfMRUCache(Map map, int maxSize) {
map = unwrapSynchronizedMap(map);
Integer size = maxSizeOfMRUCache(map);
if (size == null) return false;
set(map, "maxSize", maxSize);
return true;
}
static long sysNow() {
ping();
return System.nanoTime()/1000000;
}
static long done2_always(long startTime, String desc) {
long time = sysNow()-startTime;
print(desc + " [" + time + " ms]");
return time;
}
static long done2_always(String desc, long startTime) {
return done2_always(startTime, desc);
}
static long done2_always(long startTime) {
return done2_always(startTime, "");
}
static long lastTiming() {
return getLastTiming();
}
static Map getMCThreadLocals(Collection names) {
HashMap map = new HashMap();
for (String name : names) {
Object var = getOptMC(name);
if (var instanceof ThreadLocal)
map.put(name, ((ThreadLocal) var).get());
}
return map;
}
static List ai_staticVars() {
return ll("telegram_msg_tl", "telegram_recentHistory_tl", "ai_context_tl");
}
static int l(Object[] a) { return a == null ? 0 : a.length; }
static int l(boolean[] a) { return a == null ? 0 : a.length; }
static int l(byte[] a) { return a == null ? 0 : a.length; }
static int l(short[] a) { return a == null ? 0 : a.length; }
static int l(long[] a) { return a == null ? 0 : a.length; }
static int l(int[] a) { return a == null ? 0 : a.length; }
static int l(float[] a) { return a == null ? 0 : a.length; }
static int l(double[] a) { return a == null ? 0 : a.length; }
static int l(char[] a) { return a == null ? 0 : a.length; }
static int l(Collection c) { return c == null ? 0 : c.size(); }
static int l(Map m) { return m == null ? 0 : m.size(); }
static int l(CharSequence s) { return s == null ? 0 : s.length(); }
static long l(File f) { return f == null ? 0 : f.length(); }
static int l(Object o) {
return o == null ? 0
: o instanceof String ? l((String) o)
: o instanceof Map ? l((Map) o)
: o instanceof Collection ? l((Collection) o)
: o instanceof Object[] ? l((Object[]) o)
: o instanceof boolean[] ? l((boolean[]) o)
: o instanceof byte[] ? l((byte[]) o)
: o instanceof char[] ? l((char[]) o)
: o instanceof short[] ? l((short[]) o)
: o instanceof int[] ? l((int[]) o)
: o instanceof float[] ? l((float[]) o)
: o instanceof double[] ? l((double[]) o)
: o instanceof long[] ? l((long[]) o)
: (Integer) call(o, "size");
}
static long now_virtualTime;
static long now() {
return now_virtualTime != 0 ? now_virtualTime : System.currentTimeMillis();
}
static List synchroList() {
return Collections.synchronizedList(new ArrayList ());
}
static List synchroList(List l) {
return Collections.synchronizedList(l);
}
static int done_minPrint = 10;
static long done(long startTime, String desc) {
long time = now()-startTime;
if (time >= done_minPrint)
print(desc + " [" + time + " ms]");
return time;
}
static long done(String desc, long startTime) {
return done(startTime, desc);
}
static long done(long startTime) {
return done(startTime, "");
}
static String unixLineBreaks(String s) {
return s.replace("\r", "");
}
static boolean eq(Object a, Object b) {
return a == null ? b == null : a == b || b != null && a.equals(b);
}
static List mL_parsedNLRulesWithoutIDs(String listName) {
return map_pcall("nlLogic_parseRule",mL_rulesWithoutIDs(listName));
}
static IterableIterator shuffledIterator(final List l) {
return new IterableIterator () {
int n = l(l);
int i = 0;
Map shuffled = new HashMap();
public boolean hasNext() { return i < n; }
public A next() {
int j = random(i, n);
A a = get(i), b = get(j);
shuffled.put(j, a);
shuffled.remove(i);
++i;
return b;
}
A get(int i) {
return shuffled.containsKey(i) ? shuffled.get(i) : l.get(i);
}
};
}
// returns (first condition, rest) or null on null; rest may be null
static Pair nlLogic_extractFirstCondition(Exp e) {
if (e == null) return null;
if (e instanceof And)
return pair(((And) e).a, ((And) e).b);
return pair(e, null);
}
static boolean nlLogic_isFunc(Exp e, String name) {
return e instanceof Func && eq(((Func) e).name, name);
}
static String nlLogic_text(Exp e) {
return e == null ? null : e.text();
}
static String nlLogic_text(IfThen r) {
return r == null ? null : r.text();
}
static String aGlobalID() {
return randomID(globalIDLength());
}
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
static volatile Appendable print_log = local_log; // might be redirected, e.g. to main bot
// in bytes - will cut to half that
static volatile int print_log_max = 1024*1024;
static volatile int local_log_max = 100*1024;
static boolean print_silent; // total mute if set
static Object print_byThread_lock = new Object();
static volatile ThreadLocal print_byThread; // special handling by thread - prefers F1
static volatile Object print_allThreads;
static volatile Object print_preprocess;
static void print() {
print("");
}
static A print(String s, A o) {
print((endsWithLetterOrDigit(s) ? s + ": " : s) + o);
return o;
}
// slightly overblown signature to return original object...
static A print(A o) {
ping_okInCleanUp();
if (print_silent) return o;
String s = String.valueOf(o) + "\n";
print_noNewLine(s);
return o;
}
static void print_noNewLine(String s) {
Object f = getThreadLocal(print_byThread_dontCreate());
if (f == null) f = print_allThreads;
if (f != null)
// We do need the general callF machinery here as print_byThread is sometimes shared between modules
if (isFalse(
f instanceof F1 ? ((F1) f).get(s) :
callF(f, s))) return;
print_raw(s);
}
static void print_raw(String s) {
if (print_preprocess != null) s = (String) callF(print_preprocess, s);
s = fixNewLines(s);
Appendable loc = local_log;
Appendable buf = print_log;
int loc_max = print_log_max;
if (buf != loc && buf != null) {
print_append(buf, s, print_log_max);
loc_max = local_log_max;
}
if (loc != null)
print_append(loc, s, loc_max);
System.out.print(s);
}
static String sfu(Object o) { return structureForUser(o); }
static RuntimeException rethrow(Throwable t) {
if (t instanceof Error)
_handleError((Error) t);
throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
static RuntimeException rethrow(String msg, Throwable t) {
throw new RuntimeException(msg, t);
}
static boolean startsWith_trim(String a, String b, Matches m) {
if (!startsWith(a, b)) return false;
m.m = new String[] {trim(substring(a, l(b)))};
return true;
}
static void runPlanWithCheck(PlanInMotion plan, F0 shouldKeepRunning) {
while (isTrue(callF(shouldKeepRunning))) {
A step = plan.master_next();
if (step == null) return;
callF(step);
}
}
// get purpose 1: access a list/array/map (safer version of x.get(y))
static A get(List l, int idx) {
return l != null && idx >= 0 && idx < l(l) ? l.get(idx) : null;
}
// seems to conflict with other signatures
/*static B get(Map map, A key) {
ret map != null ? map.get(key) : null;
}*/
static A get(A[] l, int idx) {
return idx >= 0 && idx < l(l) ? l[idx] : null;
}
// default to false
static boolean get(boolean[] l, int idx) {
return idx >= 0 && idx < l(l) ? l[idx] : false;
}
// get purpose 2: access a field by reflection or a map
static Object get(Object o, String field) {
try {
if (o == null) return null;
if (o instanceof Class) return get((Class) o, field);
if (o instanceof Map)
return ((Map) o).get(field);
Field f = getOpt_findField(o.getClass(), field);
if (f != null) {
f.setAccessible(true);
return f.get(o);
}
if (o instanceof DynamicObject)
return ((DynamicObject) o).fieldValues.get(field);
} catch (Exception e) {
throw asRuntimeException(e);
}
throw new RuntimeException("Field '" + field + "' not found in " + o.getClass().getName());
}
static Object get_raw(Object o, String field) {
try {
Field f = get_findField(o.getClass(), field);
f.setAccessible(true);
return f.get(o);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static Object get(Class c, String field) {
try {
Field f = get_findStaticField(c, field);
f.setAccessible(true);
return f.get(null);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static Field get_findStaticField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0)
return f;
_c = _c.getSuperclass();
} while (_c != null);
throw new RuntimeException("Static field '" + field + "' not found in " + c.getName());
}
static Field get_findField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field))
return f;
_c = _c.getSuperclass();
} while (_c != null);
throw new RuntimeException("Field '" + field + "' not found in " + c.getName());
}
static Object get(String field, Object o) {
return get(o, field);
}
static boolean startsWith(String a, String b) {
return a != null && a.startsWith(b);
}
static boolean startsWith(String a, char c) {
return nemptyString(a) && a.charAt(0) == c;
}
static boolean startsWith(String a, String b, Matches m) {
if (!startsWith(a, b)) return false;
m.m = new String[] {substring(a, strL(b))};
return true;
}
static boolean startsWith(List a, List b) {
if (a == null || listL(b) > listL(a)) return false;
for (int i = 0; i < listL(b); i++)
if (neq(a.get(i), b.get(i)))
return false;
return true;
}
static void addAll(Collection c, Iterable b) {
if (c != null && b != null) for (A a : b) c.add(a);
}
static boolean addAll(Collection c, Collection b) {
return c != null && b != null && c.addAll(b);
}
static boolean addAll(Collection c, B... b) {
return c != null && c.addAll(Arrays.asList(b));
}
static List leadingSquareBracketOptions(String s) {
List tok = javaTokPlusBrackets2(s);
if (!isSquareBracketed(get(tok, 1))) return emptyList();
return tok_splitAtComma(javaTokPlusBrackets2(deSquareBracket(get(tok, 1))));
}
static String dropLeadingSquareBracketStuff(String s) {
return trimSubstring(s, indexOf(s, ']')+1);
}
static A callFOrKeep(Object f, A arg) {
return f == null ? arg : (A) callF(f, arg);
}
static void ai_tg_collectRuleFeedback(Map ruleScores, Collection out_facts) {
// We're lazy!
List>> l = serverTelegramLog_plusFiredRules();
for (int i = 0; i < l(l); i++) {
List rules = l.get(i).b;
if (empty(rules)) continue;
TelegramMsg msg = l.get(i).a;
int j = i+1;
while (j < l(l) && l.get(j).a.sent) ++j; // Find next user msg
List nextMsgs = pairsA(subList(l, i+1, j+1));
TelegramMsg userMsg = null;
if (nempty(nextMsgs)) {
for (TelegramMsg m : nextMsgs)
if (userMsg == null && !m.sent) userMsg = m;
}
if (userMsg == null) continue;
int score = 0, ruleNr = 0;
String s = userMsg.text;
Matches m = new Matches();
if (match("* to number *", s, m)) {
s = m.unq(0); ruleNr = parseInt(m.unq(1));
}
if (matchAny(mL("Yes Feedback Words"), s))
++score;
if (matchAny(mL("No Feedback Words"), s))
--score;
// Rules seem to be in the right order w/respect to output lines
for (String rule : ruleNr == 0 ? rules : ll(rules.get(ruleNr-1))) {
if (score != 0 && ruleScores != null)
ruleScores.put(rule, toInt(ruleScores.get(rule))+score);
if (out_facts != null)
out_facts.add("rule " + format_arg(rule) + " had "
+ (score > 0 ? "positive" : score < 0 ? "negative" : "possible") + " feedback (" + quote(userMsg.text) + ") when fired on msg " + msg.globalID + " at " + msg.date);
}
}
if (out_facts != null)
addToMechList("Rule Feedback", out_facts);
}
static ThreadLocal telegram_msg_tl = new ThreadLocal();
static Map telegram_msg() {
return telegram_msg_tl.get();
}
static void telegram_msg(Map msg) {
telegram_msg_tl.set(msg);
}
static boolean telegram_amIAuthorized() {
return contains(mL_tlftj("Authorized Telegram User IDs"), str(get(telegram_msg(), "userID")));
}
static void nlLogic_collectCheckingLog(NLLogicChecker_v2 c, final List log) {
/*c.onChecking = voidfunc(O o, NLLogicChecker_v2.Matching m) {
pcall { log.add("Checking: " + o + " with " + sfu(m.matches)); }
};*/
c.onChecked = new VF3() { public void get(Object o, NLLogicChecker_v2.Matching m, Object result) { try {
try {
String status = isTrue(result) ? "OK" : isFalse(result) ? "FAIL" : str(result);
log.add(status + ": " + o + " with " + sfu(m.matches));
} catch (Throwable __e) { _handleException(__e); }
} catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "pcall { \r\n S status = isTrue(result) ? \"OK\" : isFalse(result) ? \"FAIL\" :..."; }};
}
static List reversed(Collection l) {
return reversedList(l);
}
static List reversed(A[] l) {
return reversedList(asList(l));
}
static String reversed(String s) {
return reversedString(s);
}
static List concatLists_conservative(List a, List b) {
if (empty(a)) return b;
if (empty(b)) return a;
return concatLists(a, b);
}
static List concatLists_conservative(Collection a, Collection b) {
if (empty(a) && b instanceof List) return ((List) b);
if (empty(b) && a instanceof List) return ((List) a);
return concatLists(a, b);
}
static HashSet asHashSet(Collection c) {
synchronized(collectionMutex(c)) {
return new HashSet(c);
}
}
static HashSet asHashSet(A[] a) {
return a == null ? null : new HashSet(Arrays.asList(a));
}
static List matchAll_first(String pat, Iterable l) {
return map(matchAll(pat, l), new F1() { String get(Matches m) { try { return m.get(0); } catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "m.get(0)"; }});
}
static List filter(Iterable c, Object pred) {
if (pred instanceof F1) return filter(c, (F1 ) pred);
List x = new ArrayList();
if (c != null) for (Object o : c)
if (isTrue(callF(pred, o)))
x.add(o);
return x;
}
static List filter(Object pred, Iterable c) {
return filter(c, pred);
}
static List filter(Iterable c, F1 pred) {
List x = new ArrayList();
if (c != null) for (B o : c)
if (pred.get(o).booleanValue())
x.add(o);
return x;
}
static List filter(F1 pred, Iterable c) {
return filter(c, pred);
}
//ifclass IF1
static List filter(Iterable c, IF1 pred) {
List x = new ArrayList();
if (c != null) for (B o : c)
if (pred.get(o).booleanValue())
x.add(o);
return x;
}
static List filter(IF1 pred, Iterable c) {
return filter(c, pred);
}
//endif
// OLD version
static boolean ai_ruleAccessesInput(String rule) {
List tok = javaTokC(rule);
String t;
if (contains(tok, "ai_input")) return true;
for (int i = 0; i < l(tok); i++)
if (eqGet(tok, i+1, "(") && isIdentifier(t = tok.get(i)))
if (eq(t, "i") || swic(t, "input") || ewic(t, "input")) return true;
return false;
}
// NEW version
static boolean ai_ruleAccessesInput(IfThen rule) {
final Flag flag = new Flag();
nlLogic_visit(rule.in, new VF1() { public void get(Object o) { try {
if (o instanceof Func) {
String t = ((Func) o).name;
if (eqOneOf(t, "i", "phrase") || swic(t, "input") || ewic(t, "input"))
flag.raise();
}
if (o instanceof Exp)
if (jcontains(((Exp) o).text(), "ai_input()")) flag.raise();
} catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "if (o cast Func) {\r\n S t = o.name;\r\n if (eqOneOf(t, \"i\", \"phrase\") ..."; }});
return flag.isUp();
}
static boolean nempty(Collection c) {
return !empty(c);
}
static boolean nempty(CharSequence s) {
return !empty(s);
}
static boolean nempty(Object[] o) { return !empty(o); }
static boolean nempty(byte[] o) { return !empty(o); }
static boolean nempty(int[] o) { return !empty(o); }
static boolean nempty(Map m) {
return !empty(m);
}
static boolean nempty(Iterator i) {
return i != null && i.hasNext();
}
static boolean nempty(Object o) { return !empty(o); }
static Object first(Object list) {
return first((Iterable) list);
}
static A first(List list) {
return empty(list) ? null : list.get(0);
}
static A first(A[] bla) {
return bla == null || bla.length == 0 ? null : bla[0];
}
static A first(IterableIterator i) {
return first((Iterator ) i);
}
static A first(Iterator i) {
return i == null || !i.hasNext() ? null : i.next();
}
static A first(Iterable i) {
if (i == null) return null;
Iterator it = i.iterator();
return it.hasNext() ? it.next() : null;
}
static Character first(String s) { return empty(s) ? null : s.charAt(0); }
static A first(Pair p) {
return p == null ? null : p.a;
}
static A first(T3 t) {
return t == null ? null : t.a;
}
static void nlLogic_fillBattleSpace(NLLogicChecker_v2 c, final List battleSpace, boolean debug) {
VF2 onRuleFired = new VF2() { public void get(IfThen rule, NLLogicChecker_v2.Matching m) { try {
battleSpace.add(new RuleWithParams(rule, m.matches));
} catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "battleSpace.add(new RuleWithParams(rule, m.matches));"; }};
if (c.useIterate) {
print("Iterating over " + n2(c.rules, "rule"));
applyNLLogicFacts_v4_iterate_verbose.set(debug);
applyNLLogicFacts_v4_iterate(c, onRuleFired, c.rules);
} else {
applyNLLogicFacts_v4_verbose.set(debug);
applyNLLogicFacts_v4(c, onRuleFired, c.rules);
}
}
static String n2(long l) { return formatWithThousands(l); }
static String n2(Collection l) { return n2(l(l)); }
static String n2(double l, String singular) {
return n2(l, singular, singular + "s");
}
static String n2(double l, String singular, String plural) {
if (fraction(l) == 0)
return n2((long) l, singular, plural);
else
return l + " " + plural;
}
static String n2(long l, String singular, String plural) {
return n_fancy2(l, singular, plural);
}
static String n2(long l, String singular) {
return n_fancy2(l, singular, singular + "s");
}
static String n2(Collection l, String singular) {
return n2(l(l), singular);
}
static String n2(Collection l, String singular, String plural) {
return n_fancy2(l, singular, plural);
}
static String n2(Map m, String singular, String plural) {
return n_fancy2(m, singular, plural);
}
static String n2(Map m, String singular) {
return n2(l(m), singular);
}
static String n2(Object[] a, String singular) { return n2(l(a), singular); }
static String n2(Object[] a, String singular, String plural) { return n_fancy2(a, singular, plural); }
static boolean empty(Collection c) { return c == null || c.isEmpty(); }
static boolean empty(CharSequence s) { return s == null || s.length() == 0; }
static boolean empty(Map map) { return map == null || map.isEmpty(); }
static boolean empty(Object[] o) { return o == null || o.length == 0; }
static boolean empty(Object o) {
if (o instanceof Collection) return empty((Collection) o);
if (o instanceof String) return empty((String) o);
if (o instanceof Map) return empty((Map) o);
if (o instanceof Object[]) return empty((Object[]) o);
if (o instanceof byte[]) return empty((byte[]) o);
if (o == null) return true;
throw fail("unknown type for 'empty': " + getType(o));
}
static boolean empty(float[] a) { return a == null || a.length == 0; }
static boolean empty(int[] a) { return a == null || a.length == 0; }
static boolean empty(long[] a) { return a == null || a.length == 0; }
static boolean empty(byte[] a) { return a == null || a.length == 0; }
static boolean empty(File f) { return getFileSize(f) == 0; }
static String joinWithComma(Collection c) {
return join(", ", c);
}
static String joinWithComma(String... c) {
return join(", ", c);
}
static String joinWithComma(Pair p) {
return p == null ? "" : joinWithComma(str(p.a), str(p.b));
}
static List map(Iterable l, Object f) { return map(f, l); }
static List map(Object f, Iterable l) {
List x = emptyList(l);
if (l != null) for (Object o : l)
x.add(callF(f, o));
return x;
}
static List map(Iterable l, F1 f) { return map(f, l); }
static List map(F1 f, Iterable l) {
List x = emptyList(l);
if (l != null) for (A o : l)
x.add(callF(f, o));
return x;
}
static List map(Iterable l, IF1 f) {
List x = emptyList(l);
if (l != null) for (A o : l)
x.add(f.get(o));
return x;
}
static List map(A[] l, IF1 f) {
List x = emptyList(l);
if (l != null) for (A o : l)
x.add(f.get(o));
return x;
}
static List map(Object f, Object[] l) { return map(f, asList(l)); }
static List map(Object[] l, Object f) { return map(f, l); }
static List map(Object f, Map map) {
return map(map, f);
}
// map: func(key, value) -> list element
static List map(Map map, Object f) {
List x = new ArrayList();
if (map != null) for (Object _e : map.entrySet()) {
Map.Entry e = (Map.Entry) _e;
x.add(callF(f, e.getKey(), e.getValue()));
}
return x;
}
static int toInt(Object o) {
if (o == null) return 0;
if (o instanceof Number)
return ((Number) o).intValue();
if (o instanceof String)
return parseInt((String) o);
throw fail("woot not int: " + getClassName(o));
}
static int toInt(long l) {
if (l != (int) l) throw fail("Too large for int: " + l);
return (int) l;
}
static void nlLogic_battleItOut(List battleSpace, List facts) {
Matches mm = new Matches();
MultiMap trumps = new MultiMap();
// get conflict clusters
Map < String , Collection < String > > conflictClusters = new HashMap();
Set _ruleIDs = new HashSet();
for (RuleWithParams r : battleSpace) {
addIfNotNull(_ruleIDs, r.rule.globalID);
for (String option : unnull(r.rule.options))
if (match_vbar("conflicts with *|trumps *", option, mm)) {
String r1 = r.rule.globalID, r2 = mm.unq(0);
treeSetClusters_add(conflictClusters, r1, r2);
if (match("trumps *", option))
trumps.put(r1, r2);
}
}
for (String s : facts) {
boolean trump = match("rule * trumps rule *", s, mm);
if (trump || match("rule * conflicts with rule *", s, mm)) {
String r1 = mm.unq(0), r2 = mm.unq(1);
if (_ruleIDs.contains(r1) && _ruleIDs.contains(r2)) {
treeSetClusters_add(conflictClusters, r1, r2);
if (trump)
trumps.put(r1, r2);
}
}
}
if (nempty(conflictClusters))
print("Have conflict clusters: " + sfu(conflictClusters));
// Detect winners
final HashSet toRemove = new HashSet();
for (Collection ruleIDs : uniquifyList_identity(values(conflictClusters))) {
ruleIDs = new LinkedHashSet(ruleIDs);
for (String id : cloneList(ruleIDs)) {
List losers = trumps.get(id);
removeAll(ruleIDs, losers);
toRemove.addAll(losers);
}
String winner = random(ruleIDs);
List removing = listWithout(ruleIDs, winner);
print("Winner: " + winner + ", removing: " + removing);
toRemove.addAll(removing);
}
// Remove losers
if (nempty(toRemove)) {
for (ListIterator i = listIterator(battleSpace); i.hasNext(); )
if (toRemove.contains(i.next().rule.globalID))
i.remove();
print("Rules narrowed down to: " + collect(collect(battleSpace, "rule"), "globalID"));
}
}
static List nlLogic_highestScore(List battleSpace, final Map ruleScores) {
int highest = Integer.MIN_VALUE;
for (RuleWithParams r : battleSpace)
highest = max(highest, toInt(mapGet(ruleScores, r.rule.globalID)));
final int _highest = highest;
return filter(battleSpace , new F1() { Boolean get(RuleWithParams r) { try { return toInt(mapGet(ruleScores, r.rule.globalID)) == _highest; } catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "toInt(mapGet(ruleScores, r.rule.globalID)) == _highest"; }});
}
static String getString(Map map, Object key) {
return map == null ? null : (String) map.get(key);
}
static String getString(List l, int idx) {
return (String) get(l, idx);
}
static String getString(Object o, Object key) {
if (o instanceof Map) return getString((Map) o, key);
if (key instanceof String)
return (String) getOpt(o, (String) key);
throw fail("Not a string key: " + getClassName(key));
}
static String getString(String key, Object o) {
return getString(o, (Object) key);
}
static String format(String pat, Object... args) {
return format3(pat, args);
}
static String localDateWithMilliseconds(long time) {
SimpleDateFormat format = simpleDateFormat_local("yyyy/MM/dd HH:mm:ss''SSSS");
return format.format(time);
}
static String localDateWithMilliseconds() {
return localDateWithMilliseconds(now());
}
static String dropPrefix(String prefix, String s) {
return s == null ? null : s.startsWith(prefix) ? s.substring(l(prefix)) : s;
}
static String struct(Object o) {
return structure(o);
}
static String struct(Object o, structure_Data data) {
return structure(o, data);
}
static MechMode mechMode_value = new MechMode();
static MechMode mechMode() {
return mechMode_value;
}
static String addToMechList(String name, String text) {
return botAppendToMechList_uniqCI(name, text);
}
static String addToMechList(String name, Collection lines) {
return addToMechList(name, lines(lines));
}
static List nlLogic_unrollAnd(Exp e) {
List l = new ArrayList();
nlLogic_unrollAnd_collect(e, l);
return l;
}
static void nlLogic_unrollAnd_collect(Exp e, List out) {
if (e instanceof And) {
nlLogic_unrollAnd_collect(((And) e).a, out);
nlLogic_unrollAnd_collect(((And) e).b, out);
} else
addIfNotNull(out, e);
}
static boolean eqOneOf(Object o, Object... l) {
for (Object x : l) if (eq(o, x)) return true; return false;
}
static boolean containsNL(Collection l, String pat) {
if (l == null) return false;
if (l.contains(pat)) return true;
for (String s : l) if (match(pat, s)) return true;
return false;
}
static boolean ai_isQuestion_2(String s) {
return ai_isYesNoQuestion_1(s) || ai_questionWord_1(firstWord2(s));
}
static boolean ai_storeActualFact(String fact) {
return !contains(mL("Random facts"), fact) && mech_changed(addToMechList("Derived facts", fact));
}
static boolean telegram_authorizedToStoreRule(String rule) {
return telegram_amIFullyAuthorized() || /*telegram_amIAuthorized() &&*/ !ai_ruleContainsEval(rule);
}
static String nlLogic_addGlobalID(String rule) {
rule = ltrim(rule);
if (!startsWith(rule, "[id:"))
rule = "[id: " + aGlobalID() +
(rule.startsWith("[") ? ", " + dropPrefix("[", rule) : "] " + rule);
return rule;
}
static boolean mech_changed(String result) {
return cic(result, "changed");
}
static String appendToMechList_noUniq(String name, String text) {
return botAppendToMechList(name, text);
}
static boolean match(String pat, String s) {
return match3(pat, s);
}
static boolean match(String pat, String s, Matches matches) {
return match3(pat, s, matches);
}
static boolean match(String pat, List toks, Matches matches) {
return match3(pat, toks, matches);
}
static NLLogicChecker_v2.Matching ai_matchingWrapping(VarMatches matches) {
NLLogicChecker_v2.Matching m = new NLLogicChecker_v2.Matching();
m.matches = matches;
return m;
}
static Pair, List> ai_activeRulesAndFacts() {
return ai_activeRulesAndFacts(null);
}
static Pair, List> ai_activeRulesAndFacts(List extraFacts) {
long _startTime_103 = sysNow();
List facts = concatLists_conservative(extraFacts, mL_facts());
List unparsedRules = ai_unparsedTalkRules();
for (String listName : mL("Rule & Fact Lists"))
for (String x : splitAtEmptyLines(mL_raw(listName)))
if (anyJavaTokens(x))
if (nlLogic_parseRule(x) != null)
unparsedRules.add(x);
else
facts.add(x);
unparsedRules = sortedByCalculatedField(unparsedRules, new F1() { Object get(String s) { try { return jcontains(s, "entity("); } catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "jcontains(s, \"entity(\")"; }});
List rules = map_pcall("nlLogic_parseRule",unparsedRules);
for (String listName : mL("Default active scripts"))
addAll(rules, nlLogic_loadMechScript(listName));
nlLogic_expandMacros(rules);
done2_always("ai_activeRulesAndFacts", _startTime_103);
return pair(rules, facts);
}
static ArrayList asList(A[] a) {
return a == null ? new ArrayList () : new ArrayList (Arrays.asList(a));
}
static ArrayList asList(int[] a) {
if (a == null) return null;
ArrayList l = emptyList(a.length);
for (int i : a) l.add(i);
return l;
}
static ArrayList asList(float[] a) {
if (a == null) return null;
ArrayList l = emptyList(a.length);
for (float i : a) l.add(i);
return l;
}
static ArrayList asList(Iterable s) {
if (s instanceof ArrayList) return (ArrayList) s;
ArrayList l = new ArrayList();
if (s != null)
for (A a : s)
l.add(a);
return l;
}
static ArrayList asList(Producer p) {
ArrayList l = new ArrayList();
A a;
if (p != null) while ((a = p.next()) != null)
l.add(a);
return l;
}
static ArrayList asList(Enumeration e) {
ArrayList l = new ArrayList();
if (e != null)
while (e.hasMoreElements())
l.add(e.nextElement());
return l;
}
static String trim(String s) { return s == null ? null : s.trim(); }
static String trim(StringBuilder buf) { return buf.toString().trim(); }
static String trim(StringBuffer buf) { return buf.toString().trim(); }
static A nlLookup(Map map, String s) {
return lookupByNLMatch(map, s);
}
static A nlLookup(Map map, String s, Matches m) {
return lookupByNLMatch(map, s, m);
}
static List nlLookup(MultiMap map, String s) {
return lookupByNLMatch(map, s);
}
static Map mechMap(String listName) {
return mechMapCI(listName);
}
static String expandDollarRefsToMatches(String s, Matches m) {
return expandDollarRefsToMatches(s, m, false);
}
static String expandDollarRefsToMatches(String s, Matches m, boolean alwaysQuote) {
List tok = javaTok(s);
for (int i = 1; i < l(tok); i += 2) {
String t = tok.get(i);
if (!t.startsWith("$")) continue;
t = dropFirst(t);
int j = parseIntAtEnd(t);
if (j < 1) continue;
String x = get(m.m, j-1);
if (x == null) continue;
if (alwaysQuote || startsWith(t, "q"))
x = quote(x);
tok.set(i, x);
}
return join(tok);
}
static String strOrNull(Object o) {
return o == null ? null : str(o);
}
static Object javaEvalOrInterpret(String code) {
if (!canJavaInterpret(code)) return javaEval(code);
if (canQuickEval(code)) return quickEval(code);
Pair> p = tok_parseFunctionCall(code);
if (p != null)
return callAndMake(p.a, toObjectArray(map("javaEvalOrInterpret",p.b)));
return javaEval(code);
}
static boolean swic_trim(String a, String b, Matches m) {
if (!swic(a, b)) return false;
m.m = new String[] {trim(substring(a, l(b)))};
return true;
}
static ThreadLocal ai_setLanguage_value = new ThreadLocal();
static void ai_setLanguage(String language) {
ai_setLanguage_value.set(language);
}
static void _onLoad_ai_setLanguage() {
_threadInfo_addMakerAndRetriever(
new VF1() { public void get(Map map) { try { mapPut(map, "ai_setLanguage_value", ai_setLanguage_value.get()) ; } catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "mapPut(map, 'ai_setLanguage_value, ai_setLanguage_value!)"; }},
new VF1() { public void get(Map map) { try { ai_setLanguage((String) map.get("ai_setLanguage_value")) ; } catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "ai_setLanguage((S) map.get('ai_setLanguage_value))"; }});
}
static String random_gac36k() {
return random(keys(gac36k()));
}
static String ai_renderCulledParseTree(PTElement e) {
ai_cullParseTree(e);
return ai_renderParseTree(e);
}
static PTElement ai_parseToTreeWithGuessing(String input) {
List words = ai_uncategorizedWordsInSentence(input);
//print("Have " + n2(words, "unknown word"));
AI_BottomUpParser1 parser = new AI_BottomUpParser1();
parser.wordsToCategories = new DerivedMap(ai_wordToCategories());
Map> guessMap = new HashMap();
AutoCloseable __108 = tempSetThreadLocal(ai_getWordToCategoryWeight_map, guessMap); try {
for (String word : words) {
Map map = ai_guessCategoriesForUnknownWord_weighted(word);
guessMap.put(word, map);
parser.wordsToCategories.put(word, keys(map));
}
return ai_parseBottomUpToTree(parser, input);
} finally { _close(__108); }}
static String lines(Iterable lines) { return fromLines(lines); }
static String lines(Object[] lines) { return fromLines(asList(lines)); }
static List lines(String s) { return toLines(s); }
static LinkedHashSet ai_parseTree_simplifiedTexts(PTElement e) {
LinkedHashSet out = new LinkedHashSet();
ai_parseTree_simplifiedTexts_collect(e, out);
return out;
}
static void ai_parseTree_simplifiedTexts_collect(PTElement e, LinkedHashSet out) {
if (e == null) return;
PTElement ee = ai_parseTree_descendToComposite(e);
if (ee == null) { out.add(e.text()); return; }
e = ee;
if (e instanceof ChoosePart) {
ai_Reconstructed r = ((ChoosePart) e).reconstruction;
WeightedProduction prod = r.production;
//print("Hint: " + prod.simplificationHint + " in prod: " + sfu(prod));
//if (eq(prod.simplificationHint, "L")) out.add(r.a.text());
//else if (eq(prod.simplificationHint, "R")) out.add(r.b.text());
PTElement e1 = first(e.children), e2 = second(e.children);
List a = asList(ai_parseTree_simplifiedTexts(e1));
List b = asList(ai_parseTree_simplifiedTexts(e2));
String hint = prod.simplificationHint;
if (eqOneOf(hint, "L", "B")) insertFirst(b, "");
/*else XXX*/ if (eqOneOf(hint, "R", "B")) insertFirst(a, "");
//print("L " + className(e1) + " " + className(ai_parseTree_descendToComposite(e1)) + " " + r.a.text() + " => " + a);
//print("R " + className(e2) + " " + className(ai_parseTree_descendToComposite(e2)) + " " + r.b.text() + " => " + b);
/*for (S aa : a)
for (S bb : b)
out.add(trim(aa + " " + bb));*/
for (Pair p : combineZigZag(a, b))
out.add(trim(p.a + " " + p.b));
// Add full text too
out.add(((ChoosePart) e).word.text());
}
}
static boolean swicOneOf_trim(String s, Matches m, String... l) {
for (String x : l) if (swic_trim(s, x, m)) return true; return false;
}
static AutoCloseable tempSet(Object o, final String field, Object value) {
return tempSetField(o, field, value);
}
static String dropActuallyLeadingSquareBracketStuff(String s) {
return startsWith(s, "[") ? dropLeadingSquareBracketStuff(s) : s;
}
static String or2(String a, String b) {
return nempty(a) ? a : b;
}
static String or2(String a, String b, String c) {
return or2(or2(a, b), c);
}
static String evalWithTimeout_text(double timeoutSeconds, final Object f) {
return evalWithTimeout_text(toMS_int(timeoutSeconds), f);
}
// f : func -> O
static String evalWithTimeout_text(final int timeoutMS, final Object f) {
return pcallOrExceptionText_strOrNull(new F0() { Object get() { try {
return evalWithTimeoutOrException(timeoutMS, new F0() { Object get() { try { return strOrNull(callF(f)); } catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "ret strOrNull(callF(f));"; }});
} catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "ret evalWithTimeoutOrException(timeoutMS, func -> O { strOrNull(callF(f)) });"; }});
}
static Set ai_factsFromNewFacts(List newFacts) {
Pair, List> p = ai_rulesAndFacts();
return ai_deriveFacts(p.a, concatLists(newFacts, p.b));
}
static Set ai_factsFromNewFacts(String... newFacts) {
return ai_factsFromNewFacts(asList(newFacts));
}
static List tlft(String s) { return toLinesFullTrim(s); }
static List tlft(File f) { return toLinesFullTrim(f); }
static void _close(AutoCloseable c) {
if (c != null) try {
c.close();
} catch (Throwable e) {
// Some classes stupidly throw an exception on double-closing
if (c instanceof javax.imageio.stream.ImageOutputStream)
return;
else throw rethrow(e);
}
}
static NLLogicChecker_v2 nlLogicCheckerWithRulesAndFacts(Pair, List> p) {
NLLogicChecker_v2 c = new NLLogicChecker_v2();
c.rules = p.a;
c.facts = p.b;
return c;
}
static List listPlus_inFront(Collection l, A... more) {
return concatLists(asList(more), l);
}
static boolean telegram_amIFullyAuthorized() {
return contains(mL_tlftj("Fully Authorized Telegram User IDs"), str(telegram_msg().get("userID")));
}
// This is a bit rough... finds static and non-static methods.
// It's usually just for error messages though, so no worries.
static boolean hasFunctionNamed(Object obj, String method) {
return hasMethodNamed(obj, method);
}
static boolean hasFunctionNamed(Class c, String method) {
return hasMethodNamed(c, method);
}
static Class mc() {
return main.class;
}
static java.util.Timer doAfter(long delay, Object r) {
return doLater(delay, r);
}
static java.util.Timer doAfter(double delaySeconds, Object r) {
return doLater(delaySeconds, r);
}
static void dm_refreshTranspiler() {
call(dm_moduleOfType("#1017065/Transpiler"), "refresh");
}
static String pcallOrExceptionText(Object f, Object... args) {
try {
return str(callF(f, args));
} catch (Throwable e) {
_handleException(e);
return exceptionToStringShort(e);
}
}
static Object javaEval(String code) {
return evalJava(code);
}
static List trimAll(Collection l) {
List l2 = new ArrayList();
if (l != null) for (String s : l)
l2.add(trim(s));
return l2;
}
static List ai_unparsedTalkRules() {
return splitAtEmptyLines(mL_raw("NL Logic Examples"));
}
static boolean contains(Collection c, Object o) {
return c != null && c.contains(o);
}
static boolean contains(Object[] x, Object o) {
if (x != null)
for (Object a : x)
if (eq(a, o))
return true;
return false;
}
static boolean contains(String s, char c) {
return s != null && s.indexOf(c) >= 0;
}
static boolean contains(String s, String b) {
return s != null && s.indexOf(b) >= 0;
}
static boolean contains(BitSet bs, int i) {
return bs != null && bs.get(i);
}
static String leadingSquareBracketOptions_id(String s) {
List options = leadingSquareBracketOptions(s);
Matches m = new Matches();
for (String option : options)
if (startsWith_trim(option, "id: ", m))
return m.rest();
return null;
}
static List mL(String name) {
return mechList_opt(name);
}
static boolean eqic(String a, String b) {
if ((a == null) != (b == null)) return false;
if (a == null) return true;
return a.equalsIgnoreCase(b);
}
static boolean eqic(char a, char b) {
if (a == b) return true;
char u1 = Character.toUpperCase(a);
char u2 = Character.toUpperCase(b);
if (u1 == u2) return true;
return Character.toLowerCase(u1) == Character.toLowerCase(u2);
}
static boolean ai_applyFactToFactRules_debug;
static Collection ai_applyFactToFactRules() {
List rules = ai_parseRulesWithMacros(mL_raw("Fact-to-fact rules"));
if (ai_applyFactToFactRules_debug)
print("ai_applyFactToFactRules: " + n2(rules, "rule"));
Set outFacts = ai_deriveFacts(rules, mL("Random facts"));
appendToMechList("Derived facts", outFacts);
return outFacts;
}
static List ai_applyFactToFactRules(String rules, String facts) {
return asList(ai_deriveFacts(ai_parseRulesWithMacros(rules), tlft(facts)));
}
static String appendToMechList(String name, String text) {
return botAppendToMechList_uniqCI(name, text);
}
static String appendToMechList(String name, Collection lines) {
return botAppendToMechList_uniqCI(name, lines);
}
static List ai_facts() {
return mL_facts();
}
static boolean ai_deriveFacts_debug;
static Set ai_deriveFacts(List rules, List facts) {
final NLLogicChecker_v3 c = new NLLogicChecker_v3();
c.facts = facts;
final NLLogicChecker_v2.Matching m = new NLLogicChecker_v2.Matching();
m.verbose = ai_deriveFacts_debug;
final LinkedHashSet outFacts = new LinkedHashSet();
for (final IfThen rule : nlLogic_filterByRHSConstructs(rules, "fact")) {
if (ai_deriveFacts_debug)
print("ai_deriveFacts: Checking " + rule);
c.iterate(rule.in, m, new Runnable() { public void run() { try {
if (ai_deriveFacts_debug)
print("Got matches: " + sfu(m.matches));
for (Exp e : nlLogic_unrollAnd(rule.out)) {
e = c.apply(e, m);
if (e instanceof Func)
if (eq(((Func) e).name, "fact"))
{ outFacts.add(nlLogic_text(((Func) e).arg)); continue; }
if (c.checkHelper(e, m))
continue;
print("Ignoring unknown RHS element: " + e);
}
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (ai_deriveFacts_debug)\r\n print(\"Got matches: \" + sfu(m.matches));\r\n..."; }});
}
return outFacts;
}
static Set setMinusSet(Set l, Collection stuff) {
if (empty(stuff)) return l;
Set set = asSet(stuff);
Set l2 = similarEmptySet(l);
for (A a : l) if (!set.contains(a)) l2.add(a);
return l2;
}
static List nlLogic_sortedRuleFails(List l) {
return sortedByFieldDesc("successes", l);
}
static List nlLogic_ruleFails(NLLogicChecker_v2 c, List rules) {
List out = new ArrayList();
for (IfThen rule : unnull(rules)) {
NLLogicChecker_v2.Matching m = new NLLogicChecker_v2.Matching();
Pair p = nlLogic_checkFirstConditions2(c, rule, m);
if (p.b != null)
out.add(new RuleFailInfo(rule, p.a, m.matches, p.b));
}
return out;
}
static boolean nlLogic_unresolvedFactsToQuestions_debug;
static Collection nlLogic_unresolvedFactsToQuestions(RuleFailInfo ruleFail) {
if (ruleFail == null) return emptyList();
Exp e = nlLogic_applyMatches(nlLogic_firstCondition(ruleFail.remainingConditions), ruleFail.matches);
// Now have: fact(bla) in e
if (e instanceof Func)
if (eq(((Func) e).name, "fact")) {
String fact = "Unresolved fact: " + ((Func) e).arg.text();
if (nlLogic_unresolvedFactsToQuestions_debug) print(fact);
Pair, List> p = ai_activeRulesAndFacts(ll(fact));
return ai_deriveOutput(p.a, p.b);
}
return emptyList();
}
static Collection nlLogic_unresolvedFactsToQuestions(Collection ruleFails) {
LinkedHashSet out = new LinkedHashSet();
for (RuleFailInfo fail : ruleFails)
addAll(out, nlLogic_unresolvedFactsToQuestions(fail));
return out;
}
static JPanel withCenteredButtons(JComponent center, Object... params) {
return centerAndSouth(center, withMargin(jcenteredbuttons(params)));
}
static JTextArea showText(final String title, Object text) {
return showText(null, title, text);
}
static JTextArea showText(JTextArea ta, final String title, Object text) {
final String _text = str(text);
if (ta != null)
return activateFrameAndReturnComponent(setFrameTitle(title, setText(ta, _text)));
return swing(new F0() { JTextArea get() { try {
JTextArea textArea = newTypeWriterTextArea(_text);
makeFrame(title, new JScrollPane(textArea));
return textArea;
} catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "JTextArea textArea = newTypeWriterTextArea(_text);\r\n makeFrame(title, new ..."; }});
}
static JTextArea showText(Object text) {
return showText(str(text));
}
static JTextArea showText(String text) {
return showText(autoFrameTitle(), text);
}
static JTextArea showText() {
return showText("");
}
static void dm_reloadModule(Object module) {
dm_reloadModuleInBackground(module);
}
static void dm_reloadModule() {
dm_reloadModuleInBackground();
}
static InheritableThreadLocal> dm_currentModule_value = new InheritableThreadLocal();
static DynModule dm_currentModule() {
return getWeakRef(dm_currentModule_value.get());
}
static void _onLoad_dm_currentModule() {
setOptMC("dm_currentModule_generic", dm_currentModule_value);
}
// prevent memory leak
static void cleanMeUp_dm_currentModule() {
dm_currentModule_value = new InheritableThreadLocal();
}
static Map synchronizedMRUCache(int maxSize) {
return synchroMap(new MRUCache(maxSize));
}
static List parse3(String s) {
return dropPunctuation(javaTokPlusPeriod(s));
}
static Map unwrapSynchronizedMap(Map map) {
if (eqOneOf(className(map),
"java.util.Collections$SynchronizedMap",
"java.util.Collections$SynchronizedSortedMap",
"java.util.Collections$SynchronizedNavigableMap"))
return (Map) get_raw(map, "m");
return map;
}
static Integer maxSizeOfMRUCache(Map map) {
return (Integer) getOpt_raw(unwrapSynchronizedMap(map), "maxSize");
}
static void set(Object o, String field, Object value) {
if (o == null) return;
if (o instanceof Class) set((Class) o, field, value);
else try {
Field f = set_findField(o.getClass(), field);
f.setAccessible(true);
smartSet(f, o, value);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static void set(Class c, String field, Object value) {
if (c == null) return;
try {
Field f = set_findStaticField(c, field);
f.setAccessible(true);
smartSet(f, null, value);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static Field set_findStaticField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0)
return f;
_c = _c.getSuperclass();
} while (_c != null);
throw new RuntimeException("Static field '" + field + "' not found in " + c.getName());
}
static Field set_findField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field))
return f;
_c = _c.getSuperclass();
} while (_c != null);
throw new RuntimeException("Field '" + field + "' not found in " + c.getName());
}
//sbool ping_actions_shareable = true;
static volatile boolean ping_pauseAll;
static int ping_sleep = 100; // poll pauseAll flag every 100
static volatile boolean ping_anyActions;
static Map ping_actions = newWeakHashMap();
static ThreadLocal ping_isCleanUpThread = new ThreadLocal();
// always returns true
static boolean ping() {
if (ping_pauseAll || ping_anyActions ) ping_impl(true /* XXX */);
//ifndef LeanMode ping_impl(); endifndef
return true;
}
// returns true when it slept
static boolean ping_impl(boolean okInCleanUp) { try {
if (ping_pauseAll && !isAWTThread()) {
do
Thread.sleep(ping_sleep);
while (ping_pauseAll);
return true;
}
if (ping_anyActions) { // don't allow sharing ping_actions
if (!okInCleanUp && !isTrue(ping_isCleanUpThread.get()))
failIfUnlicensed();
Object action = null;
synchronized(ping_actions) {
if (!ping_actions.isEmpty()) {
action = ping_actions.get(currentThread());
if (action instanceof Runnable)
ping_actions.remove(currentThread());
if (ping_actions.isEmpty()) ping_anyActions = false;
}
}
if (action instanceof Runnable)
((Runnable) action).run();
else if (eq(action, "cancelled"))
throw fail("Thread cancelled.");
}
return false;
} catch (Exception __e) { throw rethrow(__e); } }
static long getLastTiming() {
ThreadLocal tl = (ThreadLocal) (getOpt(getMainClass(), "saveTiming_last"));
if (tl == null) return -1;
Long l = tl.get();
return l == null ? -1 : l;
}
static Object getOptMC(String field) {
return getOpt(mc(), field);
}
static List ll(A... a) {
ArrayList l = new ArrayList(a.length);
for (A x : a) l.add(x);
return l;
}
static Object call(Object o) {
return callF(o);
}
// varargs assignment fixer for a single string array argument
static Object call(Object o, String method, String[] arg) {
return call(o, method, new Object[] {arg});
}
static Object call(Object o, String method, Object... args) {
//ret call_cached(o, method, args);
return call_withVarargs(o, method, args);
}
static String str(Object o) {
return o == null ? "null" : o.toString();
}
static String str(char[] c) {
return new String(c);
}
static List map_pcall(Iterable l, Object f) {
return map_pcall(f, l);
}
static List map_pcall(Object f, Iterable l) {
List x = new ArrayList();
for (Object o : unnull(l)) { try { x.add(callF(f, o)); } catch (Throwable __e) { _handleException(__e); }}
return x;
}
static List map_pcall(Object f, Object[] l) {
List x = new ArrayList();
for (Object o : unnull(l)) { try { x.add(callF(f, o)); } catch (Throwable __e) { _handleException(__e); }}
return x;
}
static IfThen nlLogic_parseRule(String s) {
IfThen rule = new IfThen();
rule.originalText = s;
List tok = javaTokPlusBrackets2(s);
if (isSquareBracketed(get(tok, 1))) {
List tokOptions = javaTokPlusBrackets2(deSquareBracket(get(tok, 1)));
Matches m = new Matches();
for (String option : tok_splitAtComma(tokOptions)) {
if (startsWith_trim(option, "id: ", m))
rule.globalID = m.rest();
else
rule.options = linkedHashSet_addWithAutoInit(rule.options, option);
}
tok = dropFirst(2, tok);
}
List l = splitAtDoubleArrow(tok);
if (l(l) > 2 || l(l) == 0) return null;
rule.out = nlLogic_parseExpression(last(l));
rule.in = nlLogic_parseExpression(nextToLast(l));
if (rule.globalID == null)
//rule.globalID = aGlobalID();
rule.globalID = textMD5ToGlobalID(str(rule));
return rule;
}
static List mL_rulesWithoutIDs(String listName) {
return bOfPairs(mL_rules_addGlobalIDsIfAuthed(listName));
}
static Random random_random = new Random();
static int random(int n) {
return n <= 0 ? 0 : random_random.nextInt(n);
}
static double random(double max) {
return random()*max;
}
static double random() {
return random_random.nextInt(100001)/100000.0;
}
static double random(double min, double max) {
return min+random()*(max-min);
}
// min <= value < max
static int random(int min, int max) {
return min+random(max-min);
}
static A random(List l) {
return oneOf(l);
}
static A random(Collection c) {
if (c instanceof List) return random((List ) c);
int i = random(l(c));
return collectionGet(c, i);
}
static Pair pair(A a, B b) {
return new Pair(a, b);
}
static Pair pair(A a) {
return new Pair(a, a);
}
static int randomID_defaultLength = 12;
static String randomID(int length) {
return makeRandomID(length);
}
static String randomID() {
return randomID(randomID_defaultLength);
}
static int globalIDLength() {
return 16;
}
static boolean endsWithLetterOrDigit(String s) {
return s != null && s.length() > 0 && Character.isLetterOrDigit(s.charAt(s.length()-1));
}
static void ping_okInCleanUp() {
if (ping_pauseAll || ping_anyActions )
ping_impl(true);
}
static Object getThreadLocal(Object o, String name) {
ThreadLocal t = (ThreadLocal) (getOpt(o, name));
return t != null ? t.get() : null;
}
static A getThreadLocal(ThreadLocal tl) {
return tl == null ? null : tl.get();
}
static ThreadLocal print_byThread_dontCreate() {
return print_byThread;
}
static boolean isFalse(Object o) {
return eq(false, o);
}
static Map> callF_cache = newDangerousWeakHashMap();
static A callF(F0 f) {
return f == null ? null : f.get();
}
static B callF(F1 f, A a) {
return f == null ? null : f.get(a);
}
static B callF(IF1 f, A a) {
return f == null ? null : f.get(a);
}
static C callF(F2 f, A a, B b) {
return f == null ? null : f.get(a, b);
}
static void callF(VF1 f, A a) {
if (f != null) f.get(a);
}
static Object callF(Object f, Object... args) { try {
if (f instanceof String)
return callMC((String) f, args);
if (f instanceof Runnable) {
((Runnable) f).run();
return null;
}
if (f == null) return null;
Class c = f.getClass();
ArrayList methods;
synchronized(callF_cache) {
methods = callF_cache.get(c);
if (methods == null)
methods = callF_makeCache(c);
}
int n = l(methods);
if (n == 0) {
throw fail("No get method in " + getClassName(c));
}
if (n == 1) return invokeMethod(methods.get(0), f, args);
for (int i = 0; i < n; i++) {
Method m = methods.get(i);
if (call_checkArgs(m, args, false))
return invokeMethod(m, f, args);
}
throw fail("No matching get method in " + getClassName(c));
} catch (Exception __e) { throw rethrow(__e); } }
// used internally
static ArrayList callF_makeCache(Class c) {
ArrayList l = new ArrayList();
Class _c = c;
do {
for (Method m : _c.getDeclaredMethods())
if (m.getName().equals("get")) {
m.setAccessible(true);
l.add(m);
}
if (!l.isEmpty()) break;
_c = _c.getSuperclass();
} while (_c != null);
callF_cache.put(c, l);
return l;
}
static String fixNewLines(String s) {
int i = indexOf(s, '\r');
if (i < 0) return s;
int l = s.length();
StringBuilder out = new StringBuilder(l);
out.append(s, 0, i);
for (; i < l; i++) {
char c = s.charAt(i);
if (c != '\r')
out.append(c);
else {
out.append('\n');
if (i+1 < l && s.charAt(i+1) == '\n') ++i;
}
}
return out.toString();
}
static void print_append(Appendable _buf, String s, int max) { try {
synchronized(_buf) {
_buf.append(s);
if (!(_buf instanceof StringBuilder)) return;
StringBuilder buf = (StringBuilder) _buf;
max /= 2;
if (buf.length() > max) try {
int newLength = max/2;
int ofs = buf.length()-newLength;
String newString = buf.substring(ofs);
buf.setLength(0);
buf.append("[...] ").append(newString);
} catch (Exception e) {
buf.setLength(0);
}
}
} catch (Exception __e) { throw rethrow(__e); } }
static String structureForUser(Object o) {
return beautifyStructure(struct_noStringSharing(o));
}
static void _handleError(Error e) {
call(javax(), "_handleError", e);
}
static String substring(String s, int x) {
return substring(s, x, strL(s));
}
static String substring(String s, int x, int y) {
if (s == null) return null;
if (x < 0) x = 0;
if (x >= s.length()) return "";
if (y < x) y = x;
if (y > s.length()) y = s.length();
return s.substring(x, y);
}
static String substring(String s, IntRange r) {
return r == null ? null : substring(s, r.start, r.end);
}
static boolean isTrue(Object o) {
if (o instanceof Boolean)
return ((Boolean) o).booleanValue();
if (o == null) return false;
if (o instanceof ThreadLocal)
return isTrue(((ThreadLocal) o).get());
throw fail(getClassName(o));
}
static Field getOpt_findField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field))
return f;
_c = _c.getSuperclass();
} while (_c != null);
return null;
}
static RuntimeException asRuntimeException(Throwable t) {
if (t instanceof Error)
_handleError((Error) t);
return t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
static boolean nemptyString(String s) {
return s != null && s.length() > 0;
}
static int strL(String s) {
return s == null ? 0 : s.length();
}
static int listL(Collection l) {
return l == null ? 0 : l.size();
}
static boolean neq(Object a, Object b) {
return !eq(a, b);
}
static List javaTokPlusBrackets2(String s) {
return tok_combineRoundCurlySquareBrackets_keep(javaTok(s));
}
static boolean isSquareBracketed(String s) {
return s != null && s.startsWith("[") && s.endsWith("]");
}
static ArrayList emptyList() {
return new ArrayList();
//ret Collections.emptyList();
}
static ArrayList emptyList(int capacity) {
return new ArrayList(max(0, capacity));
}
// Try to match capacity
static ArrayList emptyList(Iterable l) {
return l instanceof Collection ? emptyList(((Collection) l).size()) : emptyList();
}
static ArrayList emptyList(Object[] l) {
return emptyList(l(l));
}
// get correct type at once
static ArrayList emptyList(Class c) {
return new ArrayList();
}
static List tok_splitAtComma(String s) {
return tok_splitAtComma(javaTok(s));
}
static List tok_splitAtComma(List tok) {
List out = new ArrayList();
for (int i = 0; i < l(tok); i++) {
int j = smartIndexOf(tok, ",", i);
out.add(joinSubList(tok, i+1, j-1));
i = j;
}
return out;
}
static String deSquareBracket(String s) {
if (startsWith(s, "[") && endsWith(s, "]"))
return substring(s, 1, l(s)-1);
return s;
}
static String trimSubstring(String s, int x) {
return trim(substring(s, x));
}
static String trimSubstring(String s, int x, int y) {
return trim(substring(s, x, y));
}
static String trimSubstring(String s, IntRange r) {
return trim(substring(s, r));
}
static int indexOf(List l, A a, int startIndex) {
if (l == null) return -1;
for (int i = startIndex; i < l(l); i++)
if (eq(l.get(i), a))
return i;
return -1;
}
static int indexOf(List l, int startIndex, A a) {
return indexOf(l, a, startIndex);
}
static int indexOf(List l, A a) {
if (l == null) return -1;
return l.indexOf(a);
}
static int indexOf(String a, String b) {
return a == null || b == null ? -1 : a.indexOf(b);
}
static int indexOf(String a, String b, int i) {
return a == null || b == null ? -1 : a.indexOf(b, i);
}
static int indexOf(String a, char b) {
return a == null ? -1 : a.indexOf(b);
}
static int indexOf(String a, int i, char b) {
return indexOf(a, b, i);
}
static int indexOf(String a, char b, int i) {
return a == null ? -1 : a.indexOf(b, i);
}
static int indexOf(String a, int i, String b) {
return a == null || b == null ? -1 : a.indexOf(b, i);
}
static int indexOf(A[] x, A a) {
if (x == null) return -1;
for (int i = 0; i < l(x); i++)
if (eq(x[i], a))
return i;
return -1;
}
static List>> serverTelegramLog_plusFiredRules() {
List msgs = serverTelegramLog();
MultiMap ruleFires = ai_tg_rulesFiredOnMessages_fullMap();
List>> l = emptyList(msgs);
for (TelegramMsg msg : msgs)
addPair(l, msg, nullIfEmpty(ruleFires.get(msg.globalID)));
return l;
}
static List pairsA(Collection> l) {
return firstOfPairs(l);
}
static List subList(List l, int startIndex) {
return subList(l, startIndex, l(l));
}
static List subList(List l, int startIndex, int endIndex) {
if (l == null) return null;
int n = l(l);
startIndex = Math.max(0, startIndex);
endIndex = Math.min(n, endIndex);
if (startIndex >= endIndex) return ll();
if (startIndex == 0 && endIndex == n) return l;
return l.subList(startIndex, endIndex);
}
static List subList(List l, IntRange r) {
return subList(l, r.start, r.end);
}
static int parseInt(String s) {
return empty(s) ? 0 : Integer.parseInt(s);
}
static int parseInt(char c) {
return Integer.parseInt(str(c));
}
static boolean matchAny(Collection patterns, String s, Matches m) {
for (String pattern : patterns)
if (match(pattern, s, m))
return true;
return false;
}
static boolean matchAny(Collection patterns, String s) {
return matchAny(patterns, s, null);
}
// 2nd version
static boolean matchAny(String pat, Iterable l) {
return matchAny(pat, l, null);
}
static boolean matchAny(String pat, Iterable l, Matches m) {
if (l != null)
for (String s : l)
if (match(pat, s, m))
return true;
return false;
}
static String format_arg(Object arg) {
return format3_formatArg(arg);
}
static String quote(Object o) {
if (o == null) return "null";
return quote(str(o));
}
static String quote(String s) {
if (s == null) return "null";
StringBuilder out = new StringBuilder((int) (l(s)*1.5+2));
quote_impl(s, out);
return out.toString();
}
static void quote_impl(String s, StringBuilder out) {
out.append('"');
int l = s.length();
for (int i = 0; i < l; i++) {
char c = s.charAt(i);
if (c == '\\' || c == '"')
out.append('\\').append(c);
else if (c == '\r')
out.append("\\r");
else if (c == '\n')
out.append("\\n");
else if (c == '\0')
out.append("\\0");
else
out.append(c);
}
out.append('"');
}
static List mL_tlftj(String name) {
return mechList_tlftj(name);
}
static volatile PersistableThrowable _handleException_lastException;
static List _handleException_onException = synchroList(ll("printStackTrace2"));
static void _handleException(Throwable e) {
_handleException_lastException = persistableThrowable(e);
Throwable e2 = innerException(e);
if (e2.getClass() == RuntimeException.class && eq(e2.getMessage(), "Thread cancelled.") || e2 instanceof InterruptedException)
return;
for (Object f : cloneList(_handleException_onException)) try {
callF(f, e);
} catch (Throwable e3) {
printStackTrace2(e3); // not using pcall here - it could lead to endless loops
}
}
static List reversedList(Collection l) {
List x = cloneList(l);
Collections.reverse(x);
return x;
}
static String reversedString(String s) {
return reverseString(s);
}
static List concatLists(Collection ... lists) {
List l = new ArrayList();
if (lists != null) for (Collection list : lists)
if (list != null)
l.addAll(list);
return l;
}
static List concatLists(Collection extends Collection > lists) {
List l = new ArrayList();
if (lists != null) for (Collection list : lists)
if (list != null)
l.addAll(list);
return l;
}
static Object collectionMutex(Object o) {
String c = className(o);
if (eq(c, "java.util.TreeMap$KeySet"))
c = className(o = getOpt(o, "m"));
else if (eq(c, "java.util.HashMap$KeySet"))
c = className(o = get_raw(o, "this$0"));
if (eqOneOf(c, "java.util.TreeMap$AscendingSubMap", "java.util.TreeMap$DescendingSubMap"))
c = className(o = get_raw(o, "m"));
return o;
}
static List matchAll(String pat, Iterable l) {
List l2 = new ArrayList();
Matches m = new Matches();
if (l != null) for (String s : l)
if (match(pat, s, m)) {
l2.add(m);
m = new Matches();
}
return l2;
}
static List javaTokC(String s) {
if (s == null) return null;
int l = s.length();
ArrayList tok = new ArrayList();
int i = 0;
while (i < l) {
int j = i;
char c, d;
// scan for whitespace
while (j < l) {
c = s.charAt(j);
d = j+1 >= l ? '\0' : s.charAt(j+1);
if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
++j;
else if (c == '/' && d == '*') {
do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
j = Math.min(j+2, l);
} else if (c == '/' && d == '/') {
do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
} else
break;
}
i = j;
if (i >= l) break;
c = s.charAt(i);
d = i+1 >= l ? '\0' : s.charAt(i+1);
// scan for non-whitespace
if (c == '\'' || c == '"') {
char opener = c;
++j;
while (j < l) {
if (s.charAt(j) == opener || s.charAt(j) == '\n') { // end at \n to not propagate unclosed string literal errors
++j;
break;
} else if (s.charAt(j) == '\\' && j+1 < l)
j += 2;
else
++j;
}
} else if (Character.isJavaIdentifierStart(c))
do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); // for stuff like "don't"
else if (Character.isDigit(c)) {
do ++j; while (j < l && Character.isDigit(s.charAt(j)));
if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L
} else if (c == '[' && d == '[') {
do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]"));
j = Math.min(j+2, l);
} else if (c == '[' && d == '=' && i+2 < l && s.charAt(i+2) == '[') {
do ++j; while (j+2 < l && !s.substring(j, j+3).equals("]=]"));
j = Math.min(j+3, l);
} else
++j;
tok.add(javaTok_substringC(s, i, j));
i = j;
}
return tok;
}
static boolean eqGet(List l, int i, Object o) {
return eq(get(l, i), o);
}
static boolean eqGet(Map map, A key, Object o) {
return eq(mapGet(map, key), o);
}
static boolean isIdentifier(String s) {
return isJavaIdentifier(s);
}
static boolean swic(String a, String b) {
return startsWithIgnoreCase(a, b);
}
static boolean swic(String a, String b, Matches m) {
if (!swic(a, b)) return false;
m.m = new String[] {substring(a, l(b))};
return true;
}
static boolean ewic(String a, String b) {
return endsWithIgnoreCase(a, b);
}
static boolean ewic(String a, String b, Matches m) {
return endsWithIgnoreCase(a, b, m);
}
static void nlLogic_visit(IfThen rule, VF1