/usr/share/perl5/GIFT/CFeedbackClient.pm is in gnuift-perl 0.1.14-12.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 | #!/usr/bin/perl -w # -*- mode: perl -*-
# GIFT, a flexible content based image retrieval system.
# Copyright (C) 1998, 1999, 2000, 2001, 2002, CUI University of Geneva
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Author of this file: Wolfgang Müller
#
require 5.002;
#use strict;
use Socket;
use FileHandle;
use English;
use IO::File;
use IO::Socket;
use XML::Parser;
use CQueryParadigmMatcher;
=pod
It might be useful for a feedback client to treat multiple sessions at the same time and
to gather the corresponding information. The session data is stored in CSessions
=cut
package CFeedbackClient;
require Exporter;
@ISA= qw(Exporter
CQueryParadigmMatcher);
@EXPORT= qw(new
setAddress
doQueryGroup
startSession
saveSession
closeSession
configureSession
makeQueryString
sendQueryString
);
#############################################################
#
# A script for performing
# benchmarks on CBIRS systems
# which use MRML
#
#############################################################
#
#
# WARNING: this code is quick and dirty, but flexible.
# it will be difficult to make it multi-threaded, but
# this probably will not be interesting for a while
#
#
#############################################################
##################################################
#
# maintaining the xpath to the currently parsed tag
#
# what we are doing here is to have a path like
# /root-element-name/child-element-name/grandchild-element-name
# to the currently parsed XML element.
# In the example above, the root XML element of the document
# has the name "root-element-name", the currently parsed XML element
# has the name "grandchild-element-name"
##############################
#
# pushes the currently parsed XML element to the back of the path
#
sub pushToXMLPath( $ ){
my$self=shift;
my$inElement=shift;
push @{$self->{path}},$inElement;
}
##############################
#
# pops the last name from the path
#
sub popFromXMLPath( ){
my$self=shift;
return pop @{$self->{path}};
}
##############################
#
# clears the path
#
sub clearXMLPath( ){
my$self=shift;
$self->{path}=[];
}
##############################
#
# transforms the path to a string
#
sub getXMLPath( ){
my$self=shift;
"/".join("/",@{$self->{path}});
}
############################################################
#
# constructor
#
##############################
#
# new
#
sub new( ){
my $class = shift;
my $self = {};
bless $self, $class;
$self->initialize();
return $self;
}
##############################
#
# initialises this:
# create an XML parser
#
sub initialize(){
my$self=shift;
$self->clearResultList();
$self->{locQuery}=[];
$self->{parser} = new XML::Parser(Handlers => {Start => \&handleStart,
End => \&handleEnd,
});
# Make the parser know who is this structure
$self->{parser}->{mCaller}=$self;
$self->{parser}->{mMagic}=42;
###############################
#
# for quick changes what to look at when debugging/testing
# set the value for a given function to "1" for getting
# debugging code printed in this function
# warning: this is done by hand, there is nothing automatic...
#
# HM 070301 added debug switch others, so we can have a completely silent mode
$self->{vDEBUG}={
configureSession => 0,
startSession => 0,
addCollection => 0,
doFirstQueryStep => 0,
doQueryStep => 0,
readAndParse => 0,
buildNewQuery => 0,
handleStart => 0,
handleEnd => 0,
setRelevantImages => 0,
sendQueryString => 0,
others => 0,
};
#
# printing which functions are to be debugged
#
print "Printing debugging info for the functions:\n" if ($self->{debug}->{others});
{
my $i;
foreach $i (keys(%{$self->{vDEBUG}})){
if($self->{vDEBUG}->{$i}){
print "$i\n";
}
}
}
###############################
#
# Initialise using reasonable values
#
#
# initially no algorithms available and none chosen
#
$self->clearAlgorithm();
$self->clearCollection();
$self->setSessionID("");
#
# query for 20 images without cutoff
#
$self->setCutoff(0.0);
$self->setResultSize(20);
#
# talk to the localhost at port 12789
#
$self->setAddress("localhost:12789");
}
##############################
#
# Set a new address for the CBIR server
#
# Parameters:
# inAddress: Address in "server:port" format
# e.g. "localhost:12789"
sub setAddress( $ ){
my$self=shift;
my $inAddress=shift;
($self->{mHost},$self->{mPort})=split(":",$inAddress);
}
###############################
#
# Setting the array of relevant images
# the relevance feedback will be generated
# based on these images.
#
# Parameter:
# a list of URLs of relevant images
#
sub setRelevantImages{
my$self=shift;
#a list of the relevant images
$self->{gRelevantImages}=[];
@{$self->{gRelevantImages}}=@_;
#
# building a hash to look up
# if a given url belongs to an
# image from the list
#
my $i;
foreach $i (@{$self->{gRelevantImages}}){
print "relevant image: $i\n" if($self->{vDEBUG}->{setRelevantImages});
$self->{locRelevantHash}->{$i}=1;
}
}
###############################
#
# get the list of relevant images
#
sub getRelevantImages(){
my$self=shift;
return $self->{gRelevantImages};
}
#########################################
#
# HELPERS FOR XML WRITING
# (largely unused, but its a c++-reflex ;-)
# in perl things like that are more quickly done
# by hand)
#
#
# attribute-value pair to attribute="value"
#
sub toAttribute{
my$self=shift;
my $inName= shift;
my $inValue= shift;
return " $inName=\"$inValue\"";
}
#
# Elementname, list of attribute/value pairs => element start tag
#
sub toStartTag( $$$ ){
my$self=shift;
my $inEmpty=shift;
my $inName=shift;
my $inAttributes=shift;
my $lReturnValue="";
my $i;
foreach $i (values(%$inAttributes)){
$lReturnValue.=$self->toAttribute(@{$i});
}
if($inEmpty){
return "<$inName $lReturnValue/>\n";
}else{
return "<$inName $lReturnValue>\n";
}
}
#
# Element name to element end tag
#
sub toEndTag( $ ){
my$self=shift;
my $inName=shift;
return "</$inName>\n";
}
########################################
#
# making a query string
#
# PARAMETERS:
# lQueryImages: a REFERENCE to a list of URLs
#
# OPTIONAL:
#
# inSession: the MRML session-id of this session
# inAlgorithm: the number of the algorithm to be used
# inCollection: the number of the collection to be used.
# both numbers are the number in the sequence in which they
# were sent by the MRML server when the information was requested
# by this client
sub makeQueryString( $;$$$ ){
my$self=shift;
my $lQueryImages=shift;
my $inSession=(shift || $self->getSessionID());
my ($inAlgorithm)=$self->getAlgorithmID(shift);
my ($inCollection)=$self->getCollectionID(shift);
my $lAdditionalAlgorithmAttributes=shift || "";
my $inResultSize=$self->getResultSize(); # the desired result size
my $inCutoff=$self->getCutoff(); # the cutoff.
if($self->{vDEBUG}->{makeQueryString}){
print $inSession,"\n";
print $inResultSize,"\n";
print $inCutoff,"\n";
print $inCollection,"\n";
print $inAlgorithm,"\n";
}
#
# building the string "query-step" + headers
#
my $lResult=qq(<?xml version="1.0" standalone="no" ?>
<!DOCTYPE mrml SYSTEM "~/public_html/dtd/mrml.dtd">
<mrml session-id="$inSession">
<query-step
result-size="$inResultSize"
result-cutoff="$inCutoff"
collection-id="$inCollection"
algorithm-id="$inAlgorithm">
<user-relevance-element-list>);
foreach $i (sort {$$a[0] cmp $$b[0]} @$lQueryImages){
my($lURL,$lRelevance)=@$i;
$lResult.=qq(
<user-relevance-element
user-relevance="$lRelevance"
image-location="$lURL"/>
);
}
$lResult.=qq(</user-relevance-element-list>
</query-step>
</mrml>
);
return $lResult;
}
############################################################
#
# Handling incoming MRML
# using XML::Parser
#
########################################
#
# Building and maintaining a list of query results
#
##############################
#
# clear the list of results
#
sub clearResultList(){
my$self=shift;
$self->{locResultList}=[];
}
##############################
#
# add a URL-userrelevance pair to the list of results
# PARAMETERS:
# inURL the URL
# inUserRelevance the relevancelevel
sub addToResultList( $$ ){
my$self=shift;
my $inURL= shift;
my $inUserRelevance= shift;
push @{$self->{locResultList}},[$inURL,$inUserRelevance];
}
##############################
#
# get the list of URL-userrelevance pairs
# the result is a reference.
#
sub getResultList( ){
my$self=shift;
return $self->{locResultList};
}
##############################
#
# turns the list of URL-userrelevance pairs
# into a hash and returns a reference to this hash
#
sub getResultHash( ){
my$self=shift;
my @lResultList=@{$self->getResultList( )};
my $lReturnValue={};
foreach $i (@lResultList){
$lReturnValue->{$i->[0]}=$i->[1];
}
return $lReturnValue;
}
########################################
#
# Expat parser handlers
#
##############################
#
# handle the start of an element
#
sub handleStart( $$ ){
my $self=shift;
die "BLACK MAGIC!" if ($self->{mMagic}!=42);
$self=$self->{mCaller};
my $inElement=shift;
$self->pushToXMLPath($inElement);
my %inAttributes=@_;
my $lPath=$self->getXMLPath();
#
# print debugging info if necessary
#
if($self->{vDEBUG}->{handleStart}){
print("start ",$inElement,",",join(":",%inAttributes),"\n");
}
#
# add an algorithm to the list of possible algorithms
#
if($lPath=~m:algorithm-list/algorithm$:){
$self->addAlgorithm($inAttributes{"algorithm-type"});
}
#
# add a collection to the list of possible collections
#
if($lPath=~m:collection-list/collection$:){
$self->addCollection($inAttributes{"collection-id"});
}
#
# add a collection to the list of possible collections
#
if($lPath=~m:algorithm/query-paradigm-list/query-paradigm:){
if($self->{vDEBUG}->{handleStart}){
print "add-QP§§§§§§§§§§§§§$inAttributes{'interaction-type'}§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§\n";
}
$self->addQueryParadigmToCurrentAlgorithm(\%inAttributes);
}
if($lPath=~m:collection/query-paradigm-list/query-paradigm:){
$self->addQueryParadigmToCurrentCollection(\%inAttributes);
}
if($lPath eq "/mrml"){
$self->setSessionID($inAttributes{"session-id"});
}
if($inElement eq "query-result-element-list"){
$self->clearResultList();
}
#
#
#
if($inElement eq "query-result-element"){
$self->addToResultList($inAttributes{"image-location"},
$inAttributes{"calculated-similarity"});
}
}
##############################
#
# handle the end of an element
#
sub handleEnd( $ ){
my $self=shift;
$self=$self->{mCaller};
my $inElement=shift;
$self->popFromXMLPath($inElement);
if($inElement eq "query-result"){
$self->buildNewQuery($self->getResultList(),
$self->{locRelevantHash});
}
print "end ",$inElement,"\n" if($self->{vDEBUG}->{handleEnd});
}
############################################################
#
# Accessors
#
#
###############################
#
# The following three functions support you in
# opening several sessions
#
##############################
#
# Clear the list of open sessions
#
sub clearSessionIDList(){
my$self=shift;
$self->{mSessionIDList}=[];
}
##############################
#
# add an MRML session-id to the list of open sessions
#
sub addSessionIDToList( $ ){
my$self=shift;
my $lAddedItem= shift || $self->getSessionID();
$self->clearSessionID() unless defined($self->{mSessionIDList});
push @{$self->{mSessionIDList}},
}
##############################
#
# get either the last or the nth session-id
# from the list
# PARAMETER
# inN : the n-th session-id in the list is to be returned
sub getSessionIDFromList( ;$ ){
my$self=shift;
return $self->{mSessionIDList}->[(0 || shift)];
}
###############################
#
# setSessionID is used at each mrml-message
# to set the SessionID
# TO THE ONE USED IN THE LAST MESSAGE
#
# remark: THIS IS NOT BEAUTIFUL
#
sub setSessionID( $ ){
my$self=shift;
$self->{gSessionID}=shift;
}
#
# get the sessionID as written by the function above
#
sub getSessionID( ){
my$self=shift;
return $self->{gSessionID} if defined($self->{gSessionID});
return "no-session-id-available";
}
########################################
#
# creating/maintaining a list of all algorithms available
#
#
##############################
#
# clear
#
sub clearAlgorithm( ){
my$self=shift;
return $self->{algorithm}=[];
}
##############################
#
# add to the "current" algorithm a query-paradigm.
# the "current" algorithm is defined as the one
# containing the query-paradigm tag in question
#
sub addQueryParadigmToCurrentAlgorithm( $ ){
my $self=shift;
my $lNewQueryParadigm=shift;
if($self->{vDEBUG}->{addQueryParadigmToCurrentAlgorithm}){
print "addQueryParadigmToCurrentAlgorithm Acting on algorithm $self->{algorithm}->[-1]->{'algorithm-id'}\n";
}
$self->{algorithm}->[-1]->{"query-paradigm-list"}=[]
unless defined($self->{algorithm}->[-1]->{"query-paradigm-list"});
# add this paradigm to the hash of query paradigms
push @{$self->{algorithm}->[-1]->{"query-paradigm-list"}},$lNewQueryParadigm;
}
##############################
#
# add an algorithm to the list of available algorithms
#
sub addAlgorithm( $ ){
my$self=shift;
my $lAlgorithmID=shift;
my $lNewAlgorithm={"algorithm-id" => $lAlgorithmID,
"query-paradigm-list" => []};
print("\naddalgorithm ",join(",",@{$lNewAlgorithm}),"\n") if $vDEBUG{addAlgorithm};
$self->clearAlgorithm() unless defined $self->{algorithm};
push @{$self->{algorithm}},$lNewAlgorithm;
}
##############################
#
# get either the 0th or the Nth algorithm from the list of available algorithms
# Parameters: N
#
# RETURNVALUE: the algorithm given back by this is a hash containing the algorithm-id
# as well as the list of available query paradigms
#
sub getAlgorithm( ;$){
my$self=shift;
return $self->{algorithm}->[(shift || 0)];
}
##############################
#
# get either the 0th or the Nth algorithm-id from the list of available algorithms
#
#
sub getAlgorithmID( ;$){
my$self=shift;
my $inAlgorithmNumber=shift;
if($inAlgorithmNumber=~m/[^0-9 \t]/){
return $inAlgorithmNumber;#in fact, the algorithm number was already an ID
}
return $self->getAlgorithm($inAlgorithmNumber||0)->{"algorithm-id"} if defined($self->getAlgorithm($inAlgorithmNumber||0)->{"algorithm-id"});
return "adefault";
}
##############################
#
# get either the 0th or the Nth algorithm-type from the list of available algorithms
#
# XXX: CHECK IF THIS FUNCTION IS USED
#
sub getAlgorithmType( ;$){
my$self=shift;
return $self->getAlgorithm(shift||0)->{"algorithm-type"};
}
############################################################
#
# Making and maintaining a list of all collections available
#
#
sub clearCollection( ){
my$self=shift;
return $self->{collection}=[];
}
##############################
#
# adding a query paradigm to the list of query-paradigms allowed for this collection
#
#
sub addQueryParadigmToCurrentCollection( $ ){
my $self=shift;
my $lNewParadigm=shift;
# add this paradigm to the hash of query paradigms
push @{$self->{collection}->[-1]->{"query-paradigm-list"}},$lNewQueryParadigm;
}
##############################
#
# adding a collection to this list
# PARAMETER: ID of the collection
#
sub addCollection( $ ){
my$self=shift;
my $lCollectionID=shift;
my $lNewCollection={"collection-id" => $lCollectionID,
"query-paradigm-list" => []};
print("\nxxx addcollection $lCollectionID:",join(",",(%{$lNewCollection})),"\n") if $vDEBUG{addCollection};
$self->clearCollection() unless defined $self->{collection};
push @{$self->{collection}},$lNewCollection;
}
##############################
#
# Get a hash describing the Nth collection
# PARAMETER: N/CollectionID
#
sub getCollection( ;$){
my$self=shift;
my $inCollectionID=shift;
if($inCollectionID=~m/[a-zA-Z]/){
foreach $i (@{$self->{collection}}){
print "\ncollection-id: $i->{'collection-id'}\n";
if($i->{"collection-id"} eq $inCollectionID){
return $i;
}
}
die "in CFeedbackClient::getCollection Could not get any collection matching $inCollectionID "
}
return $self->{collection}->[($inCollectionID || 0)];
}
##############################
#
# Get the collection ID of the Nth collection
#
sub getCollectionID( ;$){
my$self=shift;
my $inCollectionNumber=shift;
if($inCollectionNumber=~m/[^0-9 \t]/){
return $inCollectionNumber;#in fact, the collection number was already an ID
}
return $self->getCollection(shift||0)->{"collection-id"};
}
##############################
#
# Find out which algorithms fit your preferred query paradigm
# PARAMETERS: inParadigm, a query paradigm element represented
# by a hash from attriute to value
sub getAlgorithmsByParadigm( $ ){
my$self=shift;
my $inParadigm=shift;
my @lResult;
foreach $i (@{$self->{algorithm}}){
if($self->isMatchingQueryParadigmList($i->{"query-paradigm-list"},
$inParadigm)){
print "found ",$i->{"algorithm-id"},":",join(",",(%$i)),"\n";
push @lResult,$i;
}
}
return @lResult;
}
##############################
#
# Find out which algorithms fit your preferred query paradigm and collection
#
sub getAlgorithmsByParadigmAndCollection( $$ ){
my$self=shift;
my $inParadigm=shift;
my $inCollection=shift;
my @lResult;
foreach $i (@{$self->{algorithm}}){
if(($self->isMatchingQueryParadigmList($inParadigm,
$i->{"query-paradigm-list"}))){
print ("HIER\n");
if(($self->isMatchingQueryParadigmList($i->{"query-paradigm-list"},
$inCollection->{"query-paradigm-list"})))
{
print "found algorithm ",$i->{"algorithm-id"},":",join(",",(%$i)),"\n";
push @lResult,$i;
}
}
}
return @lResult;
}
sub getCollectionsByParadigm( $ ){
my$self=shift;
my $inParadigm=shift;
my @lResult;
foreach $i (@{$self->{collection}}){
if($self->isMatchingQueryParadigmList($i->{"query-paradigm-list"},
$inParadigm)){
print "found ",$i->{"collection-id"},":",join(",",(%$i)),"\n";
push @lResult,$i->{"collection-id"};
}
}
return @lResult;
}
sub collectionToParadigmList( $ ){
my$self=shift;
my $inCollection=shift;
return $inCollection->{"query-paradigm-list"};
}
sub collectionToID( $ ){
my$self=shift;
my $inCollection=shift;
return $inCollection->{"collection-id"};
}
sub algorithmToParadigmList( $ ){
my$self=shift;
my $inAlgorithm=shift;
return $inAlgorithm->{"query-paradigm-list"};
}
sub algorithmToID( $ ){
my$self=shift;
my $inAlgorithm=shift;
return $inAlgorithm->{"algorithm-id"};
}
##############################
#
# Setting the desired maximum
# size of the result of the next query
#
#
sub getResultSize( ){
my$self=shift;
return $self->{gResultSize};
}
sub setResultSize( $ ){
my$self=shift;
$self->{gResultSize}=shift;
}
##############################
#
# Setting the "cutoff": minimal desired calculated relevance
# of the retrieved result
#
sub getCutoff( ){
my$self=shift;
return $self->{gCutoff};
}
sub setCutoff( $ ){
my$self=shift;
$self->{gCutoff}=shift;
}
##############################
#
# Build a new query using makeQueryString
# out of a list of retrieved images.
#
#
# In fact this is the main function which users of this class
# will need to overload
#
sub buildNewQuery( $$ ){
my$self=shift;
#a reference to an ARRAY containing the retrieved images
my $inRetrieved=shift;
if($self->{vDEBUG}->{buildNewQuery}){
print "--------------------buildNewQuery";
my $i;
foreach $i (@$inRetrieved){
print "buildNewQuery: $$i[0]\n";
};
}
#a reference to a HASH containing the relevant images as keys
my $inRelevantHash=shift;
# this array has to be filled up using the feedback algorithm
my @outQueryImages=();
##################################################
#
# This is the real feedback algorithm
# Heirs put changements here!
#
# in this case:
{#these braces are there for scoping reasons
my $i;
foreach $i (@$inRetrieved){#for each retrieved image,...
if($$inRelevantHash{$$i[0]}){#...which is in the hash of relevant images...
push @outQueryImages,[$$i[0],1];#...add it to the query.
#... and give some output , if requested
print "adding query image $$i[0]\n" if $self->{vDEBUG}->{buildNewQuery};
}
}
}#these braces are there for scoping reasons
#
# this was the feedback algorithm
#
##################################################
# die qq(Error in feedback generating algorithm: $@) if $@;
$locQuery=$self->makeQueryString(\@outQueryImages,
$self->getSessionID(0),
$self->getAlgorithmID(0),
$self->getCollectionID(0),
);
}
#############################################################
#
#
# NETWORKING
#
#
sub old_opensock( ;$$ ){
my$self=shift;
#snipped from the perlbook
my($remote,$port,$iaddr,$paddr,$proto,$line,$newport);
$remote = shift || 'localhost';
$port = shift || 12789;
$newport=$port;
if($port=~/\D/){$newport=getservbyname($newport,'tcp');}
die "No port" unless $newport;
$iaddr = main::inet_aton($remote) or die "no host: $remote";
$paddr = main::sockaddr_in($newport,$iaddr);
$proto = getprotobyname('tcp');
socket(SOCK,main::PF_INET,main::SOCK_STREAM,$proto) or die "socket: $!";
connect(SOCK,$paddr) or die "connect: $!";
autoflush SOCK 1;
autoflush STDOUT 1;
}
#############################################################
#
#
# NETWORKING open a socket
# PARAMETERS: remote host, port.
#
# by default the values of $self->{mHost} and $self->{mPort};
# are taken.
sub opensock( ;$$ ){
my$self=shift;
#snipped from man IO::Socket
my($remote,$port,$iaddr,$paddr,$proto,$line,$newport);
$remote = shift || $self->{mHost};
$port = shift || $self->{mPort};
autoflush STDOUT 1;
print "connecting to: $remote:$port\n" if $vDEBUG{opensock};
$self->{mSocket} = IO::Socket::INET->new(PeerAddr => $remote,
PeerPort => $port,
Proto => 'tcp');
print "finished making the socket" if $vDEBUG{opensock};
$self->{mSocket}->autoflush(1);
}
###############################
#
# read from the socket and parse.
# returns the string read.
#
sub readAndParse( ){
my$self=shift;
print "\nNow I will try to read...\n" if($self->{vDEBUG}->{readAndParse});
#parsing directly from the stream
# $self->{parser}->parse($self->{mSocket});
# "\nthis value should not be used!\n";
#we read, and then we parse
#for this reason, we can return the
#string read
{
my $lRead=join("",$self->{mSocket}->getlines());
if($lRead){
if($self->{vDEBUG}->{readAndParse}){
print "Parsing: $lRead";
$lOF= new IO::File;
$lOF->open(">mrml-parsing-log");
print $lOF $lRead;
print "--\n";
}
#
$self->clearXMLPath();
$self->{parser}->parse($lRead);
}
$lRead;
}
}
###############################
#
# sends an ihandshake message
# to the server.
#
sub startSession(){
my$self=shift;
my $lUser=shift || "default-user";
my $lSession=shift || "--feedback-client-session--";
$self->opensock();
$_= qq(<?xml version="1.0" standalone="no" ?>
<!DOCTYPE mrml SYSTEM "file:/usr/share/mrml.dtd">
<mrml session-id="nothing">
<open-session user-id="$lUser" session-name="--feedback-client-session--"/>
<get-algorithms />
<get-collections />
</mrml>
);
$self->{mSocket}->print($_);
print "Sent: $_" if($vDEBUG{startSession});
$self->readAndParse();
}
###############################
#
# Save the state of the session on the server
#
# FIXME: presently this is empty
#
sub saveSession(){
my$self=shift;
}
###############################
#
# close the session
#
# FIXME: presently this is empty
#
sub closeSession(){
my$self=shift;
}
###############################
#
# FOR TESTING ONLY: TRYING TO SIMULATE A SESSION
#
# configures an algorithm
# to the server.
# HM 060201 added parametres that are needed for the benchmark
#
sub configureSession(;$$$$$$$$$ ){
my$self=shift;
my $inSessionID=shift || $self->getSessionID();
my $inAlgorithmID=shift || $self->getAlgorithmID();
my $inCollectionID=shift || $self->getCollectionID();
my $inAdditionalAlgorithmAttributes=shift || "";
my $inColorHistogramBlocked=shift || "false";
my $inColorBlocksBlocked=shift || "false";
my $inGaborHistogramBlocked=shift || "false";
my $inGaborBlocksBlocked=shift || "false";
my $inFeaturePercentage=shift || "100";
my $MESSAGEFILE="/usr/share/gift-iconfigure.mrml";
$self->opensock();
###############################
#
# We read a prefabricated configuration message, in which we
# replace the appropriate attribute values
#
# open MESSAGEFILE,$MESSAGEFILE or die "There is no $MESSAGEFILE: $!";
# while(<MESSAGEFILE>){
# s/PPsessionidPP/$inSessionID/;
# s/PPalgorithmidPP/$inAlgorithmID/;
# s/PPcollectionidPP/$inCollectionID/;
# s/PPadditionalalgorithmattributesPP/$inAdditionalAlgorithmAttributes/;
# $self->{mSocket}->print($_);
# print "Sent: $_" if $self->{vDEBUG}->{configureSession};
# }
$_= qq(<?xml version="1.0" standalone="no" ?>
<!DOCTYPE mrml SYSTEM "http://isrpc85.epfl.ch/Charmer/code/mrml.dtd">
<mrml session-id="$inSessionID">
<configure-session session-id="$inSessionID" >
<algorithm
algorithm-id="$inAlgorithmID"
algorithm-type="adefault"
collection-id="$inCollectionID"
cui-block-color-histogram="$inColorHistogramBlocked"
cui-block-color-blocks="$inColorBlocksBlocked"
cui-block-texture-histogram="$inGaborHistogramBlocked"
cui-block-texture-blocks="$inGaborBlocksBlocked"
cui-pr-percentage-of-features="$inFeaturePercentage" >
</algorithm>
</configure-session>
<query-step session-id="$inSessionID" result-size="30" algorithm-id="$inAlgorithmID" collection="$inCollectionID"/>
</mrml>);
$self->{mSocket}->print($_);
print "Sent: $_" if($vDEBUG{startSession});
$self->readAndParse();
}
##############################
#
# Send a query to the server
#
sub sendQueryString( $ ){
my$self=shift;
my $lQuery=shift;
$self->opensock();
if($self->{vDEBUG}->{sendQueryString}){
print "sendQueryString++++++++++++++++++++++++++++++Sending query....\n\n";
print $lQuery;
print "\nsendQueryString------------------------------Sending query....\n\n";
}
$self->{mSocket}->print($lQuery);
$self->readAndParse();
$self->{mSocket}->close();
}
##############################
#
# generate a query and feed it to the server.
# subsequent queries will be generated by the xml parser
# and its helpers.
sub doFirstQueryStep( $;$$$ ){
my$self=shift;
my $lInstruction=shift;
my $lQuery=shift || [[$self->getRelevantImages()->[0],1]];
my $inSession=(shift || ($self->getSessionID(0)));
my $inAlgorithm=(shift || ($self->getAlgorithmID(0)));
my $inCollection=(shift || ($self->getCollectionID(0)));
my $lQueryString;
if($lInstruction eq "query-step"){
$lQueryString=$self->makeQueryString($lQuery,
$inSession,
$inAlgorithm,
$inCollection);
}
if($lInstruction eq "cui-hierarchy-up"){
my $inResultSize=$self->getResultSize();
my $inCutoff=$self->getCutoff();
$lQueryString=qq(<?xml version="1.0" standalone="no" ?>
<!DOCTYPE mrml SYSTEM "~/public_html/dtd/mrml.dtd">
<mrml session-id=\"$inSession\">
<query-step
result-size="$inResultSize"
result-cutoff="$inCutoff"
collection-id="$inCollection"
algorithm-id="$inAlgorithm">
<cui-hierarchy-up/>
</query-step>
</mrml>);
}
if($self->{vDEBUG}->{doFirstQueryStep}){
print "doFirstQueryStep++++++++++++++++++++++++++++++Sending query....\n\n";
}
$self->sendQueryString($lQueryString);
{
print "\ndoFirstQueryStep------------------------------Sending query....\n\n";
}
return $lQueryString;
}
###############################
#
# send a feedback query, if the same query was not performed
# in the last step.
#
sub doQueryStep( $$ ){
my$self=shift;
my $inCount=shift;
my $lOld=shift;
my $lNow=$locQuery;
if ($lOld ne $lNow){
if($self->{vDEBUG}->{doQueryStep}){
print "$inCount ++++++++++++++++++++++++++++++Sending query....\n\n";
print $locQuery;
print "\n$inCount ------------------------------Sending query....\n\n";
}
$self->opensock();
$self->{mSocket}->print($locQuery);
$self->readAndParse();
return $lNow;
}else{
if($self->{vDEBUG}->{doQueryStep}){
print "$inCount ++NOT SENDING QUERY\n";
print $locQuery;
print "\n$inCount --NOT SENDING QUERY\n";
}
}
return "";
}
############################################################
#
# perform a group of queries. This can be a browsing
# query process or a simple sequence of next neigbour
# query feedback steps. What it really is depends on the
# heir which overloads doQueryStep
#
# PARAMETERS:
# $inImages reference to a list containing the relevant images
#
sub doQueryGroup( $ ){
my$self=shift;
my $inImages=shift;
$self->setRelevantImages(@$inImages);
$self->startSession("default-user");
$self->configureSession();
{
my $lOldQuery=$self->doFirstQueryStep("query-step");
my $lCount=1;
while(($lOldQuery=$self->doQueryStep($lCount,
$lOldQuery))
&&
($lCount<100)){
++$lCount;
};
}
}
|