Skip to content

Commit

Permalink
add Sequential and Parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
GramGra07 committed Oct 12, 2024
1 parent e02fb9b commit d2e5f48
Show file tree
Hide file tree
Showing 46 changed files with 948 additions and 26 deletions.
Binary file modified .gradle/7.5/checksums/checksums.lock
Binary file not shown.
Binary file modified .gradle/7.5/checksums/md5-checksums.bin
Binary file not shown.
Binary file modified .gradle/7.5/checksums/sha1-checksums.bin
Binary file not shown.
Binary file modified .gradle/7.5/executionHistory/executionHistory.bin
Binary file not shown.
Binary file modified .gradle/7.5/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified .gradle/7.5/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/7.5/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/7.5/fileHashes/resourceHashesCache.bin
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/outputFiles.bin
Binary file not shown.
Binary file modified .gradle/file-system.probe
Binary file not shown.
14 changes: 14 additions & 0 deletions RunAll.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="RunAll" type="JUnit" factoryName="JUnit">
<module name="statemachineftc.unitTest" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" />
<option name="ALTERNATIVE_JRE_PATH" value="Android API 34, extension level 7 Platform" />
<option name="PACKAGE_NAME" value="tests" />
<option name="MAIN_CLASS_NAME" value="" />
<option name="METHOD_NAME" value="" />
<option name="TEST_OBJECT" value="package" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
</component>
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ repositories {
}

dependencies {
implementation 'org.junit.jupiter:junit-jupiter:5.8.1'
implementation 'org.firstinspires.ftc:RobotCore:9.0.1'
implementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
implementation 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
implementation 'org.junit.platform:junit-platform-launcher:1.8.1'
implementation 'org.firstinspires.ftc:RobotCore:10.1.0'
}

afterEvaluate {
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#Tue Apr 02 21:32:58 MDT 2024
#Fri Oct 11 22:12:12 MDT 2024
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<merger version="3"><dataSet aapt-namespace="http://schemas.android.com/apk/res-auto" config="main$Generated" generated="true" ignore_pattern="!.svn:!.git:!.ds_store:!*.scc:.*:&lt;dir>_*:!CVS:!thumbs.db:!picasa.ini:!*~"><source path="/Users/gradengentry/Desktop/Repos/StateMachineFTC/src/main/res"/><source path="/Users/gradengentry/Desktop/Repos/StateMachineFTC/build/generated/res/rs/debug"/><source path="/Users/gradengentry/Desktop/Repos/StateMachineFTC/build/generated/res/resValues/debug"/></dataSet><dataSet aapt-namespace="http://schemas.android.com/apk/res-auto" config="main" generated-set="main$Generated" ignore_pattern="!.svn:!.git:!.ds_store:!*.scc:.*:&lt;dir>_*:!CVS:!thumbs.db:!picasa.ini:!*~"><source path="/Users/gradengentry/Desktop/Repos/StateMachineFTC/src/main/res"/><source path="/Users/gradengentry/Desktop/Repos/StateMachineFTC/build/generated/res/rs/debug"/><source path="/Users/gradengentry/Desktop/Repos/StateMachineFTC/build/generated/res/resValues/debug"/></dataSet><dataSet aapt-namespace="http://schemas.android.com/apk/res-auto" config="debug$Generated" generated="true" ignore_pattern="!.svn:!.git:!.ds_store:!*.scc:.*:&lt;dir>_*:!CVS:!thumbs.db:!picasa.ini:!*~"><source path="/Users/gradengentry/Desktop/Repos/StateMachineFTC/src/debug/res"/></dataSet><dataSet aapt-namespace="http://schemas.android.com/apk/res-auto" config="debug" generated-set="debug$Generated" ignore_pattern="!.svn:!.git:!.ds_store:!*.scc:.*:&lt;dir>_*:!CVS:!thumbs.db:!picasa.ini:!*~"><source path="/Users/gradengentry/Desktop/Repos/StateMachineFTC/src/debug/res"/></dataSet><mergedItems/></merger>
<merger version="3"><dataSet aapt-namespace="http://schemas.android.com/apk/res-auto" config="main$Generated" generated="true" ignore_pattern="!.svn:!.git:!.ds_store:!*.scc:.*:&lt;dir>_*:!CVS:!thumbs.db:!picasa.ini:!*~"><source path="/Users/gradengentry/Downloads/Repos/StateMachineFTC/src/main/res"/><source path="/Users/gradengentry/Downloads/Repos/StateMachineFTC/build/generated/res/rs/debug"/><source path="/Users/gradengentry/Downloads/Repos/StateMachineFTC/build/generated/res/resValues/debug"/></dataSet><dataSet aapt-namespace="http://schemas.android.com/apk/res-auto" config="main" generated-set="main$Generated" ignore_pattern="!.svn:!.git:!.ds_store:!*.scc:.*:&lt;dir>_*:!CVS:!thumbs.db:!picasa.ini:!*~"><source path="/Users/gradengentry/Downloads/Repos/StateMachineFTC/src/main/res"/><source path="/Users/gradengentry/Downloads/Repos/StateMachineFTC/build/generated/res/rs/debug"/><source path="/Users/gradengentry/Downloads/Repos/StateMachineFTC/build/generated/res/resValues/debug"/></dataSet><dataSet aapt-namespace="http://schemas.android.com/apk/res-auto" config="debug$Generated" generated="true" ignore_pattern="!.svn:!.git:!.ds_store:!*.scc:.*:&lt;dir>_*:!CVS:!thumbs.db:!picasa.ini:!*~"><source path="/Users/gradengentry/Downloads/Repos/StateMachineFTC/src/debug/res"/></dataSet><dataSet aapt-namespace="http://schemas.android.com/apk/res-auto" config="debug" generated-set="debug$Generated" ignore_pattern="!.svn:!.git:!.ds_store:!*.scc:.*:&lt;dir>_*:!CVS:!thumbs.db:!picasa.ini:!*~"><source path="/Users/gradengentry/Downloads/Repos/StateMachineFTC/src/debug/res"/></dataSet><mergedItems/></merger>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
4
5 <uses-sdk
6 android:minSdkVersion="24"
6-->/Users/gradengentry/Desktop/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest11121576163084389438.xml
6-->/Users/gradengentry/Downloads/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest7491562204287475936.xml
7 android:targetSdkVersion="28" />
7-->/Users/gradengentry/Desktop/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest11121576163084389438.xml
7-->/Users/gradengentry/Downloads/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest7491562204287475936.xml
8
9</manifest>
36 changes: 18 additions & 18 deletions build/outputs/logs/manifest-merger-debug-report.txt
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
-- Merging decision tree log ---
manifest
ADDED from /Users/gradengentry/Desktop/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest11121576163084389438.xml:2:13-3:63
INJECTED from /Users/gradengentry/Desktop/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest11121576163084389438.xml:2:13-3:63
INJECTED from /Users/gradengentry/Desktop/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest11121576163084389438.xml:2:13-3:63
ADDED from /Users/gradengentry/Downloads/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest7491562204287475936.xml:2:13-3:63
INJECTED from /Users/gradengentry/Downloads/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest7491562204287475936.xml:2:13-3:63
INJECTED from /Users/gradengentry/Downloads/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest7491562204287475936.xml:2:13-3:63
package
ADDED from /Users/gradengentry/Desktop/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest11121576163084389438.xml:3:16-60
INJECTED from /Users/gradengentry/Desktop/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest11121576163084389438.xml
INJECTED from /Users/gradengentry/Desktop/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest11121576163084389438.xml
ADDED from /Users/gradengentry/Downloads/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest7491562204287475936.xml:3:16-60
INJECTED from /Users/gradengentry/Downloads/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest7491562204287475936.xml
INJECTED from /Users/gradengentry/Downloads/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest7491562204287475936.xml
xmlns:android
ADDED from /Users/gradengentry/Desktop/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest11121576163084389438.xml:2:23-81
ADDED from /Users/gradengentry/Downloads/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest7491562204287475936.xml:2:23-81
uses-sdk
INJECTED from /Users/gradengentry/Desktop/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest11121576163084389438.xml reason: use-sdk injection requested
INJECTED from /Users/gradengentry/Desktop/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest11121576163084389438.xml
INJECTED from /Users/gradengentry/Desktop/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest11121576163084389438.xml
INJECTED from /Users/gradengentry/Desktop/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest11121576163084389438.xml
INJECTED from /Users/gradengentry/Desktop/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest11121576163084389438.xml
INJECTED from /Users/gradengentry/Downloads/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest7491562204287475936.xml reason: use-sdk injection requested
INJECTED from /Users/gradengentry/Downloads/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest7491562204287475936.xml
INJECTED from /Users/gradengentry/Downloads/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest7491562204287475936.xml
INJECTED from /Users/gradengentry/Downloads/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest7491562204287475936.xml
INJECTED from /Users/gradengentry/Downloads/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest7491562204287475936.xml
android:targetSdkVersion
INJECTED from /Users/gradengentry/Desktop/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest11121576163084389438.xml
ADDED from /Users/gradengentry/Desktop/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest11121576163084389438.xml
INJECTED from /Users/gradengentry/Desktop/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest11121576163084389438.xml
INJECTED from /Users/gradengentry/Downloads/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest7491562204287475936.xml
ADDED from /Users/gradengentry/Downloads/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest7491562204287475936.xml
INJECTED from /Users/gradengentry/Downloads/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest7491562204287475936.xml
android:minSdkVersion
INJECTED from /Users/gradengentry/Desktop/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest11121576163084389438.xml
ADDED from /Users/gradengentry/Desktop/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest11121576163084389438.xml
INJECTED from /Users/gradengentry/Desktop/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest11121576163084389438.xml
INJECTED from /Users/gradengentry/Downloads/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest7491562204287475936.xml
ADDED from /Users/gradengentry/Downloads/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest7491562204287475936.xml
INJECTED from /Users/gradengentry/Downloads/Repos/StateMachineFTC/build/intermediates/tmp/ProcessLibraryManifest/debug/tempAndroidManifest7491562204287475936.xml
Binary file modified build/tmp/compileDebugJavaWithJavac/previous-compilation-data.bin
Binary file not shown.
153 changes: 153 additions & 0 deletions src/main/java/org/gentrifiedApps/statemachineftc/ParallelRunSM.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package org.gentrifiedApps.statemachineftc;

import android.util.Pair;

import org.junit.jupiter.api.DisplayNameGenerator;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

public class ParallelRunSM<T extends Enum<T>> {
private List<T> states;
private Map<T, StateChangeCallback> onEnterCommands;
private Supplier<Boolean> exitTransition;
private boolean isStarted = false;
private boolean isRunning = true;
private long startTime;

private AbstractMap.SimpleEntry<Boolean, Integer> timeout;

public boolean isRunning() {
return isRunning;
}

public boolean isStarted() {
return isStarted;
}

ParallelRunSM(ParallelRunSM.Builder<T> builder) {
this.states = builder.states;
this.exitTransition = builder.exitTransition;
this.onEnterCommands = builder.onEnterCommands;
this.timeout = builder.timeout;
}

public static class Builder<T extends Enum<T>> {
private List<T> states;
private Map<T, StateChangeCallback> onEnterCommands;
private Supplier<Boolean> exitTransition;
private ParallelRunSM<T> machine;
private int stopRunningIncluded = 0;
private AbstractMap.SimpleEntry<Boolean, Integer> timeout;

public Builder() {
states = new ArrayList<>();
onEnterCommands = new HashMap<>();
}

public ParallelRunSM.Builder<T> state(T state) {
if (states.contains(state)) {
throw new IllegalArgumentException("State already exists");
}
states.add(state);
return this;
}

public ParallelRunSM.Builder<T> onEnter(T state, StateChangeCallback command) {
if (!states.contains(state)) {
throw new IllegalArgumentException("State does not exist");
}
onEnterCommands.put(state, command);
return this;
}

public ParallelRunSM.Builder<T> stopRunning(T state, Supplier<Boolean> exitCommand) {
this.stopRunningIncluded++;
if (states.contains(state)) {
throw new IllegalArgumentException("State already exists");
}
states.add(state);
onEnterCommands.put(state, () -> {
if (exitCommand.get()) {
this.machine.isRunning = false;
}
});
this.exitTransition = exitCommand;
return this;
}

public ParallelRunSM<T> build(Boolean useTimeout, Integer timeout) {
this.timeout = new AbstractMap.SimpleEntry<>(useTimeout, timeout);
if (states == null || states.isEmpty()) {
throw new IllegalArgumentException("States cannot be null or empty");
}

if (new HashSet<>(states).size() != states.size()) {
throw new IllegalArgumentException("States cannot have duplicates");
}

if (onEnterCommands.get(states.get(0)) == null) {
throw new IllegalArgumentException("Initial state must have a corresponding onEnter command");
}
if (this.stopRunningIncluded != 1) {
throw new IllegalArgumentException("Not enough or too many stopRunning commands");
}
if (this.exitTransition == null) {
throw new IllegalArgumentException("Exit transition must be set");
}
this.machine = new ParallelRunSM<T>(this);
return this.machine;
}
}

public void start() {
if (isStarted) {
throw new IllegalStateException("StateMachine has already been started");
}
isStarted = true;
startTime = System.currentTimeMillis();
}

public void stop() {
if (!isRunning) {
throw new IllegalStateException("StateMachine is already stopped");
}
isRunning = false;
//delete all actions
states.clear();
onEnterCommands.clear();
}

public boolean update() {
if (!states.isEmpty()) {
// run all states at once
for (T state : states) {
StateChangeCallback onEnterAction = onEnterCommands.get(state);
if (onEnterAction != null) {
onEnterAction.onStateChange();
}
}
}
if (checkExitTransition()) {
isRunning = false;
}
return true;
}

public boolean checkExitTransition() {
boolean exitResult = exitTransition.get();
long elapsedTime = System.currentTimeMillis() - startTime;
System.out.println("Checking exit transition: " + exitResult);
System.out.println("Elapsed time: " + elapsedTime + "ms");
final boolean condition = exitResult || (timeout.getKey() && elapsedTime > timeout.getValue());
if (condition) {
isRunning = false;
}
return condition;
}
}
Loading

0 comments on commit d2e5f48

Please sign in to comment.