<commit_before>/*
 * JaamSim Discrete Event Simulation
 * Copyright (C) 2013 Ausenco Engineering Canada Inc.
 *
 * This program 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.
 *
 * This program is 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.
 */
package com.jaamsim.CalculationObjects;

import com.jaamsim.input.Keyword;
import com.sandwell.JavaSimulation.IntegerInput;

/**
 * The MovingAverage block returns the average of the current input and the N-1 previous inputs.
 * Output(i) = 1/N * [ Input(i) + Input(i-1) + ... + Input(i-N+1) ]
 * where Input(i) = input value to the block for the i-th update,
 * and Output(i) = output value from the block for the i-th update.
 * @author <NAME>
 *
 */
public class MovingAverage extends DoubleCalculation {

	@Keyword(description = "The number of input values over which to average.",
	         example = "MovingAverage-1 NumberOfSamples { 10 }")
	private final IntegerInput numberOfSamplesInput;

	private double[] samples;  // The previous input values over which to average
	private int index;  // The next index to overwrite (the oldest value on the list)
	private int n;  // The number of inputs values over which to average

	{
		numberOfSamplesInput = new IntegerInput( "NumberOfSamples", "Key Inputs", 1);
		numberOfSamplesInput.setValidRange( 1, Integer.MAX_VALUE);
		this.addInput( numberOfSamplesInput, true);
	}

	@Override
	public void earlyInit() {
		super.earlyInit();
		samples = new double[ numberOfSamplesInput.getValue() ];
		index = 0;
		n = numberOfSamplesInput.getValue();
	}

	@Override
	public void update(double simTime) {
		// Overwrite the oldest value in the list
		samples[index] = this.getInputValue(simTime);

		// Set the index to the next oldest value<commit_msg>JS: Rename input objects for MovingAverage<commit_after>/*
 * JaamSim Discrete Event Simulation
 * Copyright (C) 2013 Ausenco Engineering Canada Inc.
 *
 * This program 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.
 *
 * This program is 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.
 */
package com.jaamsim.CalculationObjects;

import com.jaamsim.input.Keyword;
import com.sandwell.JavaSimulation.IntegerInput;

/**
 * The MovingAverage block returns the average of the current input and the N-1 previous inputs.
 * Output(i) = 1/N * [ Input(i) + Input(i-1) + ... + Input(i-N+1) ]
 * where Input(i) = input value to the block for the i-th update,
 * and Output(i) = output value from the block for the i-th update.
 * @author <NAME>
 *
 */
public class MovingAverage extends DoubleCalculation {

	@Keyword(description = "The number of input values over which to average.",
	         example = "MovingAverage-1 NumberOfSamples { 10 }")
	private final IntegerInput numberOfSamples;

	private double[] samples;  // The previous input values over which to average
	private int index;  // The next index to overwrite (the oldest value on the list)
	private int n;  // The number of inputs values over which to average

	{
		numberOfSamples = new IntegerInput( "NumberOfSamples", "Key Inputs", 1);
		numberOfSamples.setValidRange( 1, Integer.MAX_VALUE);
		this.addInput( numberOfSamples, true);
	}

	@Override
	public void earlyInit() {
		super.earlyInit();
		samples = new double[ numberOfSamples.getValue() ];
		index = 0;
		n = numberOfSamples.getValue();
	}

	@Override
	public void update(double simTime) {
		// Overwrite the oldest value in the list
		samples[index] = this.getInputValue(simTime);

		// Set the index to the next oldest value
{'fraction_non_alphanumeric': 0.08634965696711616, 'fraction_numerical': 0.007097232079488999, 'mean_word_length': 3.9858490566037736, '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': '137582', 'n_tokens_mistral': 1249, 'n_tokens_neox': 1156, 'n_words': 502}