lib/AI/MicroStructure/Reactor.pm untested

filter: Statementstmt 0.0% 0 / 183 Branchbran 0.0% 0 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 33 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 260 CC 14 SCAR 18.9
CC
14
Cov
0%
final
20.0
insert
20.0
domain
12.0
CRAP 6.7
1 #!/usr/bin/perl -W
2  
3 package AI::MicroStructure::Reactor;
4 1;
5 package AI::MicroStructure::Object;
6  
7  
8 use Cache::Memcached::Fast;
9  
10 our $memd = new Cache::Memcached::Fast({
11 servers => [ { address => 'localhost:11211', weight => 2.5 }],
12 namespace => 'my:',
13 connect_timeout => 0.2,
14 io_timeout => 0.1,
15 close_on_error => 1,
16 compress_threshold => 100_000,
17 compress_ratio => 0.9,
18 max_failures => 1,
19 max_size => 512 * 1024,
20 });
21  
22  
23 use strict;
24 use Digest::MD5 qw(md5_hex);
25 use Data::Printer;
26 use Data::Dumper;
27  
28 sub new {
29 my $pkg = shift;
30 my $self = bless {}, $pkg;
31  
32  
33 my $i = 0;
34  
35 $self->{md5} = md5_hex(@_);
36 $self->{name} = "@_";
37  
38 $self->{micro} = [split("\n",$self->check("@_","micrownet"))] ;
39 $self->{second}->{$_}=[split("\n",$self->check("$_","micrownet") )] for @{$self->{micro}};
40 $self->{weight} = {};
41  
42 foreach(@{$self->{micro}}){
43  
44  
45 foreach(@{$self->{second}->{$_}}){
46  
47 if($self->{weight}->{$_})
48 {
49 $self->{weight}->{$_}++;
50 }else{
51 $self->{weight}->{$_} = 1;
52 }
53 }
54 }
55  
56 # foreach(keys %{$self->{weight}}){
57 # delete $self->{weight}->{$_} unless($self->{weight}->{$_}>1);
58 # }
59  
60  
61  
62 return $self;
63 }
64  
65 sub decruft {
66 my $self = shift;
67 my($file) = @_;
68 my($cruftSet) = q{%§&|#[^+*( ]),'"; };
69 my $clean = $file;
70 $clean=~s/\Q$_//g for split("",$cruftSet);
71  
72 return $clean;
73 }
74  
75 sub TO_JSON {
76 my $self = shift;
77 return { class => 'Object', data => { %$self } };
78 }
79  
80  
81 sub tools {
82 my $self = shift;
83 $self->{tools} = \@_;
84  
85  
86 }
87  
88 sub check { my $self = shift;
89 my $name = shift;
90 my $prog = shift;
91 $name = $self->decruft($name);
92  
93 if(defined(my $ret = $memd->get(sprintf("%s_%s",$name,$prog)))){
94 return $ret;
95 }else{
96 my $ret = `$prog $name`;
97 $memd->set(sprintf("%s_%s",$name,$prog),$ret);
98 return $ret;
99 }
100 }
101  
102  
103  
104 sub name {
105  
106 my $self = shift;
107 return $self->{name};
108 }
109 1;
110  
111 package AI::MicroStructure::ObjectSet;
112  
113 use strict;
114 use Carp;
115 use Digest::MD5 qw(md5_hex);
116 use Statistics::Basic qw(:all);
117 use List::Util qw(max min);
118 use Data::Printer;
119  
120  
121 sub new {
122 my $pkg = shift;
123 my $self = bless {obj=>{IN=>[@_]}}, $pkg;
124 my $syntetic = {};
125  
126 $self->insert(@_) if @_;
127  
128  
129  
130 return $self;
131 }
132 sub decruft {
133 my $self = shift;
134 my($file) = @_;
135 my($cruftSet) = q{%§&|#[^+*( ]),'"; };
136 my $clean = $file;
137 $clean=~s/\Q$_//g for split("",$cruftSet);
138  
139 return $clean;
140 }
141  
142 sub check {
143 my $self = shift;
144 my $name = shift;
145 my $prog = shift;
146 $name = $self->decruft($name);
147  
148 if(defined(my $ret = $memd->get(sprintf("%s_%s",$name,$prog)))){
149 return $ret;
150 }else{
151 my $ret = `$prog $name`;
152 $memd->set(sprintf("%s_%s",$name,$prog),$ret);
153 return $ret;
154 }
155 }
156  
157 sub final {
158  
159 my $self = shift;
160  
161  
162  
163  
164 foreach(@{$self->{micro}}){
165  
166  
167 foreach(@{$self->{second}->{$_}}){
168  
169 if($self->{weight}->{$_})
170 {
171 $self->{weight}->{$_}++;
172 }else{
173 $self->{weight}->{$_} = 1;
174 }
175 }
176 }
177  
178 $self->{obj}->{mean} = sprintf "%d.3", mean values %{$self->{obj}->{center}};
179  
180  
181 foreach ( keys %{$self->{obj}->{center}}) {
182  
183 next unless $self->{obj}->{center}->{$_}>=$self->{obj}->{mean};
184  
185  
186 $self->{obj}->{dense}->{$_} = $self->{obj}->{center}->{$_};
187  
188 }
189  
190 $self->{obj}->{max} = sprintf max values %{$self->{obj}->{dense}};
191 $self->{obj}->{min} = sprintf min values %{$self->{obj}->{dense}};
192  
193  
194  
195 foreach ( keys %{$self->{obj}->{dense}}) {
196  
197 next if $self->{obj}->{dense}->{$_}!=$self->{obj}->{max}-1;
198  
199  
200 $self->{obj}->{menu}->{logic}->{$_} = $self->{obj}->{dense}->{$_};
201  
202 }
203 my @www = split("\n",`micro-relation $self->{obj}->{name} | data-freq --limit 100`);
204 $self->{obj}->{www} = [@www];
205 foreach(0..$#www/10)
206 {
207 my @s = map{my $x = $_; $x =~ s/ //g; $_=$x;}split(":",$www[$_]);
208 $self->{obj}->{menu}->{data}->{$s[1]} = $s[0];
209 }
210  
211 #5 my $check = "";
212  
213 #foreach ( keys %{$self->{obj}->{dense}}) {
214 # my $arg = sprintf("micro-relation %s",$_);
215 # my $check .= sprintf("\n%s",`$arg`);
216 #}
217  
218 return $self;
219 }
220  
221 sub members {
222 return values %{$_[0]};
223 }
224  
225 sub size {
226 return scalar keys %{$_[0]};
227 }
228  
229 sub insert {
230 my $self = shift;
231 foreach my $element (@_) {
232 if(ref $element eq "AI::MicroStructure::Object")
233 {
234  
235 $self->{ $element->name } = $element;
236 if($element->{files}){
237 foreach(values $element->{files}){
238  
239  
240 $self->{files}->{ $element->name } = $_; }
241 }}
242 $self->{obj}->{center}->{$_} = defined($self->{obj}->{center}->{$_}) ?
243 $self->{obj}->{center}->{$_} + 1 : 1
244 for keys %{$element->{weight}};
245 }
246  
247  
248  
249 # `clear`;
250 # p $self->{obj}->{center};
251  
252 }
253  
254 sub domain {
255 my $self = shift;
256 croak("neds args") unless($ARGV[0]);
257 $self->{obj}->{domain}=sprintf("%s_%s",$ARGV[0] ,md5_hex(@ARGV));
258 $self->{obj}->{name} = $ARGV[0];
259 foreach(@ARGV){
260 my $ob = AI::MicroStructure::Object->new();
261 my @t = split("\n",$ob->check($_,"micrownet")) unless(!@ARGV);
262  
263 foreach my $e(@t){
264 $ob = AI::MicroStructure::Object->new($e);
265 $self->insert($ob);
266 }
267 }
268  
269 }
270 sub retrieve { $_[0]->{$_[1]} }
271 sub includes { exists $_[0]->{ $_[1]->name } }
272 sub includes_name { exists $_[0]->{ $_[1] } }
273  
274 1;
275 package main;
276 BEGIN{
277 if(!@ARGV){
278 my $k= `micro`;
279 $k =~s/\n//g;
280 push @ARGV,$k;
281 }}
282  
283 use Data::Printer;
284 use Data::Dumper;
285 use JSON;
286 use AI::MicroStructure;
287 our @t = ();
288 our $micro = AI::MicroStructure->new();
289 my $set = AI::MicroStructure::ObjectSet->new(@t);
290 $set->domain(@ARGV);
291  
292 $set = $set->final;
293  
294 p $set;
295 #p $set;#@{[@v[$#v-10..$#v], %{$set->{obj}}]};
296 #$set->{obj},
297 #print Dumper $set;
298  
299 #my @v = sort values %{$set->{obj}->{dense}};
300  
301 #print STDOUT encode_json($set->{obj});
302  
303  
304 __DATA__