use of org.btrplace.json.model.InstanceConverter in project scheduler by btrplace.
the class TestCase method toJSON.
public String toJSON() throws JSONConverterException {
InstanceConverter ic = new InstanceConverter();
ic.getConstraintsConverter().register(new ScheduleConverter());
ReconfigurationPlanConverter pc = new ReconfigurationPlanConverter();
JSONObject o = new JSONObject();
o.put("constraint", constraint().id());
JSONArray a = new JSONArray();
for (Constant c : args()) {
a.add(c.toJSON());
}
o.put("args", a);
o.put("continuous", continuous());
o.put("groups", groups());
o.put("plan", pc.toJSON(plan()));
o.put("instance", ic.toJSON(instance()));
return o.toJSONString();
}
use of org.btrplace.json.model.InstanceConverter in project scheduler by btrplace.
the class TestCase method fromJSON.
public static TestCase fromJSON(List<Constraint> cstrs, String c) throws ParseException, JSONConverterException {
JSONParser p = new JSONParser(JSONParser.MODE_RFC4627);
JSONObject o = (JSONObject) p.parse(new StringReader(c));
String cId = o.getAsString("constraint");
Optional<Constraint> opt = cstrs.stream().filter(x -> x.id().equals(cId)).findFirst();
if (!opt.isPresent()) {
throw new IllegalArgumentException("Unknown constraint '" + cId + "'");
}
Constraint cstr = opt.get();
InstanceConverter ic = new InstanceConverter();
ic.getConstraintsConverter().register(new ScheduleConverter());
ReconfigurationPlanConverter rc = new ReconfigurationPlanConverter();
Instance i = ic.fromJSON(o.getAsString("instance"));
ReconfigurationPlan plan = rc.fromJSON(o.getAsString("plan"));
TestCase tc = new TestCase(i, plan, cstr);
List<Constant> l = new ArrayList<>();
for (Object x : (JSONArray) o.get("args")) {
l.add(Constant.fromJSON((JSONObject) x));
}
tc.args(l);
if (cstr.isSatConstraint()) {
tc.impl(cstr.instantiate(l.stream().map(x -> x.eval(null)).collect(Collectors.toList())));
}
if (tc.impl() != null) {
tc.impl().setContinuous((Boolean) o.get("continuous"));
}
return tc;
}
use of org.btrplace.json.model.InstanceConverter in project scheduler by btrplace.
the class JSON method write.
/**
* Write an instance
*
* @param instance the instance to write
* @param a the stream to write on.
* @throws IllegalArgumentException if an error occurred while writing the json
*/
public static void write(Instance instance, Appendable a) {
try {
InstanceConverter c = new InstanceConverter();
c.toJSON(instance).writeJSONString(a);
} catch (IOException | JSONConverterException e) {
throw new IllegalArgumentException(e);
}
}
Aggregations