All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openlca.util.AllocationUtils Maven / Gradle / Ivy

There is a newer version: 2.2.1
Show newest version
package org.openlca.util;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import org.openlca.core.model.Exchange;
import org.openlca.core.model.Flow;
import org.openlca.core.model.FlowProperty;
import org.openlca.core.model.Process;

public final class AllocationUtils {

	private AllocationUtils() {
	}

	/**
	 * Removes invalid allocation factors from the given process. New allocation
	 * factors are initialized with a default value if required. Values of
	 * existing allocation factors are not changed.
	 */
	public static void cleanup(Process process) {
		if (process == null)
			return;
		new AllocationCleanup(process).run();
	}

	/**
	 * Returns the list of flow properties that could be used to calculate
	 * allocation factors. These are flow properties that are present for each
	 * product output or waste input of the given process.
	 */
	public static Set allocationPropertiesOf(Process process) {
		if (process == null)
			return Collections.emptySet();

		var techFlows = process.exchanges.stream()
				.filter(Exchanges::isProviderFlow)
				.map(e -> e.flow)
				.toList();
		if (techFlows.isEmpty())
			return Collections.emptySet();

		var first = techFlows.get(0);
		var props = new HashSet();
		for (var propFac : first.flowPropertyFactors) {
			var nextProp = propFac.flowProperty;
			boolean matches = true;
			for (int i = 1; i < techFlows.size(); i++) {
				if (!hasProperty(techFlows.get(i), nextProp)) {
					matches = false;
					break;
				}
			}
			if (matches) {
				props.add(nextProp);
			}
		}

		return props;
	}

	private static boolean hasProperty(Flow flow, FlowProperty prop) {
		if (flow == null || prop == null)
			return false;
		for (var factor : flow.flowPropertyFactors) {
			if (Objects.equals(factor.flowProperty, prop))
				return true;
		}
		return false;
	}

	/**
	 * Provider flows are product outputs are waste inputs. For each of such a
	 * flow a mono-functional process can be created when applying allocation
	 * factors.
	 */
	public static List getProviderFlows(Process p) {
		if (p == null)
			return Collections.emptyList();
		return p.exchanges.stream()
				.filter(Exchanges::isProviderFlow)
				.collect(Collectors.toList());
	}

	/**
	 * Non-provider flows are product inputs, waste outputs and all elementary
	 * flows that are partitioned when applying allocation factors to create
	 * mono-functional processes from a multi-functional process.
	 */
	public static List getNonProviderFlows(Process p) {
		if (p == null)
			return Collections.emptyList();
		return p.exchanges.stream()
				.filter(e -> !Exchanges.isProviderFlow(e))
				.collect(Collectors.toList());
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy