Example 10 - Working with Multiple Workspaces In the following example, creates two workspaces, joins both and prints messages using different console artifacts, and then uses internal actions to set the current workspace.
The agent source code:MAS example10_workspaces { environment: c4jason.CartagoEnvironment agents: wsp_tester agentArchClass c4jason.CAgentArch; classpath: "../../../lib/cartago.jar";"../../../lib/c4jason.jar"; }
Highlights:!test_wsp. +!test_wsp <- ?current_wsp(Id0,Name,NodeId); println("current workspace ",Name," ",NodeId); println("creating new workspaces..."); createWorkspace("myNewWorkspace1"); createWorkspace("myNewWorkspace2"); joinWorkspace("myNewWorkspace1",WspID1); ?current_wsp(_,Name1,_); println("hello in ",Name1); makeArtifact("myCount","c4jexamples.Counter",[],ArtId); joinWorkspace("myNewWorkspace2",WspID2); ?current_wsp(_,Name2,_); println("hello in ",Name2); println("using the artifact of another wsp..."); inc [artifact_id(ArtId)]; cartago.set_current_wsp(WspID1); println("hello again in ",WspID1); println("quit.."); quitWorkspace; ?current_wsp(_,Name3,_); println("back in ",Name3); quitWorkspace; cartago.set_current_wsp(Id0); ?current_wsp(_,Name4,_); println("...and finallly in ",Name4," again.").
current_wsp(WspId,Name,NodeId
belief.cartago.set_current_wsp(WspID)
internal action makes it possible to set the current workspace, specifying its id
.createWorkspace
- to create a new workspace in current node, provided by the NodeArtifact
, joinWorkspace
- to join a workspace on the node, provided by the NodeArtifact
, quitWorkspace
to quit the workspace, provided by the WorkspaceArtifact
.