View Javadoc

1   /*
2    * Copyright 2006 - 2012 Christina Bohk and Roland Ewald
3    *  
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at 
7    *  
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *  
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
15   */
16  package p3j.gui.dialogs.execstatus;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.jamesii.core.experiments.instrumentation.computation.IComputationInstrumenter;
22  import org.jamesii.core.experiments.tasks.IComputationTask;
23  import org.jamesii.core.observe.IObserver;
24  import org.jamesii.core.observe.Mediator;
25  import org.jamesii.core.processor.IProcessor;
26  
27  /**
28   * Instruments PPPM simulations with an observer that displays the progress to
29   * the user.
30   * 
31   * @author Christina Bohk
32   * @author Roland Ewald
33   * 
34   */
35  public class ExecutionProgressInstrumenter implements IComputationInstrumenter {
36  
37    /** Serialization ID. */
38    private static final long serialVersionUID = 1037938671428167027L;
39  
40    /**
41     * List of observers. To make sure that the same dialog is used in case of
42     * multiple threads, only *one* instance of the observer is created and then
43     * saved for future use.
44     * 
45     * TODO: This should be solved more elegantly.
46     */
47    private static List<IObserver<?>> observers = new ArrayList<>();
48  
49    /** The number of trials. */
50    private int numberOfTrials;
51  
52    /**
53     * Instantiates a new execution progress instrumenter.
54     */
55    public ExecutionProgressInstrumenter() {
56  
57    }
58  
59    /**
60     * Instantiates a new execution progress instrumenter.
61     * 
62     * @param numOfTrials
63     *          the num of trials
64     */
65    public ExecutionProgressInstrumenter(Integer numOfTrials) {
66      observers.clear();
67      numberOfTrials = numOfTrials;
68    }
69  
70    @Override
71    public void instrumentComputation(IComputationTask computationTask) {
72  
73      synchronized (observers) {
74        if (observers.size() == 0) {
75          observers.add(new ExecutionProgressDialog(numberOfTrials));
76        }
77        ((ExecutionProgressDialog) observers.get(0))
78            .addSimulationRun(computationTask);
79      }
80      IProcessor simulator = computationTask.getProcessorInfo().getLocal();
81      Mediator.create(simulator);
82      simulator.registerObserver(observers.get(0));
83    }
84  
85    @Override
86    public List<? extends IObserver<?>> getInstantiatedObservers() {
87      return observers;
88    }
89  
90    /**
91     * Cleans list of observers.
92     */
93    public static void cleanObservers() {
94      observers.clear();
95    }
96  
97    public int getNumberOfTrials() {
98      return numberOfTrials;
99    }
100 
101   public void setNumberOfTrials(int numberOfTrials) {
102     this.numberOfTrials = numberOfTrials;
103   }
104 }