Skip to content

Commit

Permalink
Updated Flipper to use GraalJS (ECMAScript 2020), added FlipperEnviro…
Browse files Browse the repository at this point in the history
…nments, updated Dynamic example with FlipperEnvironment, added SLF4J logging to Flipper.
  • Loading branch information
Barachia committed Jul 10, 2020
1 parent d122a25 commit 4aaa2c1
Show file tree
Hide file tree
Showing 29 changed files with 957 additions and 59 deletions.
21 changes: 18 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>hmi.flipper2.0</groupId>
<artifactId>flipper2.0</artifactId>
<version>0.2</version>
<version>1.0</version>
<packaging>jar</packaging>

<name>${project.groupId}:${project.artifactId}</name>
Expand Down Expand Up @@ -59,7 +59,7 @@
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
Expand All @@ -73,16 +73,31 @@
<artifactId>javax.json</artifactId>
<version>1.1.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.5</version>
<version>42.2.14</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.graalvm.js</groupId>
<artifactId>js-scriptengine</artifactId>
<version>20.1.0</version>
</dependency>
<dependency>
<groupId>org.graalvm.js</groupId>
<artifactId>js</artifactId>
<version>20.1.0</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/hmi/flipper2/FlipperException.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

import org.xml.sax.SAXException;

import static hmi.flipper2.TemplateController.logger;

public class FlipperException extends Exception {

/**
Expand Down Expand Up @@ -65,6 +67,7 @@ public FlipperException(Exception ex) {
public FlipperException(Exception ex, String extra) {
_init(ex);
this.extra = extra;
logger.error(this.extra);
}

public FlipperException(SAXException ex) {
Expand Down Expand Up @@ -97,12 +100,12 @@ public void registerCurrentTemplate(String current_tf, String current_id, String

public static void handle(FlipperException e) {
if ( e.extra != null )
System.err.println(e.extra);
logger.error(e.extra);
if ( e.currentInfo != null )
System.err.println(e.currentInfo);
System.err.println("!Caught Exception: "+e.text);
logger.error(e.currentInfo);
logger.error("!Caught Exception: "+e.text);
if (e.stack != null )
System.err.println("!Stack: \n"+e.stack);
logger.error("!Stack: \n"+e.stack);
}

}
4 changes: 3 additions & 1 deletion src/main/java/hmi/flipper2/Is.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import hmi.flipper2.javascript.JsExpression;
import hmi.flipper2.postgres.Database;

import static hmi.flipper2.TemplateController.logger;

public class Is extends JsEngine {

private Database db;
Expand Down Expand Up @@ -75,7 +77,7 @@ public void rollback() throws FlipperException {
for (Map.Entry<String, TemplateFile> entry : this.is_tf_table.entrySet()) {
if (entry.getValue().is_updated) {
entry.getValue().is_updated = false;
System.out.println("INCOMPLETE:Is:rollback: should re-read is after db rollback");
logger.warn("INCOMPLETE:Is:rollback: should re-read is after db rollback");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hmi/flipper2/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static void main(String[] args) {
int count = 0;
boolean changed = true;
while( changed && (count < maxcount) ) {
System.out.println("\nIS:\n---\n"+tc.getIs("is")+"\n");
logger.debug("\nIS:\n---\n"+tc.getIs("is")+"\n");
changed = tc.checkTemplates();
count++;
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/hmi/flipper2/Template.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
import hmi.flipper2.value.JavaValueList;
import hmi.flipper2.value.PersistentJavaValue;

import static hmi.flipper2.TemplateController.logger;

public class Template extends FlipperObject {

public TemplateFile tf;
Expand Down Expand Up @@ -299,7 +301,7 @@ public Set<String> flowIn() {
for(EffectList l : listOfEffectList)
res.addAll(l.flowIn());
this.dfin = res;
System.out.println("TEMPLATE-IN["+id()+"]="+dfin);
logger.debug("TEMPLATE-IN["+id()+"]="+dfin);
return res;
}

Expand All @@ -311,7 +313,7 @@ public Set<String> flowOut() {
for (EffectList l : listOfEffectList)
res.addAll(l.flowOut());
this.dfout = res;
System.out.println("TEMPLATE-OUT["+id()+"]="+dfout);
logger.debug("TEMPLATE-OUT["+id()+"]="+dfout);
return res;
}

Expand Down
14 changes: 11 additions & 3 deletions src/main/java/hmi/flipper2/TemplateController.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import hmi.flipper2.effect.EffectList;
import hmi.flipper2.postgres.Database;
import hmi.flipper2.sax.SimpleSAXParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This class is the main Flipper interface class. You can create/modify/destroy Flipper Templatecontollers using this class.
Expand All @@ -47,6 +49,8 @@
*/

public class TemplateController {

public static Logger logger = LoggerFactory.getLogger(TemplateController.class.getName());

/**
* This method creates a new TemplateController.
Expand Down Expand Up @@ -103,8 +107,6 @@ public static TemplateController create(String name, String descr, Database db,
* The name of the existing TemplateController in the Database.
* @param db
* The Database the TemplateController is stored. When db == null the method does nothing.
* @param jslibs
* String array of additional js libs to preload.
* @exception FlipperException
* On all errors.
*/
Expand Down Expand Up @@ -202,7 +204,7 @@ public void setDebugger(FlipperDebugger fd) {

public void setDebugger(FlipperDebugger fd, boolean onoff) {
if ( !Config.debugging && fd!=null )
System.err.println("WARNING: Setting FlipperDebugger with Config.debugging=false");
logger.error("WARNING: Setting FlipperDebugger with Config.debugging=false");
this.persistent_dbg = fd;
switchDebugger(onoff);
}
Expand Down Expand Up @@ -324,9 +326,15 @@ public boolean checkTemplates(String templateFilter) throws FlipperException {
this.dbg.start_CheckTemplates(this.name, null);
}
for (Template template : filterTemplates(this.base_templates, templateFilter) ) {
long startTime = System.nanoTime();
if ( Config.debugging && this.dbg != null )
this.dbg.start_CheckTemplate(template.id(), null);
changed = checkPreconditions(template, Behaviour.IMMEDIATE_EFFECT) || changed;
long endTime = System.nanoTime();
long duration = (endTime - startTime); //divide by 1000000 to get milliseconds.
if(duration/1000000>50){
logger.debug("Duration Template, ID: " + template.id() + " and duration: " + duration/1000000);
}
// while ( this.conditional_stack != null ) {
// Template toCheck = this.conditional_stack;
// this.conditional_stack = this.conditional_stack.pop();
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/hmi/flipper2/TemplateFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import hmi.flipper2.sax.SimpleElement;
import hmi.flipper2.sax.SimpleSAXParser;

import static hmi.flipper2.TemplateController.logger;

public class TemplateFile extends FlipperObject {

public int tfid = -1;
Expand Down Expand Up @@ -184,7 +186,7 @@ public Set<String> flowIn() {
res.addAll(t.flowIn());
this.dfin = res;
this.dfin_tf = is_var2templatefile(res);
System.out.println("TF-IN["+id()+"]="+dfin_tf);
logger.debug("TF-IN["+id()+"]="+dfin_tf);
return res;
}

Expand All @@ -196,7 +198,7 @@ public Set<String> flowOut() {
res.addAll(t.flowOut());
this.dfout = res;
this.dfout_tf = is_var2templatefile(res);
System.out.println("TF-OUT["+id()+"]="+dfout_tf);
logger.debug("TF-OUT["+id()+"]="+dfout_tf);
return res;
}
}
6 changes: 4 additions & 2 deletions src/main/java/hmi/flipper2/TestScenarios.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import hmi.flipper2.postgres.Database;

import static hmi.flipper2.TemplateController.logger;

public class TestScenarios {

public static void main(String[] args) {
Expand All @@ -38,9 +40,9 @@ public static void openCheckXCloseDestroy() {
//
for(int i=0; i<5; i++) {
tc = new TemplateController(scenario, db);
// System.out.println("\nBEFORE IS:\n---\n"+tc.getIs("is")+"\n");
logger.debug("\nBEFORE IS:\n---\n"+tc.getIs("is")+"\n");
tc.checkTemplates();
System.out.println("\nIS:\n---\n"+tc.getIs("is")+"\n");
logger.debug("\nIS:\n---\n"+tc.getIs("is")+"\n");
tc.close();
}
// TemplateController.destroy(scenario, db);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hmi/flipper2/dataflow/DataFlow.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ else if ( r == EMPTY )

// public static void main(String[] args) {
// Set<String> res = extractRefs("is.a \"xis.\" en is.y.v._z is.x");
// System.out.println("RES:" + res);
// logger.debug("RES:" + res);
// }

}
4 changes: 3 additions & 1 deletion src/main/java/hmi/flipper2/debugger/CpuMemoryUsage.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

import hmi.flipper2.TemplateController;

import static hmi.flipper2.TemplateController.logger;

public class CpuMemoryUsage {

enum Channel {
Expand Down Expand Up @@ -91,7 +93,7 @@ public double getProcessCpuLoad() {
// returns a percentage value with 1 decimal point precision
return ((int) (value * 1000) / 10.0);
} catch (Exception e) {
System.out.println("EXCEPTION: " + e);
logger.error("EXCEPTION: " + e);
return 0.0;
}
}
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/hmi/flipper2/debugger/FlipperDebugger.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import hmi.flipper2.TemplateController;

import static hmi.flipper2.TemplateController.logger;

public class FlipperDebugger {

enum Kind {
Expand All @@ -38,14 +40,14 @@ public void handle_state(FlipperState s) {
boolean verbose = false;

if ( verbose )
System.out.println("#"+s.action.name() + "\t" + s.id + "\t"+pt(s.duration()));
logger.info("#"+s.action.name() + "\t" + s.id + "\t"+pt(s.duration()));
switch (s.action) {
case CheckTemplates: {
System.out.println("[");
logger.info("[");
for (Action a : Action.values()) {
System.out.println("\t+ Total Time: "+ String.format("%1$14s", a.name()) + " = " + pt(this.totalTimes[a.ordinal()]));
logger.info("\t+ Total Time: "+ String.format("%1$14s", a.name()) + " = " + pt(this.totalTimes[a.ordinal()]));
}
System.out.println("]");
logger.info("]");
}
break;
case CheckTemplate:
Expand Down Expand Up @@ -73,12 +75,12 @@ private void handle_startstop(Kind kind, Action action, String other_action, Str
state.startTime = System.nanoTime();
state.startArg = arg;
if ( verbose )
System.out.println(kind.name() + "\t" + String.format("%1$14s", action.name()) + "\t" + id);
logger.info(kind.name() + "\t" + String.format("%1$14s", action.name()) + "\t" + id);
} else {
state.stopTime = System.nanoTime();
state.stopArg = arg;
if ( verbose )
System.out.println(kind.name() + "\t" + String.format("%1$14s", action.name()) + "\t" + id + "\t("+pt(state.duration())+")");
logger.info(kind.name() + "\t" + String.format("%1$14s", action.name()) + "\t" + id + "\t("+pt(state.duration())+")");
this.totalTimes[action.ordinal()] += state.duration();
handle_state(state);
state = state.pop();
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/hmi/flipper2/effect/TemplateEffect.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import hmi.flipper2.Is;
import hmi.flipper2.dataflow.DataFlow;

import static hmi.flipper2.TemplateController.logger;

public class TemplateEffect extends Effect {

private String regexpr;
Expand All @@ -34,8 +36,8 @@ public TemplateEffect(String id, String regexpr, String isregexpr) throws Flippe
throw new FlipperException("TemplateEffect cannot have both regexpr and isregexpr");
this.regexpr = regexpr;
this.isregexpr = isregexpr;
// System.out.println("regexpr="+this.regexpr);
// System.out.println("isregexpr="+this.isregexpr);
logger.debug("regexpr="+this.regexpr);
logger.debug("isregexpr="+this.isregexpr);
}

@Override
Expand Down
Loading

0 comments on commit 4aaa2c1

Please sign in to comment.