Search in sources :

Example 1 with Contribution

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));
    });
}
Also used : UpstreamNode(org.openlca.core.results.UpstreamNode) RpcRequest(org.openlca.ipc.RpcRequest) ImpactValue(org.openlca.core.results.ImpactValue) UpstreamTree(org.openlca.core.results.UpstreamTree) LocationDescriptor(org.openlca.core.model.descriptors.LocationDescriptor) Rpc(org.openlca.ipc.Rpc) HashMap(java.util.HashMap) LocationResult(org.openlca.core.results.LocationResult) RpcResponse(org.openlca.ipc.RpcResponse) RootDescriptor(org.openlca.core.model.descriptors.RootDescriptor) ArrayList(java.util.ArrayList) StringPair(org.openlca.ipc.handlers.Upstream.StringPair) List(java.util.List) JsonArray(com.google.gson.JsonArray) EnviFlow(org.openlca.core.matrix.index.EnviFlow) ContributionResult(org.openlca.core.results.ContributionResult) Contribution(org.openlca.core.results.Contribution) Map(java.util.Map) FlowDescriptor(org.openlca.core.model.descriptors.FlowDescriptor) ImpactDescriptor(org.openlca.core.model.descriptors.ImpactDescriptor) ProcessDescriptor(org.openlca.core.model.descriptors.ProcessDescriptor) Contribution(org.openlca.core.results.Contribution) LocationResult(org.openlca.core.results.LocationResult) Rpc(org.openlca.ipc.Rpc)

Example 2 with Contribution

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);
}
Also used : Action(org.eclipse.jface.action.Action) Composite(org.eclipse.swt.widgets.Composite) RootDescriptor(org.openlca.core.model.descriptors.RootDescriptor) Section(org.eclipse.ui.forms.widgets.Section) Contribution(org.openlca.core.results.Contribution) RootEntity(org.openlca.core.model.RootEntity)

Example 3 with Contribution

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();
}
Also used : Geometry(org.openlca.geo.geojson.Geometry) LayerConfig(org.openlca.app.components.mapview.LayerConfig) M(org.openlca.app.M) MapView(org.openlca.app.components.mapview.MapView) Labels(org.openlca.app.util.Labels) Popup(org.openlca.app.util.Popup) HashMap(java.util.HashMap) Location(org.openlca.core.model.Location) ArrayList(java.util.ArrayList) Bounds(org.openlca.geo.calc.Bounds) Actions(org.openlca.app.util.Actions) Composite(org.eclipse.swt.widgets.Composite) ErrorReporter(org.openlca.app.util.ErrorReporter) Map(java.util.Map) Icon(org.openlca.app.rcp.images.Icon) Feature(org.openlca.geo.geojson.Feature) FlowDescriptor(org.openlca.core.model.descriptors.FlowDescriptor) FillLayout(org.eclipse.swt.layout.FillLayout) Section(org.eclipse.ui.forms.widgets.Section) Geometry(org.openlca.geo.geojson.Geometry) GeoJSON(org.openlca.geo.geojson.GeoJSON) FeatureCollection(org.openlca.geo.geojson.FeatureCollection) Pair(org.openlca.util.Pair) FormToolkit(org.eclipse.ui.forms.widgets.FormToolkit) MsgBox(org.openlca.app.util.MsgBox) List(java.util.List) UI(org.openlca.app.util.UI) Contribution(org.openlca.core.results.Contribution) FileChooser(org.openlca.app.components.FileChooser) CostResultDescriptor(org.openlca.app.util.CostResultDescriptor) ImpactDescriptor(org.openlca.core.model.descriptors.ImpactDescriptor) FeatureCollection(org.openlca.geo.geojson.FeatureCollection) ArrayList(java.util.ArrayList) Feature(org.openlca.geo.geojson.Feature) Pair(org.openlca.util.Pair) Location(org.openlca.core.model.Location)

Example 4 with Contribution

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();
}
Also used : Viewer(org.eclipse.jface.viewers.Viewer) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) Location(org.openlca.core.model.Location) ArrayList(java.util.ArrayList) List(java.util.List) Stream(java.util.stream.Stream) EnviFlow(org.openlca.core.matrix.index.EnviFlow) ContributionResult(org.openlca.core.results.ContributionResult) Contribution(org.openlca.core.results.Contribution) CostResultDescriptor(org.openlca.app.util.CostResultDescriptor) FlowDescriptor(org.openlca.core.model.descriptors.FlowDescriptor) ImpactDescriptor(org.openlca.core.model.descriptors.ImpactDescriptor) ProcessDescriptor(org.openlca.core.model.descriptors.ProcessDescriptor) ITreeContentProvider(org.eclipse.jface.viewers.ITreeContentProvider) FlowDescriptor(org.openlca.core.model.descriptors.FlowDescriptor) ImpactDescriptor(org.openlca.core.model.descriptors.ImpactDescriptor) CostResultDescriptor(org.openlca.app.util.CostResultDescriptor) Contribution(org.openlca.core.results.Contribution) Location(org.openlca.core.model.Location)

Example 5 with Contribution

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);
}
Also used : ParameterTable(org.openlca.core.matrix.ParameterTable) App(org.openlca.app.App) Controls(org.openlca.app.util.Controls) Images(org.openlca.app.rcp.images.Images) ModelType(org.openlca.core.model.ModelType) Database(org.openlca.app.db.Database) Actions(org.openlca.app.util.Actions) EnviFlow(org.openlca.core.matrix.index.EnviFlow) TechIndex(org.openlca.core.matrix.index.TechIndex) EnviIndex(org.openlca.core.matrix.index.EnviIndex) Composite(org.eclipse.swt.widgets.Composite) ReferenceAmount(org.openlca.core.math.ReferenceAmount) ImpactMethodViewer(org.openlca.app.viewers.combo.ImpactMethodViewer) Icon(org.openlca.app.rcp.images.Icon) ITreeContentProvider(org.eclipse.jface.viewers.ITreeContentProvider) FlowType(org.openlca.core.model.FlowType) Descriptor(org.openlca.core.model.descriptors.Descriptor) ITableLabelProvider(org.eclipse.jface.viewers.ITableLabelProvider) Button(org.eclipse.swt.widgets.Button) Process(org.openlca.core.model.Process) ContributionImage(org.openlca.app.components.ContributionImage) ArrayContentProvider(org.eclipse.jface.viewers.ArrayContentProvider) ColumnLabelProvider(org.eclipse.jface.viewers.ColumnLabelProvider) IManagedForm(org.eclipse.ui.forms.IManagedForm) Collectors(java.util.stream.Collectors) TechFlow(org.openlca.core.matrix.index.TechFlow) List(java.util.List) Strings(org.openlca.util.Strings) UI(org.openlca.app.util.UI) Contribution(org.openlca.core.results.Contribution) SWT(org.eclipse.swt.SWT) TreeViewer(org.eclipse.jface.viewers.TreeViewer) ImpactIndex(org.openlca.core.matrix.index.ImpactIndex) ImpactBuilder(org.openlca.core.matrix.ImpactBuilder) M(org.openlca.app.M) Labels(org.openlca.app.util.Labels) EagerResultProvider(org.openlca.core.results.providers.EagerResultProvider) ModelPage(org.openlca.app.editors.ModelPage) Trees(org.openlca.app.viewers.trees.Trees) Image(org.eclipse.swt.graphics.Image) ImpactMethodDao(org.openlca.core.database.ImpactMethodDao) ArrayList(java.util.ArrayList) Viewers(org.openlca.app.viewers.Viewers) HashSet(java.util.HashSet) MatrixBuilder(org.openlca.core.matrix.format.MatrixBuilder) Exchange(org.openlca.core.model.Exchange) JavaMatrix(org.openlca.core.matrix.format.JavaMatrix) MatrixData(org.openlca.core.matrix.MatrixData) Numbers(org.openlca.app.util.Numbers) FormToolkit(org.eclipse.ui.forms.widgets.FormToolkit) Action(org.eclipse.jface.action.Action) SolverContext(org.openlca.core.results.providers.SolverContext) ScrolledForm(org.eclipse.ui.forms.widgets.ScrolledForm) ContributionResult(org.openlca.core.results.ContributionResult) ImpactDescriptor(org.openlca.core.model.descriptors.ImpactDescriptor) ImpactMethodDescriptor(org.openlca.core.model.descriptors.ImpactMethodDescriptor) Collections(java.util.Collections) Action(org.eclipse.jface.action.Action) FormToolkit(org.eclipse.ui.forms.widgets.FormToolkit) Composite(org.eclipse.swt.widgets.Composite) EnviFlow(org.openlca.core.matrix.index.EnviFlow) ImpactDescriptor(org.openlca.core.model.descriptors.ImpactDescriptor) ImpactMethodViewer(org.openlca.app.viewers.combo.ImpactMethodViewer) Button(org.eclipse.swt.widgets.Button) ScrolledForm(org.eclipse.ui.forms.widgets.ScrolledForm) ImpactMethodDescriptor(org.openlca.core.model.descriptors.ImpactMethodDescriptor) ImpactMethodDao(org.openlca.core.database.ImpactMethodDao)

Aggregations

Contribution (org.openlca.core.results.Contribution)11 ArrayList (java.util.ArrayList)9 List (java.util.List)8 ImpactDescriptor (org.openlca.core.model.descriptors.ImpactDescriptor)7 FlowDescriptor (org.openlca.core.model.descriptors.FlowDescriptor)6 ProcessDescriptor (org.openlca.core.model.descriptors.ProcessDescriptor)6 RootDescriptor (org.openlca.core.model.descriptors.RootDescriptor)6 EnviFlow (org.openlca.core.matrix.index.EnviFlow)5 ContributionResult (org.openlca.core.results.ContributionResult)5 JsonArray (com.google.gson.JsonArray)4 Composite (org.eclipse.swt.widgets.Composite)4 UpstreamNode (org.openlca.core.results.UpstreamNode)4 UpstreamTree (org.openlca.core.results.UpstreamTree)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Collectors (java.util.stream.Collectors)3 Action (org.eclipse.jface.action.Action)3 ITreeContentProvider (org.eclipse.jface.viewers.ITreeContentProvider)3 FormToolkit (org.eclipse.ui.forms.widgets.FormToolkit)3 LocationDescriptor (org.openlca.core.model.descriptors.LocationDescriptor)3