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

175
LINES

< > BotCompany Repo | #1007236 // Truth Table [WORKS]

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

Download Jar. Uses 3874K of libraries. Click here for Pure Java version (13119L/92K).

1  
!7
2  
3  
extend Statement {
4  
  Statement() {
5  
    if (!_loading())
6  
      originatingUniverse = myUniverse_cache != null ? myUniverse_cache : myUniverse();
7  
  }
8  
}
9  
10  
concept ImportedFrom {
11  
  S snippetID;
12  
  long lastTime;
13  
}
14  
15  
static Q q;
16  
static S myUniverse_cache;
17  
18  
sS hE = "English (machine-generated)";
19  
sS hC = "Computer Language";
20  
21  
p {
22  
  framesBot();
23  
  db();
24  
  truthBot();
25  
  if (isHeadless()) {
26  
    calcOnConceptChanges(1000, r { makeTranslations(false) }, true);
27  
  } else {
28  
    q = new Q(true);
29  
    substance();
30  
    swingMain();
31  
  }
32  
}
33  
34  
svoid swingMain swing {
35  
  final SimpleCRUD<Statement> crud = new SimpleCRUD<Statement>(Statement) {
36  
    L<S> fields() {
37  
      ret prioritizeList(super.fields(), ll("text", "globalID"));
38  
    }
39  
  };
40  
  crud.renderer = func(Statement s) { litorderedmap(hC, s.text, hE, s.possibleEnglishTranslation) };
41  
  showConceptsTable_postProcess.set(f reversedList);
42  
  showConceptsTable_afterUpdate.set(r {
43  
    //print("Setting width");
44  
    //tableSetColumnWidths_debug = true;
45  
    //tableSetColumnWidths(crud.table, hC, 100);
46  
    //crud.table.getColumnModel().getColumn(1).setWidth(100);
47  
  });
48  
  crud.show(programTitle());
49  
  //swing { crud.table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); }
50  
  
51  
  addMenu(crud.panel, "Menu!",
52  
    "Rebuild Translations", r {
53  
      clearAICaches();
54  
      q.add(r { makeTranslations(true) });
55  
    });
56  
  
57  
  swing {
58  
    crud.buttons.add(jbutton("Export", r { swingExportConcepts("Truth Table") }));
59  
    crud.buttons.add(jbutton("Import...", f doImport));
60  
  }
61  
  addAISuiteMenu(crud.panel);
62  
  q.add(r { makeTranslations(false) });
63  
  awtCalcOnConceptChanges(crud.table, 500, r { makeTranslations(false) }, true);
64  
  
65  
  aiEnhancements();
66  
  hideConsole();
67  
}
68  
69  
svoid makeTranslations(bool all) {
70  
  for (Statement s) {
71  
    if (all || empty(s.possibleEnglishTranslation) || eq(s.possibleEnglishTranslation, "?"))
72  
      cset(s, possibleEnglishTranslation := orQuestionMark2(f conceptLanguageToEnglish, s.text));
73  
  }
74  
}
75  
76  
svoid doImport {
77  
  ImportedFrom last = last(sortedByField("lastTime", list(ImportedFrom)));
78  
  final JTextField tf = jTextField(last == null ? #1007475 : last.snippetID);
79  
  showFormTitled("Import Truth Table",
80  
    "Snippet ID", tf, r-thread {
81  
      loading {
82  
        importFrom(getTextTrim(tf));
83  
      }
84  
    });
85  
}
86  
87  
svoid importFrom(S snippetID)  {
88  
  snippetID = fsI(snippetID);
89  
  cset(uniq(ImportedFrom, +snippetID), lastTime := now());
90  
  S text = loadSnippet(snippetID);
91  
  Concepts newConcepts = new Concepts(snippetID).safeLoad(text);
92  
  print(n(list(newConcepts, "Statement"), "statement") + " loaded from " + snippetID);
93  
  
94  
  Concepts myConcepts = mainConcepts;
95  
  print("I have " + n(list(myConcepts, "Statement"), "statement");
96  
  Map<S, Statement> myIDs = indexByField(list(myConcepts, Statement), "globalID");
97  
  
98  
  int nNew = 0;
99  
  for (Concept c : list(newConcepts, "Statement")) {
100  
    S id = getString(c, "globalID");
101  
    bool has = myIDs.containsKey(id);
102  
    print("ID: " + id + " - " + (has ? "I have it" : "I don't have it"));
103  
    if (!has) {
104  
      // Creating
105  
      
106  
      Statement c2 = cnew(Statement, importedFrom := snippetID, imported := now());
107  
      importFields(c, c2);
108  
      myIDs.put(id, c2);
109  
      ++nNew;
110  
    } else {
111  
      // Updating
112  
      
113  
      Statement c2 = myIDs.get(id);
114  
      if (c2.touched > c2.imported)
115  
        print("  Statement touched locally! Not changing.");
116  
      else {
117  
        cset(c2, imported := now());
118  
        int changes = importFields(c, c2);
119  
        if (changes == 0)
120  
          print("  No change.");
121  
        else
122  
          print("  Updated statement! " + n(changes, "fields") + " changed");
123  
      }
124  
    }
125  
  }
126  
  infoBox("Import done. " + n(nNew, "new statement"));
127  
}
128  
129  
static int importFields(Concept c, Statement c2) {
130  
  ret copyConceptFields(c, c2, "globalID", "text", "possibleEnglishTranslation", "originatingUniverse");
131  
}
132  
133  
svoid truthBot {
134  
  new Android3 android;
135  
  android.greeting = "Truth Table Bot.";
136  
  android.console = false;
137  
  android.responder = new Responder {
138  
    S answer(S s, L<S> history) {
139  
      new Matches m;
140  
      if "add truth * by *" {
141  
        L<S> statements = cast unstructure($1);
142  
        S importedFrom = $2;
143  
        int added = 0;
144  
        myUniverse_cache = myUniverse();
145  
        try {
146  
          for (S text : statements) {
147  
            text = trim(text);
148  
            Statement st = conceptWhere(Statement, +text);
149  
            if (st == null) {
150  
              cnew(Statement, +text, +importedFrom, imported := now(),
151  
                possibleEnglishTranslation := conceptLanguageToEnglish(text));
152  
              ++added;
153  
            }
154  
          }
155  
        } finally {
156  
          myUniverse_cache = null;
157  
        }
158  
        ret "OK, added " + n(added, "statements");
159  
      }
160  
      
161  
      if "import from *" {
162  
        importFrom(formatSnippetID($1));
163  
        ret "OK";
164  
      }
165  
      
166  
      if "export" {
167  
        swingExportConcepts_impl("Truth Table");
168  
        ret "OK, " + exportSnippetID;
169  
      }
170  
      
171  
      null;
172  
    }
173  
  };
174  
  makeBot(android);
175  
}

Author comment

Began life as a copy of #1007165

download  show line numbers  debug dex  old transpilations   

Travelled to 23 computer(s): aoiabmzegqzx, bhatertpkbcr, bvmoasoxxqgd, cbybwowwnfue, cfunsshuasjs, fauvpijqivwj, fehiwqupcyrn, gwrvuhgaqvyk, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, nsosnbthvwzj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, vpdwwinrgdga, wtqryiryparv, xeobevbjagfg, 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 / 175 lines
Pitched / IR pitched: No / No
Views / Downloads: 835 / 4448
Version history: 76 change(s)
Referenced in: [show references]