Skip to content

Commit 23f81f2

Browse files
Dynamic Traits Cookbook (#28)
Dynamic Traits Cookbook This commit illustrates Dynamic Traits w.r.t. the Agents, respective properties and the shared lanes. Author: Aditya R Reviewers: Ajay G, Rohit B Test: Manual Issue: #24
1 parent 25239c5 commit 23f81f2

File tree

10 files changed

+116
-35
lines changed

10 files changed

+116
-35
lines changed

settings.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ project(':auth-policy').projectDir = file('auth_policy')
5757
include 'web-agents-server-recon'
5858
project(':web-agents-server-recon').projectDir = file('web_agents_server_recon')
5959

60-
include 'static-traits'
61-
project(':static-traits').projectDir = file('static_traits')
60+
include 'traits'
61+
project(':traits').projectDir = file('traits')

static_traits/README.md

Lines changed: 0 additions & 13 deletions
This file was deleted.

traits/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Traits
2+
3+
Code corresponding to the [Traits cookbook](https://swimos.org/tutorials/traits/).
4+
5+
*Read this in other languages: [简体中文](README.zh-cn.md)*
6+
7+
## Running This Demo
8+
9+
This demo only requires running the `swim.liquid.LiquidPlane` class.
10+
11+
Thus, `gradle traits:run` is sufficient for those who pick Option 2 in [`README.md` from the parent directory](../README.md).
12+
13+
For those who pick Option 3, simply execute `gradle traits:build`, unpackage either artifact in `traits/build/distributions`, and run the appropriate script in `bin/`; no file copying/editing is required.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
apply from: '../project.gradle'
22

3-
description = 'Code corresponding to the Static Traits cookbook'
3+
description = 'Code corresponding to the Traits cookbook'
44
ext.moduleName = 'swim.liquid'
55
mainClassName = 'swim.liquid.LiquidPlane'

static_traits/src/main/java/swim/liquid/LiquidPlane.java renamed to traits/src/main/java/swim/liquid/LiquidPlane.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,41 @@
1414

1515
package swim.liquid;
1616

17+
import swim.actor.ActorSpace;
1718
import swim.api.plane.AbstractPlane;
1819
import swim.kernel.Kernel;
1920
import swim.server.ServerLoader;
21+
import swim.structure.Value;
2022

2123
public class LiquidPlane extends AbstractPlane {
2224

2325
public static void main(String[] args) throws InterruptedException {
2426
final Kernel kernel = ServerLoader.loadServer();
27+
final ActorSpace space = (ActorSpace) kernel.getSpace("liquid");
2528

2629
kernel.start();
27-
System.out.println("Running Liquid server...");
30+
System.out.println("Running Basic server...");
2831
kernel.run();
2932

30-
Thread.sleep(1000);
33+
// Dynamic Agent(s)
34+
int n = 0;
35+
String nodeString = "";
36+
final String[] listOfLiquid = new String[]{"black", "pineapple", "tap", "mango"};
37+
while (n < 4) {
38+
if (n % 2 == 0) {
39+
nodeString = "/liquid/dynamic/water/" + listOfLiquid[n];
40+
} else {
41+
nodeString = "/liquid/dynamic/juice/" + listOfLiquid[n];
42+
}
43+
space.command(nodeString, "unusedForNow", Value.absent());
44+
n++;
45+
Thread.sleep(2000);
46+
}
3147

3248
System.out.println("Server will shut down in 3 seconds");
3349
Thread.sleep(3000);
3450
System.out.println("Sent shutdown signal to server");
3551
kernel.stop();
3652
}
53+
3754
}

static_traits/src/main/java/swim/liquid/agent/JuiceAgent.java renamed to traits/src/main/java/swim/liquid/agent/JuiceAgent.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@
2020

2121
public class JuiceAgent extends AbstractAgent {
2222

23+
// Lane utilized for static and dynamic traits.
2324
@SwimLane("sharedInfo")
2425
ValueLane<String> sharedInfo;
2526

2627
@Override
2728
public void didStart() {
28-
System.out.println(nodeUri() + " didStart region");
29-
pour();
29+
System.out.println(nodeUri() + " didStart");
30+
pourStatic();
31+
if (nodeUri().toString().contains("dynamic")) {
32+
pourDynamic();
33+
}
3034
close();
3135
}
3236

@@ -35,19 +39,34 @@ public void willStop() {
3539
logMessage("willStop");
3640
}
3741

38-
// Fetch value of property belonging to the /juice/orange uri.
39-
void pour() {
42+
// Fetch value of property belonging to the /liquid/static/juice/orange uri.
43+
void pourStatic() {
4044
final String juiceInfo = getProp("juiceType").stringValue(null);
4145
if (juiceInfo != null) {
42-
logMessage("Juice Property '" + juiceInfo + "'");
46+
logMessage("Static Juice Property '" + juiceInfo + "'");
47+
48+
// Set Value for the sharedInfo SwimLane which is shared by Liquid,
49+
// Water and Juice Agent.
50+
this.sharedInfo.set("Statically shared liquid is '" + juiceInfo + "'");
51+
}
52+
}
53+
54+
// Set value of the sharedInfo lane belonging to the /liquid/:trait/:id1/:id2 pattern.
55+
void pourDynamic() {
56+
final String liquidType = getProp("id1").stringValue(null);
57+
final String liquidName = getProp("id2").stringValue(null);
58+
if (liquidType != null && liquidName != null) {
59+
logMessage("Dynamic Juice Property '" + liquidName + "'");
4360

4461
// Set Value for the sharedInfo SwimLane which is shared by Liquid,
4562
// Water and Juice Agent.
46-
this.sharedInfo.set("Shared liquid is " + juiceInfo);
63+
this.sharedInfo.set("Dynamically shared liquid is '" + liquidName + " "
64+
+ liquidType + "'");
4765
}
4866
}
4967

5068
private void logMessage(Object msg) {
5169
System.out.println(nodeUri() + ": " + msg);
5270
}
71+
5372
}

static_traits/src/main/java/swim/liquid/agent/LiquidAgent.java renamed to traits/src/main/java/swim/liquid/agent/LiquidAgent.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,18 @@
2020

2121
public class LiquidAgent extends AbstractAgent {
2222

23+
// Lane utilized for static and dynamic traits.
2324
@SwimLane("sharedInfo")
2425
ValueLane<String> sharedInfo;
2526

2627
@Override
2728
public void didStart() {
28-
System.out.println(nodeUri() + " didStart region");
29-
pour();
29+
System.out.println(nodeUri() + " didStart");
30+
if (nodeUri().toString().contains("dynamic")) {
31+
pourDynamic();
32+
} else {
33+
pourStatic();
34+
}
3035
close();
3136
}
3237

@@ -36,13 +41,27 @@ public void willStop() {
3641
}
3742

3843
// Fetch info shared with other agents.
39-
void pour() {
44+
void pourStatic() {
4045
// Fetching message via sharedInfo SwimLane.
4146
final String msg = this.sharedInfo.get();
4247
logMessage(msg);
4348
}
4449

50+
// 1. Fetch value of properties belonging to the /liquid/:trait/:id/:id pattern.
51+
// 2. Open agents dynamically.
52+
// 2. Fetch value of sharedInfo lane for the opened agents.
53+
void pourDynamic() {
54+
// Dynamically open either Water or Juice Agent at runtime.
55+
if (getProp("id1").stringValue().equals("water")) {
56+
openAgent("wAgent", WaterAgent.class);
57+
} else if (getProp("id1").stringValue().equals("juice")) {
58+
openAgent("jAgent", JuiceAgent.class);
59+
}
60+
logMessage(this.sharedInfo.get());
61+
}
62+
4563
private void logMessage(Object msg) {
4664
System.out.println(nodeUri() + ": " + msg);
4765
}
66+
4867
}

static_traits/src/main/java/swim/liquid/agent/WaterAgent.java renamed to traits/src/main/java/swim/liquid/agent/WaterAgent.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020

2121
public class WaterAgent extends AbstractAgent {
2222

23+
// Lane utilized for static and dynamic traits.
2324
@SwimLane("sharedInfo")
2425
ValueLane<String> sharedInfo;
2526

2627
@Override
2728
public void didStart() {
28-
System.out.println(nodeUri() + " didStart region");
29-
pour();
29+
System.out.println(nodeUri() + " didStart");
30+
pourStatic();
31+
pourDynamic();
3032
close();
3133
}
3234

@@ -35,19 +37,34 @@ public void willStop() {
3537
logMessage("willStop");
3638
}
3739

38-
// Fetch value of property belonging to the /water/sparkling uri.
39-
void pour() {
40+
// Fetch value of property belonging to the /liquid/static/water/sparkling uri.
41+
void pourStatic() {
4042
final String waterInfo = getProp("waterType").stringValue(null);
4143
if (waterInfo != null) {
42-
logMessage("Water Property '" + waterInfo + "'");
44+
logMessage("Static Water Property '" + waterInfo + "'");
4345

4446
// Set Value for the sharedInfo SwimLane which is shared by Liquid,
4547
// Water and Juice Agent.
46-
this.sharedInfo.set("Shared liquid is " + waterInfo);
48+
this.sharedInfo.set("Statically shared liquid is '" + waterInfo + "'");
49+
}
50+
}
51+
52+
// Set value of the sharedInfo lane belonging to the /liquid/:trait/:id1/:id2 pattern.
53+
void pourDynamic() {
54+
final String liquidType = getProp("id1").stringValue(null);
55+
final String liquidName = getProp("id2").stringValue(null);
56+
if (liquidType != null && liquidName != null) {
57+
logMessage("Dynamic Water Property '" + liquidName + "'");
58+
59+
// Set Value for the sharedInfo SwimLane which is shared by Liquid,
60+
// Water and Juice Agent.
61+
this.sharedInfo.set("Dynamically shared liquid is '" + liquidName + " "
62+
+ liquidType + "'");
4763
}
4864
}
4965

5066
private void logMessage(Object msg) {
5167
System.out.println(nodeUri() + ": " + msg);
5268
}
69+
5370
}

static_traits/src/main/resources/server.recon renamed to traits/src/main/resources/server.recon

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
liquid: @fabric {
1313
@plane(class: "swim.liquid.LiquidPlane")
1414
@node {
15-
uri: "/water/sparkling"
15+
uri: "/liquid/static/water/sparkling"
1616
@agent(class: "swim.liquid.agent.WaterAgent") {
1717
waterType: "Sparkling Water"
1818
}
@@ -21,12 +21,21 @@ liquid: @fabric {
2121
}
2222
}
2323
@node {
24-
uri: "/juice/orange"
24+
uri: "/liquid/static/juice/orange"
2525
@agent(class: "swim.liquid.agent.JuiceAgent") {
2626
juiceType: "Orange Juice"
2727
}
2828
@agent(class: "swim.liquid.agent.LiquidAgent") {
2929
liquidType: "Juice"
3030
}
3131
}
32+
@node {
33+
pattern: "/liquid/:trait/:id1/:id2"
34+
@agent(class: "swim.liquid.agent.WaterAgent") {
35+
}
36+
@agent(class: "swim.liquid.agent.JuiceAgent") {
37+
}
38+
@agent(class: "swim.liquid.agent.LiquidAgent") {
39+
}
40+
}
3241
}

0 commit comments

Comments
 (0)