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

155
LINES

< > BotCompany Repo | #1021468 // Test Gazelle On Input v3 [with tree]

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

Uses 911K of libraries. Click here for Pure Java version (18030L/101K).

!7

set flag DynModule.

sclass GazelleTree {
  L<GazelleTree> children;
  GazelleTree parent;
  double weight = 1, totalWeight;
  S line, lineType;
  S prediction, judgement;
  RuleEngine2_MatchedRule mr;
  
  transient EvalContext ctx;

  *() {}
  *(S *line) {}
  
  toString {
    new LS l;
    if (mr != null)
      for (S s : unnull(mr.remainingConditions))
        l.add("if: " + s);
    addIfNempty(l, prediction);
    if (mr != null)
      l.add(mr.qualitySum() + " / " + formatDouble(mr.relativeQuality(), 2));
    ret line + appendSquareBracketed(joinWithComma(l));
  }
}

sclass EvalContext {
  RuleEngine2 engine;
  Map<S, ai_gazelle_analyzeStatementsForRule_Data> dataByRule;
}

cmodule TestRuleEngine2 > DynSCP {
  S input, processedInput;
  transient ReliableSingleThread rstCalc = dm_rstWithPostDelay(this, r calc, 500);
  transient int maxResults = 500;
  transient bool skipBadPredictions = true;
  transient JTree jTree;
  transient GazelleTree root;
  transient double qualityCutOff = -0.75;
  
  S switchableFields() { ret "skipBadPredictions maxResults"; }
  
  start {
    dm_vmBus_onMessage('gazelleRuleCreated, rstCalc);
  }
  
  visualize {
    jTree = jDynamicTree(null, func(GazelleTree tree) -> L<GazelleTree> { gt_getChildren(tree) }, makeChildrenIsFast := true);
    
    componentPopupMenu(jTree, voidfunc(JPopupMenu menu) {
      MouseEvent evt = componentPopupMenu_getEvent();
      TreePath path = jTree.getPathForLocation(evt.getX(), evt.getY());
      if (path == null) ret;
      DefaultMutableTreeNode node = cast path.getLastPathComponent();
      final GazelleTree e = cast node.getUserObject();
      jTree.setSelectionPath(path);
      
      print("Selected: " + e);
      addMenuItem(menu, "Copy line", rThread { copyTextToClipboard(e.line) });
      addMenuItem(menu, "Make rule...", rThread {
        S s = e.line;
        GazelleTree ee = e;
        while ((ee = ee.parent) != null)
          s = ee.line + "\n+ " + s;
        dm_gazelle_newRuleDialog_2(s);
      });
    });
    
    JComponent tf = dm_textField('input);
    onChangeAndNow(tf, rstCalc);
    ret northCenterAndSouthWithMargins(
      centerAndEastWithMargin(
        tf,
        jbutton("Make rule...", rThread { dm_gazelle_newRuleDialog_2(input) })),
      super.visualize(),
      rightAlignedButtons(
        treeDependentButton(jTree, "Mark good", rMark('good)),
        treeDependentButton(jTree, "Mark bad", rMark('bad)),
        treeDependentButton(jTree, "Mark funny", rMark('funny))
      ));
  }
  
  visualize2 {
    ret jscroll(jTree);
  }
  
  Runnable rMark(fS judgement) {
    ret rThread {
      GazelleTree e = selected();
      e.judgement = judgement;
      jTree_fireUserObjectChanged(jTree, e);
      saveEntry(e);
    };
  }
  
  GazelleTree selected() {
    ret (GazelleTree) jtree_selectedUserObject(jTree);
  }
  
  void calc {
    S input = this.input;
    root = new GazelleTree(input);
    jTree_setRootObject(jTree, root);
    print("Replaced root on " + isShowing(jTree) + " " + jTree);
    jtree_collapseAndOpenRootNode(jTree);
    processedInput = input;
  }
  
  L<GazelleTree> gt_getChildren(GazelleTree tree) {
    if (tree == null) null;
    if (tree.children != null) ret tree.children;
    
    // make rule engine etc.
    if (tree.ctx == null) {
      new EvalContext ctx;
      tree.ctx = ctx;
      ctx.engine = new RuleEngine2;
      ctx.engine.addRules2(dm_allRulesFromRulesModuleWithCommentsAndIDs());
      
      // gather data for predictor
      MultiMap<S> statementsByRule = ai_gazelle_indexStatementsByRule(dm_gazelle_statementsFromAppliedRules());
      ctx.dataByRule = mapValues ai_gazelle_analyzeStatementsForRule(multiMapToMap(statementsByRule));
    }
    EvalContext ctx = tree.ctx;
    
    new L<GazelleTree> children;
    L<RuleEngine2_MatchedRule> l = sortByMethodDesc qualitySum(ai_ruleEngine2_rulesForInput_3(tree.ctx.engine, tree.line));
    for (RuleEngine2_MatchedRule r : l) {
      if (r.relativeQuality() < qualityCutOff) continue;
      S line = r.applyMappingTo(r.rule.out);
      
      GazelleTree child = new(line);
      child.parent = tree;
      child.ctx = tree.ctx;
      child.mr = r;
      
      ai_gazelle_analyzeStatementsForRule_Data data = ctx.dataByRule.get(r.rule.globalID);
      child.prediction = data == null ? null : ai_gazelle_predictor1_onData(r, data);
      children.add(child);
    }
    sortByCalculatedFieldDesc(children, func(GazelleTree c) -> int { ai_goodBadToInt(c.prediction) });
    ret tree.children = children;
  }
  
  void saveEntry(GazelleTree e) {
    S modifiedRule = e.ctx.engine.formatForUser(e.mr.applyMapping().asText());
    dm_gazelle_saveAppliedRule(
      +modifiedRule,
      judgement := e.judgement,
      matchedRuleStruct := struct(e.mr));
  }
}

Author comment

Began life as a copy of #1021407

download  show line numbers  debug dex  old transpilations   

Travelled to 8 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1021468
Snippet name: Test Gazelle On Input v3 [with tree]
Eternal ID of this version: #1021468/53
Text MD5: 6490435effc9436a27111a7e88356709
Transpilation MD5: ec09ba483958d3bfcb6e44879b372d20
Author: stefan
Category: javax / a.i.
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-02-15 04:51:15
Source code size: 4943 bytes / 155 lines
Pitched / IR pitched: No / No
Views / Downloads: 307 / 835
Version history: 52 change(s)
Referenced in: [show references]