Fixed rules loading

This commit is contained in:
Andrea Cavalli 2017-12-23 01:14:38 +01:00
parent bc6609d7dd
commit 8329b09601
3 changed files with 82 additions and 8 deletions

View File

@ -12,7 +12,7 @@ public class StaticVars {
public static boolean debugOn;
public static int outputLevel = 5;
public static final boolean debugWindow2x = true;
public static final ClassLoader classLoader = StaticVars.class.getClassLoader();
public static final Class<?> classLoader = Main.instance.getClass();
private StaticVars() {

View File

@ -4,6 +4,8 @@ import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@ -16,9 +18,22 @@ import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.math.BigInteger;
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.LinkedList;
import java.util.List;
import java.util.Map;
import org.nevec.rjm.BigDecimalMath;
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.EquationsSystemPart;
import com.jogamp.common.util.IOUtil;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
public class Utils {
@ -744,4 +761,54 @@ public class Utils {
t.add(o);
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)));
}
}
}

View File

@ -4,6 +4,11 @@ import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
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.URL;
import java.nio.file.Files;
@ -13,6 +18,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
@ -42,7 +49,7 @@ public class RulesManager {
rules[val.ordinal()] = new ObjectArrayList<Rule>();
}
try {
final Path rulesPath = Paths.get(StaticVars.classLoader.getResource("rules.csv").toURI());
final Path rulesPath = Utils.getResource("/rules.csv");
if (!Files.exists(rulesPath)) {
throw new FileNotFoundException("rules.csv not found!");
}
@ -51,12 +58,13 @@ public class RulesManager {
for (String rulesLine : ruleLines) {
String[] ruleDetails = rulesLine.split(",", 1);
String ruleName = ruleDetails[0];
URL resourceURL = StaticVars.classLoader.getResource("rules" + File.separator + ruleName.replace(".", "_").replace('/', File.separatorChar) + ".js");
if (resourceURL == null) {
throw new FileNotFoundException("rules/" + ruleName + ".js not found!");
Utils.out.println("Evaluating /rules/" + ruleName + ".js");
InputStream resourcePath = Utils.getResourceStream("/rules/" + ruleName.replace(".", "_") + ".js");
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) {
e.printStackTrace();
@ -67,7 +75,6 @@ public class RulesManager {
}
public static void addRule(Rule rule) {
MathContext mc = new MathContext();
rules[rule.getRuleType().ordinal()].add(rule);
Utils.out.println(Utils.OUTPUTLEVEL_NODEBUG, "Loaded rule " + rule.getRuleName());
}