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

119
LINES

< > BotCompany Repo | #1021352 // Gazelle: Rules [Dyn Module]

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

Libraryless. Click here for Pure Java version (20104L/149K).

!7

concept Rule {
  S globalID = aGlobalIDUnlessLoading();
  S text;
  S comments; // used by rule engine
  S notes; // for humans only

  toString {
    ret globalID + ": " + escapeNewLines(rtrim(text));
  }
  
  void change {
    super.change();
    vmBus_send('gazelleRuleChanged, this);
  }
  
  void delete {
    vmBus_send('gazelleRuleDeleted, this);
    super.delete();
  }
}

// Bug when making cmodule
module Rules > DynCRUD<Rule> {
  transient CalculatedConceptFieldIndex<Rule, S> byPurpose;

  enhanceFrame { internalFrameIcon(f, gazelle_frameIcon()); }
  
  start {
    dbIndexing(Rule, 'globalID);
    byPurpose = new CalculatedConceptFieldIndex<Rule, S>(Rule, r -> gazelle_purposeFromComments(r.comments));
    
    addCountToName();
    crud.multiLineFields = litset('text, 'comments);
    crud.dontDuplicateFields = litset('globalID);
    crud.renderer = func(Rule r) -> Map { litorderedmap("Rule ID" := r.globalID, text := joinWithSpace(tlft(r.text)), comments := escapeNewLines(r.comments)) };
    crud.latestFirst = true;
    onConceptsChange(r { vmBus_sendMessage('gazelleRulesChanged, this) });
    conceptsObject().onAllChanged.add(r {
      vmBus_sendMessage('gazelleRulesGlobalChange, this)
    });
    
    // do some logic stuff here for now
    dm_vmBus_onMessage_q('gazelleRuleCreated, voidfunc(O mod, O rule) {
      S ruleID = getString globalID(rule);
      print("wiring statement conditions for new rule: " + ruleID);
      dm_gazelle_wireStatementConditions(ruleID);
    });
  }
  
  // API
  
  Pair<S, Bool> addRule(S when, S then) {
    ret addRule(when + "\n=> " + then);
  }
  
  Pair<S, Bool> addRule(S text) {
    ret addRuleWithComment(text, null);
  }
  
  Pair<S, Bool> addRuleWithComment(S text, S comment) {
    Pair<Rule, Bool> p = uniq2_sync(Rule,
      text := rtrim(text),
      comments := nullIfEmpty(rtrim(comment)));
    if (p.b) vmBus_sendMessage('gazelleRuleCreated, this, p.a);
    ret pair(p.a.globalID, p.b);
  }
  
  PairS textAndCommentForRule(S ruleID) {
    Rule r = conceptWhere Rule(globalID := ruleID);
    ret r == null ? null : pair(r.text, r.comments);
  }
  
  void setRuleText(S ruleID, S text) {
    cset(conceptWhere Rule(globalID := ruleID), +text);
  }
  
  void setComments(S ruleID, S comments) {
    cset(conceptWhere Rule(globalID := ruleID), +comments);
  }
  
  void addComments(S ruleID, S comments) {
    Rule r = conceptWhere Rule(globalID := ruleID);
    if (r != null)
      cset(r, comments := lines_rtrim(uniquifyList(concatLists(lines(r.comments), tlft(comments)))));
  }
  
  void removeComment(S ruleID, S comment) {
    Rule r = conceptWhere Rule(globalID := ruleID);
    if (r == null) ret;
    cset(r, comments := lines_rtrim(listWithout(lines(r.comments), comment)));
  }
  
  S latestRuleID() {
    ret getString globalID(last(concepts()));
  }
  
  void deleteRule(S ruleID) {
    deleteConceptsWhere Rule(globalID := ruleID);
  }
  
  void activateRule(S ruleID) {
    dm_activateOSAndModule(); // TODO: wait
    if (crud.table == null) ret;
    Rule r = conceptWhere Rule(globalID := ruleID);
    if (r == null) ret;
    selectRowIfNotNegative(crud.table, crud.indexOfConcept(r));
  }
  
  // experimental
  L<Rule> rulesForPurpose(S purpose) {
    ret byPurpose.getAll(purpose);
  }
  
  LS rulesWithText(S text) {
    ret collect globalID(conceptsWhere Rule(+text));
  }
}

Author comment

Began life as a copy of #1019783

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1021352
Snippet name: Gazelle: Rules [Dyn Module]
Eternal ID of this version: #1021352/50
Text MD5: f25997742a76c607f256db92c6db1f98
Transpilation MD5: b09ec3aab6f5fab1994d49a06abb0d89
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-03-22 15:10:39
Source code size: 3492 bytes / 119 lines
Pitched / IR pitched: No / No
Views / Downloads: 421 / 7013
Version history: 49 change(s)
Referenced in: [show references]