(no commit message)
authorymh <ymh.work@gmail.com>
Tue, 06 Apr 2010 15:28:02 +0200
changeset 38 66b32754f6b0
parent 37 47df1cde32e9
child 39 03504c36074d
(no commit message)
engine/sonyengine/mosatags.sonyengine.war
web/thdProject/plugins/iriSonyenginePlugin/config/iriSonyenginePluginConfiguration.class.php
web/thdProject/plugins/iriSonyenginePlugin/lib/client/SonyengineClient.php
web/thdProject/plugins/iriSonyenginePlugin/lib/iriSonyengineEventListener.class.php
Binary file engine/sonyengine/mosatags.sonyengine.war has changed
--- a/web/thdProject/plugins/iriSonyenginePlugin/config/iriSonyenginePluginConfiguration.class.php	Wed Mar 31 11:57:07 2010 +0200
+++ b/web/thdProject/plugins/iriSonyenginePlugin/config/iriSonyenginePluginConfiguration.class.php	Tue Apr 06 15:28:02 2010 +0200
@@ -31,12 +31,10 @@
    */
   public function connectEvents()
   {
-    $this->dispatcher->connect('iri_sonyengine.search', array('uvmcSolrEventListener', 'listenToSearch'));
-    $this->dispatcher->connect('iri_sonyengine.commit', array('uvmcSolrEventListener', 'listenToCommit'));
-    $this->dispatcher->connect('iri_sonyengine.add_document', array('uvmcSolrEventListener', 'listenToAddDocument'));
-    $this->dispatcher->connect('iri_sonyengine.update_document', array('uvmcSolrEventListener', 'listenToAddDocument'));
-    $this->dispatcher->connect('iri_sonyengine.delete_document', array('uvmcSolrEventListener', 'listenToDeleteDocument'));
-    $this->dispatcher->connect('iri_sonyengine.add_document_to_collection', array('uvmcSolrEventListener', 'listenToAddDocumentToCollection'));
-    $this->dispatcher->connect('iri_sonyengine.add_collection', array('uvmcSolrEventListener', 'listenToAddCollection'));
+    $this->dispatcher->connect('iri_sonyengine.find', array('iriSonyengineEventListener', 'listenToFind'));
+    $this->dispatcher->connect('iri_sonyengine.train', array('iriSonyengineEventListener', 'listenToEngineTrain'));
+    $this->dispatcher->connect('iri_sonyengine.reset', array('iriSonyengineEventListener', 'listenToEngineReset'));
+    $this->dispatcher->connect('iri_sonyengine.segment_create', array('iriSonyengineEventListener', 'listenToSegmentCreate'));
+    $this->dispatcher->connect('iri_sonyengine.tag_add', array('iriSonyengineEventListener', 'listenToTagAdd'));
   }
 }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/thdProject/plugins/iriSonyenginePlugin/lib/client/SonyengineClient.php	Tue Apr 06 15:28:02 2010 +0200
@@ -0,0 +1,176 @@
+<?php
+
+class SonyengineClient {
+
+	const POST_METHOD = "POST";
+	const GET_METHOD = "GET";
+	
+	private $base_url;
+	
+	public static function getInstance() {
+		$host = sfConfig::get('iri_sonyengine_service_host', "localhost");
+    	$port = sfConfig::get('iri_sonyengine_service_port', 8080);
+    	$path = sfConfig::get('iri_sonyengine_service_path', "/sonyengine");
+		
+		return new  SonyengineClient($host, $port, $path);
+	}
+	
+	public function __construct($host, $port, $path) {
+		$path = rtrim($path,"/");
+		$this->base_url = "http://$host:$port/$path";
+	}
+	
+	private function send($path, $method, $fields) {
+		
+		$path = ltrim($path,"/");
+		
+		$ch = curl_init();
+		$url = "$this->base_url/$path"
+		curl_setopt($ch,CURLOPT_URL,$url);
+		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
+		
+		if($method == self::POST_METHOD) {
+			
+			$field_string = "";
+			
+			foreach($fields as $key=>$value) { $fields_string .= $key.'='.urlencode($value).'&'; }
+			$fields_string = rtrim($fields_string,'&');
+			
+			curl_setopt($ch,CURLOPT_POST,1);
+			curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
+		}
+		
+		$buffer = curl_exec($ch);
+		curl_close($ch);
+		
+		return $buffer;
+	}
+	
+	
+	public function engineTrain($movieId = null) {
+		
+		$path = "engine/train";
+		if(! is_null($movieId)) {
+			$path .= "/$movieId";
+		}
+		
+		$res = $this->send($path, self::POST_METHOD, array());
+		
+		if($res != "ok") {
+			throw new Exception('Can not train engine');
+		}
+		
+	}
+	
+	public function engineFind($movieId, $tag, $distance = "0.05", $more = true ) {
+		
+		if($more) {
+			$more = "true";
+		}
+		else {
+			$more = "false";
+		}
+		
+		$tag = urlencode($tag);
+		
+		$path = "engine/find/$movieId/$tag/$distance/$more";
+		
+		$res = $this->send($path,self::GET_METHOD, array());
+		
+		$result = array();
+		
+		$doc = DOMDocument::loadXML($res);
+		$xpath = new DOMXPath($doc);
+		
+		$nodes = $xpath->query("/sonyengine/neighbor");
+		
+		foreach($nodes as $node) {
+			$result[] = array("id"=>$node->getAttribute("segment"),"distance"=>$node->getAttribute("distance") );	
+		}
+				
+		return $result;
+	}
+	
+	public function engineReset() {
+		$path = "engine/reset";
+		
+		$res = $this->send($path,self::POST_METHOD,array());
+
+		if($res != "ok") {
+			throw new Exception('Can not reset engine');
+		}
+		
+	}
+	
+	public function segmentCreate($movieId, $retrain) {
+		
+		$path = "";
+		if($retrain)
+			$retrain = "true";
+		else
+		    $retrain = "false";
+			
+		if(is_array($movieId)) {
+			$path = "segment/createall";
+			$doc = new DOMDocument();
+			$doc->appendChild($doc->createElement("sonyengine"));
+			
+			foreach($movieId as $id) {
+				$elem = $doc->createElement("segment");
+				$elem->setAttribute("id",$id);
+				$doc->appendChild($elem);
+			}
+			$fields = array("xml"=>$doc->saveXML(), "retrain"=>$retrain);
+		}
+		else {
+			$path = "segment/create";
+			$fields = array("id"=>$movieId, "retrain"=>$retrain);
+		}
+		
+		$res = $this->send($path, self::POST_METHOD, $fields);
+		
+		if($res != "ok") {
+			throw new Exception('Can not create segment');
+		}		
+		
+	}
+	
+	public function tagAdd($array, $retrain) {
+		// test if this is an associative array
+		// taken from http://www.php.net/manual/en/function.is-array.php#96724
+		$tags = null;
+		if($retrain)
+			$retrain = "true";
+		else
+			$retrain = "false";
+		
+		if(is_array($array) && (0 !== count(array_diff_key($array, array_keys(array_keys($array)))) || count($array)==0)) {
+			$tags = $array;
+		}
+		else {
+			$tags = array();
+			$tags[] = $array;
+		}
+		
+		$path = "tag/add";
+
+		
+		$doc = new DOMDocument();
+		$doc->appendChild($doc->createElement("sonyengine"));
+		foreach($tags as $tag) {
+			$elem = $doc->createElement("tag");
+			$elem->setAttribute("name",$tag["name"]);
+			$elem->setAttribute("segment",$tag["segment"]);
+			$doc->appendChild($elem);
+		}
+		$fields = array("xml"=>$doc->saveXML(), "retrain"=>$retrain);
+		
+		$res = $this->send($path, self::POST_METHOD, $fields);
+		
+		if($res != "ok") {
+			throw new Exception('Can not add tags');
+		}		
+
+	}
+
+}
\ No newline at end of file
--- a/web/thdProject/plugins/iriSonyenginePlugin/lib/iriSonyengineEventListener.class.php	Wed Mar 31 11:57:07 2010 +0200
+++ b/web/thdProject/plugins/iriSonyenginePlugin/lib/iriSonyengineEventListener.class.php	Tue Apr 06 15:28:02 2010 +0200
@@ -2,16 +2,82 @@
 
 class iriSonyengineEventListener
 {
-	static function listenToSearch(sfEvent $event) {
+	static function listenToFind(sfEvent $event) {
 		
 		$segment = $event["segment"];
 		$tag = $event["tag"];
-		$distance = $event["distance"];
+		if(isset($event["distantce"])) {
+			$distance = $event["distance"];
+		}
+		else {
+			$distance = "0.05";
+		}
 		if(!isset($event["more"]))
 		    $more = "false";
 		else
 			$more = $event["more"]?"true":"false";
 		
+		$client = SonyengineClient::getInstance();
+		$result = $client->engineFind($segment,$tag,$distance,$more);
 		
+		$event->setReturnValue($result);
+	}
+	
+	static function listenToEngineTrain(sfEvent $event) {
+		$client = SonyengineClient::getInstance();
+		$client->engineTrain();
+	}
+	
+	static function listenToEngineReset(sfEvent $event) {
+		$client = SonyengineClient::getInstance();
+		$client->engineReset();
+	}
+	
+	static function listenToSegmentCreate(sfEvent $event) {
+		
+		$client = SonyengineClient::getInstance();
+		$obj = $event['object'];
+
+		$objs = null;
+		
+		if(is_array($obj)) {
+			$objs = $obj;
+		}
+		else {
+			$objs = array();
+			$objs[] = $obj;
+		}
+		
+		$array = array();
+		foreach($obj as $obj_instance) {
+			$fields = $obj_instance->getSonyengineFields();
+			$array[] = $fields["id"];
+		}
+			
+		$client->segmentCreate($array, true);
+	}
+	
+	static function listenToTagAdd(sfEvent $event) {
+		
+		$client = SonyengineClient::getInstance();
+		$obj = $event['object'];
+
+		$objs = null;
+		
+		if(is_array($obj)) {
+			$objs = $obj;
+		}
+		else {
+			$objs = array();
+			$objs[] = $obj;
+		}
+		
+		$array = array();
+		foreach($obj as $obj_instance) {
+			$fields = $obj_instance->getSonyengineFields();
+			$array[] = array("segment"=>$fields["segment_id"],"name"=>$fields["name"]);
+		}
+		
+		$client->tagAdd($array, true);
 	}
 }
\ No newline at end of file