Skip to content

Commit

Permalink
add aot helloworld, change original helloworld to be interpreted
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-higgins committed Apr 21, 2024
1 parent 8d9f303 commit 86ffe70
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 22 deletions.
32 changes: 32 additions & 0 deletions imperative-helloworld-aot/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.fluxtion.example</groupId>
<artifactId>example.master</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>imperative-helloworld-aot</artifactId>
<name>imperative :: hello world - AOT</name>

<build>
<plugins>
<plugin>
<groupId>com.fluxtion</groupId>
<artifactId>fluxtion-maven-plugin</artifactId>
<version>3.0.14</version>
<executions>
<execution>
<goals>
<goal>scan</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import com.fluxtion.compiler.EventProcessorConfig;
import com.fluxtion.compiler.FluxtionCompilerConfig;
import com.fluxtion.compiler.FluxtionGraphBuilder;
import com.fluxtion.example.imperative.helloworld.generated.BreachNotifierProcessor;

/**
* This class is discovered by the maven plugin to generate an {@link com.fluxtion.runtime.EventProcessor} ahead of time
* during the compilation phase. The output is the {@link com.fluxtion.example.imperative.helloworld.generated.BreachNotifierProcessor} source
* during the compilation phase. The output is the {@link BreachNotifierProcessor} source
* file.
* <p></p>
* Nodes are added to the graph using the {{@link #buildGraph(EventProcessorConfig)}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.fluxtion.example.imperative.helloworld;

import com.fluxtion.runtime.annotations.OnTrigger;

/**
* The trigger method, printWarning on this class is invoked when a change is propagated from the parent node
*/
public class BreachNotifier {
private final DataSumCalculator dataAddition;

public BreachNotifier(DataSumCalculator dataAddition) {
this.dataAddition = dataAddition;
}

public BreachNotifier() {
this(new DataSumCalculator());
}

@OnTrigger
public boolean printWarning() {
System.out.println("WARNING DataSumCalculator value is greater than 100 sum = " + dataAddition.getSum());
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.fluxtion.example.imperative.helloworld;

import com.fluxtion.runtime.annotations.OnTrigger;
import lombok.Getter;

/**
* Aggregates two event sources and calculates the sum of their values, whenever either changes. The calculate method
* notifies a propagation of a change when the sum is > 100
*/
public class DataSumCalculator {

private final Event_A_Handler eventAHandler;
private final Event_B_Handler eventBHandler;
@Getter
private double sum;

public DataSumCalculator(Event_A_Handler eventAHandler, Event_B_Handler eventBHandler) {
this.eventAHandler = eventAHandler;
this.eventBHandler = eventBHandler;
}

public DataSumCalculator() {
this(new Event_A_Handler(), new Event_B_Handler());
}

/**
* The {@link OnTrigger} annotation marks this method to be called if any parents have changed
*
* @return flag indicating a change and a propagation of the event wave to child dependencies if the sum > 100
*/
@OnTrigger
public boolean calculate() {
sum = eventAHandler.getValue() + eventBHandler.getValue();
System.out.println("sum:" + sum);
return sum > 100;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.fluxtion.example.imperative.helloworld;

public record Event_A(double value) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.fluxtion.example.imperative.helloworld;

import com.fluxtion.runtime.annotations.OnEventHandler;
import lombok.Getter;

/**
* An entry point to {@link com.fluxtion.runtime.EventProcessor} for {@link Event_A} events. The {@link OnEventHandler}
* defines a method as an entry point.
*
*/
@Getter
public class Event_A_Handler {
private double value;

/**
* The {@link OnEventHandler} annotation marks this method as the start of an execution path with a {@link Event_A}.
* Invoked when the {@link com.fluxtion.runtime.EventProcessor} receives a {@link Event_A} event.
*
* @param eventA the input event
* @return flag indicating a change and a propagation of the event wave to child dependencies
*/
@OnEventHandler
public boolean data1Update(Event_A eventA) {
value = eventA.value();
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.fluxtion.example.imperative.helloworld;

public record Event_B(double value) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.fluxtion.example.imperative.helloworld;

import com.fluxtion.runtime.annotations.OnEventHandler;
import lombok.Getter;

/**
* An entry point to {@link com.fluxtion.runtime.EventProcessor} for {@link Event_B} events. The {@link OnEventHandler}
* defines a method as an entry point.
*
*/
@Getter
public class Event_B_Handler {
private double value;

/**
* The {@link OnEventHandler} annotation marks this method as the start of an execution path with a {@link Event_B}.
* Invoked when the {@link com.fluxtion.runtime.EventProcessor} receives a {@link Event_B} event.
*
* @param eventB the input event
* @return flag indicating a change and a propagation of the event wave to child dependencies
*/
@OnEventHandler
public boolean data1Update(Event_B eventB) {
value = eventB.value();
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.fluxtion.example.imperative.helloworld;

import com.fluxtion.compiler.Fluxtion;
import com.fluxtion.example.imperative.helloworld.generated.BreachNotifierProcessor;
import com.fluxtion.runtime.annotations.OnEventHandler;
import com.fluxtion.runtime.annotations.OnTrigger;


/**
* creates a processing graph imperatively, extracts double values from events, calculates the sum and prints a
* message to console if the sum is greater than a 100.
* <p>
* Uses @{@link OnEventHandler} annotation to declare the entry point of an execution path
* {@link OnTrigger} annotated methods indicate call back methods to be invoked if a parent propagates a change.
* The return flag from the {@link OnTrigger} method indicates if the event should be propagated. In this case
* the event is only propagated if the sum > 100.
*/
public class Main {
private static final boolean USE_AOT = true;

public static void main(String[] args) {
var eventProcessor = USE_AOT ? new BreachNotifierProcessor() : Fluxtion.interpret(new BreachNotifier());
eventProcessor.init();
eventProcessor.onEvent(new Event_A(34.4));
eventProcessor.onEvent(new Event_B(52.1));
eventProcessor.onEvent(new Event_A(105));//should create a breach warning
eventProcessor.onEvent(new Event_A(12.4));
}

}
20 changes: 1 addition & 19 deletions imperative-helloworld/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,13 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>example.master</artifactId>
<groupId>com.fluxtion.example</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>imperative-helloworld</artifactId>
<name>imperative :: hello world</name>

<build>
<plugins>
<plugin>
<groupId>com.fluxtion</groupId>
<artifactId>fluxtion-maven-plugin</artifactId>
<version>3.0.14</version>
<executions>
<execution>
<goals>
<goal>scan</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.fluxtion.example.imperative.helloworld;

import com.fluxtion.compiler.Fluxtion;
import com.fluxtion.example.imperative.helloworld.generated.BreachNotifierProcessor;
import com.fluxtion.runtime.annotations.OnEventHandler;
import com.fluxtion.runtime.annotations.OnTrigger;

Expand All @@ -19,7 +18,7 @@ public class Main {
private static final boolean USE_AOT = true;

public static void main(String[] args) {
var eventProcessor = USE_AOT ? new BreachNotifierProcessor() : Fluxtion.interpret(new BreachNotifier());
var eventProcessor = Fluxtion.interpret(new BreachNotifier());
eventProcessor.init();
eventProcessor.onEvent(new Event_A(34.4));
eventProcessor.onEvent(new Event_B(52.1));
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ along with this program. If not, see
<module>unplugged/part1</module>
<module>imperative-helloworld</module>
<module>functional-helloworld</module>
<module>imperative-helloworld-aot</module>
<module>cookbook</module>
<module>cookbook-functional</module>
<module>dispatch-performnce</module>
Expand Down

0 comments on commit 86ffe70

Please sign in to comment.