Search in sources :

Example 16 with FlowPropertyFactor

use of org.openlca.core.model.FlowPropertyFactor in project olca-modules by GreenDelta.

the class FlowPropertyFactorImport method map.

@Override
FlowPropertyFactor map(JsonObject json, long id) {
    FlowPropertyFactor f = new FlowPropertyFactor();
    f.id = id;
    String propId = Json.getRefId(json, "flowProperty");
    f.flowProperty = FlowPropertyImport.run(propId, conf);
    f.conversionFactor = Json.getDouble(json, "conversionFactor", 1.0);
    return f;
}
Also used : FlowPropertyFactor(org.openlca.core.model.FlowPropertyFactor)

Example 17 with FlowPropertyFactor

use of org.openlca.core.model.FlowPropertyFactor in project olca-app by GreenDelta.

the class AmountCursor method next.

@Override
void next(ResultSet cursor, PreparedStatement update) {
    long flowID = -1;
    try {
        // select the mapping entry if exists
        flowID = cursor.getLong("f_flow");
        FlowMapEntry entry = replacer.entries.get(flowID);
        if (entry == null)
            return;
        Flow source = replacer.flows.get(entry.sourceFlow().flow.id);
        Flow target = replacer.flows.get(entry.targetFlow().flow.id);
        if (source == null || target == null)
            return;
        // check if we should replace a flow here
        long ownerID = cursor.getLong(1);
        Set<Long> owners = type == ModelType.PROCESS ? replacer.processes : replacer.impacts;
        if (!owners.contains(ownerID))
            return;
        // get the conversion factor
        double factor = type == ModelType.PROCESS ? replacer.factors.forExchange(entry, cursor) : replacer.factors.forImpact(entry, cursor);
        // f_flow
        update.setLong(1, target.id);
        // f_unit
        update.setLong(2, entry.targetFlow().unit.id);
        // f_flow_property_factor
        FlowPropertyFactor targetPropFactor = propFactor(target, entry.targetFlow());
        update.setLong(3, targetPropFactor.id);
        // amount
        double amount = cursor.getDouble(5);
        update.setDouble(4, factor * amount);
        // resulting_amount_formula
        String formula = cursor.getString(6);
        if (Strings.nullOrEmpty(formula)) {
            update.setString(5, null);
        } else if (factor == 1) {
            update.setString(5, formula);
        } else {
            update.setString(5, Double.toString(factor) + " * (" + formula + ")");
        }
        // uncertainty
        Uncertainty uncertainty = readUncertainty(cursor);
        updateUncertainty(update, factor, uncertainty);
        // f_default_provider
        if (type == ModelType.PROCESS) {
            if (entry.targetFlow().provider == null) {
                update.setNull(10, Types.INTEGER);
            } else {
                update.setLong(10, entry.targetFlow().provider.id);
            }
        }
        update.executeUpdate();
        stats.inc(flowID, Stats.REPLACEMENT);
        updatedModels.add(ownerID);
    } catch (Exception e) {
        stats.inc(flowID, Stats.FAILURE);
    }
}
Also used : Uncertainty(org.openlca.core.model.Uncertainty) FlowMapEntry(org.openlca.io.maps.FlowMapEntry) FlowPropertyFactor(org.openlca.core.model.FlowPropertyFactor) SQLException(java.sql.SQLException) Flow(org.openlca.core.model.Flow)

Example 18 with FlowPropertyFactor

use of org.openlca.core.model.FlowPropertyFactor in project olca-app by GreenDelta.

the class FactorProvider method getFactor.

private double getFactor(FlowMapEntry entry, ResultSet cursor, boolean forExchange) {
    try {
        Flow flow = flows.get(entry.sourceFlow().flow.id);
        long propFacID = cursor.getLong("f_flow_property_factor");
        FlowPropertyFactor propFactor = propFactor(flow, propFacID);
        long unitID = cursor.getLong("f_unit");
        Unit unit = unit(propFactor, unitID);
        if (unit == null)
            return 0;
        double factor = forExchange ? entry.factor() : 1 / entry.factor();
        if (propFactor.flowProperty.id == entry.sourceFlow().property.id && unit.id == entry.sourceFlow().unit.id)
            return factor;
        // calculate an additional factor y that converts the exchange
        // property pi and unit ui to the property pj and unit uj of the
        // conversion entry:
        double pi = conversions.getPropertyFactor(propFactor.id);
        double ui = conversions.getUnitFactor(unit.id);
        FlowPropertyFactor entryPropFactor = propFactor(flow, entry.sourceFlow());
        if (entryPropFactor == null)
            return 0;
        double pj = conversions.getPropertyFactor(entryPropFactor.id);
        double uj = conversions.getUnitFactor(entry.sourceFlow().unit.id);
        double y = forExchange ? (ui * pj) / (pi * uj) : (pi * uj) / (ui * pj);
        return factor * y;
    } catch (Exception e) {
        Logger log = LoggerFactory.getLogger(getClass());
        log.error("failed to calculate conversion factor for " + entry, e);
        return 0;
    }
}
Also used : FlowPropertyFactor(org.openlca.core.model.FlowPropertyFactor) Unit(org.openlca.core.model.Unit) Logger(org.slf4j.Logger) Flow(org.openlca.core.model.Flow)

Example 19 with FlowPropertyFactor

use of org.openlca.core.model.FlowPropertyFactor in project olca-app by GreenDelta.

the class ProcessCreator method createFlow.

private Flow createFlow() {
    Flow flow = new Flow();
    flow.refId = UUID.randomUUID().toString();
    if (!Strings.isNullOrEmpty(flowName))
        flow.name = flowName;
    else
        flow.name = name;
    flow.description = description;
    flow.flowType = wasteProcess ? FlowType.WASTE_FLOW : FlowType.PRODUCT_FLOW;
    flow.referenceFlowProperty = flowProperty;
    var factor = new FlowPropertyFactor();
    factor.conversionFactor = 1;
    factor.flowProperty = flowProperty;
    flow.flowPropertyFactors.add(factor);
    new FlowDao(db).insert(flow);
    return flow;
}
Also used : FlowDao(org.openlca.core.database.FlowDao) FlowPropertyFactor(org.openlca.core.model.FlowPropertyFactor) Flow(org.openlca.core.model.Flow)

Example 20 with FlowPropertyFactor

use of org.openlca.core.model.FlowPropertyFactor in project olca-app by GreenDelta.

the class ExchangeEditDialog method createFormContent.

@Override
protected void createFormContent(IManagedForm mform) {
    var tk = mform.getToolkit();
    var body = UI.formBody(mform.getForm(), tk);
    UI.gridLayout(body, 2);
    text = UI.formText(body, tk, M.Amount);
    text.setText(Double.toString(exchange.amount));
    combo = UI.formCombo(body, tk, M.Unit);
    // fill the combo items
    // returns true if the given pair contains the reference unit
    Function<Pair<FlowPropertyFactor, Unit>, Boolean> isRef = pair -> {
        var prop = pair.first.flowProperty;
        var refProp = exchange.flow.referenceFlowProperty;
        if (!Objects.equals(prop, refProp))
            return false;
        var unit = pair.second;
        var refUnit = prop.unitGroup.referenceUnit;
        return Objects.equals(unit, refUnit);
    };
    var items = new String[units.size()];
    int selection = -1;
    for (int i = 0; i < units.size(); i++) {
        var pair = units.get(i);
        items[i] = Labels.name(pair.first.flowProperty) + " - " + Labels.name(pair.second);
        // check if pair matches exchange unit
        if (Objects.equals(pair.first, exchange.flowPropertyFactor) && Objects.equals(pair.second, exchange.unit)) {
            selection = i;
            continue;
        }
        // check if pair is reference unit
        if (selection < 0 && isRef.apply(pair)) {
            selection = i;
        }
    }
    combo.setItems(items);
    if (selection >= 0) {
        combo.select(selection);
    }
}
Also used : M(org.openlca.app.M) Text(org.eclipse.swt.widgets.Text) Labels(org.openlca.app.util.Labels) Combo(org.eclipse.swt.widgets.Combo) Shell(org.eclipse.swt.widgets.Shell) Pair(org.openlca.util.Pair) MsgBox(org.openlca.app.util.MsgBox) IManagedForm(org.eclipse.ui.forms.IManagedForm) Unit(org.openlca.core.model.Unit) Function(java.util.function.Function) Point(org.eclipse.swt.graphics.Point) ArrayList(java.util.ArrayList) FlowPropertyFactor(org.openlca.core.model.FlowPropertyFactor) Objects(java.util.Objects) List(java.util.List) Strings(org.openlca.util.Strings) FormDialog(org.eclipse.ui.forms.FormDialog) UI(org.openlca.app.util.UI) Exchange(org.openlca.core.model.Exchange) Collections(java.util.Collections) Point(org.eclipse.swt.graphics.Point) Pair(org.openlca.util.Pair)

Aggregations

FlowPropertyFactor (org.openlca.core.model.FlowPropertyFactor)36 Flow (org.openlca.core.model.Flow)21 FlowProperty (org.openlca.core.model.FlowProperty)17 Unit (org.openlca.core.model.Unit)11 FlowDao (org.openlca.core.database.FlowDao)9 UnitGroup (org.openlca.core.model.UnitGroup)6 ArrayList (java.util.ArrayList)4 ImpactFactor (org.openlca.core.model.ImpactFactor)3 Collections (java.util.Collections)2 List (java.util.List)2 Objects (java.util.Objects)2 Before (org.junit.Before)2 Test (org.junit.Test)2 M (org.openlca.app.M)2 Category (org.openlca.core.model.Category)2 Exchange (org.openlca.core.model.Exchange)2 Uncertainty (org.openlca.core.model.Uncertainty)2 UnitMappingEntry (org.openlca.io.UnitMappingEntry)2 Strings (org.openlca.util.Strings)2 SQLException (java.sql.SQLException)1