use of org.openlca.core.database.ImpactCategoryDao in project olca-modules by GreenDelta.
the class ParameterUsageTreeTest method testImpactFactor.
@Test
public void testImpactFactor() {
var global = db.insert(Parameter.global("param", 42));
var flow = new Flow();
flow.name = "CH4";
new FlowDao(db).insert(flow);
var impact = new ImpactCategory();
impact.name = "GWP";
var factor = new ImpactFactor();
factor.flow = flow;
factor.value = 24.0;
factor.formula = "2 * param";
impact.impactFactors.add(factor);
new ImpactCategoryDao(db).insert(impact);
// find the factor node
var tree = ParameterUsageTree.of("param", db);
var node = find(tree, "GWP", "CH4");
assertNotNull(node);
assertEquals(Descriptor.of(flow), node.model);
assertEquals(UsageType.FORMULA, node.usageType);
assertEquals("2 * param", node.usage);
// find the global parameter definition
node = find(tree, "param");
assertNotNull(node);
assertEquals(Descriptor.of(global), node.model);
assertEquals(UsageType.DEFINITION, node.usageType);
}
use of org.openlca.core.database.ImpactCategoryDao in project olca-modules by GreenDelta.
the class LibraryExport method buildMatrices.
private void buildMatrices() {
if (data != null)
return;
// build inventory if possible
// This makes no sense if the processes in the database are linked to
// another library, but this should be caught before running an export.
// Calculation dependencies between libraries are not allowed.
var techIdx = TechIndex.of(db);
if (!techIdx.isEmpty()) {
data = MatrixConfig.of(db, techIdx).withUncertainties(withUncertainties).withRegionalization(info.isRegionalized()).withAllocation(allocation).build();
}
// here we filter out LCIA categories from other libraries; this is fine
var impacts = new ImpactCategoryDao(db).getDescriptors().stream().filter(d -> Strings.nullOrEmpty(d.library)).toList();
if (impacts.isEmpty())
return;
var impactIdx = ImpactIndex.of(impacts);
if (data == null) {
data = new MatrixData();
}
data.impactIndex = impactIdx;
if (data.enviIndex == null) {
data.enviIndex = info.isRegionalized() ? EnviIndex.createRegionalized(db, impactIdx) : EnviIndex.create(db, impactIdx);
}
ImpactBuilder.of(db, data.enviIndex).withUncertainties(withUncertainties).build().addTo(data);
}
use of org.openlca.core.database.ImpactCategoryDao in project olca-modules by GreenDelta.
the class ImpactCategoryImport method of.
@Override
public ImportStatus<ImpactCategory> of(String id) {
var impact = imp.get(ImpactCategory.class, id);
// check if we are in update mode
var update = false;
if (impact != null) {
update = imp.shouldUpdate(impact);
if (!update) {
return ImportStatus.skipped(impact);
}
}
// resolve the proto object
var proto = imp.reader.getImpactCategory(id);
if (proto == null)
return impact != null ? ImportStatus.skipped(impact) : ImportStatus.error("Could not resolve ImpactCategory " + id);
var wrap = ProtoWrap.of(proto);
if (update) {
if (imp.skipUpdate(impact, wrap))
return ImportStatus.skipped(impact);
}
// map the data
if (impact == null) {
impact = new ImpactCategory();
impact.refId = id;
}
wrap.mapTo(impact, imp);
map(proto, impact);
// insert or update it
var dao = new ImpactCategoryDao(imp.db);
impact = update ? dao.update(impact) : dao.insert(impact);
imp.putHandled(impact);
return update ? ImportStatus.updated(impact) : ImportStatus.created(impact);
}
use of org.openlca.core.database.ImpactCategoryDao in project olca-app by GreenDelta.
the class Param method fetchAll.
static void fetchAll(IDatabase db, List<Param> params) {
if (db == null || params == null)
return;
// collect the owner relations
var processes = new ProcessDao(db).descriptorMap();
var impacts = new ImpactCategoryDao(db).descriptorMap();
var owners = new TLongLongHashMap();
try {
var sql = "select id, f_owner from tbl_parameters";
NativeSql.on(db).query(sql, r -> {
var ownerId = r.getLong(2);
if (r.wasNull() || ownerId == 0)
return true;
owners.put(r.getLong(1), ownerId);
return true;
});
} catch (Exception e) {
var log = LoggerFactory.getLogger(Param.class);
log.error("Failed to query parameter onwers", e);
}
new ParameterDao(db).getAll().forEach(p -> {
var ownerId = owners.get(p.id);
// global parameters
if (p.scope == ParameterScope.GLOBAL || ownerId == 0) {
params.add(new Param(p));
return;
}
// local parameters
var owner = p.scope == ParameterScope.IMPACT ? impacts.get(ownerId) : processes.get(ownerId);
if (owner == null) {
var log = LoggerFactory.getLogger(Param.class);
log.error("invalid owner in parameter {}", p);
return;
}
params.add(new Param(p, owner));
});
Collections.sort(params);
}
Aggregations