Initial import
[jpf-core.git] / src / main / gov / nasa / jpf / vm / choice / ExceptionThreadChoiceFromSet.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
19 package gov.nasa.jpf.vm.choice;
20
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;
26
27 /**
28  * a ThreadChoiceFromSet that reschedules the specified thread with exceptions
29  */
30 public class ExceptionThreadChoiceFromSet extends ThreadChoiceFromSet {
31
32   protected ThreadInfo exceptionThread;
33   protected String[] exceptions;
34   
35   public ExceptionThreadChoiceFromSet (String id, ThreadInfo[] runnables, ThreadInfo exceptionThread, String[] exceptionClsNames){
36     super(id);
37     
38     this.exceptionThread = exceptionThread;
39     
40     values = new ThreadInfo[runnables.length + exceptionClsNames.length];
41     exceptions = new String[values.length];
42     
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];
47     }
48     
49     isSchedulingPoint = true; // not much use otherwise
50   }
51   
52   public String getExceptionForCurrentChoice(){
53     if ((count >= 0) && (count < values.length)) {
54       return exceptions[count];
55     } else {
56       return null;
57     }
58   }
59   
60   @Override
61   public ThreadChoiceGenerator reorder (Comparator<ThreadInfo> comparator){
62     ThreadInfo[] newValues = values.clone();
63     Arrays.sort(newValues, comparator);
64     
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];
72       }
73     }
74
75     try {
76       ExceptionThreadChoiceFromSet reorderedCG = (ExceptionThreadChoiceFromSet)clone();
77       reorderedCG.values = newValues;
78       reorderedCG.exceptions = newExceptions;
79       reorderedCG.count = -1;
80       
81       return reorderedCG;
82       
83     } catch (CloneNotSupportedException cnsx){
84       throw new JPFException("clone of ExceptionalThreadChoice failed");
85     }
86   }
87   
88   @Override
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];
94       values[j] = tmp;
95       
96       String tmpX = exceptions[i];
97       exceptions[i] = exceptions[j];
98       exceptions[j] = tmpX;
99     }
100     return this;
101   }
102 }