OSDN Git Service

MGCanvasにマウス選択・移動機能のテストを追加。
[chnosproject/AI004.git] / ai.js
1 //AI004
2
3 //開発時クロスドメイン許可で起動するには、
4 // /Applications/Google\ Chrome.app/ --allow-file-access-from-files --disable-web-security
5
6 var IsDebugModeEnabledOnBoot = true;
7
8 function AI(messageBoxDOMObject, debugBoxDOMObject){
9         //ブラウザチェック
10         this.checkBrowser();
11         //サブクラス
12         this.input = new AI_Input(this);
13         this.wordRecognition = new AI_WordRecognition(this);
14         this.IOManager = new AI_IOManager(this);
15         this.networkManager = new AI_NetworkManager(this);
16         this.memory = new AI_Memory(this);
17         this.think = new AI_Think(this);
18         //出力関連
19         var that = this;
20         this.tickTimer = window.setInterval(function(){that.tick();}, 100);;
21         this.messageBox = null;
22         this.messageBoxBuffer = "";
23         this.maxMessageStringLength = 0xffff;
24         this.debugBox = null;
25         this.debugBoxBuffer = "";
26         this.maxDebugStringLength = 0xffff;
27         this.downloadBox = null;
28         //
29         this.setMessageBoxDOMObject(messageBoxDOMObject);
30         this.setDebugBoxDOMObject(debugBoxDOMObject);
31         this.modeList = [
32                 this.UUID_Mode_Standard,
33                 this.UUID_Mode_ReadMemory,
34                 this.UUID_Mode_SaveMemory,
35                 this.UUID_Mode_InternalConsole,
36                 this.UUID_Mode_CompileELCHNOS_OSECPU,
37         ];
38         this.modeProcessList = [
39                 this.inputProcess_Standard,
40                 this.inputProcess_ReadMemory,
41                 this.inputProcess_SaveMemory,
42                 this.inputProcess_InternalConsole,
43                 this.inputProcess_CompileELCHNOS_OSECPU,
44         ];
45         if(!IsDebugModeEnabledOnBoot){
46                 this.mode = this.UUID_Mode_Standard;
47                 this.processByMode = this.inputProcess_Standard;
48         } else{
49                 this.mode = this.UUID_Mode_InternalConsole;
50                 this.processByMode = this.inputProcess_InternalConsole;
51         }
52
53         //初期データの読み込み
54         AI_Bootstrap(this);
55
56         this.debug("AI system initialized.\n");
57         this.debug("To enter internal console mode,\n  type '#4ca6ed1a-e62e-470b-9d7b-e332f709e48f'.\n");
58 }
59 AI.prototype = {
60         UUIDStrLen: 36,
61         UUID_Mode_Standard:                                     "1186f6f2-c7c4-4532-8f8f-c7dea883825f",
62         UUID_Mode_ReadMemory:                           "42e11880-62b8-46ea-a1c4-481264d4440d",
63         UUID_Mode_SaveMemory:                           "52360c62-6a8a-4f6e-8bdd-43381996e996",
64         UUID_Mode_InternalConsole:                      "4ca6ed1a-e62e-470b-9d7b-e332f709e48f",
65         UUID_Mode_CompileELCHNOS_OSECPU:        "17ddde48-7d4c-498f-98d8-3e73f8845028",
66         UUID_Meaning_UndefinedString :          "f9080ed9-1fd4-4982-a979-092d1852298a",
67         UUID_Meaning_UndefinedStrings :         "24393cc6-e6c6-4da2-ae19-8e74ff71d390",
68         sendToAI: function(str, srctype){
69                 var p, strbaseindex;
70                 
71                 this.debug("**** Start inputting ****\n");
72                 
73                 //まず入力文字を#で分割したリストを取得
74                 var ary = str.split("#");
75                 //モードを確認しつつ実行
76                 p = -(this.UUIDStrLen + 1);
77                 for(;;){
78                         strbaseindex = p + 1 + this.UUIDStrLen;
79                         p = str.indexOf("#", strbaseindex);
80                         if(p == -1){
81                                 //終端まで到達
82                                 //それ以前の文字列を入力
83                                 this.processByMode(str.substring(strbaseindex), srctype);
84                                 break;
85                         }
86                         //まずはモード変更の直前までの文字列を入力
87                         this.processByMode(str.substring(strbaseindex, p), srctype);
88                         //モード変更要求
89                         this.changeMode(str.substring(p + 1, p + 1 + this.UUIDStrLen));
90                 }
91                 
92                 this.debug("**** End inputting ****\n");
93         },
94         changeMode: function(modeUUIDStr){
95                 for(var i = 0, iLen = this.modeList.length; i < iLen; i++){
96                         if(this.modeList[i] == modeUUIDStr){
97                                 break;
98                         }
99                 }
100                 if(this.modeList.length <= i){
101                         this.debug("Unknown mode-UUID. Mode was NOT changed.\n");
102                         return;
103                 }
104                 if(this.modeList[i] === this.UUID_Mode_SaveMemory){
105                         this.debug("Mode exectute for this once.\n    uuid:" + this.modeList[i] + "\n");
106                         this.modeProcessList[i].call(this);
107                 } else{
108                         this.debug("Mode changed\n    from:" + this.mode + "\n    to  :" + this.modeList[i] + "\n");
109                         this.mode = this.modeList[i];
110                         this.processByMode = this.modeProcessList[i];
111                 }
112                 
113         },
114         sendTextFromFileToAI: function(str, name, modDate, srctype){
115                 //ファイルからの読み込み時は、読み込み終了後に読み込み以前のモードに戻る。
116                 this.debug("sendTextFromFileToAI:");
117                 if(modDate){
118                         this.debug(modDate.toLocaleString());
119                 }
120                 this.debug(" [" + name + "]\n");
121                 var oldmode = this.mode;
122                 this.sendToAI(str, srctype);
123                 this.changeMode(oldmode);
124         },
125         setMessageBoxDOMObject: function(mBoxObj){
126                 this.messageBox = mBoxObj;
127         },
128         setDebugBoxDOMObject: function(dBoxObj){
129                 this.debugBox = dBoxObj;
130         },
131         message: function(str, noPrefix){
132                 if(this.messageBox){
133                         if(!noPrefix){
134                                 this.messageBoxBuffer += "AI> " + str;
135                         } else{
136                                 this.messageBoxBuffer += str;
137                         }
138                 }
139         },
140         debug: function(str){
141                 if(this.debugBox){
142                         this.debugBoxBuffer += str;
143                 }
144         },
145         tick: function(){
146                 //思考処理
147                 this.think.tick();
148                 //表示処理
149                 if(this.messageBox && this.messageBoxBuffer != ""){
150                         //messageBox
151                         var str = this.messageBox.value + this.messageBoxBuffer;
152                         this.messageBoxBuffer = "";
153                         if(str.length > this.maxMessageStringLength){
154                                 str = str.slice(str.length - (this.maxMessageStringLength >> 1));
155                         }
156                         this.messageBox.value = str;
157                         this.messageBox.scrollTop = this.messageBox.scrollHeight;
158                 }
159                 if(this.debugBox && this.debugBoxBuffer != ""){
160                         //debugBox
161                         var str = this.debugBox.value + this.debugBoxBuffer;
162                         this.debugBoxBuffer = "";
163                         if(str.length > this.maxDebugStringLength){
164                                 str = str.slice(str.length - (this.maxDebugStringLength >> 1));
165                         }
166                         this.debugBox.value = str;
167                         this.debugBox.scrollTop = this.debugBox.scrollHeight;
168                 }
169         },
170         checkBrowser: function(){
171                 if(!window.File){
172                         this.message("System> このブラウザは記憶保存(HTML5FileAPI)に対応していません。", true);
173                 }
174         },
175         inputProcess_Standard: function(str, srctype){
176                 this.debug("**** Start Processing (Standard) ****\n");
177                 
178                 this.debug("input:[" + str + "]\n");
179                 this.input.appendInput(str, srctype);
180                 
181                 this.debug("**** End Processing (Standard) ****\n");
182         },
183         inputProcess_ReadMemory: function(str, srctype){
184                 this.debug("**** Start Processing (ReadMemory) ****\n");
185                 
186                 this.memory.loadMemory(str);
187                 
188                 this.debug("**** End Processing (ReadMemory) ****\n");
189         },
190         inputProcess_SaveMemory: function(str, srctype){
191                 this.debug("**** Start Processing (SaveMemory) ****\n");
192                 
193                 this.memory.saveMemory();
194                 
195                 this.debug("**** End Processing (SaveMemory) ****\n");
196         },
197         inputProcess_InternalConsole: function(str, srctype){
198                 var that = this;
199                 this.debug("**** Start Processing (InternalConsole) ****\n");
200                 if(str == "exit"){
201                         this.debug("Exit InternalConsole.\n");
202                         this.changeMode(this.UUID_Mode_Standard);
203                 } else if(str == "show cwl"){
204                         //show candidateWordList
205                         this.wordRecognition.debugShowCandidateWordList();
206                 } else if(str == "show wl"){
207                         //show wordList
208                         this.debug("wordList:" + this.memory.wordList.length + "\n" );
209                         this.memory.wordList.logEachPropertyNamed("str", function(s){ that.debug(s); });
210                 } else if(str == "show rlc"){
211                         //show ReadLineCount
212                         this.debug(this.memory.dbInfo.readLineCount + " lines was inputted.\n" );
213                 } else if(str.indexOf("inputFromURL ") == 0){
214                         //webページを読み込む
215                         //inputFromURL http://www.aozora.gr.jp/cards/000148/files/773_14560.html
216                         //inputFromURL http://www.aozora.gr.jp/cards/000035/files/1567_14913.html
217                         //inputFromURL http://www.aozora.gr.jp/cards/000148/files/752_14964.html
218                         //inputFromURL http://www.aozora.gr.jp/cards/000035/files/301_14912.html
219                         //inputFromURL http://www.aozora.gr.jp/cards/000160/files/3368_25725.html
220                         //inputFromURL http://ja.wikipedia.org/wiki/%E3%83%A1%E3%82%A4%E3%83%B3%E3%83%9A%E3%83%BC%E3%82%B8
221                         //inputFromURL http://ja.wikipedia.org/wiki/%E6%9D%B1%E4%BA%AC%E5%AD%A6%E8%8A%B8%E5%A4%A7%E5%AD%A6%E9%99%84%E5%B1%9E%E9%AB%98%E7%AD%89%E5%AD%A6%E6%A0%A1
222                         //inputFromURL http://osecpu.osask.jp/wiki/?FrontPage
223                         //inputFromURL http://osecpu.osask.jp/wiki/?impressions
224                         
225                         var url = str.substring(13);
226                         this.debug("[" + url + "]\n");
227                         var res = this.networkManager.sendRequestThroughPHPSync("GET", url, null);
228                         //this.debug("[" + res + "]\n");
229                         var parser = new AI_HTMLParser(this);
230                         parser.loadText(res);
231                         var mainString = parser.mainString;
232                         this.debug(mainString);
233                         console.log(parser.linkList);
234                         
235                         this.changeMode(this.UUID_Mode_Standard);
236                         this.sendTextFromFileToAI(mainString, url, null, "Web");
237                         this.changeMode(this.UUID_Mode_InternalConsole);
238                         
239                 } else if(str == "savemem"){
240                         this.memory.saveMemory();
241                 } else if(str == "netDB update"){
242                         this.networkManager.networkDBUpdate();
243                 } else if(str == "netDB viewall"){
244                         this.networkManager.networkDBViewAll();
245                 } else{
246                         this.debug("Unknown command [" + str + "].\n");
247                         this.debug("Command list:\n");
248                         this.debug("Implemented command list:\n");
249                         this.debug("  show cwl\n");
250                         this.debug("  show wl\n");
251                         this.debug("  exit\n");
252                 }
253                 
254                 this.debug("**** End Processing (InternalConsole) ****\n");
255         },
256         inputProcess_CompileELCHNOS_OSECPU: function(str, srctype){
257                 this.debug("**** Start Processing (CompileELCHNOS_OSECPU) ****\n");
258                 
259                 var that = this;
260                 var cc = new ELCHNOSCompiler(function(s){ that.debug(s); }, this.downloadBox);
261                 if(cc.compile(str) != null){
262                         cc.saveBinary();
263                 }
264                 
265                 this.debug("**** End Processing (CompileELCHNOS_OSECPU) ****\n");
266         },
267 }