Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module src1;
- import java.io.*;
- import java.util.*;
- import java.lang.Math;
- behavior HelloWorld {
- Map documents = new HashMap();
- String directory;
- String outDir;
- int n; //Number of total Files to parse
- int noActors;
- long initialTime; // Starting Time
- Map allWords = new HashMap();
- Map allWordsStartingWithCharacter = new HashMap();
- void act( String[] argv ){
- int argc = argv.length;
- if(argc>=1) directory = args[0];
- if(argc>=2) outDir = args[1];;
- if(argc>=3) n=Integer.parseInt(args[2]);
- if(argc>=4) noActors=Integer.parseInt(args[3]);
- DistributeWork()@writeResults(token)@endTimer();
- }
- HashMap DistributeWork(){
- int local_n = n/noActors; // Number of files per actor
- double local_start; // start of my process
- double local_end; // end of my process
- //create actors and assign data
- FileWorker[] workers = new FileWorker[noActors];
- for (int i=0; i<noActors; i++){
- workers[i] = new FileWorker();
- }
- 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'};
- for(int i = 0; i < 26; i++){
- Map map = new HashMap();
- allWordsStartingWithCharacter.put(L[i], map);
- }
- // read available theaters
- // Vector theaters = new Vector();
- // String theater;
- // try {
- // BufferedReader in = new BufferedReader(new FileReader(theatersFile));
- // while ((theater = in.readLine())!= null){
- // theaters.add(theater);
- // }
- // in.close();
- // } catch (IOException ioe){
- // standardOutput<-println("[error] Can't open the file "+theatersFile+" for reading.");
- // }
- // migrate actors to theaters
- // for (int i=0; i<noActors; i++){
- // standardOutput<-println("Sending actor "+
- // "uan://"+nameServer+":3030/a"+i+" to "+
- // "rmsp://"+theaters.get(i%theaters.size())+"/a"+i);
- // workers[i] = new FileWorker() at
- // (new UAN("uan://"+nameServer+":3030/a"+i),
- // new UAL("rmsp://"+theaters.get(i%theaters.size())+"/a"+i)
- // );
- // }
- //start computation
- standardOutput<-println(">>>>>>Starting the computation");
- initialTime = System.currentTimeMillis();
- join{
- for (int i=0;i<noActors;i++){
- /* Length of each actor's interval of
- * integration = local_n*h. So my interval
- * starts at: */
- local_start = start + i * local_n;
- local_end = local_start + local_n ;
- workers[i]<-trap(local_start, local_end, local_n, allWordsStartingWithCharacter);
- }
- }@mergeMapResults(token)@currentContinuation;
- }
- void endTimer(){
- long finalTime = System.currentTimeMillis();
- long runningTime = finalTime - initialTime;
- standardOutput<-println("Running time for Assignment 3" +
- " approximation is "+runningTime+" ms.");
- }
- HashMap mergeMapResults( Object[] objs ) {
- System.out.println( "Merging " + objs.length + " results from mappers" );
- HashMap mergedMap = new HashMap();
- for (int i = 0; i < objs.length; i++) {
- HashMap map = (HashMap)objs[i];
- Iterator it = map.entrySet().iterator();
- while (it.hasNext()) {
- HashMap.Entry entry = (HashMap.Entry)it.next();
- double value [] = (double[])mergedMap.get( entry.getKey() );
- if (value == null) {
- mergedMap.put( entry.getKey(), entry.getValue() );
- }
- else {
- value.append( (double[])entry.getValue() );
- mergedMap.put( entry.getKey(), value );
- }
- }
- }
- return mergedMap;
- }
- void writeResults() {
- 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'};
- for(int i = 0; i < 26; i++){
- Writer writer = null;
- try {
- writer = new BufferedWriter(new OutputStreamWriter(
- new FileOutputStream("words-" + L[i] + ".txt"), "utf-8"));
- writer.write("Document lfkadjsf for letter " + L[i]);
- Iterator it = ((Map)allWordsStartingWithCharacter.get(L[i])).entrySet().iterator();
- while (it.hasNext()) {
- Map.Entry entry = (Map.Entry)it.next();
- Object key = entry.getKey();
- double value[] = new double[2];
- value = (double[])entry.getValue();
- writer.write(key + " | " + value[0] + " | " + value[1] + "\n");
- }
- } catch (IOException ex) {
- standardOutput<-println("exception here");
- standardOutput<-println(ex);
- // report
- } finally {
- try {writer.close();} catch (Exception ex) {}
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement