From: hikarupsp Date: Tue, 22 Apr 2014 14:57:11 +0000 (+0900) Subject: memdbのphpフロントエンドを実装中 X-Git-Url: http://git.osdn.jp/view?p=chnosproject%2FAI004.git;a=commitdiff_plain;h=645096366c22ce8ceb31bdf2cb57befd25f5bf1b memdbのphpフロントエンドを実装中 --- diff --git a/memdb/memorydb.js b/memdb/memorydb.js new file mode 100644 index 0000000..7c01964 --- /dev/null +++ b/memdb/memorydb.js @@ -0,0 +1,306 @@ +// 知識表現を保存するデータベース + +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; + }, +} diff --git a/memdb/memorydb.php b/memdb/memorydb.php new file mode 100644 index 0000000..614a762 --- /dev/null +++ b/memdb/memorydb.php @@ -0,0 +1,246 @@ +prepare(QUERY_SELECT_ALL_Node); + $stmt->execute(); + // + $stmt->store_result(); + if($stmt->errno != 0){ + exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B"); + } + + $stmt->bind_result($uuid, $typeid, $ident); + while($stmt->fetch()){ + $uuid = strtolower($uuid); + echo('["'); + echo(getFormedUUIDString($uuid)); + echo('","'); + echo(getFormedUUIDString($typeid)); + echo('","'); + echo($ident); + echo('"]'); + echo(PHP_EOL); + } + $stmt->close(); + exit(); + } else if(strcmp($action, 'viewallnode') == 0){ + $stmt = $db->prepare(QUERY_SELECT_ALL_Node); + $stmt->execute(); + // + $stmt->store_result(); + if($stmt->errno != 0){ + exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B"); + } + + $stmt->bind_result($uuid, $typeid, $ident); + while($stmt->fetch()){ + $uuid = strtolower($uuid); + echo('["'); + echo(getFormedUUIDString($uuid)); + echo('","'); + echo(getFormedUUIDString($typeid)); + echo('","'); + echo($ident); + echo('"]'); + echo("
"); + } + echo($stmt->num_rows); + $stmt->close(); + exit(" OK"); + } else if(strcmp($action, 'addnode') == 0){ + if(isset($_GET['nodeid'])){ + $uuid = $_GET['nodeid']; + } else{ + exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "nodeid needed."); + } + if(isset($_GET['typeid'])){ + $typeid = $_GET['typeid']; + } else{ + exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "typeid needed."); + } + if(isset($_GET['ident'])){ + $ident = $_GET['ident']; + } else{ + exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "ident needed."); + } + + $stmt = $db->prepare(QUERY_ADD_Node); + $mts = getTimeStampMs(); + $stmt->bind_param(QUERY_ADD_Node_TYPES, $nodeid, $typeid, $ident); + $stmt->execute(); + // + $stmt->store_result(); + if($stmt->errno != 0){ + exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", mysqli_error($db)); + } + $stmt->close(); + exitWithResponseCode("CEA95615-649C-4837-9E24-0C968FA57647", "OK"); + } else if(strcmp($action, 'saytest') == 0){ + /* + //for Mac OSX say command. + system("say " . escapeshellarg("sayのテストをしています。")); + */ + } +} + +//NOP error +exitWithResponseCode("B539657C-0FA6-49C2-AFB0-13AF5C7866ED"); + +function exitWithResponseCode($errid, $description = "") +{ + die('["' . $errid .'","' . $description . '"]'); +} + +function connectDB() +{ + $db = new mysqli('localhost', DATABASE_USER, DATABASE_PWD, DATABASE_NAME); + + if (mysqli_connect_error()) { + // DB connect error + exitWithResponseCode("3A8CF3C8-E6B6-4A99-9134-343CA341B591", mysqli_connect_error()); + } + + // 文字化け防止 + $db->set_charset("utf8"); + + //データベース存在確認 + $stmt = $db->prepare("show tables"); + $stmt->execute(); + // + $stmt->store_result(); + if($stmt->errno != 0){ + exitWithResponseCode("80FA2D65-9473-40B0-A3CE-159AE8E67017"); + } + //テーブルの存在確認 + $stmt->bind_result($tablename); + $found = 0; + while($stmt->fetch()){ + if($tablename == "Node"){ + $found |= 1; + } + if($tablename == "Edge"){ + $found |= 2; + } + } + if(($found & 3) != 3){ + rebuildDB($db); + } + $stmt->close(); + + return $db; +} + +function rebuildDB($db) +{ + // すでにあるテーブルの削除 + + // Node + $stmt = $db->prepare("drop table if exists Node"); + $stmt->execute(); + // エラーチェック省略 + $stmt->close(); + + // Edge + $stmt = $db->prepare("drop table if exists Edge"); + $stmt->execute(); + // エラーチェック省略 + $stmt->close(); + + // 再構築 + + // Node + $stmt = $db->prepare(QUERY_CREATE_TABLE_Node); + $stmt->execute(); + // エラーチェック省略 + $stmt->close(); + + // Edge + $stmt = $db->prepare(QUERY_CREATE_TABLE_Edge); + $stmt->execute(); + // エラーチェック省略 + $stmt->close(); +} + +function getFormedUUIDString($str) +{ + $str = strtolower($str); + return ( + substr($str, 0, 8) . "-" . + substr($str, 8, 4) . "-" . + substr($str, 12, 4) . "-" . + substr($str, 16, 4) . "-" . + substr($str, 20, 12) + ); +} + +function getTimeStampMs() +{ + return ceil(microtime(true)*1000); +} +?> \ No newline at end of file