Fixed rules loading
This commit is contained in:
parent
62418138fc
commit
b7a50c3cc5
@ -12,7 +12,7 @@ public class StaticVars {
|
|||||||
public static boolean debugOn;
|
public static boolean debugOn;
|
||||||
public static int outputLevel = 5;
|
public static int outputLevel = 5;
|
||||||
public static final boolean debugWindow2x = true;
|
public static final boolean debugWindow2x = true;
|
||||||
public static final ClassLoader classLoader = StaticVars.class.getClassLoader();
|
public static final Class<?> classLoader = Main.instance.getClass();
|
||||||
|
|
||||||
private StaticVars() {
|
private StaticVars() {
|
||||||
|
|
||||||
|
@ -4,6 +4,8 @@ import java.io.BufferedReader;
|
|||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
@ -16,9 +18,22 @@ import java.lang.reflect.Modifier;
|
|||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.nio.file.FileSystem;
|
||||||
|
import java.nio.file.FileSystemAlreadyExistsException;
|
||||||
|
import java.nio.file.FileSystems;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.nevec.rjm.BigDecimalMath;
|
import org.nevec.rjm.BigDecimalMath;
|
||||||
import org.nevec.rjm.Rational;
|
import org.nevec.rjm.Rational;
|
||||||
@ -39,6 +54,8 @@ import org.warp.picalculator.math.functions.Variable;
|
|||||||
import org.warp.picalculator.math.functions.equations.Equation;
|
import org.warp.picalculator.math.functions.equations.Equation;
|
||||||
import org.warp.picalculator.math.functions.equations.EquationsSystemPart;
|
import org.warp.picalculator.math.functions.equations.EquationsSystemPart;
|
||||||
|
|
||||||
|
import com.jogamp.common.util.IOUtil;
|
||||||
|
|
||||||
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
||||||
|
|
||||||
public class Utils {
|
public class Utils {
|
||||||
@ -744,4 +761,54 @@ public class Utils {
|
|||||||
t.add(o);
|
t.add(o);
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Path getResource(String string) throws IOException, URISyntaxException {
|
||||||
|
URL res = Main.instance.getClass().getResource(string);
|
||||||
|
boolean isResource = res != null;
|
||||||
|
if (isResource) {
|
||||||
|
try {
|
||||||
|
final URI uri = res.toURI();
|
||||||
|
if (res.getProtocol().equalsIgnoreCase("jar")) {
|
||||||
|
try {
|
||||||
|
FileSystems.newFileSystem(uri, Collections.emptyMap());
|
||||||
|
} catch (FileSystemAlreadyExistsException e) {
|
||||||
|
FileSystems.getFileSystem(uri);
|
||||||
|
}
|
||||||
|
Path myFolderPath = Paths.get(uri);
|
||||||
|
return myFolderPath;
|
||||||
|
} else {
|
||||||
|
return Paths.get(uri);
|
||||||
|
}
|
||||||
|
} catch (java.lang.IllegalArgumentException e) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Paths.get(string.substring(1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static InputStream getResourceStream(String string) throws IOException, URISyntaxException {
|
||||||
|
URL res = Main.instance.getClass().getResource(string);
|
||||||
|
boolean isResource = res != null;
|
||||||
|
if (isResource) {
|
||||||
|
try {
|
||||||
|
final URI uri = res.toURI();
|
||||||
|
if (res.getProtocol().equalsIgnoreCase("jar")) {
|
||||||
|
try {
|
||||||
|
FileSystems.newFileSystem(uri, Collections.emptyMap());
|
||||||
|
} catch (FileSystemAlreadyExistsException e) {
|
||||||
|
FileSystems.getFileSystem(uri);
|
||||||
|
}
|
||||||
|
Path myFolderPath = Paths.get(uri);
|
||||||
|
return Files.newInputStream(myFolderPath);
|
||||||
|
} else {
|
||||||
|
return Files.newInputStream(Paths.get(uri));
|
||||||
|
}
|
||||||
|
} catch (java.lang.IllegalArgumentException e) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Files.newInputStream(Paths.get(string.substring(1)));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,11 @@ import java.io.File;
|
|||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.Reader;
|
||||||
|
import java.io.Writer;
|
||||||
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
@ -13,6 +18,8 @@ import java.util.HashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.script.Bindings;
|
||||||
|
import javax.script.ScriptContext;
|
||||||
import javax.script.ScriptEngine;
|
import javax.script.ScriptEngine;
|
||||||
import javax.script.ScriptEngineManager;
|
import javax.script.ScriptEngineManager;
|
||||||
import javax.script.ScriptException;
|
import javax.script.ScriptException;
|
||||||
@ -42,7 +49,7 @@ public class RulesManager {
|
|||||||
rules[val.ordinal()] = new ObjectArrayList<Rule>();
|
rules[val.ordinal()] = new ObjectArrayList<Rule>();
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
final Path rulesPath = Paths.get(StaticVars.classLoader.getResource("rules.csv").toURI());
|
final Path rulesPath = Utils.getResource("/rules.csv");
|
||||||
if (!Files.exists(rulesPath)) {
|
if (!Files.exists(rulesPath)) {
|
||||||
throw new FileNotFoundException("rules.csv not found!");
|
throw new FileNotFoundException("rules.csv not found!");
|
||||||
}
|
}
|
||||||
@ -51,12 +58,13 @@ public class RulesManager {
|
|||||||
for (String rulesLine : ruleLines) {
|
for (String rulesLine : ruleLines) {
|
||||||
String[] ruleDetails = rulesLine.split(",", 1);
|
String[] ruleDetails = rulesLine.split(",", 1);
|
||||||
String ruleName = ruleDetails[0];
|
String ruleName = ruleDetails[0];
|
||||||
URL resourceURL = StaticVars.classLoader.getResource("rules" + File.separator + ruleName.replace(".", "_").replace('/', File.separatorChar) + ".js");
|
Utils.out.println("Evaluating /rules/" + ruleName + ".js");
|
||||||
if (resourceURL == null) {
|
InputStream resourcePath = Utils.getResourceStream("/rules/" + ruleName.replace(".", "_") + ".js");
|
||||||
throw new FileNotFoundException("rules/" + ruleName + ".js not found!");
|
if (resourcePath == null) {
|
||||||
|
System.err.println(new FileNotFoundException("/rules/" + ruleName + ".js not found!"));
|
||||||
|
} else {
|
||||||
|
engine.eval(new InputStreamReader(resourcePath));
|
||||||
}
|
}
|
||||||
Path rulePath = Paths.get(resourceURL.toURI());
|
|
||||||
engine.eval(new FileReader(rulePath.toString()));
|
|
||||||
}
|
}
|
||||||
} catch (URISyntaxException | IOException e) {
|
} catch (URISyntaxException | IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -67,7 +75,6 @@ public class RulesManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void addRule(Rule rule) {
|
public static void addRule(Rule rule) {
|
||||||
MathContext mc = new MathContext();
|
|
||||||
rules[rule.getRuleType().ordinal()].add(rule);
|
rules[rule.getRuleType().ordinal()].add(rule);
|
||||||
Utils.out.println(Utils.OUTPUTLEVEL_NODEBUG, "Loaded rule " + rule.getRuleName());
|
Utils.out.println(Utils.OUTPUTLEVEL_NODEBUG, "Loaded rule " + rule.getRuleName());
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user