<commit_before>package org.pikater.shared.experiment.webformat.server;

import java.util.LinkedHashSet;
import java.util.Set;

import org.pikater.core.CoreConstants;
import org.pikater.core.ontology.subtrees.agentInfo.Slot;
import org.pikater.shared.experiment.webformat.server.BoxSlot.SlotType;

public class ExperimentGraphValidator
{
	private final ExperimentGraphServer experimentGraph;
	private final Set<String> problems;
	
	public ExperimentGraphValidator(ExperimentGraphServer experimentGraph)
	{
		this.experimentGraph = experimentGraph;
		this.problems = new LinkedHashSet<String>();
	}
	
	//-----------------------------------------------------------
	// BASIC PUBLIC INTERFACE
	
	public boolean problemsFound()
	{
		return !this.problems.isEmpty();
	}
	
	public Set<String> getProblems()
	{
		return problems;
	}
	
	public void clear()
	{
		this.problems.clear();
	}
	
	//-----------------------------------------------------------
	// BASIC PRIVATE INTERFACE
	
	private void registerValidationProblem(String problem)
	{
		this.problems.add(problem);
	}
	
	// ------------------------------------------------------------------
	// VALIDATION INTERFACE
	
	public void validate()
	{
		// clean the instance just in case so that old problems are not effective anymore
		clear();
		
		// first check boxes
		for(BoxInfoServer box : experimentGraph)
		{
			if(box.isRegistered()) // only inspect registered boxes
			{
				/*
				 * A check whether the box is approved is needed. Currently, this is done
				 * in core system and only approved agents are sent to web.
				 */
				if(experimentGraph.edgesDefinedFor(box.getID()))
				{
					if(!experimentGraph.getSlotConnections().hasBoxSlotConnections(box))
					{
						// the box is not isolated but has no valid slot connections defined
						registerValidationProblem(String.format("At least one of '%s' boxes has no slot connections defined. Isolated boxes are not allowed.", 
								box.getAssociatedAgent().getName()));
					}
					else
					{
						switch(box.getBoxType())
						{
							case INPUT:
								validateBoxSlot(box, SlotType.OUTPUT, CoreConstants.SLOT_FILE_DATA); // potentially duplicate check but it is needed
								break;
							case CHOOSE:
								break;
							case COMPOSITE:
								break;
							case COMPUTE:
								validateBoxSlot(box, SlotType.INPUT, CoreConstants.SLOT_TRAINING_DATA); // potentially duplicate check but it is needed
								validateBoxSlot(box, SlotType.INPUT, CoreConstants.SLOT_TESTING_DATA); // potentially duplicate check but it is needed
								break;
							case MISC:
								break;
							case OPTION:
								break;
							case OUTPUT:
								validateBoxSlot(box, SlotType.INPUT, CoreConstants.SLOT_FILE_DATA); // potentially duplicate check but it is needed
								break;
							case PROCESS_DATA:
								break;
							case SEARCH:
								break;
							default:
								throw new IllegalStateException("Unknown state: " + box.getBoxType().name());
						}
					}
				}
				else
				{
					// the box is isolated, not connected to anything whatsoever
					registerValidationProblem(String.format("At least one of '%s' boxes is isolated. Isolated boxes are not allowed.", 
							box.getAssociatedAgent().getName()));
				}
			}
		}
		
		// then check edges
		/*
		for(BoxInfoServer boxFrom : experimentGraph)
		{
			for(BoxInfoServer boxTo : experimentGraph.getFromNeighbours(boxFrom.getID()))
			{
				if(boxFrom.isRegistered() != boxTo.isRegistered())
				{
					registerValidationProblem(String.format("At least one of '%s' boxes doesn't have the '%s' slot connected."));
				}
			}
		}
		*/
	}
	
	private void validateBoxSlot(BoxInfoServer box, SlotType type, String slotDataType)
	{
		Slot slotToCheck;
		if(type == SlotType.INPUT)
		{
			slotToCheck = box.getAssociatedAgent().fetchInputSlotByDataType(slotDataType);
		}
		else
		{
			slotToCheck = box.getAssociatedAgent().fetchOutputSlotByDataType(slotDataType);	
		}
		if(!experimentGraph.getSlotConnections().isSlotConnectedToAValidEndpoint(slotToCheck))
		{
			registerValidationProblem(String.format("At least one of '%s' boxes doesn't have the '%s' slot connected.", 
					box.getAssociatedAgent().getName(), CoreConstants.SLOT_FILE_DATA));
		}
	}
}<commit_msg>Mockup validation bug fixes<commit_after>package org.pikater.shared.experiment.webformat.server;

import java.util.LinkedHashSet;
import java.util.Set;

import org.pikater.core.CoreConstants;
import org.pikater.core.ontology.subtrees.agentInfo.Slot;
import org.pikater.shared.experiment.webformat.server.BoxSlot.SlotType;

public class ExperimentGraphValidator
{
	private final ExperimentGraphServer experimentGraph;
	private final Set<String> problems;
	
	public ExperimentGraphValidator(ExperimentGraphServer experimentGraph)
	{
		this.experimentGraph = experimentGraph;
		this.problems = new LinkedHashSet<String>();
	}
	
	//-----------------------------------------------------------
	// BASIC PUBLIC INTERFACE
	
	public boolean problemsFound()
	{
		return !this.problems.isEmpty();
	}
	
	public Set<String> getProblems()
	{
		return problems;
	}
	
	public void clear()
	{
		this.problems.clear();
	}
	
	//-----------------------------------------------------------
	// BASIC PRIVATE INTERFACE
	
	private void registerValidationProblem(String problem)
	{
		this.problems.add(problem);
	}
	
	// ------------------------------------------------------------------
	// VALIDATION INTERFACE
	
	public void validate()
	{
		// clean the instance just in case so that old problems are not effective anymore
		clear();
		
		// first check boxes
		for(BoxInfoServer box : experimentGraph)
		{
			if(box.isRegistered()) // only inspect registered boxes
			{
				/*
				 * A check whether the box is approved is needed. Currently, this is done
				 * in core system and only approved agents are sent to web.
				 */
				if(!experimentGraph.getSlotConnections().hasBoxSlotConnections(box))
				{
					// the box is not isolated but has no valid slot connections defined
					registerValidationProblem(String.format("At least one of '%s' boxes has no slot connections defined. Isolated boxes are not allowed.", 
							box.getAssociatedAgent().getName()));
				}
				else
				{
					switch(box.getBoxType())
					{
						case INPUT:
							validateBoxSlot(box, SlotType.OUTPUT, CoreConstants.SLOT_FILE_DATA); // potentially duplicate check but it is needed
							break;
						case CHOOSE:
							break;
						case COMPOSITE:
							break;
						case COMPUTE:
							validateBoxSlot(box, SlotType.INPUT, CoreConstants.SLOT_TRAINING_DATA); // potentially duplicate check but it is needed
							validateBoxSlot(box, SlotType.INPUT, CoreConstants.SLOT_TESTING_DATA); // potentially duplicate check but it is needed
							break;
						case MISC:
							break;
						case OPTION:
							break;
						case OUTPUT:
							validateBoxSlot(box, SlotType.INPUT, CoreConstants.SLOT_FILE_DATA); // potentially duplicate check but it is needed
							break;
						case PROCESS_DATA:
							break;
						case SEARCH:
							break;
						default:
							throw new IllegalStateException("Unknown state: " + box.getBoxType().name());
					}
				}
			}
		}
		
		// then check edges
		/*
		for(BoxInfoServer boxFrom : experimentGraph)
		{
			for(BoxInfoServer boxTo : experimentGraph.getFromNeighbours(boxFrom.getID()))
			{
				if(boxFrom.isRegistered() != boxTo.isRegistered())
				{
					registerValidationProblem(String.format("At least one of '%s' boxes doesn't have the '%s' slot connected."));
				}
			}
		}
		*/
	}
	
	private void validateBoxSlot(BoxInfoServer box, SlotType type, String slotDataType)
	{
		Slot slotToCheck;
		if(type == SlotType.INPUT)
		{
			slotToCheck = box.getAssociatedAgent().fetchInputSlotByDataType(slotDataType);
		}
		else
		{
			slotToCheck = box.getAssociatedAgent().fetchOutputSlotByDataType(slotDataType);	
		}
		if(!experimentGraph.getSlotConnections().isSlotConnectedToAValidEndpoint(slotToCheck))
		{
			registerValidationProblem(String.format("At least one of '%s' boxes doesn't have the '%s' slot connected.", 
					box.getAssociatedAgent().getName(), slotDataType));
		}
	}
}
{'fraction_non_alphanumeric': 0.13847287012030443, 'fraction_numerical': 0.0, 'mean_word_length': 2.7371559633027522, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '5714427', 'n_tokens_mistral': 2953, 'n_tokens_neox': 2275, 'n_words': 570}