From 9b00639fe4344c44b1bb9f89a65a47c4763e54a1 Mon Sep 17 00:00:00 2001 From: danilarassohin Date: Thu, 23 Mar 2023 23:19:06 +0300 Subject: [PATCH 1/4] Updated README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6c37ebd..fb5fe45 100644 --- a/README.md +++ b/README.md @@ -31,13 +31,13 @@ CompaJ has modular structure based on Maven modules. ___ #### Lang CompaJ uses Groovy under the hood with some tweaks. You can learn Groovy on its official [site](https://groovy-lang.org/documentation.html). -Documentation about CompaJ syntax will be released soon. +Documentation about CompaJ syntax available in [Wiki](https://github.com/CrissNamon/compaj/wiki/CompaJ-Lang). #### Math -CompaJ uses [Apache Commons Math](https://commons.apache.org/proper/commons-math/userguide/index.html) library for math operations. +CompaJ uses [Apache Commons Math](https://commons.apache.org/proper/commons-math/userguide/index.html) library for math operations. Learn about CompaJ math features in [Wiki](https://github.com/CrissNamon/compaj/wiki/CompaJ-Math). #### Visualization -CompaJ uses widgets system for visualization in _WorkSpace_. Documentation will be released soon. +CompaJ uses widgets system for visualization in _WorkSpace_. Documentation available in [Wiki](https://github.com/CrissNamon/compaj/wiki/CompaJ-WorkSpace) #### Other All documentation and necessary information with tutorials will be released soon. From 4acdebdc0ab228192c3ad07d68ed8a9feed32da0 Mon Sep 17 00:00:00 2001 From: danilarassohin Date: Thu, 23 Mar 2023 23:38:29 +0300 Subject: [PATCH 2/4] Added github workflow --- .github/workflows/maven.yml | 23 +++++++++++++ README.md | 2 ++ .../compaj/core/data/base/RealFunction.java | 2 +- .../core/data/base/RealFunctionTest.java | 32 +++++++++++++++++++ pom.xml | 24 ++++++++++++++ 5 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/maven.yml create mode 100644 core/src/test/java/tech/hiddenproject/compaj/core/data/base/RealFunctionTest.java diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml new file mode 100644 index 0000000..259e721 --- /dev/null +++ b/.github/workflows/maven.yml @@ -0,0 +1,23 @@ +name: Build + +on: + push: + branches: [ "dev" ] + pull_request: + branches: [ "main" ] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Set up JDK 11 + uses: actions/setup-java@v3 + with: + java-version: '11' + distribution: 'temurin' + cache: maven + - name: Build with Maven + run: mvn -B test --file pom.xml \ No newline at end of file diff --git a/README.md b/README.md index fb5fe45..48f36c4 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ # CompaJ Project +[![Build](https://github.com/CrissNamon/compaj/actions/workflows/maven.yml/badge.svg)](https://github.com/CrissNamon/compaj/actions/workflows/maven.yml) + This is the home of the CompaJ Project - open source, cross-platform programming and numeric computing platform for math modelling.
diff --git a/core/src/main/java/tech/hiddenproject/compaj/core/data/base/RealFunction.java b/core/src/main/java/tech/hiddenproject/compaj/core/data/base/RealFunction.java index 9e43557..463cf3e 100644 --- a/core/src/main/java/tech/hiddenproject/compaj/core/data/base/RealFunction.java +++ b/core/src/main/java/tech/hiddenproject/compaj/core/data/base/RealFunction.java @@ -42,7 +42,7 @@ public String toString() { } @Override - public double value(double[] doubles) { + public double value(double... doubles) { return value(ArrayUtils.toObject(doubles)); } diff --git a/core/src/test/java/tech/hiddenproject/compaj/core/data/base/RealFunctionTest.java b/core/src/test/java/tech/hiddenproject/compaj/core/data/base/RealFunctionTest.java new file mode 100644 index 0000000..e246efa --- /dev/null +++ b/core/src/test/java/tech/hiddenproject/compaj/core/data/base/RealFunctionTest.java @@ -0,0 +1,32 @@ +package tech.hiddenproject.compaj.core.data.base; + +import java.util.Random; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +@DisplayName("RealFunctionTest should > ") +public class RealFunctionTest { + + @Test + @DisplayName("bind and execute function success") + public void valueTest() { + int expected = 5; + + RealFunction realFunction = new RealFunction("MyFunction"); + realFunction.b(x -> x[0] + x[1]); + Assertions.assertEquals(expected, realFunction.value(3, 2)); + } + + @Test + @DisplayName("bind single value and execute function success") + public void singleValueTest() { + RealFunction realFunction = new RealFunction("MyFunction"); + + double expected = new Random().nextDouble(); + realFunction.b(expected); + Assertions.assertEquals(expected, realFunction.value()); + } + +} diff --git a/pom.xml b/pom.xml index 2118f4c..518f68b 100644 --- a/pom.xml +++ b/pom.xml @@ -26,6 +26,20 @@ gui lang-extensions + + + org.junit.jupiter + junit-jupiter-api + 5.9.2 + test + + + org.junit.jupiter + junit-jupiter-engine + 5.9.2 + test + + @@ -52,6 +66,16 @@ + + maven-surefire-plugin + + + *.java + + + org.apache.maven.plugins + 3.0.0-M5 + \ No newline at end of file From d32a3a7d6f93b789c77fc886798ca53879e75b8f Mon Sep 17 00:00:00 2001 From: danilarassohin Date: Thu, 23 Mar 2023 23:42:53 +0300 Subject: [PATCH 3/4] Added github workflow --- .github/workflows/maven.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 259e721..8bcecbe 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -4,7 +4,7 @@ on: push: branches: [ "dev" ] pull_request: - branches: [ "main" ] + branches: [ "main", "dev" ] jobs: build: From fb5abafc6ff2c3197c6972b74cd06f868c168704 Mon Sep 17 00:00:00 2001 From: danilarassohin Date: Fri, 24 Mar 2023 00:22:37 +0300 Subject: [PATCH 4/4] Added tests --- .../base/FirstOrderDifferentialModel.java | 5 +- .../core/data/base/RealFunctionTest.java | 1 + .../data/base/RealVectorFunctionTest.java | 32 ++++++ .../compaj/lang/groovy/CompaJScriptBase.java | 2 +- .../hiddenproject/compaj/repl/CompaJ.java | 6 +- .../tech/hiddenproject/compaj/repl/Main.java | 16 +-- .../hiddenproject/compaj/repl/REPLTest.java | 97 +++++++++++++++++++ repl/src/test/resources/test_script_1.cjn | 1 + 8 files changed, 147 insertions(+), 13 deletions(-) create mode 100644 core/src/test/java/tech/hiddenproject/compaj/core/data/base/RealVectorFunctionTest.java create mode 100644 repl/src/test/java/tech/hiddenproject/compaj/repl/REPLTest.java create mode 100644 repl/src/test/resources/test_script_1.cjn diff --git a/core/src/main/java/tech/hiddenproject/compaj/core/model/base/FirstOrderDifferentialModel.java b/core/src/main/java/tech/hiddenproject/compaj/core/model/base/FirstOrderDifferentialModel.java index e9546c9..aaa05b4 100644 --- a/core/src/main/java/tech/hiddenproject/compaj/core/model/base/FirstOrderDifferentialModel.java +++ b/core/src/main/java/tech/hiddenproject/compaj/core/model/base/FirstOrderDifferentialModel.java @@ -35,7 +35,7 @@ public void computeDerivatives(double v, double[] y, double[] yDot) throws MaxCountExceededException, DimensionMismatchException { int i = 0; for (NamedFunction entry : fns().values()) { - yDot[i] = entry.value(new Double[] {}); + yDot[i] = entry.value(); i++; } if (iterator > 0) { @@ -61,7 +61,8 @@ public String toString() { return baseModel.toString(); } - public void a(NamedFunction... e) { + @SafeVarargs + public final void a(NamedFunction... e) { baseModel.a(e); } diff --git a/core/src/test/java/tech/hiddenproject/compaj/core/data/base/RealFunctionTest.java b/core/src/test/java/tech/hiddenproject/compaj/core/data/base/RealFunctionTest.java index e246efa..851141b 100644 --- a/core/src/test/java/tech/hiddenproject/compaj/core/data/base/RealFunctionTest.java +++ b/core/src/test/java/tech/hiddenproject/compaj/core/data/base/RealFunctionTest.java @@ -26,6 +26,7 @@ public void singleValueTest() { double expected = new Random().nextDouble(); realFunction.b(expected); + Assertions.assertEquals(expected, realFunction.value()); } diff --git a/core/src/test/java/tech/hiddenproject/compaj/core/data/base/RealVectorFunctionTest.java b/core/src/test/java/tech/hiddenproject/compaj/core/data/base/RealVectorFunctionTest.java new file mode 100644 index 0000000..5fc61cd --- /dev/null +++ b/core/src/test/java/tech/hiddenproject/compaj/core/data/base/RealVectorFunctionTest.java @@ -0,0 +1,32 @@ +package tech.hiddenproject.compaj.core.data.base; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +@DisplayName("RealVectorFunctionTest should > ") +public class RealVectorFunctionTest { + + @Test + @DisplayName("bind and execute function success") + public void valueTest() { + Double[] expected = new Double[]{0d, 1d}; + + RealVectorFunction realFunction = new RealVectorFunction("MyFunction"); + realFunction.b(x -> x); + + Assertions.assertEquals(expected, realFunction.value(expected)); + } + + @Test + @DisplayName("bind single value and execute function success") + public void singleValueTest() { + Double[] expected = new Double[]{0d, 1d}; + + RealVectorFunction realFunction = new RealVectorFunction("MyFunction"); + realFunction.b(expected); + + Assertions.assertEquals(expected, realFunction.value()); + } + +} diff --git a/lang/src/main/groovy/tech/hiddenproject/compaj/lang/groovy/CompaJScriptBase.java b/lang/src/main/groovy/tech/hiddenproject/compaj/lang/groovy/CompaJScriptBase.java index 54394ef..add7abe 100644 --- a/lang/src/main/groovy/tech/hiddenproject/compaj/lang/groovy/CompaJScriptBase.java +++ b/lang/src/main/groovy/tech/hiddenproject/compaj/lang/groovy/CompaJScriptBase.java @@ -8,7 +8,7 @@ public abstract class CompaJScriptBase extends Script { - private static Set extensions; + private static final Set extensions; static { extensions = new HashSet<>(); diff --git a/repl/src/main/java/tech/hiddenproject/compaj/repl/CompaJ.java b/repl/src/main/java/tech/hiddenproject/compaj/repl/CompaJ.java index 31c96d8..c7139c7 100644 --- a/repl/src/main/java/tech/hiddenproject/compaj/repl/CompaJ.java +++ b/repl/src/main/java/tech/hiddenproject/compaj/repl/CompaJ.java @@ -32,7 +32,7 @@ public class CompaJ { private Translator translator; - { + static { CompaJScriptBase.addExtension(new StarterExtension()); CompaJScriptBase.addExtension(new MathExtension()); CompaJScriptBase.addExtension(new ArrayRealVectorExtension()); @@ -42,7 +42,7 @@ public class CompaJ { CompaJScriptBase.addExtension(new AgentExtension()); } - { + static { GroovyTranslator.getImportCustomizer() .addImports(normalImports); } @@ -54,7 +54,7 @@ public static void readFile(String url) { try { Path path = Paths.get(url); String script = new String(Files.readAllBytes(path)); - getInstance().getTranslator().evaluate(script); + readInput(script); } catch (IOException e) { System.out.println("Error: " + e.getLocalizedMessage()); } diff --git a/repl/src/main/java/tech/hiddenproject/compaj/repl/Main.java b/repl/src/main/java/tech/hiddenproject/compaj/repl/Main.java index fe2b094..df9d52f 100644 --- a/repl/src/main/java/tech/hiddenproject/compaj/repl/Main.java +++ b/repl/src/main/java/tech/hiddenproject/compaj/repl/Main.java @@ -10,20 +10,22 @@ public class Main { - private static Translator translator; + public static void main(String... args) { + init(); + processArgs(args); + System.out.println("Welcome to CompaJ REPL!"); + System.out.println("Version 0.0.3"); + processInput(); + } - public static void main(String[] args) { + protected static void init() { TranslatorUtils translatorUtils = new GroovyTranslatorUtils(); GroovyTranslator.getImportCustomizer() .addImports("tech.hiddenproject.compaj.repl.CompaJ") .addStaticImport("tech.hiddenproject.compaj.repl.CompaJ", "exit"); - translator = new GroovyTranslator(translatorUtils); + Translator translator = new GroovyTranslator(translatorUtils); CompaJ.getInstance().setTranslator(translator); - processArgs(args); - System.out.println("Welcome to CompaJ REPL!"); - System.out.println("Version 0.0.3"); - processInput(); } private static void processArgs(String... args) { diff --git a/repl/src/test/java/tech/hiddenproject/compaj/repl/REPLTest.java b/repl/src/test/java/tech/hiddenproject/compaj/repl/REPLTest.java new file mode 100644 index 0000000..be58acc --- /dev/null +++ b/repl/src/test/java/tech/hiddenproject/compaj/repl/REPLTest.java @@ -0,0 +1,97 @@ +package tech.hiddenproject.compaj.repl; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.net.URL; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.DoubleStream; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import tech.hiddenproject.compaj.extension.CompaJComplex; + +@DisplayName("REPLTest should > ") +public class REPLTest { + + private static final PrintStream STANDARD_OUT = System.out; + private static final ByteArrayOutputStream OUTPUT_CAPTOR = new ByteArrayOutputStream(); + private static final ClassLoader CLASS_LOADER = REPLTest.class.getClassLoader(); + + @BeforeAll + public static void init() { + System.setOut(new PrintStream(OUTPUT_CAPTOR)); + Main.init(); + } + + @BeforeEach + public void resetCaptor() { + OUTPUT_CAPTOR.reset(); + } + + @AfterAll + public static void release() { + System.setOut(STANDARD_OUT); + } + + @Test + @DisplayName("read input from string") + public void inputStringTest() { + String expected = "5"; + + String script = "print " + expected; + CompaJ.readInput(script); + + Assertions.assertEquals(expected, OUTPUT_CAPTOR.toString().trim()); + } + + @Test + @DisplayName("read input from file") + public void inputFileTest() { + URL fileUrl = CLASS_LOADER.getResource("test_script_1.cjn"); + + Assertions.assertNotNull(fileUrl); + + String expected = "5"; + + CompaJ.readFile(fileUrl.getPath()); + + Assertions.assertEquals(expected, OUTPUT_CAPTOR.toString().trim()); + } + + @Test + @DisplayName("load CompaJComplex extension") + public void complexExtensionTest() { + String expected = new CompaJComplex(0, 1).toString(); + + String script = "print new CompaJComplex(0, 1)"; + CompaJ.readInput(script); + + Assertions.assertEquals(expected, OUTPUT_CAPTOR.toString().trim()); + } + + @Test + @DisplayName("load Math extension") + public void mathExtensionTest() { + String expected = doubleList().stream() + .map(Math::sin) + .collect(Collectors.toList()).toString(); + + String script = "print sin(" + doubleList().toString() + ")"; + + CompaJ.readInput("print \"\""); + CompaJ.readInput(script); + + Assertions.assertEquals(expected, OUTPUT_CAPTOR.toString().trim()); + } + + private List doubleList() { + return DoubleStream.iterate(0.0, d -> d <= 10.0, d -> d + 1) + .boxed() + .collect(Collectors.toList()); + } +} diff --git a/repl/src/test/resources/test_script_1.cjn b/repl/src/test/resources/test_script_1.cjn new file mode 100644 index 0000000..c70bfd7 --- /dev/null +++ b/repl/src/test/resources/test_script_1.cjn @@ -0,0 +1 @@ +print 5 \ No newline at end of file