Search in sources :

Example 1 with Database

use of org.openlca.app.db.Database in project olca-app by GreenDelta.

the class Replacer method run.

@Override
public void run() {
    if (conf == null || (conf.models.isEmpty())) {
        log.info("no configuration; nothing to replace");
        return;
    }
    // collect the IDs of processes and LCIA categories
    // where flows should be replaced
    processes.clear();
    impacts.clear();
    for (var model : conf.models) {
        if (model.type == ModelType.PROCESS) {
            processes.add(model.id);
        } else if (model.type == ModelType.IMPACT_METHOD) {
            ImpactMethodDao dao = new ImpactMethodDao(db);
            dao.getCategoryDescriptors(model.id).forEach(d -> impacts.add(d.id));
        }
    }
    buildIndices();
    if (entries.isEmpty()) {
        log.info("found no flows that can be mapped");
        return;
    }
    log.info("found {} flows that can be mapped", entries.size());
    try {
        // start and wait for the cursors to finish
        log.info("start updatable cursors");
        List<UpdatableCursor> cursors = createCursors();
        ExecutorService pool = Executors.newFixedThreadPool(4);
        for (UpdatableCursor c : cursors) {
            pool.execute(c);
        }
        pool.shutdown();
        int i = 0;
        while (!pool.awaitTermination(10, TimeUnit.SECONDS)) {
            i++;
            log.info("waiting for cursors to finish; {} seconds", i * 10);
        }
        log.info("cursors finished");
        db.getEntityFactory().getCache().evictAll();
        // TODO when products were replaced we also need to check
        // whether these products are used in the quant. ref. of
        // product systems and project variants and convert the
        // amounts there.
        // TODO also: we need to replace such flows in allocation
        // factors; the application of the conversion factor is
        // not required there.
        // collect and log statistics
        Stats stats = new Stats();
        for (UpdatableCursor c : cursors) {
            stats.add(c.stats);
            c.stats.log(c.getClass().getName(), flows);
        }
        // TODO: update the version and last-update fields
        // of the changed models; also call the indexer
        // when the database is a connected repository
        boolean deleteMapped = false;
        Set<Long> usedFlows = null;
        if (conf.deleteMapped) {
            if (stats.failures > 0) {
                log.warn("Will not delete mapped flows because" + " there were {} failures in replacement process", stats.failures);
            } else {
                deleteMapped = true;
                usedFlows = new FlowDao(db).getUsed();
            }
        }
        // update the mapping entries
        for (Long flowID : entries.keySet()) {
            FlowMapEntry e = entries.get(flowID);
            if (flowID == null || e == null)
                continue;
            if (stats.hadFailures(flowID)) {
                e.sourceFlow().status = MappingStatus.error("Replacement error");
                continue;
            }
            if (deleteMapped && !usedFlows.contains(flowID)) {
                FlowDao dao = new FlowDao(db);
                Flow flow = dao.getForId(flowID);
                dao.delete(flow);
                log.info("removed mapped flow {} uuid={}", Labels.name(flow), flow.refId);
                e.sourceFlow().status = MappingStatus.ok("Applied and removed");
            } else {
                e.sourceFlow().status = MappingStatus.ok("Applied (not removed)");
            }
        }
    } catch (Exception e) {
        log.error("Flow replacement failed", e);
    }
}
Also used : Labels(org.openlca.app.util.Labels) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) HashMap(java.util.HashMap) ImpactMethodDao(org.openlca.core.database.ImpactMethodDao) FlowMapEntry(org.openlca.io.maps.FlowMapEntry) Collectors(java.util.stream.Collectors) ModelType(org.openlca.core.model.ModelType) Executors(java.util.concurrent.Executors) FlowRef(org.openlca.io.maps.FlowRef) ArrayList(java.util.ArrayList) Flow(org.openlca.core.model.Flow) HashSet(java.util.HashSet) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Database(org.openlca.app.db.Database) IDatabase(org.openlca.core.database.IDatabase) FlowDao(org.openlca.core.database.FlowDao) DBProvider(org.openlca.app.tools.mapping.model.DBProvider) MappingStatus(org.openlca.io.maps.MappingStatus) ExecutorService(java.util.concurrent.ExecutorService) FlowMapEntry(org.openlca.io.maps.FlowMapEntry) Flow(org.openlca.core.model.Flow) FlowDao(org.openlca.core.database.FlowDao) ImpactMethodDao(org.openlca.core.database.ImpactMethodDao) ExecutorService(java.util.concurrent.ExecutorService)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 Database (org.openlca.app.db.Database)1 DBProvider (org.openlca.app.tools.mapping.model.DBProvider)1 Labels (org.openlca.app.util.Labels)1 FlowDao (org.openlca.core.database.FlowDao)1 IDatabase (org.openlca.core.database.IDatabase)1 ImpactMethodDao (org.openlca.core.database.ImpactMethodDao)1 Flow (org.openlca.core.model.Flow)1 ModelType (org.openlca.core.model.ModelType)1 FlowMapEntry (org.openlca.io.maps.FlowMapEntry)1 FlowRef (org.openlca.io.maps.FlowRef)1 MappingStatus (org.openlca.io.maps.MappingStatus)1