<commit_before>/*
 * Copyright 2007 <NAME>, <NAME>, <NAME>
 *
 * 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.jamocha.org/
 *
 * 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 org.jamocha.rete.functions.ruleengine;

import java.io.Serializable;
import java.io.StringReader;

import org.jamocha.parser.EvaluationException;
import org.jamocha.parser.Expression;
import org.jamocha.parser.IllegalParameterException;
import org.jamocha.parser.JamochaType;
import org.jamocha.parser.JamochaValue;
import org.jamocha.parser.ParseException;
import org.jamocha.parser.Parser;
import org.jamocha.parser.ParserFactory;
import org.jamocha.rete.BoundParam;
import org.jamocha.rete.Function;
import org.jamocha.rete.Parameter;
import org.jamocha.rete.Rete;
import org.jamocha.rete.functions.FunctionDescription;

/**
 * @author <NAME>, <NAME>
 * 
 * Evaluates the string given in the first argument as though it was entered at the command
 * prompt and returns the last result of the expression(s) (if any). An optional binding as 
 * second argument can be used to catch an exception and hold the error message.
 */
public class Eval implements Function, Serializable {

	private static final class Description implements FunctionDescription {

		public String getDescription() {
			return "Evaluates the string given in the first argument as though it was entered at the command " +
					"prompt and returns the last result of the expression(s) (if any). An optional binding as " +
					"second argument can be used to catch an exception and hold the error message.";
		}

		public int getParameterCount() {
			return 2;
		}

		public String getParameterDescription(int parameter) {
			switch (parameter) {
			case 0:
				return "One or more commands in one string.";
			case 1:
				return "Optional binding to hold the error message if an exception occurres.";
			}
			return "";
		}

		public String getParameterName(int parameter) {
			switch (parameter) {
			case 0:
				return "command";
			case 1:
				return "errorBinding";
			}
			return "";
		}

		public JamochaType[] getParameterTypes(int parameter) {
			switch (parameter) {
			case 0:
				return JamochaType.STRINGS;
			case 1:
				return JamochaType.IDENTIFIERS;
			}
			return JamochaType.NONE;
		}

		public JamochaType[] getReturnType() {
			return JamochaType.ANY;
		}

		public boolean isParameterCountFixed() {
			return false;
		}

		public boolean isParameterOptional(int parameter) {
			switch (parameter) {
			case 0:
				return false;
			case 1:
				return true;
			}
			return true;
		}

		public String getExample() {			
			return "(eval \"(printout t BlackJack)\" ?catchErr)\n\n" +
					"(bind ?x \"(+ 17 4)\")\n" +
					"(eval ?x)";
		}
	}

	private static final FunctionDescription DESCRIPTION = new Description();

	private static final long serialVersionUID = 1L;

	public static final String NAME = "eval";

	public FunctionDescription getDescription() {
		return DESCRIPTION;
	}

	public String getName() {
		return NAME;
	}

	public JamochaValue executeFunction(Rete engine, Parameter[] params)
			throws EvaluationException {
		JamochaValue result = JamochaValue.NIL;
		if (params != null && params.length >= 1) {
			String command = params[0].getValue(engine).getStringValue();
			String bindName = null;
			// if an additionall Binding is provided we reset it
			if (params.length > 1 && params[1] instanceof BoundParam) {
				bindName = ((BoundParam) params[1]).getVariableName();
				engine.setBinding(bindName, JamochaValue.NIL);<commit_msg>adapted example<commit_after>/*
 * Copyright 2007 <NAME>, <NAME>, <NAME>
 *
 * 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.jamocha.org/
 *
 * 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 org.jamocha.rete.functions.ruleengine;

import java.io.Serializable;
import java.io.StringReader;

import org.jamocha.parser.EvaluationException;
import org.jamocha.parser.Expression;
import org.jamocha.parser.IllegalParameterException;
import org.jamocha.parser.JamochaType;
import org.jamocha.parser.JamochaValue;
import org.jamocha.parser.ParseException;
import org.jamocha.parser.Parser;
import org.jamocha.parser.ParserFactory;
import org.jamocha.rete.BoundParam;
import org.jamocha.rete.Function;
import org.jamocha.rete.Parameter;
import org.jamocha.rete.Rete;
import org.jamocha.rete.functions.FunctionDescription;

/**
 * @author <NAME>, <NAME>
 * 
 * Evaluates the string given in the first argument as though it was entered at the command
 * prompt and returns the last result of the expression(s) (if any). An optional binding as 
 * second argument can be used to catch an exception and hold the error message.
 */
public class Eval implements Function, Serializable {

	private static final class Description implements FunctionDescription {

		public String getDescription() {
			return "Evaluates the string given in the first argument as though it was entered at the command " +
					"prompt and returns the last result of the expression(s) (if any). An optional binding as " +
					"second argument can be used to catch an exception and hold the error message.";
		}

		public int getParameterCount() {
			return 2;
		}

		public String getParameterDescription(int parameter) {
			switch (parameter) {
			case 0:
				return "One or more commands in one string.";
			case 1:
				return "Optional binding to hold the error message if an exception occurres.";
			}
			return "";
		}

		public String getParameterName(int parameter) {
			switch (parameter) {
			case 0:
				return "command";
			case 1:
				return "errorBinding";
			}
			return "";
		}

		public JamochaType[] getParameterTypes(int parameter) {
			switch (parameter) {
			case 0:
				return JamochaType.STRINGS;
			case 1:
				return JamochaType.IDENTIFIERS;
			}
			return JamochaType.NONE;
		}

		public JamochaType[] getReturnType() {
			return JamochaType.ANY;
		}

		public boolean isParameterCountFixed() {
			return false;
		}

		public boolean isParameterOptional(int parameter) {
			switch (parameter) {
			case 0:
				return false;
			case 1:
				return true;
			}
			return true;
		}

		public String getExample() {			
			return "(eval \"(printout t BlackJack)\" ?catchErr)\n" +
					"(eval \"(printout t \\\"This is BlackJack.\\\")\")\n\n" +
					"(bind ?x \"(+ 17 4)\")\n" +
					"(eval ?x)";
		}
	}

	private static final FunctionDescription DESCRIPTION = new Description();

	private static final long serialVersionUID = 1L;

	public static final String NAME = "eval";

	public FunctionDescription getDescription() {
		return DESCRIPTION;
	}

	public String getName() {
		return NAME;
	}

	public JamochaValue executeFunction(Rete engine, Parameter[] params)
			throws EvaluationException {
		JamochaValue result = JamochaValue.NIL;
		if (params != null && params.length >= 1) {
			String command = params[0].getValue(engine).getStringValue();
			String bindName = null;
			// if an additionall Binding is provided we reset it
			if (params.length > 1 && params[1] instanceof BoundParam) {
				bindName = ((BoundParam) params[1]).getVariableName();
				engine.setBinding(bindName, JamochaValue.NIL);
{'fraction_non_alphanumeric': 0.09413386813737779, 'fraction_numerical': 0.006016545500125345, 'mean_word_length': 3.772129186602871, 'pattern_counts': {'":': 0, '<': 13, '<?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': '7317735', 'n_tokens_mistral': 2683, 'n_tokens_neox': 2216, 'n_words': 816}