Skip to content

Commit

Permalink
Version 1.1
Browse files Browse the repository at this point in the history
Added Output Handling for process blocks
  • Loading branch information
pintojoey committed May 24, 2018
1 parent 35d1ada commit 3b7ddd8
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>cz.zcu.kiv</groupId>
<artifactId>workflow_designer</artifactId>
<version>1.0</version>
<version>1.1</version>
<build>

<plugins>
Expand All @@ -23,7 +23,7 @@
<configuration>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
<mainClass>cz.zcu.kiv.WorkflowDesigner</mainClass>
</manifest>
</archive>
<descriptorRefs>
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/cz/zcu/kiv/WorkflowDesigner/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ public JSONObject toJSON(){
return blockjs;
}

public void processBlock(Map<Integer, Block> blocks, Map<String, Integer> sourceBlocks, Map<String, String> sourceParams) throws IllegalAccessException, FieldMismatchException {
public String processBlock(Map<Integer, Block> blocks, Map<String, Integer> sourceBlocks, Map<String, String> sourceParams) throws IllegalAccessException, FieldMismatchException {
String output="";
if(getInput()!=null&&getInput().size()>0) {
for (String key : getInput().keySet()) {
Data destinationData=getInput().get(key);
Expand Down Expand Up @@ -214,21 +215,24 @@ public void processBlock(Map<Integer, Block> blocks, Map<String, Integer> source

}
}
process();
output = process();
setProcessed(true);
return output;
}

public void process(){
public String process(){
String output = "";
for(Method method:context.getClass().getDeclaredMethods()){
method.setAccessible(true);
if(method.getAnnotation(BlockExecute.class)!=null){
try {
method.invoke(context);
output = (String) method.invoke(context);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
return output;
}

public Map<String, Property> getProperties() {
Expand Down
37 changes: 34 additions & 3 deletions src/main/java/cz/zcu/kiv/WorkflowDesigner/Workflow.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package cz.zcu.kiv.WorkflowDesigner;

import cz.zcu.kiv.WorkflowDesigner.Annotations.BlockType;
import org.apache.commons.io.FileUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.reflections.Reflections;

import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.Charset;
import java.util.*;

/***********************************************************************************************************************
Expand Down Expand Up @@ -130,7 +133,11 @@ public Map<Integer, Block> indexBlocks(JSONArray blocksArray) throws IllegalAcce
Block block = null;

//get Block object by type of block in JSON
Set<Class<?>> blockTypes = new Reflections(this.packageName).getTypesAnnotatedWith(BlockType.class);
Set<Class<?>> blockTypes;
if (this.classLoader == null)
blockTypes = new Reflections(this.packageName).getTypesAnnotatedWith(BlockType.class);
else
blockTypes = new Reflections(this.packageName, this.classLoader).getTypesAnnotatedWith(BlockType.class);
for(Class blockType:blockTypes){
Annotation annotation = blockType.getAnnotation(BlockType.class);
Class<? extends Annotation> type = annotation.annotationType();
Expand Down Expand Up @@ -193,7 +200,7 @@ public ArrayList<Integer> populateWaitList(JSONArray edgesArray, Map<Integer,Blo
* @param jObject
* @throws Exception
*/
public void execute(JSONObject jObject) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, FieldMismatchException {
public JSONArray execute(JSONObject jObject) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, FieldMismatchException {
JSONArray blocksArray = jObject.getJSONArray("blocks");

//Accumulate and index all blocks defined in the workflow
Expand Down Expand Up @@ -242,12 +249,20 @@ public void execute(JSONObject jObject) throws InvocationTargetException, NoSuch

if (ready) {
//Process the ready block
waitBlock.processBlock(dependencies, sourceBlock, sourceParam);
String output = waitBlock.processBlock(dependencies, sourceBlock, sourceParam);
for(int i=0;i<blocksArray.length();i++){
JSONObject block=blocksArray.getJSONObject(i);
if(block.getInt("id")==waitBlockId){
block.put("output",output);
break;
}
}
break;
}

}
}
return blocksArray;
}

/**
Expand All @@ -274,5 +289,21 @@ private void populateDependencies(int block1Id, Block block1, JSONObject edgeObj
dependencies.put(block1Id, block1);
}

/**
*
* @param args 1)package name 2)Workflow JSON 3)(Optional) Output file location (defaults to output.txt)
* @throws FieldMismatchException InputField-OutputField Mismatch
* @throws NoSuchMethodException Reflection Problems
* @throws InstantiationException
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws IOException When cannot create file
*/
public static void main(String[] args) throws FieldMismatchException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException, IOException {
JSONArray jsonArray=new Workflow(args[0]).execute(new JSONObject(args[1]));
String outputFile ="output.txt";
if(args.length>2) outputFile=args[2];
FileUtils.writeStringToFile(new File(outputFile),jsonArray.toString(4),Charset.defaultCharset());
}

}
3 changes: 2 additions & 1 deletion src/test/java/test/ArithmeticBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ArithmeticBlock {
private String operation;

@BlockExecute
public void process(){
public String process(){
switch (operation){
case "add":
op3=op1+op2;
Expand All @@ -31,6 +31,7 @@ public void process(){
op3=op1-op2;
break;
}
return String.valueOf(op3);
}

public static int getOp3() {
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/test/WorkflowDesignerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public void testJSON() throws IOException, NoSuchMethodException, InstantiationE

String json = FileUtils.readFileToString(new File("src/main/webapp/test.json"),Charset.defaultCharset());
JSONObject jsonObject = new JSONObject(json);
new Workflow("").execute(jsonObject);
JSONArray jsonArray = new Workflow("").execute(jsonObject);
assert jsonArray !=null;
assert jsonArray.length() == 3;
assert ArithmeticBlock.getOp3()==15;
}

Expand Down

0 comments on commit 3b7ddd8

Please sign in to comment.