Advertisement
machalda

begin of perl Homework #2

Jun 10th, 2012
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 12.24 KB | None | 0 0
  1.  
  2. #!/usr/bin/perl
  3. use locale;
  4. use utf8;
  5. use encoding 'utf8';
  6.  
  7. ####################### input variables
  8. @nodesTypes;
  9. @linkTypes;
  10. @nodes;
  11. @links;
  12. @initialActive;
  13. @linkWeight;
  14. $beta;
  15. $iteration;
  16. $calibration;
  17. $a;
  18. $b;
  19. $c;
  20. $t;
  21. $spacing = 6;
  22. ####################### read input and configuration
  23. sub readConfig
  24. {
  25.         my $source = shift(@_);
  26.         if(open(SOURCE,$source))
  27.         {
  28.                 #print "File open!\n";
  29.                 while ($row = <SOURCE>)
  30.                 {
  31.                         #print "Reading line... ";
  32.                         chomp($row);
  33.                         $begin  =$row;
  34.                         $begin  =~s/\t+.*//;
  35.                         $begin  =lc($begin);
  36.                         #print "beign of line is: $begin \n";
  37.                         if($begin eq "nt")
  38.                         {
  39.                                 #print "Node type found!\n";
  40.                                 $row=~s/^.*\t+//;
  41.                                 $row=~s/\n//;
  42.                                 #print "Pushing: $row \n";
  43.                                 push(@nodesTypes, $row);
  44.                         }
  45.                         elsif($begin eq "ltra")
  46.                         {
  47.                                 $row    =~s/^ltra\t+//;
  48.                                 $type   = $row;
  49.                                 $type   =~s/\t+.*//;
  50.                                 $reType = $row;
  51.                                 #print $reType."\n";
  52.                                 $reType =~s/^.+\t+//;
  53.                                 #print $reType."\n";
  54.                                 $reType =~s/\n//;
  55.                                 $type   =~s/\n//;
  56.                                 #print "Type: $type | Reverse type: $reType \n";
  57.                                 @par;
  58.                                 $par[0] = $type;
  59.                                 $par[1] = $reType;
  60.                                 push(@linkTypes, [@par]);
  61.                         }
  62.                         elsif($begin eq "n")
  63.                         {
  64.                                 $row    =~s/^n\t+//;
  65.                                 $id     =$row;
  66.                                 $id     =~s/\t+.*//;
  67.                                 $id     =~s/\n//;
  68.                                 #print "ID: $id | ";
  69.                                 $imp    =$row;
  70.                                 $imp    =~s/^.+\t+//;
  71.                                 #print "Importance: $imp | ";
  72.                                 $type   =$row;
  73.                                 $type   =~s/\S+$//;
  74.                                 $type   =~s/\t+$//;
  75.                                 $type   =~s/^\S+\t+//;
  76.                                 #print "Type: $type \n";
  77.                                 $par[0] = $id;
  78.                                 $par[1] = $type;
  79.                                 $par[2] = $imp;
  80.                                 push(@nodes, [@par]);
  81.                         }
  82.                         elsif($begin eq "l")
  83.                         {
  84.                                 $row            =~s/^l\t+//;
  85.                                 $begNode        =$row;
  86.                                 $begNode        =~s/\t+.*//;
  87.                                 $begNode        =~s/\n//;
  88.                                 #print "begNode: $begNode | ";
  89.                                 $type           =$row;
  90.                                 $type           =~s/^.+\t+//;
  91.                                 #print "type: $type | ";
  92.                                 $termNode       =$row;
  93.                                 $termNode       =~s/\S+$//;
  94.                                 $termNode       =~s/\t+$//;
  95.                                 $termNode       =~s/^\S+\t+//;
  96.                                 #print "termNode: $termNode \n";
  97.                                 $par[0] = $begNode;
  98.                                 $par[1] = $termNode;
  99.                                 $par[2] = $type;
  100.                                 push(@links, [@par]);
  101.                         }
  102.                         elsif($begin eq "ia")
  103.                         {
  104.                                 $row    =~s/^ia\t+//;
  105.                                 $type   = $row;
  106.                                 $type   =~s/\t+.*//;
  107.                                 $weight = $row;
  108.  
  109.                                 $weight =~s/^.+\t+//;
  110.                                 $weight =~s/\n//;
  111.                                 $type   =~s/\n//;
  112.  
  113.                                 @par;
  114.                                 $par[0] = $type;
  115.                                 $par[1] = $weight;
  116.                                 push(@initialActive, [@par]);
  117.                         }
  118.  
  119.                         elsif($begin eq "lw")
  120.                         {
  121.                                 $row    =~s/^lw\t+//;
  122.                                 $type   = $row;
  123.                                 $type   =~s/\t+.*//;
  124.                                 $weight = $row;
  125.  
  126.                                 $weight =~s/^.+\t+//;
  127.                                 $weight =~s/\n//;
  128.                                 $type   =~s/\n//;
  129.  
  130.                                 @par;
  131.                                 $par[0] = $type;
  132.                                 $par[1] = $weight;
  133.                                 push(@linkWeight, [@par]);
  134.                         }
  135.                         elsif($begin eq "beta")
  136.                         {
  137.                                 $row    =~s/^.+\t+//;
  138.                                 $row    =~s/\n//;
  139.                                 $beta   =$row;
  140.                         }
  141.                         elsif($begin eq "iterationsno")
  142.                         {
  143.                                 $row    =~s/^.+\t+//;
  144.                                 $row    =~s/\n//;
  145.                                 $iteration   =$row;
  146.                         }
  147.                         elsif($begin eq "calibration")
  148.                         {
  149.                                 $row    =~s/^.+\t+//;
  150.                                 $row    =~s/\n//;
  151.                                 $calibration   =lc($row);
  152.                                 #print "----------|$calibration|\n";
  153.                         }
  154.                         elsif($begin eq "a")
  155.                         {
  156.                                 $row    =~s/^.+\t+//;
  157.                                 $row    =~s/\n//;
  158.                                 $a      =$row;
  159.  
  160.                         }
  161.                         elsif($begin eq "b")
  162.                         {
  163.                                 $row    =~s/^.+\t+//;
  164.                                 $row    =~s/\n//;
  165.                                 $b      =$row;
  166.  
  167.                         }
  168.                         elsif($begin eq "c")
  169.                         {
  170.                                 $row    =~s/^.+\t+//;
  171.                                 $row    =~s/\n//;
  172.                                 $c      =$row;
  173.  
  174.                         }
  175.                         elsif($begin eq "t")
  176.                         {
  177.                                 $row    =~s/^.+\t+//;
  178.                                 $row    =~s/\n//;
  179.                                 $t      =$row;
  180.  
  181.                         }
  182.  
  183.  
  184.                         #else {#noaction here}
  185.                         #print "B: |$begin|\n";
  186.  
  187.                 }
  188.         }
  189.         else
  190.         {
  191.                 print "FATAL ERROR: unable to read configuration\n";
  192.                 exit();
  193.         }
  194.         if(!$beta)
  195.         {
  196.                 $beta=0;
  197.                 print "ERROR: Variable BETA is not set,\nusing default setting. BETA
  198. = 0\n";
  199.         }
  200.         if(!$iteration)
  201.         {
  202.                 $iteration = 10;
  203.                 print "ERROR: Variable IterationNo is not set,\nusing default settin
  204. g. IterationNo = 10\n";
  205.         }
  206.         $calibration = lc($calibration);
  207.         if($calibration ne "none")
  208.         {
  209.                 if($calibration ne "conservationofinitialactivation")
  210.                 {
  211.                         if($calibration ne "conservationoftotalactivation")
  212.                         {
  213.                                 $calibration = "none";
  214.                                 print "ERROR: Calibration is not set or is set incor
  215. rectly.\nUsing default setting.\nCALIBRATION = None";
  216.                         }
  217.                 }
  218.          }
  219.  
  220. }
  221. ######################## write all data
  222. sub writeData
  223. {
  224. $x=@linkTypes;
  225. for($i=0;$i<$x;$i++)
  226. {
  227.         print "Type: ";
  228.         print $linkTypes[$i][0];
  229.         print " | ReType: ";
  230.         print $linkTypes[$i][1];
  231.         print "\n";
  232. }
  233. for $nodeType (@nodesTypes)
  234. {
  235.         print"Node type: $nodeType \n";
  236. }
  237.  
  238. $x=@nodes;
  239. for($i=0;$i<$x;$i++)
  240. {
  241.         print "ID: ";
  242.         print $nodes[$i][0];
  243.         print " | Type: ";
  244.         print $nodes[$i][1];
  245.         print " | Importance: ";
  246.         print $nodes[$i][2];
  247.         print "\n";
  248. }
  249.  
  250. $x=@links;
  251. for($i=0;$i<$x;$i++)
  252. {
  253.         print "Initial node: ";
  254.         print $links[$i][0];
  255.         print " | Terminal node: ";
  256.         print $links[$i][1];
  257.         print " | Link type: ";
  258.         print $links[$i][2];
  259.         print "\n";
  260. }
  261.  
  262. $x=@initialActive;
  263. for($i=0;$i<$x;$i++)
  264. {
  265.         print "Node ID: ";
  266.         print $initialActive[$i][0];
  267.         print " | Activation Level: ";
  268.         print $initialActive[$i][1];
  269.         print "\n";
  270. }
  271.  
  272.  
  273. $x=@linkWeight;
  274. for($i=0;$i<$x;$i++)
  275. {
  276.         print "Type: ";
  277.         print $linkWeight[$i][0];
  278.         print " | Weight: ";
  279.         print $linkWeight[$i][1];
  280.         print "\n";
  281. }
  282. print "BETA: $beta | Iterations: $iteration | Calibration: $calibration \n";
  283. print "A: $a | B: $b | C: $c | t: $t\n";
  284. }
  285. ######################## algo
  286. sub graph
  287. {
  288.         $initial = 1;
  289.  
  290.         $y=@nodes;
  291.         print "+iterace+";
  292.         for($i=0;$i<$y;$i++)
  293.         {
  294.                 $name   =$nodes[$i][0];
  295.                 $length =length($name);
  296.                 $space  =$spacing-$length;
  297.                 if(($space%2)==1)
  298.                 {
  299.                         $sBefore=int$space/2;
  300.                         $sAfter =int($space/2)+1;
  301.                 }
  302.                 else
  303.                 {
  304.                         $sBefore=$space/2;
  305.                         $sAfter=$sBefore;
  306.                 }
  307.                 for($x=0;$x<=$sBefore;$x++)
  308.                 {print "-";}
  309.                 print $name;
  310.                 for($x=0;$x<$sAfter;$x++)
  311.                 {print "-";}
  312.                 print "+";
  313.         }
  314.  
  315.         for($counter=0;$counter<=$iteration;$counter++)
  316.         {
  317.                 print "\n";
  318.                 if($initial==1)
  319.                 {
  320.                         print "|   0   |";
  321.                         $x      =@initialActive;
  322.                         $x2     =@nodes;
  323.                         for($i=0;$i<$x;$i++)
  324.                         {
  325.                                 $nameIA =$initialActive[$i][0];
  326.                                 $levelIA=$initialActive[$i][1];
  327.                                 for($y=0;$y<$x2;$y++)
  328.                                 {
  329.                                         $nameN=$nodes[$y][0];
  330.                                         if($nameN eq $nameIA)
  331.                                         {
  332.                                                 if($levelIA < 10) { printf("%.5f", $levelIA);}
  333.                                                 elsif($levelIA < 100) {printf("%.4f",$levelIA);}
  334.                                                 elsif($levelIA < 1000) {printf("%.3f",$levelIA);}
  335.                                                 elsif($levelIA < 10000) {printf("%.2f",$levelIA);}
  336.                                                 elsif($levelIA < 100000) {printf("%.1f",$levelIA);}
  337.                                                 else{printf("%07d",$levelIA);}
  338.                                                 print "|";
  339.                                         }
  340.                                         else
  341.                                         {
  342.                                                 print "   0   |";
  343.                                         }
  344.                                 }
  345.                         }
  346.                 $initial=0;
  347.                 }
  348.         }
  349.  
  350. }
  351. ######################## main program
  352. readConfig($ARGV[0]);
  353. #writeData();
  354. graph();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement