OSDN Git Service

b3127bd26009dc7f189b31c8a3f73114c290e633
[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.wordList = new Array();
9 }
10 AI_Memory.prototype = {
11         saveMemory: function(){
12                 var m = this.env.IOManager;
13                 var s = "#" + this.env.UUID_Mode_ReadMemory + "\n";
14                 var cl = this.root;
15                 for(var i = 0, iLen = cl.length; i < iLen; i++){
16                         if(cl[i] instanceof AI_MemoryTag){
17                                 s += cl[i].parseToStringData() + "\n";
18                         }
19                 }
20                 var d = new Blob([s]);
21                 if(d){
22                         m.showDownloadLink(d);
23                 }
24         },
25         loadMemory: function(str){
26                 var a, t, d, m, q;
27                 
28                 this.env.debug("Memory loading...\n");
29                 a = str.splitByArray(["\n"]);
30                 
31                 for(var i = 1, iLen = a.length; i < iLen; i++){
32                         try{
33                                 d = eval(a[i]);
34                         } catch(e){
35                                 this.env.debug(i + ": " + e + "\n");
36                                 continue;
37                         }
38                         if(d === undefined){
39                                 continue;
40                         }
41                         q = d.type;
42                         if(q == AI_MemoryTag.prototype.Type_CandidateWord){
43                                 t = new AI_CandidateWordTag();
44                         } else if(q == AI_MemoryTag.prototype.Type_Word){
45                                 t = new AI_WordTag();
46                         } else{
47                                 t = new AI_MemoryTag();
48                         }
49                         AI_MemoryTag.prototype.loadFromMemoryData.call(t, d);
50                         this.appendMemoryTag(t);
51                 }
52                 this.verifyMemoryStructure();
53                 this.env.debug("Memory loading done.\n" + this.root.length + " tags exist.\n");
54         },
55         appendMemoryTag: function(tag){
56                 //同じUUIDのタグがあった場合はデバッグ表示をして、新たなものに置き換える。
57                 var s = this.root.isIncluded(tag, function(a, b){ return (a.uuid == b.uuid); });
58                 if(s){
59                         this.env.debug("appendMemoryTag: duplicated UUID " + tag.uuid + ", overwritten.\n");
60                         this.removeMemoryTagByObject(s);
61                 }
62                 //ルートに追加
63                 this.root.push(tag);
64                 //タグに合わせてそれぞれのサブリストに分配
65                 if(tag instanceof AI_CandidateWordTag){
66                         this.candidateWordList.push(tag);
67                 }
68                 if(tag instanceof AI_WordTag){
69                         this.wordList.push(tag);
70                 }
71         },
72         /*
73         appendMemoryTagFromString: function(str){
74                 //retv:isAppended
75                 var d;
76                 var q;
77                 var t;
78                 try{
79                         d = eval(str);
80                 } catch(e){
81                         this.env.debug(""i + ": " + e + "\n");
82                         return false;
83                 }
84                 if(d === undefined){
85                         return false;
86                 }
87                 q = d.type;
88                 if(q == AI_MemoryTag.prototype.Type_CandidateWord){
89                         t = new AI_CandidateWordTag();
90                 } else{
91                         t = new AI_MemoryTag();
92                 }
93                 AI_MemoryTag.prototype.loadFromMemoryData.call(t, d);
94                 this.appendMemoryTag(t);
95         },
96         */
97         removeMemoryTagByObject: function(obj){
98                 this.root.removeAnObject(obj);
99                 this.candidateWordList.removeAnObject(obj);
100                 this.wordList.removeAnObject(obj);
101         },
102         verifyMemoryStructure: function(){
103                 //メモリ構造検査・修復
104                 //単語が単語候補に残っていた場合は単語候補から削除
105                 for(var i = 0, iLen = this.wordList.length; i < iLen; i++){
106                         var w = this.wordList[i].str;
107                         for(var j = 0, jLen = this.candidateWordList.length; j < jLen; j++){
108                                 if(this.candidateWordList[j].str == w){
109                                         this.env.debug("Word duplicated in CWL. Remove.\n");
110                                         this.removeMemoryTagByObject(this.candidateWordList[j]);
111                                         j--;
112                                         jLen--;
113                                 }
114                         }
115                 }       
116                 this.env.debug("Memory verifying done.\n");
117         }
118 }