MessageSelector.java

/* ========================================================================
 * PlantUML : a free UML diagram generator
 * ========================================================================
 *
 * (C) Copyright 2009-2024, Arnaud Roques
 *
 * Project Info:  https://plantuml.com
 * 
 * If you like this project or if you find it useful, you can support us at:
 * 
 * https://plantuml.com/patreon (only 1$ per month!)
 * https://plantuml.com/paypal
 * 
 * This file is part of PlantUML.
 *
 * PlantUML is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * PlantUML distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 *
 *
 * Original Author:  Arnaud Roques
 *
 */
package net.sourceforge.plantuml.error.messages;

import java.util.function.DoubleSupplier;

import net.sourceforge.plantuml.core.UmlSource;
import net.sourceforge.plantuml.log.Logme;
import net.sourceforge.plantuml.teavm.TeaVM;
import net.sourceforge.plantuml.version.LicenseInfo;

/**
 * Chooses the message to show in an error diagram. See
 * {@code docs/MESSAGES_ERREUR.md}, "Algorithme de sélection".
 */
public final class MessageSelector {

	private static final DoubleSupplier RANDOM = Math::random;

	// Vega only: the moment written in the YAML header of the test file
	private static final ThreadLocal<MessageMoment> VEGA_CLOCK = new ThreadLocal<>();

	private static MessagesConfig config;

	private MessageSelector() {
	}

	// Loaded once: the problems of the file are logged once
	private static synchronized MessagesConfig getConfig() {
		if (config == null)
			config = MessagesLoader.loadDefault();

		return config;
	}

	/**
	 * Vega only: the moment the messages are chosen for, in the current thread, as
	 * {@code "2026-12-02 10:15"}. It only has an effect in {@link Logme#MODE_VEGA},
	 * where there is no message at all without it: it can add a message, never
	 * remove one.
	 *
	 * @throws IllegalArgumentException when the moment is not written as expected
	 */
	public static void setVegaClock(String moment) {
		VEGA_CLOCK.set(MessageMoment.parse(moment));
	}

	public static void clearVegaClock() {
		VEGA_CLOCK.remove();
	}

	/**
	 * The rule whose message must be shown now in the error diagram of this source,
	 * or null.
	 *
	 * Always null under TeaVM. In Vega mode, null unless a Vega clock is set: then
	 * the rule of that moment, whatever the license, and every probability is won,
	 * so that the reference files are the same on every machine. Otherwise null
	 * with a valid license, and the rule of the local time if any.
	 */
	public static MessageRule selectNow(UmlSource source) {
		if (TeaVM.isTeaVM())
			return null;

		if (Logme.MODE_VEGA) {
			final MessageMoment vegaClock = VEGA_CLOCK.get();
			if (vegaClock == null)
				return null;

			return select(getConfig(), vegaClock, source, () -> 0.0);
		}

		if (LicenseInfo.retrieveNamedOrDistributorQuickIsValid())
			return null;

		return select(getConfig(), MessageMoment.now(), source, RANDOM);
	}

	/**
	 * The first rule whose conditions match and whose probability is drawn; a rule
	 * that matches but loses the draw lets the next ones try.
	 *
	 * @param random numbers in [0, 1)
	 */
	static MessageRule select(MessagesConfig config, MessageMoment now, UmlSource source, DoubleSupplier random) {
		for (MessageRule rule : config.getRules())
			if (rule.matches(now, source) && draw(rule.getProbability(), random))
				return rule;

		return null;
	}

	private static boolean draw(double probability, DoubleSupplier random) {
		if (probability >= 1.0)
			return true;

		if (probability <= 0.0)
			return false;

		return random.getAsDouble() < probability;
	}

}