Example 08 - Linkability
Linkability is the mechanism that makes it possible to create interactions among artifacts, i.e. execute inter-artifacts operations. Besides
the usage interface, an artifact can expose operations - to be tagged with @LINK
. These operations are meant to be called
by other artifacts. In order to allow an artifact A to execute operations over an artifact B, two options are provided:
linkArtifacts
action,
specifying the name of an output port that the artifact A must expose. Then, operations of artifact A can
execute operations of the linked artifact B by using the execLinkedOp
primitive, specifying the output
port where the linked artifact has been linked.
execLinkedOp
the target identifier of the artifact BHighlights:MAS example08_linkability { environment: c4jason.CartagoEnvironment agents: linkability_tester agentArchClass c4jason.CAgentArch; classpath: "../../../lib/cartago.jar";"../../../lib/c4jason.jar"; } \end{verbatim}}} \noindent Source code of the linkable artifact: {\small{\begin{verbatim} public class LinkableArtifact extends Artifact { int count; void init(){ count = 0; } @LINK void inc(){ log("inc invoked."); count++; } @LINK void getValue(OpFeedbackParamv){ log("getValue invoked"); v.set(count); } }
@LINK
operations: the semantics of linked operations is the same of normal operation.The@ARTIFACT_INFO( outports = { @OUTPORT(name = "out-1") } ) public class LinkingArtifact extends Artifact { @OPERATION void test(){ log("executing test."); try { execLinkedOp("out-1","inc"); } catch (Exception ex){ ex.printStackTrace(); } } @OPERATION void test2(OpFeedbackParamv){ log("executing test2."); try { execLinkedOp("out-1","getValue", v); log("back: "+v.get()); } catch (Exception ex){ ex.printStackTrace(); } } @OPERATION void test3(){ log("executing test3."); try { ArtifactId id = makeArtifact("new_linked", "c4jexamples.LinkableArtifact", ArtifactConfig.DEFAULT_CONFIG); execLinkedOp(id,"inc"); } catch (Exception ex){ ex.printStackTrace(); } } }
test
and test2
operations executes respectively the inc
and getValue
operation
over the artifact linked to the out-1
port. The operation test3
instead creates an artifact and executes a linked operation directly using artifact identifier.
Highlights:
@ARTIFACT_INFO
annotation of the artifact class, outports
attribute;execOpLinked
primitive suspend the operation execution until the operation execution on the linked artifact has completed.Highlights:!test_link. +!test_link <- makeArtifact("myArtifact","c4jexamples.LinkingArtifact",[],Id1); makeArtifact("count","c4jexamples.LinkableArtifact",[],Id2); linkArtifacts(Id1,"out-1",Id2); println("artifacts linked: going to test"); test; test2(V); println("value ",V); test3.
linkArtifact
's parameters include the identifier of the linking artifact, its outport and the identifier of the linked artifact.