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.util.event;
21 import gov.nasa.jpf.util.Predicate;
22 import gov.nasa.jpf.vm.ChoiceGeneratorBase;
23 import gov.nasa.jpf.vm.SystemState;
27 * ChoiceGenerator for Events.
28 * This is basically just a pointer into the event tree
30 public class EventChoiceGenerator extends ChoiceGeneratorBase<Event> {
34 protected int nProcessed;
36 protected EventContext ctx; // optional, can replace/expand events during execution
39 * convenience method to get successors from current CG chain
41 public static EventChoiceGenerator getNext (SystemState ss, String id, Event base, EventContext ctx){
42 EventChoiceGenerator cgPrev = ss.getLastChoiceGeneratorOfType(EventChoiceGenerator.class);
44 return new EventChoiceGenerator( id, base, ctx);
46 return cgPrev.getSuccessor(id, ctx);
50 public EventChoiceGenerator (String id, Event base){
54 public EventChoiceGenerator (String id, Event base, EventContext ctx) {
61 public Event getChoice (int idx){
64 for (int i=0; i<idx; i++){
74 throw new IllegalArgumentException("choice index out of range: " + idx);
78 public void setContextExpander (EventContext ctx){
82 public boolean containsMatchingChoice (Predicate<Event> predicate){
83 for (Event e = base; e != null; e = e.getAlt()){
84 if (predicate.isTrue(e)){
91 public void addChoice (Event newEvent){
92 for (Event e = base; e != null;){
93 Event eAlt = e.getAlt();
102 public EventChoiceGenerator getSuccessor (String id){
103 return getSuccessor(id, null);
106 public EventChoiceGenerator getSuccessor (String id, EventContext ctx){
108 return new EventChoiceGenerator(id, base.getNext(), ctx);
111 Event next = cur.getNext();
113 if (cur instanceof CheckEvent){ // CheckEvents use next for conjunction
114 while (next instanceof CheckEvent){
115 next = next.getNext();
120 return new EventChoiceGenerator( id, next, ctx);
128 public Event getNextChoice () {
134 public boolean hasMoreChoices () {
136 return (nProcessed == 0);
138 return (cur.getAlt() != null);
143 public void advance () {
145 if (nProcessed == 0){
155 Event newCur = ctx.map(cur);
163 public void reset () {
170 public int getTotalNumberOfChoices () {
171 return base.getNumberOfAlternatives() + 1; // include base itself
175 public int getProcessedNumberOfChoices () {
180 public String toString() {
181 StringBuilder sb = new StringBuilder(getClass().getName());
186 //sb.append(",isCascaded:");
187 //sb.append(Boolean.toString(isCascaded));
190 for (Event e=base; e!= null; e = e.getAlt()){
197 sb.append(e.toString());
203 return sb.toString();
207 public Class<Event> getChoiceType() {
211 protected Event[] getFirstNChoices(int n){
212 Event[] a = new Event[n];
215 for (int i=0; i<n; i++){
224 public Event[] getAllChoices(){
225 return getFirstNChoices( getTotalNumberOfChoices());
229 public Event[] getProcessedChoices(){
230 return getFirstNChoices( getProcessedNumberOfChoices());
234 public Event[] getUnprocessedChoices(){
236 for (Event e=cur; e != null; e = e.getAlt()){
240 Event[] a = new Event[n];
243 for (int i=0; i<n; i++){