OSDN Git Service

042b17986a38529948ba9aae5d2dbe72c71c7725
[chnosproject/AI004.git] / memdb / memdb.js
1 // 知識表現のデータベース
2 // php経由でMySQLデータベースに同期する機能と
3 // データへのアクセスを提供する
4
5 function MemoryDB(syncPHPURL){
6         // 全データ
7         this.root = new Array();
8         //
9         this.nodeList = new Array();
10         //
11         this.syncPHPURL = syncPHPURL;
12         this.isEnabledNetDB = true;
13         //
14         this.callback_addNode = null;   // function(nodeinstance){};
15 }
16 MemoryDB.prototype = {
17         UUID_Null: "00000000-0000-0000-0000-000000000000",
18         UUID_NodeType_DecimalNumber: "e3346fd4-ac17-41c3-b3c7-e04972e5c014",
19         UUID_Node_MemoryDBNetworkTimestamp: "a2560a9c-dcf7-4746-ac14-347188518cf2",
20         createRequestObject: function(){
21                 var rq = null;
22                 // XMLHttpRequest
23                 try{
24                         // XMLHttpRequest オブジェクトを作成
25                         rq = new XMLHttpRequest();
26                 }catch(e){}
27                 // Internet Explorer
28                 try{
29                         rq = new ActiveXObject('MSXML2.XMLHTTP.6.0');
30                 }catch(e){}
31                 try{
32                         rq = new ActiveXObject('MSXML2.XMLHTTP.3.0');
33                 }catch(e){}
34                 try{
35                         rq = new ActiveXObject('MSXML2.XMLHTTP');
36                 }catch(e){}
37                 if(rq == null){
38                         return null;
39                 }
40                 return rq;
41         },
42         requestObjectDisableCache: function(rq){
43                 //call after open request.
44                 //disable cache
45                 //http://vird2002.s8.xrea.com/javascript/XMLHttpRequest.html
46                 rq.setRequestHeader('Pragma', 'no-cache');                              // HTTP/1.0 における汎用のヘッダフィールド
47                 rq.setRequestHeader('Cache-Control', 'no-cache');               // HTTP/1.1 におけるキャッシュ制御のヘッダフィールド
48                 rq.setRequestHeader('If-Modified-Since', 'Thu, 01 Jun 1970 00:00:00 GMT');
49                 
50         },
51         sendRequestSync: function(mode, url, data){
52                 //同期モード
53                 var q = this.createRequestObject();
54                 q.open(mode, url, false);
55                 q.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
56                 this.requestObjectDisableCache(q);
57                 try {
58                         q.send(data);
59                 } catch(e){
60                         console.log("sendRequestSync:Network Error.\n");
61                         this.isEnabledNetDB = false;
62                         return null;
63                 }
64                 if(q.status == 0){
65                         console.log("ネットワークにアクセスできません。" + q.status + ":" + q.statusText);
66                 }else if((200 <= q.status && q.status < 300) || (q.status == 304)){
67                         var res = q.responseText;
68                         return res;
69                 } else{
70                         console.log("サーバーがエラーを返しました。" + request.status + ":" + request.statusText);
71                 }
72                 this.isEnabledNetDB = false;
73                 return null;
74         },
75         syncDB: function(){
76                 // MySQLと同期
77                 var r, a, d, t;
78                 if(this.root.length == 0){
79                         // 初回同期時は全て取得
80                         r = this.sendRequestSync("GET", this.syncPHPURL + "?action=getallnode", null);
81                 } else{
82                         // 差分のみを取得
83                         
84                 }
85                 a = r.split("\n");
86                 for(var i = 0, iLen = a.length; i < iLen; i++){
87                         try{
88                                 d = eval(a[i]);
89                         } catch(e){
90                                 console.log(i + ": " + e + "\n");
91                                 continue;
92                         }
93                         if(d === undefined){
94                                 continue;
95                         }
96                         t = new MemoryDBNodeTag(d[0], d[1], d[2]);
97                         if(t){
98                                 this.root.push(t);
99                                 this.nodeList.push(t);
100                         }
101                 }
102                 console.log(this.root);
103         },
104         editNode: function(ident, tid, nid, disableSync){
105                 // Nodeを追加し、データベースにも反映し、可能ならばネットワークにも反映させる
106                 // nid(nodeid)は省略可能で、省略時は新たなUUIDが自動的に付与される
107                 // tid(typeid)も省略可能で、省略時はNullUUIDが付与される。
108                 // 戻り値はMemoryDBNodeTagインスタンス
109                 // すでに同じnodeidのNodeが存在している場合はundefinedを返しデータベースへの変更は行われない。
110                 var t, s, r;
111                 if(!ident){
112                         return undefined;
113                 }
114                 if(!tid){
115                         tid = this.UUID_Null;
116                 }
117                 if(!nid){
118                         nid = this.createUUID();
119                 }
120                 // 存在確認
121                 t = this.getNodeFromUUID(nid);
122                 if(t){
123                         return undefined;
124                 }
125                 t = new MemoryDBNodeTag(nid, tid, ident);
126                 
127                 if(this.isEnabledNetDB){
128                         s = this.syncPHPURL + "?action=addnode";
129                         s += "&nid=" + encodeURIComponent(nid);
130                         s += "&tid=" + encodeURIComponent(tid);
131                         s += "&ident=" + encodeURIComponent(ident);
132                         r = this.sendRequestSync("GET", s, null);
133                         console.log(r);
134                 }
135         },
136         addNode: function(ident, tid, nid){
137                 // Nodeを追加し、データベースにも反映し、可能ならばネットワークにも反映させる
138                 // nid(nodeid)は省略可能で、省略時は新たなUUIDが自動的に付与される
139                 // tid(typeid)も省略可能で、省略時はNullUUIDが付与される。
140                 // 戻り値はMemoryDBNodeTagインスタンス
141                 // すでに同じnodeidのNodeが存在している場合はundefinedを返しデータベースへの変更は行われない。
142                 var t, s, r;
143                 if(!ident){
144                         return undefined;
145                 }
146                 if(!tid){
147                         tid = this.UUID_Null;
148                 }
149                 if(!nid){
150                         nid = this.createUUID();
151                 }
152                 // 存在確認
153                 t = this.getNodeFromUUID(nid);
154                 if(t){
155                         return undefined;
156                 }
157                 t = new MemoryDBNodeTag(nid, tid, ident);
158                 
159                 if(this.isEnabledNetDB){
160                         s = this.syncPHPURL + "?action=addnode";
161                         s += "&nid=" + encodeURIComponent(nid);
162                         s += "&tid=" + encodeURIComponent(tid);
163                         s += "&ident=" + encodeURIComponent(ident);
164                         r = this.sendRequestSync("GET", s, null);
165                         console.log(r);
166                 }
167         },
168         createUUID: function(){
169                 var f = this.createUUIDSub;
170                 return (f() + f() + "-" + f() + "-" + f() + "-" + f() + "-" + f() + f() + f());
171         },
172         createUUIDSub: function(){
173                 return (((1 + Math.random()) * 0x10000) | 0).toString(16).toLowerCase().substring(1);
174         },
175         getNodeFromUUID: function(nodeid){
176                 return this.nodeList.isIncluded(nodeid, function(a, b){ return a.nodeid == b; });
177         },
178 }
179
180 function MemoryDBNodeTag(nodeid, typeid, identifier){
181         this.nodeid = nodeid;
182         this.typeid = typeid;
183         this.identifier = identifier;
184         //
185         
186 }
187 MemoryDBNodeTag.prototype = {
188
189 }
190
191 function MemoryDBEdgeTag(typeUUIDStr){
192         this.uuid = null;
193 }
194 MemoryDBEdgeTag.prototype = {
195
196 }