AI-MicroStructure

 view release on metacpan or  search on metacpan

lib/AI/MicroStructure.pm  view on Meta::CPAN



BEGIN{
use File::HomeDir;
my $fileDir = File::HomeDir->my_home . "/data-hub/structures/";
my $fileSpec = File::HomeDir->my_home . "/data-hub/structures/any.pm";
if ( -e $fileSpec ) {


} else {
    mkdir($fileDir);
    warn "missing:$fileSpec";

}

}

sub getComponents{
  my $x= {};
  $x->{"all_structures"} = [keys %MICRO];
  $x->{"count_struct"} = sprintf(keys %MICRO);
  $x->{"structures"} = {};

     foreach my $con (@{$x->{"all_structures"}}){
      next unless($con!~/any/);
      my @in = split("\n",eval{`cat $state->{path}->{"cwd/structures"}/$con.pm`;});

      $x->{"structures"}->{$state->{path}->{"cwd/structures"}}->{$con}->{name} =
        [grep{$_}grep {!/(our|my|use|sub|use|package|#|__|1)/}split("\n",`cat $state->{path}->{"cwd/structures"}/$con.pm`)];#,

      $x->{"structures"}->{$state->{path}->{"cwd/structures"}}->{$con}->{files}  =
       [split("\n",`ls -R  /home/santex/repos/KnowledgeInterDisciplinary/data/json | egrep -i "($con)";`)];
    }

 return $x;
}
sub import {
    my $class = shift;
    my @structures = ( grep { $_ eq ':all' } @_ )
      ? ( 'foo', grep { !/^(?:foo|:all)$/ } keys %MICRO  ) # 'foo' is still first
      : @_;
    $Structure = $structures[0] if @structures;
    $micro = AI::MicroStructure->new( $Structure );
    # export the microname() function
    no strict 'refs';
    my $callpkg = caller;
    *{"$callpkg\::microname"} = \&microname;    # standard theme
    # load the classes in @structures
    for my $structure( @structures ) {
        eval "require AI::MicroStructure::$structure; import AI::MicroStructure::$structure;";
        croak $@ if $@;
        *{"$callpkg\::micro$structure"} = sub { $micro->name( $structure, @_ ) };
    }
}
sub new {
    my ( $class, @args ) = ( @_ );
    my $structure;
    $structure = shift @args if @args % 2;
    $structure = $Structure unless $structure; # same default everywhere
    # defer croaking until name() is actually called
    bless { structure => $structure, args => { @args }, micro => {} ,state=>$state}, $class;
}
sub _rearrange{
   my $self = shift;
   $self->{'payload'} = shift if @_;
   return %$self;
}
# CLASS METHODS
sub add_structure {
   my $class  = shift;
   my %structures = @_;
   for my $structure ( keys %structures ) {
   croak "The structure $structure already exists!" if exists $MICRO{$structure};
   my @badnames = grep { !/^[a-z_]\w*$/i } @{$structures{$structure}};
   croak "Invalid names (@badnames) for structure $structure"
   if @badnames;
   my $code = << "EOC";
package AI::MicroStructure::$structure;
use strict;
use AI::MicroStructure::List;
our \@ISA = qw( AI::MicroStructure::List );
our \@List = qw( @{$structures{$structure}} );
__PACKAGE__->init();
1;
EOC
   eval $code;
   $MICRO{$structure} = 1; # loaded
   # export the microstructure() function
   no strict 'refs';
   my $callpkg = caller;
   *{"$callpkg\::micro$structure"} = sub { $micro->name( $structure, @_ ) };
   }
}
# load the content of __DATA__ into a structure
# this class method is used by the other AI::MicroStructure classes
sub load_data {
   my ($class, $structure ) = @_;
   $data = {};
   my $fh;
   { no strict 'refs'; $fh = *{"$structure\::DATA"}{IO}; }
   my $item;
   my @items;
   $$item = "";
   {
   if(defined($fh)){
   local $_;
   while (<$fh>) {
   /^#\s*(\w+.*)$/ && do {
   push @items, $item;
   $item = $data;
   my $last;
   my @keys = split m!\s+|\s*/\s*!, $1;
   $last = $item, $item = $item->{$_} ||= {} for @keys;
   $item = \( $last->{ $keys[-1] } = "" );
   next;
   };
   $$item .= $_;
   }
   }
}
   # clean up the items



( run in 0.608 second using v1.01-cache-2.11-cpan-39bf76dae61 )