Search in sources :

Example 6 with ImpactCategoryDao

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);
}
Also used : ImpactFactor(org.openlca.core.model.ImpactFactor) FlowDao(org.openlca.core.database.FlowDao) ImpactCategory(org.openlca.core.model.ImpactCategory) ImpactCategoryDao(org.openlca.core.database.ImpactCategoryDao) Flow(org.openlca.core.model.Flow) Test(org.junit.Test)

Example 7 with ImpactCategoryDao

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);
}
Also used : MatrixConfig(org.openlca.core.matrix.MatrixConfig) MatrixExport(org.openlca.core.matrix.io.MatrixExport) Logger(org.slf4j.Logger) NativeLib(org.openlca.nativelib.NativeLib) LoggerFactory(org.slf4j.LoggerFactory) File(java.io.File) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) ImpactCategoryDao(org.openlca.core.database.ImpactCategoryDao) NativeSolver(org.openlca.core.matrix.solvers.NativeSolver) Strings(org.openlca.util.Strings) TechIndex(org.openlca.core.matrix.index.TechIndex) IDatabase(org.openlca.core.database.IDatabase) EnviIndex(org.openlca.core.matrix.index.EnviIndex) AllocationMethod(org.openlca.core.model.AllocationMethod) ImpactIndex(org.openlca.core.matrix.index.ImpactIndex) MatrixData(org.openlca.core.matrix.MatrixData) ExecutorService(java.util.concurrent.ExecutorService) ImpactBuilder(org.openlca.core.matrix.ImpactBuilder) MatrixData(org.openlca.core.matrix.MatrixData) ImpactCategoryDao(org.openlca.core.database.ImpactCategoryDao)

Example 8 with ImpactCategoryDao

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);
}
Also used : ProtoImpactCategory(org.openlca.proto.ProtoImpactCategory) ImpactCategory(org.openlca.core.model.ImpactCategory) ImpactCategoryDao(org.openlca.core.database.ImpactCategoryDao)

Example 9 with ImpactCategoryDao

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);
}
Also used : ParameterDao(org.openlca.core.database.ParameterDao) ProcessDao(org.openlca.core.database.ProcessDao) TLongLongHashMap(gnu.trove.map.hash.TLongLongHashMap) ImpactCategoryDao(org.openlca.core.database.ImpactCategoryDao)

Aggregations

ImpactCategoryDao (org.openlca.core.database.ImpactCategoryDao)9 ImpactCategory (org.openlca.core.model.ImpactCategory)5 Test (org.junit.Test)4 ProcessDao (org.openlca.core.database.ProcessDao)3 FlowDao (org.openlca.core.database.FlowDao)2 ImpactMethodDao (org.openlca.core.database.ImpactMethodDao)2 ImpactIndex (org.openlca.core.matrix.index.ImpactIndex)2 Flow (org.openlca.core.model.Flow)2 ImpactFactor (org.openlca.core.model.ImpactFactor)2 TLongLongHashMap (gnu.trove.map.hash.TLongLongHashMap)1 File (java.io.File)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 TimeUnit (java.util.concurrent.TimeUnit)1 ActorDao (org.openlca.core.database.ActorDao)1 CategoryDao (org.openlca.core.database.CategoryDao)1 CurrencyDao (org.openlca.core.database.CurrencyDao)1 DQSystemDao (org.openlca.core.database.DQSystemDao)1 FlowPropertyDao (org.openlca.core.database.FlowPropertyDao)1 IDatabase (org.openlca.core.database.IDatabase)1