Search in sources :

Example 1 with FlowMapEntry

use of org.openlca.io.maps.FlowMapEntry in project olca-modules by GreenDelta.

the class ProcessWriter method writeElemExchanges.

private void writeElemExchanges(Process p, ElementaryFlowType type) {
    writeln(type.exchangeHeader());
    for (Exchange e : p.exchanges) {
        if (e.flow == null || e.flow.flowType != FlowType.ELEMENTARY_FLOW)
            continue;
        Compartment comp = flowCompartments.get(e.flow);
        if (comp == null || comp.type() != type)
            continue;
        FlowMapEntry mapEntry = mappedFlow(e.flow);
        if (mapEntry == null) {
            // we have an unmapped flow
            var ref = toReferenceAmount(e);
            var u = uncertainty(ref.amount, ref.uncertainty);
            writeln(e.flow.name, comp.sub().toString(), unit(ref.unit), ref.amount, u[0], u[1], u[2], u[3], e.description);
            continue;
        }
        // handle a mapped flow
        FlowRef target = mapEntry.targetFlow();
        String unit = target.unit != null ? unit(target.unit.name) : SimaProUnit.kg.symbol;
        var u = uncertainty(e.amount, e.uncertainty, mapEntry.factor());
        writeln(target.flow.name, comp.sub().toString(), unit, e.amount * mapEntry.factor(), u[0], u[1], u[2], u[3], e.description);
    }
    writeln();
}
Also used : Exchange(org.openlca.core.model.Exchange) SubCompartment(org.openlca.simapro.csv.enums.SubCompartment) FlowRef(org.openlca.io.maps.FlowRef) FlowMapEntry(org.openlca.io.maps.FlowMapEntry)

Example 2 with FlowMapEntry

use of org.openlca.io.maps.FlowMapEntry in project olca-modules by GreenDelta.

the class ProcessWriter method writeReferenceFlows.

@SuppressWarnings("unchecked")
private void writeReferenceFlows() {
    // order flows by their type
    int n = ElementaryFlowType.values().length;
    List<Flow>[] buckets = new List[n];
    for (int i = 0; i < n; i++) {
        buckets[i] = new ArrayList<>();
    }
    for (Map.Entry<Flow, Compartment> e : flowCompartments.entrySet()) {
        ElementaryFlowType type = e.getValue().type();
        buckets[type.ordinal()].add(e.getKey());
    }
    // write the flow information
    for (var type : ElementaryFlowType.values()) {
        writeln(type.blockHeader());
        // duplicate names are not allowed here
        HashSet<String> handledNames = new HashSet<>();
        for (Flow flow : buckets[type.ordinal()]) {
            String name;
            String unit = null;
            FlowMapEntry mapEntry = mappedFlow(flow);
            if (mapEntry != null) {
                // handle mapped flows
                name = mapEntry.targetFlow().flow.name;
                if (mapEntry.targetFlow().unit != null) {
                    unit = unit(mapEntry.targetFlow().unit.name);
                }
            } else {
                // handle unmapped flows
                name = flow.name;
                Unit refUnit = null;
                if (flow.referenceFlowProperty != null) {
                    if (flow.referenceFlowProperty.unitGroup != null) {
                        refUnit = flow.referenceFlowProperty.unitGroup.referenceUnit;
                    }
                }
                unit = unit(refUnit);
            }
            if (name == null || unit == null)
                continue;
            String id = name.trim().toLowerCase();
            if (handledNames.contains(id))
                continue;
            handledNames.add(id);
            writeln(name, unit, flow.casNumber, "");
        }
        writeln();
        writeln("End");
        writeln();
        writeln();
    }
}
Also used : SubCompartment(org.openlca.simapro.csv.enums.SubCompartment) FlowMapEntry(org.openlca.io.maps.FlowMapEntry) Unit(org.openlca.core.model.Unit) Flow(org.openlca.core.model.Flow) ElementaryFlowType(org.openlca.simapro.csv.enums.ElementaryFlowType) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) FlowMap(org.openlca.io.maps.FlowMap) HashSet(java.util.HashSet)

Example 3 with FlowMapEntry

use of org.openlca.io.maps.FlowMapEntry in project olca-modules by GreenDelta.

the class RefDataImport method loadElemDBFlow.

/**
 * Tries to load an elementary flow from the database, which could also be a
 * mapped flow.
 */
private Flow loadElemDBFlow(ElementaryExchange exchange) {
    String extId = exchange.flowId;
    Flow flow = flowDao.getForRefId(extId);
    if (flow != null)
        return flow;
    FlowMapEntry entry = config.getFlowMap().get(extId);
    if (entry == null)
        return null;
    flow = flowDao.getForRefId(entry.targetFlowId());
    if (flow == null)
        return null;
    index.putMappedFlow(extId, entry.factor());
    return flow;
}
Also used : FlowMapEntry(org.openlca.io.maps.FlowMapEntry) Flow(org.openlca.core.model.Flow)

Example 4 with FlowMapEntry

use of org.openlca.io.maps.FlowMapEntry in project olca-modules by GreenDelta.

the class FlowMapService method toModel.

private FlowMap toModel(ProtoFlowMap proto) {
    if (proto == null)
        return FlowMap.empty();
    var flowMap = new FlowMap();
    flowMap.name = proto.getName();
    flowMap.description = proto.getDescription();
    flowMap.refId = proto.getId();
    for (var protoEntry : proto.getMappingsList()) {
        var source = toModelRef(protoEntry.getFrom());
        var target = toModelRef(protoEntry.getTo());
        var factor = protoEntry.getConversionFactor();
        flowMap.entries.add(new FlowMapEntry(source, target, factor));
    }
    return flowMap;
}
Also used : FlowMapEntry(org.openlca.io.maps.FlowMapEntry) ProtoFlowMapEntry(org.openlca.proto.ProtoFlowMapEntry) ProtoFlowMap(org.openlca.proto.ProtoFlowMap) FlowMap(org.openlca.io.maps.FlowMap)

Example 5 with FlowMapEntry

use of org.openlca.io.maps.FlowMapEntry in project olca-app by GreenDelta.

the class MappingDialog method open.

/**
 * Opens a dialog for editing the given entry. When the returned status code
 * is `OK`, the entry was updated (maybe modified). Otherwise, it is
 * unchanged.
 */
static int open(MappingTool tool, FlowMapEntry entry) {
    if (tool == null || entry == null)
        return CANCEL;
    // the dialog works on a copy of the entry; only if
    // the user clicks on OK, the changes are applied
    var copy = entry.copy();
    var dialog = new MappingDialog(tool, entry.copy());
    int state = dialog.open();
    if (state != OK)
        return state;
    Function<FlowRef, FlowRef> check = flowRef -> {
        var r = flowRef == null ? new FlowRef() : flowRef;
        r.status = r.flow == null ? MappingStatus.error("no flow set") : MappingStatus.ok("edited or checked manually");
        return r;
    };
    entry.factor(copy.factor()).sourceFlow(check.apply(copy.sourceFlow())).targetFlow(check.apply(copy.targetFlow()));
    return state;
}
Also used : M(org.openlca.app.M) Fn(org.openlca.app.util.Fn) Labels(org.openlca.app.util.Labels) Controls(org.openlca.app.util.Controls) Hyperlink(org.eclipse.ui.forms.widgets.Hyperlink) ProcessDao(org.openlca.core.database.ProcessDao) FlowMapEntry(org.openlca.io.maps.FlowMapEntry) Function(java.util.function.Function) Point(org.eclipse.swt.graphics.Point) Location(org.openlca.core.model.Location) FlowRef(org.openlca.io.maps.FlowRef) ArrayList(java.util.ArrayList) Flow(org.openlca.core.model.Flow) IProvider(org.openlca.app.tools.mapping.model.IProvider) LocationDao(org.openlca.core.database.LocationDao) Database(org.openlca.app.db.Database) FormDialog(org.eclipse.ui.forms.FormDialog) Composite(org.eclipse.swt.widgets.Composite) FlowDao(org.openlca.core.database.FlowDao) ProcessDescriptor(org.openlca.core.model.descriptors.ProcessDescriptor) DBProvider(org.openlca.app.tools.mapping.model.DBProvider) FlowType(org.openlca.core.model.FlowType) Categories(org.openlca.util.Categories) Text(org.eclipse.swt.widgets.Text) Combo(org.eclipse.swt.widgets.Combo) Shell(org.eclipse.swt.widgets.Shell) Set(java.util.Set) FormToolkit(org.eclipse.ui.forms.widgets.FormToolkit) MsgBox(org.openlca.app.util.MsgBox) IManagedForm(org.eclipse.ui.forms.IManagedForm) Objects(java.util.Objects) Strings(org.openlca.util.Strings) UI(org.openlca.app.util.UI) IDatabase(org.openlca.core.database.IDatabase) SWT(org.eclipse.swt.SWT) Label(org.eclipse.swt.widgets.Label) MappingStatus(org.openlca.io.maps.MappingStatus) FlowRef(org.openlca.io.maps.FlowRef) Point(org.eclipse.swt.graphics.Point)

Aggregations

FlowMapEntry (org.openlca.io.maps.FlowMapEntry)13 Flow (org.openlca.core.model.Flow)6 FlowRef (org.openlca.io.maps.FlowRef)6 ArrayList (java.util.ArrayList)3 DBProvider (org.openlca.app.tools.mapping.model.DBProvider)3 SubCompartment (org.openlca.simapro.csv.enums.SubCompartment)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Set (java.util.Set)2 Database (org.openlca.app.db.Database)2 Labels (org.openlca.app.util.Labels)2 FlowDao (org.openlca.core.database.FlowDao)2 IDatabase (org.openlca.core.database.IDatabase)2 Exchange (org.openlca.core.model.Exchange)2 FlowMap (org.openlca.io.maps.FlowMap)2 SQLException (java.sql.SQLException)1 Map (java.util.Map)1 Objects (java.util.Objects)1 ExecutorService (java.util.concurrent.ExecutorService)1