lib/AI/MicroStructure.pm

filter: Statementstmt 20.0% 70 / 350 Branchbran 14.9% 17 / 114 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 47.6% 20 / 42 Podpod n/a totaltotal 20.6% 107 / 519 CC 98 SCAR 41.1
CC
98
Cov
18%
save_default
272.0
openData
110.0
END
86.2
CRAP 60.8
« lib/AI/MicroStructure/Util.pm ↑ Summary script/websock002.pl »
1 #!/usr/bin/perl
2 package AI::MicroStructure;
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Digest::MD5 qw(md5 md5_hex md5_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Digest::SHA1 qw(sha1 sha1_hex sha1_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use File::Glob;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use AI::MicroStructure::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 our $absstructdir = "";
16 our $structdir = "";
17 our @CWD=();
18 our $VERSION = '0.015';
19 our $Structure = 'any'; # default structure
20 our $CODESET = 'utf8';
21 our $LANG = '';
22 our %MICRO;
23 our %MODS;
24 our %ALIEN;
25 our $str = "[A-Z]";
26 our $special = "any";
27 our $search;
28 our $data={};
29 our $item="";
30 our @items;
31 our @a=();
32  
33  
34 our ($init,$new,$drop,$available,$lib,
35 $list,$use,$off,$switch,$mirror,
36 $version,$help,$write) = (0,0,0,0,0,0,0,0,0,0,0,0,0);
37  
38 eval "\$$_=1; " for @ARGV;
39  
40  
41  
42 if( grep{/\bnew\b/} @ARGV ){ $new = 1; cleanArgs("new"); }
43 if( grep{/\bwrite\b/} @ARGV ){ $write = 1; cleanArgs("write"); };
44 if( grep{/\bdrop\b/} @ARGV ){ $drop = 1; cleanArgs("drop"); };
45  
46 our $StructureName = $ARGV[0]; # default structure
47 our $structure = $ARGV[0]; # default structure
48  
49  
50  
51  
52  
53  
54 sub cleanArgs{
55 0 ▶ my ($key) = @_;
Subroutine cleanArgs: not called CC=3 SCAR=24.8
56 0 my @tmp=();
57 0 foreach(@ARGV){
58 0 ▶ push @tmp,$_ unless($_=~/$key/);}
Branch: 1 branch
BranchTrueFalseTotal
unless $_ =~ /$key/ 0 0 0
59  
60 0 @ARGV=@tmp;
61 }
62  
63  
64 # private class method
65 sub find_structures {
66 0 ▶ my ( $class, @dirs ) = @_;
Subroutine find_structures: not called CC=1 SCAR=6.9
67  
68  
69 $ALIEN{"base"} = [map @$_,
70 0 map { [ ( fileparse( $_, qr/\.pm$/ ) )[0] => $_ ] }
71 0 map { File::Glob::bsd_glob(
72  
73  
74 File::Spec->catfile( $_, "*.pm" ) ) } @dirs];
75  
76 0 $ALIEN{"store"}=[];
77  
78  
79  
80 0 return @{$ALIEN{"base"}};
81 }
82  
83 # fetch the list of standard structures
84  
85  
86 sub find_modules {
87  
88  
89 0 ▶ my $structures = {};
Subroutine find_modules: not called CC=3 SCAR=24.8
90 0 foreach(@INC)
91 {
92  
93 my @set = grep /($str)/, map @$_,
94 0 map { [ ( fileparse( $_, qr/\.pm$/ ) )[0] => $_ ] }
95 0 map { File::Glob::bsd_glob(
96 File::Spec->catfile( $_, qw( AI MicroStructure *.pm ) ) ) } $_;
97  
98 0 foreach(@set){
99 0 $structures->{$_}=$_;# unless($_=~/(usr\/local|basis)/);
100 }
101 }
102 0 return %$structures;
103 }
104  
105  
106 our $micro = AI::MicroStructure->new($Structure);
107 $absstructdir = $micro->{state}->{path}->{"cwd/structures"};
108  
109  
110 push @CWD,$absstructdir;
111 #push @CWD,$_ for @INC;
112  
113  
114  
115 $MICRO{$_} = 0 for keys %{{__PACKAGE__->find_structures(@CWD)} };
116 $MODS{$_} = $_ for keys %{{__PACKAGE__->find_modules(@INC)} };
117 $search = join("|",keys %MICRO);
118  
119 #if( grep{/$search/} @ARGV ){ $Structure = $ARGV[0] unless(!$ARGV[0]); }
120  
121 # fetch the list of standard structures
122  
123 #print Dumper keys %MICRO;
124 sub getComponents(){
125  
126 0 ▶ my $x= sprintf("%s",Dumper keys %MICRO);
Subroutine getComponents: not called CC=1 SCAR=6.9
127  
128 0 return $x;
129 }
130  
131  
132 # the functions actually hide an instance
133 # END OF INITIALISATION
134  
135 # support for use AI::MicroStructure 'stars'
136 # that automatically loads the required classes
137 sub import {
138 0 ▶ my $class = shift;
Subroutine import: not called CC=5 SCAR=30.9
139 # 'stars' is still first
140 0 my @structures = ( grep { $_ eq ':all' } @_ )
141 0 ▶ ? ( 'stars', grep { !/^(?:stars|:all)$/ } keys %MICRO )
Branch: 1 branch
BranchTrueFalseTotal
grep({$_ eq ':all';} @_) ? : 0 0 0
142 : @_;
143  
144 0 ▶ $Structure = $structures[0] if @structures;
Branch: 1 branch
BranchTrueFalseTotal
if @structures 0 0 0
145 0 my $micro = AI::MicroStructure->new( $Structure );
146  
147 0 $absstructdir = $micro->{state}->{path}->{"cwd/structures"};
148  
149 # export the microname() function
150 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
151 0 my $callpkg = caller;
152 0 *{"$callpkg\::microname"} = \&microname; # standard structure
153  
154 # load the classes in @structures
155 0 for my $structure( @structures ) {
156 0 eval "require '$absstructdir/$structure.pm';".
157 "import '$absstructdir/$structure.pm';";
158 0 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
159 0 ▶ *{"$callpkg\::micro$structure"} = sub { $micro->name( $structure, @_ ) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
160 }
161 }
162  
163 sub new {
164 6 ▶ my ( $class, @tools ) = ( @_ );
Subroutine new: called CC=3 SCAR=11.1
165 6 my $structure;
166 6 ▶ $structure = shift @tools if @tools % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @tools % 2 1 5 6
167 6 ▶ $structure = $Structure unless $structure; # same default everywhere
Branch: 1 branch
BranchTrueFalseTotal
unless $structure 5 1 6
168 # my $driver = {};
169 # $driver = AI::MicroStructure::Driver->new;
170  
171 # defer croaking until name() is actually called
172 6 my $self = bless { structure => $structure,
173 tools => { @tools }, micro => {}}, $class;
174  
175  
176 6 $self->{state} = AI::MicroStructure::Util::load_config();
177 0 $absstructdir = $self->{state}->{path}->{"cwd/structures"};
178  
179 # print Dumper $self;
180  
181 0 return $self;
182  
183 }
184  
185 sub _rearrange{
186 0 ▶ my $self = shift;
Subroutine _rearrange: not called CC=2 SCAR=17.9
187 0 ▶ $self->{'payload'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
188 0 return %$self;
189 }
190  
191 # CLASS METHODS
192 sub add_structure {
193 0 ▶ my $class = shift;
Subroutine add_structure: not called CC=4 SCAR=26.8
194 0 my %structures = @_;
195  
196 0 for my $structure ( keys %structures ) {
197 0 ▶ croak "The structure $structure already exists!" if exists $MICRO{$structure};
Branch: 1 branch
BranchTrueFalseTotal
if exists $AI::MicroStructure::MICRO{$structure} 0 0 0
198 0 my @badnames = grep { !/^[a-z_]\w*$/i } @{$structures{$structure}};
199 0 ▶ croak "Invalid names (@badnames) for structure $structure"
Branch: 1 branch
BranchTrueFalseTotal
if @badnames 0 0 0
200 if @badnames;
201  
202 0 my $code = << "EOC";
203 package AI::MicroStructure::$structure;
204 use strict;
205 use AI::MicroStructure::List;
206 our \@ISA = qw( AI::MicroStructure::List );
207 0 our \@List = qw( @{$structures{$structure}} );
208 __PACKAGE__->init();
209 1;
210 EOC
211 0 eval $code;
212 0 $MICRO{$structure} = 1; # loaded
213  
214 # export the microstructure() function
215 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
216 0 my $callpkg = caller;
217 0 ▶ *{"$callpkg\::micro$structure"} = sub { $micro->name( $structure, @_ ) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
218 }
219 }
220  
221  
222  
223  
224  
225 # load the content of __DATA__ into a structure
226 # this class method is used by the other AI::MicroStructure classes
227 sub load_data {
228 0 ▶ my ($class, $structure ) = @_;
Subroutine load_data: not called CC=7 SCAR=38.1
229 0 $data = {};
230  
231 0 my $fh;
232 6 ▶ { no strict 'refs'; $fh = *{"$structure\::DATA"}{IO}; }
Subroutine BEGIN: called CC=1 SCAR=1.2
233  
234 0 my $item;
235 my @items;
236 0 $$item = "";
237  
238 {
239 0 ▶ if(defined($fh)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fh) 0 0 0
240 0 local $_;
241 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
242 0 ▶ /^#\s*(\w+.*)$/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/^#\s*(\w+.*)$/ and do { push @items, $item; $item = $AI::MicroStructure::data; my $last; my @keys = split(m[\s+|\s*/\s*], $1, 0); $last = $item, $item = ($item->{$_} ||= {}) foreach (@keys); $item = \($$last{$keys[-1]} = ""); next } 0 0 0
243 0 push @items, $item;
244 0 $item = $data;
245 0 my $last;
246 0 my @keys = split m!\s+|\s*/\s*!, $1;
247 0 ▶ $last = $item, $item = $item->{$_} ||= {} for @keys;
Condition0%0/2
$item->{$_} ||= {}
l!l
00
MC/DC0%0/1
$item->{$_} ||= {}
$item->{$_}
Truth table0%0/2
A
A= $item->{$_}
Aresult
11
00
248 0 $item = \( $last->{ $keys[-1] } = "" );
249 0 next;
250 };
251 0 $$item .= $_;
252 }
253 }
254 }
255 # clean up the items
256 0 for( @items, $item ) {
257 0 $$_ =~ s/\A\s*//;
258 0 $$_ =~ s/\s*\z//;
259 0 $$_ =~ s/\s+/ /g;
260 }
261  
262  
263 0 return $data;
264 }
265  
266  
267 #fitnes
268  
269 sub fitnes {
270  
271 0 ▶ my $self = shift;
Subroutine fitnes: not called CC=1 SCAR=6.9
272 0 return sha1_hex($self->structures());
273 ##my ($config,$structure, $config ) = (shift,[$self->structures()]); FIXME
274  
275 }
276  
277 # main function
278 0 ▶sub microname { $micro->name( @_ ) };
Subroutine microname: not called CC=1 SCAR=6.9
279  
280 # corresponding method
281 sub name {
282 0 ▶ my $self = shift;
Subroutine name: not called CC=6 SCAR=34.4
283 0 my ( $structure, $count ) = ("any",1);
284  
285 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
286 0 ( $structure, $count ) = @_;
287 0 ▶ ( $structure, $count ) = ( $self->{structure}, $structure )
Branch: 1 branch
BranchTrueFalseTotal
if defined $structure and $structure =~ /^(?:0|[1-9]\d*)$/ 0 0 0
Condition0%0/3
defined $structure and $structure =~ /^(?:0|[1-9]\d*)$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $structure and $structure =~ /^(?:0|[1-9]\d*)$/
defined $structure $structure =~ /^(?:0|[1-9]\d*)$/
Truth table0%0/3
A and B
A= defined $structureB= $structure =~ /^(?:0|[1-9]\d*)$/
ABresult
0X0
100
111
288 if defined($structure) && $structure =~ /^(?:0|[1-9]\d*)$/;
289 }
290 else {
291 0 ( $structure, $count ) = ( $self->{structure}, 1 );
292 }
293  
294 0 ▶ if( ! exists $self->{micro}{$structure} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"micro"}{$structure}) 0 0 0
295 0 ▶ if( ! $MICRO{$structure} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($AI::MicroStructure::MICRO{$structure}) 0 0 0
296 0 eval "require '$absstructdir/$structure.pm';";
297 #croak "MicroStructure list $structure does not exist!" if $@;
298  
299 6 ▶ use AI::MicroStructure::any;
Subroutine BEGIN: called CC=1 SCAR=0.0
300 0 $MICRO{$structure} = 1; # loaded
301 }
302 $self->{micro}{$structure} =
303 0 "AI::MicroStructure::$structure"->new( %{ $self->{tools} } );
304 }
305  
306 0 $self->{micro}{$structure}->name( $count );
307 }
308  
309  
310  
311 # other methods
312 0 ▶sub structures { wantarray ? ( sort keys %MICRO ) : scalar keys %MICRO }
Subroutine structures: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
313 0 ▶sub has_structure { $_[1] ? exists $MICRO{$_[1]} : 0 }
Subroutine has_structure: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 0 0 0
314 0 ▶sub configure_driver { $_[1] ? exists $MICRO{$_[1]} : 0 }
Subroutine configure_driver: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 0 0 0
315 sub count {
316 0 ▶ my $self = shift;
Subroutine count: not called CC=4 SCAR=30.0
317 0 my ( $structure, $count );
318  
319 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
320 0 ( $structure, $count ) = @_;
321 0 ▶ ( $structure, $count ) = ( $self->{structure}, $structure )
Branch: 1 branch
BranchTrueFalseTotal
if $structure =~ /^(?:0|[1-9]\d*)$/ 0 0 0
322 if $structure =~ /^(?:0|[1-9]\d*)$/;
323 }
324  
325  
326 0 ▶ if( ! exists $self->{micro}{$structure} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"micro"}{$structure}) 0 0 0
327 0 return scalar ($self->{micro}{$structure}->new);
328 }
329  
330 0 return 0;
331 }
332  
333  
334 sub trim
335 {
336 0 ▶ my $self = shift;
Subroutine trim: not called CC=2 SCAR=17.9
337 0 my $string = shift;
338 0 ▶ $string = "" unless $string;
Branch: 1 branch
BranchTrueFalseTotal
unless $string 0 0 0
339 0 $string =~ s/^\s+//;
340 0 $string =~ s/\s+$//;
341 0 $string =~ s/\t//;
342 0 $string =~ s/^\s//;
343 0 return $string;
344 }
345  
346  
347 sub getBundle {
348  
349 0 ▶ my $self = shift;
Subroutine getBundle: not called CC=8 SCAR=40.3
350  
351 0 $absstructdir = $self->{state}->{path}->{"cwd/structures"};
352  
353  
354 0 my @structures = grep { !/^(?:any)/ } AI::MicroStructure->structures;
355 0 my @micros;
356 0 my @search=[];
357  
358  
359  
360  
361  
362 0 for my $structure (@structures) {
363 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
364 0 eval "require '$absstructdir/$structure.pm';";
365  
366 0 my %isa = map { $_ => 1 } @{"AI::MicroStructure::$structure\::ISA"};
367 0 ▶ if( exists $isa{'AI::MicroStructure::Locale'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $isa{'AI::MicroStructure::Locale'}) { } 0 0 0
elsif (exists $isa{'AI::MicroStructure::MultiList'}) { } 0 0 0
368 0 for my $lang ( "AI::MicroStructure::$structure"->languages() ) {
369 0 push @micros,
370 ["AI::MicroStructure::$structure"->new( lang => $lang ),$lang];
371  
372  
373 }
374 }
375 elsif( exists $isa{'AI::MicroStructure::MultiList'} ) {
376 0 for my $cat ( "AI::MicroStructure::$structure"->categories(), ':all' ) {
377 0 push @micros,
378 [ "AI::MicroStructure::$structure"->new( category => $cat ),$cat];
379 }
380 }
381 else {
382 0 push @micros, ["AI::MicroStructure::$structure"->new(),''];
383 }
384 }
385  
386 0 my $all ={};
387  
388 0 for my $test (@micros) {
389 0 my $micro = $test->[0];
390 0 my %items;
391 0 my $items = $micro->name(0);
392 0 $items{$_}++ for $micro->name(0);
393 0 my $key=sprintf("%s",$micro->structure);
394 0 $all->{$key}=[$test->[1],$micro->name($items)];
395  
396 }
397  
398  
399 0 return $all;
400  
401 }
402  
403  
404 sub save_cat {
405  
406 0 ▶ my $self = shift;
Subroutine save_cat: not called CC=5 SCAR=34.0
407 0 my $data = shift;
408 0 my $dat;
409 0 my $ret = "";
410  
411  
412 0 foreach my $key(sort keys %{$data} ) {
413 0 ▶ next unless($_);
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 0 0 0
414 #ref $hash->{$_} eq "HASH"
415 0 ▶ if(ref $data->{$key} eq "HASH"){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data->{$key} eq 'HASH') { } 0 0 0
416 0 $ret .= "\n".$self->save_cat($data->{$key});
417 }else{
418 0 $dat = $data->{$key};
419 0 $dat =~ s/^|,/\n/g;
420 0 $dat =~ s/\n\n/\n/g;
421 0 $dat =~ s/->\n|[0-9]\n//g;
422  
423 0 ▶ $ret .= "# ".($key=~/names|default|[a-z]/?$key:"names ".$key);
Branch: 1 branch
BranchTrueFalseTotal
$key =~ /names|default|[a-z]/ ? : 0 0 0
424 0 $ret .= "\n ".$dat."\n";
425 }
426  
427 }
428  
429 0 return $ret;
430  
431 }
432  
433 sub save_default {
434  
435 0 ▶ my $self = shift;
Subroutine save_default: not called CC=16 SCAR=56.1
436 0 my $data = shift;
437 0 my $line = shift;
438 0 my $dat = {};
439 0 my @in = ();
440 0 my $active=0;
441 0 ▶ $line = $Structure unless($line);
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 0 0
442  
443 0 foreach(@{$data->{rows}->{"coordinate"}}){
444  
445 0 ▶ if($_ eq $line){ $active=1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq $line) 0 0 0
446  
447 0 ▶ if(1+$line eq $_){ $active=0; }
Branch: 1 branch
BranchTrueFalseTotal
if (1 + $line eq $_) 0 0 0
448  
449 0 ▶ if($active==1){
Branch: 1 branch
BranchTrueFalseTotal
if ($active == 1) 0 0 0
450 0 $_=~s/,//g;
451 0 $_ = $self->trim($_);
452 0 ▶ $dat->{names}->{$_}=$_ unless(defined($dat->{names}->{$_}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dat->{"names"}{$_} 0 0 0
453 }
454  
455 }
456  
457 0 foreach(@{$data->{rows}->{"search"}}){
458  
459 0 ▶ if($_ eq $line){ $active=1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq $line) 0 0 0
460  
461  
462 0 ▶ if(1+$line eq $_){ $active=0; }
Branch: 1 branch
BranchTrueFalseTotal
if (1 + $line eq $_) 0 0 0
463  
464 0 ▶ if($active==1){
Branch: 1 branch
BranchTrueFalseTotal
if ($active == 1) 0 0 0
465 0 $_=~s/,//g;
466 0 $_ = $self->trim($_);
467 0 ▶ $dat->{names}->{$_}=$_ unless(defined($dat->{names}->{$_}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dat->{"names"}{$_} 0 0 0
468  
469  
470 }
471  
472 }
473  
474 0 push @in , keys %{$dat->{names}};
475 0 push @in , values %{$data->{names}};
476 0 $dat->{names} = join(" ",@in);
477 0 $dat->{names} =~ s/$line(.*?)\-\>(.*?) [1-9] /$1 $2/g;
478 0 $dat->{names} =~ s/ / /g;
479 0 my @file = grep{/$Structure/}map{File::Glob::bsd_glob(
480 File::Spec->catfile( $_, ($structdir,"*.pm") ) )}@CWD;
481  
482  
483 0 ▶ if(@file){
Branch: 1 branch
BranchTrueFalseTotal
if (@file) 0 0 0
484 0 ▶ open(SELF,"+<$file[0]") || die $!;
Branch: 1 branch
BranchTrueFalseTotal
open SELF, "+<$file[0]" or die $! 0 0 0
485  
486 0 ▶ while(<SELF>){last if /^__DATA__/}
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline SELF)) 0 0 0
487  
488 0 truncate(SELF,tell SELF);
489  
490 0 print SELF $self->save_cat($dat);
491  
492 0 truncate(SELF,tell SELF);
493 0 close SELF;
494 }
495  
496 }
497  
498 sub openData{
499  
500 0 ▶my $self = shift;
Subroutine openData: not called CC=10 SCAR=47.0
501  
502 0 my @datax = ();
503  
504 0 if(<DATA>){
505  
506 0 @datax = <DATA>;
507  
508 0 while(@datax){
509 0 chomp;
510 0 if($_=~/^#\s*(\w+.*)$/) {
511 0 @a=split(" ",$1);
512 0 if($#a){
513 0 $data->{$a[0]}->{$a[1]}="";
514 }else{
515 0 $data->{$1}="";
516 }
517 0 $item=$1 unless($#a);
518  
519 }else{
520  
521 0 my @keys = split m!\s+|\s*/\s*!,$_;
522 0 foreach(sort @keys){
523 0 if($#a){
524 0 $data->{$a[0]}->{$a[1]} .= " $_" unless($_ eq "");
525 }else{
526 0 $data->{$item} .= " $_" unless($_ eq "");
527 }
528 }
529  
530 };
531  
532 }
533 }
534 0 return $data;
535  
536  
537 }
538  
539 sub getBlank {
540  
541 0 my $self = shift;
542 0 my $structure = shift;
543 0 my $data = shift;
544  
545  
546  
547 0 my $usage = "";
548  
549 0 $usage = "#!/usr/bin/perl -W\n";
550 0 $usage .= << "EOC";
551 package AI::MicroStructure::$structure;
552 use strict;
553 use AI::MicroStructure::List;
554 our \@ISA = qw( AI::MicroStructure::List );
555 our \@List = qw( \@{\$structures{\$structure}} );
556 __PACKAGE__->init();
557 1;
558 EOC
559  
560  
561  
562 0 my $new = {};
563 0 foreach my $k
564 0 (grep{!/^[0-9]/}map{$_=$self->trim($_)}@{$data->{rows}->{"search"}}){
565  
566 0 $k =~ s/[ ]/_/g;
567 0 $k =~ s/[\(]|[\)]//g;
568 0 next if($k=~/synonyms|hypernyms/);
569 0 print $k;
570 0 $new->{$k}=[map{$_=[map{$_=$self->trim($_)}split("\n|, ",$_)]}
571 0 grep{!/synonyms|hypernyms/}split("sense~~~~~~~~~",
572 lc `micro-wnet $k`)];
573 0 next unless(@{$new->{$k}});
574 # $new->{$k}=~s/Sense*\n(.*?)\n\n/$1/g;
575 # @{$new->{$k}} = [split("\n|,",$new->{$k})];
576 0 $data->{rows}->{"ident"}->{md5_base64($new->{$k})} = $new->{$k};
577  
578 }
579  
580  
581 0 my $list = join("\n",sort keys %$new);
582  
583  
584 # $list =~ s/_//g;
585  
586 0 $usage .= "
587 __DATA__
588 # names
589 ".$list;
590  
591  
592  
593  
594 }
595  
596 sub save_new {
597  
598 0 my $self = shift;
599 0 my $StructureName = shift;
600 0 my $data = shift;
601  
602 0 $absstructdir = $self->{state}->{path}->{"cwd/structures"};
603 0 $StructureName = lc $self->trim(`micro`) unless($StructureName);
604 my $file = "$absstructdir/$StructureName.pm";
605 print `mkdir $absstructdir` unless(-d $absstructdir);
606 my $fh;
607  
608 open($fh,">$file") || die $!;
609  
610 print $fh $self->getBlank($StructureName,$data);
611  
612 close $fh;
613 $Structure = $StructureName;
614 push @CWD,$file;
615 return 1;
616 }
617  
618  
619  
620 sub drop {
621  
622 my $self = shift;
623 my $StructureName = shift;
624  
625 my @fh = grep{/$StructureName.pm/}__PACKAGE__->find_structures(@CWD);
626  
627  
628 ▶ if(defined($fh[0]) && `ls $fh[0]`)
Condition0%0/3
defined $fh[0] and `ls $fh[0]`
!ll&&!rl&&r
000
MC/DC0%0/2
defined $fh[0] and `ls $fh[0]`
defined $fh[0] `ls $fh[0]`
Truth table0%0/3
A and B
A= defined $fh[0]B= `ls $fh[0]`
ABresult
0X0
100
111
629 {
630 print `rm $fh[0]`;
631  
632 }else{
633  
634 croak "The structure $StructureName does not exist!";
635  
636 }
637  
638  
639 return 1;
640 }
641  
642 sub help {
643  
644 my $self = shift;
645 my $usage = << 'EOT';
646  
647 #current status
648 #did you create a micro structure yet ?
649 #try something like this
650  
651 $ micro new ufo; # creates a structure called ufo
652  
653 $ micro drop ufo; # deletes the structure called ufo
654  
655 $ micro structures; # shows all structure's you currently have
656  
657 #after creation of a structure you can access it in lots of ways
658  
659  
660  
661 $ micro; # one word of a random structure
662  
663 $ micro ufo; # one word of the ufo structure
664  
665 $ micro ufo all; # all words of the ufo structure
666  
667 $ micro ufo 5; # 5 random words of the ufo structure
668  
669 $ micro any 10; # 10 random words of any structure you have created
670  
671  
672 $ micro --init # initializes active memory
673  
674 $ micro --export # export relations from couchdb into git repo and tag data
675  
676  
677 # oneliners i like to use
678  
679 $ for i in `micro structures`; do echo $i; done; # echos all the structures
680  
681 $ for i in `micro ufo all`; do echo $i; done; # echos all words in ufo
682  
683 $ for i in `micro structures`; do micro all $i; done; # echos all stuctures all words
684  
685 $ for i in `micro ufo all`; do micro new $i; done; # new structure for all words in ufo
686  
687 $ for i in `micro ufo all`; do micro-wiki $i; done; # push all words against the wiki plugin dont forget setting user & password in /usr/local/bin/micro-wiki
688  
689 ###################################################################################
690 # try to follow the logic combine
691 # your-word=micro new ? ->concept->concepts->relations->node
692  
693  
694 $ micro new biology
695 $ micro new biological_process
696  
697 $ for i in `micro structures`; do
698 $ for y in `micro all $i `; do
699 $ echo "$i=$y";
700 $ micro new $y;
701 $ done
702 $ done
703  
704 #!!!!!###Hard cpu to expect ### make sure couch is on ###### or disable the store methode in micro-wiki and print $doc or consume otherweise
705 # test as single before you loope
706  
707 $ micro-wiki ufo
708  
709 # proceed
710  
711 $ for i in `micro structures`; do
712 $ for y in `micro all $i `; do
713 $ echo "$i=$y";
714 $ micro-wiki $y;
715 $ done
716 $ done
717  
718  
719 EOT
720  
721  
722  
723 return $usage;
724  
725 }
726  
727  
728 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
729  
730  
731 }
732  
733 END{
734  
735 1 ▶if($init){}
Subroutine END: called CC=18 SCAR=44.6
Branch: 1 branch
BranchTrueFalseTotal
if ($AI::MicroStructure::init) 0 1 1
736 1 ▶if($available){}
Branch: 1 branch
BranchTrueFalseTotal
if ($AI::MicroStructure::available) 0 1 1
737 1 ▶if($lib){}
Branch: 1 branch
BranchTrueFalseTotal
if ($AI::MicroStructure::lib) 0 1 1
738 1 ▶if($list){}
Branch: 1 branch
BranchTrueFalseTotal
if ($AI::MicroStructure::list) 0 1 1
739 1 ▶if($use){}
Branch: 1 branch
BranchTrueFalseTotal
if ($AI::MicroStructure::use) 0 1 1
740 1 ▶if($off){}
Branch: 1 branch
BranchTrueFalseTotal
if ($AI::MicroStructure::off) 0 1 1
741 1 ▶if($switch){}
Branch: 1 branch
BranchTrueFalseTotal
if ($AI::MicroStructure::switch) 0 1 1
742 1 ▶if($mirror){}
Branch: 1 branch
BranchTrueFalseTotal
if ($AI::MicroStructure::mirror) 0 1 1
743 1 ▶if($version){
Branch: 1 branch
BranchTrueFalseTotal
if ($AI::MicroStructure::version) 0 1 1
744 0 printf($VERSION);
745 0 exit(0);
746 }
747  
748  
749  
750 1 ▶if($help) {
Branch: 1 branch
BranchTrueFalseTotal
if ($AI::MicroStructure::help) 0 1 1
751 0 printf($micro->help());
752 0 exit(0);
753  
754 }
755  
756  
757  
758  
759  
760 1 ▶if($drop == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($AI::MicroStructure::drop == 1) 0 1 1
761 0 $micro->drop($StructureName);
762 0 exit 0;
763 }
764  
765 1 ▶if($new==1){
Branch: 1 branch
BranchTrueFalseTotal
if ($AI::MicroStructure::new == 1) 0 1 1
766  
767 1 ▶ use Term::ReadKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
768 1 ▶ use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
769  
770 0 my $data = decode_json(lc`micro-sense $StructureName words`);
771  
772  
773 0 my $char;
774 my $line;
775 0 my $senses=@{$data->{"senses"}};
776 0 ▶ $senses= 0 unless($senses);
Branch: 1 branch
BranchTrueFalseTotal
unless $senses 0 0 0
777  
778 0 printf("\n
779 \033[0;34m
780 %s
781 Type: the number you choose 1..$senses
782 \033[0m",$micro->usage($StructureName,$senses,$data));
783  
784 0 ▶ $line = 1 unless($senses != 1);
Branch: 1 branch
BranchTrueFalseTotal
unless $senses != 1 0 0 0
785 0 ▶ chomp($line = <STDIN>) unless($line);
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 0 0
786  
787 0 my $d = join("#",@{$data->{rows}->{search}});
788  
789 0 my @d = grep{/^$line#/}split("sense~~~~~~~~~",$d);
790 0 @{$data->{rows}->{"search"}}=split("#",join("",@d));
791  
792 0 ▶ if($line>0){
Branch: 1 branch
BranchTrueFalseTotal
if ($line > 0) { } 0 0 0
793 0 $micro->save_new($StructureName,$data,$line);
794 0 exit 0;
795 }else{
796  
797 0 printf "your logic is today impaired !!!\n";
798 0 exit 0;
799  
800 }
801  
802  
803  
804 }
805  
806 1 ▶ if($write == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($AI::MicroStructure::write == 1) 0 1 1
807 0 $micro->save_default();
808 }
809 }
810  
811  
812  
813  
814  
815 sub usage {
816  
817 my $self = shift;
818  
819  
820 my $search = shift;
821 my $senseNr = shift;
822 my $data = shift;
823  
824  
825 my $usage = << 'EOT';
826  
827 .--'"""""--.>_
828 .-' o\\b.\o._o.`-.
829 .-'.- ) \d888888888888b.
830 /.' b Y8888888888888888b.
831 .-'. 8888888888888888888888888b
832 / o888 Y Y8888888888888888888888b
833 / d888P/ /| Y"Y8888888888888888888b
834 J d8888/| Y .o._. "Y8888888888888Y" \
835 |d Y888b|obd88888bo. """Y88888Y' .od8
836 Fdd 8888888888888888888bo._'|| d88888|
837 Fd d 88\ Y8888Y "Y888888888b, d888888P
838 d-b 8888b Y88P' """""Y888b8888P"|
839 J 8\88888888P `m. """"" |
840 || `8888888P' "Ymm._ _J
841 |\\ Y8888P ' .mmm.YM) .mMF"'
842 | \\ Y888J ' < (@)>.- ` /MFm. |
843 J \ `YY ""' :: MM @)>F
844 L /) 88 : | ""\|
845 | ( ( Yb . ' . | L
846 \ bo 8b . . J | <0>_
847 \ "' . . . . L F <1>_
848 o._.:. . . \mm,__J/ / <2>_
849 Y8::'|. / `Y8P J <3>_
850 `|' J: . . ' . . | F <4>_
851 | L ' . _: | <5>_
852 | `: . .:oood8bdb. | 1>_
853 F `:. "-._ `" F 2>_
854 / `::. """' / 3>_
855 / `::. "" / 4>_
856 _.-d( `:::. F 5>_
857 -888b. `::::. . J 6>_
858 Y888888b. `::::::::::' 7>_
859 Y88888888bo. `::::::d 8>_
860 `"Y8888888888boo.._ `"dd88b. 9>_
861  
862  
863  
864  
865  
866 """""""""""""""""""""""""""""""""""""""""""""""
867  
868 EOT
869  
870  
871 $usage =~ s/<0>_/\033[0;32mThe word $search\033[255;34m/g;
872 $usage =~ s/<1>_/\033[0;32mhas $senseNr concept's\033[255;34m/g;
873 $usage =~ s/<2>_/\033[0;32mwe need to find out the which one\033[255;34m/g;
874 $usage =~ s/<3>_/\033[0;32mto use for our new,\033[255;34m/g;
875 $usage =~ s/<4>_/\033[0;32mmicro-structure,\033[255;34m/g;
876 $usage =~ s/<5>_//g;
877 my @row = ();
878 my $ii=0;
879 foreach my $sensnrx (sort keys %{$data->{"rows"}->{"senses"}})
880 {
881  
882  
883  
884 my $row = $data->{"rows"}->{"senses"}->{$sensnrx};
885 my $txt="";
886  
887 foreach(@{$row->{"basics"}}[1..2]){
888 next unless($_);
889 $txt .= sprintf("\033[0;31m %s\033[255;34m",$_);
890 }
891  
892 $txt .= sprintf("",$_);
893 $usage =~ s/$sensnrx>_/($sensnrx):$txt/g;
894 if($sensnrx>9){
895 $usage .= sprintf("\n(%d):%s",$sensnrx,$txt);
896  
897 }
898 }
899  
900 foreach my $ii (0..16){
901 $usage =~ s/$ii>_//g;
902 }
903  
904  
905 return $usage;
906 }
907  
908  
909 1;
910  
911 __END__
912  
913  
914 #print Dumper $micro;
915  
916 # ABSTRACT: AI::MicroStructure Creates Concepts for words
917  
918 =head1 NAME
919  
920 AI::MicroStructure
921  
922 =head1 DESCRIPTION
923  
924 Creates Concepts for words
925  
926 =head1 SYNOPSIS
927  
928 ~$ micro new world
929  
930 ~$ micro structures
931  
932 ~$ micro any 2
933  
934 ~$ micro drop world
935  
936 ~$ micro
937  
938 =head1 AUTHOR
939  
940 Hagen Geissler <santex@cpan.org>
941  
942 =head1 COPYRIGHT AND LICENCE
943  
944 Hagen Geissler <santex@cpan.org>
945  
946 =head1 SUPPORT AND DOCUMENTATION
947  
948 [sample using concepts](http://quantup.com)
949  
950 [PDF info on my works](https://github.com/santex)
951  
952  
953 =head1 SEE ALSO
954  
955 AI-MicroStructure
956 =cut
« lib/AI/MicroStructure/Util.pm ↑ Summary script/websock002.pl »