OSDN Git Service

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