Search in sources :

Example 1 with FlowRef

use of org.openlca.io.maps.FlowRef 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 FlowRef

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

the class FlowRefDialog method createFormContent.

@Override
protected void createFormContent(IManagedForm mform) {
    var tk = mform.getToolkit();
    var body = UI.formBody(mform.getForm(), tk);
    UI.gridLayout(body, 1, 10, 10);
    var filterComp = tk.createComposite(body);
    UI.gridLayout(filterComp, 2, 10, 0);
    UI.gridData(filterComp, true, false);
    var filterLabel = UI.formLabel(filterComp, tk, M.Filter);
    filterLabel.setFont(UI.boldFont());
    var filterText = UI.formText(filterComp, SWT.SEARCH);
    UI.gridData(filterText, true, false);
    var viewer = new TreeViewer(body, SWT.BORDER | SWT.SINGLE | SWT.VIRTUAL);
    UI.gridData(viewer.getControl(), true, true);
    viewer.setContentProvider(tree);
    viewer.setLabelProvider(tree);
    viewer.setFilters(new Filter(filterText, viewer));
    viewer.setInput(tree);
    viewer.addSelectionChangedListener(e -> {
        Object obj = Selections.firstOf(e);
        selected = obj instanceof FlowRef ? (FlowRef) obj : null;
        Button ok = getButton(IDialogConstants.OK_ID);
        ok.setEnabled(selected != null);
    });
    // handle double clicks
    viewer.addDoubleClickListener(e -> {
        IStructuredSelection s = viewer.getStructuredSelection();
        if (s == null || s.isEmpty())
            return;
        Object elem = s.getFirstElement();
        // double click on a flow
        if (elem instanceof FlowRef) {
            selected = (FlowRef) elem;
            okPressed();
            return;
        }
        // double click on a category
        if (!tree.hasChildren(elem))
            return;
        selected = null;
        getButton(IDialogConstants.OK_ID).setEnabled(false);
        if (viewer.getExpandedState(elem)) {
            viewer.collapseToLevel(elem, AbstractTreeViewer.ALL_LEVELS);
        } else {
            viewer.expandToLevel(elem, 1);
        }
    });
}
Also used : ViewerFilter(org.eclipse.jface.viewers.ViewerFilter) Button(org.eclipse.swt.widgets.Button) AbstractTreeViewer(org.eclipse.jface.viewers.AbstractTreeViewer) TreeViewer(org.eclipse.jface.viewers.TreeViewer) FlowRef(org.openlca.io.maps.FlowRef) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection)

Example 3 with FlowRef

use of org.openlca.io.maps.FlowRef 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)

Example 4 with FlowRef

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

the class Generator method run.

@Override
public void run() {
    try {
        log.info("generate mappings {} -> {}", sourceSystem, targetSystem);
        log.info("load source flows");
        // we only generate mappings for flows that are not already mapped
        var sourceFlows = getCandidateFlows();
        if (sourceFlows.isEmpty()) {
            log.info("found no unmapped source flows");
            return;
        }
        log.info("match unmapped flows");
        var matcher = new Matcher(targetSystem);
        for (var sourceFlow : sourceFlows) {
            var source = sourceFlow.copy();
            source.status = MappingStatus.ok();
            FlowRef matched = matcher.find(source);
            FlowRef target = null;
            if (matched != null) {
                target = matched.copy();
                target.status = getStatus(source, target);
            }
            mapping.entries.add(new FlowMapEntry(source, target, 1.0));
        }
    } catch (Exception e) {
        log.error("Generation of flow mappings failed", e);
    }
}
Also used : FlowRef(org.openlca.io.maps.FlowRef) FlowMapEntry(org.openlca.io.maps.FlowMapEntry)

Example 5 with FlowRef

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

the class Replacer method buildIndices.

private void buildIndices() {
    // first persist all target flows in the database that
    // do not have an error flag
    List<FlowRef> targetFlows = conf.mapping.entries.stream().filter(e -> e.targetFlow() != null && e.targetFlow().status != null && !e.targetFlow().status.isError()).map(FlowMapEntry::targetFlow).collect(Collectors.toList());
    conf.provider.persist(targetFlows, db);
    DBProvider dbProvider = new DBProvider(db);
    for (FlowMapEntry e : conf.mapping.entries) {
        // (both flows should have no error flag)
        if (e.sourceFlow() == null || e.sourceFlow().status == null || e.sourceFlow().status.isError() || e.targetFlow() == null || e.targetFlow().status == null || e.targetFlow().status.isError())
            continue;
        // sync the flows
        Flow source = dbProvider.sync(e.sourceFlow());
        if (source == null)
            continue;
        Flow target = dbProvider.sync(e.targetFlow());
        if (target == null)
            continue;
        entries.put(source.id, e);
        flows.put(source.id, source);
        flows.put(target.id, target);
    }
    factors = new FactorProvider(db, flows);
}
Also used : FlowRef(org.openlca.io.maps.FlowRef) FlowMapEntry(org.openlca.io.maps.FlowMapEntry) DBProvider(org.openlca.app.tools.mapping.model.DBProvider) Flow(org.openlca.core.model.Flow)

Aggregations

FlowRef (org.openlca.io.maps.FlowRef)11 FlowMapEntry (org.openlca.io.maps.FlowMapEntry)5 Flow (org.openlca.core.model.Flow)4 FlowType (org.openlca.core.model.FlowType)4 ArrayList (java.util.ArrayList)3 FlowDao (org.openlca.core.database.FlowDao)3 Map (java.util.Map)2 Objects (java.util.Objects)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 DBProvider (org.openlca.app.tools.mapping.model.DBProvider)2 Fn (org.openlca.app.util.Fn)2 IDatabase (org.openlca.core.database.IDatabase)2 LocationDao (org.openlca.core.database.LocationDao)2 ProcessDao (org.openlca.core.database.ProcessDao)2 Descriptor (org.openlca.core.model.descriptors.Descriptor)2 MappingStatus (org.openlca.io.maps.MappingStatus)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1