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.json;
21 import gov.nasa.jpf.vm.ChoiceGenerator;
23 import java.util.ArrayList;
24 import java.util.List;
28 * @author Ivan Mushketik
32 private ArrayList<Value> params = new ArrayList<Value>();
35 public CGCall(String name) {
39 void addParam(Value value) {
41 throw new NullPointerException("Null value added to CGCall");
47 public Value[] getValues() {
48 Value paramsArr[] = new Value[params.size()];
49 params.toArray(paramsArr);
54 public String getName() {
59 * This method go through JSON object and finds all CGs to set in a current state.
60 * It also gives each CG it's a unique id. Each id is calculated in a following way:
61 * <li> If CG call is in the root of JSON object it's id will be equals to it's key in JSON
62 * <li> If CG call isn't in a root object it's id will be concatenated keys of
63 * all parent objects + key of CG.
64 * <li> If CG call is in an object that is an element of array CG's id will be
65 * concatenated keys of all parent array + "[i" (where i is a pos of parent object
66 * of CG in array + CG's key)
68 * This id also used in JSONObject.fillObject() to find CG to use when field that
69 * should be set with CG is found.
71 * @param jsonObject - parsed JSON object
72 * @param CGCreators - hash of factories to create Choice Generators
73 * @return list of choice generators that should be set in a current state.
75 public static List<ChoiceGenerator<?>> createCGList(JSONObject jsonObject) {
76 List<ChoiceGenerator<?>> result = new ArrayList<ChoiceGenerator<?>>();
77 createCGs(jsonObject, "", result);
82 private static void createCGs(JSONObject jsonObject, String prefix, List<ChoiceGenerator<?>> result) {
83 for (String cgKey : jsonObject.getCGCallsKeys()) {
84 CGCall cgCall = jsonObject.getCGCall(cgKey);
85 CGCreator creator = CGCreatorFactory.getFactory().getCGCreator(cgCall.getName());
87 ChoiceGenerator<?> newCG = creator.createCG(prefix + cgKey, cgCall.getValues());
91 for (String valueKey : jsonObject.getValuesKeys()) {
92 Value v = jsonObject.getValue(valueKey);
94 if (v instanceof JSONObjectValue) {
95 createCGs(v.getObject(), prefix + valueKey, result);
97 } else if (v instanceof ArrayValue) {
98 Value[] values = v.getArray();
100 for (int i = 0; i < values.length; i++) {
101 if (values[i] instanceof JSONObjectValue) {
102 createCGs(values[i].getObject(), prefix + valueKey + "[" + i, result);