Skip to content

Commit

Permalink
Merge pull request #212 from ChHaeni/NewFunction
Browse files Browse the repository at this point in the history
Add new function to add a scalar to a filter group hash
  • Loading branch information
zaucker authored Oct 12, 2020
2 parents 25d890e + 574d655 commit 68bc6a9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/Agrammon/Formula/Builtins.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ sub get-builtins is export {
unless $filter-group ~~ Agrammon::Outputs::FilterGroupCollection;
$filter-group.scale(+$multiplier)
},
# Add a scalar to all values in a filter group
add => -> $filter-group, $additor {
die "add operator expects a filter group as its first argument"
unless $filter-group ~~ Agrammon::Outputs::FilterGroupCollection;
$filter-group.add(+$additor)
},
# P+ compiles into this
addPairwise => -> $a, $b {
my $ag = as-filter-group($a);
Expand Down
8 changes: 8 additions & 0 deletions lib/Agrammon/Outputs/FilterGroupCollection.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ class Agrammon::Outputs::FilterGroupCollection {
self.bless: instances => %!values-by-filter.map({ .key => $factor * .value })
}

#| Produce a new filter group collection which has the values of this one added to
#| a specified additor. This can be used to implement `scalar + group`, `group + scalar`
#| (these two just commute), and `group - scalar` (by passing in `- scalar` as the
#| additor).
method add(Numeric $factor --> Agrammon::Outputs::FilterGroupCollection) {
self.bless: instances => %!values-by-filter.map({ .key => $factor + .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
13 changes: 13 additions & 0 deletions t/output-filter-group-collection.t
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ given Agrammon::Outputs::FilterGroupCollection.from-scalar(0) {
}
}

{
my $group = Agrammon::Outputs::FilterGroupCollection.from-filter-to-value-pairs:
[{ac => 'blue cow'} => 4, {ac => 'pink cow'} => 7, {ac => 'blue cow'} => 31];
given $group.add(2) {
isa-ok $_, Agrammon::Outputs::FilterGroupCollection,
'Get another filter group back after adding scalar';
is +$group, 42, 'Original filter group is not changed in place';
is +$_, 46, 'Total numeric value is correct after adding scalar';
is-deeply norm(.results-by-filter-group),
norm([{ac => 'blue cow'} => 37, { ac => 'pink cow' } => 9]),
'Correct results by filter group after adding scalar';
}
}
{
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
31 changes: 31 additions & 0 deletions t/test-data/Models/hr-inclNOxExtendedWithFilters/Livestock.nhd
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,37 @@ gui = Livestock,Tierhaltung,Production animale,Livestock



####################################################
##### Prepare animal master categories

+is_cattle
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 );

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



####################################################
##### n out livestock liquid (into storage)

Expand Down

0 comments on commit 68bc6a9

Please sign in to comment.