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

176
LINES

< > TinyBrain | #1007236 - Truth Table [WORKS]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Uses 1937K of libraries. Click here for Pure Java version (13119L/93K).

!7

extend Statement {
  Statement() {
    if (!_loading())
      originatingUniverse = myUniverse_cache != null ? myUniverse_cache : myUniverse();
  }
}

concept ImportedFrom {
  S snippetID;
  long lastTime;
}

static Q q;
static S myUniverse_cache;

sS hE = "English (machine-generated)";
sS hC = "Computer Language";

p {
  framesBot();
  db();
  truthBot();
  if (isHeadless()) {
    calcOnConceptChanges(1000, r { makeTranslations(false) }, true);
  } else {
    q = new Q(true);
    substance();
    swingMain();
  }
}

svoid swingMain swing {
  final SimpleCRUD<Statement> crud = new SimpleCRUD<Statement>(Statement) {
    L<S> fields() {
      ret prioritizeList(super.fields(), ll("text", "globalID"));
    }
  };
  crud.renderer = func(Statement s) { litorderedmap(hC, s.text, hE, s.possibleEnglishTranslation) };
  showConceptsTable_postProcess.set(f reversedList);
  showConceptsTable_afterUpdate.set(r {
    //print("Setting width");
    //tableSetColumnWidths_debug = true;
    //tableSetColumnWidths(crud.table, hC, 100);
    //crud.table.getColumnModel().getColumn(1).setWidth(100);
  });
  crud.show(programTitle());
  //swing { crud.table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); }
  
  addMenu(crud.panel, "Menu!",
    "Rebuild Translations", r {
      clearAICaches();
      q.add(r { makeTranslations(true) });
    });
  
  swing {
    crud.buttons.add(jbutton("Export", r { swingExportConcepts("Truth Table") }));
    crud.buttons.add(jbutton("Import...", f doImport));
  }
  addAISuiteMenu(crud.panel);
  q.add(r { makeTranslations(false) });
  awtCalcOnConceptChanges(crud.table, 500, r { makeTranslations(false) }, true);
  
  aiEnhancements();
  hideConsole();
}

svoid makeTranslations(bool all) {
  for (Statement s) {
    if (all || empty(s.possibleEnglishTranslation) || eq(s.possibleEnglishTranslation, "?"))
      cset(s, possibleEnglishTranslation := orQuestionMark2(f conceptLanguageToEnglish, s.text));
  }
}

svoid doImport {
  ImportedFrom last = last(sortedByField("lastTime", list(ImportedFrom)));
  final JTextField tf = jTextField(last == null ? #1007475 : last.snippetID);
  showFormTitled("Import Truth Table",
    "Snippet ID", tf, r-thread {
      loading {
        importFrom(getTextTrim(tf));
      }
    });
}

svoid importFrom(S snippetID)  {
  snippetID = fsI(snippetID);
  cset(uniq(ImportedFrom, +snippetID), lastTime := now());
  S text = loadSnippet(snippetID);
  Concepts newConcepts = new Concepts(snippetID).safeLoad(text);
  print(n(list(newConcepts, "Statement"), "statement") + " loaded from " + snippetID);
  
  Concepts myConcepts = mainConcepts;
  print("I have " + n(list(myConcepts, "Statement"), "statement");
  Map<S, Statement> myIDs = indexByField(list(myConcepts, Statement), "globalID");
  
  int nNew = 0;
  for (Concept c : list(newConcepts, "Statement")) {
    S id = getString(c, "globalID");
    bool has = myIDs.containsKey(id);
    print("ID: " + id + " - " + (has ? "I have it" : "I don't have it"));
    if (!has) {
      // Creating
      
      Statement c2 = cnew(Statement, importedFrom := snippetID, imported := now());
      importFields(c, c2);
      myIDs.put(id, c2);
      ++nNew;
    } else {
      // Updating
      
      Statement c2 = myIDs.get(id);
      if (c2.touched > c2.imported)
        print("  Statement touched locally! Not changing.");
      else {
        cset(c2, imported := now());
        int changes = importFields(c, c2);
        if (changes == 0)
          print("  No change.");
        else
          print("  Updated statement! " + n(changes, "fields") + " changed");
      }
    }
  }
  infoBox("Import done. " + n(nNew, "new statement"));
}

static int importFields(Concept c, Statement c2) {
  ret copyConceptFields(c, c2, "globalID", "text", "possibleEnglishTranslation", "originatingUniverse");
}

svoid truthBot {
  new Android3 android;
  android.greeting = "Truth Table Bot.";
  android.console = false;
  android.responder = new Responder {
    S answer(S s, L<S> history) {
      new Matches m;
      if "add truth * by *" {
        L<S> statements = cast unstructure($1);
        S importedFrom = $2;
        int added = 0;
        myUniverse_cache = myUniverse();
        try {
          for (S text : statements) {
            text = trim(text);
            Statement st = conceptWhere(Statement, +text);
            if (st == null) {
              cnew(Statement, +text, +importedFrom, imported := now(),
                possibleEnglishTranslation := conceptLanguageToEnglish(text));
              ++added;
            }
          }
        } finally {
          myUniverse_cache = null;
        }
        ret "OK, added " + n(added, "statements");
      }
      
      if "import from *" {
        importFrom(formatSnippetID($1));
        ret "OK";
      }
      
      if "export" {
        swingExportConcepts_impl("Truth Table");
        ret "OK, " + exportSnippetID;
      }
      
      null;
    }
  };
  makeBot(android);
}

Author comment

Began life as a copy of #1007165

download  show line numbers  debug dex   

Travelled to 8 computer(s): bvmoasoxxqgd, cfunsshuasjs, fehiwqupcyrn, jtubtzbbkimh, onxytkatvevr, tvejysmllsmz, wtqryiryparv, xinetxnxrdbb

No comments. add comment

Snippet ID: #1007236
Snippet name: Truth Table [WORKS]
Eternal ID of this version: #1007236/77
Text MD5: b61ae7278a5fc2eabbd2aabc1f3832cf
Transpilation MD5: 5662bb82e7c42c777d7b8b4ee780e15e
Author: stefan
Category: javax / a.i.
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-06-05 13:36:04
Source code size: 5149 bytes / 176 lines
Pitched / IR pitched: No / No
Views / Downloads: 271 / 2196
Version history: 76 change(s)
Referenced in: [show]