--- /dev/null
+// 知識表現のデータベース
+// php経由でMySQLデータベースに同期する機能と
+// データへのアクセスを提供する
+
+function MemoryDB(syncPHPURL){
+ // 全データ
+ this.root = new Array();
+ //
+ this.nodeList = new Array();
+ //
+ this.syncPHPURL = syncPHPURL;
+ this.isEnabledNetDB = true;
+ //
+ this.callback_addNode = null; // function(nodeinstance){};
+}
+MemoryDB.prototype = {
+ UUID_Null: "00000000-0000-0000-0000-000000000000",
+ UUID_NodeType_DecimalNumber: "e3346fd4-ac17-41c3-b3c7-e04972e5c014",
+ UUID_Node_MemoryDBNetworkTimestamp: "a2560a9c-dcf7-4746-ac14-347188518cf2",
+ createRequestObject: function(){
+ var rq = null;
+ // XMLHttpRequest
+ try{
+ // XMLHttpRequest オブジェクトを作成
+ rq = new XMLHttpRequest();
+ }catch(e){}
+ // Internet Explorer
+ try{
+ rq = new ActiveXObject('MSXML2.XMLHTTP.6.0');
+ }catch(e){}
+ try{
+ rq = new ActiveXObject('MSXML2.XMLHTTP.3.0');
+ }catch(e){}
+ try{
+ rq = new ActiveXObject('MSXML2.XMLHTTP');
+ }catch(e){}
+ if(rq == null){
+ return null;
+ }
+ return rq;
+ },
+ requestObjectDisableCache: function(rq){
+ //call after open request.
+ //disable cache
+ //http://vird2002.s8.xrea.com/javascript/XMLHttpRequest.html
+ rq.setRequestHeader('Pragma', 'no-cache'); // HTTP/1.0 における汎用のヘッダフィールド
+ rq.setRequestHeader('Cache-Control', 'no-cache'); // HTTP/1.1 におけるキャッシュ制御のヘッダフィールド
+ rq.setRequestHeader('If-Modified-Since', 'Thu, 01 Jun 1970 00:00:00 GMT');
+
+ },
+ sendRequestSync: function(mode, url, data){
+ //同期モード
+ var q = this.createRequestObject();
+ q.open(mode, url, false);
+ q.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
+ this.requestObjectDisableCache(q);
+ try {
+ q.send(data);
+ } catch(e){
+ console.log("sendRequestSync:Network Error.\n");
+ this.isEnabledNetDB = false;
+ return null;
+ }
+ if(q.status == 0){
+ console.log("ネットワークにアクセスできません。" + q.status + ":" + q.statusText);
+ }else if((200 <= q.status && q.status < 300) || (q.status == 304)){
+ var res = q.responseText;
+ return res;
+ } else{
+ console.log("サーバーがエラーを返しました。" + request.status + ":" + request.statusText);
+ }
+ this.isEnabledNetDB = false;
+ return null;
+ },
+ syncDB: function(){
+ // MySQLと同期
+ var r, a, d, t;
+ if(this.root.length == 0){
+ // 初回同期時は全て取得
+ r = this.sendRequestSync("GET", this.syncPHPURL + "?action=getallnode", null);
+ } else{
+ // 差分のみを取得
+
+ }
+ a = r.split("\n");
+ for(var i = 0, iLen = a.length; i < iLen; i++){
+ try{
+ d = eval(a[i]);
+ } catch(e){
+ console.log(i + ": " + e + "\n");
+ continue;
+ }
+ if(d === undefined){
+ continue;
+ }
+ t = new MemoryDBNodeTag(d[0], d[1], d[2]);
+ if(t){
+ this.root.push(t);
+ this.nodeList.push(t);
+ }
+ }
+ console.log(this.root);
+ },
+ editNode: function(ident, tid, nid, disableSync){
+ // Nodeを追加し、データベースにも反映し、可能ならばネットワークにも反映させる
+ // nid(nodeid)は省略可能で、省略時は新たなUUIDが自動的に付与される
+ // tid(typeid)も省略可能で、省略時はNullUUIDが付与される。
+ // 戻り値はMemoryDBNodeTagインスタンス
+ // すでに同じnodeidのNodeが存在している場合はundefinedを返しデータベースへの変更は行われない。
+ var t, s, r;
+ if(!ident){
+ return undefined;
+ }
+ if(!tid){
+ tid = this.UUID_Null;
+ }
+ if(!nid){
+ nid = this.createUUID();
+ }
+ // 存在確認
+ t = this.getNodeFromUUID(nid);
+ if(t){
+ return undefined;
+ }
+ t = new MemoryDBNodeTag(nid, tid, ident);
+
+ if(this.isEnabledNetDB){
+ s = this.syncPHPURL + "?action=addnode";
+ s += "&nid=" + encodeURIComponent(nid);
+ s += "&tid=" + encodeURIComponent(tid);
+ s += "&ident=" + encodeURIComponent(ident);
+ r = this.sendRequestSync("GET", s, null);
+ console.log(r);
+ }
+ },
+ addNode: function(ident, tid, nid){
+ // Nodeを追加し、データベースにも反映し、可能ならばネットワークにも反映させる
+ // nid(nodeid)は省略可能で、省略時は新たなUUIDが自動的に付与される
+ // tid(typeid)も省略可能で、省略時はNullUUIDが付与される。
+ // 戻り値はMemoryDBNodeTagインスタンス
+ // すでに同じnodeidのNodeが存在している場合はundefinedを返しデータベースへの変更は行われない。
+ var t, s, r;
+ if(!ident){
+ return undefined;
+ }
+ if(!tid){
+ tid = this.UUID_Null;
+ }
+ if(!nid){
+ nid = this.createUUID();
+ }
+ // 存在確認
+ t = this.getNodeFromUUID(nid);
+ if(t){
+ return undefined;
+ }
+ t = new MemoryDBNodeTag(nid, tid, ident);
+
+ if(this.isEnabledNetDB){
+ s = this.syncPHPURL + "?action=addnode";
+ s += "&nid=" + encodeURIComponent(nid);
+ s += "&tid=" + encodeURIComponent(tid);
+ s += "&ident=" + encodeURIComponent(ident);
+ r = this.sendRequestSync("GET", s, null);
+ console.log(r);
+ }
+ },
+ createUUID: function(){
+ var f = this.createUUIDSub;
+ return (f() + f() + "-" + f() + "-" + f() + "-" + f() + "-" + f() + f() + f());
+ },
+ createUUIDSub: function(){
+ return (((1 + Math.random()) * 0x10000) | 0).toString(16).toLowerCase().substring(1);
+ },
+ getNodeFromUUID: function(nodeid){
+ return this.nodeList.isIncluded(nodeid, function(a, b){ return a.nodeid == b; });
+ },
+}
+
+function MemoryDBNodeTag(nodeid, typeid, identifier){
+ this.nodeid = nodeid;
+ this.typeid = typeid;
+ this.identifier = identifier;
+ //
+
+}
+MemoryDBNodeTag.prototype = {
+
+}
+
+function MemoryDBEdgeTag(typeUUIDStr){
+ this.uuid = null;
+}
+MemoryDBEdgeTag.prototype = {
+
+}
create table Node (
nodeid binary(16) primary key,
typeid binary(16) not null,
- identifier text character set utf8 not null
+ identifier text character set utf8 not null,
+ modtimestamp bigint
)
");
edgeid binary(16) primary key,
typeid binary(16) not null,
nodeid0 binary(16) not null,
- nodeid1 binary(16) not null
+ nodeid1 binary(16) not null,
+ modtimestamp bigint
)
");
+//
+
define("QUERY_ADD_Node", "
insert into Node (
- nodeid, typeid, identifier
+ nodeid, typeid, identifier, modtimestamp
) values (
- unhex(replace(?, '-', '')), unhex(replace(?, '-', '')), ?
+ unhex(replace(?, '-', '')), unhex(replace(?, '-', '')), ?, ?
)
");
-define("QUERY_ADD_Node_TYPES", "sss");
+define("QUERY_ADD_Node_TYPES", "sssi");
define("QUERY_ADD_Edge", "
insert into Node (
- edgeid, typeid, nodeid0, nodeid1
+ edgeid, typeid, nodeid0, nodeid1, modtimestamp
) values (
- unhex(replace(?, '-', '')), unhex(replace(?, '-', '')), unhex(replace(?, '-', '')), unhex(replace(?, '-', ''))
+ unhex(replace(?, '-', '')), unhex(replace(?, '-', '')), unhex(replace(?, '-', '')), unhex(replace(?, '-', '')), ?
)
");
-define("QUERY_ADD_Edge_TYPES", "ssss");
+define("QUERY_ADD_Edge_TYPES", "ssssi");
+
+//
define("QUERY_SELECT_ALL_Node", "select hex(nodeid), hex(typeid), identifier from Node");
+define("QUERY_SELECT_ALL_Node_With_modtimestamp", "select hex(nodeid), hex(typeid), identifier, modtimestamp from Node");
define("QUERY_SELECT_ALL_Edge", "select hex(edgeid), hex(typeid), hex(nodeid0), hex(nodeid1) from Edge");
+define("QUERY_SELECT_modified_Node", "select hex(nodeid), hex(typeid), identifier from Node WHERE modtimestamp>?");
+define("QUERY_SELECT_modified_Node_TYPES", "i");
+
//FOR DEBUG
mysqli_report(MYSQLI_REPORT_ERROR);
echo(PHP_EOL);
}
$stmt->close();
+ // put timestamp tag
+ echoMemoryDBNetworkTimestamp();
exit();
- } else if(strcmp($action, 'viewallnode') == 0){
- $stmt = $db->prepare(QUERY_SELECT_ALL_Node);
+ } else if(strcmp($action, 'getnodemod') == 0){
+ if(isset($_GET['t'])){
+ $ts = $_GET['t'];
+ } else{
+ $ts = 0;
+ }
+ $stmt = $db->prepare(QUERY_SELECT_modified_Node);
+ $stmt->bind_param(QUERY_SELECT_modified_Node_TYPES, $ts);
$stmt->execute();
//
$stmt->store_result();
echo('","');
echo($ident);
echo('"]');
+ echo(PHP_EOL);
+ }
+ $stmt->close();
+ // put timestamp tag
+ echoMemoryDBNetworkTimestamp();
+ exit();
+ } else if(strcmp($action, 'viewallnode') == 0){
+ $stmt = $db->prepare(QUERY_SELECT_ALL_Node_With_modtimestamp);
+ $stmt->execute();
+ //
+ $stmt->store_result();
+ if($stmt->errno != 0){
+ exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B");
+ }
+
+ $stmt->bind_result($uuid, $typeid, $ident, $mts);
+ while($stmt->fetch()){
+ $uuid = strtolower($uuid);
+ echo('["');
+ echo(getFormedUUIDString($uuid));
+ echo('","');
+ echo(getFormedUUIDString($typeid));
+ echo('","');
+ echo($ident);
+ echo('"]');
+ echo(' @' . $mts);
echo("<br />");
}
echo($stmt->num_rows);
$stmt->close();
- exit(" OK");
+ exit(" OK " . getTimeStampMs());
} else if(strcmp($action, 'addnode') == 0){
- if(isset($_GET['nodeid'])){
- $uuid = $_GET['nodeid'];
+ if(isset($_GET['nid'])){
+ $nodeid = $_GET['nid'];
} else{
exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "nodeid needed.");
}
- if(isset($_GET['typeid'])){
- $typeid = $_GET['typeid'];
+ if(isset($_GET['tid'])){
+ $typeid = $_GET['tid'];
} else{
exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "typeid needed.");
}
$stmt = $db->prepare(QUERY_ADD_Node);
$mts = getTimeStampMs();
- $stmt->bind_param(QUERY_ADD_Node_TYPES, $nodeid, $typeid, $ident);
+ $stmt->bind_param(QUERY_ADD_Node_TYPES, $nodeid, $typeid, $ident, $mts);
$stmt->execute();
//
$stmt->store_result();
die('["' . $errid .'","' . $description . '"]');
}
+function echoMemoryDBNetworkTimestamp()
+{
+ echo('["a2560a9c-dcf7-4746-ac14-347188518cf2","e3346fd4-ac17-41c3-b3c7-e04972e5c014","');
+ echo(getTimeStampMs());
+ echo('"]');
+ echo(PHP_EOL);
+}
+
function connectDB()
{
$db = new mysqli('localhost', DATABASE_USER, DATABASE_PWD, DATABASE_NAME);
function rebuildDB($db)
{
- // すでにあるテーブルの削除
+ //
+ // 削除
+ //
// Node
- $stmt = $db->prepare("drop table if exists Node");
- $stmt->execute();
- // エラーチェック省略
- $stmt->close();
+ $stmt = $db->query("drop table if exists Node");
// Edge
- $stmt = $db->prepare("drop table if exists Edge");
- $stmt->execute();
- // エラーチェック省略
- $stmt->close();
+ $stmt = $db->query("drop table if exists Edge");
+ //
// 再構築
+ //
// Node
- $stmt = $db->prepare(QUERY_CREATE_TABLE_Node);
- $stmt->execute();
- // エラーチェック省略
- $stmt->close();
+ $stmt = $db->query(QUERY_CREATE_TABLE_Node);
// Edge
- $stmt = $db->prepare(QUERY_CREATE_TABLE_Edge);
- $stmt->execute();
- // エラーチェック省略
- $stmt->close();
+ $stmt = $db->query(QUERY_CREATE_TABLE_Edge);
}
function getFormedUUIDString($str)
+++ /dev/null
-// 知識表現を保存するデータベース
-
-function MemoryDB(){
- //ルート
- this.root = new Array();
- // タグタイプリスト(各タグのfunctionオブジェクトの配列)
- this.tagTypeList = new Array();
- /*
- //サブリスト
- this.candidateWordList = new Array();
- this.candidateWordListLastModifiedDate = new Date();
- this.candidateWordListLastCleanedDate = new Date();
- //
- this.wordList = new Array();
- this.wordListLastModifiedDate = new Date();
- //
- this.notWordList = new Array();
- this.notWordListLastModifiedDate = new Date();
- //
- this.patternList = new Array();
- //
- this.dbInfo = new AI_DatabaseInfoTag();
- */
-}
-MemoryDB.prototype = {
- headerUUID: "42e11880-62b8-46ea-a1c4-481264d4440d", // UUID_Mode_ReadMemory
- memoryDataString: function(){
- var s = "#" + this.headerUUID + "\n";
- var cl = this.root;
- var k;
- for(var i = 0, iLen = cl.length; i < iLen; i++){
- if(cl[i] instanceof AI_MemoryTag){
- k = cl[i].parseToStringData();
- if(k !== undefined){
- s += k + "\n";
- }
- }
- }
- //dbInfoタグの保存
- k = this.dbInfo.parseToStringData();
- if(k !== undefined){
- s += k + "\n";
- }
- //
- /*
- var d = new Blob([s]);
- if(d){
- m.showDownloadLink(d);
- }
- */
- return s;
- },
- loadMemory: function(str){
- var a, t, d, m, q;
-
- // this.env.debug("Memory loading...\n");
- a = str.splitByArray(["\n"]);
-
- for(var i = 1, iLen = a.length; i < iLen; i++){
- try{
- d = eval(a[i]);
- } catch(e){
- // this.env.debug(i + ": " + e + "\n");
- continue;
- }
- if(d === undefined){
- continue;
- }
- q = d.type;
- if(q == AI_MemoryTag.prototype.Type_CandidateWord){
- t = new AI_CandidateWordTag();
- } else if(q == AI_MemoryTag.prototype.Type_Word){
- t = new AI_WordTag();
- } else if(q == AI_MemoryTag.prototype.Type_NotWord){
- t = new AI_NotWordTag();
- } else if(q == AI_MemoryTag.prototype.Type_DatabaseInfo){
- t = new AI_DatabaseInfoTag();
- } else{
- t = new AI_MemoryTag();
- }
- AI_MemoryTag.prototype.loadFromMemoryData.call(t, d);
- this.appendMemoryTag(t);
- }
- this.verifyMemoryStructure();
- this.env.debug("Memory loading done.\n" + this.root.length + " tags exist.\n");
- },
- appendMemoryTag: function(tag){
- //同じUUIDのタグがあった場合はデバッグ表示をして、追加しようとしているものに置き換える。
- //ただし、初期データに入っているものは警告を発さず上書きする。
- var s = this.root.isIncluded(tag, function(a, b){ return (a.uuid == b.uuid); });
-
- //タグに合わせて追加条件を満たしているか確認し、それぞれのサブリストに分配
- if(tag instanceof AI_CandidateWordTag){
- this.candidateWordList.push(tag);
- this.candidateWordListLastModifiedDate = new Date();
- } else if(tag instanceof AI_WordTag){
- if(this.wordList.isIncluded(tag, function(a, b){ return ((a.str == b.str) && (a !== s)); })){
- this.env.debug("appendMemoryTag: Duplicated word [" + tag.str + "].\n");
- return;
- }
- if(tag.str == undefined || tag.str.length == 0){
- this.env.debug("appendMemoryTag: Invalid word [" + tag.str + "].\n");
- return;
- }
- this.wordList.push(tag);
- this.wordListLastModifiedDate = new Date();
- } else if(tag instanceof AI_NotWordTag){
- if(this.notWordList.isIncluded(tag, function(a, b){ return ((a.str == b.str) && (a !== s)); })){
- this.env.debug("appendMemoryTag: Duplicated notWord [" + tag.str + "].\n");
- return;
- }
- if(tag.str == undefined || tag.str.length == 0){
- this.env.debug("appendMemoryTag: Invalid notWord [" + tag.str + "].\n");
- return;
- }
- this.notWordList.push(tag);
- this.notWordListLastModifiedDate = new Date();
- } else if(tag instanceof AI_PatternTag){
- this.patternList.push(tag);
- } else if(tag instanceof AI_DatabaseInfoTag){
- //データベースデータに反映させて、タグ自体は破棄する
- tag.bindDatabaseInfo(this);
- return;
- }
-
- //すでにあった重複UUIDの削除
- if(s){
- if(s.isBootstrap === undefined){
- this.env.debug("appendMemoryTag: duplicated UUID " + tag.uuid + ", overwritten.\n");
- }
- this.removeMemoryTagByObject(s);
- }
- //ルートに追加
- this.root.push(tag);
- },
- /*
- appendMemoryTagFromString: function(str){
- //retv:isAppended
- var d;
- var q;
- var t;
- try{
- d = eval(str);
- } catch(e){
- this.env.debug(""i + ": " + e + "\n");
- return false;
- }
- if(d === undefined){
- return false;
- }
- q = d.type;
- if(q == AI_MemoryTag.prototype.Type_CandidateWord){
- t = new AI_CandidateWordTag();
- } else{
- t = new AI_MemoryTag();
- }
- AI_MemoryTag.prototype.loadFromMemoryData.call(t, d);
- this.appendMemoryTag(t);
- },
- */
- removeMemoryTagByObject: function(obj){
- this.root.removeAnObject(obj);
- if(this.candidateWordList.removeAnObject(obj)){
- this.candidateWordListLastModifiedDate = new Date();
- }
- if(this.wordList.removeAnObject(obj)){
- this.wordListLastModifiedDate = new Date();
- }
- },
- verifyMemoryStructure: function(){
- //メモリ構造検査・修復
- //単語が単語候補に残っていた場合は単語候補から削除
- for(var i = 0, iLen = this.wordList.length; i < iLen; i++){
- var w = this.wordList[i].str;
- for(var j = 0, jLen = this.candidateWordList.length; j < jLen; j++){
- if(this.candidateWordList[j].str == w){
- this.env.debug("Word duplicated in CWL. Removed.\n");
- this.removeMemoryTagByObject(this.candidateWordList[j]);
- j--;
- jLen--;
- }
- }
- }
-
- this.env.wordRecognition.cleanCandidateWordList();
- //候補リスト並べ替え
- this.env.wordRecognition.sortCandidateWordListByWordCount();
- this.env.wordRecognition.computeEachWordLevel();
- this.env.wordRecognition.sortCandidateWordListByWordLevel();
- //
- this.env.debug("Memory verifying done.\n");
- },
- getTagFromWordInNotWord: function(str){
- return this.notWordList.isIncluded(str, function(a, b){ return a.str == b; });
- },
- getUUIDFromWord: function(str){
- var t = this.wordList.isIncluded(str, function(a, b){ return a.str == b; });
- if(!t){
- return this.env.UUID_Meaning_UndefinedString;
- }
- return t.uuid;
- },
- /*
- getUUIDFromWordInNotWord: function(str){
- var t = this.getTagFromWordInNotWord(str);
- if(!t){
- return this.env.UUID_Meaning_UndefinedString;
- }
- return t.uuid;
- },
- */
-}
-
-function MemoryDBTag(typeUUIDStr){
- //保存対象
- this.uuid = null;
- this.createdDate = new Date();
-
- //初期化
- this.initUUID();
-}
-AI_MemoryTag.prototype = {
- Type_CandidateWord: "2fba8fc1-2b9a-46e0-8ade-455c0bd30637",
- Type_Word: "d5eef85c-a796-4d04-bb72-8d45c94c5e4f",
- Type_Pattern: "8085e53e-0e99-4221-821c-057f38e35ed9",
- Type_Meaning: "dec1789a-9200-4f9b-9248-177495f47f7d",
- Type_DatabaseInfo: "4e7b3a3e-bb8c-4315-b3d0-6b25f9aead61",
- Type_NotWord: "505dbc8d-fa0a-4d0f-b625-d6065738f63d",
- Type_Null: "00000000-0000-0000-0000-000000000000",
- AppendType_Manual: "a2aaf202-7a6c-4dc5-b394-da9592410195",
- type: "00000000-0000-0000-0000-000000000000",
- //http://codedehitokoto.blogspot.jp/2012/01/javascriptuuid.html
- initUUID: function(){
- if(!this.uuid){
- var f = this.initUUIDSub;
- this.uuid = f() + f() + "-" +
- f() + "-" +
- f() + "-" +
- f() + "-" +
- f() + f() + f();
- }
- },
- initUUIDSub: function(){
- return (((1 + Math.random()) * 0x10000) | 0).toString(16).toLowerCase().substring(1);
- },
- parseToStringData: function(data){
- //uuid:type:
- var d = new Object();
- //
- d.id = this.uuid;
- d.type = this.type;
- d.cDate = this.createdDate.toUTCString();
- //
- d.data = data;
- //
- return this.parseArrayToStringSource(d);
- },
- loadFromMemoryData: function(data){
- this.uuid = data.id;
- this.type = data.type;
- this.createdDate = new Date(data.cDate);
- if(data.data){
- if(this.loadFromMemoryData != AI_MemoryTag.prototype.loadFromMemoryData){
- this.loadFromMemoryData(data.data);
- }
- }
- },
- parseArrayToStringSource: function(anArray){
- if(!anArray){
- return "null";
- }
- var srcstr = "var t=";
- srcstr += this.parseArrayToStringSourceSub(anArray);
- srcstr += ";t;";
- return srcstr;
- },
- parseArrayToStringSourceSub: function(anArray){
- if(!anArray){
- return "null";
- }
- var srcstr = "{";
- for(var k in anArray){
- var v = anArray[k];
- var t = Object.prototype.toString.call(v);
- if(v instanceof Array){
- srcstr += k + ":" + this.parseArrayToStringSourceSub(v) + ",";
- } else if(!isNaN(v) && v.toString().replace(/\s+/g, "").length > 0){
- //isNaNだけでは数値判定できないので、文字列化後の空白文字を削除した長さも検査している。
- srcstr += k + ":" + v + ",";
- } else if(t == "[object String]"){
- //文字列として変換
- srcstr += k + ":'" + v + "',";
- } else if(t == "[object Object]"){
- srcstr += k + ":" + this.parseArrayToStringSourceSub(v) + ",";
- } else{
- srcstr += k + ":undefined,";
- }
- }
- if(srcstr.charAt(srcstr.length - 1) == ","){
- //最後の余計なカンマを削除
- srcstr = srcstr.slice(0, srcstr.length - 1);
- }
- srcstr += "}";
- return srcstr;
- },
-}
include = function(relpath){\r
document.write("<script type='text/javascript' src=" + relpath + " charset='UTF-8'></script>");\r
}\r
-//AICore\r
+// AICore\r
include("./../aiext.js");\r
-//MGCanvas\r
+// MGCanvas\r
include("./mgcanvas.js");\r
+// memdb\r
+include("./../memdb/memdb.js")\r
<script type="text/javascript">
var mgmain;
+var memdb;
onload = function() {
+ memdb = new MemoryDB("./../memdb/memdb.php");
+ memdb.syncDB();
+
var c = document.getElementById("mainCanvas");
mgmain = new MGCanvas(c);
-
- /*
- mgmain.setGraph([[
- "A","B","C","D","E","F",
- ],[
- ["D", "F"],
- ["D", "E"],
- ["C", "E"],
- ["B", "E"],
- ["B", "C"],
- ["A", "E"],
- ["A", "B"],
- ]]);
- */
- /*
- mgmain.setGraph([[
- "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P",
- ],[
- ["A", "B"],
- ["A", "C"],
- ["A", "D"],
- ["A", "E"],
- ["A", "F"],
- ["A", "G"],
- ["A", "H"],
- ["A", "I"],
- ["A", "J"],
- ["A", "K"],
- ["A", "L"],
- ["A", "M"],
- ["A", "N"],
- ["A", "O"],
- ["A", "P"],
- ]]);
- */
- /*
- mgmain.setGraph([[
- "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P",
- ],[
- ["A", "B"],
- ["B", "C"],
- ["C", "D"],
- ["D", "E"],
- ["E", "F"],
- ["F", "G"],
- ["G", "H"],
- ["A", "I"],
- ["A", "J"],
- ["A", "K"],
- ["A", "L"],
- ["A", "M"],
- ["A", "N"],
- ["A", "O"],
- ["A", "P"],
- ]]);
- */
- /*
- //Petersen Graph
- mgmain.setGraph([[
- "A","B","C","D","E","F","G","H","I","J",
- ],[
- ["A", "B", "AB"],
- ["B", "C", "BC"],
- ["C", "D", "CD"],
- ["D", "E"],
- ["E", "A"],
- //
- ["A", "F"],
- ["B", "G"],
- ["C", "H"],
- ["D", "I"],
- ["E", "J"],
- //
- ["H", "F"],
- ["I", "G"],
- ["J", "H"],
- ["F", "I"],
- ["G", "J"],
- ]]);
- */
- /*
- mgmain.setGraph([[
- "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O",
- ],[
- ["A", "B"],
- ["A", "C"],
- ["A", "D"],
- ["A", "E"],
- //
- ["B", "F"],
- ["B", "G"],
- //
- ["C", "H"],
- ["C", "I"],
- //
- ["D", "J"],
- ["D", "K"],
- //
- ["E", "L"],
- ["E", "M"],
- //
- ["A", "N"],
- //
- ["M", "O"],
- ]]);
- */
-
+ mgmain.loadGraphFromMemoryDB(memdb);
}
</script>
</head>
<button onclick="mgmain.moveViewRelative(0, 10);">↓</button>
<button onclick="mgmain.moveViewRelative(-10, 0);">←</button>
<button onclick="mgmain.moveViewRelative(10, 0);">→</button>
-<input id="identBox" type="text">
-<button onclick="mgmain.setIdentifierForSelectedNode(getElementById('identBox').value);">set</button>
+<br />
+identifier:<input id="identBox" type="text"></input>
+<br />
+<button onclick="mgmain.setIdentifierForSelectedNode(getElementById('identBox').value);">setNodeIdent</button>
+<button onclick="memdb.addNode(getElementById('identBox').value);">addNode</button>
+
</body>
</html>
\ No newline at end of file
this.edgeList.push(new MGEdge(this, p[i][2], n(p[i][0]), n(p[i][1])));
}
},
- addNode: function(identifier, uuid){
-
- },
- addEdge: function(identifier, uuid, nodeid){
-
+ loadGraphFromMemoryDB: function(mdb){
+ var a, l, t, u, m, e, f;
+ a = mdb.nodeList;
+ l = a.length;
+ //m = 12 * l;
+ t = new MGNode(this, "Node");
+ this.nodeList.push(t);
+ f = function(){};
+ for(var i = 0; i < l; i++){
+ u = new MGNode(this, a[i].identifier);
+ this.nodeList.push(u);
+ //e = new MGEdge(this, null, t, u);
+ //this.edgeList.push(e);
+ //t = u;
+ //e.freeLength = m;
+ //e.draw = f;
+ }
},
bringToCenter: function(){
// 重心を求めて、それを表示オフセットに設定する
//
// Check
//
-
+ /*
p = this.nodeList;
for(var i = 0, iLen = p.length; i < iLen; i++){
nTemp = this.getVectorLength(p[i].vector);
if(n){
n.ignoreEdgeRepulsion = 10;
}
-
+ */
//
// Move
this.context.translate(w, h);
this.displayRect = new Rectangle(-w, -h, this.canvas.width, this.canvas.height);
this.currentScale = 1;
+ this.zoomOut();
this.positionOffset = new Point2D(0, 0);
},
convertPointToGraphLayerFromCanvasLayerP: function(pCanvas){
function MGNode(env, identifier){
this.env = env;
this.identifier = identifier;
- this.position = new Point2D(0, 0);
+ this.position = new Point2D(Math.random() * 32 - 16, Math.random() * 32 - 16);
this.size = 10;
//ランダムな初期ベクトルをもつ。
this.vector = new Point2D(Math.random() * 2 - 1, Math.random() * 2 - 1);
- this.friction = (100 - 8) / 100;
+ this.friction = 50 / 100;
this.repulsionLengthNode = 90;
this.repulsionLengthEdge = 90;
this.ignoreEdgeRepulsion = 0;
}
}
}
+
//edge
//自分を端点に含まないエッジとの距離が近い場合、そのエッジから遠ざかろうとする。
+ /*
p = this.env.edgeList;
for(var i = 0, iLen = p.length; i < iLen; i++){
var q = this.env.edgeList[i];
}
}
}
+ */
} else{
this.ignoreEdgeRepulsion--;
}
+
},
}
this.node1 = node1;
this.freeLength = 250;
//
- this.strokeStyle = "rgba(0, 0, 0, 1)";
+ this.strokeStyle = "rgba(0, 0, 0, 0.5)";
this.isSelected = false;
//
this.centerPoint = new Point2D(0, 0);