sub
new {
my
$class
=
shift
;
my
$self
= (
$#_
== 0) ? { %{ (
shift
) } } : {
@_
};
return
bless
$self
,
$class
;
}
sub
id {
my
(
$self
) =
@_
;
return
$self
->{_};
}
sub
time
{
return
$_
[0]->{
'time'
};
}
sub
facts {
my
$self
=
shift
;
return
$self
->{f}
if
exists
$self
->{f};
return
$self
->{facts}
if
exists
$self
->{facts};
$self
->{f} = [];
return
$self
->{f};
}
sub
combinations {
my
$self
=
shift
;
return
$self
->{c}
if
exists
$self
->{c};
return
$self
->{combiners}
if
exists
$self
->{combiners};
$self
->{c} = {};
return
$self
->{c};
}
sub
combination {
my
(
$self
,
$combiner
) =
@_
;
return
()
unless
$self
->combinations;
return
$self
->combinations->{
$combiner
};
}
sub
combiner_names {
my
(
$self
) =
@_
;
return
()
unless
$self
->combinations;
return
keys
%{
$self
->combinations };
}
sub
_combination_get {
my
(
$self
,
$key
,
$combiner
) =
@_
;
return
undef
unless
$self
->combinations;
if
(
$combiner
) {
my
$combination
=
$self
->combination(
$combiner
);
return
undef
unless
$combination
;
return
$combination
->{
$key
};
}
my
$combination
=
$self
->combination(
'default'
);
if
(
$combination
) {
return
$combination
->{
$key
};
}
foreach
(
values
%{
$self
->combinations }) {
return
$_
->{
$key
}
if
exists
$_
->{
$key
};
}
return
undef
;
}
sub
value {
my
$self
=
shift
;
return
$self
->_combination_get(
'v'
,
@_
);
}
sub
data {
my
$self
=
shift
;
return
$self
->_combination_get(
'd'
,
@_
);
}
sub
error {
return
$_
[0]->{error};
}
sub
message {
return
$_
[0]->{message};
}
sub
as_string {
my
(
$self
) =
@_
;
my
$out
=
"Response id '$self->{_}': "
;
$out
=
$out
.
$self
->
time
.
"ms, "
;
my
@names
=
$self
->combiner_names;
$out
=
$out
.
scalar
(
@names
) .
" combinations, "
;
$out
=
$out
.
scalar
(@{
$self
->facts }) .
" facts\n"
;
if
(
$self
->error) {
$out
.=
"Error: "
.
$self
->message .
"\n"
;
}
else
{
if
(
$self
->message) {
$out
.=
"Warning: "
.
$self
->message .
"\n"
;
}
foreach
(
@names
) {
my
$value
=
$self
->value(
$_
);
my
$data
=
$self
->data(
$_
);
$data
=
'(undef)'
unless
defined
$data
;
$out
.=
"Combiner '$_': verdict $value ($data)\n"
;
}
foreach
(@{
$self
->facts}) {
my
$d
=
$_
->{d};
$d
=
"null data"
unless
defined
$d
;
$out
.=
"Feed '$_->{f}':"
;
$out
.=
" identity '$_->{i}'"
if
exists
$_
->{i};
$out
.=
" opinion $_->{v} ($d)\n"
;
}
}
return
$out
;
}
1;