Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1.  
  2. module src1;
  3.  
  4. import java.io.*;
  5. import java.util.*;
  6. import java.lang.Math;
  7.  
  8. behavior HelloWorld {
  9. Map documents = new HashMap();
  10. String directory;
  11. String outDir;
  12. int n; //Number of total Files to parse
  13. int noActors;
  14. long initialTime; // Starting Time
  15. Map allWords = new HashMap();
  16. Map allWordsStartingWithCharacter = new HashMap();
  17.  
  18. void act( String[] argv ){
  19. int argc = argv.length;
  20. if(argc>=1) directory = args[0];
  21. if(argc>=2) outDir = args[1];;
  22. if(argc>=3) n=Integer.parseInt(args[2]);
  23. if(argc>=4) noActors=Integer.parseInt(args[3]);
  24. DistributeWork()@writeResults(token)@endTimer();
  25. }
  26.  
  27. HashMap DistributeWork(){
  28. int local_n = n/noActors; // Number of files per actor
  29. double local_start; // start of my process
  30. double local_end; // end of my process
  31.  
  32. //create actors and assign data
  33. FileWorker[] workers = new FileWorker[noActors];
  34. for (int i=0; i<noActors; i++){
  35. workers[i] = new FileWorker();
  36. }
  37. char[] L = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
  38.  
  39. for(int i = 0; i < 26; i++){
  40. Map map = new HashMap();
  41. allWordsStartingWithCharacter.put(L[i], map);
  42. }
  43.  
  44. // read available theaters
  45. // Vector theaters = new Vector();
  46. // String theater;
  47. // try {
  48. // BufferedReader in = new BufferedReader(new FileReader(theatersFile));
  49. // while ((theater = in.readLine())!= null){
  50. // theaters.add(theater);
  51. // }
  52. // in.close();
  53. // } catch (IOException ioe){
  54. // standardOutput<-println("[error] Can't open the file "+theatersFile+" for reading.");
  55. // }
  56.  
  57. // migrate actors to theaters
  58. // for (int i=0; i<noActors; i++){
  59. // standardOutput<-println("Sending actor "+
  60. // "uan://"+nameServer+":3030/a"+i+" to "+
  61. // "rmsp://"+theaters.get(i%theaters.size())+"/a"+i);
  62.  
  63. // workers[i] = new FileWorker() at
  64. // (new UAN("uan://"+nameServer+":3030/a"+i),
  65. // new UAL("rmsp://"+theaters.get(i%theaters.size())+"/a"+i)
  66. // );
  67. // }
  68.  
  69. //start computation
  70. standardOutput<-println(">>>>>>Starting the computation");
  71. initialTime = System.currentTimeMillis();
  72. join{
  73. for (int i=0;i<noActors;i++){
  74.  
  75. /* Length of each actor's interval of
  76. * integration = local_n*h. So my interval
  77. * starts at: */
  78.  
  79. local_start = start + i * local_n;
  80. local_end = local_start + local_n ;
  81. workers[i]<-trap(local_start, local_end, local_n, allWordsStartingWithCharacter);
  82. }
  83. }@mergeMapResults(token)@currentContinuation;
  84. }
  85. void endTimer(){
  86. long finalTime = System.currentTimeMillis();
  87. long runningTime = finalTime - initialTime;
  88. standardOutput<-println("Running time for Assignment 3" +
  89. " approximation is "+runningTime+" ms.");
  90. }
  91.  
  92.  
  93. HashMap mergeMapResults( Object[] objs ) {
  94. System.out.println( "Merging " + objs.length + " results from mappers" );
  95.  
  96. HashMap mergedMap = new HashMap();
  97.  
  98. for (int i = 0; i < objs.length; i++) {
  99. HashMap map = (HashMap)objs[i];
  100. Iterator it = map.entrySet().iterator();
  101.  
  102. while (it.hasNext()) {
  103. HashMap.Entry entry = (HashMap.Entry)it.next();
  104. double value [] = (double[])mergedMap.get( entry.getKey() );
  105.  
  106. if (value == null) {
  107. mergedMap.put( entry.getKey(), entry.getValue() );
  108. }
  109. else {
  110. value.append( (double[])entry.getValue() );
  111. mergedMap.put( entry.getKey(), value );
  112. }
  113. }
  114. }
  115.  
  116. return mergedMap;
  117. }
  118.  
  119. void writeResults() {
  120. char[] L = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
  121. for(int i = 0; i < 26; i++){
  122. Writer writer = null;
  123.  
  124. try {
  125. writer = new BufferedWriter(new OutputStreamWriter(
  126. new FileOutputStream("words-" + L[i] + ".txt"), "utf-8"));
  127. writer.write("Document lfkadjsf for letter " + L[i]);
  128. Iterator it = ((Map)allWordsStartingWithCharacter.get(L[i])).entrySet().iterator();
  129. while (it.hasNext()) {
  130. Map.Entry entry = (Map.Entry)it.next();
  131. Object key = entry.getKey();
  132. double value[] = new double[2];
  133. value = (double[])entry.getValue();
  134. writer.write(key + " | " + value[0] + " | " + value[1] + "\n");
  135. }
  136. } catch (IOException ex) {
  137. standardOutput<-println("exception here");
  138. standardOutput<-println(ex);
  139. // report
  140. } finally {
  141. try {writer.close();} catch (Exception ex) {}
  142. }
  143.  
  144. }
  145. }
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement