Skip to content

Commit

Permalink
Merge pull request #214 from ChHaeni/HashSign
Browse files Browse the repository at this point in the history
Add function to get sign of hash entries
  • Loading branch information
zaucker authored Oct 12, 2020
2 parents 68bc6a9 + 776fc88 commit 4099c9c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
7 changes: 7 additions & 0 deletions lib/Agrammon/Formula/Builtins.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ sub get-builtins is export {
warn @message.join || 'Warning';
},
abs => &abs,
sign => &sign,
# Construct a filter group from data
filterGroup => &filter-group,
# Turn a filter group into a simple scalar value
scalar => &filter-group-scalar,
# Get sign of all values in a filter group
hashsign => -> $filter-group {
die "hashsign operator expects a filter group as its first argument"
unless $filter-group ~~ Agrammon::Outputs::FilterGroupCollection;
$filter-group.hashsign()
},
# Scale all values in a filter group by the given multiplier
scale => -> $filter-group, $multiplier {
die "scale operator expects a filter group as its first argument"
Expand Down
5 changes: 5 additions & 0 deletions lib/Agrammon/Outputs/FilterGroupCollection.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ class Agrammon::Outputs::FilterGroupCollection {
self.bless: instances => %!values-by-filter.map({ .key => $factor + .value })
}

#| Produce a new filter group collection which has the sign of this one.
method hashsign() {
self.bless: instances => %!values-by-filter.map({ .key => sign( .value ) })
}

#| Apply an operation pairwise between this group collection and another one, returning a
#| new group collection as the result. When a group exists in collections, then the operation
#| is applied to their values. When a group exists on only one side, the base value is used.
Expand Down
15 changes: 15 additions & 0 deletions t/output-filter-group-collection.t
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ given Agrammon::Outputs::FilterGroupCollection.from-scalar(0) {
'Correct results by filter group after adding scalar';
}
}

{
my $group = Agrammon::Outputs::FilterGroupCollection.from-filter-to-value-pairs:
[{ac => 'blue cow'} => -4, {ac => 'pink cow'} => 0, {ac => 'green cow'} => 31, {ac => 'blue cow'} => 2];
given $group.hashsign() {
isa-ok $_, Agrammon::Outputs::FilterGroupCollection,
'Get another filter group back after applying function hashsign';
is +$group, 29, 'Original filter group is not changed in place';
is +$_, 0, 'Total numeric value is correct after applying function hashsign';
is-deeply norm(.results-by-filter-group),
norm([{ac => 'blue cow'} => -1, { ac => 'pink cow' } => 0, { ac => 'green cow' } => 1]),
'Correct results by filter group after applying function hashsign';
}
}

{
my $group-a = Agrammon::Outputs::FilterGroupCollection.from-filter-to-value-pairs:
[{ac => 'blue cow'} => 4, {ac => 'pink cow'} => 7, {ac => 'blue cow'} => 31];
Expand Down
25 changes: 3 additions & 22 deletions t/test-data/Models/hr-inclNOxExtendedWithFilters/Livestock.nhd
Original file line number Diff line number Diff line change
Expand Up @@ -163,30 +163,11 @@ gui = Livestock,Tierhaltung,Production animale,Livestock
####################################################
##### Prepare animal master categories

+is_cattle
+test
print = check
++units
en =
de =
fr =
++description
True, if belongs to master category _cattle_.
++formula
add(
scale(
Sum(tan_excretion, Livestock::OtherCattle::Excretion) P+
Sum(tan_excretion, Livestock::DairyCow::Excretion),
0 ),
1 ) P+
scale(
Sum(tan_excretion, Livestock::Pig::Excretion) P+
Sum(tan_excretion, Livestock::FatteningPigs::Excretion) P+
Sum(tan_excretion, Livestock::Equides::Excretion) P+
Sum(tan_excretion, Livestock::SmallRuminants::Excretion) P+
Sum(tan_excretion, Livestock::RoughageConsuming::Excretion) P+
Sum(tan_excretion, Livestock::Poultry::Excretion),
0 );

hashsign( Sum(n_from_poultry_layers_growers_other_poultry,Livestock::Poultry) );

####################################################


Expand Down

0 comments on commit 4099c9c

Please sign in to comment.