Initial import
[jpf-core.git] / src / main / gov / nasa / jpf / util / script / StringSetGenerator.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.util.script;
20
21 import java.util.ArrayList;
22 import java.util.LinkedHashMap;
23 import java.util.List;
24
25 class CG {}
26
27 class SingleChoice extends CG {
28   Event event;
29
30   SingleChoice (Event e) {
31     event = e;
32   }
33   @Override
34   public String toString() {
35     return event.toString();
36   }
37 }
38
39 class SetChoice extends CG {
40   ArrayList<Event> choices = new ArrayList<Event>();
41
42   public void add(Event e) {
43     choices.add(e);
44   }
45
46   @Override
47   public String toString() {
48     StringBuilder sb = new StringBuilder();
49     sb.append('{');
50     int i=0, n = choices.size();
51     for (Event e : choices) {
52       sb.append(e);
53       if (++i < n) sb.append(',');
54     }
55     sb.append('}');
56     return sb.toString();
57   }
58 }
59
60 /**
61  * that's mostly a test class to see what a script would be expanded to w/o
62  * having any side effects in the ElementProcessor
63  */
64 public class StringSetGenerator implements ElementProcessor {
65   LinkedHashMap<String,ArrayList<CG>> sections;
66   ArrayList<CG> queue;
67   
68   StringSetGenerator() {
69     sections = new LinkedHashMap<String,ArrayList<CG>>();
70     queue = new ArrayList<CG>();
71     sections.put("default", queue);
72   }
73   
74   @Override
75   public void process (Section sec) {
76     queue = new ArrayList<CG>();    
77     sec.processChildren(this);
78     
79     for (String id : sec.getIds()) {
80       sections.put(id,queue);      
81     }
82   }
83   
84   @Override
85   public void process (Event e) {
86     for (Event ee : e.expand()) {
87       queue.add( new SingleChoice(ee));
88     }    
89   }
90
91   @Override
92   public void process (Alternative a) {
93     SetChoice cg = new SetChoice();
94     for (ScriptElement e = a.getFirstChild(); e != null; e = e.getNextSibling()) {
95       if (e instanceof Event) {
96         for (Event ee : ((Event)e).expand()) {
97           cg.add(ee);
98         }
99       }
100     }
101     queue.add(cg);
102   }
103   
104
105   @Override
106   public void process (Repetition r) {
107     int n = r.getRepeatCount();
108     for (int i=0; i<n; i++) {
109       r.processChildren(this);
110     }
111   }
112   
113   public LinkedHashMap<String,ArrayList<CG>> getSections () {
114     return sections;
115   }
116
117   public List<CG> getCGQueue() {
118     return queue;
119   }
120 }