/* * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license */ package imat.tc; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.nio.charset.StandardCharsets; /** * * @author imanol */ public class TC { // TC process private Process process; // Read / Write streams private final OutputStream stdin; private final BufferedReader stdout; public TC () throws IOException{ // Launch TC String path = System.getProperty("user.dir"); process = new ProcessBuilder(path+"/tc-launcher").start(); // Create input and output stream stdin = process.getOutputStream(); stdout = new BufferedReader(new InputStreamReader(process.getInputStream())); } ///// TC public void TC_Init() throws IOException { stdin.write("TC_Init\n".getBytes(StandardCharsets.UTF_8)); stdin.flush(); } public String TC_Generation () throws IOException, InterruptedException{ stdin.write("TC_Generation\n".getBytes(StandardCharsets.UTF_8)); stdin.flush(); return (stdout.readLine()); } public String TC_Build_Version() throws IOException{ stdin.write("TC_Build_Version\n".getBytes(StandardCharsets.UTF_8)); stdin.flush(); return (stdout.readLine()); } public Boolean TC_Start_Mission() throws IOException{ stdin.write("TC_Start_Mission\n".getBytes(StandardCharsets.UTF_8)); stdin.flush(); return Boolean.parseBoolean(stdout.readLine().trim()); } public int TC_Get_Num_Steps() throws IOException{ stdin.write("TC_Get_Num_Steps\n".getBytes(StandardCharsets.UTF_8)); stdin.flush(); return Integer.parseInt(stdout.readLine().trim()); } public void TC_End_Mission() throws IOException { stdin.write("TC_End_Mission\n".getBytes(StandardCharsets.UTF_8)); stdin.flush(); } ///// Vehicle public void Vehicle_Set_Current_Pos(int vehicle, int pos) throws IOException { stdin.write(("Vehicle_Set_Current_Pos "+vehicle+" "+pos+"\n").getBytes(StandardCharsets.UTF_8)); stdin.flush(); } public int Vehicle_Get_Current_Pos(int vehicle) throws IOException{ stdin.write(("Vehicle_Get_Current_Pos "+vehicle+" \n").getBytes(StandardCharsets.UTF_8)); stdin.flush(); return Integer.parseInt(stdout.readLine().trim()); } public int Vehicle_Get_Dis_To_Pos(int vehicle, int pos) throws IOException{ stdin.write(("Vehicle_Get_Dis_To_Pos " +vehicle+ " "+pos+"\n").getBytes(StandardCharsets.UTF_8)); stdin.flush(); return Integer.parseInt(stdout.readLine().trim()); } public void Vehicle_Set_Goal_Pos(int vehicle, int pos) throws IOException { stdin.write(("Vehicle_Set_Goal_Pos "+vehicle+" "+pos+"\n").getBytes(StandardCharsets.UTF_8)); stdin.flush(); } public int Vehicle_Get_Next_Pos(int vehicle) throws IOException{ stdin.write(("Vehicle_Get_Next_Pos " +vehicle+"\n").getBytes(StandardCharsets.UTF_8)); stdin.flush(); return Integer.parseInt(stdout.readLine().trim()); } public int Vehicle_Get_Step_Pos(int vehicle, int step) throws IOException{ stdin.write(("Vehicle_Get_Step_Pos "+vehicle+" "+step+"\n").getBytes(StandardCharsets.UTF_8)); stdin.flush(); return Integer.parseInt(stdout.readLine().trim()); } public Boolean Vehicle_Moved_Forward() throws IOException{ stdin.write(("Vehicle_Moved_Forward "+"\n").getBytes(StandardCharsets.UTF_8)); stdin.flush(); return Boolean.parseBoolean(stdout.readLine().trim()); } public Boolean Vehicle_Goal_Reached() throws IOException{ stdin.write(("Vehicle_Goal_Reached "+"\n").getBytes(StandardCharsets.UTF_8)); stdin.flush(); return Boolean.parseBoolean(stdout.readLine().trim()); } ///// Environment public void Environment_Clean() throws IOException { //mutex.acquire(); stdin.write("Environment_Clean\n".getBytes(StandardCharsets.UTF_8)); stdin.flush(); } public void Environment_Build() throws IOException { //mutex.acquire(); stdin.write("Environment_Build\n".getBytes(StandardCharsets.UTF_8)); stdin.flush(); } public int Environment_Get_Max_Pos() throws IOException{ stdin.write("Environment_Get_Max_Pos\n".getBytes(StandardCharsets.UTF_8)); stdin.flush(); return Integer.parseInt(stdout.readLine().trim()); } public int Environment_Get_Max_Roads() throws IOException{ stdin.write("Environment_Get_Max_Roads\n".getBytes(StandardCharsets.UTF_8)); stdin.flush(); return Integer.parseInt(stdout.readLine().trim()); } public void Environment_Add_One_Way_Road(int pos1, int pos2) throws IOException { stdin.write(("Environment_Add_One_Way_Road " + pos1 + " " + pos2+"\n").getBytes(StandardCharsets.UTF_8)); stdin.flush(); } public void Environment_Add_Two_Way_Road_One_Lane(int pos1, int pos2) throws IOException { stdin.write(("Environment_Add_Two_Way_Road_One_Lane " + pos1 + " " + pos2+"\n").getBytes(StandardCharsets.UTF_8)); stdin.flush(); } public void Environment_Add_Two_Way_Road_Two_Lanes(int pos1, int pos2) throws IOException { stdin.write(("Environment_Add_Two_Way_Road_Two_Lanes " + pos1 + " " + pos2+"\n").getBytes(StandardCharsets.UTF_8)); stdin.flush(); } // Obstacles public void Obstacles_Clean() throws IOException { stdin.write("Obstacles_Clean\n".getBytes(StandardCharsets.UTF_8)); stdin.flush(); } public void Obstacles_Add_Obstacle(int pos1, int pos2) throws IOException { stdin.write(("Obstacles_Add_Obstacle "+pos1+" "+pos2+"\n").getBytes(StandardCharsets.UTF_8)); stdin.flush(); } public int Obstacles_Get_Num() throws IOException{ stdin.write("Obstacles_Get_Num\n".getBytes(StandardCharsets.UTF_8)); stdin.flush(); return Integer.parseInt(stdout.readLine().trim()); } public Boolean Obstacles_Present(int pos1, int pos2) throws IOException { stdin.write(("Obstacles_Present "+pos1+" "+pos2+"\n").getBytes(StandardCharsets.UTF_8)); stdin.flush(); return Boolean.parseBoolean(stdout.readLine().trim()); } public Boolean Obstacles_Confirm() throws IOException { stdin.write("Obstacles_Confirm\n".getBytes(StandardCharsets.UTF_8)); stdin.flush(); return Boolean.parseBoolean(stdout.readLine().trim()); } // Settings public void Settings_Set_Max_RAM(int memory_mb) throws IOException{ stdin.write(("Settings_Set_Max_RAM "+memory_mb+"\n").getBytes(StandardCharsets.UTF_8)); stdin.flush(); } public void Settings_Set_Timeout(int time_ms) throws IOException{ stdin.write(("Settings_Set_Timeout "+time_ms+"\n").getBytes(StandardCharsets.UTF_8)); stdin.flush(); } public void Settings_Enable_Logging() throws IOException{ stdin.write(("Settings_Enable_Logging\n").getBytes(StandardCharsets.UTF_8)); stdin.flush(); } public void Settings_Disable_Logging() throws IOException{ stdin.write(("Settings_Disable_Logging\n").getBytes(StandardCharsets.UTF_8)); stdin.flush(); } // Monitoring public int Monitoring_Get_Num_Vehicles() throws IOException{ stdin.write("Monitoring_Get_Num_Vehicles\n".getBytes(StandardCharsets.UTF_8)); stdin.flush(); return Integer.parseInt(stdout.readLine().trim()); } public Float Monitoring_Get_CPU_Time() throws IOException{ stdin.write("Monitoring_Get_CPU_Time\n".getBytes(StandardCharsets.UTF_8)); stdin.flush(); return Float.valueOf(stdout.readLine().trim()); } public int Monitoring_Get_RAM_Usage() throws IOException{ stdin.write("Monitoring_Get_RAM_Usage\n".getBytes(StandardCharsets.UTF_8)); stdin.flush(); return Integer.parseInt(stdout.readLine().trim()); } public int Monitoring_Get_Evaluations() throws IOException{ stdin.write("Monitoring_Get_Evaluations\n".getBytes(StandardCharsets.UTF_8)); stdin.flush(); return Integer.parseInt(stdout.readLine().trim()); } }