From 1304755c25bfa35d8580081166957b6350545c32 Mon Sep 17 00:00:00 2001 From: Riccardo Azzolini Date: Mon, 28 Jan 2019 18:19:27 +0100 Subject: [PATCH] Implement loading of DSL rules --- .../warppi/math/rules/RulesManager.java | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/it/cavallium/warppi/math/rules/RulesManager.java b/core/src/main/java/it/cavallium/warppi/math/rules/RulesManager.java index e536030f..18c23d79 100644 --- a/core/src/main/java/it/cavallium/warppi/math/rules/RulesManager.java +++ b/core/src/main/java/it/cavallium/warppi/math/rules/RulesManager.java @@ -21,6 +21,7 @@ import it.cavallium.warppi.math.MathContext; import it.cavallium.warppi.math.functions.Expression; import it.cavallium.warppi.math.functions.Variable; import it.cavallium.warppi.math.functions.Variable.V_TYPE; +import it.cavallium.warppi.math.rules.dsl.RulesDsl; import it.cavallium.warppi.math.solver.MathSolver; import it.cavallium.warppi.util.Error; import it.unimi.dsi.fastutil.objects.ObjectArrayList; @@ -38,7 +39,10 @@ public class RulesManager { for (final RuleType val : RuleType.values()) { RulesManager.rules[val.ordinal()] = new ObjectArrayList<>(); } + try { + loadDslRules(); + boolean compiledSomething = false; InputStream defaultRulesList; try { @@ -148,12 +152,40 @@ public class RulesManager { if (cacheFileStream != null) { cacheFileStream.close(); } - } catch (URISyntaxException | IOException e) { + } catch (URISyntaxException | IOException | RuntimeException e) { e.printStackTrace(); Engine.getPlatform().exit(1); } } + private static void loadDslRules() throws IOException { + final StorageUtils storageUtils = Engine.getPlatform().getStorageUtils(); + + final File dslRulesPath = storageUtils.get("rules/dsl/"); + if (!dslRulesPath.exists()) { + return; + } + + for (final File file : storageUtils.walk(dslRulesPath)) { + if (!file.toString().endsWith(".rules")) { + continue; + } + + Engine.getPlatform().getConsoleUtils().out().println( + ConsoleUtils.OUTPUTLEVEL_NODEBUG, + "RulesManager", + "Found DSL rules file: " + file.getAbsolutePath() + ); + + final String source; + try (final InputStream resource = storageUtils.getResourceStream(file.toString())) { + source = storageUtils.read(resource); + } + + RulesDsl.makeRules(source).forEach(RulesManager::addRule); + } + } + public static Rule compileJavaRule(final String scriptFile, final File tDir) throws IOException, URISyntaxException, InstantiationException, IllegalAccessException, ClassNotFoundException { final InputStream resource = Engine.getPlatform().getStorageUtils().getResourceStream(scriptFile);