OSDN Git Service

自動追尾を再実装。
[chnosproject/AI004.git] / aimemory.js
1 function AI_Memory(env){
2         this.env = env;
3         
4         //ルート
5         this.root = new Array();
6         //サブリスト
7         this.candidateWordList = new Array();
8         this.candidateWordListLastModifiedDate = new Date();
9         this.candidateWordListLastCleanedDate = new Date();
10         //
11         this.wordList = new Array();
12         this.wordListLastModifiedDate = new Date();
13         //
14         this.patternList = new Array();
15         //
16         this.dbInfo = new AI_DatabaseInfoTag();
17 }
18 AI_Memory.prototype = {
19
20         saveMemory: function(){
21                 var m = this.env.IOManager;
22                 var s = "#" + this.env.UUID_Mode_ReadMemory + "\n";
23                 var cl = this.root;
24                 var k;
25                 for(var i = 0, iLen = cl.length; i < iLen; i++){
26                         if(cl[i] instanceof AI_MemoryTag){
27                                 k = cl[i].parseToStringData();
28                                 if(k !== undefined){
29                                         s += k + "\n";
30                                 }
31                         }
32                 }
33                 //dbInfoタグの保存
34                 k = this.dbInfo.parseToStringData();
35                 if(k !== undefined){
36                         s += k + "\n";
37                 }
38                 //
39                 var d = new Blob([s]);
40                 if(d){
41                         m.showDownloadLink(d);
42                 }
43         },
44         loadMemory: function(str){
45                 var a, t, d, m, q;
46                 
47                 this.env.debug("Memory loading...\n");
48                 a = str.splitByArray(["\n"]);
49                 
50                 for(var i = 1, iLen = a.length; i < iLen; i++){
51                         try{
52                                 d = eval(a[i]);
53                         } catch(e){
54                                 this.env.debug(i + ": " + e + "\n");
55                                 continue;
56                         }
57                         if(d === undefined){
58                                 continue;
59                         }
60                         q = d.type;
61                         if(q == AI_MemoryTag.prototype.Type_CandidateWord){
62                                 t = new AI_CandidateWordTag();
63                         } else if(q == AI_MemoryTag.prototype.Type_Word){
64                                 t = new AI_WordTag();
65                         } else if(q == AI_MemoryTag.prototype.Type_DatabaseInfo){
66                                 t = new AI_DatabaseInfoTag();
67                         } else{
68                                 t = new AI_MemoryTag();
69                         }
70                         AI_MemoryTag.prototype.loadFromMemoryData.call(t, d);
71                         this.appendMemoryTag(t);
72                 }
73                 this.verifyMemoryStructure();
74                 this.env.debug("Memory loading done.\n" + this.root.length + " tags exist.\n");
75         },
76         appendMemoryTag: function(tag){
77                 //同じUUIDのタグがあった場合はデバッグ表示をして、追加しようとしているものに置き換える。
78                 //ただし、初期データに入っているものは警告を発さず上書きする。
79                 var s = this.root.isIncluded(tag, function(a, b){ return (a.uuid == b.uuid); });
80                 
81                 //タグに合わせて追加条件を満たしているか確認し、それぞれのサブリストに分配
82                 if(tag instanceof AI_CandidateWordTag){
83                         this.candidateWordList.push(tag);
84                         this.candidateWordListLastModifiedDate = new Date();
85                 } else if(tag instanceof AI_WordTag){
86                         if(this.wordList.isIncluded(tag, function(a, b){ return ((a.str == b.str) && (a !== s)); })){
87                                 this.env.debug("appendMemoryTag: Duplicated word [" + tag.str + "].\n");
88                                 return;
89                         }
90                         if(tag.str == undefined || tag.str.length == 0){
91                                 this.env.debug("appendMemoryTag: Invalid word [" + tag.str + "].\n");
92                                 return;
93                         }
94                         this.wordList.push(tag);
95                         this.wordListLastModifiedDate = new Date();
96                 } else if(tag instanceof AI_PatternTag){
97                         this.patternList.push(tag);
98                 } else if(tag instanceof AI_DatabaseInfoTag){
99                         //データベースデータに反映させて、タグ自体は破棄する
100                         tag.bindDatabaseInfo(this);
101                         return;
102                 }
103                 
104                 //すでにあった重複UUIDの削除
105                 if(s){
106                         if(s.isBootstrap === undefined){
107                                 this.env.debug("appendMemoryTag: duplicated UUID " + tag.uuid + ", overwritten.\n");
108                         }
109                         this.removeMemoryTagByObject(s);
110                 }
111                 //ルートに追加
112                 this.root.push(tag);
113         },
114         /*
115         appendMemoryTagFromString: function(str){
116                 //retv:isAppended
117                 var d;
118                 var q;
119                 var t;
120                 try{
121                         d = eval(str);
122                 } catch(e){
123                         this.env.debug(""i + ": " + e + "\n");
124                         return false;
125                 }
126                 if(d === undefined){
127                         return false;
128                 }
129                 q = d.type;
130                 if(q == AI_MemoryTag.prototype.Type_CandidateWord){
131                         t = new AI_CandidateWordTag();
132                 } else{
133                         t = new AI_MemoryTag();
134                 }
135                 AI_MemoryTag.prototype.loadFromMemoryData.call(t, d);
136                 this.appendMemoryTag(t);
137         },
138         */
139         removeMemoryTagByObject: function(obj){
140                 this.root.removeAnObject(obj);
141                 if(this.candidateWordList.removeAnObject(obj)){
142                         this.candidateWordListLastModifiedDate = new Date();
143                 }
144                 if(this.wordList.removeAnObject(obj)){
145                         this.wordListLastModifiedDate = new Date();
146                 }
147         },
148         verifyMemoryStructure: function(){
149                 //メモリ構造検査・修復
150                 //単語が単語候補に残っていた場合は単語候補から削除
151                 for(var i = 0, iLen = this.wordList.length; i < iLen; i++){
152                         var w = this.wordList[i].str;
153                         for(var j = 0, jLen = this.candidateWordList.length; j < jLen; j++){
154                                 if(this.candidateWordList[j].str == w){
155                                         this.env.debug("Word duplicated in CWL. Removed.\n");
156                                         this.removeMemoryTagByObject(this.candidateWordList[j]);
157                                         j--;
158                                         jLen--;
159                                 }
160                         }
161                 }
162                 
163                 this.env.wordRecognition.cleanCandidateWordList();
164                 //候補リスト並べ替え
165                 this.env.wordRecognition.sortCandidateWordListByWordCount();
166                 this.env.wordRecognition.computeEachWordLevel();
167                 this.env.wordRecognition.sortCandidateWordListByWordLevel();
168                 //
169                 this.env.debug("Memory verifying done.\n");
170         },
171         getUUIDFromWord: function(str){
172                 var t = this.wordList.isIncluded(str, function(a, b){ return a.str == b; });
173                 if(!t){
174                         return this.env.UUID_Meaning_UndefinedString;
175                 }
176                 return t.uuid;
177         },
178 }