Search in sources :

Example 26 with FlowDao

use of org.openlca.core.database.FlowDao in project olca-modules by GreenDelta.

the class ConversionTableTest method testFlowPropertyFactor.

@Test
public void testFlowPropertyFactor() throws Exception {
    FlowDao dao = new FlowDao(database);
    Flow flow = new Flow();
    flow.name = "test-flow";
    FlowPropertyFactor factor1 = new FlowPropertyFactor();
    factor1.conversionFactor = 1d;
    flow.flowPropertyFactors.add(factor1);
    FlowPropertyFactor factor2 = new FlowPropertyFactor();
    factor2.conversionFactor = 0.42;
    flow.flowPropertyFactors.add(factor2);
    dao.insert(flow);
    ConversionTable table = ConversionTable.create(database);
    Assert.assertEquals(1d, table.getPropertyFactor(factor1.id), 1e-16);
    Assert.assertEquals(0.42, table.getPropertyFactor(factor2.id), 1e-16);
    dao.delete(flow);
}
Also used : FlowDao(org.openlca.core.database.FlowDao) FlowPropertyFactor(org.openlca.core.model.FlowPropertyFactor) Flow(org.openlca.core.model.Flow) Test(org.junit.Test)

Example 27 with FlowDao

use of org.openlca.core.database.FlowDao in project olca-modules by GreenDelta.

the class ProductSystemWriter method mapLinks.

private void mapLinks(ProductSystem system, ProtoProductSystem.Builder proto, Map<Long, RootDescriptor> processes) {
    // collect the used flows
    var flowIDs = new HashSet<Long>();
    var usedExchanges = new TLongHashSet();
    for (var link : system.processLinks) {
        flowIDs.add(link.flowId);
        usedExchanges.add(link.exchangeId);
    }
    var flows = new FlowDao(config.db).getDescriptors(flowIDs).stream().collect(Collectors.toMap(d -> d.id, d -> d));
    // collect the used exchanges
    var exchangeIDs = new TLongIntHashMap();
    String sql = "select id, internal_id from tbl_exchanges";
    NativeSql.on(config.db).query(sql, r -> {
        long id = r.getLong(1);
        if (usedExchanges.contains(id)) {
            exchangeIDs.put(id, r.getInt(2));
        }
        return true;
    });
    // add the links
    for (var link : system.processLinks) {
        var protoLink = ProtoProcessLink.newBuilder();
        // provider
        var provider = processes.get(link.providerId);
        if (provider != null) {
            protoLink.setProvider(Refs.tinyRefOf(provider));
            Out.dep(config, provider);
        }
        // process
        var process = processes.get(link.processId);
        if (process != null) {
            protoLink.setProcess(Refs.tinyRefOf(process));
            Out.dep(config, process);
        }
        // flow
        var flow = flows.get(link.flowId);
        if (flow != null) {
            protoLink.setFlow(Refs.tinyRefOf(flow));
        }
        // linked exchange
        var eid = exchangeIDs.get(link.exchangeId);
        if (eid != 0) {
            protoLink.setExchange(ProtoExchangeRef.newBuilder().setInternalId(eid).build());
        }
        // add the link
        proto.addProcessLinks(protoLink);
    }
}
Also used : ProtoParameterRedef(org.openlca.proto.ProtoParameterRedef) Process(org.openlca.core.model.Process) ProtoExchangeRef(org.openlca.proto.ProtoExchangeRef) ProcessDao(org.openlca.core.database.ProcessDao) TLongIntHashMap(gnu.trove.map.hash.TLongIntHashMap) HashMap(java.util.HashMap) NativeSql(org.openlca.core.database.NativeSql) ProductSystemDao(org.openlca.core.database.ProductSystemDao) ProtoType(org.openlca.proto.ProtoType) Collectors(java.util.stream.Collectors) ModelType(org.openlca.core.model.ModelType) ProductSystem(org.openlca.core.model.ProductSystem) RootDescriptor(org.openlca.core.model.descriptors.RootDescriptor) ProtoParameterRedefSet(org.openlca.proto.ProtoParameterRedefSet) HashSet(java.util.HashSet) ImpactCategory(org.openlca.core.model.ImpactCategory) Strings(org.openlca.util.Strings) ProtoProcessLink(org.openlca.proto.ProtoProcessLink) ProtoProductSystem(org.openlca.proto.ProtoProductSystem) Map(java.util.Map) TLongHashSet(gnu.trove.set.hash.TLongHashSet) FlowDao(org.openlca.core.database.FlowDao) TLongIntHashMap(gnu.trove.map.hash.TLongIntHashMap) FlowDao(org.openlca.core.database.FlowDao) TLongHashSet(gnu.trove.set.hash.TLongHashSet) HashSet(java.util.HashSet) TLongHashSet(gnu.trove.set.hash.TLongHashSet)

Example 28 with FlowDao

use of org.openlca.core.database.FlowDao 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 29 with FlowDao

use of org.openlca.core.database.FlowDao in project olca-app by GreenDelta.

the class FlowUseSection method render.

void render(Composite body, FormToolkit toolkit) {
    log.trace("render flow-use-section for flow {}", flow);
    FlowDao dao = new FlowDao(database);
    Set<Long> recipients = dao.getWhereInput(flow.id);
    Set<Long> providers = dao.getWhereOutput(flow.id);
    if (recipients.isEmpty() && providers.isEmpty())
        return;
    Section section = UI.section(body, toolkit, M.UsedInProcesses);
    section.setExpanded(false);
    parent = UI.sectionClient(section, toolkit);
    this.toolkit = toolkit;
    App.runInUI("Render usage links", () -> {
        renderLinks(M.ConsumedBy, recipients, Icon.INPUT.get());
        renderLinks(M.ProducedBy, providers, Icon.OUTPUT.get());
    });
}
Also used : FlowDao(org.openlca.core.database.FlowDao) Section(org.eclipse.ui.forms.widgets.Section)

Example 30 with FlowDao

use of org.openlca.core.database.FlowDao in project olca-app by GreenDelta.

the class Matcher method findProvider.

private ProcessDescriptor findProvider(IDatabase db, FlowRef s, FlowRef t) {
    long tid = t.flow.id;
    var processIDs = t.flow.flowType == FlowType.WASTE_FLOW ? new FlowDao(db).getWhereInput(tid) : new FlowDao(db).getWhereOutput(tid);
    if (processIDs.isEmpty())
        return null;
    var candidates = new ProcessDao(db).getDescriptors(processIDs);
    if (candidates.isEmpty())
        return null;
    if (candidates.size() == 1)
        return candidates.get(0);
    ProcessDescriptor cand = null;
    double score = 0.0;
    parser.parseInto(phrase1, s.flow.name);
    for (var d : candidates) {
        // include possible location codes; location codes are
        // often added to process names
        var processName = Labels.name(d);
        parser.parseInto(phrase2, processName);
        double sim = similarity.get(phrase1, phrase2);
        if (cand == null || sim > score) {
            cand = d;
            score = sim;
        }
    }
    return cand;
}
Also used : FlowDao(org.openlca.core.database.FlowDao) ProcessDao(org.openlca.core.database.ProcessDao) ProcessDescriptor(org.openlca.core.model.descriptors.ProcessDescriptor)

Aggregations

FlowDao (org.openlca.core.database.FlowDao)38 Flow (org.openlca.core.model.Flow)21 ProcessDao (org.openlca.core.database.ProcessDao)13 FlowPropertyFactor (org.openlca.core.model.FlowPropertyFactor)10 ArrayList (java.util.ArrayList)7 Collectors (java.util.stream.Collectors)6 FlowPropertyDao (org.openlca.core.database.FlowPropertyDao)6 IDatabase (org.openlca.core.database.IDatabase)6 FlowProperty (org.openlca.core.model.FlowProperty)6 FlowDescriptor (org.openlca.core.model.descriptors.FlowDescriptor)5 HashMap (java.util.HashMap)4 List (java.util.List)4 Map (java.util.Map)4 Test (org.junit.Test)4 LocationDao (org.openlca.core.database.LocationDao)4 Process (org.openlca.core.model.Process)4 Unit (org.openlca.core.model.Unit)4 TLongHashSet (gnu.trove.set.hash.TLongHashSet)3 HashSet (java.util.HashSet)3 Objects (java.util.Objects)3