-
-
Save Groostav/9c5913e6f4696a25430d to your computer and use it in GitHub Desktop.
Powershell facade for PDOL - OPTIP, request for help on Stack Overflow. Doesnt properly invoke said powershell script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package PDOL.UserInterface.MainPanel.Workspace; | |
import PDOL.DataObject.Symbol; | |
import PDOL.DataObject.Vec; | |
import PDOL.Utility.*; | |
import java.io.*; | |
import java.nio.file.Paths; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.Scanner; | |
import static PDOL.Utility.LinqingList.from; | |
import static PDOL.Utility.Util.commaSeparate; | |
public class PowershellFacade implements Transform<Vec, Double> { | |
public static final String INPUT_FILE_NAME = "powershellInputFile.csv"; | |
private static final String OUTPUT_FILE_NAME = "powershellOutputFile.csv"; | |
private final String scriptFileName; | |
private final List<String> inputs = new ArrayList<String>(); | |
public PowershellFacade(String scriptFileName, Iterable<? extends Symbol> inputs){ | |
this.scriptFileName = scriptFileName; | |
for(Symbol symbol : inputs){ | |
this.inputs.add(symbol.name); | |
} | |
} | |
@Override | |
public Double getFrom(Vec inputPoints) { | |
writeToInputFile(inputPoints); | |
runPowershellScript(); | |
double result = readFromOutputFile(); | |
cleanUp(); | |
return result; | |
} | |
private void cleanUp() { | |
for(String path : Arrays.asList(INPUT_FILE_NAME, OUTPUT_FILE_NAME)){ | |
File file = new File(path); | |
file.delete(); | |
} | |
} | |
private double readFromOutputFile() { | |
Scanner outputReader = null; | |
try { | |
outputReader = new Scanner(new File(OUTPUT_FILE_NAME)); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. | |
} | |
String line = outputReader.nextLine(); | |
String rawValue = outputReader.nextLine(); | |
return Double.parseDouble(rawValue); | |
} | |
private void writeToInputFile(Vec inputPoints) { | |
final PrintWriter inputWriter = getInputFileWriter(); | |
inputWriter.println(commaSeparate(inputs)); | |
inputWriter.println(commaSeparate(inputPoints)); | |
inputWriter.flush(); | |
} | |
private PrintWriter getInputFileWriter() { | |
final File file = new File(INPUT_FILE_NAME); | |
final Ref<PrintWriter> stream = new Ref<PrintWriter>(); | |
Util.FailOnException(new FailingCommand() { | |
@Override | |
public void run() throws Exception { | |
stream.target = new PrintWriter(file); | |
} | |
}); | |
return stream.target; | |
} | |
private void runPowershellScript() { | |
String command = | |
"" + "powershell" + " " + | |
Paths.get("").toAbsolutePath() + "\\" + scriptFileName + " "; | |
// Paths.get("").toAbsolutePath() + "\\" + INPUT_FILE_NAME + " " + | |
// Paths.get("").toAbsolutePath() + "\\" + OUTPUT_FILE_NAME + ""; | |
try { | |
ProcessBuilder builder = new ProcessBuilder(command); | |
builder.redirectErrorStream(true); | |
Process process = builder.start(); | |
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream())); | |
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); | |
String line; | |
writer.write(command); | |
while ((line = reader.readLine ()) != null) { | |
System.out.println ("Stdout: " + line); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment