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();
}
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);
}
});
}
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;
}
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);
}
}
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);
}
Aggregations