<commit_before>/*
 * Copyright (c) 2013 University of Tartu
 */
package org.jpmml.cascading;

import java.util.*;

import cascading.flow.*;
import cascading.flow.planner.*;
import cascading.pipe.*;
import cascading.tuple.*;

import org.jpmml.evaluator.*;

public class PMMLPlanner implements AssemblyPlanner {

	private String branchName = "pmml";

	private Evaluator evaluator = null;


	public PMMLPlanner(Evaluator evaluator){
		setEvaluator(evaluator);
	}

	@Override
	public List<Pipe> resolveTails(Context context){
		List<Pipe> tails = context.getTails();

		Pipe tail = null;

		String headName = findHeadName(context);

		if(tails.size() == 0){

			if(headName != null){
				tail = new Pipe(headName);
			}
		} else

		if(tails.size() == 1){

			if(headName != null){
				throw new PlannerException();
			}

			tail = tails.get(0);
		} else

		{
			throw new PlannerException();
		}

		String tailName = findTailName(context);

		tail = new Pipe(tailName, resolveAssembly(tail));

		return Collections.singletonList(tail);
	}

	private Pipe resolveAssembly(Pipe pipe){
		Pipe tail;

		if(pipe == null){
			tail = new Pipe(getBranchName());
		} else

		{
			tail = new Pipe(getBranchName(), pipe);
		}

		Evaluator evaluator = getEvaluator();

		Fields argumentFields = FieldsUtil.getActiveFields(evaluator);
		Fields outputFields = (FieldsUtil.getPredictedFields(evaluator)).append(FieldsUtil.getOutputFields(evaluator));

		PMMLFunction function = new PMMLFunction(outputFields, evaluator);

		tail = new Each(tail, argumentFields, function, outputFields);

		return tail;
	}

	private String findHeadName(Context context){
		Flow<?> flow = context.getFlow();

		List<String> sourceNames = flow.getSourceNames();
		if(sourceNames.size() != 1){
			throw new PlannerException();
		}

		return sourceNames.get(0);
	}

	private String findTailName(Context context){
		Flow<?> flow = context.getFlow();

		List<String> sinkNames = flow.getSinkNames();
		if(sinkNames.size() != 1){
			throw new PlannerException();
		}

		return sinkNames.get(0);
	}

	public String getBranchName(){
		return this.branchName;
	}

	public void setBranchName(String branchName){

		if(branchName == null){
			throw new NullPointerException();
		}

		this.branchName = branchName;
	}

	public Evaluator getEvaluator(){
		return this.evaluator;
	}

	private void setEvaluator(Evaluator evaluator){

		if(evaluator == null){
			throw new NullPointerException();
		}

		this.evaluator = evaluator;
	}<commit_msg>Choosing the source and the sink by name<commit_after>/*
 * Copyright (c) 2013 University of Tartu
 */
package org.jpmml.cascading;

import java.util.*;

import cascading.flow.*;
import cascading.flow.planner.*;
import cascading.pipe.*;
import cascading.tuple.*;

import org.jpmml.evaluator.*;

public class PMMLPlanner implements AssemblyPlanner {

	private Evaluator evaluator = null;

	private String branchName = "pmml";

	private String headName = null;

	private String tailName = null;


	public PMMLPlanner(Evaluator evaluator){
		setEvaluator(evaluator);
	}

	@Override
	public List<Pipe> resolveTails(Context context){
		List<Pipe> tails = context.getTails();

		Pipe tail = null;

		String headName = getHeadName();
		if(headName == null){
			headName = findHeadName(context);
		}

		if(tails.size() == 0){

			if(headName != null){
				tail = new Pipe(headName);
			}
		} else

		if(tails.size() == 1){

			if(headName != null){
				throw new PlannerException();
			}

			tail = tails.get(0);
		} else

		{
			throw new PlannerException();
		}

		String tailName = getTailName();
		if(tailName == null){
			tailName = findTailName(context);
		}

		tail = new Pipe(tailName, resolveAssembly(tail));

		return Collections.singletonList(tail);
	}

	private Pipe resolveAssembly(Pipe pipe){
		Pipe tail;

		if(pipe == null){
			tail = new Pipe(getBranchName());
		} else

		{
			tail = new Pipe(getBranchName(), pipe);
		}

		Evaluator evaluator = getEvaluator();

		Fields argumentFields = FieldsUtil.getActiveFields(evaluator);
		Fields outputFields = (FieldsUtil.getPredictedFields(evaluator)).append(FieldsUtil.getOutputFields(evaluator));

		PMMLFunction function = new PMMLFunction(outputFields, evaluator);

		tail = new Each(tail, argumentFields, function, outputFields);

		return tail;
	}

	private String findHeadName(Context context){
		Flow<?> flow = context.getFlow();

		List<String> sourceNames = flow.getSourceNames();
		if(sourceNames.size() != 1){
			throw new PlannerException("Too many sources to choose from: " + sourceNames);
		}

		return sourceNames.get(0);
	}

	private String findTailName(Context context){
		Flow<?> flow = context.getFlow();

		List<String> sinkNames = flow.getSinkNames();
		if(sinkNames.size() != 1){
			throw new PlannerException("Too many sinks to choose from: " + sinkNames);
		}

		return sinkNames.get(0);
	}

	public Evaluator getEvaluator(){
		return this.evaluator;
	}

	private void setEvaluator(Evaluator evaluator){

		if(evaluator == null){
			throw new NullPointerException();
		}

		this.evaluator = evaluator;
	}

	public String getBranchName(){
		return this.branchName;
	}

	public void setBranchName(String branchName){

		if(branchName == null){
			throw new NullPointerException();
		}

		this.branchName = branchName;
	}

	public String getHeadName(){
		return this.headName;
	}

	/**
	 * Sets the name of the incoming source.
	 *
	 * This attribute is mandatory when more than one sources have been declared.
	 */
	public void setHeadName(String headName){
		this.headName = headName;
	}

	public String getTailName(){
		return this.tailName;
	}

	/**
	 * Sets the name of the outgoing sink.
	 *
	 * This attribute is mandatory when more than one sinks have been declared.
	 */
	public void setTailName(String tailName){
		this.tailName = tailName;
	}
{'fraction_non_alphanumeric': 0.1199106682700567, 'fraction_numerical': 0.0037794193437553684, 'mean_word_length': 4.115992970123023, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '3308463', 'n_tokens_mistral': 2108, 'n_tokens_neox': 1852, 'n_words': 467}