use of org.openlca.io.UnitMappingEntry in project olca-modules by GreenDelta.
the class FlowImport method createBucket.
/**
* Create the flow bucket for the given flow and unit.
*/
private FlowBucket createBucket(Flow flow, String unit) {
UnitMappingEntry mapEntry = unitMapping.getEntry(unit);
if (mapEntry == null || !mapEntry.isValid())
return null;
if (flow.getFactor(mapEntry.flowProperty) == null) {
log.error("The unit/property for flow {}/{} " + "changed in the database", flow, unit);
return null;
}
FlowBucket bucket = new FlowBucket();
bucket.conversionFactor = 1.0;
bucket.flow = flow;
bucket.flowProperty = mapEntry.flowProperty;
bucket.unit = mapEntry.unit;
return bucket;
}
use of org.openlca.io.UnitMappingEntry in project olca-modules by GreenDelta.
the class FlowImport method createFlow.
/**
* Creates a new flow and inserts it in the database.
*/
private FlowBucket createFlow(String flowKey, Flow flow, String unit) {
var entry = unitMapping.getEntry(unit);
if (entry == null || !entry.isValid()) {
// we have no valid unit mapping; we create
// a new one here
var _db = db.database;
var units = _db.insert(UnitGroup.of("New: " + unit, unit));
var prop = _db.insert(FlowProperty.of("New: " + unit, units));
entry = new UnitMappingEntry();
entry.factor = 1.0;
entry.flowProperty = prop;
entry.unit = units.referenceUnit;
entry.unitGroup = units;
entry.unitName = unit;
unitMapping.put(unit, entry);
}
flow.referenceFlowProperty = entry.flowProperty;
FlowPropertyFactor factor = new FlowPropertyFactor();
factor.flowProperty = entry.flowProperty;
factor.conversionFactor = 1.0;
flow.flowPropertyFactors.add(factor);
db.put(flow, flowKey);
return createBucket(flow, unit);
}
use of org.openlca.io.UnitMappingEntry in project olca-modules by GreenDelta.
the class UnitSync method createForQuantity.
private UnitGroup createForQuantity(CsvDataSet dataSet, QuantityRow quantity, UnitMapping mapping) {
// create unit group and flow property
var group = UnitGroup.of("Units of " + quantity.name());
for (var row : dataSet.units()) {
if (!Objects.equals(row.quantity(), quantity.name()))
continue;
var unit = Unit.of(row.name(), row.conversionFactor());
group.units.add(unit);
if (Objects.equals(row.name(), row.referenceUnit())) {
group.referenceUnit = unit;
}
}
group = db.insert(group);
group.defaultFlowProperty = db.insert(FlowProperty.of(quantity.name(), group));
log.imported(group.defaultFlowProperty);
group = db.update(group);
log.imported(group);
// create the mapping entries
for (Unit unit : group.units) {
var e = new UnitMappingEntry();
e.flowProperty = group.defaultFlowProperty;
e.unitName = unit.name;
e.unit = unit;
e.factor = unit.conversionFactor;
e.unitGroup = group;
mapping.put(unit.name, e);
}
return group;
}
use of org.openlca.io.UnitMappingEntry in project olca-modules by GreenDelta.
the class MethodImport method flow.
private FlowRec flow(spold2.ImpactFactor eFactor) {
FlowRec rec = flowCache.get(eFactor.flowID);
if (rec != null)
return rec;
// try to find an existing flow with a matching unit
Flow flow = new FlowDao(db).getForRefId(eFactor.flowID);
if (flow != null) {
FlowPropertyFactor property = null;
Unit unit = null;
for (FlowPropertyFactor f : flow.flowPropertyFactors) {
FlowProperty prop = f.flowProperty;
if (prop == null || prop.unitGroup == null) {
continue;
}
Unit u = prop.unitGroup.getUnit(eFactor.flowUnit);
if (u == null)
continue;
property = f;
unit = u;
if (Objects.equals(prop, flow.referenceFlowProperty))
break;
}
if (unit == null) {
log.warn("a flow {} with the ID {} exists but it " + "does not have a unit {}", flow.name, eFactor.flowID, eFactor.flowUnit);
return null;
}
rec = new FlowRec(flow, property, unit);
flowCache.put(eFactor.flowID, rec);
return rec;
}
// try to create a new flow
if (unitMap == null) {
unitMap = UnitMapping.createDefault(db);
}
UnitMappingEntry e = unitMap.getEntry(eFactor.flowUnit);
if (e == null) {
// TODO: not sure how far we should go with the unit mapping here
// we could even generate flow properties and unit groups for units
// that do not exist yet but for the elementary flows this should
// rarely be the case
log.warn("Unknown unit {}; could not create flow {} with ID {}", eFactor.flowUnit, eFactor.flowName, eFactor.flowID);
return null;
}
flow = new Flow();
flow.refId = eFactor.flowID;
flow.name = eFactor.flowName;
flow.flowType = FlowType.ELEMENTARY_FLOW;
if (eFactor.compartment != null) {
flow.category = new CategoryDao(db).sync(ModelType.FLOW, "Elementary flows", eFactor.compartment.compartment, eFactor.compartment.subCompartment);
}
flow.referenceFlowProperty = e.flowProperty;
FlowPropertyFactor property = new FlowPropertyFactor();
property.conversionFactor = 1.0;
property.flowProperty = e.flowProperty;
flow.flowPropertyFactors.add(property);
flow.lastChange = new Date().getTime();
flow = new FlowDao(db).insert(flow);
rec = new FlowRec(flow, property, e.unit);
flowCache.put(eFactor.flowID, rec);
return rec;
}
use of org.openlca.io.UnitMappingEntry in project olca-app by GreenDelta.
the class UnitMappingPage method mapAndSetUnitInput.
private void mapAndSetUnitInput(List<String> units) {
UnitMapping defaultMapping = UnitMapping.createDefault(database);
mappings.clear();
for (String unitName : units) {
UnitMappingEntry entry = defaultMapping.getEntry(unitName);
if (entry == null) {
entry = new UnitMappingEntry();
entry.unitName = unitName;
}
mappings.add(entry);
}
tableViewer.setInput(mappings);
checkCompletion();
}
Aggregations