use of org.openlca.core.results.Contribution in project olca-modules by GreenDelta.
the class ImpactHandler method getLocationContributions.
@Rpc("get/impacts/contributions/locations")
public RpcResponse getLocationContributions(RpcRequest req) {
return utils.contributionImpact(req, (result, impact, cache) -> {
LocationResult r = new LocationResult(result, cache.db);
List<Contribution<LocationDescriptor>> contributions = utils.toDescriptors(r.getContributions(impact));
contributions = utils.filter(contributions, contribution -> contribution.amount != 0);
return JsonRpc.encode(contributions, cache, json -> json.addProperty("unit", impact.referenceUnit));
});
}
use of org.openlca.core.results.Contribution in project olca-app by GreenDelta.
the class LocationPage method createTree.
private void createTree(Composite body, FormToolkit tk) {
Section section = UI.section(body, tk, M.ContributionTreeLocations);
UI.gridData(section, true, true);
Composite comp = UI.sectionClient(section, tk);
UI.gridLayout(comp, 1);
label = new TreeLabel();
String[] labels = { M.Location, M.Amount, M.Unit };
tree = Trees.createViewer(comp, labels, label);
tree.setContentProvider(new TreeContentProvider(this));
Trees.bindColumnWidths(tree.getTree(), 0.4, 0.3, 0.3);
// tree actions
Action onOpen = Actions.onOpen(() -> {
Object obj = Viewers.getFirstSelected(tree);
if (obj == null)
return;
if (obj instanceof Contribution) {
Contribution<?> c = (Contribution<?>) obj;
if (c.item instanceof RootDescriptor) {
App.open((RootDescriptor) c.item);
} else if (c.item instanceof RootEntity) {
App.open((RootEntity) c.item);
}
}
});
Actions.bind(tree, onOpen, TreeClipboard.onCopy(tree));
Trees.onDoubleClick(tree, e -> onOpen.run());
tree.getTree().getColumns()[1].setAlignment(SWT.RIGHT);
}
use of org.openlca.core.results.Contribution in project olca-app by GreenDelta.
the class ResultMap method update.
void update(Object selection, List<Contribution<Location>> contributions) {
if (map == null)
return;
if (layer != null) {
map.removeLayer(layer);
}
if (contributions == null || contributions.isEmpty()) {
coll = null;
map.update();
return;
}
coll = new FeatureCollection();
List<Pair<Location, Feature>> pairs = new ArrayList<>();
for (Contribution<Location> c : contributions) {
Location loc = c.item;
if (loc == null || loc.geodata == null)
continue;
FeatureCollection fc = GeoJSON.unpack(loc.geodata);
if (fc == null || fc.features.isEmpty())
continue;
Geometry g = fc.features.get(0).geometry;
if (g == null)
continue;
Feature feature = new Feature();
feature.geometry = g;
feature.properties = new HashMap<>();
feature.properties.put("result", c.amount);
addMetaData(loc, feature, selection);
pairs.add(Pair.of(loc, feature));
}
if (pairs.isEmpty())
return;
pairs.stream().sorted((p1, p2) -> {
return Double.compare(bsize(p2), bsize(p1));
}).forEach(p -> coll.features.add(p.second));
layer = map.addLayer(coll).fillScale("result").center();
map.update();
}
use of org.openlca.core.results.Contribution in project olca-app by GreenDelta.
the class TreeContentProvider method getChildren.
@Override
public Object[] getChildren(Object obj) {
if (!(obj instanceof Contribution))
return null;
Contribution<?> c = (Contribution<?>) obj;
// calculated contributions are cached
if (c.childs != null && !c.childs.isEmpty())
return c.childs.toArray();
// leaf of the contribution tree
if (!(c.item instanceof Location))
return null;
Location loc = (Location) c.item;
Object selection = page.getSelection();
Stream<Contribution<?>> stream = null;
if (selection instanceof FlowDescriptor) {
stream = contributions(loc, (FlowDescriptor) selection);
} else if (selection instanceof CostResultDescriptor) {
stream = contributions(loc, (CostResultDescriptor) selection);
} else if (selection instanceof ImpactDescriptor) {
stream = contributions(loc, (ImpactDescriptor) selection);
}
if (stream == null)
return null;
// TODO apply cutoff
c.childs = stream.filter(con -> con.amount != 0).sorted((c1, c2) -> Double.compare(c2.amount, c1.amount)).collect(Collectors.toList());
return c.childs.toArray();
}
use of org.openlca.core.results.Contribution in project olca-app by GreenDelta.
the class ImpactPage method createFormContent.
@Override
protected void createFormContent(IManagedForm mform) {
ScrolledForm form = UI.formHeader(this);
FormToolkit tk = mform.getToolkit();
Composite body = UI.formBody(form, tk);
Composite comp = tk.createComposite(body);
UI.gridLayout(comp, 4);
UI.formLabel(comp, tk, M.ImpactAssessmentMethod);
combo = new ImpactMethodViewer(comp);
List<ImpactMethodDescriptor> list = new ImpactMethodDao(Database.get()).getDescriptors().stream().sorted((m1, m2) -> Strings.compare(m1.name, m2.name)).collect(Collectors.toList());
combo.setInput(list);
combo.addSelectionChangedListener(this::setTreeInput);
zeroCheck = tk.createButton(comp, M.ExcludeZeroValues, SWT.CHECK);
zeroCheck.setSelection(true);
Controls.onSelect(zeroCheck, e -> setTreeInput(combo.getSelected()));
Button reload = tk.createButton(comp, M.Reload, SWT.NONE);
reload.setImage(Icon.REFRESH.get());
Controls.onSelect(reload, _e -> {
result = null;
setTreeInput(combo.getSelected());
});
tree = Trees.createViewer(body, M.Name, M.Category, M.Amount, M.Result);
UI.gridData(tree.getControl(), true, true);
tree.setContentProvider(new Content());
tree.setLabelProvider(new Label());
Trees.bindColumnWidths(tree.getTree(), 0.35, 0.35, 0.15, 0.15);
tree.getTree().getColumns()[2].setAlignment(SWT.RIGHT);
tree.getTree().getColumns()[3].setAlignment(SWT.RIGHT);
Action onOpen = Actions.onOpen(() -> {
Contribution<?> c = Viewers.getFirstSelected(tree);
if (c == null)
return;
if (c.item instanceof EnviFlow) {
App.open(((EnviFlow) c.item).flow());
}
if (c.item instanceof ImpactDescriptor) {
App.open((ImpactDescriptor) c.item);
}
});
Actions.bind(tree, onOpen);
Trees.onDoubleClick(tree, e -> onOpen.run());
if (!list.isEmpty()) {
ImpactMethodDescriptor m = list.get(0);
combo.select(m);
setTreeInput(m);
}
form.reflow(true);
}
Aggregations