Search in sources :

Example 46 with Unit

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

the class ImpactPage method loadFactors.

private List<Factor> loadFactors() {
    IDatabase db = Database.get();
    String sql = "select f_impact_category, f_unit, f_location, " + "value from tbl_impact_factors where f_flow = " + getModel().id;
    EntityCache ecache = Cache.getEntityCache();
    List<Factor> factors = new ArrayList<>();
    try {
        NativeSql.on(db).query(sql, r -> {
            Factor f = new Factor();
            f.impact = ecache.get(ImpactDescriptor.class, r.getLong(1));
            if (f.impact == null)
                return true;
            Unit unit = ecache.get(Unit.class, r.getLong(2));
            if (unit != null) {
                f.unit = unit.name;
            }
            long locID = r.getLong(3);
            if (!r.wasNull()) {
                f.location = ecache.get(LocationDescriptor.class, locID);
            }
            f.value = r.getDouble(4);
            factors.add(f);
            return true;
        });
    } catch (Exception e) {
        Logger log = LoggerFactory.getLogger(getClass());
        log.error("Failed to load LCIA factors", e);
    }
    // sort and set display flags
    factors.sort((f1, f2) -> {
        int c = Strings.compare(Labels.name(f1.impact), Labels.name(f2.impact));
        if (c != 0)
            return c;
        if (f1.location == null)
            return -1;
        if (f2.location == null)
            return 1;
        return Strings.compare(f1.location.code, f2.location.code);
    });
    return factors;
}
Also used : IDatabase(org.openlca.core.database.IDatabase) EntityCache(org.openlca.core.database.EntityCache) LocationDescriptor(org.openlca.core.model.descriptors.LocationDescriptor) ArrayList(java.util.ArrayList) ImpactDescriptor(org.openlca.core.model.descriptors.ImpactDescriptor) Unit(org.openlca.core.model.Unit) Logger(org.slf4j.Logger)

Example 47 with Unit

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

the class SimulationPage method getQRefText.

private String getQRefText() {
    var setup = editor.setup;
    double amount = setup.amount();
    Flow flow = setup.flow();
    Unit unit = setup.unit();
    return String.format("%s %s %s", Numbers.format(amount, 2), Labels.name(unit), Labels.name(flow));
}
Also used : Unit(org.openlca.core.model.Unit) Flow(org.openlca.core.model.Flow) TechFlow(org.openlca.core.matrix.index.TechFlow)

Example 48 with Unit

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

the class DBProvider method sync.

/**
 * Sync the flow references with the respective flow in the database (when
 * it exists). It tests that the definition of the flow reference can be
 * fulfilled with a database flow (i.e. the ref-IDs match and the flow
 * property and unit is defined for that flow). If this is not the case it
 * returns it sets an error state to the given reference. Otherwise, it will
 * mutate the flow reference to have the respective database IDs of the
 * corresponding flow and sets the property and unit to the respective
 * defaults if they are missing. Also, it will return the matching flow in
 * case there was no error (otherwise null).
 */
public Flow sync(FlowRef ref) {
    if (Sync.isInvalidFlowRef(ref))
        return null;
    // we update the status in the following sync. steps
    ref.status = null;
    // check the flow
    Flow flow = new FlowDao(db).getForRefId(ref.flow.refId);
    if (flow == null) {
        ref.status = MappingStatus.error("there is no flow with id=" + ref.flow.refId + " in the database");
        return null;
    }
    // check the flow property
    FlowProperty prop = null;
    if (ref.property == null) {
        prop = flow.referenceFlowProperty;
    } else {
        for (FlowPropertyFactor f : flow.flowPropertyFactors) {
            if (f.flowProperty == null)
                continue;
            if (Objects.equals(ref.property.refId, f.flowProperty.refId)) {
                prop = f.flowProperty;
                break;
            }
        }
    }
    if (prop == null || prop.unitGroup == null) {
        ref.status = MappingStatus.error("the flow in the database has" + " no corresponding flow property");
        return null;
    }
    // check the unit
    Unit u = null;
    if (ref.unit == null) {
        u = prop.unitGroup.referenceUnit;
    } else {
        for (Unit ui : prop.unitGroup.units) {
            if (Objects.equals(ref.unit.refId, ui.refId)) {
                u = ui;
                break;
            }
        }
    }
    if (u == null) {
        ref.status = MappingStatus.error("the flow in the database has" + " no corresponding unit");
        return null;
    }
    // check a possible provider
    Process provider = null;
    if (ref.provider != null) {
        provider = new ProcessDao(db).getForRefId(ref.provider.refId);
        if (provider == null) {
            ref.status = MappingStatus.error("the provider does not exist in the database");
            return null;
        }
        boolean exists = provider.exchanges.stream().anyMatch(e -> !e.isAvoided && Objects.equals(e.flow, flow) && ((e.isInput && flow.flowType == FlowType.WASTE_FLOW) || (!e.isInput && flow.flowType == FlowType.PRODUCT_FLOW)));
        if (!exists) {
            ref.status = MappingStatus.error("the given provider does not deliver that flow");
            return null;
        }
    }
    // sync the reference data
    if (ref.property == null) {
        ref.property = Descriptor.of(prop);
    }
    if (ref.unit == null) {
        ref.unit = Descriptor.of(u);
    }
    ref.flow.id = flow.id;
    ref.property.id = prop.id;
    ref.unit.id = u.id;
    if (provider != null) {
        ref.provider = Descriptor.of(provider);
    }
    Sync.checkFlowName(ref, flow.name);
    Sync.checkFlowCategory(ref, String.join("/", Categories.path(flow.category)));
    Sync.checkFlowType(ref, flow.flowType);
    Sync.checkFlowLocation(ref, flow.location == null ? null : flow.location.code);
    if (provider != null) {
        Sync.checkProviderLocation(ref, provider.location == null ? null : provider.location.code);
    }
    if (ref.status == null) {
        ref.status = MappingStatus.ok("flow in sync. with database");
    }
    return flow;
}
Also used : FlowDao(org.openlca.core.database.FlowDao) ProcessDao(org.openlca.core.database.ProcessDao) Process(org.openlca.core.model.Process) FlowPropertyFactor(org.openlca.core.model.FlowPropertyFactor) Unit(org.openlca.core.model.Unit) FlowProperty(org.openlca.core.model.FlowProperty) Flow(org.openlca.core.model.Flow)

Example 49 with Unit

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

the class FactorClipboard method factor.

private ImpactFactor factor(String[] row) {
    if (row.length < 4)
        return null;
    String name = row[0];
    String category = row[1];
    String amount = row[2];
    String unit = row[3];
    // filter the flows by matching names and categories
    List<Flow> candidates = flows.stream().filter(d -> Strings.nullOrEqual(d.name, name)).map(d -> new FlowDao(db).getForId(d.id)).filter(flow -> {
        if (flow.category == null)
            return Strings.nullOrEmpty(category);
        String path = CategoryPath.getFull(flow.category);
        return Strings.nullOrEqual(path, category);
    }).collect(Collectors.toList());
    if (candidates.isEmpty())
        return null;
    // find a matching flow for the unit
    // the unit in the table has the format:
    // <LCIA ref. unit> / <flow unit>
    // the following only works if the LCIA
    // ref. unit does not contain a slash, but
    // this should be very unlikely
    int i = unit.indexOf('/');
    if (i >= 0) {
        unit = unit.substring(i + 1).trim();
    }
    ImpactFactor factor = new ImpactFactor();
    for (Flow flow : candidates) {
        for (FlowPropertyFactor p : flow.flowPropertyFactors) {
            if (p.flowProperty == null || p.flowProperty.unitGroup == null)
                continue;
            Unit u = p.flowProperty.unitGroup.getUnit(unit);
            if (u == null)
                continue;
            factor.flow = flow;
            factor.flowPropertyFactor = p;
            factor.unit = u;
            if (Objects.equals(p.flowProperty, flow.referenceFlowProperty))
                break;
        }
        if (factor.flow != null)
            break;
    }
    if (factor.flow == null)
        return null;
    // set the amount value / formula
    try {
        factor.value = Double.parseDouble(amount);
    } catch (Exception e) {
        factor.formula = amount;
    }
    // uncertainty value
    if (row.length > 4) {
        factor.uncertainty = Uncertainty.fromString(row[4]);
    }
    // location
    if (row.length > 5) {
        String code = row[5];
        if (!Strings.nullOrEmpty(code)) {
            LocationDao dao = new LocationDao(db);
            factor.location = dao.getDescriptors().stream().filter(d -> Strings.nullOrEqual(code, d.code)).map(d -> dao.getForId(d.id)).findFirst().orElse(null);
        }
    }
    return factor;
}
Also used : M(org.openlca.app.M) Unit(org.openlca.core.model.Unit) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Flow(org.openlca.core.model.Flow) FlowPropertyFactor(org.openlca.core.model.FlowPropertyFactor) ImpactFactor(org.openlca.core.model.ImpactFactor) Objects(java.util.Objects) LocationDao(org.openlca.core.database.LocationDao) List(java.util.List) Database(org.openlca.app.db.Database) Strings(org.openlca.util.Strings) IDatabase(org.openlca.core.database.IDatabase) Uncertainty(org.openlca.core.model.Uncertainty) FlowDao(org.openlca.core.database.FlowDao) FlowDescriptor(org.openlca.core.model.descriptors.FlowDescriptor) Collections(java.util.Collections) CategoryPath(org.openlca.io.CategoryPath) ImpactFactor(org.openlca.core.model.ImpactFactor) FlowDao(org.openlca.core.database.FlowDao) LocationDao(org.openlca.core.database.LocationDao) FlowPropertyFactor(org.openlca.core.model.FlowPropertyFactor) Unit(org.openlca.core.model.Unit) Flow(org.openlca.core.model.Flow)

Example 50 with Unit

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

the class UnitGroupInfoPage method createAdditionalInfo.

protected void createAdditionalInfo(InfoSection infoSection, Composite body) {
    dropComponent(infoSection.composite(), M.DefaultFlowProperty, "defaultFlowProperty");
    Section section = UI.section(body, toolkit, M.Units);
    UI.gridData(section, true, true);
    Composite client = UI.sectionClient(section, toolkit);
    UnitViewer unitViewer = new UnitViewer(client, editor);
    CommentAction.bindTo(section, unitViewer, "units", getComments());
    List<Unit> units = getModel().units;
    units.sort((u1, u2) -> Strings.compare(u1.name, u2.name));
    unitViewer.setInput(units);
    editor.onSaved(() -> unitViewer.setInput(getModel().units));
}
Also used : Composite(org.eclipse.swt.widgets.Composite) Unit(org.openlca.core.model.Unit) InfoSection(org.openlca.app.editors.InfoSection) Section(org.eclipse.ui.forms.widgets.Section)

Aggregations

Unit (org.openlca.core.model.Unit)50 UnitGroup (org.openlca.core.model.UnitGroup)18 Flow (org.openlca.core.model.Flow)13 FlowProperty (org.openlca.core.model.FlowProperty)13 FlowPropertyFactor (org.openlca.core.model.FlowPropertyFactor)11 UnitGroupDao (org.openlca.core.database.UnitGroupDao)9 ArrayList (java.util.ArrayList)6 FlowDao (org.openlca.core.database.FlowDao)4 FlowPropertyDao (org.openlca.core.database.FlowPropertyDao)4 List (java.util.List)3 IDatabase (org.openlca.core.database.IDatabase)3 Collections (java.util.Collections)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 Objects (java.util.Objects)2 Test (org.junit.Test)2 M (org.openlca.app.M)2 ProcessDao (org.openlca.core.database.ProcessDao)2 UnitDao (org.openlca.core.database.UnitDao)2 ImpactFactor (org.openlca.core.model.ImpactFactor)2