<commit_before>package org.openlca.io.ilcd.input;

import java.util.List;

import org.openlca.core.database.IDatabase;
import org.openlca.core.database.ParameterDao;
import org.openlca.core.model.Parameter;
import org.openlca.core.model.ParameterScope;
import org.openlca.core.model.Process;
import org.openlca.ilcd.util.LangString;
import org.openlca.ilcd.util.ParameterExtension;
import org.openlca.ilcd.util.ProcessBag;
import org.openlca.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Adds the parameters of an ILCD process data set to an openLCA process data
 * set. If the scope of the parameter is 'global' it is inserted as database
 * parameter if it not yet exists.
 */
class ProcessParameterConversion {

	private Logger log = LoggerFactory.getLogger(getClass());
	private Process olcaProcess;
	private ParameterDao dao;

	public ProcessParameterConversion(Process olcaProcess, IDatabase database) {
		this.olcaProcess = olcaProcess;
		this.dao = new ParameterDao(database);
	}

	public void run(ProcessBag ilcdProcess) {
		List<org.openlca.ilcd.processes.Parameter> iParameters = ilcdProcess
				.getParameters();
		for (org.openlca.ilcd.processes.Parameter iParameter : iParameters) {
			if (iParameter.getName() == null
					|| iParameter.getName().startsWith("temp_olca_param"))
				continue;
			Parameter param = convertParameter(iParameter);
			addOrInsert(param);
		}
	}

	private Parameter convertParameter(
			org.openlca.ilcd.processes.Parameter iParameter) {
		ParameterScope scope = ParameterScope.PROCESS;
		if (isGlobal(iParameter))
			scope = ParameterScope.GLOBAL;
		Parameter param = new Parameter();
		param.setScope(scope);
		param.setName(iParameter.getName());
		Double mean = iParameter.getMeanValue();
		param.setDescription(LangString.getLabel(iParameter.getComment()));
		if (mean != null)
			param.setValue(mean);
		param.setInputParameter(true);
		if (iParameter.getFormula() != null && scope == ParameterScope.PROCESS) {
			param.setFormula(iParameter.getFormula());
			param.setInputParameter(false);
		}
		return param;
	}

	private boolean isGlobal(org.openlca.ilcd.processes.Parameter iParameter) {<commit_msg>check if parameter formulas are plain numbers<commit_after>package org.openlca.io.ilcd.input;

import java.util.List;

import org.openlca.core.database.IDatabase;
import org.openlca.core.database.ParameterDao;
import org.openlca.core.model.Parameter;
import org.openlca.core.model.ParameterScope;
import org.openlca.core.model.Process;
import org.openlca.ilcd.util.LangString;
import org.openlca.ilcd.util.ParameterExtension;
import org.openlca.ilcd.util.ProcessBag;
import org.openlca.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Adds the parameters of an ILCD process data set to an openLCA process data
 * set. If the scope of the parameter is 'global' it is inserted as database
 * parameter if it not yet exists.
 */
class ProcessParameterConversion {

	private Logger log = LoggerFactory.getLogger(getClass());
	private Process olcaProcess;
	private ParameterDao dao;

	public ProcessParameterConversion(Process olcaProcess, IDatabase database) {
		this.olcaProcess = olcaProcess;
		this.dao = new ParameterDao(database);
	}

	public void run(ProcessBag ilcdProcess) {
		List<org.openlca.ilcd.processes.Parameter> iParameters = ilcdProcess
				.getParameters();
		for (org.openlca.ilcd.processes.Parameter iParameter : iParameters) {
			if (iParameter.getName() == null
					|| iParameter.getName().startsWith("temp_olca_param"))
				continue;
			Parameter param = convertParameter(iParameter);
			addOrInsert(param);
		}
	}

	private Parameter convertParameter(
			org.openlca.ilcd.processes.Parameter iParameter) {
		ParameterScope scope = ParameterScope.PROCESS;
		if (isGlobal(iParameter))
			scope = ParameterScope.GLOBAL;
		Parameter param = new Parameter();
		param.setScope(scope);
		param.setName(iParameter.getName());
		Double mean = iParameter.getMeanValue();
		param.setDescription(LangString.getLabel(iParameter.getComment()));
		if (mean != null)
			param.setValue(mean);
		param.setInputParameter(true);
		if (hasFormula(iParameter) && scope == ParameterScope.PROCESS) {
			param.setFormula(iParameter.getFormula());
			param.setInputParameter(false);
		}
		return param;
	}

	private boolean hasFormula(org.openlca.ilcd.processes.Parameter iParameter) {
		if (iParameter.getFormula() == null)
			return false;
		try {
			Double.parseDouble(iParameter.getFormula());
			return false; // the "formula" is a plain number
		} catch (Exception e) {
			return true; // not a number
		}
	}

	private boolean isGlobal(org.openlca.ilcd.processes.Parameter iParameter) {
{'fraction_non_alphanumeric': 0.10846448319863888, 'fraction_numerical': 0.0008507018290089324, 'mean_word_length': 4.983460559796438, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '985641', 'n_tokens_mistral': 1570, 'n_tokens_neox': 1472, 'n_words': 318}