Search in sources :

Example 6 with Exchange

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

the class CausalFactorTable method refresh.

public void refresh() {
    List<Exchange> products = Util.getProviderFlows(process());
    List<Exchange> newProducts = new ArrayList<>(products);
    List<Integer> removalIndices = new ArrayList<>();
    for (int i = 0; i < columns.length; i++) {
        Exchange product = columns[i].product;
        if (products.contains(product))
            newProducts.remove(product);
        else
            removalIndices.add(i);
    }
    for (int col : removalIndices) removeColumn(col);
    for (Exchange product : newProducts) addColumn(product);
    viewer.setInput(Util.getNonProviderFlows(process()));
    createModifySupport();
    viewer.refresh(true);
}
Also used : Exchange(org.openlca.core.model.Exchange) ArrayList(java.util.ArrayList)

Example 7 with Exchange

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

the class CausalFactorTable method initColumns.

private void initColumns() {
    List<Exchange> pFlows = Util.getProviderFlows(process());
    columns = new Column[pFlows.size()];
    for (int i = 0; i < columns.length; i++) {
        columns[i] = new Column(pFlows.get(i));
    }
    Arrays.sort(columns);
}
Also used : Exchange(org.openlca.core.model.Exchange) TableColumn(org.eclipse.swt.widgets.TableColumn)

Example 8 with Exchange

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

the class Exchanges method canRemove.

/**
 * Checks if the given exchanges can be removed from the process. The exchanges
 * cannot be removed and a corresponding error message is displayed when:
 *
 * <li>one of the given exchanges is the reference flow of the process
 * <li>at least one of the exchanges is used in a product system
 * <li>at least one of the exchanges is needed as default provider link
 */
static boolean canRemove(Process p, List<Exchange> exchanges) {
    if (p == null || exchanges == null)
        return false;
    // check reference flow
    if (p.quantitativeReference != null && exchanges.contains(p.quantitativeReference)) {
        MsgBox.error(M.CannotDeleteRefFlow, M.CannotDeleteRefFlowMessage);
        return false;
    }
    // collect product and waste flows
    List<Exchange> techFlows = exchanges.stream().filter(e -> e.flow != null && e.flow.flowType != FlowType.ELEMENTARY_FLOW).collect(Collectors.toList());
    if (techFlows.isEmpty())
        return true;
    // check usage in product systems
    var usages = new ExchangeUseSearch(Database.get(), p).findUses(techFlows);
    if (!usages.isEmpty()) {
        MsgBox.error(M.CannotRemoveExchanges, M.ExchangesAreUsed);
        return false;
    }
    // check provider links
    List<Exchange> providers = techFlows.stream().filter(e -> (e.isInput && e.flow.flowType == FlowType.WASTE_FLOW) || (!e.isInput && e.flow.flowType == FlowType.PRODUCT_FLOW)).collect(Collectors.toList());
    if (providers.isEmpty())
        return true;
    for (Exchange provider : providers) {
        String query = "select f_owner from tbl_exchanges where " + "f_default_provider = " + p.id + " and " + "f_flow = " + provider.flow.id + "";
        IDatabase db = Database.get();
        AtomicReference<ProcessDescriptor> ref = new AtomicReference<>();
        try {
            NativeSql.on(db).query(query, r -> {
                long owner = r.getLong(1);
                ProcessDescriptor d = new ProcessDao(db).getDescriptor(owner);
                if (d != null) {
                    ref.set(d);
                    return false;
                }
                return true;
            });
        } catch (Exception e) {
            Logger log = LoggerFactory.getLogger(Exchanges.class);
            log.error("Failed to query default providers " + query, e);
            return false;
        }
        if (ref.get() == null)
            continue;
        // we found an usage as default provider, now we need to make sure
        // that there is no other exchange with the same flow and direction
        // that can fulfill this role (and that is not in the list of
        // exchanges to be deleted).
        boolean ok = p.exchanges.stream().filter(e -> e.id != provider.id && e.isInput == provider.isInput && e.flow != null && e.flow.id == provider.flow.id && !exchanges.contains(e)).findAny().isPresent();
        if (ok)
            continue;
        MsgBox.error("Flow used as default provider", "This process is linked as default provider with flow `" + Strings.cut(Labels.name(provider.flow), 75) + "` in process `" + Strings.cut(Labels.name(ref.get()), 75) + "`.");
        return false;
    }
    return true;
}
Also used : FlowType(org.openlca.core.model.FlowType) Descriptor(org.openlca.core.model.descriptors.Descriptor) M(org.openlca.app.M) Labels(org.openlca.app.util.Labels) ExchangeUseSearch(org.openlca.core.database.usage.ExchangeUseSearch) Logger(org.slf4j.Logger) Process(org.openlca.core.model.Process) ProcessDao(org.openlca.core.database.ProcessDao) LoggerFactory(org.slf4j.LoggerFactory) MsgBox(org.openlca.app.util.MsgBox) NativeSql(org.openlca.core.database.NativeSql) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collectors(java.util.stream.Collectors) ModelType(org.openlca.core.model.ModelType) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Database(org.openlca.app.db.Database) Strings(org.openlca.util.Strings) IDatabase(org.openlca.core.database.IDatabase) Exchange(org.openlca.core.model.Exchange) ProcessDescriptor(org.openlca.core.model.descriptors.ProcessDescriptor) IDatabase(org.openlca.core.database.IDatabase) ExchangeUseSearch(org.openlca.core.database.usage.ExchangeUseSearch) AtomicReference(java.util.concurrent.atomic.AtomicReference) Logger(org.slf4j.Logger) Exchange(org.openlca.core.model.Exchange) ProcessDao(org.openlca.core.database.ProcessDao) ProcessDescriptor(org.openlca.core.model.descriptors.ProcessDescriptor)

Example 9 with Exchange

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

the class ModelImport method mapRefProcess.

private void mapRefProcess(ProcessInstance pi, Process process) {
    if (pi == null || process == null)
        return;
    system.referenceProcess = process;
    Exchange qRef = process.quantitativeReference;
    if (qRef == null)
        return;
    system.referenceExchange = qRef;
    system.targetAmount = qRef.amount;
    system.targetFlowPropertyFactor = qRef.flowPropertyFactor;
    system.targetUnit = qRef.unit;
}
Also used : Exchange(org.openlca.core.model.Exchange)

Example 10 with Exchange

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

the class ProcessWriter method map.

private void map(Exchange e, JsonObject obj) {
    Json.put(obj, "@type", Exchange.class.getSimpleName());
    Json.put(obj, "isAvoidedProduct", e.isAvoided);
    Json.put(obj, "isInput", e.isInput);
    Json.put(obj, "baseUncertainty", e.baseUncertainty);
    Json.put(obj, "amount", e.amount);
    Json.put(obj, "amountFormula", e.formula);
    Json.put(obj, "dqEntry", e.dqEntry);
    Json.put(obj, "description", e.description);
    Json.put(obj, "costFormula", e.costFormula);
    Json.put(obj, "costValue", e.costs);
    Json.put(obj, "currency", exp.handleRef(e.currency));
    Json.put(obj, "internalId", e.internalId);
    Json.put(obj, "location", exp.handleRef(e.location));
    Json.put(obj, "flow", exp.handleRef(e.flow));
    Json.put(obj, "unit", Json.asRef(e.unit));
    var property = e.flowPropertyFactor != null ? e.flowPropertyFactor.flowProperty : null;
    Json.put(obj, "flowProperty", exp.handleRef(property));
    Json.put(obj, "uncertainty", Uncertainties.map(e.uncertainty));
    // default provider
    if (e.defaultProviderId == 0L || exp.db == null)
        return;
    if (exp.exportProviders) {
        Json.put(obj, "defaultProvider", exp.handleRef(ModelType.PROCESS, e.defaultProviderId));
    } else {
        var d = new ProcessDao(exp.db).getDescriptor(e.defaultProviderId);
        Json.put(obj, "defaultProvider", Json.asRef(d));
    }
}
Also used : Exchange(org.openlca.core.model.Exchange) ProcessDao(org.openlca.core.database.ProcessDao)

Aggregations

Exchange (org.openlca.core.model.Exchange)90 Process (org.openlca.core.model.Process)20 ArrayList (java.util.ArrayList)15 Flow (org.openlca.core.model.Flow)15 Test (org.junit.Test)7 AllocationFactor (org.openlca.core.model.AllocationFactor)6 ProcessDao (org.openlca.core.database.ProcessDao)5 Parameter (org.openlca.core.model.Parameter)5 ProcessLink (org.openlca.core.model.ProcessLink)5 HashSet (java.util.HashSet)3 Uncertainty (org.openlca.core.model.Uncertainty)3 JsonObject (com.google.gson.JsonObject)2 HashMap (java.util.HashMap)2 List (java.util.List)2 M (org.openlca.app.M)2 Labels (org.openlca.app.util.Labels)2 MsgBox (org.openlca.app.util.MsgBox)2 FlowProperty (org.openlca.core.model.FlowProperty)2 FlowPropertyFactor (org.openlca.core.model.FlowPropertyFactor)2 IExchange (org.openlca.ecospold.IExchange)2