From faa3c4f33e4070c5cb7fd32943b06f19a062708e Mon Sep 17 00:00:00 2001 From: Ou-An <75078332+AoMedium@users.noreply.github.com> Date: Wed, 23 Nov 2022 18:48:30 +1300 Subject: [PATCH] not working: test plugin pattern with yml --- src/FileReader.java | 19 ++++++ src/functions/FunctionCollector.java | 31 ++++++++++ src/functions/Growth.java | 11 ++++ src/functions/IFunction.java | 6 ++ src/functions/LightInterception.java | 11 ++++ src/graph.xml | 46 ++------------ src/main.rgg | 91 +++++++++++++--------------- src/models/Fruit.rgg | 5 ++ src/models/GenericOrgan.java | 15 +++++ src/models/Leaf.rgg | 4 ++ src/models/config/BooleanField.java | 11 ++++ src/models/config/ConfigFile.java | 24 ++++++++ src/models/config/Field.java | 13 ++++ src/modules/light.rgg | 14 ----- src/modules/organs.rgg | 40 ------------ src/organConfig.yml | 10 +++ src/project.gs | 31 +++++----- src/simulationModels/simpleModel.yml | 4 ++ 18 files changed, 225 insertions(+), 161 deletions(-) create mode 100644 src/FileReader.java create mode 100644 src/functions/FunctionCollector.java create mode 100644 src/functions/Growth.java create mode 100644 src/functions/IFunction.java create mode 100644 src/functions/LightInterception.java create mode 100644 src/models/Fruit.rgg create mode 100644 src/models/GenericOrgan.java create mode 100644 src/models/Leaf.rgg create mode 100644 src/models/config/BooleanField.java create mode 100644 src/models/config/ConfigFile.java create mode 100644 src/models/config/Field.java delete mode 100644 src/modules/light.rgg delete mode 100644 src/modules/organs.rgg create mode 100644 src/organConfig.yml create mode 100644 src/simulationModels/simpleModel.yml diff --git a/src/FileReader.java b/src/FileReader.java new file mode 100644 index 0000000..2ea7bb9 --- /dev/null +++ b/src/FileReader.java @@ -0,0 +1,19 @@ +import java.io.File; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; + +import models.config.ConfigFile; + +public class FileReader { + // public FileReader() { + + // } + + // public ConfigFile readConfig(String path) { + // File file = new File("src/simulationModels/test.json"); + + // ObjectMapper mapper = new ObjectMapper(); + // ConfigFile config = mapper.readValue(file, ConfigFile.class); + // } +} \ No newline at end of file diff --git a/src/functions/FunctionCollector.java b/src/functions/FunctionCollector.java new file mode 100644 index 0000000..cb0dd26 --- /dev/null +++ b/src/functions/FunctionCollector.java @@ -0,0 +1,31 @@ +package functions; + +import java.util.ArrayList; +import java.util.List; + +public class FunctionCollector { + // XCompiler unable to compile <> with type , so left generic + private List functions; + + public FunctionCollector() { + init(); + } + + public void init() { + functions = new ArrayList(); + } + + public void run() { + for (IFunction function : functions) { + function.run(); + } + } + + public void add(IFunction function) { + functions.add(function); + } + + public List getFunctions() { + return functions; + } +} \ No newline at end of file diff --git a/src/functions/Growth.java b/src/functions/Growth.java new file mode 100644 index 0000000..11d51f0 --- /dev/null +++ b/src/functions/Growth.java @@ -0,0 +1,11 @@ +package functions; + +public class Growth implements IFunction { + public void init() { + println("init Growth"); + } + + public void run() { + println("run Growth"); + } +} diff --git a/src/functions/IFunction.java b/src/functions/IFunction.java new file mode 100644 index 0000000..00b1af7 --- /dev/null +++ b/src/functions/IFunction.java @@ -0,0 +1,6 @@ +package functions; + +public interface IFunction { + void init(); + void run(); +} diff --git a/src/functions/LightInterception.java b/src/functions/LightInterception.java new file mode 100644 index 0000000..366cbc1 --- /dev/null +++ b/src/functions/LightInterception.java @@ -0,0 +1,11 @@ +package functions; + +public class LightInterception implements IFunction { + public void init() { + println("init LightInterception"); + } + + public void run() { + println("run LightInterception"); + } +} diff --git a/src/graph.xml b/src/graph.xml index 6ad39b8..dc82c92 100644 --- a/src/graph.xml +++ b/src/graph.xml @@ -1,55 +1,17 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - + - - - - - - - - - - - - - - + diff --git a/src/main.rgg b/src/main.rgg index 527f97f..9c86d9a 100644 --- a/src/main.rgg +++ b/src/main.rgg @@ -1,52 +1,43 @@ -import static light.*; -import static organs.*; - -const float GOLDEN_ANGLE = 137.5; -const float BRANCH_ANGLE = 50; -const float LEAF_ANGLE = 70; -const int PHYLLOCHRON = 1; - -protected void init() -[ -Axiom ==> Bud(1, PHYLLOCHRON, 1); -// light source is placed -// above the scene -==>> ^ M(50) RU(180) MyLight; -] -public void run() -[ - Bud(r, p, o), (p > 0) ==> Bud(r, p-1, o); - b:Bud(r, p, o), (r < 10 && p == 0 && o < 4) ==> if (forall(distance(b, (* Internode *)) > 0.6)) ( - RV(-0.1) Internode Node - [ RL(BRANCH_ANGLE) Bud(r, PHYLLOCHRON, o+1) ] - [ RL(LEAF_ANGLE) Leaf(0f) ] - RH(GOLDEN_ANGLE) RV(-0.1) Internode Bud(r+1, PHYLLOCHRON, o) - ); - - Bud(r, p, o), (r == 10) ==> RV(-0.1) Internode RV(-0.1) Internode Flower; -] - -// light model instance -// 100000: number of random rays -// 5: recursion depth (nb. of reflections) -LightModel lm = new LightModel(100000, 5); - -public void grow() { -// apply growth rules -run(); -// compute light -lm.compute(); -// calculate the amount of light -// (integrated over the whole spectrum), -// absorbed by a leaf -absorb(); -} +import java.util.List; +import java.util.ArrayList; +import java.io.File; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; + +import models.*; +import functions.*; + +static List organs = new ArrayList(); + +protected void init() { + + Leaf leaf = new Leaf(); + File organConfig = new File("/var/model/src/organConfig.yml"); + + ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); + JsonNode organTree = mapper.readTree(organConfig); + + for (JsonNode organNode : organTree) { + // Create organ instances + String organName = organNode.get("name"); + Class organClass = Class.forName(organName); + GenericOrgan organ = (GenericOrgan) organClass.getDeclaredConstructor().newInstance(); + + JsonNode functions = organNode.get("functions"); + + for (JsonNode functionName : functions) { + String className = functionName.asText(); + Class functionClass = Class.forName(className); + IFunction function = (IFunction) functionClass.getDeclaredConstructor().newInstance(); -protected void absorb() [ -lf:Leaf ::> { -// 2.25 - conversion factor W -> PAR -// (photosynthetically active radiation [mikro mol/m^2/s]) -lf[al] = lm.getAbsorbedPower3d(lf).integrate() * 2.25; -//println(lf[al]); + organ.getCollector().add(function); + } + } } -] \ No newline at end of file +public void run() { + for (GenericOrgan organ : organs) { + organ.run(); + } +} \ No newline at end of file diff --git a/src/models/Fruit.rgg b/src/models/Fruit.rgg new file mode 100644 index 0000000..bcf2ca1 --- /dev/null +++ b/src/models/Fruit.rgg @@ -0,0 +1,5 @@ +import models.GenericOrgan; + +public class Fruit extends GenericOrgan { + +} diff --git a/src/models/GenericOrgan.java b/src/models/GenericOrgan.java new file mode 100644 index 0000000..7dd0b37 --- /dev/null +++ b/src/models/GenericOrgan.java @@ -0,0 +1,15 @@ +package models; + +import functions.FunctionCollector; + +public abstract class GenericOrgan { + private FunctionCollector collector; + + public FunctionCollector getCollector() { + return collector; + } + + public void run() { + collector.run(); + } +} \ No newline at end of file diff --git a/src/models/Leaf.rgg b/src/models/Leaf.rgg new file mode 100644 index 0000000..65dd740 --- /dev/null +++ b/src/models/Leaf.rgg @@ -0,0 +1,4 @@ +import models.GenericOrgan; + +public class Leaf extends GenericOrgan { +} diff --git a/src/models/config/BooleanField.java b/src/models/config/BooleanField.java new file mode 100644 index 0000000..f4cc356 --- /dev/null +++ b/src/models/config/BooleanField.java @@ -0,0 +1,11 @@ +package models.config; + +public class BooleanField extends Field { + + private boolean value; + + public BooleanField(String name, boolean value) { + super(name); + this.value = value; + } +} diff --git a/src/models/config/ConfigFile.java b/src/models/config/ConfigFile.java new file mode 100644 index 0000000..3f4a6bf --- /dev/null +++ b/src/models/config/ConfigFile.java @@ -0,0 +1,24 @@ +package models.config; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +import com.fasterxml.jackson.annotation.*; + +public class ConfigFile { + private Map fields; + + public ConfigFile() { + fields = new HashMap(); + } + + @JsonAnySetter + public void addField(String name, String value) { + fields.put(name, value); + } + + public Map getFields() { + return fields; + } +} \ No newline at end of file diff --git a/src/models/config/Field.java b/src/models/config/Field.java new file mode 100644 index 0000000..d4d1a27 --- /dev/null +++ b/src/models/config/Field.java @@ -0,0 +1,13 @@ +package models.config; + +abstract class Field { + private String name; + + public Field(String name) { + this.name = name; + } + + public String getName() { + return name; + } +} \ No newline at end of file diff --git a/src/modules/light.rgg b/src/modules/light.rgg deleted file mode 100644 index 68fd408..0000000 --- a/src/modules/light.rgg +++ /dev/null @@ -1,14 +0,0 @@ -// the light source -module MyLamp extends SpotLight { -{ -setPower(200.0); // power in W -setAttenuationDistance(50.0); // in m -setAttenuationExponent(0.0); -setInnerAngle(22.5 * Math.PI/180.0); -setOuterAngle(30.0 * Math.PI/180.0); -} -} -// light specification with colour (R, G, B) -module MyLight extends LightNode(1.0, 1.0, 1.0) { -{ setLight(new MyLamp()); } -} \ No newline at end of file diff --git a/src/modules/organs.rgg b/src/modules/organs.rgg deleted file mode 100644 index 37a376a..0000000 --- a/src/modules/organs.rgg +++ /dev/null @@ -1,40 +0,0 @@ -const ShaderRef leafmat = new ShaderRef("Leafmat"); -const ShaderRef petalmat = new ShaderRef("Petalmat"); -const ShaderRef internodemat = new ShaderRef("Internodemat"); -const ShaderRef nodemat = new ShaderRef("Nodemat"); - -// buds are red spheres -module Bud(int rank, int phyllo, int order) extends Sphere(0.1) { -{ setShader(nodemat); } -} -module Node extends Sphere(0.07) { -{ setShader(nodemat); } -} -module Internode extends Cylinder(1, 0.05) { -{ setShader(internodemat); } -} -module Leaf(float al) extends Parallelogram(2, 1) { -{ setShader(leafmat); } -} -module Flower ==> -RU(180) Cone(0.3, 0.3).(setShader(internodemat)) -M(-0.25) RL(90) -[ -for (int i = 1; i <= 5;i++) ( -[ RU(i*360/5) RL(20) Parallelogram(2, 1).(setShader(petalmat)) ] -) -] -RU(45) -[ -for (int i = 1; i <= 5; i++) ( -[ RU(i*360/5) RL(40) F(0.3, 0.1, 14) -RV(-0.3) F(0.3, 0.1, 14) RV(-0.3) F(0.3, 0.1, 14) ] -) -] -RU(-45) -[ -for (int i = 1; i <= 5; i++) ( -[ RU(i*360/5) RL(70) Frustum(0.7, 0.2, 0.05).(setColor(0x8DAF58)) ] -) -] -; \ No newline at end of file diff --git a/src/organConfig.yml b/src/organConfig.yml new file mode 100644 index 0000000..0d6810b --- /dev/null +++ b/src/organConfig.yml @@ -0,0 +1,10 @@ +leaf: + name: models.Leaf + functions: + - functions.Growth + - functions.LightInterception + +fruit: + name: models.Fruit + functions: + - functions.Growth \ No newline at end of file diff --git a/src/project.gs b/src/project.gs index 31160c2..f44ebb2 100644 --- a/src/project.gs +++ b/src/project.gs @@ -1,22 +1,23 @@ - - - - + + + - - + + + + + - - - + + @@ -24,8 +25,8 @@ - - + + @@ -42,13 +43,13 @@ - + - + - - + + diff --git a/src/simulationModels/simpleModel.yml b/src/simulationModels/simpleModel.yml new file mode 100644 index 0000000..4c8bfa4 --- /dev/null +++ b/src/simulationModels/simpleModel.yml @@ -0,0 +1,4 @@ +name: Simple Model +configs: + - environmentConfig.yml + - organConfig.yml \ No newline at end of file