<commit_before>package backbone;

import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.zip.GZIPInputStream;

import ser2mat.ser2mat;
import cell.CModel;

public class Interface{

	public static void main(String[] args) throws Exception{
		System.out.println("DIATOMAS Java model");

		CModel model = new CModel("default");
		
		int NArg = args.length;
		for(int ii=0; ii<NArg; ii+=2) {
			String arg = args[ii];
			//
			if(arg.equalsIgnoreCase("help") || arg.equalsIgnoreCase("--help") || arg.equalsIgnoreCase("?") || arg.equalsIgnoreCase("/?")) {
				System.out.println("Usage: java -jar diatomas.jar [option0] [value0] [option1] [value1] ...");
				System.out.println("where [value] be a number (where 0 == false, 1 == true) or sometimes string and [option] can be any of the following:");
				System.out.println("help || --help || ? || /?\t\t Show this help text");
				
				System.out.println("  Model:");
				System.out.println("seed\t\t\t Set random seed");
				System.out.println("simulation\t\t Set simulation type: (0) E. coli (1) dental/biofilm (2) dental/flock");
				System.out.println("sticking\t\t\t Enable or disable cell-cell EPS links (sticking springs)");
				System.out.println("filament\t\t\t Enable or disable filial links between mother and daughter cells");
				System.out.println("gravity\t\t\t Enable or disable gravity");
				System.out.println("anchoring\t\t\t Enable or disable cell-substratum EPS links (anchoring springs)");
				System.out.println("gravityZ\t\t\t Gravity parallel to the plane instead of perpendicular (needs gravity 1)");
				System.out.println("sphereStraightFil\t\t Sphere-sphere filial links are straight (like streptococci, not staphyllococci)");
				System.out.println("initialAtSubstratum\t\t Initial cells start at substratum (y==ball.radius)");
				System.out.println("syntrophyFactor\t\t Growth acceleration due to cells being stuck to a cell of a different type (1.0 is no acceleration)");
				System.out.println("growthTimeStep\t\t Time passed in seconds per growth step");
				System.out.println("relaxationTimeStep\t\t Time passed in seconds per relaxation step");
				System.out.println("normalForce\t\t\t Use normal force acting at the substratum, y=0");
				
				System.out.println("  OS communication");
				System.out.println("waitForFinish || disableWaitForFinish\t When calling command line arguments from the model, waits for them to finish running or continues with the model");
				System.out.println("echoCommand \t\t Echoes or silences the command line functions ran from the model");
								
				System.out.println("  Comsol:");
				System.out.println("comsol \t\t\t Enable or disable the use of COMSOL. Enable: use backbone file WithComsol.java, otherwise use WithoutComsol.java");
				System.out.println("port [comsol server port]\t\t\t\t Use port [comsol server port] to start COMSOL server");
				System.out.println("64bit \t\t\t Enable (use 64 bit) or disable (use 32 bit) the use of a 32 bit architecture (can eliminate memory issues, can cause memory leak)");
				
				System.out.println("  Other:");
				System.out.println("start \t\t\t Starts the model automatically after looping through the arguments, or not");
				System.out.println("load [path/filename.seg]\t Load the specified file instead of the default parameters. Automatically starts model after loading. Be sure to specify argument comsol");
				System.out.println("ser2mat [path] \t\t\t Converts all .ser files found in [path]/output/ to .mat files. Automatically inhibits model starting after loading");
								
				System.out.println("*\t\t\t\t Any unrecognised argument is assumed to be simulation the name");
				return;
			}
			//
			boolean boolNameSet = false;
			if(arg.equalsIgnoreCase("64bit") || arg.equalsIgnoreCase("bit64")) 
															{Assistant.bit64 = (Integer.parseInt(args[ii+1])==1)?true:false;			continue;}
			if(arg.equalsIgnoreCase("anchoring") || arg.equalsIgnoreCase("anchor"))
															{model.anchoring = (Integer.parseInt(args[ii+1])==1)?true:false;				continue;}
			if(arg.equalsIgnoreCase("comsol")) 				{model.comsol = (Integer.parseInt(args[ii+1])==1)?true:false;					continue;}
			if(arg.equalsIgnoreCase("echocommand")) 		{Assistant.echoCommand = (Integer.parseInt(args[ii+1])==1)?true:false;			continue;}
			if(arg.equalsIgnoreCase("filament")) 			{model.filament = (Integer.parseInt(args[ii+1])==1)?true:false;					continue;}
			if(arg.equalsIgnoreCase("gravity")) 			{model.gravity = (Integer.parseInt(args[ii+1])==1)?true:false;					continue;}
			if(arg.equalsIgnoreCase("gravityz")) 			{model.gravityZ = (Integer.parseInt(args[ii+1])==1)?true:false;					continue;}
			if(arg.equalsIgnoreCase("growthTimeStep"))		{model.growthTimeStep = Double.parseDouble(args[ii+1]);							continue;}
			if(arg.equalsIgnoreCase("initialatsubstratum")) {model.initialAtSubstratum = (Integer.parseInt(args[ii+1])==1)?true:false;		continue;}
			if(arg.equalsIgnoreCase("relaxationtimeStep"))	{model.relaxationTimeStepdt = Double.parseDouble(args[ii+1]);					continue;}
			if(arg.equalsIgnoreCase("normalforce"))			{model.normalForce = (Integer.parseInt(args[ii+1])==1)?true:false;				continue;}			
			if(arg.equalsIgnoreCase("port")) 				{Assistant.port = Integer.parseInt(args[ii+1]);									continue;}
			if(arg.equalsIgnoreCase("seed")) 				{model.randomSeed = Integer.parseInt(args[ii+1]);								continue;}
			if(arg.equalsIgnoreCase("simulation")) 			{model.simulation = Integer.parseInt(args[ii+1]);								continue;}
			if(arg.equalsIgnoreCase("start"))				{Assistant.start = (Integer.parseInt(args[ii+1])==1)?true:false;				continue;}
			if(arg.equalsIgnoreCase("spherestraightfil")) 	{model.sphereStraightFil = (Integer.parseInt(args[ii+1])==1)?true:false;		continue;}
			if(arg.equalsIgnoreCase("sticking")) 			{model.sticking = (Integer.parseInt(args[ii+1])==1)?true:false;					continue;}
			if(arg.equalsIgnoreCase("syntrophyFactor"))		{model.syntrophyFactor = Double.parseDouble(args[ii+1]);						continue;}
			if(arg.equalsIgnoreCase("waitforfinish")) 		{Assistant.waitForFinish = (Integer.parseInt(args[ii+1])==1)?true:false;		continue;}
			if(arg.equalsIgnoreCase("load")){
				String loadPath = args[ii+1];
				if(!loadPath.contains("/")) {
					// loadPath doesn't state which simulation to load --> load the most recent one
					// Open directory
					File dir = new File(loadPath + "/output/");
					// Construct filter
					FilenameFilter filter = new FilenameFilter() {
					    public boolean accept(File dir, String name) {
					    	return name.endsWith(".ser");
					    }
					};
					// List filtered files
					String[] files = dir.list(filter);
					if(files==null) throw new Exception("No .ser files found in directory " + loadPath + "/output/");
					// Update loadPath based on found .ser files
					java.util.Arrays.sort(files);
					loadPath = loadPath + "/output/" + files[files.length-1];
				}
				model = Load(loadPath);
				model.Write("Loaded " + loadPath, "");
				Assistant.start = true;
				continue;}
			if(arg.equalsIgnoreCase("ser2mat")){
				String modelPath = args[ii+1];
				// Open directory
				File dir = new File(modelPath + "/output/");
				// Construct filter
				FilenameFilter filter = new FilenameFilter() {
				    public boolean accept(File dir, String name) {
				    	return name.endsWith(".ser");
				    }
				};
				// List filtered files and convert
				String[] files = dir.list(filter);
				if(files==null) throw new Exception("No .ser files found in directory " + modelPath + "/output/");
				java.util.Arrays.sort(files);
				for(String fileName : files) { 
					model.Write("Loading " + fileName,"", true, false);
					String loadPath = modelPath + "/output/" + fileName;
					model = Load(loadPath);
					ser2mat.Convert(model);
				}
				
				Assistant.start = false;
			}
			// If not any of the above, it must be the name
			if(boolNameSet)	throw new Exception("Name already set to '" + model.name + "': cannot set to '" + arg + "'");
			model.name=arg;
			boolNameSet = true;
			ii--;				// subtract 1 from ii because we don't want to ignore the argument after this name
		}
		
		// Done analysing input arguments
		// Start model if requested
		if(Assistant.start) {
			try {
				model.Write("=====================================", "");
				String message = "Starting simulation '" + model.name + "' w/ arguments: ";
				for(int jj=0; jj<args.length; jj++) 	message += args[jj] + " ";
				model.Write(message,"");
				model.Write("=====================================", "");
				new Run(model);
			} catch (RuntimeException E) {
				StringWriter sw = new StringWriter();				// We need this line and below to get the exception as a string for the log
				PrintWriter pw = new PrintWriter(sw);
				E.printStackTrace(pw);								// Write the exception stack trace to a print
				model.Write(sw.toString(), "error", false, true);	// Convert stack trace to string, print to log
				E.printStackTrace();								// Throw the error so the simulation stops running
			}
		}
	}
	
	public static CModel Load(String loadPath) {
		FileInputStream fis = null;
		GZIPInputStream gz = null;
		ObjectInputStream ois = null;<commit_msg>Implemented variable name setting through arguments. Requires further tuning, e.g. *10 or *0.1, variables need to be set after loading the simulation<commit_after>package backbone;

import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.util.zip.GZIPInputStream;

import ser2mat.ser2mat;
import cell.CModel;

public class Interface{

	public static void main(String[] args) throws Exception{
		System.out.println("DIATOMAS Java model");

		CModel model = new CModel("default");
		
		int NArg = args.length;
		for(int ii=0; ii<NArg; ii+=2) {
			String arg = args[ii];
			//
			if(arg.equalsIgnoreCase("help") || arg.equalsIgnoreCase("--help") || arg.equalsIgnoreCase("?") || arg.equalsIgnoreCase("/?")) {
				System.out.println("Usage: java -jar diatomas.jar [option0] [value0] [option1] [value1] ...");
				System.out.println("where [value] be a number (0 == false, 1 == true) or string");
				System.out.println("  Other:");
				System.out.println("start \t\t\t Starts the model automatically after looping through the arguments, or not");
				System.out.println("load [path/filename.seg]\t Load the specified file instead of the default parameters. Automatically starts model after loading. Be sure to specify argument comsol");
				System.out.println("ser2mat [path] \t\t\t Converts all .ser files found in [path]/output/ to .mat files. Automatically inhibits model starting after loading");
								
				System.out.println("*\t\t\t\t Any unrecognised argument is assumed to be simulation the name");
				return;
			}
			if(arg.equalsIgnoreCase("load")){
				String loadPath = args[ii+1];
				if(!loadPath.contains("/")) {
					// loadPath doesn't state which simulation to load --> load the most recent one
					// Open directory
					File dir = new File(loadPath + "/output/");
					// Construct filter
					FilenameFilter filter = new FilenameFilter() {
					    public boolean accept(File dir, String name) {
					    	return name.endsWith(".ser");
					    }
					};
					// List filtered files
					String[] files = dir.list(filter);
					if(files==null) throw new Exception("No .ser files found in directory " + loadPath + "/output/");
					// Update loadPath based on found .ser files
					java.util.Arrays.sort(files);
					loadPath = loadPath + "/output/" + files[files.length-1];
				}
				model = Load(loadPath);
				model.Write("Loaded " + loadPath, "");
				Assistant.start = true;
				continue;
			}
			// Convert ser to mat files
			if(arg.equalsIgnoreCase("ser2mat")){
				String modelPath = args[ii+1];
				// Open directory
				File dir = new File(modelPath + "/output/");
				// Construct filter
				FilenameFilter filter = new FilenameFilter() {
				    public boolean accept(File dir, String name) {
				    	return name.endsWith(".ser");
				    }
				};
				// List filtered files and convert
				String[] files = dir.list(filter);
				if(files==null) throw new Exception("No .ser files found in directory " + modelPath + "/output/");
				java.util.Arrays.sort(files);
				for(String fileName : files) { 
					model.Write("Loading " + fileName,"", true, false);
					String loadPath = modelPath + "/output/" + fileName;
					model = Load(loadPath);
					ser2mat.Convert(model);
				}
				
				Assistant.start = false;
				continue;
			}
			// See if we can set a field value to this
			for(Field field : CModel.class.getFields()) {
				if(arg.equalsIgnoreCase(field.getName())) {
					@SuppressWarnings("rawtypes")
					Class fieldClass = CModel.class.getField(arg).get(model).getClass();
					String value = args[ii+1];
					if(fieldClass.equals(Double.class)) {				// Does the field contain a double?
						field.setDouble(model, Double.parseDouble(value));
					} else if(fieldClass.equals(Integer.class)) {		// An int?
						field.setInt(model, Integer.parseInt(value));
					} else if(fieldClass.equals(String.class)) {		// A string?
						field.set(model, value);
					} else {
						throw new RuntimeException("Unknown class type");
					}
				}
			}
		}
		
		// Done analysing input arguments
		// Start model if requested
		if(Assistant.start) {
			try {
				model.Write("=====================================", "");
				String message = "Starting simulation '" + model.name + "' w/ arguments: ";
				for(int jj=0; jj<args.length; jj++) 	message += args[jj] + " ";
				model.Write(message,"");
				model.Write("=====================================", "");
				new Run(model);
			} catch (RuntimeException E) {
				StringWriter sw = new StringWriter();				// We need this line and below to get the exception as a string for the log
				PrintWriter pw = new PrintWriter(sw);
				E.printStackTrace(pw);								// Write the exception stack trace to a print
				model.Write(sw.toString(), "error", false, true);	// Convert stack trace to string, print to log
				E.printStackTrace();								// Throw the error so the simulation stops running
			}
		}
	}
	
	public static CModel Load(String loadPath) {
		FileInputStream fis = null;
		GZIPInputStream gz = null;
		ObjectInputStream ois = null;
{'fraction_non_alphanumeric': 0.14835164835164835, 'fraction_numerical': 0.006387362637362637, 'mean_word_length': 3.584697732997481, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '331376', 'n_tokens_mistral': 5264, 'n_tokens_neox': 4363, 'n_words': 1186}