<commit_before>
=head1 LICENSE
   Copyright 2016 EMBL - European Bioinformatics Institute
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
     http://www.apache.org/licenses/LICENSE-2.0
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
=cut

package Bio::Metadata::Loader::RuleSetLoaderRole;

use strict;
use warnings;

use Carp;
use Moose::Role;

use JSON qw(decode_json);
use Try::Tiny;
use autodie;
use Data::Dumper;
use MooseX::Params::Validate;
use Bio::Metadata::Types;
use Bio::Metadata::Validate::Support::OlsLookup;

use Unicode::CaseFold;

has 'ols_lookup' => (
  is      => 'rw',
  isa     => 'Bio::Metadata::Validate::Support::OlsLookup',
  default => sub { return Bio::Metadata::Validate::Support::OlsLookup->new() }
);

requires 'load';

around 'load' => sub {
  my $orig = shift @_;
  my $self = shift @_;
  my @args = @_;

  my $rule_set   = $self->$orig(@args);
  my $ols_lookup = $self->ols_lookup;

  for my $g ( $rule_set->all_rule_groups ) {

    #import extra rules
    for my $i ( $g->all_imports ) {
      my $organised_rules = $g->organised_rules;

      #import, unless rule with same name already known in the group
      my $terms = $ols_lookup->matching_terms( $i->term );

      my @created_rules = map { $i->create_rule($_) } @$terms;
      my @rules_to_add =
        grep { !exists $organised_rules->{ fc $_->name } } @created_rules;
      my @elided_rules =
        grep { exists $organised_rules->{ fc $_->name } } @created_rules;

      $g->add_imported_rule(@rules_to_add);

      my $num_rules_added  = scalar(@rules_to_add);
      my $num_rules_elided = scalar(@elided_rules);

      print "Imported $num_rules_added rules into "
        . $g->name
        . ' from '
        . $i->term->term_iri
        . ". $num_rules_elided rules were elided$/";
    }
  }

  return $rule_set;
};

1;<commit_msg>remove print commands, can appear on page in production<commit_after>
=head1 LICENSE
   Copyright 2016 EMBL - European Bioinformatics Institute
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
     http://www.apache.org/licenses/LICENSE-2.0
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
=cut

package Bio::Metadata::Loader::RuleSetLoaderRole;

use strict;
use warnings;

use Carp;
use Moose::Role;

use JSON qw(decode_json);
use Try::Tiny;
use autodie;
use Data::Dumper;
use MooseX::Params::Validate;
use Bio::Metadata::Types;
use Bio::Metadata::Validate::Support::OlsLookup;

use Unicode::CaseFold;

has 'ols_lookup' => (
  is      => 'rw',
  isa     => 'Bio::Metadata::Validate::Support::OlsLookup',
  default => sub { return Bio::Metadata::Validate::Support::OlsLookup->new() }
);

requires 'load';

around 'load' => sub {
  my $orig = shift @_;
  my $self = shift @_;
  my @args = @_;

  my $rule_set   = $self->$orig(@args);
  my $ols_lookup = $self->ols_lookup;

  for my $g ( $rule_set->all_rule_groups ) {

    #import extra rules
    for my $i ( $g->all_imports ) {
      my $organised_rules = $g->organised_rules;

      #import, unless rule with same name already known in the group
      my $terms = $ols_lookup->matching_terms( $i->term );

      my @created_rules = map { $i->create_rule($_) } @$terms;
      my @rules_to_add =
        grep { !exists $organised_rules->{ fc $_->name } } @created_rules;
      my @elided_rules =
        grep { exists $organised_rules->{ fc $_->name } } @created_rules;

      $g->add_imported_rule(@rules_to_add);

      my $num_rules_added  = scalar(@rules_to_add);
      my $num_rules_elided = scalar(@elided_rules);
    }
  }

  return $rule_set;
};

1;
{'fraction_non_alphanumeric': 0.12266973532796317, 'fraction_numerical': 0.004602991944764097, 'mean_word_length': 3.5507853403141363, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3423999', 'n_tokens_mistral': 1422, 'n_tokens_neox': 1382, 'n_words': 427}