# HG changeset patch # User ymh # Date 1270560482 -7200 # Node ID 66b32754f6b035f3771c2e0545bcfbea7a2a1fa2 # Parent 47df1cde32e9aa5819dafcf53748d200dcf38cdc (no commit message) diff -r 47df1cde32e9 -r 66b32754f6b0 engine/sonyengine/mosatags.sonyengine.war Binary file engine/sonyengine/mosatags.sonyengine.war has changed diff -r 47df1cde32e9 -r 66b32754f6b0 web/thdProject/plugins/iriSonyenginePlugin/config/iriSonyenginePluginConfiguration.class.php --- 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 diff -r 47df1cde32e9 -r 66b32754f6b0 web/thdProject/plugins/iriSonyenginePlugin/lib/client/SonyengineClient.php --- /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 @@ +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 diff -r 47df1cde32e9 -r 66b32754f6b0 web/thdProject/plugins/iriSonyenginePlugin/lib/iriSonyengineEventListener.class.php --- 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