Search in sources :

Example 16 with IDatabase

use of org.openlca.core.database.IDatabase in project olca-modules by GreenDelta.

the class DbLibrarySwap method techFlowsOf.

private List<TechFlow> techFlowsOf(IDatabase db) {
    var processes = new ProcessDao(db).getDescriptors().stream().collect(map());
    var flows = new FlowDao(db).getDescriptors().stream().filter(d -> d.flowType != FlowType.ELEMENTARY_FLOW).collect(map());
    var protoIndex = library.getProductIndex();
    var list = new ArrayList<TechFlow>();
    for (int i = 0; i < protoIndex.getProductCount(); i++) {
        var protoEntry = protoIndex.getProduct(i);
        var process = processes.get(protoEntry.getProcess().getId());
        var flow = flows.get(protoEntry.getProduct().getId());
        if (process != null && flow != null) {
            list.add(TechFlow.of(process, flow));
        }
    }
    return list;
}
Also used : FlowType(org.openlca.core.model.FlowType) JsonImport(org.openlca.jsonld.input.JsonImport) UpdateMode(org.openlca.jsonld.input.UpdateMode) RootEntity(org.openlca.core.model.RootEntity) ProcessDao(org.openlca.core.database.ProcessDao) ZipStore(org.openlca.jsonld.ZipStore) NativeSql(org.openlca.core.database.NativeSql) Collectors(java.util.stream.Collectors) File(java.io.File) RootDescriptor(org.openlca.core.model.descriptors.RootDescriptor) ArrayList(java.util.ArrayList) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) TechFlow(org.openlca.core.matrix.index.TechFlow) List(java.util.List) IDatabase(org.openlca.core.database.IDatabase) Map(java.util.Map) TLongHashSet(gnu.trove.set.hash.TLongHashSet) FlowDao(org.openlca.core.database.FlowDao) Collector(java.util.stream.Collector) FlowDao(org.openlca.core.database.FlowDao) ProcessDao(org.openlca.core.database.ProcessDao) ArrayList(java.util.ArrayList)

Example 17 with IDatabase

use of org.openlca.core.database.IDatabase in project olca-modules by GreenDelta.

the class Graph method build.

/**
 * Creates a graph that synchronizes the given eILCD model with the given
 * database.
 */
static Graph build(Model model, IDatabase db) {
    Graph g = new Graph();
    Logger log = LoggerFactory.getLogger(Graph.class);
    if (model == null || model.info == null || db == null) {
        log.warn("Invalid constraints; return empty index");
        return g;
    }
    Technology tech = model.info.technology;
    if (tech == null) {
        log.warn("No processes in model; return empty index");
        return g;
    }
    Map<Integer, Group> groups = tech.groups.stream().collect(Collectors.toMap(group -> group.id, group -> group));
    ProcessDao dao = new ProcessDao(db);
    for (ProcessInstance pi : tech.processes) {
        if (pi.process == null || pi.process.uuid == null) {
            log.warn("Invalid process reference node={}", pi.id);
            continue;
        }
        String refID = pi.process.uuid;
        Process process = dao.getForRefId(refID);
        if (process == null) {
            log.warn("Could not find process {}; skip node {}", refID, pi.id);
            continue;
        }
        Node n = Node.init(pi, process);
        if (!pi.groupRefs.isEmpty()) {
            GroupRef gr = pi.groupRefs.get(0);
            n.group = groups.get(gr.groupID);
        }
        g.putNode(n);
    }
    buildLinks(g, model);
    QuantitativeReference qRef = model.info.quantitativeReference;
    if (qRef != null && qRef.refProcess != null) {
        g.root = g.getNode(qRef.refProcess);
    }
    return g;
}
Also used : GroupRef(org.openlca.ilcd.models.GroupRef) Technology(org.openlca.ilcd.models.Technology) DownstreamLink(org.openlca.ilcd.models.DownstreamLink) Logger(org.slf4j.Logger) QuantitativeReference(org.openlca.ilcd.models.QuantitativeReference) Process(org.openlca.core.model.Process) ProcessDao(org.openlca.core.database.ProcessDao) LoggerFactory(org.slf4j.LoggerFactory) Connection(org.openlca.ilcd.models.Connection) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) List(java.util.List) Model(org.openlca.ilcd.models.Model) IDatabase(org.openlca.core.database.IDatabase) ProcessInstance(org.openlca.ilcd.models.ProcessInstance) Map(java.util.Map) Exchange(org.openlca.core.model.Exchange) Group(org.openlca.ilcd.models.Group) Collections(java.util.Collections) Group(org.openlca.ilcd.models.Group) Process(org.openlca.core.model.Process) Logger(org.slf4j.Logger) Technology(org.openlca.ilcd.models.Technology) ProcessDao(org.openlca.core.database.ProcessDao) ProcessInstance(org.openlca.ilcd.models.ProcessInstance) GroupRef(org.openlca.ilcd.models.GroupRef) QuantitativeReference(org.openlca.ilcd.models.QuantitativeReference)

Example 18 with IDatabase

use of org.openlca.core.database.IDatabase in project olca-app by GreenDelta.

the class ImpactPage method loadFactors.

private List<Factor> loadFactors() {
    IDatabase db = Database.get();
    String sql = "select f_impact_category, f_unit, f_location, " + "value from tbl_impact_factors where f_flow = " + getModel().id;
    EntityCache ecache = Cache.getEntityCache();
    List<Factor> factors = new ArrayList<>();
    try {
        NativeSql.on(db).query(sql, r -> {
            Factor f = new Factor();
            f.impact = ecache.get(ImpactDescriptor.class, r.getLong(1));
            if (f.impact == null)
                return true;
            Unit unit = ecache.get(Unit.class, r.getLong(2));
            if (unit != null) {
                f.unit = unit.name;
            }
            long locID = r.getLong(3);
            if (!r.wasNull()) {
                f.location = ecache.get(LocationDescriptor.class, locID);
            }
            f.value = r.getDouble(4);
            factors.add(f);
            return true;
        });
    } catch (Exception e) {
        Logger log = LoggerFactory.getLogger(getClass());
        log.error("Failed to load LCIA factors", e);
    }
    // sort and set display flags
    factors.sort((f1, f2) -> {
        int c = Strings.compare(Labels.name(f1.impact), Labels.name(f2.impact));
        if (c != 0)
            return c;
        if (f1.location == null)
            return -1;
        if (f2.location == null)
            return 1;
        return Strings.compare(f1.location.code, f2.location.code);
    });
    return factors;
}
Also used : IDatabase(org.openlca.core.database.IDatabase) EntityCache(org.openlca.core.database.EntityCache) LocationDescriptor(org.openlca.core.model.descriptors.LocationDescriptor) ArrayList(java.util.ArrayList) ImpactDescriptor(org.openlca.core.model.descriptors.ImpactDescriptor) Unit(org.openlca.core.model.Unit) Logger(org.slf4j.Logger)

Example 19 with IDatabase

use of org.openlca.core.database.IDatabase in project olca-app by GreenDelta.

the class GeoFactorCalculator method run.

@Override
public void run() {
    // check the input
    if (setup == null || impact == null) {
        log.error("no setup or LCIA category");
        return;
    }
    IDatabase db = Database.get();
    if (db == null) {
        log.error("no connected database");
        return;
    }
    if (setup.bindings.isEmpty()) {
        log.warn("no flow bindings; nothing to do");
        return;
    }
    // and finally generate the LCIA factors
    if (setup.features.isEmpty()) {
        log.error("no features available for the " + "intersection calculation");
        return;
    }
    var params = calcParamVals(setup.features);
    createFactors(params);
}
Also used : IDatabase(org.openlca.core.database.IDatabase)

Example 20 with IDatabase

use of org.openlca.core.database.IDatabase in project olca-app by GreenDelta.

the class GeoJsonImportWizard method performFinish.

@Override
public boolean performFinish() {
    File[] files = filePage.getFiles();
    if (files == null || files.length == 0)
        return false;
    IDatabase db = Database.get();
    if (db == null) {
        MsgBox.error(M.NoDatabaseOpened, M.NeedOpenDatabase);
        return true;
    }
    try {
        getContainer().run(true, false, m -> {
            m.beginTask("Import geometries from GeoJSON...", IProgressMonitor.UNKNOWN);
            for (File f : files) {
                new GeoJsonImport(f, db).run();
            }
            m.done();
        });
        Navigator.refresh(Navigator.findElement(ModelType.LOCATION));
        return true;
    } catch (Exception e) {
        ErrorReporter.on("Failed to import GeoJSON file", e);
        return true;
    }
}
Also used : IDatabase(org.openlca.core.database.IDatabase) GeoJsonImport(org.openlca.geo.GeoJsonImport) File(java.io.File)

Aggregations

IDatabase (org.openlca.core.database.IDatabase)25 ProcessDao (org.openlca.core.database.ProcessDao)12 File (java.io.File)9 Process (org.openlca.core.model.Process)9 Test (org.junit.Test)8 Category (org.openlca.core.model.Category)4 Logger (org.slf4j.Logger)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 FlowDao (org.openlca.core.database.FlowDao)3 ProcessDescriptor (org.openlca.core.model.descriptors.ProcessDescriptor)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 CategoryDao (org.openlca.core.database.CategoryDao)2 Derby (org.openlca.core.database.Derby)2 NativeSql (org.openlca.core.database.NativeSql)2 ParameterDao (org.openlca.core.database.ParameterDao)2 ProductSystemDao (org.openlca.core.database.ProductSystemDao)2 ProductSystemBuilder (org.openlca.core.matrix.ProductSystemBuilder)2