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