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;
22 * abstract class that hold API to create event trees
24 * this factors out constructor methods so that they can be used inside of
25 * EventTrees and EventForests
27 public interface EventConstructor {
29 //--- overridable event factory method to facilitate creation of specialized event classes
31 default Event event (String name) {
32 return new Event(name, this);
35 default Event event (String name, Object... arguments){
36 return new Event(name, arguments, this);
39 //--- compound constructors that create sets of events
41 default Event alternatives (Event... events){
42 Event last = events[0];
43 for (int i = 1; i < events.length; i++) {
51 default Event sequence (Event... events) {
52 Event base = events[0];
54 for (int i = 1; i < events.length; i++) {
55 base.addNext(events[i]);
60 default Event iteration (int count, Event... events) {
61 Event seq = sequence(events);
62 Event[] it = new Event[count];
65 for (int i=1; i<count; i++){
66 it[i] = seq.deepClone();
73 * an alterative of all combinations of the specified events (regardless of order)
75 default Event anyCombination (Event... events){
76 int n = events.length;
78 for (int i=0; i<n; i++){
82 Event[] pathBuffer = new Event[n];
84 // we use the no-event as the anchor
85 Event eFirst = new NoEvent();
87 // now fill in all the remaining combinations
88 for (int i=1; i<=max; i++){
89 // init the path buffer
91 for (int j=0, m=i; m != 0; j++){
93 pathBuffer[pathLength++] = events[j];
98 eFirst.addPath( pathLength, pathBuffer);
105 default void generatePermutation (int length, Event[] events, Event anchor, Event perm){
107 anchor.addAlternative(perm);
111 for (Event e : events){
113 // check if e is already in there
114 for (Event ee = perm; ee != null; ee = ee.getNext()){
119 e = perm.deepClone().addNext(e.deepClone());
125 generatePermutation( length-1, events, anchor, e);
131 * generate tree with all event permutations without repetitions.
132 * <2do> this is not particularly optimized
134 default Event anyPermutation (Event... events){
135 Event a = new NoEvent();
136 generatePermutation( events.length, events, a, null);