Technologies used in this article :

  1. Neuro4j Workflows 3.4.1
  2. Maven 4.0
  3. Neuro4j Studio 3.0 (based on Eclipse Neon)
  4. JDK 1.8

 

1. Generate project structure with Maven

Issue below Maven command to create a standard Java project structure.

mvn archetype:generate -DgroupId=org.neuro4j.workflow.tutorial -DartifactId=WorkflowExample 
    		-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

2. Import project into Neuro4j Studio

3. Add Neuro4j Workflow 3.3.1 dependency

Add the Neuro4j Workflow 3.3.1 dependencies listed below in Maven pom.xml file.

 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>org.neuro4j.workflow.tutorial</groupId>
	<artifactId>WorkflowExample</artifactId>
	<packaging>jar</packaging>
	<version>1.1-SNAPSHOT</version>
	<name>WorkflowExample</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<org.neuro4j.version>3.3.1</org.neuro4j.version>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.neuro4j</groupId>
			<artifactId>neuro4j-workflow-common</artifactId>
			<version>${org.neuro4j.version}</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.8.2</version>
			<scope>test</scope>
		</dependency>		
	</dependencies>
	<build>
		<resources>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.n4j</include>
				</includes>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<includes>
					<include>**/*.png</include>
				</includes>
			</resource>
		</resources>
		<testResources>
			<testResource>
				<directory>src/test/java</directory>
				<includes>
					<include>**/**</include>
				</includes>
			</testResource>
			<testResource>
				<directory>src/test/resources</directory>
				<includes>
					<include>**/**</include>
				</includes>
			</testResource>
		</testResources>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

4. Custom Block

  1. Open Neuro4j Studio
  2. Switch to Neuro4j Flow perspective
  3. select package "org.neuro4j.workflow.tutorial"
  4. Select New Custom Block Wizard
  5. New CustomBlock Wizard
  6. New CustomBlock
  7. Click Next.
  8. New CustomBlock
  9. Click Finish.
  10. Update method "execute".

A simple java CustomBlock.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package org.neuro4j.workflow.tutorial;

import static org.neuro4j.workflow.tutorial.HelloWorld.IN_NAME;
import static org.neuro4j.workflow.tutorial.HelloWorld.OUT_MESSAGE;

import org.neuro4j.workflow.ActionBlock;
import org.neuro4j.workflow.FlowContext;
import org.neuro4j.workflow.common.FlowExecutionException;
import org.neuro4j.workflow.common.ParameterDefinition;
import org.neuro4j.workflow.common.ParameterDefinitionList;
import static org.neuro4j.workflow.enums.ActionBlockCache.*;
import org.neuro4j.workflow.enums.CachedNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *  HelloWorld block receives name as input parameter and returns message as output parameters.
 *  
 */
@ParameterDefinitionList(input = { @ParameterDefinition(name = IN_NAME, isOptional = true, type = "java.lang.String") }, 
                         output = { @ParameterDefinition(name = OUT_MESSAGE, isOptional = true, type = "java.lang.String") })

// will create  just one instance  of HelloWorld's class in workflow
@CachedNode(type=SINGLETON)
public class HelloWorld implements ActionBlock {
	
	private static final Logger logger = LoggerFactory.getLogger(HelloWorld.class);

	static final String IN_NAME = "name";

	static final String OUT_MESSAGE = "message";

	public int execute(FlowContext ctx) throws FlowExecutionException {

		String name = (String) ctx.get(IN_NAME);

		String message = "Hello World! ";

		if (name != null) {
			message += name;
		}
		
		logger.debug("Message: {}", message);
		
		ctx.put(OUT_MESSAGE, message);

		return NEXT;
	}


}

4. Workflow

Create workflow

  1. select package "org.neuro4j.workflow.tutorial"
  2. Select New Workflow Wizard
  3. New Workflow Wizard
  4. Add Start Node, Custom Node and End Node to flow
  5. Rename Start Node to "Start"
  6. New Workflow Wizard
  7. Select Custom Node. On Properties View select implementation class
  8. Select CustomBlock
  9. Select class "org.neuro4j.workflow.tutorial.HelloWorld"
  10. Save flow.

5. Client java code

Client's java code to run workflow.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package org.neuro4j.workflow.tutorial;

import java.util.HashMap;
import java.util.Map;

import org.neuro4j.workflow.ExecutionResult;
import org.neuro4j.workflow.common.WorkflowEngine;
import org.neuro4j.workflow.common.WorkflowEngine.ConfigBuilder;

/**
 * This is client's class.
 */
public class App 
{

	
    public static void main( String[] args )
    {
    	
    	// create engine with default configuration
    	WorkflowEngine engine = new WorkflowEngine(new ConfigBuilder());
    	
    	// put input parameters
    	Map<String, Object> parameters = new HashMap<String, Object>();
    	parameters.put("name", "Workflow");

    	//execute flow
    	ExecutionResult result =  engine.execute("org.neuro4j.workflow.tutorial.HelloFlow-Start", parameters);
    	
    	if (result.getException() == null)
    	{
    		String message = (String) result.getFlowContext().get("message");
    		System.out.println("Message: " + message);
    		
    	} else {
    		result.print();
    	}

    }
}

6. Run code

Run code using maven or Eclipse.

mvn clean install
mvn compile exec:java -Dexec.mainClass="org.neuro4j.workflow.tutorial.App"

Result:

Message: Hello World! Workflow

 

Download Source Code

Download java project – WorkflowExample.zip

What's next?