2017-12-21 23:21:29 +01:00
|
|
|
package org.warp.picalculator.math.rules;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileNotFoundException;
|
2018-03-22 10:06:31 +01:00
|
|
|
import java.io.FileOutputStream;
|
2017-12-21 23:21:29 +01:00
|
|
|
import java.io.FileReader;
|
|
|
|
import java.io.IOException;
|
2017-12-23 01:14:38 +01:00
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
2018-03-22 10:06:31 +01:00
|
|
|
import java.io.ObjectInputStream;
|
|
|
|
import java.io.ObjectOutputStream;
|
|
|
|
import java.io.OutputStream;
|
2018-03-22 20:40:41 +01:00
|
|
|
import java.io.PrintWriter;
|
2017-12-23 01:14:38 +01:00
|
|
|
import java.io.Reader;
|
|
|
|
import java.io.Writer;
|
|
|
|
import java.net.URI;
|
2017-12-21 23:21:29 +01:00
|
|
|
import java.net.URISyntaxException;
|
|
|
|
import java.net.URL;
|
2018-03-22 20:40:41 +01:00
|
|
|
import java.net.URLClassLoader;
|
2017-12-21 23:21:29 +01:00
|
|
|
import java.nio.file.Files;
|
2018-03-22 20:40:41 +01:00
|
|
|
import java.nio.file.OpenOption;
|
2017-12-21 23:21:29 +01:00
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.nio.file.Paths;
|
2018-03-22 10:06:31 +01:00
|
|
|
import java.nio.file.StandardOpenOption;
|
|
|
|
import java.nio.file.attribute.BasicFileAttributes;
|
2017-12-21 23:21:29 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2018-03-22 20:40:41 +01:00
|
|
|
import java.util.zip.ZipFile;
|
2017-12-21 23:21:29 +01:00
|
|
|
|
2017-12-23 01:14:38 +01:00
|
|
|
import javax.script.Bindings;
|
2018-03-22 10:06:31 +01:00
|
|
|
import javax.script.Compilable;
|
|
|
|
import javax.script.CompiledScript;
|
2017-12-23 01:14:38 +01:00
|
|
|
import javax.script.ScriptContext;
|
2017-12-21 23:21:29 +01:00
|
|
|
import javax.script.ScriptEngine;
|
|
|
|
import javax.script.ScriptEngineManager;
|
|
|
|
import javax.script.ScriptException;
|
|
|
|
|
2018-03-22 20:40:41 +01:00
|
|
|
import org.eclipse.jdt.core.JDTCompilerAdapter;
|
2017-12-23 15:20:42 +01:00
|
|
|
import org.warp.picalculator.Error;
|
2017-12-21 23:21:29 +01:00
|
|
|
import org.warp.picalculator.StaticVars;
|
2017-12-22 22:39:58 +01:00
|
|
|
import org.warp.picalculator.Utils;
|
2017-12-23 15:20:42 +01:00
|
|
|
import org.warp.picalculator.math.Function;
|
2017-12-21 23:21:29 +01:00
|
|
|
import org.warp.picalculator.math.MathContext;
|
2017-12-23 15:20:42 +01:00
|
|
|
import org.warp.picalculator.math.functions.Expression;
|
2017-12-21 23:21:29 +01:00
|
|
|
import org.warp.picalculator.math.functions.Subtraction;
|
|
|
|
import org.warp.picalculator.math.functions.Sum;
|
|
|
|
import org.warp.picalculator.math.functions.SumSubtraction;
|
|
|
|
import org.warp.picalculator.math.functions.Variable;
|
|
|
|
import org.warp.picalculator.math.functions.Variable.V_TYPE;
|
2018-03-01 09:54:14 +01:00
|
|
|
import org.warp.picalculator.math.solver.MathSolver;
|
2017-12-21 23:21:29 +01:00
|
|
|
|
2018-03-22 20:40:41 +01:00
|
|
|
import com.jogamp.common.util.IOUtil;
|
|
|
|
|
2017-12-21 23:21:29 +01:00
|
|
|
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
|
|
|
|
|
|
|
public class RulesManager {
|
|
|
|
|
|
|
|
public static ObjectArrayList<Rule>[] rules;
|
|
|
|
|
|
|
|
private RulesManager() {
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public static void initialize() {
|
2018-03-22 20:40:41 +01:00
|
|
|
Utils.out.println(Utils.OUTPUTLEVEL_NODEBUG, "RulesManager", "Loading the rules");
|
2017-12-21 23:21:29 +01:00
|
|
|
rules = new ObjectArrayList[RuleType.values().length];
|
|
|
|
for (RuleType val : RuleType.values()) {
|
|
|
|
rules[val.ordinal()] = new ObjectArrayList<Rule>();
|
|
|
|
}
|
|
|
|
try {
|
2018-03-22 20:40:41 +01:00
|
|
|
final Path rulesPath = Utils.getResource("/rules.csv");
|
2017-12-21 23:21:29 +01:00
|
|
|
if (!Files.exists(rulesPath)) {
|
2018-03-22 20:40:41 +01:00
|
|
|
throw new FileNotFoundException("rules.csv not found!");
|
2017-12-21 23:21:29 +01:00
|
|
|
}
|
|
|
|
List<String> ruleLines = Files.readAllLines(rulesPath);
|
2017-12-24 11:59:09 +01:00
|
|
|
ruleLines.remove(0); //Remove the headers
|
2018-03-22 20:40:41 +01:00
|
|
|
|
|
|
|
boolean useCache = false;
|
|
|
|
Path tDir = Paths.get(System.getProperty("java.io.tmpdir"), "WarpPi-Calculator").resolve("rules-rt");
|
|
|
|
Path cacheFilePath = Paths.get(Utils.getJarDirectory().toString()).resolve("math-rules-cache.zip").toAbsolutePath();
|
|
|
|
if (cacheFilePath.toFile().exists()) {
|
|
|
|
try {
|
|
|
|
if (tDir.toFile().exists()) {
|
|
|
|
tDir.toFile().delete();
|
|
|
|
}
|
|
|
|
Utils.unzip(cacheFilePath.toString(), tDir.getParent().toString(), "");
|
|
|
|
useCache = !StaticVars.debugOn;
|
|
|
|
cacheFilePath.toFile().delete();
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2017-12-21 23:21:29 +01:00
|
|
|
for (String rulesLine : ruleLines) {
|
2017-12-24 11:59:09 +01:00
|
|
|
if (rulesLine.length() > 0) {
|
|
|
|
String[] ruleDetails = rulesLine.split(",", 1);
|
|
|
|
String ruleName = ruleDetails[0];
|
2018-03-22 10:06:31 +01:00
|
|
|
String ruleNameEscaped = ruleName.replace(".", "_");
|
2018-03-22 20:40:41 +01:00
|
|
|
Utils.out.println(Utils.OUTPUTLEVEL_NODEBUG, "RulesManager", "Evaluating /rules/" + ruleNameEscaped + ".java");
|
2018-03-22 10:06:31 +01:00
|
|
|
String pathWithoutExtension = "/rules/" + ruleNameEscaped;
|
|
|
|
String scriptFile = pathWithoutExtension + ".java";
|
|
|
|
InputStream resourcePath = Utils.getResourceStream(scriptFile);
|
2017-12-24 11:59:09 +01:00
|
|
|
if (resourcePath == null) {
|
2018-03-22 10:06:31 +01:00
|
|
|
System.err.println(new FileNotFoundException("/rules/" + ruleName + ".java not found!"));
|
2017-12-24 11:59:09 +01:00
|
|
|
} else {
|
2018-03-22 20:40:41 +01:00
|
|
|
Rule r = null;
|
|
|
|
if (useCache) {
|
|
|
|
try {
|
|
|
|
Utils.out.println(Utils.OUTPUTLEVEL_DEBUG_MIN, "RulesManager", ruleName, "Trying to load cached rule");
|
|
|
|
r = loadClassRuleFromSourceFile(scriptFile, tDir);
|
|
|
|
if (r != null) {
|
|
|
|
Utils.out.println(Utils.OUTPUTLEVEL_DEBUG_MIN, "RulesManager", ruleName, "Loaded cached rule");
|
2018-03-22 10:06:31 +01:00
|
|
|
}
|
2018-03-22 20:40:41 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
Utils.out.println(Utils.OUTPUTLEVEL_NODEBUG, "RulesManager", ruleName, "Can't load the rule!");
|
2018-03-22 10:06:31 +01:00
|
|
|
}
|
|
|
|
}
|
2018-03-22 20:40:41 +01:00
|
|
|
if (r == null || !useCache) {
|
|
|
|
Utils.out.println(Utils.OUTPUTLEVEL_DEBUG_MIN, "RulesManager", ruleName, "This rule is not cached. Compiling");
|
2018-03-22 10:06:31 +01:00
|
|
|
try {
|
2018-03-22 20:40:41 +01:00
|
|
|
r = compileJavaRule(scriptFile, tDir);
|
2018-03-24 23:33:04 +01:00
|
|
|
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException | IOException e) {
|
2018-03-22 10:06:31 +01:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-03-24 23:33:04 +01:00
|
|
|
if (r != null) {
|
|
|
|
RulesManager.addRule(r);
|
|
|
|
}
|
2017-12-24 11:59:09 +01:00
|
|
|
}
|
2017-12-21 23:21:29 +01:00
|
|
|
}
|
|
|
|
}
|
2018-03-22 20:40:41 +01:00
|
|
|
Utils.out.println(Utils.OUTPUTLEVEL_NODEBUG, "RulesManager", "Loaded all the rules successfully");
|
|
|
|
Utils.zip(tDir.toString(), cacheFilePath.toString(), "");
|
|
|
|
Utils.out.println(Utils.OUTPUTLEVEL_NODEBUG, "RulesManager", "Cached the compiled rules");
|
2017-12-21 23:21:29 +01:00
|
|
|
} catch (URISyntaxException | IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
System.exit(1);
|
|
|
|
}
|
|
|
|
}
|
2018-03-22 10:06:31 +01:00
|
|
|
|
2018-03-22 20:40:41 +01:00
|
|
|
public static Rule compileJavaRule(String scriptFile, Path tDir) throws IOException, URISyntaxException, InstantiationException, IllegalAccessException, ClassNotFoundException {
|
|
|
|
InputStream resource = Utils.getResourceStream(scriptFile);
|
|
|
|
String text = Utils.read(resource);
|
|
|
|
String[] textArray = text.split("\\n", 5);
|
2018-03-24 23:33:04 +01:00
|
|
|
System.err.println(text);
|
2018-03-22 20:40:41 +01:00
|
|
|
String javaClassName = textArray[2].substring(6);
|
|
|
|
String javaClassNameAndPath = new StringBuilder("org.warp.picalculator.math.rules.").append(javaClassName).toString();
|
|
|
|
final int extIndex = javaClassNameAndPath.lastIndexOf('.');
|
|
|
|
String javaCode = new StringBuilder("package ").append(javaClassNameAndPath.substring(0, extIndex >= 0 ? extIndex : javaClassNameAndPath.length())).append(";\n")
|
|
|
|
.append(textArray[4]).toString();
|
|
|
|
Path tDirPath = tDir.resolve(javaClassNameAndPath.replace('.', File.separatorChar)).getParent();
|
|
|
|
Path tFileJava = tDirPath.resolve(javaClassName + ".java");
|
|
|
|
Path tFileClass = tDirPath.resolve(javaClassName + ".class");
|
|
|
|
if (!tDirPath.toFile().exists()) {
|
|
|
|
Files.createDirectories(tDirPath);
|
|
|
|
}
|
|
|
|
Files.write(tFileJava, javaCode.getBytes("UTF-8"), StandardOpenOption.WRITE, StandardOpenOption.CREATE);
|
|
|
|
boolean compiled = org.eclipse.jdt.internal.compiler.batch.Main.compile(new String[] {"-nowarn", "-1.8", tFileJava.toString()}, new PrintWriter(System.out), new PrintWriter(System.err), null);
|
|
|
|
if (StaticVars.debugOn) {
|
|
|
|
tFileJava.toFile().deleteOnExit();
|
|
|
|
} else {
|
|
|
|
tFileJava.toFile().delete();
|
|
|
|
}
|
|
|
|
if (compiled) {
|
|
|
|
tFileClass.toFile().deleteOnExit();
|
|
|
|
return loadClassRuleDirectly(javaClassNameAndPath, tDir);
|
|
|
|
} else {
|
|
|
|
throw new IOException("Can't build script file '" + scriptFile + "'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Rule loadClassRuleFromSourceFile(String scriptFile, Path tDir) throws IOException, URISyntaxException, InstantiationException, IllegalAccessException, ClassNotFoundException {
|
2018-03-22 10:06:31 +01:00
|
|
|
InputStream resource = Utils.getResourceStream(scriptFile);
|
|
|
|
String text = Utils.read(resource);
|
2018-03-22 20:40:41 +01:00
|
|
|
String[] textArray = text.split("\\n", 5);
|
|
|
|
String javaClassName = textArray[2].substring(6);
|
|
|
|
String javaClassNameAndPath = new StringBuilder("org.warp.picalculator.math.rules.").append(javaClassName).toString();
|
|
|
|
try {
|
|
|
|
return loadClassRuleDirectly(javaClassNameAndPath, tDir);
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Rule loadClassRuleDirectly(String javaClassNameAndPath, Path tDir) throws IOException, URISyntaxException, InstantiationException, IllegalAccessException, ClassNotFoundException {
|
|
|
|
URLClassLoader cl = new URLClassLoader(new URL[] {tDir.toUri().toURL()});
|
|
|
|
Class<?> aClass = cl.loadClass(javaClassNameAndPath);
|
|
|
|
cl.close();
|
|
|
|
return (Rule) aClass.newInstance();
|
2018-03-22 10:06:31 +01:00
|
|
|
}
|
|
|
|
|
2017-12-23 15:20:42 +01:00
|
|
|
public static void warmUp() {
|
|
|
|
ObjectArrayList<Function> uselessResult = null;
|
2018-02-05 00:05:31 +01:00
|
|
|
boolean uselessVariable = false;
|
2017-12-23 15:20:42 +01:00
|
|
|
for (RuleType val : RuleType.values()) {
|
|
|
|
final ObjectArrayList<Rule> ruleList = rules[val.ordinal()];
|
|
|
|
for (final Rule rule : ruleList) {
|
2018-02-05 00:05:31 +01:00
|
|
|
String ruleName = "<null>";
|
|
|
|
try {
|
|
|
|
ruleName = rule.getRuleName();
|
|
|
|
ObjectArrayList<Function> uselessResult2 = rule.execute(generateUselessExpression());
|
|
|
|
uselessVariable = (uselessResult == null ? new ObjectArrayList<>() : uselessResult).equals(uselessResult2);
|
|
|
|
uselessResult = uselessResult2;
|
|
|
|
} catch (Exception e) {
|
|
|
|
if (uselessVariable || true) {
|
|
|
|
System.err.println("Exception thrown by rule '" + ruleName + "'!");
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2017-12-23 15:20:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
new MathSolver(generateUselessExpression()).solveAllSteps();
|
|
|
|
} catch (InterruptedException | Error e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-24 11:59:09 +01:00
|
|
|
private static Function generateUselessExpression() {
|
2017-12-23 15:20:42 +01:00
|
|
|
MathContext mc = new MathContext();
|
2017-12-24 11:59:09 +01:00
|
|
|
Function expr = new Expression(mc);
|
|
|
|
expr = expr.setParameter(0, new Variable(mc, 'x', V_TYPE.VARIABLE));
|
2017-12-23 15:20:42 +01:00
|
|
|
return expr;
|
|
|
|
}
|
2017-12-21 23:21:29 +01:00
|
|
|
|
|
|
|
public static void addRule(Rule rule) {
|
2017-12-22 22:39:58 +01:00
|
|
|
rules[rule.getRuleType().ordinal()].add(rule);
|
2018-03-22 20:40:41 +01:00
|
|
|
Utils.out.println(Utils.OUTPUTLEVEL_NODEBUG, "RulesManager", rule.getRuleName(), "Loaded as " + rule.getRuleType() + " rule");
|
2017-12-21 23:21:29 +01:00
|
|
|
}
|
|
|
|
}
|