OSDN Git Service

保存コミット
[chnosproject/AI004.git] / aimtbase.js
1 function AI_MemoryTag(typeUUIDStr){
2         this.uuid = null;
3         this.initUUID();
4         this.type = typeUUIDStr;
5         this.createdDate = new Date();
6 }
7 AI_MemoryTag.prototype = {
8         Type_CandidateWord: "2fba8fc1-2b9a-46e0-8ade-455c0bd30637",
9         Type_Word: "d5eef85c-a796-4d04-bb72-8d45c94c5e4f",
10         //http://codedehitokoto.blogspot.jp/2012/01/javascriptuuid.html
11         initUUID: function(){
12                 if(!this.uuid){
13                         var f = this.initUUIDSub;
14                         this.uuid = f() + f() + "-" + 
15                                                 f() + "-" + 
16                                                 f() + "-" + 
17                                                 f() + "-" + 
18                                                 f() + f() + f();
19                 }
20         },
21         initUUIDSub: function(){
22                 return (((1 + Math.random()) * 0x10000) | 0).toString(16).toLowerCase().substring(1);
23         },
24         parseToStringData: function(data){
25                 //uuid:type:
26                 var d = new Object();
27                 //
28                 d.id = this.uuid;
29                 d.type = this.type;
30                 d.cDate = this.createdDate.toUTCString();
31                 //
32                 d.data = data;
33                 //
34                 return this.parseArrayToStringSource(d);
35         },
36         loadFromMemoryData: function(data){
37                 this.uuid = data.id;
38                 this.type = data.type;
39                 this.createdDate = new Date(data.cDate);
40                 if(data.data){
41                         if(this.loadFromMemoryData != AI_MemoryTag.prototype.loadFromMemoryData){
42                                 this.loadFromMemoryData(data.data);
43                         }
44                 }
45         },
46         escapeForMemory: function(str){
47                 return "\"" + str.replaceAll(":", "@:").replaceAll("\"", "\\\"") + "\"";
48         },
49         unescapeForMemory: function(str){
50                 return str.replaceAll("@:", ":");
51         },
52         parseArrayToStringSource: function(anArray){
53                 if(!anArray){
54                         return "null";
55                 }
56                 var srcstr = "var t=";
57                 srcstr += this.parseArrayToStringSourceSub(anArray);
58                 srcstr += ";t;";
59                 return srcstr;
60         },
61         parseArrayToStringSourceSub: function(anArray){
62                 if(!anArray){
63                         return "null";
64                 }
65                 var srcstr = "{";
66                 for(var k in anArray){
67                         var v = anArray[k];
68                         var t = Object.prototype.toString.call(v);
69                         if(v instanceof Array){
70                                 srcstr += k + ":" + this.parseArrayToStringSourceSub(v) + ",";
71                         } else if(!isNaN(v) && v.toString().replace(/\s+/g, "").length > 0){
72                                 //isNaNだけでは数値判定できないので、文字列化後の空白文字を削除した長さも検査している。
73                                 srcstr += k + ":" + v + ",";
74                         } else if(t == "[object String]"){
75                                 //文字列として変換
76                                 srcstr += k + ":'" + v + "',";
77                         } else if(t == "[object Object]"){
78                                 srcstr += k + ":" + this.parseArrayToStringSourceSub(v) + ",";
79                         } else{
80                                 srcstr += k + ":undefined,";
81                         }
82                 }
83                 if(srcstr.charAt(srcstr.length - 1) == ","){
84                         //最後の余計なカンマを削除
85                         srcstr = srcstr.slice(0, srcstr.length - 1);
86                 }
87                 srcstr += "}";
88                 return srcstr;
89         },
90 }