1 function AI_Memory(env){
5 this.root = new Array();
7 this.candidateWordList = new Array();
8 this.candidateWordListLastModifiedDate = new Date();
9 this.candidateWordListLastCleanedDate = new Date();
11 this.wordList = new Array();
12 this.wordListLastModifiedDate = new Date();
14 this.patternList = new Array();
16 this.dbInfo = new AI_DatabaseInfoTag();
18 AI_Memory.prototype = {
20 saveMemory: function(){
21 var m = this.env.IOManager;
22 var s = "#" + this.env.UUID_Mode_ReadMemory + "\n";
25 for(var i = 0, iLen = cl.length; i < iLen; i++){
26 if(cl[i] instanceof AI_MemoryTag){
27 k = cl[i].parseToStringData();
34 k = this.dbInfo.parseToStringData();
39 var d = new Blob([s]);
41 m.showDownloadLink(d);
44 loadMemory: function(str){
47 this.env.debug("Memory loading...\n");
48 a = str.splitByArray(["\n"]);
50 for(var i = 1, iLen = a.length; i < iLen; i++){
54 this.env.debug(i + ": " + e + "\n");
61 if(q == AI_MemoryTag.prototype.Type_CandidateWord){
62 t = new AI_CandidateWordTag();
63 } else if(q == AI_MemoryTag.prototype.Type_Word){
65 } else if(q == AI_MemoryTag.prototype.Type_DatabaseInfo){
66 t = new AI_DatabaseInfoTag();
68 t = new AI_MemoryTag();
70 AI_MemoryTag.prototype.loadFromMemoryData.call(t, d);
71 this.appendMemoryTag(t);
73 this.verifyMemoryStructure();
74 this.env.debug("Memory loading done.\n" + this.root.length + " tags exist.\n");
76 appendMemoryTag: function(tag){
77 //同じUUIDのタグがあった場合はデバッグ表示をして、追加しようとしているものに置き換える。
78 //ただし、初期データに入っているものは警告を発さず上書きする。
79 var s = this.root.isIncluded(tag, function(a, b){ return (a.uuid == b.uuid); });
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");
90 if(tag.str == undefined || tag.str.length == 0){
91 this.env.debug("appendMemoryTag: Invalid word [" + tag.str + "].\n");
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);
106 if(s.isBootstrap === undefined){
107 this.env.debug("appendMemoryTag: duplicated UUID " + tag.uuid + ", overwritten.\n");
109 this.removeMemoryTagByObject(s);
115 appendMemoryTagFromString: function(str){
123 this.env.debug(""i + ": " + e + "\n");
130 if(q == AI_MemoryTag.prototype.Type_CandidateWord){
131 t = new AI_CandidateWordTag();
133 t = new AI_MemoryTag();
135 AI_MemoryTag.prototype.loadFromMemoryData.call(t, d);
136 this.appendMemoryTag(t);
139 removeMemoryTagByObject: function(obj){
140 this.root.removeAnObject(obj);
141 if(this.candidateWordList.removeAnObject(obj)){
142 this.candidateWordListLastModifiedDate = new Date();
144 if(this.wordList.removeAnObject(obj)){
145 this.wordListLastModifiedDate = new Date();
148 verifyMemoryStructure: function(){
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]);
163 this.env.wordRecognition.cleanCandidateWordList();
165 this.env.wordRecognition.sortCandidateWordListByWordCount();
166 this.env.wordRecognition.computeEachWordLevel();
167 this.env.wordRecognition.sortCandidateWordListByWordLevel();
169 this.env.debug("Memory verifying done.\n");
171 getUUIDFromWord: function(str){
172 var t = this.wordList.isIncluded(str, function(a, b){ return a.str == b; });
174 return this.env.UUID_Meaning_UndefinedString;