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.
19 package gov.nasa.jpf.vm.choice;
21 import gov.nasa.jpf.JPFException;
22 import gov.nasa.jpf.vm.ThreadChoiceGenerator;
23 import gov.nasa.jpf.vm.ThreadInfo;
24 import java.util.Arrays;
25 import java.util.Comparator;
28 * a ThreadChoiceFromSet that reschedules the specified thread with exceptions
30 public class ExceptionThreadChoiceFromSet extends ThreadChoiceFromSet {
32 protected ThreadInfo exceptionThread;
33 protected String[] exceptions;
35 public ExceptionThreadChoiceFromSet (String id, ThreadInfo[] runnables, ThreadInfo exceptionThread, String[] exceptionClsNames){
38 this.exceptionThread = exceptionThread;
40 values = new ThreadInfo[runnables.length + exceptionClsNames.length];
41 exceptions = new String[values.length];
43 System.arraycopy(runnables, 0, values, 0, runnables.length);
44 for (int i=0, j=runnables.length; i<exceptionClsNames.length; i++, j++){
45 values[j] = exceptionThread;
46 exceptions[j] = exceptionClsNames[i];
49 isSchedulingPoint = true; // not much use otherwise
52 public String getExceptionForCurrentChoice(){
53 if ((count >= 0) && (count < values.length)) {
54 return exceptions[count];
61 public ThreadChoiceGenerator reorder (Comparator<ThreadInfo> comparator){
62 ThreadInfo[] newValues = values.clone();
63 Arrays.sort(newValues, comparator);
65 // we don't really reorder occurrences of the exceptionThread, but since the Comparator
66 // only knows ThreadInfos that shouldn't matter
67 String[] newExceptions = new String[exceptions.length];
68 for (int i=0, j=-1; i<newValues.length; i++){
69 if (newValues[i] == exceptionThread){
70 for (j++; exceptions[j] == null; j++);
71 newExceptions[i] = exceptions[j];
76 ExceptionThreadChoiceFromSet reorderedCG = (ExceptionThreadChoiceFromSet)clone();
77 reorderedCG.values = newValues;
78 reorderedCG.exceptions = newExceptions;
79 reorderedCG.count = -1;
83 } catch (CloneNotSupportedException cnsx){
84 throw new JPFException("clone of ExceptionalThreadChoice failed");
89 public ThreadChoiceFromSet randomize () {
90 for (int i = values.length - 1; i > 0; i--) {
91 int j = random.nextInt(i + 1);
92 ThreadInfo tmp = values[i];
93 values[i] = values[j];
96 String tmpX = exceptions[i];
97 exceptions[i] = exceptions[j];