OSDN Git Service

AIのDB連携機能を実装中。
authorhikarupsp <hikarupsp@users.sourceforge.jp>
Sun, 2 Feb 2014 07:11:23 +0000 (16:11 +0900)
committerhikarupsp <hikarupsp@users.sourceforge.jp>
Sun, 2 Feb 2014 07:11:23 +0000 (16:11 +0900)
ai.js
aimemtag.js
aimtbase.js
ainet.js
aiwrcgnz.js
dbmysql.php [new file with mode: 0644]
elcc/elccide.html
webcpu/webcpu.js

diff --git a/ai.js b/ai.js
index 8bf3e29..6bbcde3 100644 (file)
--- a/ai.js
+++ b/ai.js
@@ -234,6 +234,10 @@ AI.prototype = {
                        
                } else if(str == "savemem"){
                        this.memory.saveMemory();
+               } else if(str == "netDB update"){
+                       this.networkManager.networkDBUpdate();
+               } else if(str == "netDB viewall"){
+                       this.networkManager.networkDBViewAll();
                } else{
                        this.debug("Unknown command [" + str + "].\n");
                        this.debug("Command list:\n");
index d0f659b..64c14ab 100644 (file)
@@ -48,6 +48,7 @@ var AI_WordTag = function(str, uuid){
 });
 
 var AI_PatternTag = function(pattern, uuid, func){
+       AI_PatternTag.base.call(this, AI_PatternTag.base.prototype.Type_Pattern);
        // p.func(this.env, separated, separated_UUID);
        //patternには関数も指定できる。その場合、関数の形式は
        //f(separated, separated_UUID)となる。戻り値がtrueの場合、パターンはマッチしたとみなされる
@@ -67,6 +68,7 @@ var AI_PatternTag = function(pattern, uuid, func){
 });
 
 var AI_MeaningTag = function(uuid, description){
+       AI_MeaningTag.base.call(this, AI_MeaningTag.base.prototype.Type_Meaning);
        if(description){
                this.description = description;
        }
index beca799..5df630f 100644 (file)
@@ -13,6 +13,8 @@ function AI_MemoryTag(typeUUIDStr){
 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",
        
        //http://codedehitokoto.blogspot.jp/2012/01/javascriptuuid.html
        initUUID: function(){
index 34b09dd..75621fd 100644 (file)
--- a/ainet.js
+++ b/ainet.js
@@ -1,6 +1,7 @@
 function AI_NetworkManager(env){
        this.env = env;
        this.PHPExtPath = "./ainet.php";
+       this.DBPHPPath = "./dbmysql.php";
 }
 AI_NetworkManager.prototype = {
        //from PCD2013GSCL
@@ -87,4 +88,28 @@ AI_NetworkManager.prototype = {
                sendURL += encodeURIComponent(url);
                return this.sendRequestSync("GET", sendURL);
        },
+       networkDBUpdate: function(){
+               //Upload
+               var cl = this.env.memory.root;
+               var k;
+               for(var i = 0, iLen = cl.length; i < iLen; i++){
+                       k = cl[i];
+                       if(k instanceof AI_MemoryTag){
+                               var sendURL = this.DBPHPPath;
+                               sendURL += "?action=add";
+                               sendURL += "&uuid=" + k.uuid;
+                               sendURL += "&typeid=" + k.type;
+                               sendURL += "&desc=" + (k.str ? k.str : "");
+                               sendURL += "&data=" + k.parseToStringData();
+                               var res = this.sendRequestSync("GET", sendURL);
+                               this.env.debug(res + "\n");
+                       }
+               }
+       },
+       networkDBViewAll: function(){
+               var sendURL = this.DBPHPPath;
+               sendURL += "?action=viewall";
+               var res = this.sendRequestSync("GET", sendURL);
+               this.env.debug(res + "\n");
+       },
 }
\ No newline at end of file
index e09220d..027c130 100644 (file)
@@ -64,12 +64,14 @@ AI_WordRecognition.prototype = {
                                this.env.memory.removeMemoryTagByObject(this.env.memory.candidateWordList[i]);
                                i--;
                                iLen--;
+                               continue;
                        }
                        if(this.env.memory.candidateWordList[i].wordLevel < 1){
                                this.env.debug("Too small wordLevel of candidateWord [" + this.env.memory.candidateWordList[i].str + "]. Removed.\n");
                                this.env.memory.removeMemoryTagByObject(this.env.memory.candidateWordList[i]);
                                i--;
                                iLen--;
+                               continue;
                        }
                }
                this.env.memory.candidateWordListLastCleanedDate = new Date();
diff --git a/dbmysql.php b/dbmysql.php
new file mode 100644 (file)
index 0000000..9b79d41
--- /dev/null
@@ -0,0 +1,227 @@
+<?php
+//responseError("");
+
+//
+// Settings
+//
+
+//データベースユーザー名
+define("DATABASE_USER", "aiclient");
+//データベースパスワード
+define("DATABASE_PWD", "WhoAmI?");
+//データベース名
+define("DATABASE_NAME", "aimemory");
+
+//
+// Static values
+//
+
+define("QUERY_CREATE_TABLE_MEMORY_TAG_ROOT", "
+create table MemoryTagRoot (
+       uuid binary(16) primary key,
+       typeid binary(16) not null,
+       description text character set utf8 not null,
+       created_timestamp bigint,
+       modified_timestamp bigint,
+       data text character set utf8 not null,
+       index(uuid)
+)
+");
+
+define("QUERY_ADD_MTAG", "insert into MemoryTagRoot (
+       uuid, typeid, description, created_timestamp, modified_timestamp, data
+) values (
+       unhex(replace(?, '-', '')), unhex(replace(?, '-', '')), ?, ?, ?, ?
+)");
+define("QUERY_ADD_MTAG_TYPES", "sssiis");
+
+define("QUERY_SELECT_ALL_MTAG", "select hex(uuid), hex(typeid), description, created_timestamp, modified_timestamp, data from MemoryTagRoot");
+
+
+//FOR DEBUG
+mysqli_report(MYSQLI_REPORT_ERROR);
+
+$db = connectDB();
+
+//action解釈
+if(isset($_GET['action'])){
+       $action = $_GET['action'];
+       if(strcmp($action, 'rebuild') == 0){
+               rebuildDB($db);
+               responseError("CEA95615-649C-4837-9E24-0C968FA57647", "OK");
+       } else if(strcmp($action, 'viewall') == 0){
+               $stmt = $db->prepare(QUERY_SELECT_ALL_MTAG);
+               $stmt->execute();
+               //
+               $stmt->store_result();
+               if($stmt->errno != 0){
+                       responseError("A0518702-C90C-4785-B5EA-1A213DD0205B");
+               }
+               
+               $stmt->bind_result($uuid, $typeid, $desc, $cts, $mts, $data);
+               while($stmt->fetch()){
+                       $uuid = strtolower($uuid);
+                       echo('["');
+                       echo(getFormedUUIDString($uuid));
+                       echo('","');
+                       echo(getFormedUUIDString($typeid));
+                       echo('","');
+                       echo($desc);
+                       echo('",');
+                       echo($cts);
+                       echo(',');
+                       echo($mts);
+                       echo(',"');
+                       echo($data);
+                       echo('"]');
+                       echo(PHP_EOL);
+               }
+               echo($stmt->num_rows);
+               $stmt->close();
+               exit(" OK");
+       } else if(strcmp($action, 'viewallhtml') == 0){
+               $stmt = $db->prepare(QUERY_SELECT_ALL_MTAG);
+               $stmt->execute();
+               //
+               $stmt->store_result();
+               if($stmt->errno != 0){
+                       responseError("A0518702-C90C-4785-B5EA-1A213DD0205B");
+               }
+               
+               $stmt->bind_result($uuid, $typeid, $desc, $cts, $mts, $data);
+               while($stmt->fetch()){
+                       $uuid = strtolower($uuid);
+                       echo('["');
+                       echo(getFormedUUIDString($uuid));
+                       echo('","');
+                       echo(getFormedUUIDString($typeid));
+                       echo('","');
+                       echo($desc);
+                       echo('",');
+                       echo($cts);
+                       echo(',');
+                       echo($mts);
+                       //echo(',"');
+                       //echo($data);
+                       //echo('"]');
+                       echo("<br />");
+               }
+               echo($stmt->num_rows);
+               $stmt->close();
+               exit(" OK");
+       } else if(strcmp($action, 'add') == 0){
+               if(isset($_GET['uuid'])){
+                       $uuid = $_GET['uuid'];
+               } else{
+                       responseError("A0518702-C90C-4785-B5EA-1A213DD0205B", "uuid needed.");
+               }
+               if(isset($_GET['typeid'])){
+                       $typeid = $_GET['typeid'];
+               } else{
+                       responseError("A0518702-C90C-4785-B5EA-1A213DD0205B", "typeid needed.");
+               }
+               if(isset($_GET['desc'])){
+                       $desc = $_GET['desc'];
+               } else{
+                       responseError("A0518702-C90C-4785-B5EA-1A213DD0205B", "desc needed.");
+               }
+               if(isset($_GET['data'])){
+                       $data = $_GET['data'];
+               } else{
+                       responseError("A0518702-C90C-4785-B5EA-1A213DD0205B", "data needed.");
+               }
+               $stmt = $db->prepare(QUERY_ADD_MTAG);
+               //$uuid = "12363456-96EC-4E56-BC1F-B58DD0A76161";
+               //$typeid = "12323456-96EC-4E36-BC1F-B58DD0A76161";
+               $mts = getTimeStampMs();
+               //$desc = "aiueo";
+               //$data = "[0]";
+               $stmt->bind_param(QUERY_ADD_MTAG_TYPES, $uuid, $typeid, $desc, $mts, $mts, $data);
+               $stmt->execute();
+               //
+               $stmt->store_result();
+               if($stmt->errno != 0){
+                       responseError("A0518702-C90C-4785-B5EA-1A213DD0205B", mysqli_error($db));
+               }
+               $stmt->close();
+               exit("OK");
+       }
+}
+
+//NOP error
+responseError("B539657C-0FA6-49C2-AFB0-13AF5C7866ED");
+
+function responseError($errid, $description = "")
+{
+       die('["' . $errid .'","' . $description . '"]');
+}
+
+function connectDB()
+{
+       $db = new mysqli('localhost', DATABASE_USER, DATABASE_PWD, DATABASE_NAME);
+       
+       if (mysqli_connect_error()) {
+               // DB connect error
+               responseError("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){
+               responseError("80FA2D65-9473-40B0-A3CE-159AE8E67017");
+       }
+       //テーブルの存在確認
+       $stmt->bind_result($tablename);
+       $found = false;
+       while($stmt->fetch()){
+               if($tablename == "MemoryTagRoot"){
+                       $found = true;
+               }
+       }
+       if(!$found){
+               rebuildDB($db);
+       }
+       $stmt->close();
+       
+       return $db;
+}
+
+function rebuildDB($db)
+{
+       //すでにあるテーブルの削除
+       //MemoryTagRoot
+       $stmt = $db->prepare("drop table if exists MemoryTagRoot");
+       $stmt->execute();
+       //エラーチェック省略
+       $stmt->close();
+       
+       //再構築
+       $stmt = $db->prepare(QUERY_CREATE_TABLE_MEMORY_TAG_ROOT);
+       $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
index 622dc27..5471a1a 100755 (executable)
@@ -149,6 +149,8 @@ function handleDragOver(evt){
                <br />
                <button onclick="enableDebugMode();">EnableDebugMode</button>
                <button onclick="disableDebugMode();">DisableDebugMode</button>
+               <button onclick="mainCPU.showIntegerRegister();">ShowIntegerRegister</button>
+               <button onclick="mainCPU.showPointerRegister();">ShowPointerRegister</button>
                <br />
                <button onclick="if(binStr){ mainCompiler.saveBinary(); }">Create bin.ose</button>
                
index f610284..4d04fba 100755 (executable)
@@ -441,6 +441,20 @@ WebCPU.prototype = {
                        this.message(" - - - - \n", 30);
                }
        },
+       showIntegerRegister: function(){
+               for(var i = 0; i < this.registers.Integer.length; i++){
+                       this.message("R" +  ("00" + i.toString(16)).slice(-2).toUpperCase() + ":0x" + this.registers.Integer[i].toString(16).toUpperCase() + "\n");
+               }
+       },
+       showPointerRegister: function(){
+               for(var i = 0; i < this.registers.Pointer.length; i++){
+                       if(this.registers.Pointer[i]){
+                               this.message("P" +  ("00" + i.toString(16)).slice(-2).toUpperCase() + ":" + this.registers.Pointer[i].toString() + "\n");
+                       } else{
+                               this.message("P" +  ("00" + i.toString(16)).slice(-2).toUpperCase() + ":(null)\n");
+                       }
+               }
+       },
        refreshDebugIntegerRegisterText: function(){
                if(this.debugIntegerRegisterText != null){
                        this.debugIntegerRegisterText.value = "";