2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
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
10 * http://www.apache.org/licenses/LICENSE-2.0.
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.
18 package gov.nasa.jpf.vm.choice;
20 import gov.nasa.jpf.Config;
21 import gov.nasa.jpf.JPFException;
22 import gov.nasa.jpf.vm.ChoiceGenerator;
23 import gov.nasa.jpf.vm.ChoiceGeneratorBase;
24 import gov.nasa.jpf.vm.IntChoiceGenerator;
26 import java.util.Arrays;
27 import java.util.Comparator;
30 * Choice Generator that enumerates an interval of int values. Pretty simplistic
31 * implementation for now, but at least it can count up and down
33 * randomizing is handled through RandomOrderIntCG
35 public class IntIntervalGenerator extends ChoiceGeneratorBase<Integer> implements IntChoiceGenerator {
37 protected int min, max;
42 public void reset () {
46 throw new JPFException("IntIntervalGenerator delta value is 0");
63 * don't use this since it is not safe for cascaded ChoiceGenerators
64 * (we need the 'id' to be as context specific as possible)
66 @Deprecated public IntIntervalGenerator(int min, int max){
70 @Deprecated public IntIntervalGenerator(int min, int max, int delta){
71 this("?", min, max, delta);
74 public IntIntervalGenerator(String id, int min, int max, int delta) {
84 public IntIntervalGenerator(String id, int min, int max) {
85 this(id, min, max, 1);
88 public IntIntervalGenerator(Config conf, String id) {
90 min = conf.getInt(id + ".min");
91 max = conf.getInt(id + ".max");
92 delta = conf.getInt(id + ".delta", 1);
98 public Integer getChoice (int idx){
99 int nChoices = getTotalNumberOfChoices();
100 if (idx >= 0 && idx < nChoices){
101 return min + idx*delta;
103 throw new IllegalArgumentException("choice index out of range: " + idx);
108 public Integer getNextChoice () {
109 return new Integer(next);
113 public boolean hasMoreChoices () {
126 public void advance () {
131 public int getTotalNumberOfChoices () {
132 return Math.abs((max - min) / delta) + 1;
136 public int getProcessedNumberOfChoices () {
141 return (Math.abs((next - min) / delta) + 1);
147 return (Math.abs((max - next) / delta) + 1);
152 public boolean isAscending(){
157 * note this should only be called before the CG is advanced since it resets
158 * the enumeration state
160 public void reverse(){
166 public Integer[] getChoices(){
167 int n = getTotalNumberOfChoices();
168 Integer[] vals = new Integer[n];
169 int v = (delta > 0) ? min : max;
171 for (int i=0; i<n; i++){
180 public boolean supportsReordering(){
185 public ChoiceGenerator<Integer> reorder (Comparator<Integer> comparator){
186 Integer[] vals = getChoices();
187 Arrays.sort(vals, comparator);
189 return new IntChoiceFromList(id, vals);
193 public String toString () {
194 StringBuilder sb = new StringBuilder(getClass().getName());
199 sb.append(",isCascaded:");
200 sb.append(isCascaded);
206 sb.append(",delta=");
212 sb.append(getNextChoice());
214 return sb.toString();
218 public Class<Integer> getChoiceType() {
219 return Integer.class;
223 public ChoiceGenerator<Integer> randomize() {
224 return new RandomOrderIntCG(this);