//FILENAME: gazelle/main.java package gazelle; 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 loadableUtils.utils.BetterThreadLocal; import x30_pkg.x30_util; import static x30_pkg.x30_util.VF1; import static x30_pkg.x30_util.l; import static x30_pkg.x30_util.indexOf; import static x30_pkg.x30_util.getOpt; import static x30_pkg.x30_util.setOpt; import static x30_pkg.x30_util.callOpt; import static x30_pkg.x30_util.get; import static x30_pkg.x30_util.assertTrue; import static x30_pkg.x30_util.isTrue; import x30_pkg.x30_util.DynamicObject; import loadableUtils.utils; import loadableUtils.utils.CloseablesHolder; import loadableUtils.utils.UploadedImage; import loadableUtils.utils.UploadedFile; import loadableUtils.utils.WithTimestamp; import static loadableUtils.utils.fail; import static loadableUtils.utils.javaCompileToJar_localLibraries; import static loadableUtils.utils.dm_current_mandatory; import static loadableUtils.utils.match; import static loadableUtils.utils.getField; import static loadableUtils.utils.fieldType; import static loadableUtils.utils.struct; import static loadableUtils.utils.structure; import static loadableUtils.utils.dm_current_generic; import static loadableUtils.utils.dm_current_mandatory_generic; import static loadableUtils.utils.cset; import static loadableUtils.utils.DynamicObject_loading; import static loadableUtils.utils.rethrow; import static loadableUtils.utils.concepts_internStringsLongerThan; import static loadableUtils.utils.realMC_tl; import loadableUtils.utils.F0; import loadableUtils.utils.F1; import loadableUtils.utils.IF0; import loadableUtils.utils.IF1; import loadableUtils.utils.IVF1; import loadableUtils.utils.IVF2; import loadableUtils.utils.Matches; import loadableUtils.utils.Q; import loadableUtils.utils.structure_Data; import loadableUtils.utils.structure_ClassInfo; import loadableUtils.utils.MultiSet; import loadableUtils.utils.Concept; import loadableUtils.utils.Concepts; import loadableUtils.utils.RC; import loadableUtils.utils.IConceptIndex; import loadableUtils.utils.IFieldIndex; import loadableUtils.utils.DynModule; import loadableUtils.utils.Pt; import loadableUtils.utils.SecretValue; import static loadableUtils.utils.dynamicObjectIsLoading_threadLocal; import loadableUtils.utils.HCRUD; import loadableUtils.utils.HCRUD_Data; import loadableUtils.utils.HCRUD_Concepts; import loadableUtils.utils.ValueConverterForField; import loadableUtils.utils.DefaultValueConverterForField; import loadableUtils.utils.OrError; import loadableUtils.utils.HTML; import static loadableUtils.utils.appendParamsToURL; import static loadableUtils.utils.litset; import static loadableUtils.utils.printStackTrace; import static loadableUtils.utils.loadJQuery2; import static loadableUtils.utils.cget; import static loadableUtils.utils.javaTok; import static loadableUtils.utils.getConcept; import static loadableUtils.utils.toTokCondition; import static loadableUtils.utils.targetBlank; import static loadableUtils.utils.b; import static loadableUtils.utils.i; import static loadableUtils.utils.n; import static loadableUtils.utils.p; import static loadableUtils.utils.findConceptWhere; import static loadableUtils.utils.addParamToURL; import static loadableUtils.utils.match2; import static loadableUtils.utils.trueOrNull; import static loadableUtils.utils.empty; import static loadableUtils.utils.containsAngleBracketVars; import static loadableUtils.utils.list; import static loadableUtils.utils.hasMethod; import static loadableUtils.utils.toUtf8; import static loadableUtils.utils.cic; import static loadableUtils.utils.remove; import static loadableUtils.utils.emptyList; import static loadableUtils.utils.programID; import static loadableUtils.utils.hselect; import static loadableUtils.utils.print_byThread; import static loadableUtils.utils.conceptsWhere; import static loadableUtils.utils.addSlash; import static loadableUtils.utils.dm_makeModule; import static loadableUtils.utils.charAt; import static loadableUtils.utils.conceptWhere; import static loadableUtils.utils.tok_identifiersInOrder; import static loadableUtils.utils.grabValueAndCancel; import static loadableUtils.utils.simpleDateFormat; import static loadableUtils.utils.toArray; import static loadableUtils.utils.cloneList; import static loadableUtils.utils.defaultTimerName; import static loadableUtils.utils.regexp; import static loadableUtils.utils.ping; import static loadableUtils.utils.cmp; import static loadableUtils.utils.formatFunctionCall; import static loadableUtils.utils.ahref_onClick; import static loadableUtils.utils.getDBProgramID; import static loadableUtils.utils.countConceptsCI; import static loadableUtils.utils.div_center; import static loadableUtils.utils.divUnlessEmpty; import static loadableUtils.utils.parseLong; import static loadableUtils.utils.uniqCI2; import static loadableUtils.utils.js_setRedirect; import static loadableUtils.utils.massageArgsForVarArgsCall; import static loadableUtils.utils.eqOneOf; import static loadableUtils.utils.joinWithBR; import static loadableUtils.utils.javaxDataDir; import static loadableUtils.utils.substring; import static loadableUtils.utils.toSeconds; import static loadableUtils.utils.getConceptOpt; import static loadableUtils.utils.hhtml; import static loadableUtils.utils.appendBracketed; import static loadableUtils.utils.aGlobalID; import static loadableUtils.utils.removeSubList; import static loadableUtils.utils.javaTokForJFind_array; import static loadableUtils.utils.boostHashCombine; import static loadableUtils.utils.formatDoubleArrowMap_horizontallyAligned; import static loadableUtils.utils.thinAProgramsBackups; import static loadableUtils.utils.tempSetTL; import static loadableUtils.utils.pnl; import static loadableUtils.utils.hPopDownButton; import static loadableUtils.utils.hcenter3; import static loadableUtils.utils.shortClassNameIs; import static loadableUtils.utils.shortName; import static loadableUtils.utils.camelCase; import static loadableUtils.utils.subBot_noCacheHeaders; import static loadableUtils.utils.instancesOf; import static loadableUtils.utils.getProgramID; import static loadableUtils.utils.swic; import static loadableUtils.utils.unnull; import static loadableUtils.utils.findTranslators2; import static loadableUtils.utils.joinWithComma; import static loadableUtils.utils.conceptsWithFieldGreaterThan_sorted; import static loadableUtils.utils.replaceDollarVars; import static loadableUtils.utils.tdTop; import static loadableUtils.utils.subList; import static loadableUtils.utils.formatDouble; import static loadableUtils.utils.evalWithTimeoutOrFail; import static loadableUtils.utils.addAll; import static loadableUtils.utils.getVar; import static loadableUtils.utils.ahref; import static loadableUtils.utils.serveJSON_breakAtLevels; import static loadableUtils.utils.unlock; import static loadableUtils.utils.replaceVars; import static loadableUtils.utils.last; import static loadableUtils.utils.psI; import static loadableUtils.utils.warn; import static loadableUtils.utils.isStringList; import static loadableUtils.utils.plural; import static loadableUtils.utils.trimAll; import static loadableUtils.utils.getClassLoader; import static loadableUtils.utils.htmldecode_dropAllTags; import static loadableUtils.utils.addIfNotNull; import static loadableUtils.utils.distinctCIFieldValuesOfConcepts; import static loadableUtils.utils.startQ; import static loadableUtils.utils.filterKeys; import static loadableUtils.utils.pair; import static loadableUtils.utils.hbutton; import static loadableUtils.utils.parseInt; import static loadableUtils.utils.hbuttonLink; import static loadableUtils.utils.lastIndexOf; import static loadableUtils.utils.parseFirstLong; import static loadableUtils.utils.litciset; import static loadableUtils.utils.joinWithSpace; import static loadableUtils.utils.doAfter; import static loadableUtils.utils.printIndent; import static loadableUtils.utils.put; import static loadableUtils.utils.replaceSublist; import static loadableUtils.utils.fileNotEmpty; import static loadableUtils.utils.emptyObjectArray; import static loadableUtils.utils.deleteConcepts; import static loadableUtils.utils.conceptID; import static loadableUtils.utils.isSubclassOf; import static loadableUtils.utils.joinSubList; import static loadableUtils.utils._hashCode; import static loadableUtils.utils.className; import static loadableUtils.utils.hjs_copyToClipboard; import static loadableUtils.utils.iround; import static loadableUtils.utils.listPlus; import static loadableUtils.utils.dm_ownResource; import static loadableUtils.utils.small; import static loadableUtils.utils.h2_title; import static loadableUtils.utils.sortedIC; import static loadableUtils.utils.addAnchorToURL; import static loadableUtils.utils.pcallF; import static loadableUtils.utils.replace; import static loadableUtils.utils.ownResource; import static loadableUtils.utils.findCodeTokens; import static loadableUtils.utils.regexpFirstGroupIC; import static loadableUtils.utils.wrapInDiv; import static loadableUtils.utils.hjs; import static loadableUtils.utils.ciSet; import static loadableUtils.utils.getClassName; import static loadableUtils.utils.sortedByCalculatedFieldDesc; import static loadableUtils.utils.iterateWithIndex1; import static loadableUtils.utils.sleepSeconds; import static loadableUtils.utils.values; import static loadableUtils.utils.unicodeFromCodePoint; import static loadableUtils.utils.parseLongOpt; import static loadableUtils.utils.toObjectArray; import static loadableUtils.utils.loadTextFile; import static loadableUtils.utils.mapValues; import static loadableUtils.utils.clear; import static loadableUtils.utils.hbody; import static loadableUtils.utils.call_withVarargs; import static loadableUtils.utils.stringIf; import static loadableUtils.utils.third; import static loadableUtils.utils.hsansserif; import static loadableUtils.utils.div_vbar; import static loadableUtils.utils.javaTokWithAngleBrackets; import static loadableUtils.utils.javaCompile_overInternalBot; import static loadableUtils.utils.conceptsWhereCI; import static loadableUtils.utils.callF; import static loadableUtils.utils.eqic_gen; import static loadableUtils.utils.trim; import static loadableUtils.utils.replaceAngleBracketVars_curly; import static loadableUtils.utils.phraseCache; import static loadableUtils.utils.roundToInt; import static loadableUtils.utils.htitle_h2; import static loadableUtils.utils.isStaticMethod; import static loadableUtils.utils.paramsPlus; import static loadableUtils.utils.pre_htmlencode; import static loadableUtils.utils.hscript; import static loadableUtils.utils.exceptionToStringShort; import static loadableUtils.utils.toString; import static loadableUtils.utils.stepAllWithTimeout; import static loadableUtils.utils.sortedByConceptIDDesc; import static loadableUtils.utils.joinNemptiesWithColon; import static loadableUtils.utils.joinNemptiesWithSpace; import static loadableUtils.utils.conceptsWhereIC; import static loadableUtils.utils.lithashset; import static loadableUtils.utils.dropLoadableUtilsPackageFromStruct; import static loadableUtils.utils.setOpt_findField; import static loadableUtils.utils.hashCode; import static loadableUtils.utils.newFileOutputStream; import static loadableUtils.utils.shortClassName; import static loadableUtils.utils.postProcess; import static loadableUtils.utils.htextfield; import static loadableUtils.utils.hhidden; import static loadableUtils.utils.length; import static loadableUtils.utils.print; import static loadableUtils.utils.mapPairs; import static loadableUtils.utils.identityHashCode; import static loadableUtils.utils.lock; import static loadableUtils.utils.findMethodNamed; import static loadableUtils.utils.firstToLower; import static loadableUtils.utils.htmlEncode2_gen; import static loadableUtils.utils.simpleSpacesTrim_javaTok; import static loadableUtils.utils.isSubtypeOf; import static loadableUtils.utils.hpostform; import static loadableUtils.utils.loadFunction_cached; import static loadableUtils.utils.listL; import static loadableUtils.utils.litlist; import static loadableUtils.utils.toMS; import static loadableUtils.utils.map; import static loadableUtils.utils.isStringCollection; import static loadableUtils.utils.max; import static loadableUtils.utils.addParamsToURL; import static loadableUtils.utils.f2s; import static loadableUtils.utils.asCIMap; import static loadableUtils.utils.md5; import static loadableUtils.utils.javaValueToHTML; import static loadableUtils.utils.isTrue_getOpt; import static loadableUtils.utils.isConceptList; import static loadableUtils.utils.getThreadLocal; import static loadableUtils.utils.sort; import static loadableUtils.utils.htmlEncode_quote; import static loadableUtils.utils.indexConceptFieldIC; import static loadableUtils.utils.jextract; import static loadableUtils.utils.hotwire_makeClassLoader; import static loadableUtils.utils.deref; import static loadableUtils.utils.matchesToMapUsingVarList_ciMap; import static loadableUtils.utils.dbBot; import static loadableUtils.utils.putAll; import static loadableUtils.utils.dropSuffix; import static loadableUtils.utils.subBot_serveWithContentType; import static loadableUtils.utils.cForwardAndBackRefsWithFieldIndicator; import static loadableUtils.utils.objectsWhereCI; import static loadableUtils.utils.getStringOpt; import static loadableUtils.utils.pIfNempty; import static loadableUtils.utils.removeAll; import static loadableUtils.utils.elapsedSeconds_sysNow; import static loadableUtils.utils.pairsA; import static loadableUtils.utils.checkFieldsIC; import static loadableUtils.utils.swic_notSame; import static loadableUtils.utils.nemptyString; import static loadableUtils.utils.uncurly; import static loadableUtils.utils.min; import static loadableUtils.utils.arraycopy; import static loadableUtils.utils.htmlTable2_noHtmlEncode; import static loadableUtils.utils.parseSecondLong; import static loadableUtils.utils.cloneMap; import static loadableUtils.utils.ifThenElse; import static loadableUtils.utils.jsQuote; import static loadableUtils.utils.htmlEncode2_nlToBr; import static loadableUtils.utils.ymd_minus_hms; import static loadableUtils.utils.unlisted; import static loadableUtils.utils.hsnippetimg_scaleToWidth; import static loadableUtils.utils.tempAdd; import static loadableUtils.utils.compileRegexp; import static loadableUtils.utils.evalWithTimeout; import static loadableUtils.utils.dbLock; import static loadableUtils.utils.lookupPossiblyCI; import static loadableUtils.utils.strOrNull; import static loadableUtils.utils.concatMap; import static loadableUtils.utils.isAngleBracketVar; import static loadableUtils.utils._close; import static loadableUtils.utils.starsToAngleBracketedNumbers; import static loadableUtils.utils.countPred; import static loadableUtils.utils.close_pcall; import static loadableUtils.utils.wrapPatternSyntaxException; import static loadableUtils.utils._entrySet; import static loadableUtils.utils.first; import static loadableUtils.utils.span; import static loadableUtils.utils.litorderedmap; import static loadableUtils.utils.multiSetToPairsByPopularity; import static loadableUtils.utils.objectToMap; import static loadableUtils.utils.renderEqualsCommaProperties; import static loadableUtils.utils.hjs_autoExpandingTextAreas; import static loadableUtils.utils.hmobilefix; import static loadableUtils.utils.printVars_str; import static loadableUtils.utils.hotwire; import static loadableUtils.utils.span_title; import static loadableUtils.utils.hNotificationPopups; import static loadableUtils.utils.firstNotNull; import static loadableUtils.utils.strL; import static loadableUtils.utils.score; import static loadableUtils.utils.quote; import static loadableUtils.utils.htmlEncode_nlToBr_withIndents; import static loadableUtils.utils.htmlTable2; import static loadableUtils.utils.llNonNulls; import static loadableUtils.utils.contains; import static loadableUtils.utils.countConceptsWhereCI; import static loadableUtils.utils.uniqCI; import static loadableUtils.utils.jsonEncodeMap; import static loadableUtils.utils.sleep; import static loadableUtils.utils.allConcepts; import static loadableUtils.utils.isB; import static loadableUtils.utils.uniquifyCI; import static loadableUtils.utils.close; import static loadableUtils.utils.isFile; import static loadableUtils.utils.conceptWhereIC; import static loadableUtils.utils.swing; import static loadableUtils.utils.findEndOfBracketPart; import static loadableUtils.utils._getClass; import static loadableUtils.utils.indentStructureString_firstLevels; import static loadableUtils.utils.add; import static loadableUtils.utils.ciMultiMap; import static loadableUtils.utils.mapKeys; import static loadableUtils.utils.clearTokens; import static loadableUtils.utils.subBot_serve500; import static loadableUtils.utils.saveTextFile; import static loadableUtils.utils.conceptWhereCI; import static loadableUtils.utils.pnlToString; import static loadableUtils.utils.emptyDoubleArray; import static loadableUtils.utils.lines; import static loadableUtils.utils.unquote; import static loadableUtils.utils.nempty; import static loadableUtils.utils.isEmpty; import static loadableUtils.utils.dm_os; import static loadableUtils.utils.serveText; import static loadableUtils.utils.sortByConceptID; import static loadableUtils.utils.vm_busListenersByMessage_live; import static loadableUtils.utils.shorten; import static loadableUtils.utils.jfindAll; import static loadableUtils.utils.litmap; import static loadableUtils.utils.weakSet; import static loadableUtils.utils.myClassLoader; import static loadableUtils.utils.findConceptsWhereCI; import static loadableUtils.utils.jfind_preprocess; import static loadableUtils.utils.hrefresh; import static loadableUtils.utils.fileInSameDir; import static loadableUtils.utils.synchronizedSet; import static loadableUtils.utils.countConceptsWhereIC; import static loadableUtils.utils.beforeVerticalBar; import static loadableUtils.utils.isIdentifier; import static loadableUtils.utils.getTimeZone; import static loadableUtils.utils.dropTrailingSlashIfNemptyAfterwards; import static loadableUtils.utils.neq; import static loadableUtils.utils.all; import static loadableUtils.utils.paramsPlus_inFront; import static loadableUtils.utils.inputText; import static loadableUtils.utils.regexpMatcherIC; import static loadableUtils.utils.uniq; import static loadableUtils.utils.nullIfEmpty; import static loadableUtils.utils.isSafeStandardFunction; import static loadableUtils.utils.indent; import static loadableUtils.utils.angleBracketVarsToStars; import static loadableUtils.utils.derefRef; import static loadableUtils.utils.dbBotStandardName; import static loadableUtils.utils.joinNempties; import static loadableUtils.utils.keySet; import static loadableUtils.utils.optCast; import static loadableUtils.utils.emptyIntArray; import static loadableUtils.utils.reversed; import static loadableUtils.utils.stdcompare; import static loadableUtils.utils.eqGetOneOf; import static loadableUtils.utils.done2_always; import static loadableUtils.utils.isInstance; import static loadableUtils.utils.regexpReplace; import static loadableUtils.utils.programFile; import static loadableUtils.utils.dropFirstAndLast; import static loadableUtils.utils.dm_doEvery; import static loadableUtils.utils.setOptAllDyn_pcall; import static loadableUtils.utils.onIndividualConceptChange_notOnAllChanged; import static loadableUtils.utils.pcallOpt; import static loadableUtils.utils.nonNulls; import static loadableUtils.utils.hinputfield; import static loadableUtils.utils.asCIMultiSet; import static loadableUtils.utils.dm_getModule; import static loadableUtils.utils.deleteConcept; import static loadableUtils.utils.replaceAngleBracketVars; import static loadableUtils.utils.litobjectarray; import static loadableUtils.utils.getMainBot; import static loadableUtils.utils.strOrEmpty; import static loadableUtils.utils.call; import static loadableUtils.utils.cnew; import static loadableUtils.utils.uniqCI_returnIfNew; import static loadableUtils.utils.run; import static loadableUtils.utils.isInteger; import static loadableUtils.utils.indexConceptFieldDesc; import static loadableUtils.utils.cdelete; import static loadableUtils.utils.ciMap; import static loadableUtils.utils.flexMatchIC_iterate; import static loadableUtils.utils.snippetLink; import static loadableUtils.utils.now; import static loadableUtils.utils.smartTimerTask; import static loadableUtils.utils.csetIfUnset; import static loadableUtils.utils.hsubmit; import static loadableUtils.utils.eqicOneOf; import static loadableUtils.utils.htitle; import static loadableUtils.utils.lastException; import static loadableUtils.utils.splitAtSpace; import static loadableUtils.utils.htmlEncode2; import static loadableUtils.utils.optPar; import static loadableUtils.utils.javaxCachesDir; import static loadableUtils.utils.h1_title; import static loadableUtils.utils.singleObjectMethodProxy; import static loadableUtils.utils.sameSnippetID; import static loadableUtils.utils.compareIC; import static loadableUtils.utils.callFAll; import static loadableUtils.utils.listZip; import static loadableUtils.utils.match3_withIntsAndIDs; import static loadableUtils.utils.getClass; import static loadableUtils.utils.syncIdentityHashSet; import static loadableUtils.utils.containsKey; import static loadableUtils.utils.jsonEncode; import static loadableUtils.utils.regexpMatcher; import static loadableUtils.utils.collectionMutex; import static loadableUtils.utils.joinNemptiesWithVBar; import static loadableUtils.utils.mapGet; import static loadableUtils.utils.parseSnippetID; import static loadableUtils.utils.conceptFields; import static loadableUtils.utils.firstToUpper; import static loadableUtils.utils.asList; import static loadableUtils.utils.getType; import static loadableUtils.utils.endsWith; import static loadableUtils.utils.listSet; import static loadableUtils.utils.hfulltag; import static loadableUtils.utils.allBackRefs; import static loadableUtils.utils.vmBus_send; import static loadableUtils.utils.dm_getStem; import static loadableUtils.utils.set; import static loadableUtils.utils.hselect_list; import static loadableUtils.utils.dm_rEnter; import static loadableUtils.utils.loadLibraryOrSrcLib; import static loadableUtils.utils.callOpt_getCache; import static loadableUtils.utils.sfu; import static loadableUtils.utils.getAndClearThreadLocal; import static loadableUtils.utils.getStackTrace; import static loadableUtils.utils.javax; import static loadableUtils.utils.isQuoted; import static loadableUtils.utils.emptyCharArray; import static loadableUtils.utils.hhiddenMulti; import static loadableUtils.utils.isFalse; import static loadableUtils.utils.eq; import static loadableUtils.utils.isDigit; import static loadableUtils.utils.hinlinepostform; import static loadableUtils.utils.dm_callOS; import static loadableUtils.utils.isDirectory; import static loadableUtils.utils.hform; import static loadableUtils.utils.h1; import static loadableUtils.utils.h2; import static loadableUtils.utils.h3; import static loadableUtils.utils.random; import static loadableUtils.utils.trimSubstring; import static loadableUtils.utils.md5OrNull; import static loadableUtils.utils.pcallFAll; import static loadableUtils.utils.synchronizedMRUCache; import static loadableUtils.utils.getString; import static loadableUtils.utils.conceptsFile; import static loadableUtils.utils.urlencode; import static loadableUtils.utils.hr; import static loadableUtils.utils.dm_call; import static loadableUtils.utils.saveTiming_tl; import static loadableUtils.utils.join; import static loadableUtils.utils.fsI; import static loadableUtils.utils.binaryMimeType; import static loadableUtils.utils.odd; import static loadableUtils.utils.isInstanceOf; import static loadableUtils.utils.lmap; import static loadableUtils.utils.ahref_undecorated; import static loadableUtils.utils.javaTokForStructure; import static loadableUtils.utils.change; import static loadableUtils.utils.vmBus_query; import static loadableUtils.utils.assertEquals; import static loadableUtils.utils.joinMap; import static loadableUtils.utils.tlft; import static loadableUtils.utils.dm_vmBus_onMessage; import static loadableUtils.utils.endsWithLetterOrDigit; import static loadableUtils.utils.dm_startQ; import static loadableUtils.utils.ll; import static loadableUtils.utils.concatArrays; import static loadableUtils.utils.n2; import static loadableUtils.utils.lines_rtrim; import static loadableUtils.utils.second; import static loadableUtils.utils.replaceTokens; import static loadableUtils.utils.takeFirst; import static loadableUtils.utils.hparagraphs; import static loadableUtils.utils.entrySet; import static loadableUtils.utils.jsonEncode_breakAtLevel1; import static loadableUtils.utils.codeTokensAsStringArray; import static loadableUtils.utils.hijackPrint; import static loadableUtils.utils.keys; import static loadableUtils.utils.uniqCI2_sync; import static loadableUtils.utils.smartIndexOf; import static loadableUtils.utils.assertIdentifier; import static loadableUtils.utils.ol; import static loadableUtils.utils.or; import static loadableUtils.utils.syncMRUCache; import static loadableUtils.utils.dropPunctuation; import static loadableUtils.utils.rtrim; import static loadableUtils.utils.iterator; import static loadableUtils.utils.assertNotNull; import static loadableUtils.utils.currentThread; import static loadableUtils.utils.transpileRaw; import static loadableUtils.utils.str; import static loadableUtils.utils.tempInterceptPrint; import static loadableUtils.utils.printWithIndent; import static loadableUtils.utils.appendRoundBracketed; import static loadableUtils.utils.hideCredentials; import static loadableUtils.utils.intRange; import static loadableUtils.utils.intern; import static loadableUtils.utils.asCISet; import static loadableUtils.utils.syncMapPut2_createLinkedHashMap; import static loadableUtils.utils.th; import static loadableUtils.utils.syncMapToPairs; import static loadableUtils.utils.tr; import static loadableUtils.utils.tt; import static loadableUtils.utils.dm_moduleID; import static loadableUtils.utils.conceptIDOrNull; import static loadableUtils.utils.ul; import static loadableUtils.utils.programDir; import static loadableUtils.utils.or2; import static loadableUtils.utils.similarEmptyMap; import static loadableUtils.utils._get; import static loadableUtils.utils.div_alignRight; import static loadableUtils.utils.gazelle_allSyntacticPatternsFromInput; import static loadableUtils.utils.getConceptFieldCIIndex; import static loadableUtils.utils.ul_htmlEncode; import static loadableUtils.utils.checkFields; import static loadableUtils.utils.eqic; import static loadableUtils.utils.hpre_htmlencode; import static loadableUtils.utils.pairA; import static loadableUtils.utils.parseIntOpt; import static loadableUtils.utils._handleException; import static loadableUtils.utils.dropFirst; import static loadableUtils.utils.pairB; import static loadableUtils.utils.htitledSectionWithDiv; import static loadableUtils.utils.urldecode; import static loadableUtils.utils.nConcepts; import static loadableUtils.utils.dropPrefix; import static loadableUtils.utils.deAngleBracket; import static loadableUtils.utils.emptyMap; import static loadableUtils.utils.primitiveToBoxedTypeOpt; import static loadableUtils.utils.tok_integersInOrder; import static loadableUtils.utils.getClientIPFromHeaders; import static loadableUtils.utils.allToString; import static loadableUtils.utils.filter; import static loadableUtils.utils.getWeakRef; import static loadableUtils.utils.ensureConceptClassIsIndexed; import static loadableUtils.utils.ensureDBNotRunning; import static loadableUtils.utils.findStaticMethod; import static loadableUtils.utils.findBackRefs; import static loadableUtils.utils.nempties; import static loadableUtils.utils.htmlEncode_nlToBR; import static loadableUtils.utils.tok_findEndOfStatement; import static loadableUtils.utils.tag; import static loadableUtils.utils.syncL; import static loadableUtils.utils.htextarea; import static loadableUtils.utils.hhead; import static loadableUtils.utils.toLong; import static loadableUtils.utils.localTimeWithSeconds; import static loadableUtils.utils.hjs_selectize; import static loadableUtils.utils.concatLists; import static loadableUtils.utils.invokeMethod; import static loadableUtils.utils.isInQ; import static loadableUtils.utils.countTokens; import static loadableUtils.utils.sysNow; import static loadableUtils.utils.countConcepts; import static loadableUtils.utils.hPopDownButtonWithText; import static loadableUtils.utils.countWordsWithoutAngleBracketedAndStars; import loadableUtils.utils.Var; import loadableUtils.utils.Scorer; import loadableUtils.utils.ITokCondition; import loadableUtils.utils.ConceptsRefChecker; import loadableUtils.utils.FixedRateTimer; import loadableUtils.utils.IterableIterator; import loadableUtils.utils.F2; import loadableUtils.utils.Flag; import loadableUtils.utils.StringBufferWithMaxSize; import loadableUtils.utils.IFieldsToList; import loadableUtils.utils.IF2; import loadableUtils.utils.DynNewBot2; import loadableUtils.utils._MethodCache; import loadableUtils.utils.DynGazelleRocks; import loadableUtils.utils.ConceptWithGlobalID; import loadableUtils.utils.MultiMap; import loadableUtils.utils.HTMLTabs; import loadableUtils.utils.BreadthFirstPathFinder_withLinkType; import loadableUtils.utils.ReliableSingleThread_Multi; import loadableUtils.utils.Steppable; import loadableUtils.utils.Pair; import loadableUtils.utils.HTMLFramer1; import loadableUtils.utils.JavaXClassLoader; import loadableUtils.utils.IntRange; import loadableUtils.utils.FieldIndicator; import loadableUtils.utils.JavaXClassLoaderWithParent2; import loadableUtils.utils.HDivWithVerticalExpandButton; import loadableUtils.utils.ConceptFieldIndexCI_withTopTen; import loadableUtils.utils.Timed; import loadableUtils.utils.CMissingObject; import loadableUtils.utils.SteppableAndIF0; import loadableUtils.utils.ConceptsShadowLogger; import loadableUtils.utils.ConceptClassesDependentValue; import loadableUtils.utils.RealmCopy; import loadableUtils.utils.ConceptShadow; import loadableUtils.utils.HTMLPopDownButton; import loadableUtils.utils.TransitiveHull; import loadableUtils.utils.CreatedDeletedChanged; import loadableUtils.utils.HTMLPaginator; import loadableUtils.utils.User; import loadableUtils.utils.UserPost; import loadableUtils.utils.Conversation; import loadableUtils.utils.ExecutedStep; import loadableUtils.utils.InputHandled; import loadableUtils.utils.AuthedDialogID; import loadableUtils.utils.UploadedSound; import static loadableUtils.utils.passwordSalt; import java.text.*; public class main { static public class GazelleMonitor extends GazelleBEA { public void init() { super.init(); botName = heading = adminName = "Gazelle Monitor"; } } static public boolean _moduleClass_GazelleMonitor = true; static public String mainLibID = "#1030952"; static public String programID; static public void _onLoad_initUtils() { utils.__javax = javax(); } static public ThreadLocal dynamicObjectIsLoading_threadLocal() { return DynamicObject_loading; } static public class BEACalculations { public GazelleBEA mod; public BEACalculations(GazelleBEA mod) { this.mod = mod; } transient public int liveOutputDelay = 1000; transient public double algoTimeout = 30.0; public void reactAllInputsWithSomePatterns() { for (BEAObject input : mod.beaObjectsOfType("input")) { ReactInputWithSomePatterns r = new ReactInputWithSomePatterns(); r.input = input; r.ccOut = db_mainConcepts(); r.max = 10; r.start(); stepAllWithTimeout(10.0, r); } } public void reactAllInputsWithAllSyntacticPatterns() { for (BEAObject input : mod.beaList("input")) { ping(); for (BEAObject pattern : mod.beaList("Syntactic Pattern")) { ping(); { if (patternAlwaysMatches(pattern)) continue; } reactInputWithPattern(input, pattern); } } } public void reactAllInputsWithPattern(BEAObject pattern) { for (BEAObject input : mod.beaList("input")) { ping(); reactInputWithPattern(input, pattern); } } public boolean patternAlwaysMatches(BEAObject pat) { return isTrue_getOpt("alwaysMatches", pat); } public class ReactInputWithSomePatterns implements Steppable { public BEAObject input; public Concepts ccOut; public int max = 3, n; public Iterator patterns; public void start() { n = countConceptsWhereIC(ccOut, BEAObject.class, "input", input, "type", "match"); patterns = iterator(sortByConceptID(conceptsWhereIC(BEAObject.class, "type", "Pattern"))); } public boolean step() { if (n >= max) return false; if (!patterns.hasNext()) return false; if (reactInputWithPattern(input, patterns.next(), ccOut) != null) ++n; return true; } } public Pair> matchInputWithPatternList(String text, BEAObject patternList) { List patterns = (List) (cget("patterns", patternList)); for (BEAObject pattern : unnullForIteration(patterns)) { Map mapping = matchInputWithPattern(text, pattern); if (mapping != null) return pair(pattern, mapping); } return null; } public ThreadLocal matchInput_droppedPunctuation = new ThreadLocal(); public Map matchInputWithPattern(BEAObject input, BEAObject pattern) { String text = getString("text", input); return matchInputWithPattern(text, pattern); } public Map matchInputWithPattern(String text, BEAObject pattern) { String pat = getString("text", pattern); boolean punctuationOnly = isTrue_getOpt("punctuationOnly", pattern); if (pattern.typeIs("Syntactic Pattern")) return matchInputWithSyntacticPattern(text, pat, punctuationOnly); else if (eqic_gen(getOpt("subType", pattern), "PhraseCache")) return phraseCache(pat, text) ? litmap() : null; else return matchInputWithPattern(text, pat, punctuationOnly); } public Map matchInputWithSyntacticPattern(String text, String pat) { return matchInputWithSyntacticPattern(text, pat, false); } public Map matchInputWithSyntacticPattern(String text, String pat, boolean punctuationOnly) { pat = starsToAngleBracketedNumbers(pat); return matchInputWithPattern(text, pat, punctuationOnly); } public Map matchInputWithPattern(String text, String pat) { return matchInputWithPattern(text, pat, false); } public Map matchInputWithPattern(String text, String pat, boolean punctuationOnly) { Map mapping = flexMatchAngleBracketVarsIC_honorPunctuation_noBrackets_first(pat, text); if (mapping != null || punctuationOnly) return mapping; return null; } public BEAObject reactInputWithPattern(BEAObject input, BEAObject pattern) { return reactInputWithPattern(input, pattern, db_mainConcepts()); } public BEAObject reactInputWithPattern(BEAObject input, BEAObject pattern, Concepts ccOut) { if (input == null || pattern == null) return null; matchInput_droppedPunctuation.set(false); Map mapping = matchInputWithPattern(input, pattern); if (mapping == null) return null; BEAObject match = uniqCI(ccOut, BEAObject.class, "type", "Match", "input", input, "pattern", pattern); cset(match, "mapping", mapping, "droppedPunctuation", trueOrNull(matchInput_droppedPunctuation.get())); return match; } public Object serveAnalyzeInput(GazelleBEA.Req req) { Concepts ccOut = new Concepts(); BEAObject input = getConcept(BEAObject.class, parseLong(req.get("id"))); if (input == null) input = cnew(ccOut, BEAObject.class, "type", "Input", "text", req.get("q")); String text = getString("text", input); ReactInputWithSomePatterns r = new ReactInputWithSomePatterns(); r.input = input; r.ccOut = ccOut; r.max = 1000; r.start(); stepAllWithTimeout(10.0, r); Collection matches = conceptsWhereIC(ccOut, BEAObject.class, "type", "Match"); return h1_title("Analyze Input") + p("Input: " + b(htmlEncode_nlToBr_withIndents(text))) + p(empty(matches) ? "No matches" : "Matches") + ul_htmlEncode(matches); } public Object serveQueryPage(GazelleBEA.Req req) { return serveQueryPage(req, false); } public Object serveQueryPage(GazelleBEA.Req req, boolean withFrame) { String q = req.get("q"), algorithm = req.get("algorithm"); boolean dontRun = eq(req.get("dontRun"), "1"); boolean liveOutput = eq(req.get("liveOutput"), "1"); if (liveOutput) req.noSpam(); if (eqic(algorithm, "process input")) algorithm = "Process new input v3"; Map> algorithms = algorithms(); String output = empty(algorithm) || dontRun ? null : doAlgorithm(algorithms, algorithm, q, liveOutput); if (liveOutput) return jsonEncode(litorderedmap("answer", output)); String right = withFrame ? "" : mod.html_loggedIn(); Algorithm alg = callF(lookupPossiblyCI(algorithms, algorithm)); if (alg != null) { alg.q = q; alg.req = mod.currentReq(); } String centered = (withFrame ? "" : htitle_h2(ahref(mod.baseLink + "/", "Gazelle") + " Query" + htmlEncode2((empty(q) ? "" : ": " + q) + appendSquareBracketed(algorithm)))) + hscript(replaceVars("\r\n var sugLoading = false, sugTriggerAgain = false;\r\n\r\n function sugTrigger() {\r\n console.log(\"sugTrigger\");\r\n if (sugLoading) { sugTriggerAgain = true; return; }\r\n\r\n // get form data as JSON\r\n \r\n var data = {\r\n \"liveOutput\" : \"1\",\r\n \"q\": $(\"textarea[name=q]\").val(),\r\n \"algorithm\": $(\"select[name=algorithm]\").val(),\r\n \"algorithmID\": $(\"select[name=algorithmID]\").val(), // XXX: do this for all additional alg parameters\r\n };\r\n \r\n const url = \"/query\";\r\n console.log(\"Loading \" + url + \" with \" + data);\r\n sugLoading = true;\r\n $.post(url, data,\r\n function(result) {\r\n console.log(\"Suggestor result: \" + result);\r\n const answer = !result ? \"\" : JSON.parse(result).answer;\r\n if (answer) {\r\n $(\"#liveResult .sectionContents\").html(answer);\r\n $(\"#liveResult\").show();\r\n } else\r\n $(\"#liveResult\").hide();\r\n //$(\"#suggestorResult\").html(answer ? \"Suggestor says: \" + answer : \"\");\r\n }\r\n ).always(function() {\r\n console.log(\"sug loading done\");\r\n setTimeout(function() {\r\n sugLoading = false;\r\n if (sugTriggerAgain) { sugTriggerAgain = false; sugTrigger(); }\r\n }, liveOutputDelay);\r\n });\r\n }\r\n\r\n $(document).ready(function() {\r\n $(\"textarea[name=q], input[name=q], select[name=algorithm]\").on('input propertychange change', sugTrigger);\r\n sugTrigger();\r\n });\r\n ", "liveOutputDelay", liveOutputDelay)) + hform(p("Query (Ctrl+Enter): " + htextarea(q, "id", "mainInput", "name", "q", "class", "auto-expand", "style", "width: 300px", "onkeydown", jquery_submitFormOnCtrlEnter(), "autofocus", dontRun || empty(q) ? true : null) + " " + (alg == null ? "" : alg.additionalInputs()) + "   Algorithm: " + hselect_list(keys(algorithms), algorithm, "name", "algorithm") + "   " + hbutton("Go"))); String content = (empty(right) ? "" : div_alignRight(right)) + div_center(centered) + htitledSectionWithDiv("Live Result", "", "id", "liveResult", "style", "display: none", "innerDivStyle", "max-height: 150px; overflow: auto") + (output == null ? "" : h3(algorithm) + output); if (withFrame) { req.framer.addInHead(hjs_handleScrollToAnchor()); req.framer.add(content); return mod.completeFrame(req); } else return hhtml(hhead(hmobilefix() + hsansserif() + loadJQuery2() + hjs_handleScrollToAnchor() + hjs_autoExpandingTextAreas() + mod.webSocketHeadStuff(req) + hNotificationPopups()) + hbody(content)); } public String doAlgorithm(Map> algorithms, String algorithm, String q, boolean liveMode) { GazelleBEA.Req req = mod.currentReq(); StringBufferWithMaxSize out = new StringBufferWithMaxSize(1000000); IF0 algo = lookupPossiblyCI(algorithms, algorithm); if (algo == null) out.append("Algorithm not found: " + algorithm); else { if (isB(evalWithTimeout(algoTimeout, new Runnable() { public void run() { try { AutoCloseable __21 = mod.enter(); try { AutoCloseable __22 = tempSetTL(mod.currentReq, req); try { Algorithm alg = algo.get(); alg.liveMode = liveMode; alg.out = out; alg.q = q; alg.req = mod.currentReq(); alg.runIt(); } finally { _close(__22); } } finally { _close(__21); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "temp mod.enter();\r\n temp tempSetTL(mod.currentReq, req);\r\n Algo..."; } }))) out.append("\n[algorithm timeout]"); } return str(out); } public class Algorithm implements Runnable { public String q; public GazelleBEA.Req req; public Appendable out; public boolean liveMode = false; public Concepts localConcepts = db_mainConcepts(); public List nonMatches; public void run() { } public void runLiveMode() { } public String additionalInputs() { return ""; } public void appendHTML(String html) { appendHTML(this.out, html); } public void appendHTML(Appendable out, String html) { try { if (empty(html)) return; html = wrapInDiv(html, "style", "margin-top: 0.5em"); out.append(html).append("\n"); } catch (Exception __e) { throw rethrow(__e); } } public void appendHTML_raw(String html) { try { out.append(html).append("\n"); } catch (Exception __e) { throw rethrow(__e); } } public void appendText(String text) { appendText(this.out, text); } public void appendText(Appendable out, String text) { try { if (empty(text)) return; out.append(hpre_htmlencode(rtrim(text))); } catch (Exception __e) { throw rethrow(__e); } } public void appendTextBold(String text) { appendTextBold(this.out, text); } public void appendTextBold(Appendable out, String text) { try { if (empty(text)) return; out.append(b(hpre_htmlencode(rtrim(text)))); } catch (Exception __e) { throw rethrow(__e); } } public void allowSuggestingAnswer(BEAObject input) { if (input == null) return; String text = input.text(); String url = mod.baseLink + "/saveAnswer"; appendHTML(hpostform("Suggest an answer to " + htmlEncode_quote(text) + ": " + htextarea(text, "id", "suggestAnswer", "name", "text", "class", "auto-expand", "style", "width: 300px") + hhidden("inputID", input.id) + hhidden("redirect", "") + " " + hsubmit("Save answer") + " [Optional rewrite type: " + hinputfield("rewriteType", "style", "width: 100px") + " ]", "action", url, "onsubmit", js_setRedirect())); } public void allowSuggestingPattern(BEAObject input) { if (input == null) return; String text = input.text(); String url = mod.baseLink + "/savePattern"; appendHTML(hpostform("Suggest a pattern (with angle bracket vars): " + htextarea(text, "name", "text", "class", "auto-expand", "style", "width: 300px") + hhidden("fromInput", conceptIDOrNull(input)) + hhidden("userTyped", true) + hhidden("redirect", "") + " " + hsubmit("Save pattern"), "action", url, "onsubmit", js_setRedirect())); } public void allowSuggestingSyntacticPattern(BEAObject input) { if (input == null) return; String text = input.text(); String url = mod.baseLink + "/saveSyntacticPattern"; appendHTML(hpostform("Suggest a syntactic pattern (with * vars): " + htextarea(text, "name", "text", "class", "auto-expand", "style", "width: 300px") + hhidden("fromInput", conceptIDOrNull(input)) + hhidden("userTyped", true) + hhidden("redirect", "") + " " + hsubmit("Save syntactic pattern"), "action", url, "onsubmit", js_setRedirect())); } public BEAObject saveInput(String q) { if (userAgentIsBot(beaMod().currentUserAgent())) return null; Pair p = uniqCI2(BEAObject.class, "type", "Input", "text", q); BEAObject input = p.a; mod.saveUserAgent(input); if (p.b) { csetIfUnset(input, "createdBy", mod.user(mod.currentReq())); appendHTML("Input saved"); } return input; } public void runNonMatches() { if (nempty(nonMatches)) { appendHTML(hr()); callFAll(nonMatches); } } public void addNonMatch(Runnable r) { add(nonMatches, r); } public void runIt() { if (liveMode) runLiveMode(); else run(); runNonMatches(); } } public class ReverseInput extends Algorithm { public void run() { appendText(reversed(q)); } public void runLiveMode() { run(); } } public class TimeoutTest extends Algorithm { public void run() { sleepSeconds(120); } } public class RunPattern extends Algorithm { public void run() { BEAObject pattern = mod.beaGet(parseFirstLong(q)); if (pattern == null) { appendText("Pattern not found"); return; } appendText("Pattern: " + pattern); for (BEAObject input : mod.beaList("Input")) { String text = input.text(); Map mapping = matchInputWithPattern(input, pattern); if (mapping == null) addNonMatch(new Runnable() { public void run() { try { appendText("Checking Input: " + input + " - No match"); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "appendText(\"Checking Input: \" + input + \" - No match\");"; } }); else { String result = "\n" + renderMappingAsText(mapping); appendText("Checking Input: " + input + result); allowStoringMatch(this, input, pattern, mapping); } } } } public String renderMappingAsText(Map mapping) { return indent(formatDoubleArrowMap_horizontallyAligned(mapping)); } public class FindRewrites extends Algorithm { public void run() { BEAObject match = mod.beaGet(parseFirstLong(q)); if (match == null || !match.typeIs("Match")) { appendText("Match not found"); return; } BEAObject pattern = (BEAObject) (cget("pattern", match)); BEAObject input = (BEAObject) (cget("input", match)); Map mapping = (Map) (get("mapping", match)); appendText("Match: " + match); appendText("Input: " + input); appendText("Pattern: " + pattern); appendText("Mapping:\n" + renderMappingAsText(mapping)); appendRewrites(this, input, input.text(), pattern, match, mapping); } } public List appendRewrites(Algorithm algo, BEAObject input, String inputText, BEAObject pattern, BEAObject match, Map mapping) { Collection rewrites = mod.beaBackRefs(pattern, "Rewrite"); List texts = new ArrayList(); for (BEAObject r : rewrites) { String text = r.text(); String textWithVars = text; if (pattern.typeIs("Syntactic pattern")) textWithVars = starsToAngleBracketedNumbers(text); String text2 = replaceAngleBracketVars_curly(textWithVars, mapping); algo.appendText(" Using pattern rewrite: " + text); algo.appendTextBold(" " + inputText + "\n" + " => " + text2); texts.add(text2); BEAObject existing = match == null || input == null ? null : conceptWhereIC(BEAObject.class, "type", "Rewrite", "input", input, "text", text2, "match", match, "patternRewrite", r, "label", "good"); if (existing != null) algo.appendHTML("Rewrite exists: " + mod.beaLinkHTML(existing)); else if (match == null) algo.appendText("[Can't save, no match]"); else if (input == null) algo.appendText("[Can't save, no input object]"); else algo.appendHTML(hinlinepostform(hhiddenMulti("action", "create", "f_type", "Rewrite", "f_text", text2, "f_input", input.id, "metaInfo_input", "concept", "f_match", match.id, "metaInfo_match", "concept", "f_patternRewrite", r.id, "metaInfo_patternRewrite", "concept", "f_label", "good") + hbutton("Good rewrite"), "target", "_blank", "action", mod.crudLink(BEAObject.class))); } return texts; } public class ProcessNewInput extends Algorithm { public void run() { BEAObject input = handleInput(this, q = trim(q), false); for (BEAObject pattern : mod.beaListAny("Pattern", "Syntactic Pattern")) { checkPattern(this, input, q, pattern); } } } public void checkPattern(Algorithm algo, BEAObject input, String q, BEAObject pattern) { Map mapping = matchInputWithPattern(q, pattern); if (mapping == null) algo.addNonMatch(new Runnable() { public void run() { try { algo.appendText("Checking Pattern: " + pattern + " - No match"); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "algo.appendText(\"Checking Pattern: \" + pattern + \" - No match\");"; } }); else { String result = "\n" + renderMappingAsText(mapping); algo.appendText("Checking Pattern: " + pattern + result); if (input == null) { } else allowStoringMatch(algo, input, pattern, mapping); appendRewrites(algo, input, q, pattern, null, mapping); } } public class ProcessNewInput_v2 extends Algorithm { public void run() { q = trim(q); saveInput(q); BEAObject input = handleInput(this, q, false); if (input != null) { Collection rewrites = mod.beaBackRefs(input, "Rewrite"); BEAObject rewrite = random(rewrites); if (rewrite != null) appendHTML("My answer: " + b(htmlEncode2(rewrite.text())) + " " + ahref(mod.beaShortURL(rewrite), "[source]")); } allowSuggestingAnswer(input); allowSuggestingPattern(input); allowSuggestingSyntacticPattern(input); for (BEAObject pattern : syntacticPatternsSortedBySpecificityDesc()) { { if (patternAlwaysMatches(pattern)) continue; } Map mapping = matchInputWithPattern(q, pattern); if (mapping == null) addNonMatch(new Runnable() { public void run() { try { appendHTML("Checking Syntactic Pattern: " + mod.beaLinkHTML(pattern) + " - No match"); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "appendHTML(\"Checking Syntactic Pattern: \" + mod.beaLinkHTML(pattern) + \" - No..."; } }); else { String result = "Grouped as: " + replaceAngleBracketVars_curly(starsToAngleBracketedNumbers(pattern.text()), mapping) + "\n" + renderMappingAsText(mapping); appendHTML(hr()); appendHTML("Checking Syntactic Pattern: " + mod.beaLinkHTML(pattern)); appendText(result); BEAObject match = null; if (input == null) { } else match = allowStoringMatch(this, input, pattern, mapping); MultiMap subInputRewrites = ciMultiMap(); for (String subInput : uniquifyCI(values(mapping))) if (!eqic(subInput, q)) { BEAObject subInputObj = handleInput(this, subInput, "Sub-Input", true); if (subInputObj != null) { List subRewrites = concatLists(conceptsWhereCI(BEAObject.class, "type", "Rewrite", "input", subInputObj), conceptsWhereCI(BEAObject.class, "type", "Rewrite", "isRewriteOf", subInputObj)); for (BEAObject r : subRewrites) appendHTML("  Sub-Rewrite found: " + r); subInputRewrites.putAll(subInput, subRewrites); } } appendRewrites(this, input, q, pattern, match, mapping); Map rewrittenMapping = mapValues(mapping, s -> { BEAObject rewrite = random(subInputRewrites.get(s)); return rewrite == null ? s : uncurly(rewrite.text()); }); String randomRewrite = replaceAngleBracketVars_curly(starsToAngleBracketedNumbers(pattern.text()), rewrittenMapping); if (neq(randomRewrite, q)) appendText("Random rewrite from sub-inputs: " + randomRewrite); } } } } public class ProcessNewInput_v3 extends Algorithm { public void run() { q = trim(q); saveInput(q); BEAObject input = handleInput(this, q, false); HTMLTabs tabs = new HTMLTabs(); { StringBuffer toTab = new StringBuffer(); Algorithm alg = new ProcessNewInput_v2(); alg.q = q; alg.out = toTab; alg.runIt(); tabs.add("Match input with patterns", toTab); } { StringBuffer toTab = new StringBuffer(); Algorithm alg = new SyntacticPatternFromInput(); alg.q = q; alg.out = toTab; alg.runIt(); tabs.add("Make syntactic pattern", toTab); } appendHTML(tabs.html()); } } public BEAObject allowStoringMatch(Algorithm algo, BEAObject input, BEAObject pattern, Map mapping) { BEAObject match = conceptWhereCI(BEAObject.class, "type", "Match", "pattern", pattern, "input", input, "mapping", mapping); if (match != null) { algo.appendHTML("Match exists: " + mod.beaLinkHTML(match)); return match; } algo.appendHTML(joinWithSpace(storeMatchForm(input, pattern, "good"), storeMatchForm(input, pattern, "bad"))); return null; } public String storeMatchForm(BEAObject input, BEAObject pattern, String label) { return hinlinepostform(hhiddenMulti("pattern", pattern.id, "input", input.id, "label", label, "redirect", "") + hbutton(firstToUpper(label) + " match"), "action", mod.baseLink + "/storeMatch", "onsubmit", js_redirectAutoScroll2()); } public class MakeSyntacticPatterns extends Algorithm implements Runnable { public void run() { for (BEAObject p : mod.beaList("Pattern")) { appendText(p + " => " + makeSyntacticPattern(p)); } } } public String syntacticPatternText(BEAObject p) { return simpleSpacesTrim_javaTok(angleBracketVarsToStars(p.text())); } public BEAObject makeSyntacticPattern(BEAObject p) { if (p == null || !p.typeIs("Pattern") || empty(p.text())) return null; String text = syntacticPatternText(p); BEAObject sp = uniqCI(BEAObject.class, "type", "Syntactic Pattern", "text", text); cset(p, "syntacticPattern", sp); return sp; } public String processInputURL(String q) { return addParamsToURL(mod.baseLink + "/query", "q", q, "algorithm", "process input"); } public BEAObject handleInput(Algorithm algo, String q, boolean showProcessLink) { return handleInput(algo, q, "Input", showProcessLink); } public BEAObject handleInput(Algorithm algo, String q, String desc, boolean showProcessLink) { algo.appendHTML(hSingleRowTable_withSpacing((desc + ":"), tt(htmlEncode_nlToBr_withIndents(q)), !showProcessLink ? "" : targetBlank_noFollow(processInputURL(q), "Process"))); BEAObject input = mod.findInput(q); algo.appendHTML(inputHTML(input, desc, q)); return input; } public String inputHTML(BEAObject input, String q) { return inputHTML(input, "Input", q); } public String inputHTML(BEAObject input, String desc, String q) { return input != null ? (desc + " exists: ") + mod.beaLinkHTML(input) : !mod.requestAuthed() && !mod.allowAnonymousInputUpload ? null : hinlinepostform(hhiddenMulti("text", q, "redirect", "") + hbutton(("Save " + desc)), "action", mod.baseLink + "/saveInput", "onsubmit", js_setRedirect()); } public String syntacticPatternLinkHTML(BEAObject obj, String text) { return syntacticPatternLinkHTML(obj, text, null); } public String syntacticPatternLinkHTML(BEAObject obj, String text, BEAObject fromInput) { String desc = "Syntactic pattern"; return obj != null ? mod.beaLinkHTML(obj) : !mod.requestAuthed() ? null : hinlinepostform(hhiddenMulti("text", text, "fromInput", conceptIDOrNull(fromInput), "redirect", "") + hbutton(("Save " + (firstToLower(desc)))), "action", mod.baseLink + "/saveSyntacticPattern", "onsubmit", js_setRedirect()); } public class SyntacticPatternFromInput extends Algorithm implements Runnable { public void run() { BEAObject input = handleInput(this, q, false); if (countTokens(q) > 10) { appendText("Input too long to extract syntactic patterns"); return; } String q2 = dropPunctuation(q); List data = new ArrayList(); for (String pat : sortedIC(gazelle_allSyntacticPatternsFromInput(q))) { Map mapping = matchInputWithSyntacticPattern(q2, pat); BEAObject obj = conceptWhereIC(BEAObject.class, "type", "Syntactic Pattern", "text", pat); Map row = litorderedmap("Syntactic Pattern", htmlEncode2(pat), "In database", syntacticPatternLinkHTML(obj, pat, input)); for (Map.Entry __19 : _entrySet(mapping)) { String key = __19.getKey(); String val = __19.getValue(); row.put("Argument " + key, htmlEncode2(val)); } data.add(row); } appendHTML(htmlTable2_noHtmlEncode(data)); } } public class ApplyTextFunctions extends Algorithm { public void run() { q = trim(q); saveInput(q); BEAObject input = handleInput(this, q, false); LinkedHashSet objectsMade = new LinkedHashSet(); for (BEAObject f : mod.beaList("JavaX Standard Function")) { try { addIfNotNull(objectsMade, reactFunctionWithInput(this, f, input)); } catch (Throwable __e) { _handleException(__e); } } appendHTML(h3("Results of function calls")); HCRUD crud = mod.makeCRUD(BEAObject.class, mod.currentReq()); HCRUD_Concepts crudData = (HCRUD_Concepts) (crud.data); crudData.listConcepts_firstStep = () -> objectsMade; appendHTML(crud.renderTable(true)); } } public class FindPath extends Algorithm { public BreadthFirstPathFinder_withLinkType pathFinder = new BreadthFirstPathFinder_withLinkType(); public BEAObject start, dest; public void run() { start = mod.beaGet(parseFirstLong(q)); dest = mod.beaGet(parseSecondLong(q)); if (start == null || dest == null) { appendText("Please supply object IDs of start and end"); return; } appendHTML("Looking for path from " + mod.beaToHTML(start) + " to " + mod.beaToHTML(dest)); pathFinder.getChildren = o -> cForwardAndBackRefsWithFieldIndicator(BEAObject.class, o); pathFinder.add(start); while (!pathFinder.nodeReached(dest) && pathFinder.step()) { } List> path = pathFinder.examplePathWithTypes(start, dest); if (path == null) appendText("No path found!"); else { appendHTML(b("Path found!")); appendHTML(ol(map(path, p -> { String html = mod.beaHTML(p.a); if (p.b == null) return html; String arrow = ifThenElse(p.b.forward, ">>", "<<"); return html + " " + htmlEncode2(joinNemptiesWithSpace(arrow, p.b.field, arrow)); }))); } } } public class ExecuteTextQuery extends Algorithm { public void run() { saveInput(q = trim(q)); appendHTML(h3("Result")); appendHTML(htmlEncode2(strOrEmpty(executeTextQuery(q)))); } } public class InputSummary extends Algorithm { public void run() { BEAObject mainInput = saveInput(q = trim(q)); Collection inputs = conceptsWhere(BEAObject.class, "type", "Input", "text", q); if (l(inputs) > 1) appendHTML("Note: Input appears " + l(inputs) + " times in the database: " + joinWithSpace(map(o -> mod.beaToHTML(o), inputs))); for (BEAObject input : inputs) { if (input != mainInput) { appendHTML(hr()); appendHTML("Input: " + input); } Collection rewrites = mod.beaBackRefs(input, "Rewrite"); if (empty(rewrites)) appendHTML(h3("No rewrites")); else for (Pair __17 : iterateWithIndex1(rewrites)) { int i = pairA(__17); BEAObject r = pairB(__17); appendHTML("Rewrite " + i + ": " + ahref(mod.beaURL(r), tok_dropCurlyBrackets(r.text()))); } } } } public class ApplyPatternList extends Algorithm { public String additionalInputs() { return hselect("patternListID", mapToKey(__62 -> conceptID(__62), mod.beaList("Pattern List")), req.get("patternListID")); } public void run() { BEAObject input = handleInput(this, q = trim(q), false); BEAObject patternList = mod.beaGet("patternListID", req); if (patternList == null) return; List patterns = (List) (cget("patterns", patternList)); appendHTML("Pattern list: " + mod.beaHTML(patternList)); for (BEAObject pattern : unnullForIteration(patterns)) { appendText("Trying pattern " + pattern); checkPattern(this, input, q, pattern); } } } public class ApplyAlgorithmObject extends Algorithm { public String additionalInputs() { return hselect("algorithmID", mapToKey(__63 -> conceptID(__63), concatLists(wideningListCast(Concept.class, list(BEARegExpReplacement.class)), wideningListCast(Concept.class, conceptsWhereIC(UserPost.class, "type", "JavaX Code (HTML Bot)")))), req.get("algorithmID")); } public void runLiveMode() { Concept algo = getConcept(parseLongOpt(req.get("algorithmID"))); appendHTML("Algorithm object: " + htmlEncode2_nlToBr(str(algo))); if (algo instanceof UserPost) { Object result = mod.callHtmlBot_dropMadeByComment(((UserPost) algo).id, req.params()); appendText("Result: " + result); } else if (algo instanceof BEARegExpReplacement) { Object result = ((BEARegExpReplacement) algo).apply(q); appendText("Result: " + result); } else if (algo == null) appendText("Algorithm object not found"); else appendText("Unknown algorithm object type: " + _className(algo)); } } public class ApplyRegExpReplacement extends Algorithm { public void run() { BEARegExpReplacement regExpReplacement = (BEARegExpReplacement) (mod.beaGet(q)); if (regExpReplacement == null) { appendText("Please enter reg exp replacement ID"); return; } appendHTML("Applying " + beaHTML(regExpReplacement)); for (BEAObject input : sortedByConceptIDDesc(mod.beaList("Input"))) { String text = input.text(); String replaced = regExpReplacement.apply(text); if (neq(text, replaced)) { appendHTML(hr()); appendText(" " + text + "\n=> " + replaced); appendPossibleRewrite(this, input, replaced, litmap("f_regularExpression", regExpReplacement.id, "metaInfo_regularExpression", "concept")); } } } } public class BackEndAlgorithm extends Algorithm { public String prefix() { return shortClassName(this) + ": "; } @Override public void appendText(String s) { printWithIndent(prefix(), s); } @Override public void appendHTML(String s) { appendText(htmlDecode_dropTags(s)); } @Override public void addNonMatch(Runnable r) { r.run(); } } public class MatchInputWithAllPatternLists extends Algorithm { public void run() { BEAObject input = saveInput(q); for (BEAObject patternList : sortedByConceptIDDesc(beaList("Pattern List"))) reactInputWithPatternList(this, input, q, patternList, 1, 2); } } public class RunTestCasesOnCodePost extends Algorithm { public String additionalInputs() { MultiSet ms = distinctCIFieldValuesOfConcepts_multiSet(BEAObject.class, "type"); List types = filter(pairsA(multiSetToPairsByPopularity(ms)), type -> cic(type, "test case")); return "Test case type: " + hselect_list("testCaseType", types, req.get("testCaseType")) + " " + hselect("algorithmID", mapToKey(__64 -> conceptID(__64), conceptsWhereIC(UserPost.class, "type", "JavaX Code (HTML Bot)")), req.get("algorithmID")); } public void run() { String testCaseType = req.get("testCaseType"); Concept algo = getConcept(parseLongOpt(req.get("algorithmID"))); Collection testCases = beaList(testCaseType); appendText(n2(testCases, "test case") + " found for type " + quote(testCaseType)); appendHTML("Algorithm object: " + htmlEncode2_nlToBr(str(algo))); if (!(algo instanceof UserPost)) return; Scorer scorer = new Scorer(); for (BEAObject testCase : testCases) { ping(); Map fields = toCIMap(objectToMap(testCase)); String input = str(getAny(fields, "input")); String expected = str(getAny(fields, "output", "result")); String result = str(mod.callHtmlBot_dropMadeByComment(algo.id, litmap("q", input))); boolean ok = eq(result, expected); appendText((ok ? "OK" : "BAD") + ": Input: " + quote(input) + ", expected output: " + quote(expected) + ", result: " + quote(result)); scorer.add(ok); } appendText(str(scorer)); } } public void reactInputWithPatternList(Algorithm algo, BEAObject input, String text, BEAObject patternList, int level, int recursionLevels) { Pair> match = matchInputWithPatternList(text, patternList); algo.appendHTML(hr()); algo.appendHTML(joinNemptiesWithColon(level == 1 ? beaHTML(patternList) : null, stringIf(match == null, "No match"))); if (match != null) { BEAObject pattern = match.a; Map mapping = match.b; algo.appendHTML("Matched as " + beaHTML(pattern)); algo.appendText(renderMappingAsText(mapping)); if (input != null) { BEAObject matchObj = uniqCI(algo.localConcepts, BEAObject.class, "type", "Match", "input", input, "patternList", patternList, "winningPattern", pattern, "mapping", mapping); algo.appendHTML("Match saved as: " + beaHTML(matchObj)); } if (level < recursionLevels) { algo.appendHTML_raw("
    "); for (Map.Entry __18 : _entrySet(mapping)) { String key = __18.getKey(); String subInput = __18.getValue(); if (!eqic(key, text)) { algo.appendHTML_raw("
  • "); algo.appendText("Processing sub-Input: " + subInput); reactInputWithPatternList(algo, null, subInput, patternList, level + 1, recursionLevels); algo.appendHTML_raw("
  • "); } } algo.appendHTML_raw("
"); } } } public int patternSpecificity(BEAObject pattern) { return countWordsWithoutAngleBracketedAndStars(pattern.text()); } public List syntacticPatternsSortedBySpecificityDesc() { return sortedByCalculatedFieldDesc(__42 -> patternSpecificity(__42), mod.beaList("Syntactic Pattern")); } public Object executeTextQuery(String s) { Matches m = new Matches(); if (match3_withIntsAndIDs("all references in field of object ", s, m)) { Object value = cget(mod.beaGet(m.unq(1)), m.unq(0)); return value instanceof Collection ? instancesOf(BEAObject.class, (Collection) value) : value instanceof BEAObject ? ll((BEAObject) value) : ll(); } return new QueryNotUnderstood(s); } static public class QueryNotUnderstood implements IFieldsToList { public String s; public QueryNotUnderstood() { } public QueryNotUnderstood(String s) { this.s = s; } public String toString() { return shortClassName_dropNumberPrefix(this) + "(" + s + ")"; } public boolean equals(Object o) { if (!(o instanceof QueryNotUnderstood)) return false; QueryNotUnderstood __23 = (QueryNotUnderstood) o; return eq(s, __23.s); } public int hashCode() { int h = 1528655894; h = boostHashCombine(h, _hashCode(s)); return h; } public Object[] _fieldsToList() { return new Object[] { s }; } } public String beaToHTML(BEAObject o) { return mod.beaToHTML(o); } public String beaHTML(BEAObject o) { return mod.beaHTML(o); } public String beaURL(BEAObject o) { return mod.beaURL(o); } public boolean isSyntacticPattern(BEAObject pat) { return pat != null && pat.typeIs("Syntactic pattern"); } public String patternTextWithAngleBracketVars(BEAObject pat) { String text = pat.text(); return isSyntacticPattern(pat) ? starsToAngleBracketedNumbers(text) : text; } public boolean isCommentOrFeedback(BEAObject o) { return o != null && o.typeIsOneOf("Comment", "Useful", "Feedback"); } public String feedbackHTML(BEAObject o) { Collection l = filter(__43 -> isCommentOrFeedback(__43), mod.beaBackRefs(o)); return joinWithBR(map(l, c -> c.typeIs("Comment") ? ahref(beaURL(c), htmlEncode2(c.text())) : beaHTML(c))); } public String bestInputURL(String text) { text = trim(text); BEAObject input = mod.findInput(text); return input != null ? beaURL(input) : processInputURL(text); } public String bestInputHTML(String text) { if (empty(text)) return ""; return ahref(bestInputURL(text), span(htmlEncode2(text), "style", "background-color: #ccc")); } public String performRewrite(BEAObject rewrite, Map mapping) { String rewriteText = starsToAngleBracketedNumbers(rewrite.text()); return replaceAngleBracketVars(rewriteText, mapping); } public Pair exampleForPatternRewrite(BEAObject pattern, BEAObject rewrite) { String patText = patternTextWithAngleBracketVars(pattern); Set vars = collectAngleBracketVars(patText); Map mapping = mapToValues_ciMap(vars, var -> "$" + var); return pair(replaceAngleBracketVars(patText, mapping), performRewrite(rewrite, mapping)); } public class RegExpToPossibleSubInputs extends Algorithm implements IFieldsToList { public BEAObject input; public BEAObject regExp; public RegExpToPossibleSubInputs() { } public RegExpToPossibleSubInputs(BEAObject input, BEAObject regExp) { this.regExp = regExp; this.input = input; } public String toString() { return shortClassName_dropNumberPrefix(this) + "(" + input + ", " + regExp + ")"; } public boolean equals(Object o) { if (!(o instanceof RegExpToPossibleSubInputs)) return false; RegExpToPossibleSubInputs __16 = (RegExpToPossibleSubInputs) o; return eq(input, __16.input) && eq(regExp, __16.regExp); } public int hashCode() { int h = -208954156; h = boostHashCombine(h, _hashCode(input)); h = boostHashCombine(h, _hashCode(regExp)); return h; } public Object[] _fieldsToList() { return new Object[] { input, regExp }; } public void run() { String text = input.text(); String pat = regExp.text(); for (IntRange r : regexpFindRangesIC(pat, text)) appendHTML(beaHTML(csetAndReturn(uniq(localConcepts, BEAObject.class, "type", "Possible Sub-Input", "input", input, "characterRange", r, "text", substring(text, r)), "regExp", regExp, "byAlgorithm", shortName(this)))); } } public void appendPossibleRewrite(Algorithm algo, BEAObject input, String text) { appendPossibleRewrite(algo, input, text, null); } public void appendPossibleRewrite(Algorithm algo, BEAObject input, String text, Map moreInfo) { BEAObject existing = input == null ? null : conceptWhereIC(BEAObject.class, "type", "Rewrite", "input", input, "text", text, "label", "good"); if (existing != null) algo.appendHTML("Rewrite exists: " + mod.beaLinkHTML(existing)); else if (input != null) algo.appendHTML(joinWithSpace(map(ll("good", "bad"), label -> hinlinepostform(hhiddenMulti("action", "create", "f_type", "Rewrite", "f_text", text, "f_input", input.id, "metaInfo_input", "concept", "f_label", label, "redirectAfterSave", "") + hhiddenMulti(moreInfo) + hbutton(firstToUpper(label) + " rewrite", "onclick", js_redirectAutoScroll2()), "action", mod.crudLink(BEAObject.class))))); } public BEAObject reactFunctionWithInput(Algorithm algo, BEAObject f, BEAObject input) { String fName = getString("name", f); if (!isIdentifier(fName)) return null; if (!isSafeStandardFunction(fName)) { { if (algo != null) algo.addNonMatch(new Runnable() { public void run() { try { algo.appendText("Function " + fName + " not safe"); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "algo.appendText(\"Function \" + fName + \" not safe\");"; } }); } return null; } BEAObject o = cnew(algo.localConcepts, BEAObject.class, "type", "Function Result", "function", f, "functionCalled", fName, "input", input, "calculating", true); print("Function result: " + o); try { Object holder = getStandardFunctionHolder(fName); Object result = evalWithTimeoutOrFail(10.0, () -> call(holder, fName, input.text())); if (!(result instanceof List) && isStringCollection(result)) result = asList((Collection) result); if (result == null) cset(o, "resultType", "null"); else if (result instanceof String) cset(o, "resultType", "string", "result", result); else if (isStringList(result)) cset(o, "resultType", "list of strings", "result", result); else cset(o, "result", "other" + appendRoundBracketed(className(result)), "result", shorten(1000, str(result))); } catch (Throwable e) { cset(o, "resultType", "error", "error", getStackTrace(e)); } cset(o, "calculating", null); return o; } public BEAObject convertSyntacticToSemanticMatch(BEAObject match, BEAObject semanticPattern) { BEAObject syntacticPattern = beaGet("pattern", match); BEAObject input = beaGet("input", match); String pat2 = syntacticPattern.text(); String pat1 = semanticPattern.text(); List vars = new ArrayList(); String pat2b = angleBracketVarsToStars(pat1, vars); String warning = eq(pat2, pat2b) ? null : "Patterns don't match: " + quote(pat2) + " / " + quote(pat2b); Map mapping = (Map) (cget("mapping", match)); Map mapping2 = mapKeys(mapping, key -> or(_get(vars, parseInt(key) - 1), key)); mapping2 = putKeysFirst(vars, mapping2); BEAObject match2 = uniqCI(BEAObject.class, "type", "Match", "input", input, "pattern", semanticPattern, "fromSyntacticMatch", match); cset(match2, "mapping", mapping2); return match2; } public Collection convertSyntacticToSemanticMatches(BEAObject match) { List out = new ArrayList(); BEAObject syntacticPattern = beaGet("pattern", match); for (BEAObject semanticPattern : mod.beaList("Pattern", "syntacticPattern", syntacticPattern)) { try { out.add(convertSyntacticToSemanticMatch(match, semanticPattern)); } catch (Throwable __e) { _handleException(__e); } } return out; } public Collection convertSyntacticToSemanticMatchesForWholePattern(BEAObject pattern) { if (pattern.typeIs("Syntactic Pattern")) return concatMap_lists(__65 -> convertSyntacticToSemanticMatches(__65), mod.beaList("Match", "pattern", pattern)); else { List out = new ArrayList(); BEAObject syntacticPattern = beaGet("syntacticPattern", pattern); if (syntacticPattern != null) for (BEAObject match : mod.beaList("Match", "pattern", syntacticPattern)) out.add(convertSyntacticToSemanticMatch(match, pattern)); return out; } } public BEAObject beaGet(String field, BEAObject o) { return mod.beaGet(field, o); } public Collection beaList(String type, Object... params) { return mod.beaList(type, params); } public int simpleObjectScore(BEAObject o) { int score = 0; if (eqic_gen(getOpt("label", o), "good")) ++score; else if (eqic_gen(getOpt("label", o), "bad")) --score; Collection l = findBackRefsWithField(BEAObject.class, "object", o); score += countWhereCI(l, "type", "Useful"); score -= countWhereCI(l, "type", "Bad"); return score; } public List sortedBySimpleObjectScore(int minScore, Collection l) { return sortedByCalculatedFieldDesc(__44 -> simpleObjectScore(__44), filter(l, o -> simpleObjectScore(o) >= minScore)); } public BEAObject createPatternListFromUsefulSyntacticPatterns() { List patterns = sortedBySimpleObjectScore(0, beaList("Syntactic Pattern")); return mod.autoMigrateToCustomClass(uniqCI(BEAObject.class, "type", "Pattern List", "byFunction", "createPatternListFromUsefulSyntacticPatterns", "patterns", patterns)); } public Map> algorithms() { return litorderedmap("Process new input v3", (IF0) () -> new ProcessNewInput_v3(), "Process new input v2", (IF0) () -> new ProcessNewInput_v2(), "Process new input", (IF0) () -> new ProcessNewInput(), "Run pattern", (IF0) () -> new RunPattern(), "Find rewrites for match", (IF0) () -> new FindRewrites(), "Reverse input (test algorithm)", (IF0) () -> new ReverseInput(), "Timeout test", (IF0) () -> new TimeoutTest(), "Syntactic pattern from input", (IF0) () -> new SyntacticPatternFromInput(), "Apply all text functions", (IF0) () -> new ApplyTextFunctions(), "Find Path", (IF0) () -> new FindPath(), "Execute text query", (IF0) () -> new ExecuteTextQuery(), "Make syntactic patterns for all patterns in DB", (IF0) () -> new MakeSyntacticPatterns(), "Input summary", (IF0) () -> new InputSummary(), "Apply pattern list", (IF0) () -> new ApplyPatternList(), "Apply regular expression replacement to all inputs", (IF0) () -> new ApplyRegExpReplacement(), "Match input with all pattern lists", (IF0) () -> new MatchInputWithAllPatternLists(), "Apply algorithm object", (IF0) () -> new ApplyAlgorithmObject(), "Run test cases on code post", (IF0) () -> new RunTestCasesOnCodePost()); } public void putAlgorithmsInDatabase() { for (String name : keys(algorithms())) uniq(BEAObject.class, "type", "Classic Query Algorithm", "name", name); } } abstract static public class GazelleBEA extends DynGazelleRocks { static public boolean _switchableField_mirrorBEAObjects = true; public boolean mirrorBEAObjects = false; static public boolean _switchableField_enableAutoRuns = true; public boolean enableAutoRuns = true; static public boolean _switchableField_enableNewBEAObjectNotis = true; public boolean enableNewBEAObjectNotis = true; static public boolean _switchableField_printAllConceptChanges = true; public boolean printAllConceptChanges = false; static public boolean _switchableField_useShadowLogger = true; public boolean useShadowLogger = false; static public boolean _switchableField_autoActivateDynamicObjects = true; public boolean autoActivateDynamicObjects = true; static public boolean _switchableField_reloadWhenMainLibUpdates = true; public boolean reloadWhenMainLibUpdates = false; static public boolean _switchableField_baseSystemVersion = true; public String baseSystemVersion = "1"; static public boolean _switchableField_backupFrequency = true; public int backupFrequency = 5; transient public boolean allObjectsActivated = false; transient public Throwable startError; public String activateOnlyTheseIDs; public String dontActivateTheseIDs; transient public ReliableSingleThread_Multi rstAutoRuns = dm_rstMulti(this, 1000, __37 -> performAutoRuns(__37)); transient public Q notificationQ; transient public ReliableSingleThread_Multi rstDistributeNewObject = dm_rstMulti(this, 1000, __38 -> distributeNewObject_impl(__38)); transient public Set newObjectsDistributed = weakSet(); transient public ReliableSingleThread_Multi rstUpdateBEAMirrors = dm_rstMulti(this, 100, c -> c.updateMirrorPost()); transient public ReliableSingleThread_Multi rstAutoActivateDynamicObjects = dm_rstMulti(this, 100, __39 -> autoActivateDynamicObject(__39)); transient public BEACalculations calculations = new BEACalculations(this); transient public BEACalculations calc = calculations; transient public int newFieldsToShow = 3; transient public boolean allowAnonymousInputUpload = true; static public boolean _switchableField_maxInputLength = true; public int maxInputLength = 50000; static public boolean _switchableField_mainDomainWithProtocol = true; public String mainDomainWithProtocol = "https://bea.gazelle.rocks"; static public boolean _switchableField_verboseQStartsAndStops = true; public boolean verboseQStartsAndStops = false; transient public ConceptClassesDependentValue inputsWithoutRewrites; transient public ConceptClassesDependentValue inputsWithoutMatches; transient public ConceptClassesDependentValue syntacticPatternsWithoutRewrites; transient public Set deadClassPaths = syncLinkedHashSet(); transient public x30_pkg.x30_util.BetterThreadLocal beaThreadOwner = new x30_pkg.x30_util.BetterThreadLocal(); public boolean verboseClassLoading = false; transient public boolean saveAllQsAsInput = true; transient public boolean moveNavItemsToMisc = false; transient public boolean additionalCommandsForObjects = true; transient public boolean exportUsersInVM = false; transient public boolean sourceIsPublic = true; transient public boolean makeAnonymousUsers = true; transient public boolean showInputAndPatternNavItems = true; transient public boolean quickDBReloadEnabled = false; public Concepts _getReloadData() { return db_mainConcepts(); } public void _setReloadData_early(Object data) { quickImportMainConcepts(data, () -> quickDBReloadEnabled); } public void init() { super.init(); botName = heading = adminName = "Gazelle"; favIconID = psI("#1102967"); enableVars = true; showTalkToBotLink = false; phoneNumberSpecialInputField = false; showMetaBotOnEveryPage = false; showDeliveredDomains = false; showCRUDToEveryone = true; } public Class lookForClass(ClassLoader cl, String name) { try { if (!name.startsWith(DynClassName_mainPrefixForVM())) return null; if (verboseClassLoading) print("Looking for class " + name); DynClassName dcn = DynClassName_parse(name); if (dcn == null) dcn = DynClassName_parseOther(name); if (dcn == null) { vmBus_send("lookingForClass_noDCNParse", name); return null; } String subPath = vmClassNameToSubPath(dcn.fullClassNameForVM()); File byteCode = dcn.byteCodeFile(); vmBus_send("lookingForClass_haveByteCodeFile", byteCode); if (classLoaderContainsByteCodePath(cl, byteCode)) { vmBus_send("lookingForClass_haveByteCodePath", byteCode); return null; } boolean exists = fileExistsInInDirOrZip(byteCode, subPath); printVars("exists", exists, "subPath", subPath, "byteCode", byteCode); if (!exists) { vmBus_send("lookingForClass_subPathNotInByteCode", byteCode, subPath); throw fail("Class not found: " + subPath + " in " + byteCode); } addByteCodePathToClassLoader(cl, byteCode); assertTrue("Byte code added", classLoaderContainsByteCodePath(cl, byteCode)); vmBus_send("lookingForClass_byteCodePathAdded", byteCode); return (Class) call(cl, "super_findClass", name); } catch (Throwable __e) { _handleException(__e); } return null; } public void db() { main.db(); } public void startDB() { ClassLoader cl = dm_moduleClassLoader(); setOpt(cl, "verbose", verboseClassLoading); setFieldToIF1Proxy(cl, "findClass_extension", (IF1) name -> { vmBus_send("lookingForClass", cl, name); Class c = lookForClass(cl, name); vmBus_send("lookingForClass_result", cl, name, c); return c; }); Object classFinder = _defaultClassFinder(); db_mainConcepts().classFinder = new F1() { public Class get(String name) { try { AutoCloseable __32 = enter(); try { print("Deserializing class " + name); try { String name2 = dropPrefix(mcName() + "$", name); String mainPrefix = dotToDollar(DynClassName_mainPrefix()); if (name2.startsWith(mainPrefix)) { String vmName = DynClassName_parse(name2).fullClassNameForVM(); if (verboseClassLoading) print("Loading dynamic class " + vmName); Class c = classLoader_loadClass(module(), vmName); return c; } Class c = (Class) (callF(classFinder, name)); print("c", c); return c; } catch (Throwable e) { printStackTrace(e); throw rethrow(e); } } finally { _close(__32); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "temp enter();\r\n print(\"Deserializing class \" + name);\r\n try {\r\n ..."; } }; db(); Concepts concepts = db_mainConcepts(); concepts.useBackRefsForSearches = true; concepts.newBackupEveryXMinutes = backupFrequency; fixConceptIDs(); inputsWithoutRewrites = new ConceptClassesDependentValue(litset(BEAObject.class), () -> countPred(beaList("Input"), c -> empty(beaBackRefs(c, "Rewrite")))); inputsWithoutMatches = new ConceptClassesDependentValue(litset(BEAObject.class), () -> countPred(beaList("Input"), c -> empty(beaBackRefs(c, "Match")))); syntacticPatternsWithoutRewrites = new ConceptClassesDependentValue(litset(BEAObject.class), () -> countPred(beaList("Syntactic Pattern"), c -> empty(beaBackRefs(c, "Rewrite")))); if (useShadowLogger) { ConceptsShadowLogger shadowLogger = new ConceptsShadowLogger(db_mainConcepts()); shadowLogger.install(); shadowLogger.writer = filePrintWriter_append(programFile("shadow.log")); dm_doEvery(10.0, new Runnable() { public void run() { try { shadowLogger.flush(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "shadowLogger.flush();"; } }); ownResource(shadowLogger); } if (printAllConceptChanges) printAllConceptChanges(); } public Map emergencyFlags() { return (Map) parseEqualsProperties(joinNemptiesWithEmptyLines(loadTextFile(programFile("gazelle-emergency-options.txt")), loadTextFile(javaxDataDir("gazelle-emergency-options.txt")))); } public void start() { try { start2(); } catch (Throwable e) { _handleException(e); startError = e; } } public void start2() { print("Module ID: " + dm_moduleID()); useBotNameAsModuleName = false; setOptAllDyn_pcall(module(), pnl("emergencyFlags", emergencyFlags())); change(); String name = actualMCDollar() + "BEAObject"; print("name", name); assertEquals(BEAObject.class, callF(defaultDefaultClassFinder(), name)); assertEquals(ConceptWithGlobalID.class, callF(defaultDefaultClassFinder(), mcDollar() + "ConceptWithGlobalID")); botDisabled = true; storeBaseClassesInStructure = true; super.start(); assertSameVerbose("miscMap me", db_mainConcepts().miscMapGet(DynNewBot2.class), this); assertSameVerbose("beaMod()", main.beaMod(), this); print("main concepts: " + db_mainConcepts() + ", count: " + db_mainConcepts().countConcepts()); print(renderConceptClassesWithCount(db_mainConcepts())); print("Inputs: " + n2(beaCount("Input"))); dm_onSnippetTranspiled(mainLibID, new Runnable() { public void run() { try { if (reloadWhenMainLibUpdates) dm_reloadModule(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (reloadWhenMainLibUpdates)\r\n dm_reloadModule();"; } }); if (!enabled) return; if (exportUsersInVM) { dm_vmBus_answerToMessage("lookupGazelleUser", new F3() { public Object get(String name, String pw, String salt) { try { if (!eq(salt, passwordSalt())) return null; User user = conceptWhereCI(User.class, "name", name); if (user == null) return null; if (!eq(getVar(user.passwordMD5), hashPW(pw))) return null; return user; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (!eq(salt, passwordSalt())) null;\r\n User user = conceptWhereCI User..."; } }); } rstUpdateBEAMirrors.addAll(list(BEAObject.class)); newObjectsDistributed.addAll(list(BEAObject.class)); onIndividualConceptChange_notOnAllChanged(BEAObject.class, o -> { if (enableAutoRuns) rstAutoRuns.add(o); if (enableNewBEAObjectNotis && newObjectsDistributed.add(o)) rstDistributeNewObject.add(o); }); notificationQ = dm_startQ(); dm_doEvery(5 * 60.0, new Runnable() { public void run() { try { { AutoCloseable __33 = enter(); try { print(new ConceptsRefChecker(db_mainConcepts()).runAndFixAll()); } finally { _close(__33); } } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "{ temp enter();\r\n print(ConceptsRefChecker(db_mainConcepts()).runAndFixA..."; } }); setField("activateOnlyTheseIDs", nemptyLinesLL(loadTextFile(programDir("gazelle-activate-only")), loadTextFile(javaxDataDir("gazelle-activate-only")))); Set ids = activateOnlyTheseIDs(); Set antiIDs = dontActivateTheseIDs(); print("Object IDs to activate: " + (ids == null ? "ALL" : ids)); if (nempty(antiIDs)) print("Object IDs not to activate: " + (ids == null ? "ALL" : ids)); for (BEAObject bea : list(BEAObject.class)) if ((ids == null || contains(ids, bea.id)) && !contains(antiIDs, bea.id)) { callActivate(bea); } cleanReifiedWebSockets(); allObjectsActivated = true; print("All objects activated"); } public void makeIndices() { super.makeIndices(); indexConceptClass(BSettings.class); indexConceptFieldDesc(BEAObject.class, "_modified"); indexConceptFieldCIWithTopTen(BEAObject.class, "type"); indexConceptFieldIC(BEAObject.class, "text"); indexConceptFieldIC(BEAObject.class, "purpose"); } public boolean isMaster(Req req) { return req == null ? null : req.masterAuthed; } @Override public List crudClasses(DynGazelleRocks.Req req) { return listPlus(super.crudClasses(req), BEAObject.class, AuthedDialogID.class); } public Set hiddenCrudClasses() { Set set = super.hiddenCrudClasses(); set.remove(UploadedSound.class); addAll(set, Conversation.class, ExecutedStep.class, InputHandled.class); return set; } public String authFormHeading() { return h3(adminName); } transient public IF0 authFormMoreContent; public String authFormMoreContent() { return authFormMoreContent != null ? authFormMoreContent.get() : authFormMoreContent_base(); } final public String authFormMoreContent_fallback(IF0 _f) { return _f != null ? _f.get() : authFormMoreContent_base(); } public String authFormMoreContent_base() { String html = p(html_loggedIn(false)); HTMLFramer1 framer = new HTMLFramer1(); if (anonymousUserWarning(currentReq(), framer)) html += lines(framer.contents); return html + super.authFormMoreContent(); } @Override public void makeFramer(DynGazelleRocks.Req req) { super.makeFramer(req); if (!allObjectsActivated) req.framer.add(p("WARNING: Start failure")); if (startError != null) req.framer.add(pre_htmlEncode("WARNING: Start error\n\n" + renderStackTrace(startError))); req.framer.addInHead(hjs_selectize()); req.framer.addInHead(hjs_selectizeClickable()); req.framer.addInHead(hjs_autoExpandingTextAreas()); } @Override public HCRUD_Concepts crudData(Class c, DynGazelleRocks.Req _req) { Req req = (Req) _req; HCRUD_Concepts cc = super.crudData(c, req); if (c == UploadedSound.class || c == UploadedFile.class || c == UploadedImage.class) { cc.onCreate.add(o -> cset(o, "createdBy", currentUser())); cc.emptyConcept = () -> { Concept ccc = cc.emptyConcept_base(); cset(ccc, "isPublic", true); return (A) ccc; }; } if (c == BEAObject.class) { cc.lsMagic = true; cc.humanizeFieldNames = false; cc.convertConceptValuesToRefs = true; cc.itemName = () -> "BEA Object"; cc.addRenderer("meta_code", new HCRUD_Data.AceEditor(80, 20)); cc.titleForObjectID = id -> beaHTML(beaGet(toLong(id))); cc.isEditableValue = o -> cc.isEditableValue_base(o) && !eq(shortClassName(o), "MultiSet"); cc.conceptClassForComboBoxSearch = (info, query) -> { if (endsWith(info, "_nativeValue")) return Concept.class; return cc.conceptClassForComboBoxSearch_base(info, query); }; cc.comboBoxSearchBaseItems = (info, query) -> { Matches m = new Matches(); printVars("comboBoxSearchBaseItems", "info", info); if (jMatchStart("type=", info, m)) { var l = beaList(m.unq(0)); print("Got " + nItems(l)); return cc.comboBoxItems(l); } return cc.comboBoxSearchBaseItems_base(info, query); }; cc.massageItemMapForUpdate = (o, map) -> { cc.massageItemMapForUpdate_base(o, map); if (!isMaster(req)) { Object createdByOld = cget("createdBy", o); if (createdByOld != null || map.get("createdBy") != user(req)) map.remove("createdBy"); } }; cc.onCreate.add(o -> cset(o, "createdBy", currentUser())); cc.getObjectForDuplication = id -> { BEAObject o = beaGet(toLong(id)); Map item = cc.getObjectForDuplication_base(id); item.put("createdBy", req.auth.user.get()); item.put("meta_createdFrom", o); item = or((Map) pcallOpt(cget("cleanObjectForDuplication", websiteEnhancersObject()), "cleanObjectForDuplication", o, item), item); return item; }; cc.emptyObject = () -> { Map item = cc.emptyObject_base(); item.put("type", ""); return item; }; Set deletableRefs = litciset("Match"); cc.objectCanBeDeleted = id -> { BEAObject o = (BEAObject) (cc.conceptForID(id)); return userCanEditObject(user(req), o) && all(findBackRefs(BEAObject.class, o), x -> contains(deletableRefs, x.type())); }; cc.actuallyDeleteConcept = o -> { deleteConcepts(filter(findBackRefs(BEAObject.class, o), o2 -> contains(deletableRefs, o2.type()))); cdelete(o); }; } return cc; } public boolean newLinkTargetBlank_base(Class c) { return super.newLinkTargetBlank_base(c) || c == BEAObject.class; } @Override public HCRUD makeCRUD(Class c, DynGazelleRocks.Req req, HTMLFramer1 framer) { HCRUD crud = super.makeCRUD(c, req, framer); HCRUD_Concepts data = (HCRUD_Concepts) (crud.data); crud.showOnlySelected = true; crud.showSearchField = true; if (data.customFilter == null) crud.descending = true; crud.cleanItemIDs = true; if (c == BEAObject.class) { crud.allowFieldRenaming = true; var renderValue_old = crud.renderValue; crud.renderValue = (field, value) -> { String html = crud.renderValue_fallback(renderValue_old, field, value); return addShowMoreButton(html); }; crud.haveSelectizeClickable = true; crud.cellColumnToolTips = true; crud.unshownFields = litset("mirrorPost", "globalID"); crud.uneditableFields = litset("log"); crud.showTextFieldsAsAutoExpandingTextAreas = true; crud.duplicateInNewTab = true; crud.customizeACEEditor = ace -> { ace.onKeyDown = "function(event) { " + jquery_submitFormOnCtrlEnter() + " }"; }; HCRUD_Concepts cc = (HCRUD_Concepts) (crud.data); String typeFilter = req.get("type"); if (nempty(typeFilter)) cc.addCIFilter("type", typeFilter); crud.renderCmds = map -> { BEAObject o = getConcept(BEAObject.class, crud.itemIDAsLong(map)); if (o == null) return ""; List cmds = new ArrayList(); addAll(cmds, allToString(optCast(Collection.class, pcall(o, "cmds")))); if (fileNotEmpty(javaSourceFileForObject(o))) cmds.add(targetBlank(baseLink + "/javaSource?id=" + o.id, "Show Java source")); for (String field : ll("text", "what", "type")) { String text = getStringOpt(o, field); if (nempty(text)) cmds.add(targetBlank_noFollow(addParamsToURL(baseLink + "/query", "q", text, "algorithm", "process input"), "Use field " + quote(field) + " as query")); } if (o.typeIs("Input")) { cmds.add(ahref(addParamsToURL(crudLink(BEAObject.class), "cmd", "new", "title", "Add Pattern For Input", "f_type", "Pattern", "f_text", getStringOpt("text", o), "f_shouldMatch", o.id, "metaInfo_shouldMatch", "concept"), "Add pattern")); cmds.add(ahref(appendParamsToURL(baseLink + "/query", "q", o.text(), "algorithm", "Apply all text functions"), "Apply all text functions")); } if (o.typeIs("Pattern")) cmds.add(ahref(appendParamsToURL(baseLink + "/query", "q", o.id, "algorithm", "Run pattern"), "Try-run pattern against all inputs")); if (o.typeIsOneOf("Pattern", "Syntactic Pattern")) { cmds.add(ahref(appendParamsToURL(baseLink + "/reactAllInputsWithPattern", "patternID", o.id), "React pattern with all inputs")); cmds.add(ahref(appendParamsToURL(baseLink + "/convertSyntacticToSemanticMatchesForWholePattern", "patternID", o.id), "Convert all syntactic matches to semantic matches")); } if (o.typeIs("Match")) { cmds.add(ahref(appendParamsToURL(baseLink + "/query", "q", o.id, "algorithm", "Find rewrites for match"), "Find rewrites")); if (beaTypeIs(beaGet("pattern", o), "Syntactic Pattern")) cmds.add(ahref(appendParamsToURL(baseLink + "/convertSyntacticToSemanticMatches", "matchID", o.id), "Convert to semantic matches")); } cmds.add(targetBlank(addParamsToURL(crudLink(BEAObject.class), "cmd", "new", "newField1_name", "inReferenceTo", "newField1_type", "BEAObject", "newField1_conceptValue", o.id), "Reference this")); cmds.add(ahref(addParamsToURL(baseLink + "/markUseful", "redirect", beaShortURL(o), "objectID", o.id), "Mark useful")); cmds.add(ahref(addParamsToURL(baseLink + "/markBad", "redirect", beaShortURL(o), "objectID", o.id), "Mark bad")); cmds.add(addCommentHTML(o)); if (o.typeIsOneOf("Script", "Step in script") || eqic(beforeVerticalBar(o.type()), "Instruction")) cmds.add(ahref(addParamsToURL(baseLink + "/runInstruction", "instruction", o.id), "Run")); if (o.typeIs("Function Result")) { if (eqic(cget("resultType", o), "string")) cmds.add(ahref(addParamsToURL(baseLink + "/convertResultToInput", "result", o.id), "Convert to input")); } if (o.typeIs("Auto Run")) cmds.add(ahref(addParamsToURL(baseLink + "/performAutoRunOnAllObjects", "autoRun", o.id), "Run on all objects")); if (o.getClass() != BEAObject.class) cmds.add(ahref(addParamsToURL(baseLink + "/migrateToBase", "id", o.id), "Migrate to BEAObject")); cmds.add(ahref(addParamsToURL(baseLink + "/performAutoRuns", "onObject", o.id), "Perform auto runs on this object")); framer.addInHead(hjs_copyToClipboard()); cmds.add(ahref_onClick(formatFunctionCall("copyToClipboard", jsQuote(o.globalIDStr())), "Copy global ID [" + o.globalIDStr() + "]")); List items = llNempties(crud.renderCmds_base(map), !o.typeIsOneOf("Input", "Pattern", "Syntactic Pattern") ? null : addRewriteHTML(o), !canAutoMigrate(o) ? null : ahref(addParamsToURL(baseLink + "/autoMigrate", "id", o.id), "Auto-migrate to " + shortClassName(defaultCustomClass(o)))); if (isObjectWithCode(o) && isMasterAuthed(req)) { String link = baseLink + "/activateDynamicObject?id=" + o.id; if (!codeHashUpToDate(o)) if (cget("meta_codeHash", o) == null) items.add(ahref(link, "Compile new code", "title", "Object has not been compiled yet")); else items.add(ahref(link, "Compile changed code", "title", "Code has changed afer last compilation")); else if (compileErrorInObject(o)) items.add("Code error. " + ahref(link, "Compile again", "title", "Object has code errors - maybe recompiling fixes them?")); else if (o.customCodeLoaded()) items.add(ahref(link, "Compile again", "title", "Custom code is loaded. Click if you want to recompile anyway")); else items.add(ahref(link, "Compile & load")); } if (additionalCommandsForObjects) { if (hasMethodNamed(o, "html")) items.add(targetBlank(beaMod().baseLink + "/beaHTML/" + o.id, "Show HTML")); if (nempty(getStringOpt("meta_code", o))) items.add(targetBlank(addParamsToURL(beaMod().baseLink + "/get-field", "obj", o.id, "field", "meta_code"), "Show code")); String codeState = getStringOpt("meta_codeState", o); if (swic(codeState, "error")) items.add(targetBlank(addParamsToURL(beaMod().baseLink + "/beaHTML/249697", "obj", o.id, "field", "meta_codeState"), "Show compile error")); boolean master = beaMod().isMasterAuthed(); if (hasMethod(o, "_activate")) { boolean active = isTrue(pcallOpt(o, "_active")); boolean shouldActivate = isTrue(getOpt(o, "meta_shouldActivate")); if (active != shouldActivate) items.add("Warning: Object is semi-activated"); else if (active) items.add("Object is active"); else items.add("Object is inactive"); if (master) { if (!active || !shouldActivate) items.add(ahref(addParamsToURL(beaMod().baseLink + "/activateObject", "id", o.id), "ACTIVATE")); if (active || shouldActivate) items.add(ahref(addParamsToURL(beaMod().baseLink + "/deactivateObject", "id", o.id), "DEACTIVATE")); } } if (master) { List methodCalls = new ArrayList(); for (Method method : methodsSortedByNameIC(allLiveMethodsBelowClass(o, BEAObject.class))) { Map params = litorderedmap("id", o.id, "name", method.getName()); boolean incomplete = false, problem = false; var types = method.getParameterTypes(); for (int i = 1; i <= l(types); i++) { var argType = types[i - 1]; incomplete = true; if (eq(argType, String.class)) params.put("arg" + i, "putArgHere"); else if (isSubclassOf(argType, BEAObject.class)) params.put("conceptArg" + i, "putArgHere"); else { params.put("arg" + i, "missingConverterProblem"); problem = true; } } String url = addParamsToURL("/callAnyMethod", params); String html = method.getName(); if (problem) html += appendBracketed("probably can't call, need converter"); else if (incomplete) html += appendBracketed("fill in parameters first"); methodCalls.add(targetBlank(url, html)); } if (nempty(methodCalls)) items.add(hPopDownButtonWithText("Call a method", methodCalls)); } } try { addAll(items, o.directCmds()); } catch (Throwable __e) { _handleException(__e); } try { addAll(items, (List) callOpt(getWebsiteEnhancer("commandsForObject"), "commandsFor", o)); } catch (Throwable __e) { _handleException(__e); } items.add(hPopDownButton(cmds)); return joinNemptiesWithVBar(items); }; cc.massageItemMapForList = (item, map) -> { BEAObject o = (BEAObject) item; if (o.typeIs("Input")) { Collection matches = objectsWhereNotIC(objectsWhereCI(findBackRefs(o, BEAObject.class), "type", "match"), "label", "bad"); ((Map) map).put("Best Matches", new HTML(hparagraphs(lmap(__45 -> matchDescHTML(__45), takeFirst(3, matches))))); } String idField = crud.idField(); Object id = ((Map) map).get(idField); if (id instanceof String) ((Map) map).put(idField, new HTML(ahref(beaShortURL(o), id))); if (o.typeIs("Function Result")) { Object value = getOpt("result", o); ((Map) map).put("result", new HTML(javaValueToHTML(value))); } if (o.typeIs("Match")) { Map mapping = o.mapping(); if (mapping != null) ((Map) map).put("mapping", new HTML(joinWithBR(map(mapping, (k, v) -> htmlEncode2(k) + "=" + calculations.bestInputHTML(v))))); } if (o.getClass() != BEAObject.class) ((Map) map).put("Java Class", dropPrefix(actualMCDollar(), className(o))); if (eq(req.get("showSimpleObjectScore"), "1")) ((Map) map).put("Simple object score", calculations.simpleObjectScore(o)); o.massageItemMapForList(((Map) map)); }; crud.massageFormMatrix = (map, matrix) -> { for (int i = 1; i <= newFieldsToShow; i++) { String nf = "newField" + i; String key = (nf + "_conceptValue"); BEAObject refValue = beaGet(req.get(key)); String refSelector = crud.renderInput(key, cc.makeConceptsComboBox(key, BEAObject.class), refValue) + hjs("$(\"[name=" + key + "]\").hide()"); String nativeSelector = crud.renderInput((nf + "_nativeValue"), cc.makeConceptsComboBox((nf + "_nativeValue"), Concept.class), null) + hjs("$(\"[name=" + nf + "_nativeValue]\").hide()"); List types = ll("String", "BEAObject", "Bool", "Native"); String toggleVis = "\r\n $(\"[name=" + nf + "_value]\").toggle(value == \"String\" || value == \"Bool\");\r\n $(\"#" + nf + "_refBox\").toggle(value == \"BEAObject\");\r\n $(\"#" + nf + "_nativeSel\").toggle(value == \"Native\");\r\n "; String type = req.get((nf + "_type")); String typeSelector = hselect_list(types, type, "name", (nf + "_type"), "onchange", "var value = this.value;" + toggleVis); if (nempty(type)) typeSelector += hscript("(function(value) {" + toggleVis + "})(" + jsQuote(type) + ")"); matrix.add(ll("Add field:
" + htextfield((nf + "_name"), req.get((nf + "_name")), "title", "New field name"), htmlTable2_noHtmlEncode(ll(ll(crud.renderTextField((nf + "_value"), "", 40) + span(refSelector, "id", (nf + "_refBox"), "style", "display: none"), span(nativeSelector, "id", (nf + "_nativeSel"), "style", "display: none"), "Type", typeSelector)), "noHeader", true, "tableParams", litobjectarray("style", "width: 100%")))); } }; crud.preprocessUpdateParams = params -> { params = cloneMap(params); params = mapValues(params, v -> eq(v, "") ? null : v); for (int i = 1; i <= max(newFieldsToShow, 10); i++) { String nf = "newField" + i; String name = trim(params.get((nf + "_name"))), type = params.get((nf + "_type")), refValue = params.get((nf + "_conceptValue")), nativeValue = params.get((nf + "_nativeValue")), value = params.get((nf + "_value")); if (eqic(type, "BEAObject")) { value = refValue; params.put("metaInfo_" + name, "concept"); } else if (eqic(type, "Native")) { value = nativeValue; params.put("metaInfo_" + name, "concept"); } else if (eqic(type, "Bool")) params.put("metaInfo_" + name, "bool"); if (eq(value, "")) value = null; if (nempty(name)) params.put(crud.fieldPrefix + name, value); } return params; }; if (!isMasterAuthed(req)) cc.objectCanBeEdited = id -> userCanEditObject(user(req), (BEAObject) cc.conceptForID(id)); } return crud; } public boolean userCanEditObject(User user, BEAObject o) { return user != null && (user.isMaster || cget("createdBy", o) == user); } @Override public Object servePossiblyUserlessBotFunction(DynGazelleRocks.Req req, String function, Map data, User user) { if (eq(function, "beaExport")) { Collection ids = concatLists(tok_integersAsLongs(urldecode(req.subURI)), tok_integersAsLongs(req.get("ids"))); var objects = map(__46 -> beaGet(__46), ids); return serveJSON_breakAtLevels(2, "result", map(objects, obj -> litorderedmap("id", obj.id, "gid", str(obj.globalID()), "struct", obj.structureString()))); } return super.servePossiblyUserlessBotFunction(req, function, data, user); } @Override public Object serveBotFunction(DynGazelleRocks.Req req, String function, Map data, User user) { if (eq(function, "beaList")) { long changedAfter = toLong(data.get("changedAfter")); double pollFor = min(bot_maxPollSeconds, toLong(data.get("pollFor"))); long startTime = sysNow(); if (changedAfter > 0 && changedAfter == now()) sleep(1); Collection objects; while (true) { objects = changedAfter == 0 ? list(BEAObject.class) : conceptsWithFieldGreaterThan_sorted(BEAObject.class, "_modified", changedAfter); if (nempty(objects) || pollFor == 0 || elapsedSeconds_sysNow(startTime) >= pollFor) return serveJSON_breakAtLevels(2, "result", map(objects, obj -> litorderedmap("gid", str(obj.globalID()), "struct", obj.structureString()))); sleep(bot_pollInterval); } } return super.serveBotFunction(req, function, data, user); } @Override public Object serveOtherPage2(DynGazelleRocks.Req req) { return serveOtherPage2((Req) req); } public Object serveOtherPage2(Req req) { { Object __31 = super.serveOtherPage2(req); if (__31 != null) return __31; } String uri = dropTrailingSlashIfNemptyAfterwards(req.uri); Matches m = new Matches(); if (swic_notSame(uri, "/beaCRUD/", m)) return renderBEAObjectTable(req, urldecode(m.rest())); if (eq(uri, "/inputs")) return renderBEAObjectTable(req, "input"); if (eq(uri, "/syntacticMatchesTable")) return hrefresh(baseLink + "/matchesTable?syntacticOnly=1"); if (eq(uri, "/matchesTable")) { boolean syntacticOnly = eq("1", req.get("syntacticOnly")); List list = new ArrayList(); for (BEAObject match : beaList("Match")) { BEAObject input = cgetOpt(BEAObject.class, match, "input"); BEAObject pat = cgetOpt(BEAObject.class, match, "pattern"); if (syntacticOnly && !calculations.isSyntacticPattern(pat)) continue; { if (calculations.patternAlwaysMatches(pat)) continue; } Map mapping = match.mapping(); if (input == null || pat == null || mapping == null) continue; list.add(match); } list = sortedByCalculatedFieldDesc(list, match -> conceptID(cgetBEA("input", match))); HTMLPaginator paginator = new HTMLPaginator(); paginator.processParams(req.params()); paginator.baseLink = addParamsToURL(baseLink, filterKeys(req.params(), p -> eq(p, "syntacticOnly"))); paginator.max = l(list); req.framer.add(divUnlessEmpty(paginator.renderNav())); list = subList(list, paginator.visibleRange()); List data = new ArrayList(); for (BEAObject match : list) { BEAObject input = cgetOpt(BEAObject.class, match, "input"); BEAObject pat = cgetOpt(BEAObject.class, match, "pattern"); Map mapping = match.mapping(); String patText = calculations.patternTextWithAngleBracketVars(pat); List tok = javaTokWithAngleBrackets(patText); BitSet bs = new BitSet(); tok = replaceAngleBracketVars(tok, mapping, bs); for (Pair __26 : bitSetStreaksAndNonStreaks(bs, l(tok))) { IntRange r = pairA(__26); boolean b = pairB(__26); String t = joinSubList(tok, r); replaceTokens(tok, r, b ? formatSubInput(input, t, "obj" + match.id) : htmlEncode2(t)); } String boldenedInput = join(tok); data.add(litorderedmap("Input", aname("obj" + match.id, joinWithBR(ahref(beaURL(input), htmlEncode2_str(call(input, "text"))), boldenedInput)), "Pattern", ahref(beaURL(pat), htmlEncode2(pat.text())), "DB object", beaHTML_justID(match), "Feedback", joinNemptiesWithSpace(calculations.feedbackHTML(match), addCommentHTML(match, "defaultComment", "good match", "redirectAfterSave", req.uriWithParams(), "text", "+")))); } req.framer.title = syntacticOnly ? "Syntactic matches table" : "Matches table"; req.framer.add(p()); req.framer.add(htmlTable2_noHtmlEncode(data)); return completeFrame(req); } if (eq(uri, "/rewritesTable")) { List data = new ArrayList(); for (BEAObject input : beaList("Input")) for (BEAObject r : beaBackRefs(input, "Rewrite")) data.add(litorderedmap("Input", input.text(), "Rewrite", tok_dropCurlyBrackets(r.text()), "Rewrite Type", getOpt("rewriteType", r))); req.framer.title = "Rewrites table"; req.framer.add(p()); req.framer.add(htmlTable2(data)); return completeFrame(req); } if (eq(uri, "/patternRewritesTable")) { List data = new ArrayList(); for (BEAObject pat : beaListAny("Pattern", "Syntactic Pattern")) for (BEAObject r : beaBackRefs(pat, "Rewrite")) if (cget("isRewriteOf", r) == pat) { Pair example = calculations.exampleForPatternRewrite(pat, r); data.add(litorderedmap("Pattern", ahref(beaURL(pat), htmlEncode2(pat.text())), "Rewrite", ahref(beaURL(r), htmlEncode2(tok_dropCurlyBrackets(r.text()))), "Example", joinWithBR(htmlEncode2(example.a), htmlEncode2("=> " + example.b)), "Rewrite Type", htmlEncode2_str(getOpt("rewriteType", r)), "Cmds", hPopDownButton(targetBlank(conceptEditLink(r), "Edit")))); } req.framer.title = "Pattern rewrites table"; req.framer.add(p()); req.framer.add(htmlTable2_noHtmlEncode(data)); return completeFrame(req); } if (eq(uri, "/syntacticPatternsWithoutRewrites")) { HCRUD crud = makeBEAObjectCRUD(req, "Syntactic Pattern"); HCRUD_Concepts data = (HCRUD_Concepts) (crud.data); IF1 prev = data.customFilter; data.customFilter = list -> { list = filter(list, c -> empty(beaBackRefs(c, "Rewrite"))); return postProcess(prev, list); }; crud.customTitle = "Syntactic patterns without rewrites"; return serveCRUD(req, BEAObject.class, crud); } if (eq(uri, "/inputsWithoutRewrites")) { HCRUD crud = makeBEAObjectCRUD(req, "input"); HCRUD_Concepts data = (HCRUD_Concepts) (crud.data); IF1 prev = data.customFilter; data.customFilter = list -> { list = filter(list, c -> empty(beaBackRefs(c, "Rewrite"))); return postProcess(prev, list); }; crud.customTitle = "Inputs without rewrites"; return serveCRUD(req, BEAObject.class, crud); } if (eq(uri, "/inputsWithRewrites")) { HCRUD crud = makeBEAObjectCRUD(req, "input"); HCRUD_Concepts data = (HCRUD_Concepts) (crud.data); IF1 prev = data.customFilter; data.customFilter = list -> { list = filter(list, c -> nempty(beaBackRefs(c, "Rewrite"))); return postProcess(prev, list); }; crud.customTitle = "Inputs with rewrites"; return serveCRUD(req, BEAObject.class, crud); } if (eq(uri, "/inputsWithoutMatches")) { HCRUD crud = makeBEAObjectCRUD(req, "input"); HCRUD_Concepts data = (HCRUD_Concepts) (crud.data); IF1 prev = data.customFilter; data.customFilter = list -> { list = filter(list, c -> empty(beaBackRefs(c, "Match"))); return postProcess(prev, list); }; crud.customTitle = "Inputs without matches"; return serveCRUD(req, BEAObject.class, crud); } if (eq(uri, "/patterns")) return renderBEAObjectTable(req, "pattern"); if (eq(uri, "/syntacticPatterns")) return renderBEAObjectTable(req, "Syntactic Pattern"); if (eq(uri, "/matches")) return renderBEAObjectTable(req, "match"); if (eq(uri, "/rewrites")) return renderBEAObjectTable(req, "rewrite"); if (eq(uri, "/aiTasks")) return renderBEAObjectTable(req, "AI Task"); if (eq(uri, "/query")) return calculations.serveQueryPage(req); if (eq(uri, "/queryHome")) return calculations.serveQueryPage(req, true); if (eq(uri, "/saveInput")) { if (userAgentIsBot(currentUserAgent())) return "You're a bot"; String text = trim(req.get("text")); String info = trim(req.get("info")); String uncleanedText = trim(req.get("uncleanedText")); if (empty(text)) return subBot_serve500("Input empty"); if (l(text) > maxInputLength) return subBot_serve500("Input too long"); BEAObject input = uniqCI(BEAObject.class, "type", "Input", "text", text, "createdBy", user(req)); saveUserAgent(input); if (nempty(info) || nempty(uncleanedText)) cnew(BEAObject.class, "type", "Input Source", "input", input, "source", info, "uncleanedText", uncleanedText); return hrefresh(or2(req.get("redirect"), baseLink + "/")); } if (eq(uri, "/markUseful")) { BEAObject o = beaGet("objectID", req); if (o == null) return subBot_serve500("Object not found"); uniqCI(BEAObject.class, "type", "Useful", "object", o, "createdBy", user(req)); return hrefresh(or2(req.get("redirect"), baseLink + "/")); } if (eq(uri, "/markBad")) { BEAObject o = beaGet("objectID", req); if (o == null) return subBot_serve500("Object not found"); uniqCI(BEAObject.class, "type", "Bad", "object", o, "createdBy", user(req)); return hrefresh(or2(req.get("redirect"), baseLink + "/")); } if (eq(uri, "/saveAnswer")) { String text = trim(req.get("text")); String rewriteType = or2(trim(req.get("rewriteType")), "Suggested Answer"); long inputID = toLong(req.get("inputID")); BEAObject input = beaGet(inputID); if (input == null) return subBot_serve500("Input not found"); if (empty(text)) return subBot_serve500("Text empty"); if (l(text) > maxInputLength) return subBot_serve500("Text too long"); uniqCI(BEAObject.class, "type", "Rewrite", "text", text, "isRewriteOf", input, "rewriteType", rewriteType, "createdBy", user(req)); return hrefresh(or2(req.get("redirect"), baseLink + "/")); } if (eq(uri, "/javaSource")) { BEAObject o = beaGet("id", req); return serveText(loadTextFile(javaSourceFileForObject(o))); } if (startsWith(uri, "/beaHTML/", m)) { req.noSpam(); String rest = m.rest(); int i = smartIndexOf(rest, "/"); BEAObject o = beaGet(takeFirst(i, rest)); req.subURI = substring(rest, i + 1); req.makers.add(o); IF0 result = call_optional(o, "html", req); if (result != null) return result.get(); result = call_optional(o, "html", req.webRequest); if (result != null) return result.get(); return call(o, "html"); } if (eq(uri, "/is-a-gazelle")) return "yes"; if (eq(uri, "/get-field")) { BEAObject obj = beaGet("obj", req); String field = req.get("field"); Object value = cget(field, obj); return serveText(str(value)); } if (sourceIsPublic || masterAuthed(req)) if (eqicOneOf(uri, "/source", "/sources", "/src", "sourceCode", "/code")) return "You will be redirected to my source code..." + hrefresh(3.0, snippetURL(programID())); if (!inMasterMode(req)) return null; if (eq(uri, "/uploadInputs")) return serveUploadTexts(req, "Input"); if (eq(uri, "/uploadPatterns")) return serveUploadTexts(req, "Pattern"); if (eq(uri, "/analyzeInput")) return calculations.serveAnalyzeInput(req); if (eq(uri, "/allBEATypes")) { return h2_title("All object types") + ul(renderObjectTypes()); } if (eq(uri, "/storeMatch")) { BEAObject pattern = beaGet(req.get("pattern")); BEAObject input = beaGet(req.get("input")); String label = req.get("label"); BEAObject match = calculations.reactInputWithPattern(input, pattern); if (match == null) return "Couldn't match"; cset(match, "label", label); return hrefresh(or2(req.get("redirect"), beaObjectURL(match))); } if (eq(uri, "/saveSyntacticPattern") || eq(uri, "/savePattern")) { String type = cic(uri, "syntactic") ? "Syntactic Pattern" : "Pattern"; String text = req.get("text"); BEAObject fromInput = beaGet(req.get("fromInput")); BEAObject pat = uniqCI(BEAObject.class, "type", type, "text", text, "createdBy", user(req)); csetIfUnset(pat, "fromInput", fromInput); if (fromInput != null) calculations.reactInputWithPattern(fromInput, pat); return hrefresh(or2(req.get("redirect"), baseLink + "/")); } if (eq(uri, "/runInstruction")) { BEAObject o = beaGet(req.get("instruction")); runInstruction(o); return hrefresh(beaObjectURL(o)); } if (eq(uri, "/convertResultToInput")) { BEAObject result = beaGet(req.get("result")); if (result == null) return subBot_serve500("Object not found"); String text = unquote(getString("result", result)); if (l(text) > maxInputLength) return subBot_serve500("Input too long"); BEAObject input = uniqCI(BEAObject.class, "type", "Input", "text", text); uniqCI(BEAObject.class, "type", "Input Source", "input", input, "source", result); return hrefresh(beaShortURL(input)); } if (eq(uri, "/setWordType")) { BEAObject r = beaGet(req.get("resultID")); String wordType = assertNempty(req.get("wordType")); List result = assertStringList(cget("result", r)); for (String s : result) uniqCI(BEAObject.class, "type", "Word type", "word", s, "wordType", wordType); return "OK"; } if (eq(uri, "/reload")) { dm_reloadModuleIn(3); return "Reloading module in 3"; } if (eq(uri, "/performAutoRuns")) { BEAObject o = beaGet(req.get("onObject")); if (o == null) return "Object not found"; rstAutoRuns.add(o); return hrefresh(beaURL(o)); } if (eq(uri, "/reactAllInputsWithAllSyntacticPatterns")) { return str(returnTimed(new Runnable() { public void run() { try { calculations.reactAllInputsWithAllSyntacticPatterns(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "calculations.reactAllInputsWithAllSyntacticPatterns();"; } })); } if (eq(uri, "/reactAllInputsWithPattern")) { BEAObject pat = beaGet(req.get("patternID")); if (pat == null) return "Pattern not found"; return str(returnTimed(new Runnable() { public void run() { try { calculations.reactAllInputsWithPattern(pat); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "calculations.reactAllInputsWithPattern(pat);"; } })); } if (eq(uri, "/storeSubInput")) { BEAObject input = beaGet(req.get("input")); if (input == null) return "Input not found"; String text = req.get("text"); String type = eqic(req.get("label"), "good") ? "Sub-Input" : "Bad Sub-Input"; BEAObject o = uniqCI(BEAObject.class, "type", type, "input", input, "text", text, "createdBy", currentUser()); return hrefresh(or2(req.get("redirect"), beaURL(o))); } if (eq(uri, "/performAutoRunOnAllObjects")) { BEAObject autoRun = beaGet("autoRun", req); performAutoRunOnAllObjects(autoRun); return "OK"; } if (eq(uri, "/autoMigrate")) { BEAObject o = beaGet("id", req); if (o == null) return "Object not found"; String url = beaURL(o); return hsansserif() + "Migrated " + beaHTML(o) + " to " + className(autoMigrateToCustomClass(o)) + hrefresh(1.0, url); } if (eq(uri, "/migrateToBase")) { BEAObject o = beaGet("id", req); if (o == null) return "Object not found"; String url = beaURL(o); return hsansserif() + "Migrated " + beaHTML(o) + " to " + className(autoMigrateToBase(o)) + hrefresh(1.0, url); } if (eq(uri, "/convertSyntacticToSemanticMatches")) { BEAObject match = beaGet("matchID", req); if (match == null) return "Match not found"; return ul_htmlEncode2(calculations.convertSyntacticToSemanticMatches(match)); } if (eq(uri, "/convertSyntacticToSemanticMatchesForWholePattern")) { BEAObject pat = beaGet("patternID", req); if (pat == null) return "Pattern not found"; return ul_htmlEncode2(calculations.convertSyntacticToSemanticMatchesForWholePattern(pat)); } if (eq(uri, "/createPatternListFromUsefulSyntacticPatterns")) return hrefresh(beaURL(calc.createPatternListFromUsefulSyntacticPatterns())); if (eq(uri, "/createConceptShadows")) return createConceptShadows(); if (eq(uri, "/compareConceptShadows")) { if (conceptShadows == null) return createConceptShadows(); List newShadows = allConceptShadows(); List> diff = diffConceptShadows(conceptShadows, newShadows); return subBot_serveText(nDiffs(diff) + ":\n\n" + pnlToString(diff)); } if (eq(uri, "/includeScript")) { String snippetID = req.get("id"); Object wired = hotwire(snippetID); return "Wired: " + wired; } if (eq(uri, "/dependents")) { BEAObject o = beaGet("id", req); return ul(lmap(__47 -> htmlEncode2_str(__47), objectWithDependents(o))); } if (eq(uri, "/prototypeUsers")) { BEAObject o = beaGet("id", req); return ul(lmap(__48 -> htmlEncode2_str(__48), prototypeUsers(o))); } if (eq(uri, "/activateObject")) { BEAObject o = beaGet("id", req); if (o == null) return "Object not found"; cset(o, "meta_shouldActivate", true); callActivate(o); return "Object activated." + hrefresh(1.0, beaURL(o)); } if (eq(uri, "/deactivateObject")) { BEAObject o = beaGet("id", req); if (o == null) return "Object not found"; cset(o, "meta_shouldActivate", false); callDeactivate(o); return "Object deactivated." + hrefresh(1.0, beaURL(o)); } if (eq(uri, "/reactivateDynamicObject")) { BEAObject o = beaGet("id", req); if (o == null) return "Object not found"; long id = o.id; reactivateAndMigratePrototypeUsers(o); return "OK" + hrefresh(1.0, beaURL(id)); } if (eq(uri, "/activateDynamicObject")) { BEAObject o = beaGet("id", req); if (o == null) return "Object not found"; compileAndLoadObject(o); o = beaGet("id", req); String url = or2(req.get("redirect"), beaURL(o)); return hsansserif() + "Migrated " + beaHTML(o) + " to " + className(o) + hrefresh(1.0, url); } if (eq(uri, "/callAnyMethod")) { BEAObject o = beaGet("id", req); if (o == null) return "Object not found"; String name = req.get("name"); boolean showPrints = eq("1", req.get("showPrints")); List args = new ArrayList(); for (Map.Entry __27 : _entrySet(req.params())) { String key = __27.getKey(); String val = __27.getValue(); List groups = regexpExtractGroups("^(arg|conceptArg)(\\d+)$", key); if (groups != null) { int idx = parseInt(second(groups)); String type = first(groups); Object arg = eq(type, "conceptArg") ? getConcept(parseLong(val)) : val; listSet(args, idx - 1, arg); } } String arg = req.get("arg"); if (arg != null) listSet(args, 0, arg); String result = hijackPrintPlusResult_text(() -> { try { return call(o, name, toObjectArray(args)); } catch (Throwable e) { printStackTrace(e); return null; } }); return serveText("Result of " + o + " . " + name + "(" + joinWithComma(args) + "):\n" + result); } if (eq(uri, "/download")) return subBot_serveFileWithName("gazelle-database." + ymd_minus_hms() + ".gz", conceptsFile()); if (eq(uri, "/errorSource")) return subBot_serveText(loadTextFile(transpilerErrorSourceFile())); if (eq(uri, "/changeUserPassword")) { String name = req.get("name"), newPW = req.get("newPW"); if (empty(name)) return "Need 'name' parameter (user name)"; if (empty(newPW)) return "Need 'newPW' parameter"; User user = conceptWhereCI(User.class, "name", name); cset(user, "passwordMD5", new SecretValue(hashPW(newPW))); return "Password for " + htmlEncode2(user) + " updated!"; } return null; } transient public List conceptShadows; public Object createConceptShadows() { long _startTime_28 = sysNow(); String profile = profileThisThreadToString(new Runnable() { public void run() { try { conceptShadows = allConceptShadows(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "conceptShadows = allConceptShadows();"; } }); done2_always("Make shadows", _startTime_28); return subBot_serveText(n2(conceptShadows, "shadow") + " made in " + lastTiming_formatted() + "\n\n" + profile); } public HCRUD makeBEAObjectCRUD(Req req, String type) { HCRUD crud = makeCRUD(BEAObject.class, req); crud.baseLink = req.uri(); HCRUD_Concepts data = (HCRUD_Concepts) (crud.data); data.itemName = () -> firstToUpper(type); data.addCIFilter("type", eqic(type, "(no type)") ? null : type); if (eqicOneOf(type, "Input", "Pattern", "AI Task")) { IF0> prev = data.emptyObject; data.emptyObject = () -> { Map item = data.emptyObject_fallback(prev); item.put("text", ""); return item; }; } return crud; } public Object renderBEAObjectTable(Req req, String type) { HCRUD crud = makeBEAObjectCRUD(req, type); return serveCRUD(req, BEAObject.class, crud); } public Object serveUploadTexts(Req req, String type) { String inputs = req.get("text"); List output = new ArrayList(); if (nempty(inputs)) { for (String text : tlft(inputs)) { Pair p = uniqCI2_sync(BEAObject.class, "type", type, "text", text); if (cget("uploadedBy", p.a) == null) cset(p.a, "uploadedBy", req.auth.user); output.add(type + " " + (p.b ? "added" : "exists") + " (ID " + p.a.id + "): " + text); } } return h2("Upload " + plural(type)) + hpostform(p(plural(type) + " (one per line):") + p(htextarea(inputs, "name", "text")) + pIfNempty(htmlEncode_nlToBR(lines(output))) + hsubmit("Upload " + plural(type))); } public Collection beaObjectsOfType(String type) { return conceptsWhereCI(BEAObject.class, "type", type); } public void reactAllInputsWithSomePatterns() { calculations.reactAllInputsWithSomePatterns(); } transient public IF0 navDiv; public String navDiv() { return navDiv != null ? navDiv.get() : navDiv_base(); } final public String navDiv_fallback(IF0 _f) { return _f != null ? _f.get() : navDiv_base(); } public String navDiv_base() { return div_vbar(newNavLinks(), "style", "margin-bottom: 0.5em"); } transient public IF0> newNavLinks; public List newNavLinks() { return newNavLinks != null ? newNavLinks.get() : newNavLinks_base(); } final public List newNavLinks_fallback(IF0> _f) { return _f != null ? _f.get() : newNavLinks_base(); } public List newNavLinks_base() { HCRUD crud = makeCRUD(BEAObject.class, currentReq()); int inputsWithoutRewrites = this.inputsWithoutRewrites.get(); int inputsWithoutMatches = this.inputsWithoutMatches.get(); int syntacticPatternsWithoutRewrites = this.syntacticPatternsWithoutRewrites.get(); String allObjectTypesLink = ahref(baseLink + "/allBEATypes", "All object types"); List items = new ArrayList(); if (showInputAndPatternNavItems) addAll(items, ahref(baseLink + "/query", "Query"), ahref(baseLink + "/newSearch", span_title("New BEA-based searched in development", "New Search (dev.)")), beaNavLink("Input", crud), beaNavLink("Pattern", crud), beaNavLink("Syntactic Pattern", crud), ahref(baseLink + "/syntacticPatternsWithoutRewrites", n2(syntacticPatternsWithoutRewrites) + " Syntactic patterns w/o rewrites"), beaNavLink("Match", crud), beaNavLink("Rewrite", crud), beaNavLink("AI Task", crud), ahref(baseLink + "/inputsWithoutMatches", n2(inputsWithoutMatches) + " Inputs w/o matches"), ahref(baseLink + "/inputsWithoutRewrites", n2(inputsWithoutRewrites) + " Inputs w/o rewrites")); addAll(items, inlineSwappable("navDiv_allObjectTypes", this, () -> allObjectTypesLink + " " + hPopDownButton(listPlus(renderObjectTypes(10), allObjectTypesLink))), new HTMLPopDownButton(ahref(baseLink + "/stats", "Stats"), !isMasterAuthed() ? null : ahref(baseLink + "/download", "DB download"), targetBlank(getProgramURL(), "Source code"), !isMasterAuthed() ? null : ahref(baseLink + "/refchecker", "Reference checker"), ahref(baseLink + "/inputsWithRewrites", "Inputs with rewrites"), ahref(baseLink + "/rewritesTable", "Rewrites table"), ahref(baseLink + "/patternRewritesTable", "Pattern rewrites table"), ahref(baseLink + "/matchesTable", "Matches table"), ahref(baseLink + "/matchesTable?syntacticOnly=1", "Syntactic matches table")).width(350).height(500).html()); return items; } public String beaNavLink(String type, HCRUD crud) { return beaNavLink(type, crud, beaCount(type)); } public String beaNavLink(String type, HCRUD crud, int count) { String plural = firstToLower(plural(type)); String link = baseLink + "/" + camelCase(plural); return ahref(link, n2(count) + " " + firstToUpper(plural)) + (!crud.actuallyAllowCreate() ? "" : " " + ahref(addParamToURL(link, "cmd", "new"), "+")); } public String beaObjectURL(BEAObject o) { return conceptLink(o, currentReq()); } public String matchDescHTML(BEAObject m) { try { BEAObject pat = cget("pattern", m); Map mapping = (Map) (cget("mapping", m)); return ahref_undecorated(crudLink(m), htmlEncode2(quote(getString("text", pat))) + "
  with " + renderEqualsCommaProperties(mapping)); } catch (Throwable __e) { _handleException(__e); } return htmlEncode2(str(m)); } public Collection beaList() { return list(BEAObject.class); } public Collection beaList(String type, Object... params) { return conceptsWhereCI(BEAObject.class, paramsPlus_inFront(params, "type", type)); } public Collection beaListAny(String... types) { return concatLists(lmap(__49 -> beaList(__49), litciset(types))); } public Collection beaListAny(Collection types) { return concatLists(lmap(__50 -> beaList(__50), asCISet(types))); } public BEAObject beaGet(long id) { return getConceptOpt(BEAObject.class, id); } public BEAObject beaGet(String id) { return beaGet(parseFirstLong(id)); } public boolean beaTypeIs(BEAObject o, String type) { return o != null && o.typeIs(type); } public BEAObject beaGet(String key, Req req) { return beaGet(req.get(key)); } public BEAObject beaGet(String field, BEAObject o) { return cgetBEA(field, o); } public BEAObject cgetBEA(String field, BEAObject o) { return (BEAObject) cget(field, o); } public BEAObject beaGet(BEAObject o, String field) { return cgetBEA(o, field); } public BEAObject cgetBEA(BEAObject o, String field) { return cgetBEA(field, o); } public String beaToHTML(BEAObject o) { return beaLinkHTML(o); } public String beaHTML(BEAObject o) { return beaLinkHTML(o); } public String beaLinkHTML(BEAObject o) { return o == null ? null : ahref(conceptLink(o), htmlEncode2_nlToBr(str(o))); } public String beaHTML_targetBlank(BEAObject o) { return o == null ? null : targetBlank(conceptLink(o), htmlEncode2_nlToBr(str(o))); } public String beaHTML_justID(BEAObject o) { return o == null ? null : ahref(beaShortURL(o), o.id); } public String beaFullURL(BEAObject o) { return o == null ? null : mainDomainWithProtocol + beaURL(o); } public String beaURL(long id) { return baseLink + "/" + id; } public String beaURL(BEAObject o) { return beaShortURL(o); } public String beaShortURL(BEAObject o) { return o == null ? null : baseLink + "/" + o.id; } public Collection beaBackRefs(BEAObject o, String type) { return objectsWhereCI(findBackRefs(BEAObject.class, o), "type", type); } public Collection beaBackRefs(BEAObject o) { return findBackRefs(BEAObject.class, o); } transient public IF1 serveDefaultPage; @Override public Object serveDefaultPage(DynGazelleRocks.Req req) { return serveDefaultPage != null ? serveDefaultPage.get(req) : serveDefaultPage_base(req); } final public Object serveDefaultPage_fallback(IF1 _f, DynGazelleRocks.Req req) { return _f != null ? _f.get(req) : serveDefaultPage_base(req); } public Object serveDefaultPage_base(DynGazelleRocks.Req req) { HTMLFramer1 framer = req.framer; framer.add(centerGazelleLogo()); return completeFrame(req); } public String centerGazelleLogo() { return hcenter3(hsnippetimg_scaleToWidth(200, "#1102967", 200, 110, "title", "Gazelle"), "style", "margin-top: 100px"); } public String html_loggedIn() { return html_loggedIn(true); } public String html_loggedIn(boolean offerLogInLink) { User user = user(currentReq()); return user == null ? (offerLogInLink ? ahref(baseLink + "/login", "Log in") : "Not logged in") : "Logged in as " + htmlEncode2(user.name); } transient public IVF1 distributeNewObject_impl; public void distributeNewObject_impl(BEAObject o) { if (distributeNewObject_impl != null) distributeNewObject_impl.get(o); else distributeNewObject_impl_base(o); } final public void distributeNewObject_impl_fallback(IVF1 _f, BEAObject o) { if (_f != null) _f.get(o); else distributeNewObject_impl_base(o); } public void distributeNewObject_impl_base(BEAObject o) { if (newObjectIsWorthNotification(o)) distributeNewObject_impl_noCheck(o); } transient public IF1 newObjectIsWorthNotification; public boolean newObjectIsWorthNotification(BEAObject o) { return newObjectIsWorthNotification != null ? newObjectIsWorthNotification.get(o) : newObjectIsWorthNotification_base(o); } final public boolean newObjectIsWorthNotification_fallback(IF1 _f, BEAObject o) { return _f != null ? _f.get(o) : newObjectIsWorthNotification_base(o); } public boolean newObjectIsWorthNotification_base(BEAObject o) { return !o.typeIsOneOf("Match", "Live WebSocket"); } transient public IVF1 distributeNewObject_impl_noCheck; public void distributeNewObject_impl_noCheck(BEAObject o) { if (distributeNewObject_impl_noCheck != null) distributeNewObject_impl_noCheck.get(o); else distributeNewObject_impl_noCheck_base(o); } final public void distributeNewObject_impl_noCheck_fallback(IVF1 _f, BEAObject o) { if (_f != null) _f.get(o); else distributeNewObject_impl_noCheck_base(o); } public void distributeNewObject_impl_noCheck_base(BEAObject o) { distributeNotification("New object: " + o); } public void performAutoRuns(BEAObject o) { AutoCloseable __34 = enter(); try { for (BEAObject autoRun : beaList("Auto Run")) { if (!isTrue(getOpt("enabled", autoRun))) { print("Not enabled: " + autoRun); continue; } performAutoRunOnObject(autoRun, o); } } finally { _close(__34); } } public void performAutoRunOnAllObjects(BEAObject autoRun) { for (BEAObject o : list(BEAObject.class)) performAutoRunOnObject(autoRun, o); } public void performAutoRunOnObject(BEAObject autoRun, BEAObject o) { ping(); String type = getString("onChangedObjectOfType", autoRun); if (!o.typeIs(type)) return; print("Running " + autoRun); BEAObject procedure = (BEAObject) (cget("procedure", autoRun)); String internalCode = getString("internalCode", procedure); printVars_str("internalCode", internalCode, "o", o); if (eqic(internalCode, "convertInputToPattern")) { String text = o.text(); if (!containsAngleBracketVars(text)) { print("No angle bracket vars"); return; } BEAObject p = uniqCI_returnIfNew(BEAObject.class, "type", "Pattern", "text", text); cset(p, "fromInput", o, "byProcedure", procedure, "byAutoRun", autoRun); print("p", p); } else if (eqic(internalCode, "convertInputToSyntacticPattern")) { String text = o.text(); if (!containsStars(text)) { print("No stars"); return; } BEAObject p = uniqCI_returnIfNew(BEAObject.class, "type", "Syntactic Pattern", "text", text); cset(p, "fromInput", o, "byProcedure", procedure, "byAutoRun", autoRun); print("p", p); } else if (eqic(internalCode, "dropPunctuationFromPattern")) { String text = o.text(); String text2 = dropPunctuation(text); if (eq(text, text2)) return; BEAObject p = uniqCI(BEAObject.class, "type", o.type(), "text", text2); cset(o, "withoutPunctuation", p); } else if (eqic(internalCode, "makeSyntacticPattern")) { print("sp", calculations.makeSyntacticPattern(o)); } else if (eqic(internalCode, "runFunctionOnInput")) { print("runFunctionOnInput"); BEAObject function = beaGet(o, "function"); BEAObject input = beaGet(o, "input"); if (function == null || input == null) { print("Missing parameters"); return; } if (cget("result", o) != null) { print("Has result"); return; } BEAObject result = calculations.reactFunctionWithInput(calculations.new BackEndAlgorithm(), function, input); print("result", result); if (result != null) { cset(result, "request", o); cset(o, "result", result); } } else print("Unknown internal code"); } public BEAObject findInput(String text) { return conceptWhereIC(BEAObject.class, "type", "Input", "text", text); } public String addRewriteHTML(BEAObject o) { return ahref(addParamsToURL(crudLink(BEAObject.class), "cmd", "new", "title", "Add Rewrite", "f_type", "Rewrite", "f_text", getStringOpt("text", o), "f_isRewriteOf", o.id, "metaInfo_isRewriteOf", "concept"), "Add Rewrite"); } @Override public Object serveIntegerLink(DynGazelleRocks.Req req, long id) { BEAObject o = getConceptOpt(BEAObject.class, id); if (o != null) return htitle(str(o)) + hrefresh(conceptLink_long(o)); return super.serveIntegerLink(req, id); } public void distributeTestNotification() { distributeNotification("It is " + localTimeWithSeconds()); } public void distributeNotification(String text) { notificationQ.add(new Runnable() { public void run() { try { String jsCode = "window.createNotification({ theme: 'success', showDuration: 3000 })(" + jsonEncodeMap("message", text) + ");"; sendJavaScriptToAllWebSockets(jsCode); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "/*for (User user)\r\n if (nemptyAfterTrim(user.notificationSetting))\r\n ..."; } }); } public void sendJavaScriptToAllWebSockets(String jsCode) { for (Pair p : syncMapToPairs(webSockets)) { pcall(p.a, "send", jsonEncodeMap("eval", jsCode)); } } public void runInstruction(BEAObject o) { if (o == null) return; try { BEAObject instruction = o; if (o.typeIs("Step in script")) instruction = (BEAObject) cget(o, "instruction"); if (instruction.typeIs("Instruction | List objects by type")) { saveInstructionResult(o, beaList(getString("typeToList", instruction))); return; } if (instruction.typeIs("Instruction | List object types")) { saveInstructionResult(o, distinctCIFieldValuesOfConcepts(BEAObject.class, "type")); return; } if (instruction.typeIs("Instruction | Filter list by text starting with")) { BEAObject scriptRun = cgetBEA("scriptRun", o); if (scriptRun == null) throw fail("Need to be run as part of script"); List steps = scriptRunSteps(scriptRun); int idx = indexOf(steps, o); if (idx < 0) throw fail("Step not found in script run"); List list = firstNotNull(map(reversed(takeFirst(steps, idx)), step -> optCast(List.class, cget("data", cgetBEA("result", step))))); String prefix = getString("prefix", instruction); List filtered = filter(list, obj -> swic(obj.text(), prefix)); saveInstructionResult(o, filtered); return; } if (instruction.typeIs("Script")) { BEAObject script = instruction; BEAObject scriptRun = cnew(BEAObject.class, "type", "Script Run", "script", script); int i = 0; List steps = new ArrayList(); while ((instruction = (BEAObject) cget(instruction, "step" + (++i))) != null) { BEAObject step = cnew(BEAObject.class, "type", "Step in script", "step", i, "scriptRun", scriptRun, "instruction", instruction); steps.add(step); } cset(scriptRun, "steps", steps); return; } cnew(BEAObject.class, "type", "Instruction Error", "instruction", o, "error", "Unknown instruction type"); } catch (Throwable e) { cnew(BEAObject.class, "type", "Instruction Error", "instruction", o, "error", getStackTrace(e)); } } public BEAObject saveInstructionResult(BEAObject instruction, Object data) { BEAObject result = cnew(BEAObject.class, "type", "Instruction Result", "instruction", instruction, "data", data); cset(instruction, "result", result); return result; } public List scriptRunSteps(BEAObject scriptRun) { return (List) cget("steps", scriptRun); } public String addCommentHTML(BEAObject o, Object... __) { String redirectAfterSave = (String) (optPar("redirectAfterSave", __)); String defaultComment = (String) (optPar("defaultComment", __)); String text = optPar("text", __, "Add comment"); return ahref(addParamsToURL(crudLink(BEAObject.class), "cmd", "new", "title", "Add Comment", "f_type", "Comment", "f_on", o.id, "f_text", unnull(defaultComment), "redirectAfterSave", redirectAfterSave, "autofocus", "f_text", "metaInfo_on", "concept"), text); } public String formatSubInput(BEAObject input, String text, String anchor) { String redirect = addAnchorToURL(currentReq.get().uriWithParams(), anchor); int goodCount = countConceptsCI(BEAObject.class, "type", "Sub-Input", "input", input, "text", text); int badCount = countConceptsCI(BEAObject.class, "type", "Bad Sub-Input", "input", input, "text", text); return calculations.bestInputHTML(text) + " " + small(joinNemptiesWithSpace(ahref(addParamsToURL(baseLink + "/storeSubInput", "label", "good", "text", text, "input", input.id, "redirect", redirect), unicode_thumbsUp()), goodCount == 0 ? "" : n2(goodCount), ahref(addParamsToURL(baseLink + "/storeSubInput", "label", "bad", "text", text, "input", input.id, "redirect", redirect), unicode_thumbsDown()), badCount == 0 ? "" : n2(badCount))); } public List renderObjectTypes() { return renderObjectTypes(Integer.MAX_VALUE); } public List renderObjectTypes(int max) { MultiSet ms = distinctCIFieldValuesOfConcepts_multiSet(BEAObject.class, "type"); return mapPairs(takeFirst(max, multiSetToPairsByPopularity(ms)), (type, count) -> { String _type = or2(type, "(no type)"); return n2(count) + " " + ahref(baseLink + "/beaCRUD/" + urlencode(_type), htmlEncode2(_type)); }); } public BEAObject autoMigrateToBase(BEAObject o) { return migrateToClass(o, BEAObject.class); } public BEAObject autoMigrateToCustomClass(BEAObject o) { return migrateToClass(o, defaultCustomClass(o)); } public BEAObject migrateToClass(BEAObject o, Class targetClass) { if (o == null || o._concepts != db_mainConcepts()) return null; if (targetClass != null && targetClass != _getClass(o)) { BEAObject newObject = unlistedCopyToClass_withConverter_pcall(targetClass, o); beaCallOpt(newObject, "_fixAfterMigration", o); return replaceConceptAndUpdateRefs(o, newObject); } return o; } public void setCodeState(BEAObject o, long timestamp, String codeHash, Object state) { if (empty(state)) state = null; cset(o, "meta_codeState", state); } public String codeForObject(BEAObject o) { return getStringOpt("meta_code", o); } public String actualCodeHash(BEAObject o) { return md5OrNull(codeForObject(o)); } public boolean codeHashUpToDate(BEAObject o) { return eq(actualCodeHash(o), getStringOpt("meta_codeHash", o)); } public boolean compileErrorInObject(BEAObject o) { return swic(getStringOpt("meta_codeState", o), "error."); } public boolean isDependentOn(BEAObject a, BEAObject b) { return contains(optCast(Concept.RefL.class, getOpt("meta_dependsOnCode", a)), b); } public Collection objectWithDependents(BEAObject o) { return stepAllAndGet(new TransitiveHull<>(x -> filter(y -> isDependentOn(y, x), list(BEAObject.class)), o)); } public void compileAndLoadObject(BEAObject o) { dm_mediumRefreshTranspiler(); Collection withDependents = objectWithDependents(o); print("Dependents for " + o + ": " + withDependents); for (BEAObject x : withDependents) compileAndLoadObject_withoutDependents(x); } public Class protoClass(BEAObject o) { BEAObject proto = cget("meta_prototype", o); Class protoClass = null; if (proto != null) { if (!isActivatedDynamicObject(proto)) throw fail("Prototype not activated: " + proto); protoClass = proto.getClass(); } return protoClass; } public String setCodeHash(BEAObject o) { String code = codeForObject(o); String codeHash = empty(code) ? null : md5(code); cset(o, "meta_codeHash", codeHash); return codeHash; } public void compileAndLoadObject_withoutDependents(BEAObject o) { try { print("compileAndLoadObject_withoutDependents", o); assertSame(db_mainConcepts(), o._concepts); ClassLoader cl = dm_moduleClassLoader(); long timestamp = now(); setCodeState(o, timestamp, null, "Compiling"); String codeHash = null; transpileAndCompileForHotwiring_src.set(null); try { Class protoClass = protoClass(o); codeHash = setCodeHash(o); String code = codeForObject(o); if (empty(code)) { if (protoClass != null) { o = migrateToClass(o, protoClass); setCodeState(o, timestamp, null, "Migrated to proto class " + shortClassName(protoClass)); callActivate(o); } else { o = autoMigrateToBase(o); setCodeState(o, timestamp, null, null); } return; } actualCompile(o); reactivateAndMigratePrototypeUsers(o); print("Done compiling"); } catch (Throwable e) { cset(o, "meta_javaClass", null); setCodeState(o, timestamp, codeHash, "Error. " + exceptionToStringShort(e)); throw rethrow(e); } } catch (Exception __e) { throw rethrow(__e); } } public void setCodeHashAndActualCompile(BEAObject o) { setCodeHash(o); actualCompile(o); } public void actualCompile(BEAObject o) { Class protoClass = protoClass(o); cset(o, "meta_codeSafety", null); DynClassName dcn = new DynClassName(o.id, aGlobalID(), o.id); cset(o, "meta_javaClass", dcn.fullClassName()); String code = codeForObject(o); List tok = javaTok(code); String baseClass = "BEAObject"; List implementedInterfaces = new ArrayList(); for (int i : jfindAll(tok, "implements", (_tok, nIdx) -> { String left = get(_tok, nIdx - 1); return !(isIdentifier(left) || eq(left, ">")); })) { int iSemi = tok_findEndOfStatement(tok, i) - 1; List tokWhat = subList(tok, i + 1, iSemi); String className; if (nCodeTokens(tokWhat) == 3 && eq(tokWhat.get(3), "from")) { className = tokWhat.get(1); className = "B" + tokWhat.get(5) + "." + className; } else className = join(dropFirstAndLast(tokWhat)); className = expandShortClassRef(className); implementedInterfaces.add(className); clearTokens(tok, i, iSemi + 2); } int tokIdx; if ((tokIdx = jfind_any(tok, "extends ;", "extends .;", "extends ..;")) > 0) { int j = indexOf(tok, ";", tokIdx); baseClass = joinSubList(tok, tokIdx + 2, j - 1); baseClass = expandShortClassRef(baseClass); clearTokens(tok, tokIdx, j + 2); } if ((tokIdx = jfind(tok, "extends from *;")) > 0) { String className = tok.get(tokIdx + 2); baseClass = expandShortClassRef("B" + parseFirstLong_regexp(tok.get(tokIdx + 6)) + "." + className); int j = indexOf(tok, ";", tokIdx); clearTokens(tok, tokIdx, j + 2); } if ((tokIdx = jfind(tok, "extends <> from ;")) > 0) { String className = tok.get(tokIdx + 2); int j = indexOf(tok, ";", tokIdx); baseClass = expandShortClassRef("B" + tok.get(j - 2) + "." + className) + joinSubList(tok, tokIdx + 4, j - 5); clearTokens(tok, tokIdx, j + 2); } List imports = new ArrayList(); while ((tokIdx = jfind(tok, "import from ;")) > 0) { String className = tok.get(tokIdx + 2); String name = expandShortClassRef("B" + tok.get(tokIdx + 6) + "." + className); imports.add("import " + name + ";"); int j = indexOf(tok, ";", tokIdx); clearTokens(tok, tokIdx, j + 2); } String global = getGlobalCode(tok); code = join(tok); String src = lines(imports) + "mainPackage " + dcn.makePackageName() + "\n" + "mainClassName " + dcn.makeMainClassName() + "\n" + "concept " + dcn.makeBEAClassName() + " extends " + (protoClass == null ? baseClass : protoClass.getName().replace("$", ".")) + (empty(implementedInterfaces) ? "" : " implements " + joinWithComma(implementedInterfaces)) + " {\n" + code + "\n}\n" + global + "\n" + customCodePostlude(); ClassLoader cl = dm_moduleClassLoader(); var files = filesFromClassLoader(cl); print("Compiling with " + n2(files, "byte code path")); javaCompileToJar_localLibraries.set(files); File bytecode; List libs = new ArrayList(); try { bytecode = transpileAndCompileForHotwiring(src, libs); libs.remove(str(parseSnippetID(mainLibID))); print("libs", libs); } finally { String javaSrc = transpileAndCompileForHotwiring_src.get(); saveTextFile(javaSourceFileForObject(o), javaSrc); List dependencies = new ArrayList(); for (String id : tok_identifiers(javaTok(javaSrc))) { long objID = regexpToLongIC("^b_?(\\d+)", id); if (objID != 0 && objID != o.id) dependencies.add(getConceptOrMarkMissingObject(BEAObject.class, objID)); } cset(o, "meta_libraries", nullIfEmpty(joinWithSpace(libs))); cset(o, "meta_dependsOnCode", nullIfEmpty(dependencies)); } renameFileVerbose(bytecode, dcn.byteCodeFile()); cset(o, "meta_javaClass", dcn.fullClassName()); } public void reactivateAndMigratePrototypeUsers(BEAObject o) { var p = reactivateDynamicObject(o); if (p == null) throw fail("Activation of " + o.id + " failed. " + getOpt("meta_codeState", o)); File deadPath = byteCodePathForClass(o); print("New dead class path: " + deadPath + " (total: " + n2(deadClassPaths) + ")"); deadClassPaths.add(deadPath); o = p.a; assertNotNull("Activated object", o); String codeHash = getStringOpt("meta_codeHash", o); long timestamp = now(); setCodeState(o, timestamp, codeHash, "Custom code loaded"); print("Custom code loaded"); migratePrototypeUsers(o); } public String expandShortClassRef(String name) { List groups = regexpGroups("^B(\\d+)(\\..+)$", name); if (groups != null) { long baseID = parseLong(first(groups)); BEAObject baseObj = beaGet(baseID); name = mainClassNameForObject(baseObj) + second(groups); if (empty(name)) throw fail("Referenced object doesn't have a Java class: " + baseID); } return name; } public void migratePrototypeUsers(BEAObject o) { for (BEAObject derivative : prototypeUsers(o)) { try { print("Migrating prototype user " + derivative + " of " + o + " to new base class " + o.getClass()); migrateToClass(derivative, o.getClass()); } catch (Throwable __e) { _handleException(__e); } } } public boolean basedOnUnactivatedPrototype(BEAObject o) { BEAObject proto = optCast(BEAObject.class, cget("meta_prototype", o)); return proto != null && !isActivatedDynamicObject(proto); } public Collection prototypeUsers(BEAObject o) { TransitiveHull th = new TransitiveHull<>(x -> filter(conceptsWhere(BEAObject.class, "meta_prototype", x), d -> empty(codeForObject(d))), o); return setMinus_inPlace(stepAllAndGet(th), o); } public Pair reactivateDynamicObject(BEAObject o) { try { return reactivateDynamicObject_impl(o); } catch (Throwable e) { _handleException(e); cset(o, "meta_javaClass", null); cset(o, "meta_codeState", exceptionToStringShort(e)); return null; } } public boolean isActivatedDynamicObject(BEAObject o) { return startsWith(className(o), packagePrefix()); } public Pair reactivateDynamicObject_impl(BEAObject o) { try { assertNotNull(o); assertSame(db_mainConcepts(), o._concepts); DynClassName dcn = dcnForObject(o); if (dcn == null) { cset(o, "meta_codeState", null); return pair(o, false); } File byteCode = dcn.byteCodeFile(); loadObjectsLibraries(o); dm_addByteCodePathToModuleClassLoader(byteCode); String fullName = dcn.fullClassNameForVM(); print("fullName", fullName); ClassLoader cl = dm_moduleClassLoader(); Class targetClass = cl.loadClass(fullName); print("targetClass", targetClass); assertNotNull("Class not found: " + fullName, targetClass); o = migrateToClass(o, targetClass); assertNotNull("autoMigrated", o); callActivate(o); cset(o, "meta_codeState", "[" + formatGMTWithMilliseconds_24() + "] " + "Custom code reloaded"); return pair(o, true); } catch (Exception __e) { throw rethrow(__e); } } public File javaSourceFileForObject(BEAObject o) { return programFile("BEA Java Sources/" + o.id + ".java"); } public File byteCodeFile(DynClassName name) { return name == null ? null : programFile("Object ByteCode/" + name.objectID + "-" + name.globalID + ".jar"); } public DynClassName dcnForObject(BEAObject o) { String javaClassName = getString("meta_javaClass", o); return DynClassName_parse(javaClassName); } public File byteCodeFileForObject(BEAObject o) { DynClassName dcn = dcnForObject(o); return dcn == null ? null : dcn.byteCodeFile(); } public BEAObject autoMigrateUnlistedOrKeep(BEAObject o) { return canAutoMigrate(o) ? unlistedCopyToClass_withConverter(defaultCustomClass(o), o) : o; } public boolean canAutoMigrate(BEAObject o) { return o != null && !eqOneOf(defaultCustomClass(o), _getClass(o), null); } public Class defaultCustomClass(BEAObject o) { if (o == null) return null; BEAObject entry = conceptWhereCI(BEAObject.class, "type", "Auto Custom Class", "forType", o.type()); if (entry == null) return null; String className = getString("toClassName", entry); Class c = findClassThroughDefaultClassFinder(className); return c; } public String queryLink(String algorithm, String q) { return addParamsToURL(baseLink + "/query", "algorithm", algorithm, "q", q); } public String currentUserAgent() { Req req = currentReq(); if (req == null) return null; return mapGet(req.webRequest.headers(), "user-agent"); } public void saveUserAgent(BEAObject input) { String userAgent = currentUserAgent(); if (nempty(userAgent) && !userAgentIsBot(userAgent)) uniqCI2(BEAObject.class, "type", "Input Source", "input", input, "userAgent", userAgent); } public boolean isObjectWithCode(BEAObject o) { return o != null && o.hasCustomCode(); } public boolean createdByMasterUser(BEAObject o) { User user = optCast(User.class, cget("createdBy", o)); return user != null && user.isMaster; } public void autoActivateDynamicObject(BEAObject o) { if (!autoActivateDynamicObjects) return; String code = codeForObject(o); if (empty(code)) return; if (!createdByMasterUser(o)) return; String codeState = getString("meta_codeState", o); String time = leadingAngleBracketStuff(codeState); long timestamp = empty(time) ? 0 : parseDateWithMillisecondsGMT(time); if (o._modified <= timestamp) return; String hash = getString("meta_codeHash", o); if (eq(hash, md5(code))) return; print("Auto-activating dynamic object: " + o); compileAndLoadObject(o); sleepSeconds(1); } public String customCodePostlude() { return "!include early #1031282\n\n"; } public String makeJavaClassName(BEAObject o) { return aGlobalID(); } public String packagePrefix() { return "dyn.b_"; } public String makePackageName(BEAObject o) { return packagePrefix() + o.id; } public BEAObject websiteEnhancersObject() { Collection l = beaList("Live Website Enhancers"); if (l(l) > 1) warn("Multiple Live Website Enhancers: " + l); return first(l); } public BEAObject getWebsiteEnhancer(String key) { return beaGet(websiteEnhancersObject(), key); } public String newLinkForCRUD(HCRUD crud, Class c) { if (c == User.class) return baseLink + "/register"; else return super.newLinkForCRUD(crud, c); } public String callHTMLFixer(Object caller, String html) { BEAObject fixer = beaGet("htmlFixer", websiteEnhancersObject()); return or((String) callOpt(fixer, "fixHTML", html, caller), html); } @Override public Object html3(DynGazelleRocks.Req req) { return html3((Req) req); } public Object html3(Req req) { assertSame(realMC(), mc()); assertSame(main.beaMod(), me()); vmBus_send("logMethodCall", this, "html3", req); vmBus_send("html3", this, req); BHTTPRequest reified = null; if (reifyHTTPRequests()) reified = cnew(BHTTPRequest.class, "type", "Live HTTP Request", "req", req, "uri", req.uri(), "ip", req.webRequest.clientIP(), "user", user(req)); long time = sysNow(); try { return html4(req); } finally { time = sysNow() - time; if (reified != null) cset(reified, "type", "Past HTTP Request", "processingTime", time); } } public Object html4(Req req) { if (saveAllQsAsInput && !cic(req.uri(), "/live") && user(req) != null) { String text = trim(req.get("q")); if (nempty(text)) { BEAObject input = uniqCI(BEAObject.class, "type", "Input", "text", text); uniqCI(BEAObject.class, "type", "Input Source", "input", input, "source", "Web Request (q parameter)"); } } Object response = html5(req); if (response instanceof String) response = routeThroughAll(pagePostProcessors, ((String) response)); return response; } transient public List> pagePostProcessors = syncL(); public Object html5(Req req) { String uri = req.uri(); for (BEAObject rewrite : filter(__51 -> createdByMasterUser(__51), beaList("Live URL Rewrite"))) { try { String input = getString("input", rewrite); boolean ci = isTrueOpt("caseInsensitive", rewrite); vmBus_send("testingURLRewrite", rewrite, uri); if (eqOrEqic(ci, uri, input)) { vmBus_send("applyingURLRewrite", rewrite, uri); String output = getString("output", rewrite); return hrefresh(appendParamsToURL(output, req.params())); } } catch (Throwable __e) { _handleException(__e); } } return super.html3(req); } @Override public Object serveOtherPage(DynGazelleRocks.Req req) { return serveOtherPage((Req) req); } public Object serveOtherPage(Req req) { if (eq(req.uri, "/login")) return serveAuthForm(null); if (startsWith_addingSlash(req.uri, "/myLoginData")) return serveMyLoginData(req); return super.serveOtherPage(req); } public void cleanMeUp_deactivateBEAObjects() { Collection objects = filter(beaList(), o -> hasMethod(o, "_deactivate")); print("Deactivating " + nObjects(objects) + " for shutdown"); for (BEAObject bea : objects) pcallOpt(bea, "_deactivate"); if (nempty(objects)) print("Done"); } public DynClassName DynClassName_parse(String className) { className = replace(className, "$", "."); List groups = regexpFirstGroups("^dyn\\.b_(\\d+)_([a-z]+)\\.B(\\d+)\\$?(.*)", className); if (verboseClassLoading) printVars("DynClassName_parse", "className", className, "groups", groups); if (groups == null) return null; return new DynClassName(parseLong(first(groups)), second(groups), parseLong(third(groups)), fourth(groups)); } public DynClassName DynClassName_parseOther(String className) { className = replace(className, "$", "."); List groups = regexpFirstGroups("^dyn\\.b_(\\d+)_([a-z]+)\\.(.*)", className); if (verboseClassLoading) printVars("DynClassName_parseOther", "className", className, "groups", groups); if (groups == null) return null; return new DynClassName(parseLong(first(groups)), second(groups), 0, third(groups)); } public String DynClassName_mainPrefix() { return "dyn."; } public String DynClassName_mainPrefixForVM() { return "dyn."; } public class DynClassName { public long compileBaseObjectID; public String globalID; public long objectID; public String innerClass; public DynClassName(long compileBaseObjectID, String globalID, long objectID) { this.objectID = objectID; this.globalID = globalID; this.compileBaseObjectID = compileBaseObjectID; } public DynClassName(long compileBaseObjectID, String globalID, long objectID, String innerClass) { this.innerClass = innerClass; this.objectID = objectID; this.globalID = globalID; this.compileBaseObjectID = compileBaseObjectID; } public String makePackageName() { return dropDotSuffix(DynClassName_mainPrefix()); } public String makeMainClassName() { return "b_" + compileBaseObjectID + "_" + globalID; } public String makeBEAClassName() { return joinNempties("$", objectID == 0 ? "" : "B" + objectID, innerClass); } public String fullClassName() { return joinWithDot(makePackageName(), makeMainClassName(), makeBEAClassName()); } public String fullClassNameForVM() { return makePackageName() + "." + makeMainClassName() + "$" + makeBEAClassName(); } public File byteCodeFile() { return programFile("Object ByteCode/" + baseSystemVersion + "/" + compileBaseObjectID + "/" + globalID + ".jar"); } } public String mainClassNameForObject(BEAObject o) { if (o == null) return null; String fullBaseClass = getString("meta_javaClass", o); return takeFirst(fullBaseClass, lastIndexOf(fullBaseClass, ".")); } public void loadObjectsLibraries(BEAObject o) { List libs = splitAtSpace_trim(getString("meta_libraries", o)); if (addLibrariesToClassLoader(dm_moduleClassLoader(), libs)) print("Loaded libs for " + o + ": " + libs); } public void callActivate(BEAObject bea) { if (getBoolOpt("meta_shouldActivate", bea, true)) { loadObjectsLibraries(bea); pcallOpt(bea, "_activate"); } } public void callDeactivate(BEAObject bea) { pcallOpt(bea, "_deactivate"); } public void onNewWebSocket(WebSocketInfo ws) { super.onNewWebSocket(ws); Req req = (Req) (ws.req); String uri = ws.uri; BEAObject reifiedWebSocket = null; if (webSocketsToBEA()) { long _startTime_29 = sysNow(); var webRequest = req == null ? null : req.webRequest; var cookie = cookieFromWebRequest(webRequest); User user = user(req); reifiedWebSocket = cnew(webSocketClass, "webSocketInfo", ws, "type", "Live WebSocket", "uri", uri, "user", user); ws.dbRepresentation = reifiedWebSocket; ws.eval(replaceDollarVars("if (typeof $ !== \"undefined\") $(\".webSocketPlaceholder\").html($html);", "html", jsQuote(beaHTML(reifiedWebSocket)))); done2_always("Reify WebSocket", _startTime_29); } onNewWebSocket2(ws); Matches m = new Matches(); if (startsWith(uri, "/beaHTML/", m)) { BEAObject handler = beaGet(m.rest()); if (handler != null) { print("Have WebSocket " + reifiedWebSocket + "/" + ws + " at " + uri + " for " + handler); if (!handler._isAlive()) { print("HAndler not alive!?"); return; } cset(reifiedWebSocket, "handler", handler); beaPcall(handler, "useWebSocket", ws); } } } transient public IVF1 onNewWebSocket2; public void onNewWebSocket2(WebSocketInfo ws) { if (onNewWebSocket2 != null) onNewWebSocket2.get(ws); else onNewWebSocket2_base(ws); } final public void onNewWebSocket2_fallback(IVF1 _f, WebSocketInfo ws) { if (_f != null) _f.get(ws); else onNewWebSocket2_base(ws); } public void onNewWebSocket2_base(WebSocketInfo ws) { } transient public Class webSocketClass = BWebSocket.class; public void onWebSocketClose(WebSocketInfo ws) { super.onWebSocketClose(ws); retireReifiedWebSocket((BWebSocket) ws.dbRepresentation); } public void retireReifiedWebSocket(BWebSocket ws) { if (!keepReifiedWebSockets()) cdelete(ws); else cset(ws, "type", "Closed WebSocket"); } transient public TimedCache webSocketsToBEA_cache = new TimedCache<>(10.0, () -> webSocketsToBEA_load()); public boolean webSocketsToBEA() { return webSocketsToBEA_cache.get(); } public Boolean webSocketsToBEA_load() { return getBoolOpt("webSocketsToBEA", liveGazelleSettings()); } transient public TimedCache keepReifiedWebSockets_cache = new TimedCache<>(10.0, () -> keepReifiedWebSockets_load()); public boolean keepReifiedWebSockets() { return keepReifiedWebSockets_cache.get(); } public Boolean keepReifiedWebSockets_load() { return getBoolOpt("keepReifiedWebSockets", liveGazelleSettings()); } transient public TimedCache reifyHTTPRequests_cache = new TimedCache<>(10.0, () -> reifyHTTPRequests_load()); public boolean reifyHTTPRequests() { return reifyHTTPRequests_cache.get(); } public Boolean reifyHTTPRequests_load() { return getBoolOpt("reifyHTTPRequests", liveGazelleSettings()); } public BSettings gazelleSettings() { return conceptWhere(BSettings.class); } public BEAObject liveGazelleSettings() { return first(filter(__52 -> createdByMasterUser(__52), beaList("Live Gazelle Settings"))); } public Object withFrame(String html) { return serveWithNavigation(html); } public Object serveWithNavigation(String html) { var framer = framer(); framer.add(html); return completeFrame(); } @Override public Object completeFrame_base(DynGazelleRocks.Req req) { if (nempty(((Req) req).makers)) ((Req) req).framer.addNavItem(hPopDownButtonWithText("Page by", map(((Req) req).makers, o -> { if (o instanceof BEAObject) return beaHTML_targetBlank((BEAObject) o); return htmlEncode2(shortenStr(o, 40)); }))); return super.completeFrame_base(((Req) req)); } public List> renderStats_base() { return listPlusItems_inPlace(super.renderStats_base(), ll("Live / dead class paths", n2(classLoaderPathsCount(dm_moduleClassLoader())) + " / " + n2(deadClassPaths))); } transient public IVF2 makeNavItems; @Override public void makeNavItems(DynGazelleRocks.Req req, HTMLFramer1 framer) { if (makeNavItems != null) makeNavItems.get(req, framer); else makeNavItems_base(req, framer); } final public void makeNavItems_fallback(IVF2 _f, DynGazelleRocks.Req req, HTMLFramer1 framer) { if (_f != null) _f.get(req, framer); else makeNavItems_base(req, framer); } public void makeNavItems_base(DynGazelleRocks.Req req, HTMLFramer1 framer) { super.makeNavItems(req, framer); String webSocketPlaceholder = span("", "class", "webSocketPlaceholder"); if (moveNavItemsToMisc) { var navItems = cloneList(framer.navItems); framer.clearNavItems(); framer.addNavItem(hPopDownButtonWithText("Misc", map(navItems, ni -> framer.renderNavItem(ni))) + " " + webSocketPlaceholder); } else framer.addNavItem(webSocketPlaceholder); if (findConcept(CMissingObject.class) != null) framer.addNavItem(makeClassNavItem(CMissingObject.class, req)); } transient public int showMoreThreshold = 500; transient public int showMoreHeight = 250; transient public IF1 addShowMoreButton; public String addShowMoreButton(String html) { return addShowMoreButton != null ? addShowMoreButton.get(html) : addShowMoreButton_base(html); } final public String addShowMoreButton_fallback(IF1 _f, String html) { return _f != null ? _f.get(html) : addShowMoreButton_base(html); } public String addShowMoreButton_base(String html) { if (l(html) < showMoreThreshold) return html; return new HDivWithVerticalExpandButton(showMoreHeight, html).html(); } public boolean deleteQsWhenEmpty() { BSettings settings = gazelleSettings(); return settings != null && settings.deleteQsWhenEmpty; } public Set activateOnlyTheseIDs() { if (empty(activateOnlyTheseIDs)) return null; return mapToLinkedHashSet(__66 -> parseLong(__66), tok_integers(activateOnlyTheseIDs)); } public Set dontActivateTheseIDs() { if (empty(dontActivateTheseIDs)) return null; return mapToLinkedHashSet(__67 -> parseLong(__67), tok_integers(dontActivateTheseIDs)); } public GazelleBEA beaMod() { return this; } public Object beaCall(BEAObject c, String method, Object... args) { return cCall(c, method, args); } public Object beaCallOpt(BEAObject c, String method, Object... args) { return cCallOpt(c, method, args); } public Object beaPcall(BEAObject c, String method, Object... args) { return cPcall(c, method, args); } public Object beaCall(Concept.Ref ref, String method, Object... args) { return cCall(ref, method, args); } public Object beaPcall(Concept.Ref ref, String method, Object... args) { return cPcall(ref, method, args); } public Object beaCall(long id, String method, Object... args) { return cCall(beaMod().beaGet(id), method, args); } public Object beaPcall(long id, String method, Object... args) { return cPcall(beaMod().beaGet(id), method, args); } public Object beaCallOpt(long id, String method, Object... args) { return cCallOpt(beaMod().beaGet(id), method, args); } public
A beaGet(Class c, String key, GazelleBEA.Req req) { return optCast(c, beaGet(key, req)); } public String beaHTMLURL(BEAObject o) { return o == null ? null : beaMod().baseLink + "/beaHTML/" + o.id; } public String beaHTML(Object o) { if (o instanceof BEAObject) return beaHTML((BEAObject) o); return htmlEncode2(o); } public BEAObject beaGetOpt(long id) { return getConceptOpt(BEAObject.class, id); } static public Collection beaDeleteType(String type) { return deleteConceptsWhereIC(BEAObject.class, "type", type); } public int beaCount(String type) { return countConceptsWhereCI(BEAObject.class, "type", type); } public BEAObject beaNew(Object... __) { __ = prependParamIfOddCount("type", __); return cnew(BEAObject.class, __); } public String getGlobalCode(List tok) { Var global = new Var<>(""); int idx; while ((idx = jfind_any(tok, "global {", "exports {")) >= 0) { int j = findEndOfBracketPart(tok, idx + 2); global.set(joinSubList(tok, idx + 3, j - 1)); clearTokens(tok, idx, j + 1); } return global.get(); } public String getGlobalCode(BEAObject o) { return getGlobalCode(javaTok(codeForObject(o))); } public AutoCloseable beaEnter(BEAObject o) { if (o == null) return null; return combineAutoCloseables(tempSetTL(beaThreadOwner, o), enter()); } public JComponent visualize() { JComponent _c = super.visualize(); addControls(); return _c; } public void addControls() { } transient public IF1 beaObjectToString_long; public String beaObjectToString_long(BEAObject o) { return beaObjectToString_long != null ? beaObjectToString_long.get(o) : beaObjectToString_long_base(o); } final public String beaObjectToString_long_fallback(IF1 _f, BEAObject o) { return _f != null ? _f.get(o) : beaObjectToString_long_base(o); } public String beaObjectToString_long_base(BEAObject o) { return beaObjectToString_long_static(o); } static public String beaObjectToString_long_static(BEAObject o) { if (o == null) return "null"; String type = strOrNull(cget("type", o)); String s = or2(type, "(no type)"); s += " " + o.id; s += appendBracketed(strOrNull(getOpt("label", o))); if (eqic(type, "Match")) s += " " + getOpt("mapping", o); boolean enabled = eq(true, getOpt("enabled", o)); if (enabled) s += " [enabled]"; String purpose = getStringOpt("purpose", o); if (nempty(purpose)) s += " " + quote(purpose); String text = or2(o.text(), getStringOpt("name", o), getStringOpt("internalCode", o)); if (text != null) s += " " + quote(text); Object fc = getOpt("functionCalled", o); if (fc != null) s += " " + fc; Object result = getOpt("result", o); if (result != null) s += " = " + shorten_str(result); return s; } public boolean useErrorHandling() { return false; } public User findOrCreateUserForLogin(String name, String pw) { { User u = super.findOrCreateUserForLogin(name, pw); if (u != null) return u; } Object u = vmBus_query("lookupGazelleUser", name, pw, passwordSalt()); if (u == null) return null; String contact = getString("contact", u); var passwordMD5 = new SecretValue(hashPW(pw)); if (passwordMD5 == null) return null; boolean isMaster = getBoolOpt("isMaster", u); return cnew(User.class, "name", name, "contact", contact, "passwordMD5", passwordMD5, "isMaster", isMaster, "copiedFromMainDB", true); } public String conceptLink(Concept c) { if (c instanceof BEAObject) return beaShortURL((BEAObject) c); return super.conceptLink(c); } public String conceptLink_long(Concept c) { return super.conceptLink(c); } public Req newReq() { return new Req(); } public class Req extends DynGazelleRocks.Req { public Set makers = syncCompactSet(); public void add(Object html) { framer.add(html); } public boolean debug() { return eq(get("debug"), "1"); } } transient public IF0 serveHomePage; @Override public Object serveHomePage() { return serveHomePage != null ? serveHomePage.get() : serveHomePage_base(); } final public Object serveHomePage_fallback(IF0 _f) { return _f != null ? _f.get() : serveHomePage_base(); } public Object serveHomePage_base() { return super.serveHomePage(); } public Req currentReq() { return (Req) super.currentReq(); } public void cleanReifiedWebSockets() { for (BWebSocket o : instancesOf(BWebSocket.class, cloneList(beaList("Live WebSocket")))) if (o.webSocket() == null) retireReifiedWebSocket(o); } public Class defaultCRUDClass() { return BEAObject.class; } public Object serveFavIcon() { Object response = super.serveFavIcon(); print("serveFavIcon: " + response); return response; } public boolean alwaysShowLogInLink() { return true; } public String logInLink() { return baseLink + "/login"; } public String defaultRedirectAfterLogin() { return baseLink + "/crud/BEAObject"; } @Override public void fillReqAuthFromCookie(DynGazelleRocks.Req req, String cookie, AuthedDialogID auth) { super.fillReqAuthFromCookie(req, cookie, auth); if (makeAnonymousUsers && auth != null && auth.user.get() == null) { String pw = aGlobalID(); User userObj = cnew(User.class, "passwordMD5", new SecretValue(hashPW(pw)), "password", new SecretValue(pw)); cset(userObj, "name", "guest" + userObj.id); cset(auth, "user", userObj); vmBus_send("userCreated", userObj); } } public boolean userHasData(User user) { Collection refs = allBackRefs(user); refs = withoutInstancesOf(AuthedDialogID.class, refs); refs = withoutInstancesOf(BWebSocket.class, refs); pnl("USERDATA", refs); return nempty(refs); } public boolean anonymousUserWarning(Req req, HTMLFramer1 framer) { User user = user(req); if (print("confirm", empty(req.get("confirmLogout"))) && print("isAnonymousUser", isAnonymousUser(user)) && print("userHasData", userHasData(user))) { framer.add(h2("Anonymous User Warning")); framer.add(p("You are currently logged in as anonymous user " + b(htmlEncode2(user)) + " and seem to have some data stored on the server. Do you want to download your login data before logging out?")); framer.add(p(joinWithSpace(hlinkButton(baseLink + "/myLoginData", "Download Login Data"), hlinkButton(baseLink + req.uri + "?logout=1&confirmLogout=1", "Just log me out")))); return true; } return false; } @Override public Object handleLogout(DynGazelleRocks.Req req) { print("handleLogout " + user(((Req) req))); if (anonymousUserWarning(((Req) req), framer(((Req) req)))) return completeFrame(((Req) req)); print("Actual logout"); return super.handleLogout(((Req) req)); } public boolean isAnonymousUser(User user) { return cget("password", user) != null; } public Object serveMyLoginData(Req req) { User user = user(req); SecretValue pw = (SecretValue) (cget("password", user)); boolean isAnon = pw != null; if (!isAnon) { req.framer.add(p("You are not an anonymous user. Only anonymous users can download their login data.")); return beaMod().completeFrame(req); } if (eq(req.uri(), "/myLoginData/download")) { String host = mapGet(req.headers(), "host"); String text = jsonEncode_breakAtLevel1(litorderedmap("site", host, "user", user.name, "password", pw.get(), "date", dateWithSecondsUTC(), "ip", getClientIPFromHeaders(req.headers()))); String filename = "my-login-at-" + host + ".txt"; return subBot_noCacheHeaders(addFilenameHeader(filename, subBot_serveWithContentType(text, binaryMimeType()))); } req.framer.add(hcenter(linesLL(h1(req.framer.htmlTitle("Download Login Data")), p("You are currently logged in as an anonymous user and are able to upload data to the site. In order to access, modify or delete this data later, you should definitely download your login data now. Otherwise you risk losing this data and/or being unable to delete it."), p(hbuttonLink(baseLink + "/myLoginData/download", "Download my login data"))))); return completeFrame(req); } public HTMLFramer1 framer(Req req) { if (req.framer == null) makeFramer(req); return req.framer; } public boolean userCanSeeObject(User user, Object o) { if (user == null) return false; if (user.isMaster) return true; if (o instanceof Concept) { if (cget("createdBy", (Concept) o) == user) return true; if (isTrueOpt("isPublic", (Concept) o)) return true; } return false; } @Override public File uploadsBaseDir() { return programFile("uploads"); } } static public class BEAObject extends ConceptWithGlobalID { public UserPost mirrorPost() { return (UserPost) cget("mirrorPost", this); } public void change() { super.change(); var mod = beaMod(); if (mod != null) { mod.rstUpdateBEAMirrors.add(this); if (mod.autoActivateDynamicObjects) mod.rstAutoActivateDynamicObjects.add(this); } } public void delete() { cdelete(mirrorPost()); pcallOpt(this, "_deactivate"); super.delete(); } public void updateMirrorPost() { GazelleBEA mod = beaMod(); if (isDeleted() || !mod.mirrorBEAObjects) return; if (mirrorPost() == null) cset(this, "mirrorPost", cnew(UserPost.class, "type", "BEA Object", "creator", mod.internalUser(), "botInfo", "BEA Mirror Bot")); String text = structureString(); cset(mirrorPost(), "title", str(this), "text", text); } public String structureString() { String text = "Error"; try { structure_Data data = new structure_Data() { public structure_ClassInfo newClass(Class c) { structure_ClassInfo info = super.newClass(c); if (c == Concept.Ref.class) { info.special = true; info.serializeObject = o -> { Concept cc = (Concept) (deref((Concept.Ref) o)); if (cc != null) append("CRef(id=" + cc.id + ", c=" + quote(dynShortClassName(cc)) + ")", 6); else append("CRef", 1); }; } return info; } public void setFields(structure_ClassInfo info, List fields) { if (isSubclassOf(info.c, BEAObject.class)) { removeAll(fields, getField(BEAObject.class, "refs"), getField(BEAObject.class, "backRefs")); } super.setFields(info, fields); } }; String struct = structure(this, data); struct = structure_convertTokenMarkersToExplicit(struct); struct = dropLoadableUtilsPackageFromStruct(struct); text = indentStructureString_firstLevels(1, struct); } catch (Throwable __e) { _handleException(__e); } return text; } public String toString() { return shorten(toString_long()); } public String toString_long() { var mod = beaMod(); if (mod != null) return mod.beaObjectToString_long(this); return "[NO MODULE] " + mod.beaObjectToString_long_static(this); } public boolean _isAlive() { return !_conceptsDefunctOrUnregistered(); } public boolean hasCustomCode() { return cget("meta_code", this) != null || cget("meta_prototype", this) != null; } public boolean customCodeLoaded() { String className = getStringOpt("meta_javaClass", this); return className != null && eq(replace(className(this), "$", "."), className); } public String type() { return getStringOpt("type", this); } public boolean typeIs(String type) { return eqic(type(), type); } public boolean typeIsAny(String... types) { return typeIsOneOf(types); } public boolean typeIsOneOf(String... types) { return eqicOneOf(type(), types); } public String text() { return getStringOpt("text", this); } public BEAObject input() { return (BEAObject) cget("input", this); } public BEAObject isRewriteOf() { return (BEAObject) cget("isRewriteOf", this); } public String inputText() { BEAObject input = or(input(), isRewriteOf()); return input == null ? null : input.text(); } public Map mapping() { return keysAndValuesToString(cgetOpt(Map.class, this, "mapping")); } public List directCmds() { return ll(); } public Collection allObjects() { return list(_concepts, BEAObject.class); } public String baseLink() { return beaMod().baseLink; } public BEAObject beaGet(String field, BEAObject o) { return beaMod().beaGet(field, o); } public BEAObject beaGet(long id) { return beaMod().beaGet(id); } public BEAObject beaGet(String field) { return beaMod().beaGet(field, this); } public Object beaCall(BEAObject c, String method, Object... args) { return cCall(c, method, args); } public Object beaCallOpt(BEAObject c, String method, Object... args) { return cCallOpt(c, method, args); } public Object beaPcall(BEAObject c, String method, Object... args) { return cPcall(c, method, args); } public Object beaCall(Concept.Ref ref, String method, Object... args) { return cCall(ref, method, args); } public Object beaPcall(Concept.Ref ref, String method, Object... args) { return cPcall(ref, method, args); } public Object beaCall(long id, String method, Object... args) { return cCall(beaMod().beaGet(id), method, args); } public Object beaPcall(long id, String method, Object... args) { return cPcall(beaMod().beaGet(id), method, args); } public Object beaCallOpt(long id, String method, Object... args) { return cCallOpt(beaMod().beaGet(id), method, args); } public A beaGet(Class c, String key, GazelleBEA.Req req) { return optCast(c, beaGet(key, req)); } public String beaHTMLURL(BEAObject o) { return o == null ? null : beaMod().baseLink + "/beaHTML/" + o.id; } public String beaHTML(Object o) { if (o instanceof BEAObject) return beaHTML((BEAObject) o); return htmlEncode2(o); } public BEAObject beaGetOpt(long id) { return getConceptOpt(BEAObject.class, id); } static public Collection beaDeleteType(String type) { return deleteConceptsWhereIC(BEAObject.class, "type", type); } public int beaCount(String type) { return countConceptsWhereCI(BEAObject.class, "type", type); } public BEAObject beaNew(Object... __) { __ = prependParamIfOddCount("type", __); return cnew(BEAObject.class, __); } public gazelle.main.GazelleBEA beaMod() { return _concepts == null ? main.beaMod() : (GazelleBEA) _concepts.miscMapGet(DynNewBot2.class); } public Collection beaAll() { return beaList(); } public Collection beaList() { return beaMod().beaList(); } public Collection beaListAny(String... types) { return beaMod().beaListAny(types); } public Collection beaList(String type, Object... params) { return beaMod().beaList(type, params); } public String beaHTML(BEAObject o) { return beaMod().beaHTML(o); } public String beaURL(BEAObject o) { return beaShortURL(o); } public String beaShortURL(BEAObject o) { return beaMod().beaURL(o); } public String fixHtml(String html) { return fixHTML(html); } public String htmlFixer(String html) { return fixHTML(html); } public String htmlFix(String html) { return fixHTML(html); } public String fixHTML(String html) { return beaMod().callHTMLFixer(this, html); } public boolean createdByMasterUser(BEAObject o) { return beaMod().createdByMasterUser(o); } public String myUri() { return myURI(); } public String myURI() { return beaMod().baseLink + "/beaHTML/" + id; } public void massageItemMapForList(Map map) { } public BEAObject beaGet(String key, GazelleBEA.Req req) { return beaMod().beaGet(key, req); } public BEAObject me() { return this; } public Object completeFrame(GazelleBEA.Req req) { return beaMod().completeFrame(req); } } static public class BEARegExp extends BEAObject { static final public String _fieldOrder = "text caseInsensitive"; public String text; public boolean caseInsensitive = true; public boolean valid() { return nempty(text); } public java.util.regex.Pattern compile() { return compileRegexpPossiblyIC_unicodeCase(text, caseInsensitive); } } static public class BEARegExpReplacement extends BEARegExp { public String replacement; public String apply(String text) { try { return regexpReplace_directWithRefs(compile().matcher(text), unnull(replacement)); } catch (Throwable e) { throw fail(format_quoted("Was searching * in * ", this.text, text)); } } public List directCmds() { return listPlus(super.directCmds(), !valid() ? "Note: not valid" : targetBlank(beaMod().queryLink("Apply regular expression replacement to all inputs", str(id)), "Apply to all inputs")); } } static public class BEAPatternList extends BEAObject { public RefL patterns = new RefL(); public String toString() { return super.toString() + ": " + nPatterns(patterns); } } static public GazelleBEA beaMod() { return (GazelleBEA) dm_current_mandatory(); } static public JavaXClassLoader hotwire_makeClassLoader(List files) { ClassLoader cl = myClassLoader(); return new JavaXClassLoaderWithParent2(null, files, cl, ll()); } static public class BWebSocket extends BEAObject { static final public String _fieldOrder = "webSocketInfo screenShot onScreenShotChanged"; transient public GazelleBEA.WebSocketInfo webSocketInfo; transient public WithTimestamp screenShot; transient public Set onScreenShotChanged = syncLinkedHashSet(); public Object webSocket() { return getWeakRef(webSocketInfo == null ? null : webSocketInfo.webSocket); } public void sendJavaScript(String js) { if (empty(js)) return; Object ws = webSocket(); call(ws, "send", jsonEncode(litorderedmap("eval", js))); } public void setScreenShot(WithTimestamp screenShot) { this.screenShot = screenShot; vmBus_send("screenShotChanged", this, screenShot); print("Listeners: " + cloneList(onScreenShotChanged)); pcallFAll(onScreenShotChanged); } } static public class BWithResources extends BEAObject { static final public String _fieldOrder = "resources q log activated meta_shouldActivate"; transient public CloseablesHolder resources = new CloseablesHolder(); transient public Q q = startQ(); public List> log; transient public boolean activated = false; public boolean meta_shouldActivate = true; public AutoCloseable enter() { return beaMod().beaEnter(this); } public void _handleException(Throwable e) { beaPrint(getStackTrace(e)); } public boolean _active() { return activated; } final public void _activate() { addToQ(new Runnable() { public void run() { try { if (activated) return; activated = true; _activateImpl(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (activated) return;\r\n activated = true;\r\n _activateImpl();"; } }); } public void _activateImpl() { } public void _deactivate() { addToQ(new Runnable() { public void run() { try { _deactivateImpl(); resources.close(); activated = false; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "_deactivateImpl();\r\n resources.close();\r\n activated = false;"; } }); } public void _deactivateImpl() { } public void delete() { _deactivate(); waitForQToEmpty(q()); super.delete(); } public void addResource(AutoCloseable r) { resources.add(r); } public void clearBEAPrintLog() { inQ(new Runnable() { public void run() { try { log = null; change(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "log = null;\r\n change();"; } }); } public int maxBeaPrintLength() { return 10000; } public void beaPrint(Object o) { beaPrint("", o); } public void beaPrint(String s, Object o) { AutoCloseable __35 = enter(); try { inQ(new Runnable() { public void run() { try { String text = combinePrintParameters(s, o); text = shorten(maxBeaPrintLength(), text); if (log == null) log = new ArrayList(); synchronized (log) { truncateListFromStart(log, maxLogSize() - 1); printIndent(id + "|", text); log.add(new WithTimestamp(text)); } change(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "String text = combinePrintParameters(s, o);\r\n text = shorten(maxBeaPrint..."; } }); } finally { _close(__35); } } public int maxLogSize() { return getIntOpt("maxLogSize", this, 10); } public BWithResources me() { return this; } public void inQ(Runnable r) { if (r == null) return; if (isInQ(q)) r.run(); else addToQ(r); } public void addToQ(Runnable r) { if (r == null) return; getQ().add(new Runnable() { public void run() { try { AutoCloseable __36 = enter(); try { try { r.run(); } catch (Throwable e) { beaPrint(getStackTrace(e)); } } finally { _close(__36); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "temp enter(); \r\n try {\r\n r.run();\r\n } catch (Throwable e) {\r..."; } }); } public Q q() { return getQ(); } public Q getQ() { return q; } public void useWebSocket(GazelleBEA.WebSocketInfo ws) { } } static public class BSettings extends Concept { public boolean deleteQsWhenEmpty = false; } static public class BHTTPRequest extends BEAObject { transient public GazelleBEA.Req req; } static public AutoCloseable tempInterceptPrintIfNotIntercepted(F1 f) { return print_byThread().get() == null ? tempInterceptPrint(f) : null; } static volatile public Concepts mainConcepts; static public Concepts db_mainConcepts() { if (mainConcepts == null) mainConcepts = newConceptsWithClassFinder(getDBProgramID()); return mainConcepts; } static public void cleanMeUp_concepts() { if (db_mainConcepts() != null) db_mainConcepts().cleanMeUp(); } static public String unnullForIteration(String s) { return s == null ? "" : s; } static public Collection unnullForIteration(Collection l) { return l == null ? immutableEmptyList() : l; } static public List unnullForIteration(List l) { return l == null ? immutableEmptyList() : l; } static public int[] unnullForIteration(int[] l) { return l == null ? emptyIntArray() : l; } static public char[] unnullForIteration(char[] l) { return l == null ? emptyCharArray() : l; } static public double[] unnullForIteration(double[] l) { return l == null ? emptyDoubleArray() : l; } static public short[] unnullForIteration(short[] l) { return l == null ? emptyShortArray() : l; } static public Map unnullForIteration(Map l) { return l == null ? immutableEmptyMap() : l; } static public Iterable unnullForIteration(Iterable i) { return i == null ? immutableEmptyList() : i; } static public A[] unnullForIteration(A[] a) { return a == null ? (A[]) emptyObjectArray() : a; } static public BitSet unnullForIteration(BitSet b) { return b == null ? new BitSet() : b; } static public Pt unnullForIteration(Pt p) { return p == null ? new Pt() : p; } static public Symbol unnullForIteration(Symbol s) { return s == null ? emptySymbol() : s; } static public Pair unnullForIteration(Pair p) { return p != null ? p : new Pair(null, null); } static public long unnullForIteration(Long l) { return l == null ? 0L : l; } static public Map flexMatchAngleBracketVarsIC_honorPunctuation_noBrackets_first(String pat, String input) { return grabValueAndCancel(new VF1>>() { public void get(VF1> onMatch) { try { flexMatchAngleBracketVarsIC_honorPunctuation_noBrackets_iterate(pat, input, onMatch); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "flexMatchAngleBracketVarsIC_honorPunctuation_noBrackets_iterate(pat, input, o..."; } }); } static public String appendSquareBracketed(String b) { return empty(b) ? "" : "" + " [" + b + "]"; } static public String appendSquareBracketed(String a, String b) { return empty(b) ? a : a + appendSquareBracketed(b); } static public String jquery_submitFormOnCtrlEnter() { return "if ((event.keyCode == 10 || event.keyCode == 13) && event.ctrlKey) $(this).closest('form').submit();"; } static public String hjs_handleScrollToAnchor() { return hjs("\r\n $(window).on(\"load\", function() {\r\n var anchor = window.location.hash.substr(1);\r\n var match = anchor.match(/^scrollTo(\\d+)$/);\r\n if (match) {\r\n var y = parseInt(match[1]);\r\n console.log(\"Scrolling to \" + y);\r\n window.scrollTo(0, y);\r\n }\r\n });\r\n "); } static public boolean userAgentIsBot(String userAgent) { return cic(userAgent, "bot") || cic(userAgent, "crawler"); } static public String js_redirectAutoScroll2() { return "$(\"input[name=redirect], input[name=redirectAfterSave]\").val(window.location.toString().replace(/(#.+?)$/, \"\") + (window.scrollY == 0 ? \"\" : \"#scrollTo\" + Math.round(window.scrollY)));"; } static public String hSingleRowTable_withSpacing(Object... elements) { return hSingleRowTable_withSpacing(10, elements); } static public String hSingleRowTable_withSpacing(int spacing, Object... elements) { return tag("table", tr(join("", map(new F1() { public String get(Object x) { try { return td_top(x); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "td_top(x)"; } }, nonNulls(elements))))); } static public String targetBlank_noFollow(String link, Object contents, Object... params) { return tag("a", contents, paramsPlus(params, "href", link, "target", "_blank", "rel", "nofollow")); } static public String tok_dropCurlyBrackets(String s) { return join(tok_dropCurlyBrackets(javaTok(s))); } static public List tok_dropCurlyBrackets(List tok) { for (int i = 1; i < l(tok); i += 2) { while (eqGetOneOf(tok, i, "{", "}")) { tokAppend(tok, i - 1, tok.get(i + 1)); removeSubList(tok, i, i + 2); } } return tok; } static public Map mapToKey(Iterable l, IF1 f) { return mapToKeys(l, f); } static public Map mapToKey(IF1 f, Iterable l) { return mapToKeys(f, l); } static public List wideningListCast(Class c, List l) { return (List) l; } static public Collection wideningListCast(Class c, Collection l) { return (Collection) l; } static public String _className(Object o) { return className(o); } static public String htmlDecode_dropTags(String html) { return htmldecode_dropAllTags(html); } static public MultiSet distinctCIFieldValuesOfConcepts_multiSet(Class c, String field) { return distinctCIFieldValuesOfConcepts_multiSet(db_mainConcepts(), c, field); } static public MultiSet distinctCIFieldValuesOfConcepts_multiSet(Concepts concepts, Class c, String field) { IFieldIndex index = concepts.getCIFieldIndex(c, field); if (index != null) return index.allValues_multiSet(); MultiSet set = ciMultiSet(); for (A x : concepts.list(c)) set.add((String) getOpt(x, field)); return set; } static public TreeMap toCIMap(Map map) { return asCIMap(map); } static public B getAny(Map map, A... keys) { for (A a : unnullForIteration(keys)) { B b = map.get(a); if (b != null) return b; } return null; } static public String shortClassName_dropNumberPrefix(Object o) { return dropNumberPrefix(shortClassName(o)); } static public Set collectAngleBracketVars(String s) { return collectAngleBracketVars(javaTokWithAngleBrackets(s)); } static public Set collectAngleBracketVars(List tok) { Set set = ciSet(); for (int i = 0; i < l(tok); i++) if (isAngleBracketVar(tok.get(i))) set.add(deAngleBracket(tok.get(i))); return set; } static public Map mapToValues_ciMap(Iterable l, Object f) { return mapToValues_ciMap(f, l); } static public Map mapToValues_ciMap(Object f, Iterable l) { Map map = ciMap(); if (l != null) for (String o : l) map.put(o, callF(f, o)); return map; } static public Map mapToValues_ciMap(Iterable l, IF1 f) { return mapToValues_ciMap(f, l); } static public List regexpFindRangesIC(String pat, String s) { Matcher m = regexpMatcherIC(pat, s); List l = new ArrayList(); while (m.find()) l.add(new IntRange(m.start(), m.end())); return l; } static public A csetAndReturn(A c, Object... values) { cset(c, values); return c; } static public Object getStandardFunctionHolder(String functionName) { for (Object o : mcAndUtils()) if (hasMethodNamed(o, functionName)) return o; return loadFunction_cached(functionName); } static public Map putKeysFirst(Map map, Object... keys) { Map m2 = litorderedmap(); Map remaining = cloneMap(map); for (Object key : keys) { if (remaining.containsKey(key)) { m2.put(key, remaining.get(key)); remaining.remove(key); } } m2.putAll(remaining); return m2; } static public Map putKeysFirst(List keys, Map map) { return putKeysFirst(map, toObjectArray(keys)); } static public List concatMap_lists(Object f, Iterable l) { return concatMap(f, l); } static public List concatMap_lists(Iterable l, Object f) { return concatMap(l, f); } static public List concatMap_lists(Object f, Object[] l) { return concatMap(f, l); } static public List concatMap_lists(Object[] l, Object f) { return concatMap(l, f); } static public > List concatMap_lists(Iterable l, IF1 f) { return concatMap(l, f); } static public > List concatMap_lists(IF1 f, Iterable l) { return concatMap(f, l); } static public List findBackRefsWithField(Concept c, Class type, String field) { return filter(findBackRefs(c, type), o -> cget(o, field) == c); } static public List findBackRefsWithField(Class type, Concept c, String field) { return findBackRefsWithField(c, type, field); } static public List findBackRefsWithField(Class type, String field, Concept c) { return findBackRefsWithField(c, type, field); } static public int countWhereCI(Collection c, Object... data) { int n = 0; if (c != null) for (Object x : c) if (checkFieldsIC(x, data)) ++n; return n; } static public ReliableSingleThread_Multi dm_rstMulti(IVF1 processObject) { return dm_rstMulti(dm_current_mandatory(), processObject); } static public ReliableSingleThread_Multi dm_rstMulti(DynModule mod, IVF1 processObject) { return dm_rstMulti(mod, new ReliableSingleThread_Multi<>(processObject)); } static public ReliableSingleThread_Multi dm_rstMulti(int delay, IVF1 processObject) { return dm_rstMulti(dm_current_mandatory(), delay, processObject); } static public ReliableSingleThread_Multi dm_rstMulti(DynModule mod, int delay, IVF1 processObject) { return dm_rstMulti(mod, new ReliableSingleThread_Multi<>(delay, processObject)); } static public ReliableSingleThread_Multi dm_rstMulti(DynModule mod, ReliableSingleThread_Multi rst) { rst.setEnter(dm_rEnter(mod)); return rst; } static public Set syncLinkedHashSet() { return synchroLinkedHashSet(); } static public void quickImportMainConcepts(Object oldConcepts) { quickImportMainConcepts(oldConcepts, null); } static public void quickImportMainConcepts(Object oldConcepts, IF0 enabled) { if (!shortNameIs(oldConcepts, "Concepts")) return; Concepts concepts = db_mainConcepts(); concepts.miscMapPut("dbGrabber", new F0() { public Boolean get() { try { concepts.miscMapRemove("dbGrabber"); try { if (isFalse(callF(enabled))) return false; concepts.idCounter = getLong("idCounter", oldConcepts); printVars_str("idCounter", concepts.idCounter); Map oldMap = (Map) (_get("concepts", oldConcepts)); printVars_str("oldMap", oldMap); RealmCopy rc = new RealmCopy(); rc.nonTransientOnly = true; rc.fullCopy = true; rc.overrideCollectionOrMapCopy = o -> { rc.classMap.put(_getClass(o), _getClass(o)); return false; }; { AutoCloseable __1 = tempSetTL(dynamicObjectIsLoading_threadLocal(), true); try { concepts.concepts.putAll((Map) rc.copy(oldMap)); } finally { _close(__1); } } pnl("quickImportDB", mapSortedByFunctionOnKey(__68 -> str(__68), rc.classMap)); concepts.assignConceptsToUs(); print(nConcepts(countConcepts()) + " quick-imported"); return true; } catch (Throwable __e) { _handleException(__e); } return false; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "concepts.miscMapRemove(\"dbGrabber\");\r\n pcall {\r\n if (isFalse(callF(en..."; } }); } static public String vmClassNameToSubPath(String className) { return empty(className) ? null : replace(className, ".", "/") + ".class"; } static public boolean classLoaderContainsByteCodePath(ClassLoader cl, File byteCodePath) { return contains(filesFromClassLoader_new(cl), byteCodePath); } static public boolean fileExistsInInDirOrZip(File location, String subPath) { try { if (location.isDirectory()) { File f = new File(location, subPath); return f.exists(); } else if (location.isFile()) { return zipContainsFile(location, subPath); } return false; } catch (Exception __e) { throw rethrow(__e); } } static public void printVars(Object... params) { printVars_str(params); } static public boolean addByteCodePathToClassLoader(ClassLoader cl, File bytecode) { try { print("Adding byte code path " + bytecode + " to " + cl); return isTrue(call(cl, "addFile", bytecode)); } catch (Exception __e) { throw rethrow(__e); } } static public void db() { conceptsAndBot(); } static public void db(Integer autoSaveInterval) { conceptsAndBot(autoSaveInterval); } static public ClassLoader dm_moduleClassLoader() { return dm_moduleClassLoader(dm_current_mandatory_generic()); } static public ClassLoader dm_moduleClassLoader(Object mod) { return getClassLoader(dm_getModule(mod)); } static public void setFieldToIF1Proxy(Object o, String field, IF1 target) { setFieldToSingleMethodProxy(o, field, target, "get"); } static public Object _defaultClassFinder_value = defaultDefaultClassFinder(); static public Object _defaultClassFinder() { return _defaultClassFinder_value; } static public String mcName() { return mc().getName(); } static public String dotToDollar(String s) { return replace(s, ".", "$"); } static public Class classLoader_loadClass(Object realm, String name) { try { ClassLoader cl = classLoader(realm); return cl == null ? null : cl.loadClass(name); } catch (Exception __e) { throw rethrow(__e); } } static public void fixConceptIDs() { fixConceptIDs(db_mainConcepts()); } static public void fixConceptIDs(Concepts cc) { if (cc == null) return; for (Map.Entry __0 : _entrySet(cc.concepts)) { long id = __0.getKey(); Concept c = __0.getValue(); if (id == 0) { print("0 concept key: " + c); continue; } if (c.id == id) continue; print("Fixing concept ID: " + c.id + " => " + id + ": " + c); c.id = id; } } static public PrintWriter filePrintWriter_append(File f) { return printWriter(bufferedFileOutputStream_append(f)); } static public void printAllConceptChanges() { printAllConceptChanges(db_mainConcepts()); } static public void printAllConceptChanges(Concepts cc) { cc.addConceptIndex(new IConceptIndex() { public void update(Concept c) { print("Updated: " + c); } public void remove(Concept c) { print("Removed: " + c); } }); cc.onAllChanged.add(new Runnable() { public void run() { try { print("General concepts change"); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "print(\"General concepts change\");"; } }); } static public Map parseEqualsProperties(String text) { return parseEqualsProperties(text, new HashMap()); } static public Map parseEqualsProperties(String text, Map map) { for (String s : tlft(text)) { int i = indexOf(s, '='); if (i > 0) map.put(trimSubstring(s, 0, i), trimSubstring(s, i + 1)); } return map; } static public String joinNemptiesWithEmptyLines(List l) { return joinWithEmptyLines(nempties(l)); } static public String joinNemptiesWithEmptyLines(String... l) { return joinNemptiesWithEmptyLines(asList(l)); } static public String actualMCDollar() { return actualMC().getName() + "$"; } static public Object defaultDefaultClassFinder() { return new F1() { public Class get(String name) { Class c = get2(name); return c; } public Class get2(String name) { if (eq(name, "
")) return mc(); { Class c = findClass_fullName(name); if (c != null) return c; } if (startsWithAny(name, "loadableUtils.utils$", "main$", mcDollar())) for (String pkg : ll("loadableUtils.utils$", mcDollar())) { String newName = pkg + afterDollar(name); { Class c = findClass_fullName(newName); if (c != null) return c; } } return null; } }; } static public String mcDollar() { return mcName() + "$"; } static public void assertSameVerbose(Object x, Object y) { assertSameVerbose((String) null, x, y); } static public void assertSameVerbose(String msg, Object x, Object y) { if (x != y) throw fail((msg != null ? msg + ": " : "") + sfu(y) + " !== " + sfu(x)); else print("OK: " + sfu(x)); } static public String renderConceptClassesWithCount(Concepts cc) { MultiSet ms = new MultiSet(); for (Concept c : values(cc.concepts)) ms.add(dynamicShortName(c)); return renderMultiSet(ms); } static public void dm_onSnippetTranspiled(VF1 r) { dm_requireSnippetUpdatesModule(); final DynModule m = dm_current_mandatory(); dm_vmBus_onMessage("snippetUpdate", new VF1() { public void get(final List l) { try { m.q().add(new Runnable() { public void run() { try { String uri = getString(l, 1); Matches m = new Matches(); if (swic(uri, "/transpileOK/", m)) { String snippetID = fsI(firstIntAsString(m.rest())); callF(r, snippetID); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "String uri = getString(l, 1);\r\n new Matches m;\r\n if (swic(uri, \"/transp..."; } }); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "switch to m.q();\r\n S uri = getString(l, 1);\r\n new Matches m;\r\n if (s..."; } }); } static public void dm_onSnippetTranspiled(String snippetID, Runnable r) { if (empty(snippetID) || r == null) return; dm_onSnippetTranspiled(new VF1() { public void get(String snippetID2) { try { if (sameSnippetID(snippetID, snippetID2)) r.run(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (sameSnippetID(snippetID, snippetID2))\r\n r.run();"; } }); } static public void dm_reloadModule(Object module) { dm_reloadModuleInBackground(module); } static public void dm_reloadModule() { dm_reloadModuleInBackground(); } static public AutoCloseable dm_vmBus_answerToMessage(String msg, final IF0 f) { final DynModule m = dm_current_mandatory(); return dm_ownResource(vmBus_addListener_basic(msg, new F2() { public Object get(String _msg, Object arg) { try { AutoCloseable __1 = m.enter(); try { return callF(f); } finally { _close(__1); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "temp m.enter();\r\n ret callF(f);"; } })); } static public AutoCloseable dm_vmBus_answerToMessage(String msg, final F0 f) { final DynModule m = dm_current_mandatory(); return dm_ownResource(vmBus_addListener_basic(msg, new F2() { public Object get(String _msg, Object arg) { try { AutoCloseable __2 = m.enter(); try { return callF(f); } finally { _close(__2); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "temp m.enter();\r\n ret callF(f);"; } })); } static public AutoCloseable dm_vmBus_answerToMessage(String msg, final F1 f) { final DynModule m = dm_current_mandatory(); return dm_ownResource(vmBus_addListener_basic(msg, new F2() { public Object get(String _msg, Object arg) { try { AutoCloseable __3 = m.enter(); try { return callF(f, arg); } finally { _close(__3); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "temp m.enter();\r\n ret callF(f, arg);"; } })); } static public AutoCloseable dm_vmBus_answerToMessage(String msg, F2 f) { final DynModule m = dm_current_mandatory(); return dm_ownResource(vmBus_addListener_basic(msg, new F2() { public Object get(String _msg, Object arg) { try { if (arg instanceof Object[]) { if (((Object[]) arg).length != 2) return null; AutoCloseable __4 = m.enter(); try { return callF(f, ((Object[]) arg)[0], ((Object[]) arg)[1]); } finally { _close(__4); } } return null; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (arg cast O[]) {\r\n if (arg.length != 2) null;\r\n temp m.enter();\r..."; } })); } static public AutoCloseable dm_vmBus_answerToMessage(String msg, F3 f) { final DynModule m = dm_current_mandatory(); return dm_ownResource(vmBus_addListener_basic(msg, new F2() { public Object get(String _msg, Object arg) { try { if (arg instanceof Object[]) { if (((Object[]) arg).length != 3) return null; AutoCloseable __5 = m.enter(); try { return callF(f, ((Object[]) arg)[0], ((Object[]) arg)[1], ((Object[]) arg)[2]); } finally { _close(__5); } } return null; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (arg cast O[]) {\r\n if (arg.length != 3) null;\r\n temp m.enter();\r..."; } })); } static public String nemptyLinesLL(Object... l) { return nemptyLines(ll(l)); } static public void indexConceptClass(Class c) { indexConceptClass(db_mainConcepts(), c); } static public void indexConceptClass(Concepts cc, Class c) { ensureConceptClassIsIndexed(cc, c); } static public ConceptFieldIndexCI_withTopTen indexConceptFieldCIWithTopTen(Class c, String field) { return indexConceptFieldCIWithTopTen(db_mainConcepts(), c, field); } static public ConceptFieldIndexCI_withTopTen indexConceptFieldCIWithTopTen(Concepts cc, Class c, String field) { var idx = getConceptFieldCIIndex(cc, c, field); if (idx instanceof ConceptFieldIndexCI_withTopTen) return ((ConceptFieldIndexCI_withTopTen) idx); if (idx != null) cc.removeCIFieldIndex(c, field); return new ConceptFieldIndexCI_withTopTen(cc, c, field); } static public String pre_htmlEncode(Object contents, Object... params) { return pre_htmlencode(contents, params); } static public String renderStackTrace(StackTraceElement[] st) { return stackTraceToString(st); } static public String renderStackTrace(Throwable e) { return stackTraceToString(e); } static public String renderStackTrace(String msg) { return renderStackTrace(new Throwable(msg)); } static public String hjs_selectizeClickable() { return hjs("\r\n Selectize.define('clickable', function(options) {\r\n var self = this;\r\n var setup = self.setup;\r\n this.setup = function() {\r\n setup.apply(self, arguments);\r\n \r\n var clicking = false;\r\n \r\n // Detect click on a .clickable\r\n self.$dropdown_content.on('mousedown click', function(e) {\r\n if ($(e.target).hasClass('clickable')) {\r\n if (e.type === 'mousedown') {\r\n clicking = true;\r\n self.isFocused = false; // awful hack to defuse the document mousedown listener\r\n } else {\r\n self.isFocused = true;\r\n setTimeout(function() {\r\n clicking = false; // wait until blur has been preempted\r\n });\r\n }\r\n } else { // cleanup in case user right-clicked or dragged off the element\r\n clicking = false;\r\n self.isFocused = true;\r\n }\r\n });\r\n \r\n // Intercept default handlers\r\n self.$dropdown.off('mousedown click', '[data-selectable]').on('mousedown click', '[data-selectable]', function() {\r\n if (!clicking) {\r\n return self.onOptionSelect.apply(self, arguments);\r\n }\r\n });\r\n self.$control_input.off('blur').on('blur', function() {\r\n if (!clicking) {\r\n return self.onBlur.apply(self, arguments);\r\n }\r\n });\r\n }\r\n });\r\n "); } static public boolean jMatchStart(String pat, String s) { return jMatchStart(pat, s, null); } static public boolean jMatchStart(String pat, String s, Matches matches) { if (s == null) return false; return jMatchStart(pat, javaTok(s), matches); } static public boolean jMatchStart(String pat, List toks) { return jMatchStart(pat, toks, null); } static public boolean jMatchStart(String pat, List toks, Matches matches) { List tokpat = jfind_preprocess(javaTok(pat)); if (toks.size() < tokpat.size()) return false; String[] m = match2x(tokpat, toks.subList(0, tokpat.size())); if (m == null) return false; if (matches != null) { matches.m = new String[m.length + 1]; arraycopy(m, matches.m); matches.m[m.length] = join(toks.subList(tokpat.size(), toks.size())); } return true; } static public String nItems(long n) { return n2(n, "item"); } static public String nItems(Collection l) { return nItems(l(l)); } static public String nItems(Map map) { return nItems(l(map)); } static public Throwable pcall(Runnable r) { try { r.run(); return null; } catch (Throwable e) { return e; } } static public Object pcall(Object o, String method, Object... args) { try { return call(o, method, args); } catch (Throwable __e) { return null; } } static public A pcall(IF0 f) { try { return f == null ? null : f.get(); } catch (Throwable __e) { return null; } } static public List llNempties(String... a) { ArrayList l = new ArrayList(a.length); if (a != null) for (String x : a) if (nempty(x)) l.add(x); return l; } static public boolean hasMethodNamed(Object obj, String method) { if (obj == null) return false; if (obj instanceof Class) return hasMethodNamed((Class) obj, method); return hasMethodNamed(obj.getClass(), method); } static public boolean hasMethodNamed(Class c, String method) { while (c != null) { for (Method m : c.getDeclaredMethods()) if (m.getName().equals(method)) return true; c = c.getSuperclass(); } return false; } static public List methodsSortedByNameIC(Collection l) { return sortedByCalculatedFieldIC(l, m -> m.getName()); } static public List allLiveMethodsBelowClass(Object o, Class base) { Class c = _getClass(o); boolean isInstance = c != o; List methods = new ArrayList(); while (c != null && c != base) { for (Method m : c.getDeclaredMethods()) if (isInstance != isStaticMethod(m)) methods.add(m); c = c.getSuperclass(); } return methods; } static public List objectsWhereNotIC(Collection c, Object... data) { if (c == null) return null; List l = new ArrayList(); for (A x : c) if (!checkFieldsIC(x, data)) l.add(x); return l; } static public List tok_integersAsLongs(String s) { return map(__69 -> parseLong(__69), tok_integers(s)); } static public A cgetOpt(Class type, Concept c, String field) { return optCast(type, cget(c, field)); } static public List> bitSetStreaksAndNonStreaks(BitSet bs, int n) { int start = 0; Boolean last = null; List> out = new ArrayList(); for (int i = 0; i < n; i++) { Boolean result = bs.get(i); if (last != null && neq(result, last)) { out.add(pair(intRange(start, i), last)); start = i; } last = result; } if (start < n) out.add(pair(intRange(start, n), last)); return out; } static public String aname(String anchor, Object contents, Object... params) { return tag("a", contents, concatArrays(new Object[] { "name", anchor }, params)); } static public String htmlEncode2_str(Object o) { return htmlEncode2_gen(o); } static public boolean startsWith(String a, String b) { return a != null && a.startsWith(unnull(b)); } static public boolean startsWith(String a, char c) { return nemptyString(a) && a.charAt(0) == c; } static public boolean startsWith(String a, String b, Matches m) { if (!startsWith(a, b)) return false; if (m != null) m.m = new String[] { substring(a, strL(b)) }; return true; } static public 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 public IF0 call_optional(Object o, String method, String[] arg) { return call_optional(o, method, new Object[] { arg }); } static public IF0 call_optional(Object o, String method, Object... args) { if (!canCallWithVarargs(o, method, args)) return null; return if0_const(call(o, method, args)); } static public String snippetURL(String id) { return snippetLink(id); } static public A assertNempty(A a) { return assertNempty("empty", a); } static public A assertNempty(String msg, A a) { if (empty(a)) throw fail(msg + ": " + a); return a; } static public List assertStringList(Object o) { if (!(o instanceof List)) throw fail("Not a list: " + className(o)); List l = (List) o; for (Object x : l) if (!(x instanceof String)) throw fail("Not a string: " + className(x)); return l; } static public void dm_reloadModuleIn(double seconds) { Object mod = dm_current_generic(); if (mod == null) return; print("Reloading module in " + seconds); doAfter(seconds, new Runnable() { public void run() { try { dm_reloadModule(mod); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "dm_reloadModule(mod)"; } }); } static public Timed returnTimed(IF0 f) { long time = nanos(); A a = f.get(); time = nanos() - time; return new Timed(a, nsToMS(time)); } static public Timed returnTimed(Runnable r) { return returnTimed(runnableToIF0(r)); } static public String ul_htmlEncode2(String... list) { return ul_htmlEncode(list); } static public String ul_htmlEncode2(Collection list, Object... params) { return ul_htmlEncode(list, params); } static public List allConceptShadows() { return allConceptShadows(db_mainConcepts()); } static public List allConceptShadows(Concepts cc) { if (cc == null) return null; List l = asList(cc.allConcepts()); assertTrue("Concepts are sorted by id", isSortedListByCalculatedField(__70 -> conceptID(__70), l)); return lmap(__71 -> conceptShadow(__71), l); } static public List> diffConceptShadows(List l1, List l2) { List> diffs = new ArrayList(); int i1 = 0, i2 = 0; while (i1 < l(l1) && i2 < l(l2)) { ConceptShadow s1 = l1.get(i1), s2 = l2.get(i2); long id1 = s1.id(), id2 = s2.id(); if (id1 < id2) { diffs.add(new CreatedDeletedChanged.Deleted(s1)); ++i1; } else if (id1 > id2) { diffs.add(new CreatedDeletedChanged.Created(s2)); ++i2; } else { if (!eq(s1, s2)) diffs.add(new CreatedDeletedChanged.Changed(s1, s2)); ++i1; ++i2; } } while (i1 < l(l1)) diffs.add(new CreatedDeletedChanged.Deleted(l1.get(i1++))); while (i2 < l(l2)) diffs.add(new CreatedDeletedChanged.Created(l2.get(i2++))); return diffs; } static public Object subBot_serveText(Object s) { return call(getMainBot(), "serveByteArray", toUtf8(str(s)), "text/plain; charset=utf8"); } static public String nDiffs(long n) { return n2(n, "diff"); } static public String nDiffs(Collection l) { return nDiffs(l(l)); } static public String nDiffs(Map map) { return nDiffs(l(map)); } static public List regexpExtractGroups(String pat, String s) { return regexpFirstGroups(pat, s); } static public String hijackPrintPlusResult_text(IF0 f) { Var var = new Var(); String out = hijackPrint(new Runnable() { public void run() { try { var.set(f.get()); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "var.set(f.get())"; } }); return lines_rtrim(ll(out, str(var))); } static public Object subBot_serveFileWithName(File file) { return subBot_serveFileWithName(file, file.getName()); } static public Object subBot_serveFileWithName(File file, String name) { return call(getMainBot(), "serveFileWithName", file, name); } static public Object subBot_serveFileWithName(String name, File file) { return subBot_serveFileWithName(file, name); } static public Object subBot_serveFileWithName(File file, String name, String mimeType) { return call(getMainBot(), "serveFileWithName", file, name, mimeType); } static public File transpilerErrorSourceFile() { return javaxCachesDir("error-source.java"); } static public String profileThisThreadToString(Runnable r) { return profileThisThreadToString(poorMansProfiling_defaultInterval(), r); } static public String profileThisThreadToString(int interval, Runnable r) { poorMansProfiling(interval, currentThread()); try { r.run(); } catch (Throwable __e) { _handleException(__e); } return poorMansProfiling_stopAndRenderResults(); } static public Pair profileThisThreadToString(IF0 f) { return profileThisThreadToString(poorMansProfiling_defaultInterval(), f); } static public Pair profileThisThreadToString(int interval, IF0 f) { Var var = new Var(); String profile = profileThisThreadToString(interval, new Runnable() { public void run() { try { var.set(callF(f)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "var.set(callF(f))"; } }); return pair(var.get(), profile); } static public String lastTiming_formatted() { return renderElapsedTimePleasantly(lastTiming()); } static public A inlineSwappable(String fieldName, Object host, IF0 defaultCalculation) { if (nempty(fieldName)) { IF0 replacement = (IF0) (getOpt(host, fieldName)); if (replacement != null) return replacement.get(); } return defaultCalculation.get(); } static public String getProgramURL() { return snippetLink(getProgramID()); } static public boolean containsStars(String s) { return containsStars(javaTok_cached(s)); } static public boolean containsStars(List tok) { for (int i = 1; i < l(tok); i += 2) if (eq(tok.get(i), "*")) return true; return false; } static public String unicode_thumbsUp() { return unicodeFromCodePoint(0x1F44D); } static public String unicode_thumbsDown() { return unicodeFromCodePoint(0x1F44E); } static public A unlistedCopyToClass_withConverter_pcall(Class c, Concept a) { if (a == null) return null; A b = unlisted(c); ccopyFields_withConverter_pcall(a, b); return b; } static public A replaceConceptAndUpdateRefs(Concept a, A b) { assertTrue("object to replace with must be unlisted", isUnlisted(b)); Concepts cc = a.concepts(); assertTrue("object to replace must be listed", cc != null); List refs = allBackRefObjects(a); b.id = a.id; deleteConcept(a); cc.register_phase2(b); cset(a, "meta_migratedTo", b); for (Concept.Ref ref : refs) ref.set(b); return b; } static public A stepAllAndGet(SteppableAndIF0 s) { stepAll(s); return s.get(); } static public void dm_mediumRefreshTranspiler() { dm_call(dm_moduleOfType("#1017065/Transpiler"), "mediumRefresh"); } static public void assertSame(Object a, Object b) { assertSame("", a, b); } static public void assertSame(String msg, Object a, Object b) { if (a != b) throw fail(joinNemptiesWithColon(msg, a + " != " + b + " (" + identityHash(a) + "/" + identityHash(b) + ")")); } static public int nCodeTokens(String s) { return countCodeTokens(s); } static public int nCodeTokens(List tok) { return countCodeTokens(tok); } static public int jfind_any(String src, String... patterns) { return jfind_any(javaTok(src), patterns); } static public int jfind_any(List tok, String... patterns) { int i = -1; for (String pat : patterns) i = minUnlessMinus1(i, jfind(tok, pat)); return i; } static public int jfind(String s, String in) { return jfind(javaTok(s), in); } static public int jfind(List tok, String in) { return jfind(tok, 1, in); } static public int jfind(List tok, int startIdx, String in) { return jfind(tok, startIdx, in, null); } static public int jfind(List tok, String in, Object condition) { return jfind(tok, 1, in, condition); } static public int jfind(List tok, String in, ITokCondition condition) { return jfind(tok, 1, in, condition); } static public int jfind(List tok, int startIndex, String in, ITokCondition condition) { return jfind(tok, startIndex, in, toTokCondition(condition)); } static public int jfind(List tok, int startIdx, String in, Object condition) { return jfind(tok, startIdx, javaTokForJFind_array(in), condition); } static public int jfind(List tok, List tokin) { return jfind(tok, 1, tokin); } static public int jfind(List tok, int startIdx, List tokin) { return jfind(tok, startIdx, tokin, null); } static public int jfind(List tok, int startIdx, String[] tokinC, Object condition) { return findCodeTokens(tok, startIdx, false, tokinC, condition); } static public int jfind(List tok, int startIdx, List tokin, Object condition) { return jfind(tok, startIdx, codeTokensAsStringArray(tokin), condition); } static public List jfind_preprocess(List tok) { for (String type : litlist("quoted", "id", "int")) replaceSublist(tok, ll("<", "", type, "", ">"), ll("<" + type + ">")); replaceSublist(tok, ll("\\", "", "*"), ll("\\*")); return tok; } static public long parseFirstLong_regexp(String s) { return firstLong_regexp(s); } static public List filesFromClassLoader(Class c) { return filesFromClassLoader(getClassLoader(c)); } static public List filesFromClassLoader(ClassLoader cl) { Collection l = filesFromClassLoader_new(cl); return l == null ? null : asList(l); } static public ThreadLocal transpileAndCompileForHotwiring_src = new ThreadLocal(); static public File transpileAndCompileForHotwiring(String src) { return transpileAndCompileForHotwiring(src, null); } static public File transpileAndCompileForHotwiring(String src, List libs) { transpileAndCompileForHotwiring_src.set(null); if (libs == null) libs = new ArrayList(); src = transpileRaw(src); transpileAndCompileForHotwiring_src.set(src); src = findTranslators2(src, libs); String dehlibs = join(" ", libs); File bytecode = javaCompile_overInternalBot(src, dehlibs); print("bytecode", bytecode); print("Files: " + listZip(bytecode)); return bytecode; } static public List tok_identifiers(String s) { return tok_identifiersInOrder(s); } static public List tok_identifiers(List tok) { return tok_identifiersInOrder(tok); } static public long regexpToLongIC(String regexp, String input) { return parseLong(regexpFirstGroupIC(regexp, input)); } static public Concept getConceptOrMarkMissingObject(Class requiredClass, long id) { return getConceptOrMarkMissingObject(db_mainConcepts(), requiredClass, id); } static public Concept getConceptOrMarkMissingObject(Concepts cc, Class requiredClass, long id) { if (cc == null) return null; Concept c = getConcept(cc, id); if (isInstanceOf(c, requiredClass)) return c; if (c instanceof CMissingObject) return c; if (c != null) throw fail("Can't cast: " + toStringWithClassName(c) + "/" + requiredClass); Lock __0 = dbLock(cc); lock(__0); try { if (getConcept(cc, id) == null) return cnew(cc, CMissingObject.class); return null; } finally { unlock(__0); } } static public boolean renameFileVerbose(File a, File b) { if (a == null || b == null) return false; boolean result = a.renameTo(b); print("Renaming " + f2s(a) + " to " + f2s(b) + "? " + yesNo2(result)); return result; } static public boolean renameFileVerbose(File a, String newName) { return renameFileVerbose(a, fileInSameDir(a, newName)); } static public File byteCodePathForClass(Class c) { if (c == null) return null; ClassLoader cl = getClassLoader(c); Collection files = (Collection) (getOpt(cl, "files")); if (files != null) { String name = c.getName().replace('.', '/') + ".class"; for (File location : files) if (dirOrZipContainsPath(location, name)) return location; throw fail(name + " not found in: " + files); } return null; } static public File byteCodePathForClass(Object o) { return byteCodePathForClass(_getClass(o)); } static public List regexpGroups(String pat, String s) { return regexpFirstGroups(pat, s); } static public List setMinus_inPlace(List l, int i) { return removeAndReturnCollection(l, i); } static public > B setMinus_inPlace(B l, A a) { return removeAndReturnCollection(l, a); } static public void dm_addByteCodePathToModuleClassLoader(File bytecode) { try { ClassLoader cl = getClassLoader(dm_current_mandatory_generic()); print("Adding to class loader " + cl + ": " + bytecode); call(cl, "addFile", bytecode); } catch (Exception __e) { throw rethrow(__e); } } static public String formatGMTWithMilliseconds_24() { return formatGMTWithMilliseconds_24(now()); } static public String formatGMTWithMilliseconds_24(long time) { var sdf = simpleDateFormat("HH:mm:ss''SSSS", TimeZone.getTimeZone("GMT")); return sdf.format(time) + " GMT"; } static public A unlistedCopyToClass_withConverter(Class c, Concept a) { if (a == null) return null; A b = unlisted(c); ccopyFields_withConverter(a, b); return b; } static public Class findClassThroughDefaultClassFinder(String shortName) { return shortName == null ? null : (Class) callF(_defaultClassFinder(), "main$" + shortName); } static public String leadingAngleBracketStuff(String s) { if (!startsWith(s, ']')) return null; return substring(s, 1, smartIndexOf(s, ']')); } static public long parseDateWithMillisecondsGMT(String s) { try { return simpleDateFormat_GMT("yyyy/MM/dd HH:mm:ss''SSSS").parse(dropSuffix(" GMT", s)).getTime(); } catch (Exception __e) { throw rethrow(__e); } } static public Object realMC() { return getThreadLocal(realMC_tl()); } static public Class mc() { return main.class; } static public A routeThroughAll(Iterable> processors, A a) { for (var processor : unnullForIteration(cloneList(processors))) { try { a = processor.get(a); } catch (Throwable __e) { _handleException(__e); } } return a; } static public boolean isTrueOpt(Object o) { if (o instanceof Boolean) return ((Boolean) o).booleanValue(); return false; } static public boolean isTrueOpt(String field, Object o) { return isTrueOpt(getOpt(field, o)); } static public boolean eqOrEqic(boolean caseInsensitive, String a, String b) { return caseInsensitive ? eqic(a, b) : eq(a, b); } static public boolean startsWith_addingSlash(String a, String b) { return startsWith_addingSlash(a, b, null); } static public boolean startsWith_addingSlash(String a, String b, Matches m) { return startsWith(addSlash(a), addSlash(b), m); } static public String nObjects(long n) { return n2(n, "object"); } static public String nObjects(Collection l) { return nObjects(l(l)); } static public String nObjects(Map m) { return nObjects(l(m)); } static public List regexpFirstGroups(String pat, String s) { if (s == null) return null; Matcher m = regexp(pat, s); return m.find() ? regexpGetGroups(m) : null; } static public A fourth(List l) { return _get(l, 3); } static public A fourth(A[] bla) { return bla == null || bla.length <= 3 ? null : bla[3]; } static public String dropDotSuffix(String s) { return dropTrailingDot(s); } static public String joinWithDot(Iterable l) { return join(".", l); } static public String joinWithDot(A... l) { return joinWithDot(asList(l)); } static public List splitAtSpace_trim(String s) { return nempties(trimAll(splitAtSpace(s))); } static public boolean addLibrariesToClassLoader(ClassLoader cl, Collection libIDs) { boolean change = false; for (String libID : unnullForIteration(libIDs)) change |= addLibraryToClassLoader(cl, libID); return change; } static public boolean getBoolOpt(Object o, String field) { return getBoolOpt(o, field, false); } static public boolean getBoolOpt(Object o, String field, boolean defaultValue) { return getBoolOpt(field, o, defaultValue); } static public boolean getBoolOpt(String field, Object o) { return getBoolOpt(field, o, false); } static public boolean getBoolOpt(String field, Object o, boolean defaultValue) { Object val = getOpt(o, field); return defaultValue ? !eq(Boolean.FALSE, val) : eq(Boolean.TRUE, val); } static public String shortenStr(Object o) { return shorten_str(o); } static public String shortenStr(Object o, int max) { return shorten_str(o, max); } static public List listPlusItems_inPlace(List l, A... more) { addAll(l, more); return l; } static public int classLoaderPathsCount(Class c) { return classLoaderPathsCount(getClassLoader(c)); } static public int classLoaderPathsCount(ClassLoader cl) { return l(filesFromClassLoader_new(cl)); } static public A findConcept(Class c, Object... params) { return findConceptWhere(c, params); } static public A findConcept(Concepts concepts, Class c, Object... params) { return findConceptWhere(concepts, c, params); } static public LinkedHashSet mapToLinkedHashSet(Object f, Iterable l) { LinkedHashSet x = new LinkedHashSet(l(l)); if (l != null) for (Object o : l) x.add(callF(f, o)); return x; } static public LinkedHashSet mapToLinkedHashSet(IF1 f, Iterable l) { return mapToLinkedHashSet((Object) f, l); } static public List tok_integers(String s) { return tok_integersInOrder(s); } static public Object cCall(Concept.Ref ref, String method, Object... args) { return ccall(ref, method, args); } static public Object cCall(Object object, String method, Object... args) { return ccall(object, method, args); } static public Object cCallOpt(Concept.Ref ref, String method, Object... args) { return ccallOpt(ref, method, args); } static public Object cCallOpt(Object object, String method, Object... args) { return ccallOpt(object, method, args); } static public Object cPcall(Concept.Ref ref, String method, Object... args) { return pcallWithEnter(cDeref(ref), method, args); } static public Object cPcall(Object object, String method, Object... args) { return pcallWithEnter(object, method, args); } static public List deleteConceptsWhereIC(Class c, Object... params) { return deleteConceptsWhereCI(c, params); } static public Object[] prependParamIfOddCount(Object x, Object[] params) { if (odd(l(params))) return itemPlusObjectArray(x, params); return params; } static public AutoCloseable combineAutoCloseables(final AutoCloseable a, final AutoCloseable b) { return a == null ? b : b == null ? a : new AutoCloseable() { public String toString() { return "pClose(a); pClose(b);"; } public void close() throws Exception { pClose(a); pClose(b); } }; } static public AutoCloseable combineAutoCloseables(AutoCloseable a, AutoCloseable b, AutoCloseable c, AutoCloseable... more) { return combineAutoCloseables(concatLists(ll(a, b, c), asList(more))); } static public AutoCloseable combineAutoCloseables(Iterable l) { return foldl(new F2() { public AutoCloseable get(AutoCloseable a, AutoCloseable b) { try { return combineAutoCloseables(a, b); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "combineAutoCloseables(a,b)"; } }, null, l); } static public String shorten_str(Object o) { return shorten(str(o)); } static public String shorten_str(Object o, int max) { return shorten(str(o), max); } static public Set syncCompactSet() { return new CompactHashSet(); } static public List withoutInstancesOf(Iterable i, Class c) { List l = new ArrayList(); if (i == null) return l; c = primitiveToBoxedTypeOpt(c); for (A o : i) if (!isInstance(c, o)) l.add(o); return l; } static public List withoutInstancesOf(Class c, Iterable i) { return withoutInstancesOf(i, c); } static public String hlinkButton(String url, String text, Object... params) { return hbuttonLink(url, text, params); } static public String dateWithSecondsUTC() { return dateWithSecondsUTC(now()); } static public String dateWithSecondsUTC(long time) { SimpleDateFormat format = simpleDateFormat_UTC("yyyy/MM/dd"); return format.format(time) + " " + formatUTCWithSeconds_24(time); } static public Object addFilenameHeader(Object r, String name) { call(r, "addHeader", "Content-Disposition", "inline; filename=\"" + name + "\""); return r; } static public Object addFilenameHeader(String name, Object r) { return addFilenameHeader(r, name); } static public String hcenter(Object contents, Object... params) { return hfulltag("center", contents, params); } static public String linesLL(Object... x) { return lines(ll(x)); } static public String dynShortClassName(Object o) { return shortDynamicClassName(o); } static public String structure_convertTokenMarkersToExplicit(String s) { return join(structure_convertTokenMarkersToExplicit(javaTokForStructure(s))); } static public List structure_convertTokenMarkersToExplicit(List tok) { TreeSet refs = new TreeSet(); for (int i = 1; i < l(tok); i += 2) { String t = tok.get(i); if (t.startsWith("t") && isInteger(t.substring(1))) { refs.add(parseInt(t.substring(1))); tok.set(i, "r" + substring(t, 1)); } } if (empty(refs)) return tok; for (int i : refs) { int idx = i * 2 + 1; if (idx >= l(tok)) continue; String t = ""; if (endsWithLetterOrDigit(tok.get(idx - 1))) t = " "; tok.set(idx, t + "m" + i + " " + tok.get(idx)); } return tok; } static public Map keysAndValuesToString(Map map) { return mapKeysAndValues(__72 -> str(__72), map); } static public String fixHTML(Object contents) { String s = str(contents); return hhtml(hbody(s)); } static public java.util.regex.Pattern compileRegexpPossiblyIC_unicodeCase(String pat, boolean ic) { return ic ? compileRegexpIC_unicodeCase(pat) : compileRegexp(pat); } static public String regexpReplace_directWithRefs(String s, String pat, String replacement) { Matcher m = regexp(pat, s); return regexpReplace_direct(m, replacement); } static public String regexpReplace_directWithRefs(Matcher m, String replacement) { StringBuffer buf = new StringBuffer(); while (m.find()) m.appendReplacement(buf, replaceDollarVars2_dyn(replacement, v -> !isInteger(v) ? null : matcherGroup(m, parseInt(v)))); m.appendTail(buf); return str(buf); } static public String format_quoted(String pat, Object... args) { return format_quoteAll(pat, args); } static public String nPatterns(long n) { return n2(n, "pattern"); } static public String nPatterns(Collection l) { return nPatterns(l(l)); } static public String nPatterns(Map map) { return nPatterns(l(map)); } static public void waitForQToEmpty(Q q) { if (q == null || inQ(q)) return; Flag flag = new Flag(); q.add(flag); flag.waitUntilUp(); } static public boolean inQ(Q q) { return isInQ(q); } static public String combinePrintParameters(String s, Object o) { return (endsWithLetterOrDigit(s) ? s + ": " : s) + o; } static public void truncateListFromStart(List l, int n) { if (l(l) <= n) return; removeSubList(l, 0, l(l) - n); } static public int getIntOpt(List c, int i) { return toIntOpt(get(c, i)); } static public int getIntOpt(Map map, Object key) { return toIntOpt(mapGet(map, key)); } static public int getIntOpt(Object o, String field) { return getIntOpt(o, field, 0); } static public int getIntOpt(Object o, String field, int defaultValue) { return toIntOpt(getOpt(o, field), defaultValue); } static public int getIntOpt(String field, Object o) { return getIntOpt(field, o, 0); } static public int getIntOpt(String field, Object o, int defaultValue) { return getIntOpt(o, field, defaultValue); } static public Concepts newConceptsWithClassFinder(String progID) { Concepts cc = new Concepts(progID); cc.classFinder = _defaultClassFinder(); return cc; } static public List immutableEmptyList() { return Collections.emptyList(); } static public short[] emptyShortArray = new short[0]; static public short[] emptyShortArray() { return emptyShortArray; } static public Map immutableEmptyMap() { return Collections.emptyMap(); } static public Symbol emptySymbol_value; static public Symbol emptySymbol() { if (emptySymbol_value == null) emptySymbol_value = symbol(""); return emptySymbol_value; } static public void flexMatchAngleBracketVarsIC_honorPunctuation_noBrackets_iterate(String pat, String input, VF1> onMatch) { List vars = new ArrayList(); String starsPat = angleBracketVarsToStars(pat, vars); flexMatchIC_iterate(starsPat, javaTok(starsPat), javaTok(input), new VF1() { public void get(Matches m) { try { Map map = matchesToMapUsingVarList_ciMap(m, vars); if (map != null) callF(onMatch, map); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "SS map = matchesToMapUsingVarList_ciMap(m, vars);\r\n if (map != null) callF..."; } }); } static public String td_top(Object contents, Object... params) { return tdTop(contents, params); } static public void tokAppend(List tok, int i, String s) { tok.set(i, tok.get(i) + s); } static public Map mapToKeys(Iterable l, IF1 f) { if (l == null) return null; HashMap map = new HashMap(); for (A a : l) map.put(f.get(a), a); return map; } static public Map mapToKeys(IF1 f, Iterable l) { return mapToKeys(l, f); } static public MultiSet ciMultiSet() { MultiSet ms = new MultiSet(); ms.map = ciMap(); return ms; } static public MultiSet ciMultiSet(Iterable l) { return asCIMultiSet(l); } static public String dropNumberPrefix(String s) { return dropFirst(s, indexOfNonDigit(s)); } static public List mcAndUtils() { return llNonNulls(mc(), findClass_fullName("loadableUtils.utils")); } static public Set synchroLinkedHashSet() { return Collections.synchronizedSet(new CompactLinkedHashSet()); } static public boolean shortNameIs(String name, Object o) { return isShortNamed(name, o); } static public boolean shortNameIs(Object o, String name) { return shortNameIs(name, o); } static public long getLong(Object o, String field) { return toLong(getOpt(o, field)); } static public long getLong(String field, Object o) { return getLong(o, field); } static public LinkedHashMap mapSortedByFunctionOnKey(IF1 f, Map map) { List l = new ArrayList(map.keySet()); sortInPlaceByCalculatedField(l, f); LinkedHashMap map2 = new LinkedHashMap(); for (A a : l) map2.put(a, map.get(a)); return map2; } static public Collection filesFromClassLoader_new(Class c) { return filesFromClassLoader(getClassLoader(c)); } static public Collection filesFromClassLoader_new(ClassLoader cl) { return (Collection) getOpt(cl, "files"); } static public boolean zipContainsFile(File inZip, String fileName) { try { ZipFile zip = new ZipFile(inZip); try { return zip.getEntry(fileName) != null; } finally { _close(zip); } } catch (Exception __e) { throw rethrow(__e); } } volatile static public boolean conceptsAndBot_running = false; static public boolean conceptsAndBot_thinOnStart = true; static public void conceptsAndBot() { conceptsAndBot(null); } static public void conceptsAndBot(Integer autoSaveInterval) { if (conceptsAndBot_running) return; conceptsAndBot_running = true; Concepts cc = db_mainConcepts(); try { if (cc.useFileLock) { if (!cc.fileLock().tryToLock()) { ensureDBNotRunning(dbBotStandardName()); cc.fileLock().forceLock(); } } else ensureDBNotRunning(dbBotStandardName()); } catch (Throwable _e) { cc.dontSave = true; throw rethrow(_e); } cc.persist(autoSaveInterval); dbBot(false); if (conceptsAndBot_thinOnStart) { try { thinAProgramsBackups(getDBProgramID(), true); } catch (Throwable __e) { _handleException(__e); } } } static public void setFieldToSingleMethodProxy(Object o, String field, Object target, String methodName) { if (o == null || target == null) return; Class type = fieldType(o, field); if (type == null) throw fail("Field " + field + " not found in " + className(o)); set(o, field, singleObjectMethodProxy(type, target, methodName)); } static public ClassLoader classLoader(Object o) { return classLoaderForObject(o); } static public PrintWriter printWriter(OutputStream out) { return newPrintWriter(out); } static public PrintWriter printWriter(Writer out) { return newPrintWriter(out); } static public BufferedOutputStream bufferedFileOutputStream_append(File f) { try { return bufferedOutputStream(newFileOutputStream_append(f)); } catch (Exception __e) { throw rethrow(__e); } } static public String joinWithEmptyLines(Iterable l) { return join("\n\n", map(__73 -> rtrim(__73), l)); } static public String joinWithEmptyLines(String... l) { return joinWithEmptyLines(asList(l)); } static public Class actualMC() { return or((Class) realMC(), mc()); } static public Object get2(Object o, String field1, String field2) { return get(get(o, field1), field2); } static public HashMap findClass_fullName_cache = new HashMap(); static public Class findClass_fullName(String name) { synchronized (findClass_fullName_cache) { if (findClass_fullName_cache.containsKey(name)) return findClass_fullName_cache.get(name); Class c; try { c = Class.forName(name); } catch (ClassNotFoundException e) { c = null; } findClass_fullName_cache.put(name, c); return c; } } static public boolean startsWithAny(String a, Collection b) { for (String prefix : unnullForIteration(b)) if (startsWith(a, prefix)) return true; return false; } static public boolean startsWithAny(String a, String... b) { if (b != null) for (String prefix : unnullForIteration(b)) if (startsWith(a, prefix)) return true; return false; } static public boolean startsWithAny(String a, Collection b, Matches m) { for (String prefix : unnullForIteration(b)) if (startsWith(a, prefix, m)) return true; return false; } static public String afterDollar(String s) { return substring(s, smartIndexOf(s, '$') + 1); } static public String dynamicShortName(Object o) { return shortDynamicClassName(o); } static public String renderMultiSet(MultiSet ms) { if (ms == null) return "-"; List l = new ArrayList(); for (Object o : ms.highestFirst()) l.add(str(o) + " (" + ms.get(o) + ")"); return joinWithComma(l); } static public String renderMultiSet(Iterable l) { return renderMultiSet(toMultiSet(l)); } static public void dm_requireSnippetUpdatesModule() { if (!dm_subOSConnectorToChannel("snippetUpdates")) dm_requireModule("#1019239/SnippetUpdates"); } static public String firstIntAsString(String s) { return jextract("", s); } static public void dm_reloadModuleInBackground(Object module) { dm_callOS("reloadModuleInBackground", dm_getStem(module)); } static public void dm_reloadModuleInBackground() { dm_reloadModuleInBackground(dm_current_mandatory_generic()); } static public AutoCloseable vmBus_addListener_basic(String msg, Object listener) { Map map = vm_busListenersByMessage_live(); synchronized (map) { Set listeners = map.get(msg); if (listeners == null) map.put(msg, listeners = syncIdentityHashSet()); return tempAdd(listeners, listener); } } static public String nemptyLines(Iterable l) { return lines(nempties(allToStrOrNull(l))); } static public String stackTraceToString(StackTraceElement[] st) { return lines(st); } static public String stackTraceToString(Throwable e) { return getStackTrace_noRecord(e); } static public String[] match2x(List pat, List tok) { ArrayList result = new ArrayList(); if (pat.size() != tok.size()) return null; for (int i = 1; i < pat.size(); i += 2) { String p = pat.get(i), t = tok.get(i); boolean match = false; if (eq(p, "*")) match = true; else if (eq(p, "")) match = isQuoted(t); else if (eq(p, "")) match = isIdentifier(t); else if (eq(p, "")) match = isInteger(t); else { if (!eq(p, t)) return null; continue; } if (!match) return null; result.add(t); } return result.toArray(new String[result.size()]); } static public List sortedByCalculatedFieldIC(Iterable c, Object f) { return sortByCalculatedFieldIC(c, f); } static public List sortedByCalculatedFieldIC(Object f, Iterable c) { return sortedByCalculatedFieldIC(c, f); } static public List sortedByCalculatedFieldIC(IF1 f, Iterable c) { return sortedByCalculatedFieldIC((Object) f, c); } static public List sortedByCalculatedFieldIC(Iterable c, IF1 f) { return sortedByCalculatedFieldIC((Object) f, c); } static public boolean canCallWithVarargs(Object o, String method, Object... args) { if (o == null) return false; if (o instanceof Class) { Class c = (Class) o; _MethodCache cache = callOpt_getCache(c); if (cache.findStaticMethod(method, args) != null) return true; List methods = cache.cache.get(method); if (methods != null) methodSearch: for (Method m : methods) { { if (!(m.isVarArgs() && isStaticMethod(m))) continue; } if (massageArgsForVarArgsCall(m, args) != null) return true; } } else { Class c = o.getClass(); _MethodCache cache = callOpt_getCache(c); if (cache.findMethod(method, args) != null) return true; List methods = cache.cache.get(method); if (methods != null) methodSearch: for (Method m : methods) { { if (!(m.isVarArgs())) continue; } if (massageArgsForVarArgsCall(m, args) != null) return true; } } return false; } static public IF0 if0_const(final A a) { return () -> a; } static public long nanos() { return nanoTime(); } static public double nsToMS(double nanoseconds) { return nanoseconds / 1e6; } static public IF0 runnableToIF0(Runnable r) { return r == null ? null : () -> { r.run(); return null; }; } static public boolean isSortedListByCalculatedField(Collection l, IF1 f) { return isSortedListByComparator(l, (a, b) -> cmp(f.get(a), f.get(b))); } static public boolean isSortedListByCalculatedField(IF1 f, Collection l) { return isSortedListByCalculatedField(l, f); } static public ConceptShadow conceptShadow(Concept c) { if (c == null) return null; return new ConceptShadow(c); } static public int poorMansProfiling_defaultInterval() { return 25; } static public MultiSet poorMansProfiling_results = new MultiSet(); static public int poorMansProfiling_samples; static public java.util.Timer poorMansProfiling_timer; static public Lock poorMansProfiling_lock = lock(); static public Thread poorMansProfiling_threadToSample; static public void poorMansProfiling() { poorMansProfiling(poorMansProfiling_defaultInterval()); } static public void poorMansProfiling(Thread thread) { poorMansProfiling(poorMansProfiling_defaultInterval(), thread); } static public void poorMansProfiling(int interval) { poorMansProfiling(interval, null); } static public void poorMansProfiling(int interval, Thread thread) { Lock __0 = poorMansProfiling_lock; lock(__0); try { poorMansProfiling_threadToSample = thread; poorMansProfiling_stop(); poorMansProfiling_clear(); poorMansProfiling_timer = doEvery_daemon(interval, new Runnable() { public void run() { try { Map map = poorMansProfiling_threadToSample != null ? litmap(poorMansProfiling_threadToSample, poorMansProfiling_threadToSample.getStackTrace()) : runnableThreadsWithStackTraces(); Lock __1 = poorMansProfiling_lock; lock(__1); try { poorMansProfiling_samples++; for (Thread t : keys(map)) { { if (isSystemThread(t)) continue; } StringBuilder buf = new StringBuilder(); for (StackTraceElement e : map.get(t)) buf.append(e).append("\n"); poorMansProfiling_results.add(str(buf)); } } finally { unlock(__1); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "Map map =\r\n poorMansProfiling_threadToSampl..."; } }); } finally { unlock(__0); } } static public void poorMansProfiling_stop() { Lock __2 = poorMansProfiling_lock; lock(__2); try { if (poorMansProfiling_timer != null) { stopTimer(poorMansProfiling_timer); poorMansProfiling_timer = null; } } finally { unlock(__2); } } static public void poorMansProfiling_clear() { Lock __3 = poorMansProfiling_lock; lock(__3); try { poorMansProfiling_results.clear(); poorMansProfiling_samples = 0; } finally { unlock(__3); } } static public MultiSet poorMansProfiling_results() { return new MultiSet(poorMansProfiling_results); } static public MultiSet poorMansProfiling_stopAndGetResults() { Lock __4 = poorMansProfiling_lock; lock(__4); try { poorMansProfiling_stop(); return poorMansProfiling_results(); } finally { unlock(__4); } } static public String poorMansProfiling_stopAndRenderResults() { return poorMansProfiling_renderFullResults(poorMansProfiling_stopAndGetResults()); } static public String renderElapsedTimePleasantly(long ms) { double secs = toSeconds(ms); if (secs >= 1) return formatDouble(secs, 3) + " s"; return ms + " ms"; } static public String renderElapsedTimePleasantly(Runnable r) { long time = sysNow(); { if (r != null) r.run(); } return renderElapsedTimePleasantly(sysNow() - time); } static public long lastTiming() { return getLastTiming(); } static public Map> javaTok_cached_cache = synchronizedMRUCache(100); static public List javaTok_cached(String s) { List tok = javaTok_cached_cache.get(s); if (tok == null) javaTok_cached_cache.put(s, tok = javaTok(s)); return tok; } static public A ccopyFields_withConverter_pcall(Concept x, A y, String... fields) { if (x == null || y == null) return y; if (empty(fields)) { for (String field : conceptFields(x)) pcallF(() -> cSmartSetField_withConverter(y, field, cget(x, field))); } else for (String field : fields) { Object o = cget(x, field); if (o != null) pcallF(() -> cSmartSetField_withConverter(y, field, o)); } return y; } static public boolean isUnlisted(Concept c) { return c != null && c.concepts() == null; } static public List allBackRefObjects(Concept c) { return cloneList(c == null ? null : c.backRefs); } static public void stepAll(Steppable s) { if (s != null) while (s.step()) { ping(); } } static public Object dm_moduleOfType(String type) { return first(dm_modulesOfType(type)); } static public int identityHash(Object o) { return identityHashCode(o); } static public int countCodeTokens(String s) { return countTokens(s); } static public int countCodeTokens(List tok) { return countTokens(tok); } static public int minUnlessMinus1(int a, int b) { return a == -1 ? b : b == -1 ? a : min(a, b); } static public long firstLong_regexp(String s) { return parseLongOr0(regexpExtract("\\d+", s)); } static public String toStringWithClassName(Object o) { return toStringWithClass(o); } static public String yesNo2(Boolean b) { return eq(b, true) ? "Yes" : eq(b, false) ? "No" : "Unknown"; } static public boolean dirOrZipContainsPath(File location, String subPath) { try { if (location.isDirectory()) { return new File(location, subPath).exists(); } else if (location.isFile()) { return zipFileContains_falseOnError(location, subPath); } return false; } catch (Exception __e) { throw rethrow(__e); } } static public List removeAndReturnCollection(List l, int i) { remove(l, i); return l; } static public > B removeAndReturnCollection(B l, A a) { remove(l, a); return l; } static public A ccopyFields_withConverter(Concept x, A y, String... fields) { if (x == null || y == null) return y; if (empty(fields)) { for (String field : conceptFields(x)) cSmartSetField_withConverter(y, field, cget(x, field)); } else for (String field : fields) { Object o = cget(x, field); if (o != null) cSmartSetField_withConverter(y, field, o); } return y; } static public SimpleDateFormat simpleDateFormat_GMT(String format) { SimpleDateFormat sdf = new SimpleDateFormat(format); sdf.setTimeZone(TimeZone.getTimeZone("GMT")); return sdf; } static public List regexpGetGroups(Matcher matcher) { int n = matcher.groupCount(); List l = new ArrayList(); for (int i = 1; i <= n; i++) l.add(matcher.group(i)); return l; } static public List regexpGetGroups(String pat, String s) { Matcher m = regexpMatcher(pat, s); if (m.find()) return regexpGetGroups(m); return null; } static public String dropTrailingDot(String s) { return dropSuffix(".", s); } static public boolean addLibraryToClassLoader(ClassLoader cl, String libraryID) { try { return addByteCodePathToClassLoader(cl, loadLibraryOrSrcLib(libraryID)); } catch (Exception __e) { throw rethrow(__e); } } static public Object ccall(Concept.Ref ref, String method, Object... args) { return callWithEnter(cDeref(ref), method, args); } static public Object ccall(Object object, String method, Object... args) { return callWithEnter(object, method, args); } static public Object ccallOpt(Concept.Ref ref, String method, Object... args) { return callOptWithEnter(cDeref(ref), method, args); } static public Object ccallOpt(Object object, String method, Object... args) { return callOptWithEnter(object, method, args); } static public Object pcallWithEnter(Object o, String method, Object... args) { try { return callWithEnter(o, method, args); } catch (Throwable __e) { return null; } } static public A cDeref(Concept.Ref ref) { return ref == null ? null : ref.get(); } static public List deleteConceptsWhereCI(Class c, Object... params) { List l = cloneList(findConceptsWhereCI(c, params)); deleteConcepts(l); return l; } static public Object[] itemPlusObjectArray(Object a, Object[] l) { Object[] out = new Object[l(l) + 1]; out[0] = a; arraycopy(l, 0, out, 1, l(l)); return out; } static public void pClose(AutoCloseable c) { close_pcall(c); } static public A foldl(F2 f, A seed, Iterable l) { A a = seed; if (l != null) for (B b : l) a = callF(f, a, b); return a; } static public A foldl(F2 f, A seed, B[] l) { A a = seed; if (l != null) for (B b : l) a = callF(f, a, b); return a; } static public A foldl(Object f, A seed, B[] l) { A a = seed; if (l != null) for (B b : l) a = (A) callF(f, a, b); return a; } static public SimpleDateFormat simpleDateFormat_UTC(String format) { SimpleDateFormat sdf = new SimpleDateFormat(format); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); return sdf; } static public String formatUTCWithSeconds_24(long time) { SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); return sdf.format(time) + " UTC"; } static public String shortDynamicClassName(Object o) { if (o instanceof DynamicObject && ((DynamicObject) o).className != null) return ((DynamicObject) o).className; return shortClassName(o); } static public Map mapKeysAndValues(Object f, Map map) { return mapKeysAndValues(f, f, map); } static public Map mapKeysAndValues(Object fKey, Object fValue, Map map) { if (map == null) return null; Map m = similarEmptyMap(map); for (Map.Entry __1 : _entrySet((Map) map)) { Object key = __1.getKey(); Object val = __1.getValue(); m.put(callF(fKey, key), callF(fValue, val)); } return m; } static public Map mapKeysAndValues(IF1 f, Map map) { return mapKeysAndValues(f, f, map); } static public Map mapKeysAndValues(IF1 fKey, IF1 fValue, Map map) { if (map == null) return null; Map m = similarEmptyMap(map); for (Map.Entry __0 : _entrySet(map)) { A key = __0.getKey(); B val = __0.getValue(); m.put(fKey.get(key), fValue.get(val)); } return m; } static public Map compileRegexpIC_unicodeCase_cache = syncMRUCache(10); static public java.util.regex.Pattern compileRegexpIC_unicodeCase(String pat) { java.util.regex.Pattern p = compileRegexpIC_unicodeCase_cache.get(pat); if (p == null) { try { compileRegexpIC_unicodeCase_cache.put(pat, p = java.util.regex.Pattern.compile(pat, Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE)); } catch (PatternSyntaxException e) { throw rethrow(wrapPatternSyntaxException(e)); } } return p; } static public String regexpReplace_direct(String s, String pat, String replacement) { Matcher m = regexp(pat, s); return regexpReplace_direct(m, replacement); } static public String regexpReplace_direct(Matcher m, String replacement) { StringBuffer buf = new StringBuffer(); while (m.find()) m.appendReplacement(buf, replacement); m.appendTail(buf); return str(buf); } static public String replaceDollarVars2_dyn(String s, IF1 f) { if (f == null) return s; return regexpReplace(s, "\\$(\\w+)|\\$\\{(.+?)\\}", matcher -> { String var = or(matcher.group(1), matcher.group(2)); String val = f.get(var); return val == null ? matcher.group() : str(val); }); } static public String matcherGroup(java.util.regex.Matcher m, int n) { return m != null && n >= 1 && n <= m.groupCount() ? m.group(n) : null; } static public String format_quoteAll(String pat, Object... args) { if (args.length == 0) return pat; List tok = javaTok(pat); int argidx = 0; for (int i = 1; i < tok.size(); i += 2) if (tok.get(i).equals("*")) tok.set(i, quote(argidx < args.length ? args[argidx++] : "null")); return join(tok); } static public int toIntOpt(Object o) { return toIntOpt(o, 0); } static public int toIntOpt(Object o, int defaultValue) { if (o == null) return defaultValue; if (o instanceof Number) return ((Number) o).intValue(); if (o instanceof String) return parseIntOpt((String) o); return defaultValue; } static public WeakHasherMap symbol_map = new WeakHasherMap(new Hasher() { public int hashCode(Symbol symbol) { return symbol.text.hashCode(); } public boolean equals(Symbol a, Symbol b) { if (a == null) return b == null; return b != null && eq(a.text, b.text); } }); static public Symbol symbol(String s) { if (s == null) return null; synchronized (symbol_map) { Symbol symbol = new Symbol(s, true); Symbol existingSymbol = symbol_map.findKey(symbol); if (existingSymbol == null) symbol_map.put(existingSymbol = symbol, true); return existingSymbol; } } static public Symbol symbol(CharSequence s) { if (s == null) return null; if (s instanceof Symbol) return (Symbol) s; if (s instanceof String) return symbol((String) s); return symbol(str(s)); } static public Symbol symbol(Object o) { return symbol((CharSequence) o); } static public int indexOfNonDigit(String s) { int n = l(s); for (int i = 0; i < n; i++) if (!isDigit(s.charAt(i))) return i; return -1; } static public boolean isShortNamed(String name, Object o) { return eq(shortClassName(o), name); } static public List sortInPlaceByCalculatedField(List l, final F1 f) { sort(l, new Comparator() { public int compare(A a, A b) { return stdcompare(f.get(a), f.get(b)); } }); return l; } static public List sortInPlaceByCalculatedField(List l, final IF1 f) { sort(l, new Comparator() { public int compare(A a, A b) { return stdcompare(f.get(a), f.get(b)); } }); return l; } static public ClassLoader classLoaderForObject(Object o) { if (o instanceof ClassLoader) return ((ClassLoader) o); if (o == null) return null; return _getClass(o).getClassLoader(); } static public PrintWriter newPrintWriter(OutputStream out) { return new PrintWriter(outputStreamToWriter(out)); } static public PrintWriter newPrintWriter(Writer out) { return new PrintWriter(out); } static public BufferedOutputStream bufferedOutputStream(OutputStream out) { if (out == null) return null; if (out instanceof BufferedOutputStream) return ((BufferedOutputStream) out); return new BufferedOutputStream(out, defaultBufferedOutputStreamSize()); } static public FileOutputStream newFileOutputStream_append(File path) { try { return newFileOutputStream(path, true); } catch (Exception __e) { throw rethrow(__e); } } static public FileOutputStream newFileOutputStream_append(String path) { try { return newFileOutputStream(path, true); } catch (Exception __e) { throw rethrow(__e); } } static public MultiSet toMultiSet(Iterable l) { return asMultiSet(l); } static public boolean dm_subOSConnectorToChannel(String channel) { if (hasMethodNamed(dm_os(), "subConnectorToChannel")) return false; dm_osCall("subConnectorToChannel", channel); return true; } static public String dm_requireModule(String moduleLibID) { return dm_makeModule(moduleLibID); } static public List allToStrOrNull(Iterable c) { return lmap(__74 -> strOrNull(__74), c); } static public List allToStrOrNull(Object[] c) { return lmap(__75 -> strOrNull(__75), c); } static public String getStackTrace(Throwable throwable) { lastException(throwable); return getStackTrace_noRecord(throwable); } static public String getStackTrace_noRecord(Throwable throwable) { StringWriter writer = new StringWriter(); throwable.printStackTrace(new PrintWriter(writer)); return hideCredentials(writer.toString()); } static public String getStackTrace() { return getStackTrace_noRecord(new Throwable()); } static public List sortByCalculatedFieldIC(Iterable c, final Object f) { List l = cloneList(c); sort(l, new Comparator() { public int compare(A a, A b) { return compareIC((String) callF(f, a), (String) callF(f, b)); } }); return l; } static public long nanoTime() { return System.nanoTime(); } static public boolean isSortedListByComparator(Collection l, Comparator cmp) { Iterator it = iterator(l); if (!it.hasNext()) return true; A a = it.next(); while (it.hasNext()) { A b = it.next(); if (cmp.compare(a, b) > 0) return false; a = b; } return true; } static public FixedRateTimer doEvery_daemon(long delay, final Object r) { return doEvery_daemon(defaultTimerName(), delay, r); } static public FixedRateTimer doEvery_daemon(String timerName, long delay, final Object r) { return doEvery_daemon(timerName, delay, delay, r); } static public FixedRateTimer doEvery_daemon(long delay, long firstDelay, final Object r) { return doEvery_daemon(defaultTimerName(), delay, firstDelay, r); } static public FixedRateTimer doEvery_daemon(String timerName, long delay, long firstDelay, final Object r) { FixedRateTimer timer = new FixedRateTimer(true); timer.scheduleAtFixedRate(smartTimerTask(r, timer, delay), firstDelay, delay); return timer; } static public FixedRateTimer doEvery_daemon(double delaySeconds, final Object r) { return doEvery_daemon(toMS(delaySeconds), r); } static public Map runnableThreadsWithStackTraces() { Map map = filterMap((__77, __78) -> isThreadRunnable_x(__77, __78), Thread.getAllStackTraces()); map.remove(currentThread()); return map; } static public boolean isSystemThread(Thread t) { ThreadGroup g = t.getThreadGroup(); return g != null && g.getName().equals("system"); } static public void stopTimer(java.util.Timer timer) { if (timer != null) timer.cancel(); } static public ThreadLocal poorMansProfiling_renderFullResults_backwards = new ThreadLocal(); static public String poorMansProfiling_renderFullResults(final MultiSet traces) { int samples = poorMansProfiling_samples; boolean backwards = isTrue(getAndClearThreadLocal(poorMansProfiling_renderFullResults_backwards)); final int n = traces.size(); int percent = ratioToIntPercent(l(traces), samples); return (samples == 0 ? "Nothing sampled" : percent + "% core activity [" + n2(samples, "sample") + " taken]") + "\n\n" + joinMap(backwards ? traces.lowestFirst() : traces.highestFirst(), new F1() { public Object get(String trace) { try { return traces.get(trace) + "/" + n + "\n" + trace + "\n\n"; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "traces.get(trace) + \"/\" + n + \"\\n\" + trace + \"\\n\\n\""; } }); } static public long getLastTiming() { ThreadLocal tl = saveTiming_tl(); if (tl == null) return -1; Long l = tl.get(); return l == null ? -1 : l; } static public boolean cSmartSetField_withConverter(Concept c, String field, Object value) { return cSmartSetField_withConverter(c, field, value, false); } static public boolean cSmartSetField_withConverter(Concept c, String field, Object value, boolean verbose) { return cSmartSetField_withConverter(c, field, value, new DefaultValueConverterForField(), verbose); } static public boolean cSmartSetField_withConverter(Concept c, String field, Object value, ValueConverterForField converter) { return cSmartSetField_withConverter(c, field, value, converter, false); } static public boolean cSmartSetField_withConverter(Concept c, String field, Object value, ValueConverterForField converter, boolean verbose) { try { Field f = setOpt_findField(c.getClass(), field); if (f != null) { if (verbose) print("cSmartSetField_withConverter: setting " + f + " = " + value); if (converter != null && value != null) { OrError result = converter.convertValue(c, f, value); if (result == null) throw fail("Unknown conversion " + className(value) + " => " + f); if (!result.ok()) throw fail("Unknown conversion " + className(value) + " => " + f + ": " + result.error()); value = result.get(); } if (verbose) print("cSmartSetField_withConverter: converted value=" + value); if (value instanceof List && f.getType() == Concept.RefL.class) { Concept.RefL l = (Concept.RefL) (f.get(c)); if (verbose) print("cSmartSetField_withConverter: RefL=" + l); l.replaceWithList((List) value); if (verbose) print("cSmartSetField_withConverter: backrefs=" + c.backRefs); return true; } if (value instanceof Concept && f.getType() == Concept.Ref.class) return ((Concept.Ref) f.get(c)).set((Concept) value); } return _csetField(c, field, value); } catch (Exception __e) { throw rethrow(__e); } } static public List dm_modulesOfType(String type) { List l = splitAtSlash(type); if (l(l) == 2) return filterByProgramIDAndShortClassName(dm_listModules(), first(l), second(l)); else return filterByShortClassName(dm_listModules(), type); } static public long parseLongOr0(String s) { return parseLong(s); } static public long parseLongOr0(Object s) { return parseLong(s); } static public String regexpExtract(String pat, String s) { if (s == null) return null; Matcher m = regexpMatcher(pat, s); return m.find() ? m.group() : null; } static public String toStringWithClass(Object o) { return o == null ? null : className(o) + " - " + o; } static public boolean zipFileContains_falseOnError(File inZip, String fileName) { try { return zipFileContains(inZip, fileName); } catch (Throwable e) { return false; } } static public Object callWithEnter(Object o, String method, String[] arg) { return callWithEnter(o, method, new Object[] { arg }); } static public Object callWithEnter(Object o, String method, Object... args) { if (o == null) return null; AutoCloseable __1 = tempEnter(o); try { return call_withVarargs(o, method, args); } finally { _close(__1); } } static public Object callOptWithEnter(Object o, String method, String[] arg) { return callOptWithEnter(o, method, new Object[] { arg }); } static public Object callOptWithEnter(Object o, String method, Object... args) { if (o == null) return null; AutoCloseable __1 = tempEnter(o); try { return callOpt_withVarargs(o, method, args); } finally { _close(__1); } } static public Writer outputStreamToWriter(OutputStream out) { try { return new OutputStreamWriter(out, "UTF-8"); } catch (Exception __e) { throw rethrow(__e); } } static public int defaultBufferedOutputStreamSize() { return 65536; } static public MultiSet asMultiSet(Iterable l) { return new MultiSet(l); } static public Object dm_osCall(String functionName, Object... args) { return dm_callOS(functionName, args); } static public Map filterMap(Map map, Object f) { if (map == null) return null; Map m2 = similarEmptyMap(map); for (A a : keys(map)) { B b = map.get(a); if (isTrue(callF(f, a, b))) m2.put(a, b); } return m2; } static public Map filterMap(Object f, Map map) { return filterMap(map, f); } static public Map filterMap(Map map, IF2 f) { return filterMap(f, map); } static public Map filterMap(IF2 f, Map map) { return filterMap(map, (Object) f); } static public Set isThreadRunnable_x_exclude = lithashset("Java.lang.Thread.start0", "java.lang.Object.wait", "java.net.Inet6AddressImpl.lookupAllHostAddr", "java.io.FileInputStream.readBytes", "jdk.internal.misc.Unsafe.park", "sun.misc.Unsafe.park", "java.net.SocketInputStream.socketRead0", "java.net.PlainSocketImpl.socketConnect", "java.net.PlainSocketImpl.socketAccept", "sun.awt.X11.XToolkit.waitForEvents", "java.net.DualStackPlainSocketImpl.accept0", "org.jnativehook.GlobalScreen$NativeHookThread.enable", "java.lang.Thread.sleep", "sun.nio.ch.EPollArrayWrapper.epollWait", "com.ibm.lang.management.internal.MemoryNotificationThread.processNotificationLoop", "com.ibm.tools.attach.target.IPC.waitSemaphore", "sun.nio.ch.Net.poll", "sun.nio.ch.Net.accept", "sun.nio.ch.Net.connect0", "sun.nio.ch.EPoll.wait", "sun.nio.ch.SocketDispatcher.read0", "sun.nio.fs.LinuxWatchService.poll", "java.lang.ref.Reference.waitForReferencePendingList", "com.sun.java.accessibility.internal.AccessBridge.runDLL", "java.lang.ProcessHandleImpl.waitForProcessExit0"); static public boolean isThreadRunnable_x(Thread t, StackTraceElement[] trace) { { Boolean __1 = (Boolean) vmBus_query("isThreadRunnable_x", t, trace); if (__1 != null) return __1; } if (t.getState() != Thread.State.RUNNABLE) return false; StackTraceElement e = first(trace); if (e == null) return false; String s = e.getClassName() + "." + e.getMethodName(); return !isThreadRunnable_x_exclude.contains(s); } static public int ratioToIntPercent(double x, double y) { return roundToInt(x * 100 / y); } static public boolean _csetField(Concept c, String field, Object value) { try { Field f = setOpt_findField(c.getClass(), field); if (value instanceof RC) value = c._concepts.getConcept((RC) value); value = deref(value); if (value instanceof String && l((String) value) >= concepts_internStringsLongerThan) value = intern((String) value); if (f == null) { assertIdentifier(field); Object oldVal = mapGet(c.fieldValues, field); if (value instanceof Concept) { if (oldVal instanceof Concept.Ref) return ((Concept.Ref) oldVal).set((Concept) value); else { dynamicObject_setRawFieldValue(c, field, c.new Ref((Concept) value)); c.change(); return true; } } else { if (oldVal instanceof Concept.Ref) ((Concept.Ref) oldVal).unindexAndDrop(); if (eq(oldVal, value)) return false; if (isConceptList(value) && nempty(((List) value))) { dynamicObject_setRawFieldValue(c, field, c.new RefL(((List) value))); c.change(); return true; } if (value == null) { dynamicObject_dropRawField(c, field); } else { if (!isPersistable(value)) throw fail("Can't persist: " + c + "." + field + " = " + value); dynamicObject_setRawFieldValue(c, field, value); } c.change(); return true; } } else if (isSubtypeOf(f.getType(), Concept.Ref.class)) { ((Concept.Ref) f.get(c)).set((Concept) derefRef(value)); c.change(); return true; } else if (isSubtypeOf(f.getType(), Concept.RefL.class)) { ((Concept.RefL) f.get(c)).replaceWithList(lmap(__76 -> derefRef(__76), (List) value)); c.change(); return true; } else { Object old = f.get(c); if (neq(value, old)) { boolean isTransient = isTransient(f); if (!isTransient && !isPersistable(value)) throw fail("Can't persist: " + c + "." + field + " = " + value); f.set(c, value); if (!isTransient) c.change(); return true; } } return false; } catch (Exception __e) { throw rethrow(__e); } } static public List splitAtSlash(String s) { return trimAll(asList(s.split("/"))); } static public List filterByProgramIDAndShortClassName(Iterable l, String progID, String className) { List out = new ArrayList(); for (Object o : unnull(l)) if (shortClassNameIs(o, className) && sameSnippetID(getProgramID(o), progID)) out.add(o); return out; } static public List dm_listModules() { return (List) dm_callOS("listModules"); } static public List filterByShortClassName(Iterable l, String name) { List out = new ArrayList(); for (Object o : unnull(l)) if (shortClassNameIs(o, name)) out.add(o); return out; } static public boolean zipFileContains(File inZip, String fileName) { try { ZipFile zip = new ZipFile(inZip); try { return zipFileContains(zip, fileName); } finally { zip.close(); } } catch (Exception __e) { throw rethrow(__e); } } static public boolean zipFileContains(ZipFile zip, String fileName) { try { return zip.getEntry(fileName) != null; } catch (Exception __e) { throw rethrow(__e); } } static public AutoCloseable tempEnter(Object o) { return o == null ? null : optCast(AutoCloseable.class, rcallOpt("enter", o)); } static public Object callOpt_withVarargs(Object o, String method, Object... args) { try { if (o == null) return null; if (o instanceof Class) { Class c = (Class) o; _MethodCache cache = callOpt_getCache(c); Method me = cache.findMethod(method, args); if (me == null) { return null; } if ((me.getModifiers() & Modifier.STATIC) == 0) return null; return invokeMethod(me, null, args); } else { Class c = o.getClass(); _MethodCache cache = callOpt_getCache(c); Method me = cache.findMethod(method, args); if (me != null) return invokeMethod(me, o, args); List methods = cache.cache.get(method); if (methods != null) methodSearch: for (Method m : methods) { { if (!(m.isVarArgs())) continue; } Object[] newArgs = massageArgsForVarArgsCall(m, args); if (newArgs != null) return invokeMethod(m, o, newArgs); } return null; } } catch (Exception __e) { throw rethrow(__e); } } static public void dynamicObject_setRawFieldValue(DynamicObject o, Object key, Object value) { if (o == null) return; synchronized (o) { o.fieldValues = syncMapPut2_createLinkedHashMap((LinkedHashMap) o.fieldValues, key, value); } } static public void dynamicObject_dropRawField(DynamicObject o, Object key) { if (o == null) return; synchronized (o) { o.fieldValues = (LinkedHashMap) syncMapRemove_deleteMapIfEmpty((Map) o.fieldValues, key); } } static public boolean isPersistable(Object o) { return !isInAnonymousClass(o); } static public boolean isTransient(Field f) { return (f.getModifiers() & java.lang.reflect.Modifier.TRANSIENT) != 0; } static public Object rcallOpt(String method, Object o, Object... args) { return callOpt_withVarargs(o, method, args); } static public > C syncMapRemove_deleteMapIfEmpty(C map, A key) { if (map != null && key != null) synchronized (collectionMutex(map)) { map.remove(key); if (map.isEmpty()) return null; } return map; } static public boolean isInAnonymousClass(Object o) { if (o == null) return false; return isAnonymousClassName(className(o)); } static public boolean isAnonymousClassName(String s) { for (int i = 0; i < l(s); i++) if (s.charAt(i) == '$' && Character.isDigit(s.charAt(i + 1))) return true; return false; } static public interface Hasher { public int hashCode(A a); public boolean equals(A a, A b); } static public class Symbol { public String text; public Symbol() { } public Symbol(String text, boolean dummy) { this.text = text; } public int hashCode() { return loadableUtils.utils.hashCode(text); } public String toString() { return text; } public boolean equals(Object o) { return this == o; } } static final public class WeakHasherMap extends AbstractMap implements Map { public Hasher hasher = null; final public boolean keyEquals(Object k1, Object k2) { return (hasher == null ? k1.equals(k2) : hasher.equals(k1, k2)); } final public int keyHashCode(Object k1) { return (hasher == null ? k1.hashCode() : hasher.hashCode(k1)); } final public WeakKey WeakKeyCreate(K k) { if (k == null) return null; else return new WeakKey(k); } final public WeakKey WeakKeyCreate(K k, ReferenceQueue q) { if (k == null) return null; else return new WeakKey(k, q); } final public class WeakKey extends WeakReference { public int hash; public WeakKey(K k) { super(k); hash = keyHashCode(k); } final public WeakKey create(K k) { if (k == null) return null; else return new WeakKey(k); } public WeakKey(K k, ReferenceQueue q) { super(k, q); hash = keyHashCode(k); } final public WeakKey create(K k, ReferenceQueue q) { if (k == null) return null; else return new WeakKey(k, q); } @Override public boolean equals(Object o) { if (o == null) return false; if (this == o) return true; if (!(o.getClass().equals(WeakKey.class))) return false; Object t = this.get(); @SuppressWarnings("unchecked") Object u = ((WeakKey) o).get(); if ((t == null) || (u == null)) return false; if (t == u) return true; return keyEquals(t, u); } @Override public int hashCode() { return hash; } } public HashMap hash; public ReferenceQueue queue = new ReferenceQueue(); @SuppressWarnings("unchecked") final public void processQueue() { WeakKey wk; while ((wk = (WeakKey) queue.poll()) != null) { hash.remove(wk); } } public WeakHasherMap(int initialCapacity, float loadFactor) { hash = new HashMap(initialCapacity, loadFactor); } public WeakHasherMap(int initialCapacity) { hash = new HashMap(initialCapacity); } public WeakHasherMap() { hash = new HashMap(); } public WeakHasherMap(Hasher h) { hash = new HashMap(); hasher = h; } @Override public int size() { return entrySet().size(); } @Override public boolean isEmpty() { return entrySet().isEmpty(); } @Override public boolean containsKey(Object key) { @SuppressWarnings("unchecked") K kkey = (K) key; return hash.containsKey(WeakKeyCreate(kkey)); } @Override public V get(Object key) { @SuppressWarnings("unchecked") K kkey = (K) key; return hash.get(WeakKeyCreate(kkey)); } @Override public V put(K key, V value) { processQueue(); return hash.put(WeakKeyCreate(key, queue), value); } @Override public V remove(Object key) { processQueue(); @SuppressWarnings("unchecked") K kkey = (K) key; return hash.remove(WeakKeyCreate(kkey)); } @Override public void clear() { processQueue(); hash.clear(); } @SuppressWarnings("TypeParameterShadowing") final public class Entry implements Map.Entry { public Map.Entry ent; public K key; public Entry(Map.Entry ent, K key) { this.ent = ent; this.key = key; } @Override public K getKey() { return key; } @Override public V getValue() { return ent.getValue(); } @Override public V setValue(V value) { return ent.setValue(value); } final public boolean keyvalEquals(K o1, K o2) { return (o1 == null) ? (o2 == null) : keyEquals(o1, o2); } final public boolean valEquals(V o1, V o2) { return (o1 == null) ? (o2 == null) : o1.equals(o2); } @SuppressWarnings("NonOverridingEquals") public boolean equals(Map.Entry e) { return (keyvalEquals(key, e.getKey()) && valEquals(getValue(), e.getValue())); } @Override public int hashCode() { V v; return (((key == null) ? 0 : keyHashCode(key)) ^ (((v = getValue()) == null) ? 0 : v.hashCode())); } } final public class EntrySet extends AbstractSet> { public Set> hashEntrySet = hash.entrySet(); @Override public Iterator> iterator() { return new Iterator>() { public Iterator> hashIterator = hashEntrySet.iterator(); public Map.Entry next = null; @Override public boolean hasNext() { while (hashIterator.hasNext()) { Map.Entry ent = hashIterator.next(); WeakKey wk = ent.getKey(); K k = null; if ((wk != null) && ((k = wk.get()) == null)) { continue; } next = new Entry(ent, k); return true; } return false; } @Override public Map.Entry next() { if ((next == null) && !hasNext()) throw new NoSuchElementException(); Map.Entry e = next; next = null; return e; } @Override public void remove() { hashIterator.remove(); } }; } @Override public boolean isEmpty() { return !(iterator().hasNext()); } @Override public int size() { int j = 0; for (Iterator> i = iterator(); i.hasNext(); i.next()) j++; return j; } @Override public boolean remove(Object o) { processQueue(); if (!(o instanceof Map.Entry)) return false; @SuppressWarnings("unchecked") Map.Entry e = (Map.Entry) o; Object ev = e.getValue(); WeakKey wk = WeakKeyCreate(e.getKey()); Object hv = hash.get(wk); if ((hv == null) ? ((ev == null) && hash.containsKey(wk)) : hv.equals(ev)) { hash.remove(wk); return true; } return false; } @Override public int hashCode() { int h = 0; for (Iterator> i = hashEntrySet.iterator(); i.hasNext(); ) { Map.Entry ent = i.next(); WeakKey wk = ent.getKey(); Object v; if (wk == null) continue; h += (wk.hashCode() ^ (((v = ent.getValue()) == null) ? 0 : v.hashCode())); } return h; } } public Set> entrySet = null; @Override public Set> entrySet() { if (entrySet == null) entrySet = new EntrySet(); return entrySet; } public K findKey(Object key) { processQueue(); K kkey = (K) key; WeakKey wkey = WeakKeyCreate(kkey); WeakKey found = hashMap_findKey(hash, wkey); return found == null ? null : found.get(); } } static public class TimedCache { public long timeout; volatile public A value; public Object function; public long set; public Lock lock = lock(); public boolean keepValueWhileCalculating = false; public int stores, fails, hits; public TimedCache(double timeoutSeconds, IF0 function) { this(function, timeoutSeconds); } public TimedCache(double timeoutSeconds, Object function) { this(function, timeoutSeconds); } public TimedCache(Object function, double timeoutSeconds) { this(timeoutSeconds); this.function = function; } public TimedCache(double timeoutSeconds) { timeout = toMS(timeoutSeconds); } public A set(A a) { Lock __0 = lock; lock(__0); try { ++stores; value = a; set = now(); return a; } finally { unlock(__0); } } public boolean has() { Lock __1 = lock; lock(__1); try { clean(); if (set != 0) { ++hits; return true; } ++fails; return false; } finally { unlock(__1); } } public A get() { Lock __2 = lock; lock(__2); try { if (function != null) return get(function); clean(); if (set != 0) ++hits; else ++fails; return value; } finally { unlock(__2); } } public A get(Object makerFunction) { Lock __3 = lock; lock(__3); try { if (keepValueWhileCalculating) { if (value == null || shouldClean()) set((A) callF(makerFunction)); return value; } else { return this.has() ? getNoClean() : set((A) callF(makerFunction)); } } finally { unlock(__3); } } public A getNoClean() { Lock __4 = lock; lock(__4); try { return value; } finally { unlock(__4); } } public void clean() { Lock __5 = lock; lock(__5); try { if (shouldClean()) clear(); } finally { unlock(__5); } } public boolean shouldClean() { return timeout > 0 && now() > set + timeout; } public void clear() { Lock __6 = lock; lock(__6); try { set = 0; value = null; } finally { unlock(__6); } } public String stats() { return "Stores: " + stores + ", hits: " + hits + ", fails: " + fails; } public A peek() { return value; } } static public class CompactLinkedHashSet extends AbstractSet { public UnsynchronizedCompactHashSet> entries = new UnsynchronizedCompactHashSet(); public Entry head, tail; static public class Entry { public A value; public Entry prev, next; public int hashCode() { return loadableUtils.utils.hashCode(value); } public boolean equals(Object o) { return o == this || eq(value, o); } } public boolean add(A a) { if (entries.contains(a)) return false; Entry n = new Entry(); n.value = a; n.prev = tail; if (tail != null) tail.next = n; tail = n; if (head == null) head = n; entries.add(n); return true; } public boolean remove(Object a) { return remove(entries.find(a)); } public boolean remove(Entry node) { if (node == null) return false; if (node.next != null) node.next.prev = node.prev; else tail = node.prev; if (node.prev != null) node.prev.next = node.next; else head = node.next; entries.remove(node); return true; } public int size() { return entries.size(); } public IterableIterator iterator() { return new IterableIterator() { public Entry entry = head, prev = null; public boolean hasNext() { return entry != null; } public A next() { A a = entry.value; prev = entry; entry = entry.next; return a; } public void remove() { if (prev == null) throw new IllegalStateException(); CompactLinkedHashSet.this.remove(prev); prev = null; } }; } public void clear() { entries.clear(); head = tail = null; } public boolean contains(Object a) { return entries.contains(a); } public A find(Object o) { Entry e = entries.find(o); return e == null ? null : e.value; } public A prevElement(A a) { Entry e = entries.find(a); if (e == null || e.prev == null) return null; return e.prev.value; } public A nextElement(A a) { Entry e = entries.find(a); if (e == null || e.next == null) return null; return e.next.value; } public A first() { return head == null ? null : head.value; } public A last() { return tail == null ? null : tail.value; } public boolean removeIfSame(Object o) { A value = find(o); if (value == o) { remove(value); return true; } return false; } } static abstract public class F3 { abstract public D get(A a, B b, C c); } static public class CompactHashSet extends java.util.AbstractSet { final static public int INITIAL_SIZE = 0; final static public int FIRST_INCREMENT = 3; public final static double LOAD_FACTOR = 0.75; final static public Object nullObject = new Object(); final static public Object deletedObject = new Object(); public int elements; public int freecells; public A[] objects; public int modCount; public CompactHashSet() { this(INITIAL_SIZE); } public CompactHashSet(int size) { objects = (A[]) (size == 0 ? emptyObjectArray() : new Object[size]); elements = 0; freecells = objects.length; modCount = 0; } public CompactHashSet(Collection c) { this(c.size()); addAll(c); } @Override public Iterator iterator() { return new CompactHashIterator(); } @Override public int size() { return elements; } @Override public boolean isEmpty() { return elements == 0; } @Override public boolean contains(Object o) { return find(o) != null; } synchronized public A find(Object o) { if (objects.length == 0) return null; if (o == null) o = nullObject; int hash = o.hashCode(); int index = (hash & 0x7FFFFFFF) % objects.length; int offset = 1; while (objects[index] != null && !(objects[index].hashCode() == hash && objects[index].equals(o))) { index = ((index + offset) & 0x7FFFFFFF) % objects.length; offset = offset * 2 + 1; if (offset == -1) offset = 2; } return objects[index]; } public boolean removeIfSame(Object o) { A value = find(o); if (value == o) { remove(value); return true; } return false; } @Override synchronized public boolean add(Object o) { if (objects.length == 0) rehash(FIRST_INCREMENT); if (o == null) o = nullObject; int hash = o.hashCode(); int index = (hash & 0x7FFFFFFF) % objects.length; int offset = 1; int deletedix = -1; while (objects[index] != null && !(objects[index].hashCode() == hash && objects[index].equals(o))) { if (objects[index] == deletedObject) deletedix = index; index = ((index + offset) & 0x7FFFFFFF) % objects.length; offset = offset * 2 + 1; if (offset == -1) offset = 2; } if (objects[index] == null) { if (deletedix != -1) index = deletedix; else freecells--; modCount++; elements++; objects[index] = (A) o; if (1 - (freecells / (double) objects.length) > LOAD_FACTOR) rehash(); return true; } else return false; } @Override synchronized public boolean remove(Object o) { if (objects.length == 0) return false; if (o == null) o = nullObject; int hash = o.hashCode(); int index = (hash & 0x7FFFFFFF) % objects.length; int offset = 1; while (objects[index] != null && !(objects[index].hashCode() == hash && objects[index].equals(o))) { index = ((index + offset) & 0x7FFFFFFF) % objects.length; offset = offset * 2 + 1; if (offset == -1) offset = 2; } if (objects[index] != null) { objects[index] = (A) deletedObject; modCount++; elements--; return true; } else return false; } @Override synchronized public void clear() { elements = 0; for (int ix = 0; ix < objects.length; ix++) objects[ix] = null; freecells = objects.length; modCount++; } @Override synchronized public Object[] toArray() { Object[] result = new Object[elements]; Object[] objects = this.objects; int pos = 0; for (int i = 0; i < objects.length; i++) if (objects[i] != null && objects[i] != deletedObject) { if (objects[i] == nullObject) result[pos++] = null; else result[pos++] = objects[i]; } return result; } @Override synchronized public T[] toArray(T[] a) { int size = elements; if (a.length < size) a = (T[]) java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size); A[] objects = this.objects; int pos = 0; for (int i = 0; i < objects.length; i++) if (objects[i] != null && objects[i] != deletedObject) { if (objects[i] == nullObject) a[pos++] = null; else a[pos++] = (T) objects[i]; } return a; } public void rehash() { int garbagecells = objects.length - (elements + freecells); if (garbagecells / (double) objects.length > 0.05) rehash(objects.length); else rehash(objects.length * 2 + 1); } public void rehash(int newCapacity) { int oldCapacity = objects.length; @SuppressWarnings("unchecked") A[] newObjects = (A[]) new Object[newCapacity]; for (int ix = 0; ix < oldCapacity; ix++) { Object o = objects[ix]; if (o == null || o == deletedObject) continue; int hash = o.hashCode(); int index = (hash & 0x7FFFFFFF) % newCapacity; int offset = 1; while (newObjects[index] != null) { index = ((index + offset) & 0x7FFFFFFF) % newCapacity; offset = offset * 2 + 1; if (offset == -1) offset = 2; } newObjects[index] = (A) o; } objects = newObjects; freecells = objects.length - elements; } public class CompactHashIterator implements Iterator { public int index; public int lastReturned = -1; public int expectedModCount; @SuppressWarnings("empty-statement") public CompactHashIterator() { synchronized (CompactHashSet.this) { for (index = 0; index < objects.length && (objects[index] == null || objects[index] == deletedObject); index++) ; expectedModCount = modCount; } } @Override public boolean hasNext() { synchronized (CompactHashSet.this) { return index < objects.length; } } @SuppressWarnings("empty-statement") @Override public T next() { synchronized (CompactHashSet.this) { int length = objects.length; if (index >= length) { lastReturned = -2; throw new NoSuchElementException(); } lastReturned = index; for (index += 1; index < length && (objects[index] == null || objects[index] == deletedObject); index++) ; if (objects[lastReturned] == nullObject) return null; else return (T) objects[lastReturned]; } } @Override public void remove() { synchronized (CompactHashSet.this) { if (modCount != expectedModCount) throw new ConcurrentModificationException(); if (lastReturned == -1 || lastReturned == -2) throw new IllegalStateException(); if (objects[lastReturned] != null && objects[lastReturned] != deletedObject) { objects[lastReturned] = (A) deletedObject; elements--; modCount++; expectedModCount = modCount; } } } } synchronized public int capacity() { return objects.length; } synchronized public boolean shrinkToFactor(double factor) { if (factor > LOAD_FACTOR) throw fail("Shrink factor must be equal to or smaller than load factor: " + factor + " / " + LOAD_FACTOR); int newCapacity = max(INITIAL_SIZE, iround(size() / factor)); if (newCapacity >= capacity()) return false; rehash(newCapacity); return true; } } static public class UnsynchronizedCompactHashSet extends java.util.AbstractSet { final static public int INITIAL_SIZE = 3; public final static double LOAD_FACTOR = 0.75; final static public Object nullObject = new Object(); final static public Object deletedObject = new Object(); public int elements; public int freecells; public A[] objects; public int modCount; public UnsynchronizedCompactHashSet() { this(INITIAL_SIZE); } public UnsynchronizedCompactHashSet(int size) { objects = (A[]) new Object[(size == 0 ? 1 : size)]; elements = 0; freecells = objects.length; modCount = 0; } public UnsynchronizedCompactHashSet(Collection c) { this(c.size()); addAll(c); } @Override public Iterator iterator() { return new CompactHashIterator(); } @Override public int size() { return elements; } @Override public boolean isEmpty() { return elements == 0; } @Override public boolean contains(Object o) { return find(o) != null; } public A find(Object o) { if (o == null) o = nullObject; int hash = o.hashCode(); int index = (hash & 0x7FFFFFFF) % objects.length; int offset = 1; while (objects[index] != null && !(objects[index].hashCode() == hash && objects[index].equals(o))) { index = ((index + offset) & 0x7FFFFFFF) % objects.length; offset = offset * 2 + 1; if (offset == -1) offset = 2; } return objects[index]; } public boolean removeIfSame(Object o) { A value = find(o); if (value == o) { remove(value); return true; } return false; } @Override public boolean add(Object o) { if (o == null) o = nullObject; int hash = o.hashCode(); int index = (hash & 0x7FFFFFFF) % objects.length; int offset = 1; int deletedix = -1; while (objects[index] != null && !(objects[index].hashCode() == hash && objects[index].equals(o))) { if (objects[index] == deletedObject) deletedix = index; index = ((index + offset) & 0x7FFFFFFF) % objects.length; offset = offset * 2 + 1; if (offset == -1) offset = 2; } if (objects[index] == null) { if (deletedix != -1) index = deletedix; else freecells--; modCount++; elements++; objects[index] = (A) o; if (1 - (freecells / (double) objects.length) > LOAD_FACTOR) rehash(); return true; } else return false; } @Override public boolean remove(Object o) { if (o == null) o = nullObject; int hash = o.hashCode(); int index = (hash & 0x7FFFFFFF) % objects.length; int offset = 1; while (objects[index] != null && !(objects[index].hashCode() == hash && objects[index].equals(o))) { index = ((index + offset) & 0x7FFFFFFF) % objects.length; offset = offset * 2 + 1; if (offset == -1) offset = 2; } if (objects[index] != null) { objects[index] = (A) deletedObject; modCount++; elements--; return true; } else return false; } @Override public void clear() { elements = 0; for (int ix = 0; ix < objects.length; ix++) objects[ix] = null; freecells = objects.length; modCount++; } @Override public Object[] toArray() { Object[] result = new Object[elements]; Object[] objects = this.objects; int pos = 0; for (int i = 0; i < objects.length; i++) if (objects[i] != null && objects[i] != deletedObject) { if (objects[i] == nullObject) result[pos++] = null; else result[pos++] = objects[i]; } return result; } @Override public T[] toArray(T[] a) { int size = elements; if (a.length < size) a = (T[]) java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size); A[] objects = this.objects; int pos = 0; for (int i = 0; i < objects.length; i++) if (objects[i] != null && objects[i] != deletedObject) { if (objects[i] == nullObject) a[pos++] = null; else a[pos++] = (T) objects[i]; } return a; } public void rehash() { int garbagecells = objects.length - (elements + freecells); if (garbagecells / (double) objects.length > 0.05) rehash(objects.length); else rehash(objects.length * 2 + 1); } public void rehash(int newCapacity) { int oldCapacity = objects.length; @SuppressWarnings("unchecked") A[] newObjects = (A[]) new Object[newCapacity]; for (int ix = 0; ix < oldCapacity; ix++) { Object o = objects[ix]; if (o == null || o == deletedObject) continue; int hash = o.hashCode(); int index = (hash & 0x7FFFFFFF) % newCapacity; int offset = 1; while (newObjects[index] != null) { index = ((index + offset) & 0x7FFFFFFF) % newCapacity; offset = offset * 2 + 1; if (offset == -1) offset = 2; } newObjects[index] = (A) o; } objects = newObjects; freecells = objects.length - elements; } public class CompactHashIterator implements Iterator { public int index; public int lastReturned = -1; public int expectedModCount; @SuppressWarnings("empty-statement") public CompactHashIterator() { for (index = 0; index < objects.length && (objects[index] == null || objects[index] == deletedObject); index++) ; expectedModCount = modCount; } @Override public boolean hasNext() { return index < objects.length; } @SuppressWarnings("empty-statement") @Override public T next() { int length = objects.length; if (index >= length) { lastReturned = -2; throw new NoSuchElementException(); } lastReturned = index; for (index += 1; index < length && (objects[index] == null || objects[index] == deletedObject); index++) ; if (objects[lastReturned] == nullObject) return null; else return (T) objects[lastReturned]; } @Override public void remove() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); if (lastReturned == -1 || lastReturned == -2) throw new IllegalStateException(); if (objects[lastReturned] != null && objects[lastReturned] != deletedObject) { objects[lastReturned] = (A) deletedObject; elements--; modCount++; expectedModCount = modCount; } } } public int capacity() { return objects.length; } public boolean shrinkToFactor(double factor) { if (factor > LOAD_FACTOR) throw fail("Shrink factor must be equal to or smaller than load factor: " + factor + " / " + LOAD_FACTOR); int newCapacity = max(INITIAL_SIZE, iround(size() / factor)); if (newCapacity >= capacity()) return false; rehash(newCapacity); return true; } } static public Method hashMap_findKey_method; static public A hashMap_findKey(HashMap map, Object key) { try { if (hashMap_findKey_method == null) hashMap_findKey_method = findMethodNamed(HashMap.class, "getNode"); Map.Entry entry = (Map.Entry) hashMap_findKey_method.invoke(map, hashMap_internalHash(key), key); return entry == null ? null : entry.getKey(); } catch (Exception __e) { throw rethrow(__e); } } static public String find(String pattern, String text) { Matcher matcher = Pattern.compile(pattern).matcher(text); if (matcher.find()) return matcher.group(1); return null; } static public A find(Collection c, Object... data) { for (A x : c) if (checkFields(x, data)) return x; return null; } static public int hashMap_internalHash(Object key) { int h; return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); } }