comparison src/main/gov/nasa/jpf/vm/choice/TypedObjectChoice.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
19 package gov.nasa.jpf.vm.choice;
20
21 import gov.nasa.jpf.Config;
22 import gov.nasa.jpf.vm.ChoiceGeneratorBase;
23 import gov.nasa.jpf.vm.ClassInfo;
24 import gov.nasa.jpf.vm.ElementInfo;
25 import gov.nasa.jpf.vm.Heap;
26 import gov.nasa.jpf.vm.VM;
27 import gov.nasa.jpf.vm.ReferenceChoiceGenerator;
28
29 import java.util.ArrayList;
30
31 /**
32 * a choice generator that enumerates the set of all objects of a certain type. This
33 * is a replacement for the old 'Verify.randomObject'
34 */
35 public class TypedObjectChoice extends ChoiceGeneratorBase<Integer> implements ReferenceChoiceGenerator {
36
37 // the requested object type
38 protected String type;
39
40 // the object references
41 protected int[] values;
42
43 // our enumeration state
44 protected int count;
45
46
47 public TypedObjectChoice (Config conf, String id) {
48 super(id);
49
50 Heap heap = VM.getVM().getHeap();
51
52 type = conf.getString(id + ".type");
53 if (type == null) {
54 throw conf.exception("missing 'type' property for TypedObjectChoice " + id);
55 }
56
57 ArrayList<ElementInfo> list = new ArrayList<ElementInfo>();
58
59 for ( ElementInfo ei : heap.liveObjects()) {
60 ClassInfo ci = ei.getClassInfo();
61 if (ci.isInstanceOf(type)) {
62 list.add(ei);
63 }
64 }
65
66 values = new int[list.size()];
67 int i = 0;
68 for ( ElementInfo ei : list) {
69 values[i++] = ei.getObjectRef();
70 }
71
72 count = -1;
73 }
74
75 @Override
76 public void advance () {
77 count++;
78 }
79
80 @Override
81 public int getProcessedNumberOfChoices () {
82 return count+1;
83 }
84
85 @Override
86 public int getTotalNumberOfChoices () {
87 return values.length;
88 }
89
90 @Override
91 public boolean hasMoreChoices () {
92 return !isDone && (count < values.length-1);
93 }
94
95 @Override
96 public void reset () {
97 count = -1;
98
99 isDone = false;
100 }
101
102 @Override
103 public Integer getNextChoice () {
104 if ((count >= 0) && (count < values.length)) {
105 return new Integer(values[count]);
106 } else {
107 return new Integer(-1);
108 }
109 }
110
111 @Override
112 public String toString() {
113 StringBuilder sb = new StringBuilder("TypedObjectChoice [id=");
114 sb.append(id);
115 sb.append(",type=");
116 sb.append(type);
117 sb.append(",values=");
118 for (int i=0; i< values.length; i++) {
119 if (i>0) {
120 sb.append(',');
121 }
122 if (i == count) {
123 sb.append("=>");
124 }
125 sb.append(values[i]);
126 }
127 sb.append(']');
128
129 return sb.toString();
130 }
131
132 @Override
133 public TypedObjectChoice randomize() {
134 for (int i = values.length - 1; i > 0; i--) {
135 int j = random.nextInt(i + 1);
136 int tmp = values[i];
137 values[i] = values[j];
138 values[j] = tmp;
139 }
140 return this;
141 }
142
143 @Override
144 public Class<Integer> getChoiceType() {
145 return Integer.class;
146 }
147 }