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

131
LINES

< > BotCompany Repo | #1027557 // Scenarios DB [backup v1]

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

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

!7

concept Scenario {
  S name;
  LS conditions;
  LS actions;

  toString { ret "Scenario " + quote(name); }
}

cmodule Scenarios > DynCRUD<Scenario> {
  transient Scenario selected;
  new L<Undo> undoList;

  abstract class Undo { abstract S undo(); }
  
  record UndoCreateConcept(long conceptID) > Undo {
    toString { ret "Create " + getConcept(conceptID); }
    
    S undo() {
      Concept c = getConcept(conceptID);
      S result = "Deleted " + c + " again";
      deleteConcept(conceptID);
      ret result;
    }
  }

  record UndoDeleteConcept(Concept concept) > Undo {
    toString { ret "Delete " + concept; }
    
    S undo() {
      registerConcept(concept);
      ret "Restored " + concept;
    }
  }

  record UndoSetConceptField(long conceptID, S field, O value, O newValue) > Undo {
    toString { ret "Set " + field + " of " + getConcept(conceptID) + " to " + newValue; }
    
    S undo() {
      Concept c = getConcept(conceptID);
      S result = "Reset " + field + " of " + c + " to " + value;
      cset(c, field, value);
      ret result;
    }
  }

  Scenario findConceptWithName(S name) {
    ret conceptWhereCI Scenario(+name);
  }
  
  S answer(S s) {
    ret dm_q(() -> {
      print("answering: " + s);
      new Matches m;
      if "undo" ret singleUndo();
      if "anything to undo"
        ret empty(undoList) ? "Nothing to undo" : n2(undoList, "undoable action");
      if "how many scenarios"
        ret str(countConcepts(Scenario));
      if "delete scenario *" {
        S name = $1;
        setField(selected := null);
        Scenario sc = findConceptWithName(name);
        if (sc == null)
 ret format("Scenario * not found", name);
        unregisterConcept(sc);
        addUndo(new UndoDeleteConcept(sc));
        ret format("Scenario * deleted", name);
      }
      if "new scenario *" {
        S name = $1;
        Scenario sc = conceptWhereCI Scenario(+name);
        if (sc != null)
          ret format("Scenario * exists", name);
        setField(selected := uniqCI Scenario(+name));
        addUndo(new UndoCreateConcept(selected.id));
        ret format("Scenario * created", name);
      }
      if "list scenarios"
        ret or2(joinWithComma(collect name(list(Scenario))), "No scenarios defined");
      if "rename scenario * to *" {
        S name1 = $1, name2 = $2;
        if (!eqic(name1, name2)
          && conceptWhereCI Scenario(name := name2) != null)
          ret format("A scenario named * exists", name2);
        Scenario sc = findConceptWithName(name1);
        if (sc == null)
          ret format("Scenario * not found", name1);
        addUndo(new UndoSetConceptField(sc.id, 'name, name1, name2));
        cset(sc, name := name2);
        ret format("Scenario * renamed to *", name1, name2);
      }

      null;
    });
  }

  void addUndo(Undo undo) {
    if (undo == null) ret;
    undoList.add(undo);
    change();
    print("Undo added: " + undo);
  }

  afterVisualize {
    addComponent(crud.buttons, jbutton("Talk to me", rThread talkToMe));
  }

  void talkToMe enter { dm_showConversationPopupForModule(); }

  void clearUndos q {
    if (empty(undoList)) ret;
    undoList.clear();
    change;
    print("Undo list cleared");
  }

  S singleUndo() {
    Undo undo = popLast(undoList);
    if (undo == null) ret "Nothing to undo";
    change();
    ret or2(undo.undo(), "Last action undone");
  }

  // API

  Undo lastUndo() {
    ret dm_q(() -> last(undoList));
  }
}

Author comment

Began life as a copy of #1027443

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1027557
Snippet name: Scenarios DB [backup v1]
Eternal ID of this version: #1027557/1
Text MD5: a329ddf7d732ca9a81e97dce13248725
Transpilation MD5: c2ea18628812f53776571337635023be
Author: stefan
Category: javax / scenarios
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-03-23 21:33:59
Source code size: 3595 bytes / 131 lines
Pitched / IR pitched: No / No
Views / Downloads: 158 / 236
Referenced in: [show references]