Search in sources :

Example 96 with Process

use of org.openlca.core.model.Process in project olca-modules by GreenDelta.

the class AvoidedFlowsTest method testWaste.

@Test
public void testWaste() {
    Process p = TestProcess.refProduct("p", 1, "kg").wasteOut("w", 0.5, "kg").elemOut("CO2", 2.0, "kg").get();
    TestProcess.findExchange(p, "w").isAvoided = true;
    p = db.update(p);
    Process w = TestProcess.refWaste("w", 1.0, "kg").elemOut("CO2", 2.0, "kg").get();
    check(p, w);
}
Also used : Process(org.openlca.core.model.Process) TestProcess(org.openlca.core.TestProcess) Test(org.junit.Test)

Example 97 with Process

use of org.openlca.core.model.Process 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 98 with Process

use of org.openlca.core.model.Process 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 99 with Process

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

the class InfoPage method createDqEntryRow.

private Hyperlink createDqEntryRow(Composite parent, FormToolkit tk) {
    UI.formLabel(parent, tk, M.DataQualityEntry);
    Supplier<String> dqLabel = () -> {
        Process p = getModel();
        return p.dqSystem == null || Strings.nullOrEmpty(p.dqEntry) ? "(not specified)" : p.dqSystem.applyScoreLabels(p.dqEntry);
    };
    Hyperlink link = UI.formLink(parent, tk, dqLabel.get());
    Controls.onClick(link, e -> {
        if (getModel().dqSystem == null) {
            MsgBox.info("No data quality system is selected");
            return;
        }
        String oldVal = getModel().dqEntry;
        DQSystem system = getModel().dqSystem;
        String entry = getModel().dqEntry;
        DataQualityShell shell = DataQualityShell.withoutUncertainty(parent.getShell(), system, entry);
        shell.onOk = (_shell) -> {
            getModel().dqEntry = _shell.getSelection();
        };
        shell.onDelete = (_shell) -> {
            getModel().dqEntry = null;
        };
        shell.addDisposeListener(_e -> {
            if (Objects.equals(oldVal, getModel().dqEntry))
                return;
            link.setText(dqLabel.get());
            link.pack();
            getEditor().setDirty(true);
        });
        shell.open();
    });
    link.setEnabled(isEditable());
    new CommentControl(parent, getToolkit(), "dqEntry", getComments());
    return link;
}
Also used : DataQualityShell(org.openlca.app.editors.processes.data_quality.DataQualityShell) DQSystem(org.openlca.core.model.DQSystem) CommentControl(org.openlca.app.editors.comments.CommentControl) Process(org.openlca.core.model.Process) Hyperlink(org.eclipse.ui.forms.widgets.Hyperlink) ImageHyperlink(org.eclipse.ui.forms.widgets.ImageHyperlink)

Example 100 with Process

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

the class ProcessEditor method evalFormulas.

private void evalFormulas() {
    Process p = getModel();
    var errors = Formulas.eval(Database.get(), p);
    if (!errors.isEmpty()) {
        String message = errors.get(0);
        if (errors.size() > 1)
            message += " (" + (errors.size() - 1) + " more)";
        MsgBox.error(M.FormulaEvaluationFailed, message);
    }
}
Also used : Process(org.openlca.core.model.Process)

Aggregations

Process (org.openlca.core.model.Process)103 Test (org.junit.Test)35 ProcessDao (org.openlca.core.database.ProcessDao)27 Exchange (org.openlca.core.model.Exchange)20 TestProcess (org.openlca.core.TestProcess)15 ProductSystem (org.openlca.core.model.ProductSystem)14 Flow (org.openlca.core.model.Flow)13 Parameter (org.openlca.core.model.Parameter)13 Before (org.junit.Before)9 IDatabase (org.openlca.core.database.IDatabase)9 ProcessDocumentation (org.openlca.core.model.ProcessDocumentation)9 ProcessDescriptor (org.openlca.core.model.descriptors.ProcessDescriptor)9 AbstractZipTest (org.openlca.jsonld.AbstractZipTest)8 AllocationMethod (org.openlca.core.model.AllocationMethod)6 FlowProperty (org.openlca.core.model.FlowProperty)6 ImpactMethod (org.openlca.core.model.ImpactMethod)6 Location (org.openlca.core.model.Location)6 File (java.io.File)5 UnitGroup (org.openlca.core.model.UnitGroup)5 FullResult (org.openlca.core.results.FullResult)5