• Ei tuloksia

An Example of Using the Agent Architecture Tasks

5. Implementation of the Language-Independent Agent Architecture

5.1 The Implementation of the Agent Architecture

5.1.3 An Example of Using the Agent Architecture Tasks

This section continues example of Section 4.1.2. Figure 5.4 shows the tags for agent architecture infrastructure defined in Figure 4.2. This figure has been extended from Figure 4.2 by specifying which tags each entity has. Agent architecture will automatically assign the IDs for each entity, if needed, so they are not required to be explicitly defined. Typically, the decision of what kind of tags things have is design-time decision.

InfrastructureAgent

Agent for opening a view Task (C#):

Figure 5.4: The agent architecture setup for the opening a view in Eclipse.

The tags are shown as a rectangle with dashed border line inside the boxes of the used areas, locations, and tasks. Inside the rectangle, each tag is on its own line.

5. Implementation of the Language-Independent Agent Architecture 33

The pattern for the text on each line is key = value. The tags of the Transporter are omitted from the picture for brevity.

Now that the tags has been decided, it is time to actually implement things. Let us assume that the MMA is written inC#, and the Eclipse plug-in is written inJava.

Two methods shown in Listing 5.1 contain the code required to construct the main area of MMA. This code uses theAreaFactory and TaskPool services to create area and set up the task pool, respectively. The AreaFactory is part of abstract factory design pattern used to create Areas. These services are singletons and obtained at run-time using suitable frameworks of the native language.

public c l a s s MMASetup {

public Area CreateTAArea ( A r e a F a c t o r y a r e a F a c t o r y ) {

return a r e a F a c t o r y.CreateSubArea (new MMAAreaArchetype ( ) ) ; }

public void DoMMATaskPoolSetup ( TaskPool t a s k P o o l ) {

t a s k P o o l.R e g i s t e r C o n t e x t C r e a t o r ( new D e f a u l t T a s k C r e a t o r (

new S t a r t A p p l i c a t i o n T a s k A r c h e t y p e ( ) ) );

} }

Listing 5.1: Setting up the agent architecture environment for the sub-area of MMA.

The MMAAreaArchetype is a simple class implementing the required interface specified in agent architecture. This interface has methods to extract the information about what kind of tags, tasks, and locations the area will have. Thus building an area is a very simple action.

The StartApplicationTaskArchetype is similarly to MMAAreaArchetype informa-tion-encapsulating class about the starting application task. The task archetype provides information about the tags associated with the task, and the class of the task. Thus the TaskPool service will use the tags to locate the correct context cre-ator, and the context creator will know which class to instantiate. This instantiated class will be the task to be executed. The code for both MMAAreaArchetype and StartApplicationTaskArchetype is given in Appendix 1.

The agent arhictecture-related task implementation to start an application can be seen in Listing 5.2. Each task must implement the AgentTask context interface defined by agent architecture. Additionally, agent architecture provides an

Agent-5. Implementation of the Language-Independent Agent Architecture 34

TaskSkeleton, a helper class with implementation for DCI-related things. This way task writer can concentrate on actual task. TheAgentTaskSkeleton class also imple-ments AgentTask but leaves those methods abstract, hence theShowDataTask only extends AgentTaskSkeleton without needing to explicitly implement AgentTask.

public c l a s s S t a r t A p p l i c a t i o n T a s k : A g e n t T a s k S k e l e t o n {

public override T a s k D e s t i n a t i o n S e a r c h F i l t e r GetRequirements ( ) {

// A l l o c a t e t h e f a c t o r y

T a s k D e s t i n a t i o n F a c t o r y t d f = . . . ;

return t d f.C r e a t e F i l t e r (

null, // Any a r e a t a g s a r e ok null, // Any l o c a t i o n t a g s a r e ok

null, // No need t o match s p e c i f i c a r e a ID null, // No need t o match s p e c i f i c l o c a t i o n ID true // V i s i t e d l o c a t i o n s a r e a l l o w e d

); }

public override S t r i n g ExecuteTask ( ) {

// Agent and L o c a t i o n a r e r o l e s o f AgentTask Agent a g e n t = t h i s .RoleMap.Get<Agent >();

L o c a t i o n l o c a t i o n = t h i s .RoleMap.Get<L o c a t i o n >();

// S t a r t up a p p l i c a t i o n i f n e e d e d . . .

// Return t a s k r e s u l t t e l l i n g t h a t a l l went f i n e return " TaskResultOK ";

} }

Listing 5.2: Model implementation for task to start up application.

The GetRequirements method returns the filter to match locations against when searching the location to execute task. The task to start the application can be executed on any location, since it does not use any location methods. Therefore all task destination filter values are null. Abstract factory pattern is used once again to createTaskDestinationSearchFilter in order to express required location for task.

5. Implementation of the Language-Independent Agent Architecture 35

The ExecuteTask method will be invoked only when suitable location has been found for the task to execute on. Inside the method, it is shown how one gets hold of agent and location. Furthermore, it is visible how the task result is returned.

Listing 5.3 contains the code necessary to create an agent and start it up. Once again, the archetype is used to provide agent architecture with required information in order to create new agent. For this example functionality, the task graph is simple, as shown in Figure 5.4. There are two tasks to execute, and there is only one transition from first to second task. The the requirement of the transition that the output of the previous task is TaskResultOK. The agent archetype is given in Appendix 1.

public void OpenView ( A ge n tF a ct or y a g e n t F a c t o r y , S t r i n g m a n i p u l a t o r I n f o , S t r i n g a p p l i c a t i o n P a t h , I n t 3 2 viewID ) {

a g e n t F a c t o r y.C r e at e A g e n t (

new OpenViewAgentArchetype (

m a n i p u l a t o r I n f o , a p p l i c a t i o n P a t h , viewID ) ).S t a r t E x e c u t i n g I n N e w T h r e a d ( );

}

Listing 5.3: Instantiation of agent for opening a view.

The agent archetype is given chance to add some data to its Data Container before the agent is returned to whoever wanted to create it. In this case, the name of manipulator, its application path, and the ID of the view are added to agent’s Data Container. Once the agent has been created, it is started by either Star-tExecutingInNewThread orStartExecutingInThisThread methods. The first method creates designated thread to start executing the agent, and the second one blocks until this agent execution lifeline stops or the agent is transported elsewhere. No further actions are required from the user of the agent architecture. The framework will take care of the task resolution, the serialization and the deserialization, and the transportation of the agent.

Listing 5.4 and Listing 5.5 show the required code to set up sub-area of Eclipse plug-in and the implementation of the task to show data, respectively. Note that except for different syntax, code is very similar to C# version.

public c l a s s E c l i p s e S e t u p {

public Area c r e a t e T e s t i n g A r e a ( A r e a F a c t o r y a r e a F a c t o r y ) {

return a r e a F a c t o r y.c r e a t e A r e a (new T e s t i n g A r e a A r c h e t y p e ( ) ) ; }

5. Implementation of the Language-Independent Agent Architecture 36

public void d o E c l i p s e T a s k P o o l S e t u p ( TaskPool t a s k P o o l ) {

t a s k P o o l.r e g i s t e r C o n t e x t C r e a t o r ( new D e f a u l t T a s k C r e a t o r (

new OpenViewTaskArchetype ( )

) ); }

public void d o E c l i p s e L o c a t i o n S e t u p (

L o c a t i o n M e t h o d R e t r i e v e r S e r v i c e methods , E c l i p s e M a n i p u l a t o r m a n i p u l a t o r )

{

methods.r e g i s t e r C o n t e x t C r e a t o r (

new D e f a u l t L o c a t i o n M e t h o d C o n t e x t C r e a t o r ( new Tags ( // Area t a g s

new Tag ( " example " , " i n f r a " ) , new Tag ( " i n f r a " , " T e s t i n g A r e a " ) ) ,

new Tags ( // L o c a t i o n t a g s new Tag ( " example " , " i n f r a " ) , new Tag ( " i n f r a " , " E c l i p s e " ) ) ,

m a n i p u l a t o r // R e s o u r c e )

); }

Listing 5.4: Setting up the agent architecture environment for Testing sub-area.

public c l a s s OpenViewTask extends A g e n t T a s k S k e l e t o n {

public T a s k D e s t i n a t i o n S e a r c h F i l t e r g e t R e q u i r e m e n t s ( ) {

Agent a g e n t = t h i s .getRoleMap ( ).g e t ( Agent. c l a s s); S t r i n g m a n i p u l a t o r I n f o = a g e n t.g e t D a t a C o n t a i n e r ( )

.g e t ( " m a n i p u l a t o r " ).t o S t r i n g ( ); T a s k D e s t i n a t i o n F a c t o r y t d f = . . . ;

i f ( " E c l i p s e ".e q u a l s ( m a n i p u l a t o r I n f o ) ) {

return t d f.c r e a t e F i l t e r (

new Tags ( // R e q u i r e d t a g s f o r a r e a new Tag ( " example " , " i n f r a " ) ,

5. Implementation of the Language-Independent Agent Architecture 37

new Tag ( " i n f r a " , " T e s t i n g A r e a " ) ) ,

new Tags ( // R e q u i r e d t a g s f o r l o c a t i o n new Tag ( " example " , " i n f r a " ) ,

new Tag ( " i n f r a " , " E c l i p s e " ) ) ,

null , // No need t o match s p e c i f i c a r e a ID null , // No need t o match s p e c i f i c l o c a t i o n ID true // V i s i t e d l o c a t i o n s a r e a l l o w e d

);

} e l s e { . . . } }

public S t r i n g e x e c u t e T a s k ( ) {

// Agent and L o c a t i o n a r e r o l e s o f AgentTask Agent a g e n t = t h i s .getRoleMap ( ).g e t ( Agent. c l a s s);

L o c a t i o n l o c a t i o n = t h i s .getRoleMap ( ).g e t ( L o c a t i o n. c l a s s);

L o c a t i o n M e t h o d R e t r i e v e r S e r v i c e l o c a t i o n M e t h o d s = . . . ;

// R e t r i e v e some n a t i v e r e s o u r c e r e p r e s e n t e d by t h i s l o c a t i o n E c l i p s e M a n i p u l a t o r m a n i p u l a t o r = l o c a t i o n M e t h o d s

.c r e a t e C o n t e x t ( l o c a t i o n )

.g e t R e s o u r c e ( E c l i p s e M a n i p u l a t o r. c l a s s);

// Command m a n i p u l a t o r t o open v i e w . . .

// Return t a s k r e s u l t t e l l i n g t h a t a l l went f i n e return " TaskResultOK ";

} }

Listing 5.5: Model implementation for task to collect data in Eclipse plug-in.

As in C# version, archetypes are used to supply required information to agent architecture. All the archetypes of the Listing 5.4 are presented in the Appendix 1.

5. Implementation of the Language-Independent Agent Architecture 38

5.2 The Implementation of the Language-Independent Data