Search in sources :

Example 1 with UnitMappingEntry

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;
}
Also used : UnitMappingEntry(org.openlca.io.UnitMappingEntry)

Example 2 with UnitMappingEntry

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);
}
Also used : UnitMappingEntry(org.openlca.io.UnitMappingEntry) FlowPropertyFactor(org.openlca.core.model.FlowPropertyFactor)

Example 3 with UnitMappingEntry

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;
}
Also used : UnitMappingEntry(org.openlca.io.UnitMappingEntry) Unit(org.openlca.core.model.Unit)

Example 4 with UnitMappingEntry

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;
}
Also used : CategoryDao(org.openlca.core.database.CategoryDao) FlowDao(org.openlca.core.database.FlowDao) UnitMappingEntry(org.openlca.io.UnitMappingEntry) FlowPropertyFactor(org.openlca.core.model.FlowPropertyFactor) Unit(org.openlca.core.model.Unit) FlowProperty(org.openlca.core.model.FlowProperty) Date(java.util.Date) Flow(org.openlca.core.model.Flow)

Example 5 with UnitMappingEntry

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();
}
Also used : UnitMapping(org.openlca.io.UnitMapping) UnitMappingEntry(org.openlca.io.UnitMappingEntry)

Aggregations

UnitMappingEntry (org.openlca.io.UnitMappingEntry)6 FlowProperty (org.openlca.core.model.FlowProperty)2 FlowPropertyFactor (org.openlca.core.model.FlowPropertyFactor)2 Unit (org.openlca.core.model.Unit)2 Date (java.util.Date)1 CategoryDao (org.openlca.core.database.CategoryDao)1 FlowDao (org.openlca.core.database.FlowDao)1 Flow (org.openlca.core.model.Flow)1 UnitMapping (org.openlca.io.UnitMapping)1