<commit_before>package org.raspberrypi.cecil.controller;

import java.io.File;
import java.util.ArrayList;

import org.raspberrypi.cecil.model.Model;
import org.raspberrypi.cecil.model.outputstream.ErrorOutputStream;
import org.raspberrypi.cecil.model.outputstream.OutputError;
import org.raspberrypi.cecil.pojo.Program;
import org.raspberrypi.cecil.view.Frame;

/**
 * Controller class for interfacing between the model and view.
 * This class creates an instance of the view (Frame) and model (Cecil) classes.
 * This class contains the main method.
 * 
 * MIT Open Source License
 * @author <NAME> (cj8g10), <NAME> (sa10g10)
 * Southampton University, United Kingdom
 * @version 1.1
 * 
 * @date 11/11/2013
 *
 */
public class Controller implements ControllerInterface {
	private Frame view;
	private Model model;

	/**
	 * Launches the application by creating a new CecilController.
	 */
	public static void main(String[] args) {
		new Controller();
	}


	/**
	 * Constructor which creates a view (Frame) and model (Cecil) object.
	 */
	public Controller() {
		view = new Frame(this);		
		model = new Model();

		view.setInstructionList(model.getInstructions());
		view.setProgramCode(this.getDefaultInput());
	}

	/**
	 * Private method to generate default input editor program
	 * @return Program 
	 */
	private ArrayList<ArrayList<String>> getDefaultInput() {
		ArrayList<ArrayList<String>> program = new ArrayList<ArrayList<String>>();
		ArrayList<String> line = new ArrayList<String>();

		line.add(".start");
		line.add("load");
		line.add("num1");
		line.add(";");
		program.add(line);

		line = new ArrayList<String>();
		line.add(" ");
		line.add("add");
		line.add("num2");
		line.add(";");
		program.add(line);

		line = new ArrayList<String>();
		line.add(" ");
		line.add("printch");
		line.add(" ");
		line.add(";");
		program.add(line);

		line = new ArrayList<String>();
		line.add(" ");
		line.add("printb");
		line.add(" ");
		line.add(";");
		program.add(line);

		line = new ArrayList<String>();
		line.add(" ");
		line.add("xor");
		line.add("num2");
		line.add(";");
		program.add(line);

		line = new ArrayList<String>();
		line.add(" ");
		line.add("printb");
		line.add(" ");
		line.add(";");
		program.add(line);

		line = new ArrayList<String>();
		line.add(" ");
		line.add("xdec");
		line.add(" ");
		line.add(";");
		program.add(line);

		line = new ArrayList<String>();
		line.add(" ");
		line.add("cset");
		line.add(" ");
		line.add(";");
		program.add(line);

		line = new ArrayList<String>();
		line.add(" ");
		line.add("jizero");
		line.add("end");
		line.add(";");
		program.add(line);

		line = new ArrayList<String>();
		line.add(" ");
		line.add("print");
		line.add(" ");
		line.add(";");
		program.add(line);

		line = new ArrayList<String>();
		line.add(".end");
		line.add("stop");
		line.add(" ");
		line.add(";");
		program.add(line);

		line = new ArrayList<String>();
		line.add(".num1");
		line.add("insert");
		line.add("63");
		line.add(";");
		program.add(line);

		line = new ArrayList<String>();
		line.add(".num2");
		line.add("insert");
		line.add("2");
		line.add(";");
		program.add(line);

		return program;
	}

	@Override
	public void compileClicked(ArrayList<ArrayList<String>> code) {
		if(checkCorrectInput(code)) {
			model.compile(new Program(code));
			this.setViewOutput();
		}

		else { 
			model.setToDefault();
			this.setViewOutput();
			view.setConsoleError(model.getErrorStream().getErrors());
		}

	}

	@Override
	public void runClicked() {
		model.run();
		this.setViewOutput();
	}

	@Override
	public void stepThroughClicked() {
		view.highlightStepThrough(model.stepThrough());
		this.setViewOutput();
	}

	public boolean checkCorrectInput(ArrayList<ArrayList<String>> code){
		for(int i = 0; i<code.size(); i++){
			ArrayList<String> input = code.get(i);

			if(input.get(1).equals(" ")){
				if(!input.get(0).equals(" ") || !input.get(2).equals(" ")){
					addErrorToOutputstream(i+1, "An instruction must be provided");
					return false;
				}
			}
			else{

				if(model.isBinaryInstruction(input.get(1))){
					if(input.get(2).equals(" ")){
						addErrorToOutputstream(i+1, "A binary instruction must be succeeded by a datafield");
						System.out.println(model.getErrorStream().getErrors().isEmpty());
						return false;
					}
				}
				if(!model.isBinaryInstruction(input.get(1))){
					if(!input.get(2).equals(" ")){
						addErrorToOutputstream(i+1, "A unary instruction must not be succeeded by a datafield");
						return false;
					}
				}
			}
		}
		return true;
	}

	private void addErrorToOutputstream (int line, String msg){
		ErrorOutputStream e = new ErrorOutputStream();
		e.getErrors().add(new OutputError(line, msg));
		model.setErrorStream(e);
	}

	/**
	 * Refreshes the View output using the current snapshot of the memory
	 */
	private void setViewOutput() {
		if (model.getAcc() != null) {
			ArrayList<String> accStack = new ArrayList<String>();
			for (int i = 0; i < model.getAcc().size(); i++) {
				accStack.add(Integer.toString(model.getAcc().get(i)));
			}
			view.setAccStack(accStack);
		} else {
			view.setAccStack(new ArrayList<String>());
		}

		if (model.getXReg() != null && model.getXReg().size() > 0) {
			ArrayList<String> xStack = new ArrayList<String>();
			for (int i = 0; i < model.getXReg().size(); i++) {
				xStack.add(Integer.toString(model.getXReg().get(i)));
			}
			view.setXStack(xStack);
		} else {
			view.setXStack(new ArrayList<String>());
		}

		if (model.getYReg() != null && model.getYReg().size() > 0) {
			ArrayList<String> yStack = new ArrayList<String>();
			for (int i = 0; i < model.getYReg().size(); i++) {
				yStack.add(Integer.toString(model.getYReg().get(i)));
			}
			view.setYStack(yStack);
		} else {
			view.setYStack(new ArrayList<String>());
		}

		view.setCarryFlag(model.isCarryFlag());
		view.setZeroFlag(model.isZeroFlag());
		view.setNegativeFlag(model.isNegativeFlag());

		view.setMemoryAllocation(model.getMemory());

		if (!model.isCompileSuccess() && model.getErrorStream() != null && model.getErrorStream().getErrors() != null) {
			view.setConsoleError(model.getErrorStream().getErrors());
		} else {
			view.setConsoleResult(model.getStdStream().getOutput());
		}
		view.setButtonsEnabled(model.isCompileSuccess());
	}


	@Override
	public void fileOpened(File file) {
		Program program = model.fileToProgram(file);
		if (program != null && program.getProgramStatements() != null) {
			view.clearVisualisations();
			view.setProgramCode(program.getProgramStatements());
			view.setFilename(file.getName());
		}
	}

	@Override
	public void saveToFile(ArrayList<ArrayList<String>> code, String filename) {
		if (code != null && filename != null) {
			Program program = new Program(code);
			File file = model.programToFile(program, filename);
			view.setFilename(file.getName());
		}
	}	
}<commit_msg>GPIO hook method implemented in Controller.java<commit_after>package org.raspberrypi.cecil.controller;

import java.io.File;
import java.util.ArrayList;

import org.raspberrypi.cecil.model.Model;
import org.raspberrypi.cecil.model.outputstream.ErrorOutputStream;
import org.raspberrypi.cecil.model.outputstream.OutputError;
import org.raspberrypi.cecil.pojo.Program;
import org.raspberrypi.cecil.view.Frame;

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.Pin;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;

/**
 * Controller class for interfacing between the model and view.
 * This class creates an instance of the view (Frame) and model (Cecil) classes.
 * This class contains the main method.
 * 
 * MIT Open Source License
 * @author <NAME> (cj8g10), <NAME> (sa10g10)
 * Southampton University, United Kingdom
 * @version 1.1
 * 
 * @date 11/11/2013
 *
 */
public class Controller implements ControllerInterface {
	private Frame view;
	private Model model;

	/**
	 * Launches the application by creating a new CecilController.
	 */
	public static void main(String[] args) {
		new Controller();
	}


	/**
	 * Constructor which creates a view (Frame) and model (Cecil) object.
	 */
	public Controller() {
		view = new Frame(this);		
		model = new Model();

		view.setInstructionList(model.getInstructions());
		view.setProgramCode(this.getDefaultInput());
	}

	/**
	 * Private method to generate default input editor program
	 * @return Program 
	 */
	private ArrayList<ArrayList<String>> getDefaultInput() {
		ArrayList<ArrayList<String>> program = new ArrayList<ArrayList<String>>();
		ArrayList<String> line = new ArrayList<String>();

		line.add(".start");
		line.add("load");
		line.add("num1");
		line.add(";");
		program.add(line);

		line = new ArrayList<String>();
		line.add(" ");
		line.add("add");
		line.add("num2");
		line.add(";");
		program.add(line);

		line = new ArrayList<String>();
		line.add(" ");
		line.add("printch");
		line.add(" ");
		line.add(";");
		program.add(line);

		line = new ArrayList<String>();
		line.add(" ");
		line.add("printb");
		line.add(" ");
		line.add(";");
		program.add(line);

		line = new ArrayList<String>();
		line.add(" ");
		line.add("xor");
		line.add("num2");
		line.add(";");
		program.add(line);

		line = new ArrayList<String>();
		line.add(" ");
		line.add("printb");
		line.add(" ");
		line.add(";");
		program.add(line);

		line = new ArrayList<String>();
		line.add(" ");
		line.add("xdec");
		line.add(" ");
		line.add(";");
		program.add(line);

		line = new ArrayList<String>();
		line.add(" ");
		line.add("cset");
		line.add(" ");
		line.add(";");
		program.add(line);

		line = new ArrayList<String>();
		line.add(" ");
		line.add("jizero");
		line.add("end");
		line.add(";");
		program.add(line);

		line = new ArrayList<String>();
		line.add(" ");
		line.add("print");
		line.add(" ");
		line.add(";");
		program.add(line);

		line = new ArrayList<String>();
		line.add(".end");
		line.add("stop");
		line.add(" ");
		line.add(";");
		program.add(line);

		line = new ArrayList<String>();
		line.add(".num1");
		line.add("insert");
		line.add("63");
		line.add(";");
		program.add(line);

		line = new ArrayList<String>();
		line.add(".num2");
		line.add("insert");
		line.add("2");
		line.add(";");
		program.add(line);

		return program;
	}

	@Override
	public void compileClicked(ArrayList<ArrayList<String>> code) {
		if(checkCorrectInput(code)) {
			model.compile(new Program(code));
			this.setViewOutput();
		}

		else { 
			model.setToDefault();
			this.setViewOutput();
			view.setConsoleError(model.getErrorStream().getErrors());
		}

	}

	@Override
	public void runClicked() {
		model.run();
		this.setViewOutput();
	}

	@Override
	public void stepThroughClicked() {
		view.highlightStepThrough(model.stepThrough());
		this.setViewOutput();
	}

	/**
	 * GPIO hook
	 */
	public void sendOutputToGPIO() {
		if(System.getProperties().getProperty("sun.desktop").equals("raspbian")) {
			/* create gpio controller */
			GpioController gpio = GpioFactory.getInstance();
			GpioPinDigitalOutput opin;
			
			for(int i = 0; i < 11; i++) {
				if((model.getAcc().get(model.getAcc().size() - 1) << i) == 1) {
					/* provision gpio pin #01 as an output pin and turn on */
					switch(i) {
					case 1:
						opin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01, "MyLED", PinState.HIGH);
						break;
					case 2:
						opin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_02, "MyLED", PinState.HIGH);
						break;
					case 3:
						opin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_03, "MyLED", PinState.HIGH);
						break;
					case 4:
						opin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_04, "MyLED", PinState.HIGH);
						break;
					case 5:
						opin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_05, "MyLED", PinState.HIGH);
						break;
					case 6:
						opin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_06, "MyLED", PinState.HIGH);
						break;
					case 7:
						opin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_07, "MyLED", PinState.HIGH);
						break;
					case 8:
						opin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_08, "MyLED", PinState.HIGH);
						break;
					case 9:
						opin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_09, "MyLED", PinState.HIGH);
						break;
					case 10:
						opin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_10, "MyLED", PinState.HIGH);
						break;
					}
				}
			}
		}
	}

	public boolean checkCorrectInput(ArrayList<ArrayList<String>> code){
		for(int i = 0; i<code.size(); i++) {
			ArrayList<String> input = code.get(i);

			if(input.get(1).equals(" ")){
				if(!input.get(0).equals(" ") || !input.get(2).equals(" ")){
					addErrorToOutputstream(i+1, "An instruction must be provided");
					return false;
				}
			}
			else{

				if(model.isBinaryInstruction(input.get(1))){
					if(input.get(2).equals(" ")){
						addErrorToOutputstream(i+1, "A binary instruction must be succeeded by a datafield");
						System.out.println(model.getErrorStream().getErrors().isEmpty());
						return false;
					}
				}
				if(!model.isBinaryInstruction(input.get(1))){
					if(!input.get(2).equals(" ")){
						addErrorToOutputstream(i+1, "A unary instruction must not be succeeded by a datafield");
						return false;
					}
				}
			}
		}
		return true;
	}

	private void addErrorToOutputstream (int line, String msg){
		ErrorOutputStream e = new ErrorOutputStream();
		e.getErrors().add(new OutputError(line, msg));
		model.setErrorStream(e);
	}

	/**
	 * Refreshes the View output using the current snapshot of the memory
	 */
	private void setViewOutput() {
		if (model.getAcc() != null) {
			ArrayList<String> accStack = new ArrayList<String>();
			for (int i = 0; i < model.getAcc().size(); i++) {
				accStack.add(Integer.toString(model.getAcc().get(i)));
			}
			view.setAccStack(accStack);
		} else {
			view.setAccStack(new ArrayList<String>());
		}

		if (model.getXReg() != null && model.getXReg().size() > 0) {
			ArrayList<String> xStack = new ArrayList<String>();
			for (int i = 0; i < model.getXReg().size(); i++) {
				xStack.add(Integer.toString(model.getXReg().get(i)));
			}
			view.setXStack(xStack);
		} else {
			view.setXStack(new ArrayList<String>());
		}

		if (model.getYReg() != null && model.getYReg().size() > 0) {
			ArrayList<String> yStack = new ArrayList<String>();
			for (int i = 0; i < model.getYReg().size(); i++) {
				yStack.add(Integer.toString(model.getYReg().get(i)));
			}
			view.setYStack(yStack);
		} else {
			view.setYStack(new ArrayList<String>());
		}

		view.setCarryFlag(model.isCarryFlag());
		view.setZeroFlag(model.isZeroFlag());
		view.setNegativeFlag(model.isNegativeFlag());

		view.setMemoryAllocation(model.getMemory());

		if (!model.isCompileSuccess() && model.getErrorStream() != null && model.getErrorStream().getErrors() != null) {
			view.setConsoleError(model.getErrorStream().getErrors());
		} else {
			view.setConsoleResult(model.getStdStream().getOutput());
		}
		view.setButtonsEnabled(model.isCompileSuccess());
	}


	@Override
	public void fileOpened(File file) {
		Program program = model.fileToProgram(file);
		if (program != null && program.getProgramStatements() != null) {
			view.clearVisualisations();
			view.setProgramCode(program.getProgramStatements());
			view.setFilename(file.getName());
		}
	}

	@Override
	public void saveToFile(ArrayList<ArrayList<String>> code, String filename) {
		if (code != null && filename != null) {
			Program program = new Program(code);
			File file = model.programToFile(program, filename);
			view.setFilename(file.getName());
		}
	}	
}
{'fraction_non_alphanumeric': 0.17462351810317206, 'fraction_numerical': 0.008074335148990709, 'mean_word_length': 3.879924953095685, 'pattern_counts': {'":': 0, '<': 90, '<?xml version=': 0, '>': 83, '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': '131280', 'n_tokens_mistral': 6110, 'n_tokens_neox': 5209, 'n_words': 1029}