Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use locale;
- use utf8;
- use encoding 'utf8';
- ####################### input variables
- @nodesTypes;
- @linkTypes;
- @nodes;
- @links;
- @initialActive;
- @linkWeight;
- $beta;
- $iteration;
- $calibration;
- $a;
- $b;
- $c;
- $t;
- $spacing = 6;
- ####################### read input and configuration
- sub readConfig
- {
- my $source = shift(@_);
- if(open(SOURCE,$source))
- {
- #print "File open!\n";
- while ($row = <SOURCE>)
- {
- #print "Reading line... ";
- chomp($row);
- $begin =$row;
- $begin =~s/\t+.*//;
- $begin =lc($begin);
- #print "beign of line is: $begin \n";
- if($begin eq "nt")
- {
- #print "Node type found!\n";
- $row=~s/^.*\t+//;
- $row=~s/\n//;
- #print "Pushing: $row \n";
- push(@nodesTypes, $row);
- }
- elsif($begin eq "ltra")
- {
- $row =~s/^ltra\t+//;
- $type = $row;
- $type =~s/\t+.*//;
- $reType = $row;
- #print $reType."\n";
- $reType =~s/^.+\t+//;
- #print $reType."\n";
- $reType =~s/\n//;
- $type =~s/\n//;
- #print "Type: $type | Reverse type: $reType \n";
- @par;
- $par[0] = $type;
- $par[1] = $reType;
- push(@linkTypes, [@par]);
- }
- elsif($begin eq "n")
- {
- $row =~s/^n\t+//;
- $id =$row;
- $id =~s/\t+.*//;
- $id =~s/\n//;
- #print "ID: $id | ";
- $imp =$row;
- $imp =~s/^.+\t+//;
- #print "Importance: $imp | ";
- $type =$row;
- $type =~s/\S+$//;
- $type =~s/\t+$//;
- $type =~s/^\S+\t+//;
- #print "Type: $type \n";
- $par[0] = $id;
- $par[1] = $type;
- $par[2] = $imp;
- push(@nodes, [@par]);
- }
- elsif($begin eq "l")
- {
- $row =~s/^l\t+//;
- $begNode =$row;
- $begNode =~s/\t+.*//;
- $begNode =~s/\n//;
- #print "begNode: $begNode | ";
- $type =$row;
- $type =~s/^.+\t+//;
- #print "type: $type | ";
- $termNode =$row;
- $termNode =~s/\S+$//;
- $termNode =~s/\t+$//;
- $termNode =~s/^\S+\t+//;
- #print "termNode: $termNode \n";
- $par[0] = $begNode;
- $par[1] = $termNode;
- $par[2] = $type;
- push(@links, [@par]);
- }
- elsif($begin eq "ia")
- {
- $row =~s/^ia\t+//;
- $type = $row;
- $type =~s/\t+.*//;
- $weight = $row;
- $weight =~s/^.+\t+//;
- $weight =~s/\n//;
- $type =~s/\n//;
- @par;
- $par[0] = $type;
- $par[1] = $weight;
- push(@initialActive, [@par]);
- }
- elsif($begin eq "lw")
- {
- $row =~s/^lw\t+//;
- $type = $row;
- $type =~s/\t+.*//;
- $weight = $row;
- $weight =~s/^.+\t+//;
- $weight =~s/\n//;
- $type =~s/\n//;
- @par;
- $par[0] = $type;
- $par[1] = $weight;
- push(@linkWeight, [@par]);
- }
- elsif($begin eq "beta")
- {
- $row =~s/^.+\t+//;
- $row =~s/\n//;
- $beta =$row;
- }
- elsif($begin eq "iterationsno")
- {
- $row =~s/^.+\t+//;
- $row =~s/\n//;
- $iteration =$row;
- }
- elsif($begin eq "calibration")
- {
- $row =~s/^.+\t+//;
- $row =~s/\n//;
- $calibration =lc($row);
- #print "----------|$calibration|\n";
- }
- elsif($begin eq "a")
- {
- $row =~s/^.+\t+//;
- $row =~s/\n//;
- $a =$row;
- }
- elsif($begin eq "b")
- {
- $row =~s/^.+\t+//;
- $row =~s/\n//;
- $b =$row;
- }
- elsif($begin eq "c")
- {
- $row =~s/^.+\t+//;
- $row =~s/\n//;
- $c =$row;
- }
- elsif($begin eq "t")
- {
- $row =~s/^.+\t+//;
- $row =~s/\n//;
- $t =$row;
- }
- #else {#noaction here}
- #print "B: |$begin|\n";
- }
- }
- else
- {
- print "FATAL ERROR: unable to read configuration\n";
- exit();
- }
- if(!$beta)
- {
- $beta=0;
- print "ERROR: Variable BETA is not set,\nusing default setting. BETA
- = 0\n";
- }
- if(!$iteration)
- {
- $iteration = 10;
- print "ERROR: Variable IterationNo is not set,\nusing default settin
- g. IterationNo = 10\n";
- }
- $calibration = lc($calibration);
- if($calibration ne "none")
- {
- if($calibration ne "conservationofinitialactivation")
- {
- if($calibration ne "conservationoftotalactivation")
- {
- $calibration = "none";
- print "ERROR: Calibration is not set or is set incor
- rectly.\nUsing default setting.\nCALIBRATION = None";
- }
- }
- }
- }
- ######################## write all data
- sub writeData
- {
- $x=@linkTypes;
- for($i=0;$i<$x;$i++)
- {
- print "Type: ";
- print $linkTypes[$i][0];
- print " | ReType: ";
- print $linkTypes[$i][1];
- print "\n";
- }
- for $nodeType (@nodesTypes)
- {
- print"Node type: $nodeType \n";
- }
- $x=@nodes;
- for($i=0;$i<$x;$i++)
- {
- print "ID: ";
- print $nodes[$i][0];
- print " | Type: ";
- print $nodes[$i][1];
- print " | Importance: ";
- print $nodes[$i][2];
- print "\n";
- }
- $x=@links;
- for($i=0;$i<$x;$i++)
- {
- print "Initial node: ";
- print $links[$i][0];
- print " | Terminal node: ";
- print $links[$i][1];
- print " | Link type: ";
- print $links[$i][2];
- print "\n";
- }
- $x=@initialActive;
- for($i=0;$i<$x;$i++)
- {
- print "Node ID: ";
- print $initialActive[$i][0];
- print " | Activation Level: ";
- print $initialActive[$i][1];
- print "\n";
- }
- $x=@linkWeight;
- for($i=0;$i<$x;$i++)
- {
- print "Type: ";
- print $linkWeight[$i][0];
- print " | Weight: ";
- print $linkWeight[$i][1];
- print "\n";
- }
- print "BETA: $beta | Iterations: $iteration | Calibration: $calibration \n";
- print "A: $a | B: $b | C: $c | t: $t\n";
- }
- ######################## algo
- sub graph
- {
- $initial = 1;
- $y=@nodes;
- print "+iterace+";
- for($i=0;$i<$y;$i++)
- {
- $name =$nodes[$i][0];
- $length =length($name);
- $space =$spacing-$length;
- if(($space%2)==1)
- {
- $sBefore=int$space/2;
- $sAfter =int($space/2)+1;
- }
- else
- {
- $sBefore=$space/2;
- $sAfter=$sBefore;
- }
- for($x=0;$x<=$sBefore;$x++)
- {print "-";}
- print $name;
- for($x=0;$x<$sAfter;$x++)
- {print "-";}
- print "+";
- }
- for($counter=0;$counter<=$iteration;$counter++)
- {
- print "\n";
- if($initial==1)
- {
- print "| 0 |";
- $x =@initialActive;
- $x2 =@nodes;
- for($i=0;$i<$x;$i++)
- {
- $nameIA =$initialActive[$i][0];
- $levelIA=$initialActive[$i][1];
- for($y=0;$y<$x2;$y++)
- {
- $nameN=$nodes[$y][0];
- if($nameN eq $nameIA)
- {
- if($levelIA < 10) { printf("%.5f", $levelIA);}
- elsif($levelIA < 100) {printf("%.4f",$levelIA);}
- elsif($levelIA < 1000) {printf("%.3f",$levelIA);}
- elsif($levelIA < 10000) {printf("%.2f",$levelIA);}
- elsif($levelIA < 100000) {printf("%.1f",$levelIA);}
- else{printf("%07d",$levelIA);}
- print "|";
- }
- else
- {
- print " 0 |";
- }
- }
- }
- $initial=0;
- }
- }
- }
- ######################## main program
- readConfig($ARGV[0]);
- #writeData();
- graph();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement