<commit_before>/*
 * JaamSim Discrete Event Simulation
 * Copyright (C) 2014 Ausenco Engineering Canada Inc.
 * Copyright (C) 2018 JaamSim Software Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jaamsim.states;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map.Entry;

import com.jaamsim.Graphics.DisplayEntity;
import com.jaamsim.basicsim.Entity;
import com.jaamsim.basicsim.FileEntity;
import com.jaamsim.events.EventManager;
import com.jaamsim.input.BooleanInput;
import com.jaamsim.input.ColourInput;
import com.jaamsim.input.InputAgent;
import com.jaamsim.input.Keyword;
import com.jaamsim.input.Output;
import com.jaamsim.input.StringKeyInput;
import com.jaamsim.input.StringListInput;
import com.jaamsim.math.Color4d;
import com.jaamsim.units.DimensionlessUnit;
import com.jaamsim.units.TimeUnit;

public abstract class StateEntity extends DisplayEntity implements StateUser {

	@Keyword(description = "A list of state/DisplayEntity pairs. For each state, the graphics "
	                     + "will be changed to those for the corresponding DisplayEntity.",
	         exampleList = {"{ idle DisplayEntity1 } { working DisplayEntity2 }"})
	protected final StringKeyInput<DisplayEntity> stateGraphics;

	@Keyword(description = "If TRUE, a log file (.trc) will be printed with the time of every state change during the run.",
	         exampleList = {"TRUE"})
	private final BooleanInput traceState;

	@Keyword(description = "A list of states for which the entity is considered working.",
		     exampleList = "'Transit - Seg1L' 'Transit - Seg1B'")
	protected final StringListInput workingStateListInput;

	private StateRecord presentState; // The present state of the entity
	private final HashMap<String, StateRecord> states;
	private final ArrayList<StateEntityListener> stateListeners;

	private long lastStateCollectionTick;
	private long workingTicks;
	private boolean useCurrentCycle;

	protected FileEntity stateReportFile;        // The file to store the state information

	protected static final String STATE_IDLE = "Idle";
	protected static final String STATE_WORKING = "Working";
	protected static final String STATE_INACTIVE = "Inactive";

	protected static final Color4d COL_IDLE = ColourInput.LIGHT_GREY;
	protected static final Color4d COL_WORKING = ColourInput.GREEN;
	protected static final Color4d COL_INACTIVE = ColourInput.WHITE;

	{
		stateGraphics = new StringKeyInput<>(DisplayEntity.class, "StateGraphics", KEY_INPUTS);
		stateGraphics.setHidden(true);
		this.addInput(stateGraphics);

		traceState = new BooleanInput("TraceState", KEY_INPUTS, false);
		traceState.setHidden(true);
		this.addInput(traceState);

		workingStateListInput = new StringListInput("WorkingStateList", MAINTENANCE, new ArrayList<String>(0));
		this.addInput(workingStateListInput);
	}

	public StateEntity() {
		states = new HashMap<>();
		stateListeners = new ArrayList<>();
	}

	@Override
	public void earlyInit() {
		super.earlyInit();

		this.initStateData();

		if (testFlag(FLAG_GENERATED))
			return;

		// Create state trace file if required
		if (traceState.getValue()) {
			String fileName = InputAgent.getReportFileName(InputAgent.getRunName() + "-" + this.getName() + ".trc");
			stateReportFile = new FileEntity( fileName);
		}<commit_msg>JS: move the 'StateGraphics' keyword to the 'Graphics' category<commit_after>/*
 * JaamSim Discrete Event Simulation
 * Copyright (C) 2014 Ausenco Engineering Canada Inc.
 * Copyright (C) 2018 JaamSim Software Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jaamsim.states;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map.Entry;

import com.jaamsim.Graphics.DisplayEntity;
import com.jaamsim.basicsim.Entity;
import com.jaamsim.basicsim.FileEntity;
import com.jaamsim.events.EventManager;
import com.jaamsim.input.BooleanInput;
import com.jaamsim.input.ColourInput;
import com.jaamsim.input.InputAgent;
import com.jaamsim.input.Keyword;
import com.jaamsim.input.Output;
import com.jaamsim.input.StringKeyInput;
import com.jaamsim.input.StringListInput;
import com.jaamsim.math.Color4d;
import com.jaamsim.units.DimensionlessUnit;
import com.jaamsim.units.TimeUnit;

public abstract class StateEntity extends DisplayEntity implements StateUser {

	@Keyword(description = "A list of state/DisplayEntity pairs. For each state, the graphics "
	                     + "will be changed to those for the corresponding DisplayEntity.",
	         exampleList = {"{ idle DisplayEntity1 } { working DisplayEntity2 }"})
	protected final StringKeyInput<DisplayEntity> stateGraphics;

	@Keyword(description = "If TRUE, a log file (.trc) will be printed with the time of every state change during the run.",
	         exampleList = {"TRUE"})
	private final BooleanInput traceState;

	@Keyword(description = "A list of states for which the entity is considered working.",
		     exampleList = "'Transit - Seg1L' 'Transit - Seg1B'")
	protected final StringListInput workingStateListInput;

	private StateRecord presentState; // The present state of the entity
	private final HashMap<String, StateRecord> states;
	private final ArrayList<StateEntityListener> stateListeners;

	private long lastStateCollectionTick;
	private long workingTicks;
	private boolean useCurrentCycle;

	protected FileEntity stateReportFile;        // The file to store the state information

	protected static final String STATE_IDLE = "Idle";
	protected static final String STATE_WORKING = "Working";
	protected static final String STATE_INACTIVE = "Inactive";

	protected static final Color4d COL_IDLE = ColourInput.LIGHT_GREY;
	protected static final Color4d COL_WORKING = ColourInput.GREEN;
	protected static final Color4d COL_INACTIVE = ColourInput.WHITE;

	{
		stateGraphics = new StringKeyInput<>(DisplayEntity.class, "StateGraphics", GRAPHICS);
		stateGraphics.setHidden(true);
		this.addInput(stateGraphics);

		traceState = new BooleanInput("TraceState", KEY_INPUTS, false);
		traceState.setHidden(true);
		this.addInput(traceState);

		workingStateListInput = new StringListInput("WorkingStateList", MAINTENANCE, new ArrayList<String>(0));
		this.addInput(workingStateListInput);
	}

	public StateEntity() {
		states = new HashMap<>();
		stateListeners = new ArrayList<>();
	}

	@Override
	public void earlyInit() {
		super.earlyInit();

		this.initStateData();

		if (testFlag(FLAG_GENERATED))
			return;

		// Create state trace file if required
		if (traceState.getValue()) {
			String fileName = InputAgent.getReportFileName(InputAgent.getRunName() + "-" + this.getName() + ".trc");
			stateReportFile = new FileEntity( fileName);
		}
{'fraction_non_alphanumeric': 0.08952020202020201, 'fraction_numerical': 0.005303030303030303, 'mean_word_length': 4.8199853049228505, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '365341', 'n_tokens_mistral': 2305, 'n_tokens_neox': 2210, 'n_words': 712}