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