Search in sources :

Example 1 with ReconfigurationPlanConverter

use of org.btrplace.json.plan.ReconfigurationPlanConverter in project scheduler by btrplace.

the class JSON method write.

/**
 * Write a reconfiguration plan.
 *
 * @param plan the plan to write
 * @param a    the stream to write on.
 * @throws IllegalArgumentException if an error occurred while writing the json
 */
public static void write(ReconfigurationPlan plan, Appendable a) {
    try {
        ReconfigurationPlanConverter c = new ReconfigurationPlanConverter();
        c.toJSON(plan).writeJSONString(a);
    } catch (IOException | JSONConverterException e) {
        throw new IllegalArgumentException(e);
    }
}
Also used : ReconfigurationPlanConverter(org.btrplace.json.plan.ReconfigurationPlanConverter)

Example 2 with ReconfigurationPlanConverter

use of org.btrplace.json.plan.ReconfigurationPlanConverter 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();
}
Also used : ScheduleConverter(org.btrplace.safeplace.testing.verification.btrplace.ScheduleConverter) JSONObject(net.minidev.json.JSONObject) Constant(org.btrplace.safeplace.spec.term.Constant) InstanceConverter(org.btrplace.json.model.InstanceConverter) ReconfigurationPlanConverter(org.btrplace.json.plan.ReconfigurationPlanConverter) JSONArray(net.minidev.json.JSONArray)

Example 3 with ReconfigurationPlanConverter

use of org.btrplace.json.plan.ReconfigurationPlanConverter 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;
}
Also used : Constant(org.btrplace.safeplace.spec.term.Constant) ModelView(org.btrplace.model.view.ModelView) JSONConverterException(org.btrplace.json.JSONConverterException) ReconfigurationPlanConverter(org.btrplace.json.plan.ReconfigurationPlanConverter) Collectors(java.util.stream.Collectors) InstanceConverter(org.btrplace.json.model.InstanceConverter) ReconfigurationPlan(org.btrplace.plan.ReconfigurationPlan) ArrayList(java.util.ArrayList) JSONParser(net.minidev.json.parser.JSONParser) Objects(java.util.Objects) Constraint(org.btrplace.safeplace.spec.Constraint) List(java.util.List) StringReader(java.io.StringReader) JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject) ParseException(net.minidev.json.parser.ParseException) ScheduleConverter(org.btrplace.safeplace.testing.verification.btrplace.ScheduleConverter) Optional(java.util.Optional) Instance(org.btrplace.model.Instance) Collections(java.util.Collections) SatConstraint(org.btrplace.model.constraint.SatConstraint) ScheduleConverter(org.btrplace.safeplace.testing.verification.btrplace.ScheduleConverter) Constraint(org.btrplace.safeplace.spec.Constraint) SatConstraint(org.btrplace.model.constraint.SatConstraint) Instance(org.btrplace.model.Instance) Constant(org.btrplace.safeplace.spec.term.Constant) InstanceConverter(org.btrplace.json.model.InstanceConverter) ReconfigurationPlan(org.btrplace.plan.ReconfigurationPlan) ArrayList(java.util.ArrayList) JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject) StringReader(java.io.StringReader) ReconfigurationPlanConverter(org.btrplace.json.plan.ReconfigurationPlanConverter) JSONParser(net.minidev.json.parser.JSONParser) JSONObject(net.minidev.json.JSONObject)

Aggregations

ReconfigurationPlanConverter (org.btrplace.json.plan.ReconfigurationPlanConverter)3 JSONArray (net.minidev.json.JSONArray)2 JSONObject (net.minidev.json.JSONObject)2 InstanceConverter (org.btrplace.json.model.InstanceConverter)2 Constant (org.btrplace.safeplace.spec.term.Constant)2 ScheduleConverter (org.btrplace.safeplace.testing.verification.btrplace.ScheduleConverter)2 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 JSONParser (net.minidev.json.parser.JSONParser)1 ParseException (net.minidev.json.parser.ParseException)1 JSONConverterException (org.btrplace.json.JSONConverterException)1 Instance (org.btrplace.model.Instance)1 SatConstraint (org.btrplace.model.constraint.SatConstraint)1 ModelView (org.btrplace.model.view.ModelView)1 ReconfigurationPlan (org.btrplace.plan.ReconfigurationPlan)1