<commit_before>/**
   Copyright 2016, <NAME>

   This file is part of Yadrol.

   Yadrol 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.

   Yadrol 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.

   You should have received a copy of the GNU General Public License
   along with Yadrol.  If not, see <http://www.gnu.org/licenses/>.
**/

package org.phatonin.yadrol.core.expressions;

import java.util.List;
import java.util.Map;

import org.phatonin.yadrol.core.EvaluationContext;
import org.phatonin.yadrol.core.EvaluationException;
import org.phatonin.yadrol.core.Expression;
import org.phatonin.yadrol.core.ExpressionStringer;
import org.phatonin.yadrol.core.Location;
import org.phatonin.yadrol.core.Precedence;
import org.phatonin.yadrol.core.Scope;
import org.phatonin.yadrol.core.values.ContainerVisitor;
import org.phatonin.yadrol.core.values.Function;

/**
 * <code>count container</code>
 * 
 *
 */
public class Count extends AbstractIntegerExpression {
	private final Expression container;

	public Count(Location location, Expression container) {
		super(location);
		this.container = container;
	}

	public Expression getContainer() {
		return container;
	}

	@Override
	public AbstractIntegerExpression reduce() throws EvaluationException {
		if (isPureConstant()) {
			return pureExpression();
		}
		return new Count(getLocation(), container.reduce());
	}

	@Override
	public boolean isPureConstant() throws EvaluationException {
		return container.isPureConstant();
	}

	@Override
	public Expression substituteVariables(Scope scope) {
		return new Count(getLocation(), container.substituteVariables(scope));
	}

	@Override
	public long evaluateInteger(EvaluationContext ctx, Scope scope) throws EvaluationException {
		try {
			Object container = this.container.evaluateList(ctx, scope);
			return new CountVisitor().visit(container, null);
		}
		catch (EvaluationException e) {
			throw completeStack(e);
		}
	}
	
	private class CountVisitor extends ContainerVisitor<Long,Void,EvaluationException> {
		@Override
		protected Long visitScalar(Object value, Void param) throws RuntimeException {
			return 1L;
		}

		@Override
		public Long visitUndef(Void param) throws RuntimeException {
			return 0L;
		}

		@Override
		public Long visit(List<Object> value, Void param) throws RuntimeException {<commit_msg>fixed bug, container was evaluated as a list<commit_after>/**
   Copyright 2016, <NAME>

   This file is part of Yadrol.

   Yadrol 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.

   Yadrol 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.

   You should have received a copy of the GNU General Public License
   along with Yadrol.  If not, see <http://www.gnu.org/licenses/>.
**/

package org.phatonin.yadrol.core.expressions;

import java.util.List;
import java.util.Map;

import org.phatonin.yadrol.core.EvaluationContext;
import org.phatonin.yadrol.core.EvaluationException;
import org.phatonin.yadrol.core.Expression;
import org.phatonin.yadrol.core.ExpressionStringer;
import org.phatonin.yadrol.core.Location;
import org.phatonin.yadrol.core.Precedence;
import org.phatonin.yadrol.core.Scope;
import org.phatonin.yadrol.core.values.ContainerVisitor;
import org.phatonin.yadrol.core.values.Function;

/**
 * <code>count container</code>
 * 
 *
 */
public class Count extends AbstractIntegerExpression {
	private final Expression container;

	public Count(Location location, Expression container) {
		super(location);
		this.container = container;
	}

	public Expression getContainer() {
		return container;
	}

	@Override
	public AbstractIntegerExpression reduce() throws EvaluationException {
		if (isPureConstant()) {
			return pureExpression();
		}
		return new Count(getLocation(), container.reduce());
	}

	@Override
	public boolean isPureConstant() throws EvaluationException {
		return container.isPureConstant();
	}

	@Override
	public Expression substituteVariables(Scope scope) {
		return new Count(getLocation(), container.substituteVariables(scope));
	}

	@Override
	public long evaluateInteger(EvaluationContext ctx, Scope scope) throws EvaluationException {
		try {
			Object container = this.container.evaluate(ctx, scope);
			return new CountVisitor().visit(container, null);
		}
		catch (EvaluationException e) {
			throw completeStack(e);
		}
	}
	
	private class CountVisitor extends ContainerVisitor<Long,Void,EvaluationException> {
		@Override
		protected Long visitScalar(Object value, Void param) throws RuntimeException {
			return 1L;
		}

		@Override
		public Long visitUndef(Void param) throws RuntimeException {
			return 0L;
		}

		@Override
		public Long visit(List<Object> value, Void param) throws RuntimeException {
{'fraction_non_alphanumeric': 0.08325641498984677, 'fraction_numerical': 0.002584456341148237, 'mean_word_length': 4.574074074074074, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '681165', 'n_tokens_mistral': 1634, 'n_tokens_neox': 1487, 'n_words': 491}