Initial import
[jpf-core.git] / src / main / gov / nasa / jpf / vm / choice / DoubleThresholdGenerator.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18 package gov.nasa.jpf.vm.choice;
19
20 import gov.nasa.jpf.Config;
21 import gov.nasa.jpf.vm.ChoiceGeneratorBase;
22 import gov.nasa.jpf.vm.DoubleChoiceGenerator;
23
24 /**
25  * ChoiceGenerator instance that produces a simple 3 value enumeration
26  * 
27  */
28 public class DoubleThresholdGenerator extends ChoiceGeneratorBase<Double> implements DoubleChoiceGenerator {
29
30   protected double[] values = new double[3];
31   protected int count;
32
33   public DoubleThresholdGenerator(Config conf, String id) {
34     super(id);
35
36     values[0] = conf.getDouble(id + ".low");
37     values[1] = conf.getDouble(id + ".threshold");
38     values[2] = conf.getDouble(id + ".high");
39     count = -1;
40   }
41
42   @Override
43   public Double getChoice (int idx){
44     if (idx >= 0 && idx < 3){
45       return values[idx];
46     } else {
47       throw new IllegalArgumentException("choice index out of range: " + idx);
48     }
49   }
50   
51   @Override
52   public void reset () {
53     count = -1;
54
55     isDone = false;
56   }
57
58   @Override
59   public boolean hasMoreChoices () {
60     return !isDone && (count < 2);
61   }
62
63   @Override
64   public Double getNextChoice () {
65     if (count >=0) {
66       return new Double(values[count]);
67     } else {
68       return new Double(values[0]);
69     }
70   }
71
72   @Override
73   public void advance () {
74     if (count < 2)
75       count++;
76   }
77
78   @Override
79   public int getTotalNumberOfChoices () {
80     return 3;
81   }
82
83   @Override
84   public int getProcessedNumberOfChoices () {
85     return count + 1;
86   }
87
88   @Override
89   public String toString() {
90     StringBuilder sb = new StringBuilder(getClass().getName());
91     sb.append("[id=\"");
92     sb.append(id);
93     sb.append("\",");
94     
95     for (int i=0; i<3; i++) {
96       if (count == i) {
97         sb.append(MARKER);
98       }
99       sb.append(values[i]);
100       if (count < 2) {
101         sb.append(',');
102       }
103     }
104     sb.append(']');
105     return sb.toString();
106   }
107   
108   @Override
109   public DoubleThresholdGenerator randomize () {
110     for (int i = values.length - 1; i > 0; i--) {
111       int j = random.nextInt(i + 1);
112       double tmp = values[i];
113       values[i] = values[j];
114       values[j] = tmp;
115     }    
116     return this;
117   }
118
119   @Override
120   public Class<Double> getChoiceType() {
121     return Double.class;
122   }
123 }