comparison src/main/gov/nasa/jpf/vm/choice/DoubleThresholdGenerator.java @ 0:61d41facf527

initial v8 import (history reset)
author Peter Mehlitz <Peter.C.Mehlitz@nasa.gov>
date Fri, 23 Jan 2015 10:14:01 -0800
parents
children fdc263e5806b
comparison
equal deleted inserted replaced
-1:000000000000 0:61d41facf527
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 void reset () {
44 count = -1;
45
46 isDone = false;
47 }
48
49 @Override
50 public boolean hasMoreChoices () {
51 return !isDone && (count < 2);
52 }
53
54 @Override
55 public Double getNextChoice () {
56 if (count >=0) {
57 return new Double(values[count]);
58 } else {
59 return new Double(values[0]);
60 }
61 }
62
63 @Override
64 public void advance () {
65 if (count < 2)
66 count++;
67 }
68
69 @Override
70 public int getTotalNumberOfChoices () {
71 return 3;
72 }
73
74 @Override
75 public int getProcessedNumberOfChoices () {
76 return count + 1;
77 }
78
79 @Override
80 public String toString() {
81 StringBuilder sb = new StringBuilder(getClass().getName());
82 sb.append("[id=\"");
83 sb.append(id);
84 sb.append("\",");
85
86 for (int i=0; i<3; i++) {
87 if (count == i) {
88 sb.append(MARKER);
89 }
90 sb.append(values[i]);
91 if (count < 2) {
92 sb.append(',');
93 }
94 }
95 sb.append(']');
96 return sb.toString();
97 }
98
99 @Override
100 public DoubleThresholdGenerator randomize () {
101 for (int i = values.length - 1; i > 0; i--) {
102 int j = random.nextInt(i + 1);
103 double tmp = values[i];
104 values[i] = values[j];
105 values[j] = tmp;
106 }
107 return this;
108 }
109
110 @Override
111 public Class<Double> getChoiceType() {
112 return Double.class;
113 }
114 }