Search in sources :

Example 1 with ParameterRedefSet

use of org.openlca.core.model.ParameterRedefSet in project olca-modules by GreenDelta.

the class ProductSystemDaoTest method testParameterRedefSets.

@Test
public void testParameterRedefSets() {
    ProductSystemDao dao = new ProductSystemDao(Tests.getDb());
    ProductSystem sys = new ProductSystem();
    ParameterRedefSet s1 = new ParameterRedefSet();
    sys.parameterSets.add(s1);
    s1.name = "Baseline";
    s1.isBaseline = true;
    ParameterRedef p1 = new ParameterRedef();
    p1.name = "p";
    p1.description = "just for testing";
    p1.value = 42.0;
    s1.parameters.add(p1);
    ParameterRedefSet s2 = new ParameterRedefSet();
    sys.parameterSets.add(s2);
    s2.name = "Something else";
    s2.isBaseline = false;
    s2.parameters.add(p1.copy());
    dao.insert(sys);
    sys = dao.getForId(sys.id);
    Assert.assertEquals(2, sys.parameterSets.size());
    for (ParameterRedefSet s : sys.parameterSets) {
        if (s.isBaseline) {
            Assert.assertEquals("Baseline", s.name);
        } else {
            Assert.assertEquals("Something else", s.name);
        }
        Assert.assertEquals("p", s.parameters.get(0).name);
    }
    dao.delete(sys);
}
Also used : ProductSystem(org.openlca.core.model.ProductSystem) ParameterRedef(org.openlca.core.model.ParameterRedef) ParameterRedefSet(org.openlca.core.model.ParameterRedefSet) Test(org.junit.Test)

Example 2 with ParameterRedefSet

use of org.openlca.core.model.ParameterRedefSet in project olca-modules by GreenDelta.

the class ParametersTest method testFindRedefOwners.

@Test
public void testFindRedefOwners() {
    // create a process with a local parameter
    var process = TestProcess.refProduct("prod", 1, "kg").param("param", 42).get();
    var param = process.parameters.get(0);
    // create project and product system
    var project = new Project();
    project.variants.add(new ProjectVariant());
    db.insert(project);
    var system = new ProductSystem();
    system.parameterSets.add(new ParameterRedefSet());
    db.insert(system);
    // should not find something
    var owners = Parameters.findRedefOwners(param, process, db);
    assertTrue(owners.isEmpty());
    // add parameter redefs
    var redef = new ParameterRedef();
    redef.name = param.name;
    redef.contextId = process.id;
    redef.contextType = ModelType.PROCESS;
    redef.value = 24;
    project.variants.get(0).parameterRedefs.add(redef.copy());
    project = db.update(project);
    system.parameterSets.get(0).parameters.add(redef.copy());
    system = db.update(system);
    // should find something
    owners = Parameters.findRedefOwners(param, process, db);
    assertEquals(2, owners.size());
    assertTrue(owners.contains(Descriptor.of(system)));
    assertTrue(owners.contains(Descriptor.of(project)));
    db.clear();
}
Also used : Project(org.openlca.core.model.Project) ProjectVariant(org.openlca.core.model.ProjectVariant) ProductSystem(org.openlca.core.model.ProductSystem) ParameterRedef(org.openlca.core.model.ParameterRedef) ParameterRedefSet(org.openlca.core.model.ParameterRedefSet) Test(org.junit.Test)

Example 3 with ParameterRedefSet

use of org.openlca.core.model.ParameterRedefSet in project olca-app by GreenDelta.

the class ParameterPage2 method createFormContent.

@Override
protected void createFormContent(IManagedForm mform) {
    form = UI.formHeader(this);
    tk = mform.getToolkit();
    body = UI.formBody(form, tk);
    // create the baseline scenario if necessary
    ParameterRedefSet base = getModel().parameterSets.stream().filter(s -> s.isBaseline).findFirst().orElse(null);
    if (base == null) {
        base = new ParameterRedefSet();
        base.name = "Baseline";
        base.isBaseline = true;
        getModel().parameterSets.add(base);
    }
    // sort and render the parameter sets
    List<ParameterRedefSet> sets = paramSets();
    sets.sort((s1, s2) -> {
        if (s1.isBaseline)
            return -1;
        if (s2.isBaseline)
            return 1;
        return Strings.compare(s1.name, s2.name);
    });
    for (ParameterRedefSet s : paramSets()) {
        sections.add(new ParameterSection(s));
    }
    // render the add button at the end of the list
    addButton = new AddButton();
    form.reflow(true);
}
Also used : ParameterRedefSet(org.openlca.core.model.ParameterRedefSet)

Example 4 with ParameterRedefSet

use of org.openlca.core.model.ParameterRedefSet in project olca-modules by GreenDelta.

the class ProductSystemImport method addParameterSets.

private void addParameterSets(JsonObject json, ProductSystem sys) {
    var array = Json.getArray(json, "parameterSets");
    if (array == null || array.size() == 0)
        return;
    for (JsonElement elem : array) {
        if (!elem.isJsonObject())
            continue;
        var obj = elem.getAsJsonObject();
        var set = new ParameterRedefSet();
        sys.parameterSets.add(set);
        set.name = Json.getString(obj, "name");
        set.description = Json.getString(obj, "description");
        set.isBaseline = Json.getBool(obj, "isBaseline", false);
        JsonArray redefs = Json.getArray(obj, "parameters");
        if (redefs != null && redefs.size() > 0) {
            set.parameters.addAll(ParameterRedefs.read(redefs, conf));
        }
    }
}
Also used : JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) ParameterRedefSet(org.openlca.core.model.ParameterRedefSet)

Example 5 with ParameterRedefSet

use of org.openlca.core.model.ParameterRedefSet in project olca-modules by GreenDelta.

the class ParameterRedefTest method testInParameterRedefSet.

@Test
public void testInParameterRedefSet() {
    // create the model
    ProductSystemDao dao = new ProductSystemDao(db);
    ProductSystem sys = new ProductSystem();
    sys.refId = UUID.randomUUID().toString();
    ParameterRedefSet paramSet = new ParameterRedefSet();
    sys.parameterSets.add(paramSet);
    paramSet.isBaseline = true;
    paramSet.name = "Baseline";
    paramSet.parameters.add(redef);
    dao.insert(sys);
    // write and clear DB
    with(zip -> new JsonExport(Tests.getDb(), zip).write(sys));
    db.clear();
    Assert.assertNull(dao.getForRefId(sys.refId));
    Assert.assertNull(paramDao.getForRefId(globalParam.refId));
    // import and check
    with(zip -> new JsonImport(zip, db).run());
    ProductSystem sys2 = dao.getForRefId(sys.refId);
    Assert.assertEquals("R", sys2.parameterSets.get(0).parameters.get(0).name);
    Parameter p = paramDao.getForRefId(globalParam.refId);
    Assert.assertEquals("R", p.name);
}
Also used : JsonImport(org.openlca.jsonld.input.JsonImport) ProductSystem(org.openlca.core.model.ProductSystem) Parameter(org.openlca.core.model.Parameter) JsonExport(org.openlca.jsonld.output.JsonExport) ProductSystemDao(org.openlca.core.database.ProductSystemDao) ParameterRedefSet(org.openlca.core.model.ParameterRedefSet) AbstractZipTest(org.openlca.jsonld.AbstractZipTest) Test(org.junit.Test)

Aggregations

ParameterRedefSet (org.openlca.core.model.ParameterRedefSet)5 Test (org.junit.Test)3 ProductSystem (org.openlca.core.model.ProductSystem)3 ParameterRedef (org.openlca.core.model.ParameterRedef)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 ProductSystemDao (org.openlca.core.database.ProductSystemDao)1 Parameter (org.openlca.core.model.Parameter)1 Project (org.openlca.core.model.Project)1 ProjectVariant (org.openlca.core.model.ProjectVariant)1 AbstractZipTest (org.openlca.jsonld.AbstractZipTest)1 JsonImport (org.openlca.jsonld.input.JsonImport)1 JsonExport (org.openlca.jsonld.output.JsonExport)1