All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.btrplace.safeplace.testing.TestCase Maven / Gradle / Ivy

/*
 * Copyright (c) 2017 University Nice Sophia Antipolis
 *
 * This file is part of btrplace.
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see .
 */

package org.btrplace.safeplace.testing;

import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import net.minidev.json.parser.JSONParser;
import net.minidev.json.parser.ParseException;
import org.btrplace.json.JSONConverterException;
import org.btrplace.json.model.InstanceConverter;
import org.btrplace.json.plan.ReconfigurationPlanConverter;
import org.btrplace.model.Instance;
import org.btrplace.model.constraint.SatConstraint;
import org.btrplace.model.view.ModelView;
import org.btrplace.plan.ReconfigurationPlan;
import org.btrplace.safeplace.spec.Constraint;
import org.btrplace.safeplace.spec.term.Constant;
import org.btrplace.safeplace.testing.verification.btrplace.ScheduleConverter;

import java.io.StringReader;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Fabien Hermenier
 */
public class TestCase {

    private Instance instance;

    private ReconfigurationPlan plan;

    private Constraint cstr;

    private List args;

    private SatConstraint impl;

    private List groups;
    public TestCase(Instance i, ReconfigurationPlan plan, Constraint cstr) {
        instance = i;
        this.plan = plan;
        this.cstr = cstr;
        this.args = Collections.emptyList();
        groups = new ArrayList<>();
    }

    public Constraint constraint() {
        return cstr;
    }

    public List args() {
        return args;
    }

    public TestCase args(List args) {
        this.args = Collections.unmodifiableList(args);
        return this;
    }

    public ReconfigurationPlan plan() {
        return plan;
    }

    public Instance instance() {
        return instance;
    }

    public TestCase impl(SatConstraint s) {
        impl = s;
        return this;
    }

    public List groups() {
        return groups;
    }

    public SatConstraint impl() {
        return this.impl;
    }

    public boolean continuous() {
        return impl() == null || impl().isContinuous();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        TestCase testCase = (TestCase) o;

        return Objects.equals(instance, testCase.instance) &&
                Objects.equals(plan, testCase.plan) &&
                Objects.equals(cstr, testCase.cstr) &&
                Objects.equals(args, testCase.args) &&
                Objects.equals(impl, testCase.impl);
    }

    @Override
    public int hashCode() {
        return Objects.hash(instance, plan, cstr, args, impl, groups);
    }

    @Override
    public String toString() {
        String restriction = "CONTINUOUS ";
        if (impl() != null && !impl().isContinuous()) {
            restriction = "DISCRETE ";
        }
        String res = "Constraint: " + restriction + cstr.toString(args) + "\n"
                + instance.getModel().getMapping() + "\n"
                + instance.getModel().getViews().stream().map(ModelView::toString).collect(Collectors.joining("\n", "", "\n"));

        if (continuous() || !plan.isApplyable()) {
            return res + "Plan:\n" + plan + "\n";
        } else {
            return res + "Result:\n" + plan.getResult().getMapping() + "\n";
        }
    }

    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();
    }

    public static TestCase fromJSON(List 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 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 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;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy