Skip to content

Commit

Permalink
not working: test plugin pattern with yml
Browse files Browse the repository at this point in the history
  • Loading branch information
AoMedium committed Nov 23, 2022
1 parent 298c64b commit faa3c4f
Show file tree
Hide file tree
Showing 18 changed files with 225 additions and 161 deletions.
19 changes: 19 additions & 0 deletions src/FileReader.java
Original file line number Diff line number Diff line change
@@ -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);
// }
}
31 changes: 31 additions & 0 deletions src/functions/FunctionCollector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package functions;

import java.util.ArrayList;
import java.util.List;

public class FunctionCollector {
// XCompiler unable to compile <> with type <IFunction>, 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;
}
}
11 changes: 11 additions & 0 deletions src/functions/Growth.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package functions;

public class Growth implements IFunction {
public void init() {
println("init Growth");
}

public void run() {
println("run Growth");
}
}
6 changes: 6 additions & 0 deletions src/functions/IFunction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package functions;

public interface IFunction {
void init();
void run();
}
11 changes: 11 additions & 0 deletions src/functions/LightInterception.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package functions;

public class LightInterception implements IFunction {
public void init() {
println("init LightInterception");
}

public void run() {
println("run LightInterception");
}
}
46 changes: 4 additions & 42 deletions src/graph.xml
Original file line number Diff line number Diff line change
@@ -1,55 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?><graph xmlns:gx="http://grogra.de/xmlpersistence" xmlns="http://grogra.de/xmlpersistence">
<node root="MainGraph" id="0" type="de.grogra.graph.impl.Node">
<data gx:name="extentIndex" gx:value="7"/>
<node id="14737" type="de.grogra.rgg.RGGRoot" edges="+">
<node id="14739" type="organs$Bud" edges="-">
<data gx:name="radius" gx:value="0.1"/>
<data gx:name="rank" gx:value="1"/>
<data gx:name="phyllo" gx:value="1"/>
<data gx:name="order" gx:value="1"/>
</node>
<node id="14740" type="de.grogra.turtle.M" edges="-">
<data gx:name="length" gx:value="50.0"/>
<node id="14741" type="de.grogra.turtle.RU" edges="-">
<data gx:name="angle" gx:value="180.0"/>
<node id="14742" type="light$MyLight" edges="-">
<object gx:name="light" gx:type="de.grogra.imp3d.objects.SpotLight">
<object gx:name="color" gx:value="false 1.0 1.0 1.0"/>
<data gx:name="shadowless" gx:value="false"/>
<data gx:name="visualize" gx:value="false"/>
<data gx:name="numberofrays" gx:value="500"/>
<data gx:name="raylength" gx:value="5.0"/>
<data gx:name="power" gx:value="200.0"/>
<data gx:name="attenuationExponent" gx:value="0.0"/>
<data gx:name="attenuationDistance" gx:value="50.0"/>
<data gx:name="innerAngle" gx:value="0.3926991"/>
<data gx:name="outerAngle" gx:value="0.5235988"/>
</object>
</node>
</node>
</node>
<node id="14968" type="de.grogra.rgg.RGGRoot" edges="+">
<node id="14969" type="de.grogra.rgg.Axiom" edges="-"/>
</node>
</node>
<node root="MetaGraph" id="1" type="de.grogra.graph.impl.Node">
<data gx:name="extentIndex" gx:value="7"/>
<node id="14734" type="main" edges="+">
<node id="14966" type="main" edges="+">
<data gx:name="extentIndex" gx:value="7"/>
<object gx:name="initialTurtleState" color="14" length="100.0" diameter="0.1" tropism="0.0" parameter="0.0" carbon="0.0" heartwood="0.0" internodeCount="0"/>
<object gx:name="lm" gx:type="de.grogra.rgg.LightModel">
<data gx:name="rayCount" gx:value="100000"/>
<data gx:name="depth" gx:value="5"/>
<data gx:name="minPower" gx:value="0.001"/>
<data gx:name="seed" gx:value="0"/>
<data gx:name="threadCount" gx:value="0"/>
<object gx:name="spectrumFactory" gx:type="de.grogra.ray.physics.Spectrum3d" gx:value="0.0 0.0 0.0"/>
<array gx:name="visibleLayers" gx:value="true true true true true true true true true true true true true true true true"/>
</object>
</node>
<node id="14735" type="light" edges="+">
<data gx:name="extentIndex" gx:value="7"/>
<object gx:name="initialTurtleState" color="14" length="100.0" diameter="0.1" tropism="0.0" parameter="0.0" carbon="0.0" heartwood="0.0" internodeCount="0"/>
</node>
<node id="14736" type="organs" edges="+">
<node id="14967" type="Leaf" edges="+">
<data gx:name="extentIndex" gx:value="7"/>
<object gx:name="initialTurtleState" color="14" length="100.0" diameter="0.1" tropism="0.0" parameter="0.0" carbon="0.0" heartwood="0.0" internodeCount="0"/>
</node>
Expand Down
91 changes: 41 additions & 50 deletions src/main.rgg
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
]
public void run() {
for (GenericOrgan organ : organs) {
organ.run();
}
}
5 changes: 5 additions & 0 deletions src/models/Fruit.rgg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import models.GenericOrgan;

public class Fruit extends GenericOrgan {

}
15 changes: 15 additions & 0 deletions src/models/GenericOrgan.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
4 changes: 4 additions & 0 deletions src/models/Leaf.rgg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import models.GenericOrgan;

public class Leaf extends GenericOrgan {
}
11 changes: 11 additions & 0 deletions src/models/config/BooleanField.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
24 changes: 24 additions & 0 deletions src/models/config/ConfigFile.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
13 changes: 13 additions & 0 deletions src/models/config/Field.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
14 changes: 0 additions & 14 deletions src/modules/light.rgg

This file was deleted.

40 changes: 0 additions & 40 deletions src/modules/organs.rgg

This file was deleted.

10 changes: 10 additions & 0 deletions src/organConfig.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
leaf:
name: models.Leaf
functions:
- functions.Growth
- functions.LightInterception

fruit:
name: models.Fruit
functions:
- functions.Growth
Loading

0 comments on commit faa3c4f

Please sign in to comment.