Search in sources :

Example 6 with Constant

use of org.btrplace.safeplace.spec.term.Constant 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)

Example 7 with Constant

use of org.btrplace.safeplace.spec.term.Constant in project scheduler by btrplace.

the class DefaultFuzzer method get.

@Override
public TestCase get() {
    ReconfigurationPlan p;
    fuzzingDuration = -System.currentTimeMillis();
    lastValidationDuration = 0;
    iterations = 0;
    TestCase tc;
    do {
        lastValidationDuration += predicates.lastDuration();
        p = fuzzer.get();
        tc = new TestCase(InstanceConverter.toInstance(p), p, cstr);
        iterations++;
        if (iterations > 10000) {
            // We abandon the fuzzing stage. Too hard to get a validation.
            return null;
        }
    } while (!predicates.test(tc));
    lastValidationDuration += predicates.lastDuration();
    List<Constant> specArgs = new ArrayList<>();
    for (UserVar<?> v : cstr.args()) {
        Domain<?> d = domain(v, p.getOrigin());
        Object o = v.pick(d);
        specArgs.add(new Constant(o, v.type()));
    }
    tc.args(specArgs);
    if (cstr.isSatConstraint()) {
        SatConstraint impl = cstr.instantiate(specArgs.stream().map(c -> c.eval(null)).collect(Collectors.toList()));
        tc.instance().getSatConstraints().add(impl);
        fuzzRestriction(impl);
        tc.impl(impl);
    }
    fuzzingDuration += System.currentTimeMillis();
    store(tc);
    return tc;
}
Also used : TestCase(org.btrplace.safeplace.testing.TestCase) Constant(org.btrplace.safeplace.spec.term.Constant) ReconfigurationPlan(org.btrplace.plan.ReconfigurationPlan) SatConstraint(org.btrplace.model.constraint.SatConstraint) ArrayList(java.util.ArrayList)

Example 8 with Constant

use of org.btrplace.safeplace.spec.term.Constant in project scheduler by btrplace.

the class PlusTest method testBadCollections.

@Test(expectedExceptions = { RuntimeException.class })
public void testBadCollections() throws RuntimeException {
    Constant v1 = new Constant(new HashSet<>(Arrays.asList(1, 2)), new SetType(IntType.getInstance()));
    Constant v2 = new Constant(new HashSet<>(Collections.singletonList(VMStateType.getInstance().parse("running"))), new SetType(VMStateType.getInstance()));
    @SuppressWarnings("unused") SetPlus bad = new SetPlus(v1, v2);
}
Also used : SetType(org.btrplace.safeplace.spec.type.SetType) Constant(org.btrplace.safeplace.spec.term.Constant) SetPlus(org.btrplace.safeplace.spec.term.SetPlus) Test(org.testng.annotations.Test)

Aggregations

Constant (org.btrplace.safeplace.spec.term.Constant)8 SetType (org.btrplace.safeplace.spec.type.SetType)4 Test (org.testng.annotations.Test)4 ArrayList (java.util.ArrayList)2 Set (java.util.Set)2 JSONArray (net.minidev.json.JSONArray)2 JSONObject (net.minidev.json.JSONObject)2 InstanceConverter (org.btrplace.json.model.InstanceConverter)2 ReconfigurationPlanConverter (org.btrplace.json.plan.ReconfigurationPlanConverter)2 SatConstraint (org.btrplace.model.constraint.SatConstraint)2 ReconfigurationPlan (org.btrplace.plan.ReconfigurationPlan)2 Constraint (org.btrplace.safeplace.spec.Constraint)2 SetMinus (org.btrplace.safeplace.spec.term.SetMinus)2 SetPlus (org.btrplace.safeplace.spec.term.SetPlus)2 ScheduleConverter (org.btrplace.safeplace.testing.verification.btrplace.ScheduleConverter)2 Context (org.btrplace.safeplace.testing.verification.spec.Context)2 StringReader (java.io.StringReader)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1