<commit_before>package de.ptb.epics.eve.data.scandescription.macro;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

import org.apache.log4j.Logger;

import de.ptb.epics.eve.util.io.StringUtil;

/**
 * @author <NAME>
 * @since 1.23
 */
public class MacroResolver {
	private static final Logger LOGGER = Logger.getLogger(MacroResolver.class
			.getName());
	
	private static MacroResolver INSTANCE;
	
	private List<Macro> macros;
	
	private MacroResolver() {
		macros = new ArrayList<Macro>();
		macros.add(new WeekMacro());
		macros.add(new YearMacro());
		macros.add(new Year2DMacro());
		macros.add(new MonthMacro());
		macros.add(new MonthStrMacro());
		macros.add(new DayMacro());
		macros.add(new DayStrMacro());
		macros.add(new DateMacro());
		macros.add(new DateHyphenMacro());
		macros.add(new TimeMacro());
		macros.add(new TimeHyphenMacro());
	}
	
	public static MacroResolver getInstance() {
		if (MacroResolver.INSTANCE == null) {
			MacroResolver.INSTANCE = new MacroResolver();
		}
		return MacroResolver.INSTANCE;
	}
	
	/**
	 * Resolves a string containing macros by replacing each occurence of a 
	 * known macro.
	 * 
	 * @param macroString the unresolved string
	 * @return the resolved string
	 */
	public String resolve(String macroString) {
		// extract pv names
		Pattern p = Pattern.compile("\\$\\{PV\\:[^\\}\\$]*\\}");
		Matcher m = p.matcher(macroString);
		final List<String> pvmacros = new ArrayList<String>();
		while (m.find()) {
			pvmacros.add(m.group());
		}
		
		// connect pvs
		final List<MacroPV> pvs = new ArrayList<>();
		for (String s : pvmacros) {
			pvs.add(new MacroPV(s));
		}
		;
		
		// add non-pv macros
		List<String> searchList = new ArrayList<>();
		List<String> replacementList = new ArrayList<>();
		for (Macro macro : this.macros) {
			searchList.add(macro.getName());
			replacementList.add(macro.resolve());
		}
		
		int loops = 0;
		final int max_loops = 30;
		
		while (loops < max_loops) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				LOGGER.warn(e.getMessage(), e);
			}
			if (allResolved(pvs)) {
				break;
			}
			loops++;
		}
		if (!pvs.isEmpty()) {
			if (loops < max_loops) {
				LOGGER.info("PVs read. Resolving macros.");
			} else {
				LOGGER.warn("One or more PVs could not be read and will not be resolved.");
			}
		}
		for (MacroPV macroPV: pvs) {
			if (!macroPV.isResolved()) {
				macroPV.disconnect();
			}
			searchList.add(macroPV.getName());
			replacementList.add(macroPV.resolve());
		}
		
		return StringUtil.replaceEach(macroString, searchList, replacementList);
	}
	
	/*
	 * 
	 */
	private boolean allResolved(List<MacroPV> macropvlist) {
		for (MacroPV macroPV : macropvlist) {
			if (!macroPV.isResolved()) {
				return false;
			}
		}
		return true;
	}
	
	/**
	 * Returns a new list containing all macros of MacroResolver.
	 * @return a new list containing all macros
	 */
	public List<Macro> getMacros() {
		return new ArrayList<Macro>(this.macros);
	}
}<commit_msg>added debug log for macro resolver<commit_after>package de.ptb.epics.eve.data.scandescription.macro;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

import org.apache.log4j.Logger;

import de.ptb.epics.eve.util.io.StringUtil;

/**
 * @author <NAME>
 * @since 1.23
 */
public class MacroResolver {
	private static final Logger LOGGER = Logger.getLogger(MacroResolver.class
			.getName());
	
	private static MacroResolver INSTANCE;
	
	private List<Macro> macros;
	
	private MacroResolver() {
		macros = new ArrayList<Macro>();
		macros.add(new WeekMacro());
		macros.add(new YearMacro());
		macros.add(new Year2DMacro());
		macros.add(new MonthMacro());
		macros.add(new MonthStrMacro());
		macros.add(new DayMacro());
		macros.add(new DayStrMacro());
		macros.add(new DateMacro());
		macros.add(new DateHyphenMacro());
		macros.add(new TimeMacro());
		macros.add(new TimeHyphenMacro());
	}
	
	public static MacroResolver getInstance() {
		if (MacroResolver.INSTANCE == null) {
			MacroResolver.INSTANCE = new MacroResolver();
		}
		return MacroResolver.INSTANCE;
	}
	
	/**
	 * Resolves a string containing macros by replacing each occurence of a 
	 * known macro.
	 * 
	 * @param macroString the unresolved string
	 * @return the resolved string
	 */
	public String resolve(String macroString) {
		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("resolving macros in '" + macroString + "'");
		}
		// extract pv names
		Pattern p = Pattern.compile("\\$\\{PV\\:[^\\}\\$]*\\}");
		Matcher m = p.matcher(macroString);
		final List<String> pvmacros = new ArrayList<String>();
		while (m.find()) {
			pvmacros.add(m.group());
		}
		
		// connect pvs
		final List<MacroPV> pvs = new ArrayList<>();
		for (String s : pvmacros) {
			pvs.add(new MacroPV(s));
		}
		;
		
		// add non-pv macros
		List<String> searchList = new ArrayList<>();
		List<String> replacementList = new ArrayList<>();
		for (Macro macro : this.macros) {
			searchList.add(macro.getName());
			replacementList.add(macro.resolve());
		}
		
		int loops = 0;
		final int max_loops = 30;
		
		while (loops < max_loops) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				LOGGER.warn(e.getMessage(), e);
			}
			if (allResolved(pvs)) {
				break;
			}
			loops++;
		}
		if (!pvs.isEmpty()) {
			if (loops < max_loops) {
				LOGGER.info("PVs read. Resolving macros.");
			} else {
				LOGGER.warn("One or more PVs could not be read and will not be resolved.");
			}
		}
		for (MacroPV macroPV: pvs) {
			if (!macroPV.isResolved()) {
				macroPV.disconnect();
			}
			searchList.add(macroPV.getName());
			replacementList.add(macroPV.resolve());
		}
		
		String resolvedString = StringUtil.replaceEach(macroString, searchList, replacementList);
		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("resolved string is: '" + resolvedString + "'");
		}
		return resolvedString;
	}
	
	/*
	 * 
	 */
	private boolean allResolved(List<MacroPV> macropvlist) {
		for (MacroPV macroPV : macropvlist) {
			if (!macroPV.isResolved()) {
				return false;
			}
		}
		return true;
	}
	
	/**
	 * Returns a new list containing all macros of MacroResolver.
	 * @return a new list containing all macros
	 */
	public List<Macro> getMacros() {
		return new ArrayList<Macro>(this.macros);
	}
}
{'fraction_non_alphanumeric': 0.145571608040201, 'fraction_numerical': 0.003454773869346734, 'mean_word_length': 3.5234375, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 31, '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': '5222347', 'n_tokens_mistral': 2554, 'n_tokens_neox': 2178, 'n_words': 500}