Skip to content

Commit

Permalink
Initial source commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
waterlan committed Oct 27, 2022
1 parent c3503bc commit d446278
Show file tree
Hide file tree
Showing 36 changed files with 8,594 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.bak
.settings
6 changes: 6 additions & 0 deletions asm/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions asm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions asm/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ASM</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
24 changes: 24 additions & 0 deletions asm/src/console/ASM.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package console;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ASM extends Application {

@Override
public void start(Stage primaryStage) throws Exception {

VBox root = new VBox();
Scene scene = new Scene(root, 600, 400);
new Console(root, primaryStage);
primaryStage.setScene(scene);
primaryStage.setTitle("ASM Console");
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}
248 changes: 248 additions & 0 deletions asm/src/console/CommandLineParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
package console;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import exceptions.SignalDoesNotExist;
import math.Calculations;
import math.ConvCorr;
import math.Transformations;
import signals.PlotCommands;
import signals.Signal;
import signals.SignalWindow;
import signals.Sources;
import signals.Windowing;

public class CommandLineParser {
private final Sources sources;
private final Windowing windowing;
private final Calculations calculations;
private final Transformations transformations;
private final PlotCommands plotCommands;
private final ConvCorr convcorr;
private final Console console;
private final Commands commands = new Commands();
private boolean don = true;
private boolean bon = false;
private File signalDirectory = new File(System.getProperty("user.home"));
Map<String, Signal> signals;

public CommandLineParser(Map<String, Signal> signals, Console console) {
this.signals = signals;
this.sources = new Sources(signals, this);
this.windowing = new Windowing(signals, this);
this.calculations = new Calculations(signals, this);
this.transformations = new Transformations(signals, this);
this.plotCommands = new PlotCommands(signals, this);
this.convcorr = new ConvCorr(signals, this);
this.console = console;
}

public String getString(List<String> args, String name, String defaultValue) {
if (args.isEmpty()) {
return defaultValue;
} else {
String result = args.get(0) == null || args.get(0).isEmpty() ? defaultValue : args.get(0);
args.remove(0);
return result;
}
}

public void print(String message) {
console.print(message);
}

public void println(String message) {
console.println(message);
}

public int getInt(List<String> args, String name, int min, int max, int defaultValue) {
int result;
if (args.isEmpty()) {
return defaultValue;
} else {
try {
result = args.get(0) == null || args.get(0).isEmpty() || args.get(0).equals(".") ? defaultValue
: Integer.parseInt(args.get(0));
} catch (NumberFormatException e) {
println("error: invalid expression for (integer) " + name);
result = defaultValue;
}
args.remove(0);
if (result < min)
return min;
else if (result > max)
return max;
else
return result;
}
}

public double getDouble(List<String> args, String name, double min, double max, double defaultValue) {
double result;
if (args.isEmpty()) {
return defaultValue;
} else {
try {
result = args.get(0) == null || args.get(0).isEmpty() || args.get(0).equals(".") ? defaultValue
: Double.parseDouble(args.get(0));
} catch (NullPointerException e) {
result = defaultValue;
} catch (NumberFormatException e) {
println("error: invalid expression for (double) " + name);
result = defaultValue;
}
args.remove(0);
if (result < min)
return min;
else if (result > max)
return max;
else
return result;
}
}

int parseCommand(String command_with_args) {

if (command_with_args == null) {
return 0;
}
String cmd = command_with_args.trim();
if (cmd.isEmpty()) {
return 0;
}

String[] args = cmd.split("\\s+");
List<String> argList = new ArrayList<>(Arrays.asList(args));
return parseCommand(argList);
}

public void showSignal(Signal s, boolean override) {
if (!don && !override)
return;
if (s == null)
return;
SignalWindow w = s.getWindow();
if (w == null) {
w = new SignalWindow(s, this, signals);
} else {
w.show(bon);
}
}

public void closeSignal(Signal s) {
if (s == null)
return;
SignalWindow w = s.getWindow();
if (w == null) {
return;
} else {
w.close();
}
}

public void showConsole() {
console.show();
}

private void printCommand(List<String> argList) {
StringBuffer line = new StringBuffer();
line.append(">");
for (String arg : argList) {
if (arg == null || arg.isEmpty()) {
line.append(" .");
} else {
line.append(" " + arg);
}
}
println(line.toString());
}

public int parseCommand(List<String> argList) {

if (argList == null || argList.isEmpty()) {
return 0;
}
printCommand(argList);
String command = argList.get(0);
argList.remove(0);
List<String> cmdMatches = commands.getCommand(command);
if (cmdMatches.size() > 1) {
println("Ambiguous command: " + command);
return 0;
} else if (cmdMatches.size() == 1)
command = cmdMatches.get(0);
if (!argList.isEmpty() && argList.get(0) != null && argList.get(0).matches("^\\?$|-h")) {
String help = commands.getHelp(command);
if (help != null) {
println(help);
return 0;
}
}

try {
if (command.equals("list")) {
for (String signalName : signals.keySet()) {
println(signalName);
}
} else if (command.equals("?")) {
List<String> cmds = commands.getAllCommands();
for (String cmd : cmds)
println(cmd);
} else if (command.equals("don")) {
don = true;
println("Display is ON");
} else if (command.equals("doff")) {
don = false;
println("Display is OFF");
} else if (command.equals("bon")) {
bon = true;
println("Bar graph is ON");
} else if (command.equals("boff")) {
bon = false;
println("Bar graph is OFF");
} else if (command.equals("exit") || command.equals("quit")) {
System.exit(0);
} else if (PlotCommands.plotcommands.containsKey(command)) {
Signal s = plotCommands.PlotCommandsCi(argList, command);
showSignal(s, command.equals("display"));
} else if (Sources.functions.containsKey(command)) {
Signal s = sources.SourcesCi(argList, command);
showSignal(s, false);
} else if (Windowing.windows.containsKey(command)) {
Signal s = windowing.WindowCi(argList, command);
showSignal(s, false);
} else if (Calculations.calculations.containsKey(command)) {
Signal s = calculations.CalculateCi(argList, command);
showSignal(s, false);
} else if (Transformations.transformations.containsKey(command)) {
Signal s = transformations.TransformationCi(argList, command);
showSignal(s, false);
} else if (ConvCorr.convcorr.containsKey(command)) {
Signal s = convcorr.ConvCorrCi(argList, command);
showSignal(s, false);
} else {
println("error: command not found.");
}
} catch (SignalDoesNotExist e) {
println("error: " + e.getMessage());
} catch (IOException e) {
println("error: " + e.getMessage());
}
return 0;
}

public File getSignalDirectory() {
return signalDirectory;
}

public void setSignalDirectory(File signalDirectory) {
this.signalDirectory = signalDirectory;
}
}
61 changes: 61 additions & 0 deletions asm/src/console/Commands.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package console;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import math.Calculations;
import math.ConvCorr;
import math.Transformations;
import signals.PlotCommands;
import signals.Sources;
import signals.Windowing;

public class Commands {

public static final Map<String, String[]> commands = new HashMap<String, String[]>() {
{
put("doff", new String[] { "", "" });
put("don", new String[] { "", "" });
put("list", new String[] { "", "" });
put("boff", new String[] { "", "" });
put("bon", new String[] { "", "" });
put("exit", new String[] { "", "" });
put("quit", new String[] { "", "" });
}
};

public Commands() {
commands.putAll(Sources.functions);
commands.putAll(PlotCommands.plotcommands);
commands.putAll(Windowing.windows);
commands.putAll(Calculations.calculations);
commands.putAll(Transformations.transformations);
commands.putAll(ConvCorr.convcorr);
}

public List<String> getCommand(String command) {
List<String> matches = new ArrayList<String>();
for (String cmd : commands.keySet()) {
if (cmd.startsWith(command))
matches.add(cmd);
}
return matches;
}

public List<String> getAllCommands() {
List<String> cmds = new ArrayList<String>(commands.keySet());
Collections.sort(cmds);
return cmds;
}

public String getHelp(String command) {
if (commands.containsKey(command))
return commands.get(command)[1];
else
return null;
}

}

0 comments on commit d446278

Please sign in to comment.