Created
April 24, 2022 20:18
-
-
Save AnarCom/8b96c57d81e43c09bb57a28158a088a9 to your computer and use it in GitHub Desktop.
Retejs + Jackson simple interpretation test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.annotation.JsonSubTypes; | |
import com.fasterxml.jackson.annotation.JsonTypeInfo; | |
import com.fasterxml.jackson.annotation.JsonTypeInfo.As; | |
import com.fasterxml.jackson.annotation.JsonTypeName; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import java.io.BufferedReader; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.Map; | |
import lombok.Data; | |
import lombok.Getter; | |
import lombok.Setter; | |
public class AnotherTestMain { | |
public static void main(String[] args) throws Exception { | |
// var t = new Dog(); | |
// t.barkVolume = 10; | |
// | |
// var json = new ObjectMapper().writeValueAsString(t); | |
// System.out.println(json); | |
// | |
// Animal an = new ObjectMapper().readerFor(Animal.class) | |
// .readValue(json); | |
// System.out.println(an); | |
var reader = new BufferedReader(new FileReader("./test.json")); | |
StringBuilder b = new StringBuilder(); | |
String line; | |
while ((line = reader.readLine()) != null) { | |
b.append(line); | |
} | |
String json = b.toString(); | |
NodeGroup a = new ObjectMapper().readValue(json, NodeGroup.class); | |
var outputNodeKey = a.nodes.keySet().stream() | |
.filter((i) -> a.nodes.get(i) instanceof OutputNode) | |
.findFirst().orElseThrow(RuntimeException::new); | |
// System.out.println(outputNodeKey); | |
var score = ((OutputNode) a.nodes.get(outputNodeKey)).calculateScore(a.getNodes()); | |
System.out.println(score); | |
} | |
@Data | |
public static class NodeGroup { | |
private String id; | |
Map<String, AbstractNode> nodes; | |
} | |
@JsonTypeInfo( | |
use = JsonTypeInfo.Id.NAME, | |
include = As.PROPERTY, | |
property = "name") | |
@JsonSubTypes({ | |
@JsonSubTypes.Type(value = AddNode.class, name = "Add"), | |
@JsonSubTypes.Type(value = OutputNode.class, name = "Output"), | |
@JsonSubTypes.Type(value = NumberNode.class, name = "Number"), | |
// @JsonSubTypes.Type(value = Cat.class, name = "cat") | |
}) | |
@Getter | |
@Setter | |
public static abstract class AbstractNode { | |
Integer id; | |
// data | |
private Integer[] position; | |
private Map<String, InputConnection> inputs; | |
private Map<String, OutputConnection> outputs; | |
public abstract Double calculate(Map<String, AbstractNode> nodeMap); | |
} | |
@Data | |
static class InputConnection { | |
private InputConnectionData[] connections; | |
} | |
@Data | |
static class InputConnectionData { | |
private Integer node; | |
private String output; | |
private Map<String, Object> data; | |
} | |
@Data | |
static class OutputConnection { | |
private OutputConnectionData[] connections; | |
} | |
@Data | |
static class OutputConnectionData { | |
private Integer node; | |
private String input; | |
private Map<String, Object> data; | |
} | |
@JsonTypeName("Add") | |
@Getter | |
@Setter | |
static class AddNode extends AbstractNode { | |
private AddNodeData data; | |
@Override | |
public Double calculate(Map<String, AbstractNode> nodeMap) { | |
Double sum; | |
if(getInputs().get("num1").connections.length == 0){ | |
sum = data.num1; | |
} else { | |
sum = nodeMap.get( | |
getInputs().get("num1").connections[0].node.toString() | |
).calculate(nodeMap); | |
} | |
if(sum == null){ | |
sum = 10D; | |
} | |
if(getInputs().get("num2").connections.length == 0){ | |
sum += data.num2; | |
} else { | |
sum += nodeMap.get( | |
getInputs().get("num2").connections[0].node.toString() | |
).calculate(nodeMap); | |
} | |
return sum; | |
// return null; | |
} | |
static class AddNodeData { | |
public Double num1; | |
public Double num2; | |
public String preview; | |
} | |
} | |
@JsonTypeName("Number") | |
@Getter | |
@Setter | |
static class NumberNode extends AbstractNode { | |
private NumberData data; | |
@Override | |
public Double calculate(Map<String, AbstractNode> nodeMap) { | |
return data.num; | |
} | |
@Getter | |
@Setter | |
static class NumberData { | |
private Double num; | |
} | |
} | |
@JsonTypeName("Output") | |
@Getter | |
@Setter | |
static class OutputNode extends AbstractNode { | |
private OutputData data; | |
@Override | |
public Double calculate(Map<String, AbstractNode> nodeMap) { | |
return null; | |
} | |
public Double calculateTime(Map<String, AbstractNode> nodeMap) { | |
return null; | |
} | |
public Double calculateScore(Map<String, AbstractNode> nodeMap) { | |
if (getInputs().get("res").connections.length == 0) { | |
return 0D; | |
} | |
return nodeMap.get( | |
getInputs().get("res").connections[0].node.toString() | |
).calculate(nodeMap); | |
} | |
@Getter | |
@Setter | |
static class OutputData { | |
private Double previewRes; | |
private Double previewTime; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment