AFAS::Inheritance

From Agent Factory

Jump to: navigation, search

This is a lesson from the AF-AgentSpeak language guide.

Introduction

One key difference between AF-AgentSpeak and other implementations of AgentSpeak is our inclusion of mechanisms that support reuse. Our model is based on some of the accepted reuse mechanisms employed in Object-Oriented Programming Languages. Given Agent Factory is implemented in Java, it seems only natural that our implementation would use similar modifiers to those employed in Java.

The overall approach adopted is based on a language independent reuse framework for AOP languages that we have developed and tested with AF-AgentSpeak, AFAPL, and AF-TeleoReactive.

Inheritance

Inheritance in AF-AgentSpeak is supported through the #extends modifier. Its use is the same as extends in Java, in that it is followed by the label of the agent program that is being extended. The effect of extending an agent program is that all plan rules, modules, actions and sensors specified in that program are combined with the additional plan rules, modules, actions and sensors declared in the agent program that is extending. Terminologically, we say that the agent program that is being extended is the parent (agent) program, and the extended agent program is the sub-(agent) program.

On terms of syntax, the inheritance mechanism is integrated with the #agent declaration that must appear at the start of any agent program:

#agent MyAgentProgram

As with Java, the label that is used for the program must match the name of the file in which the agent program has been written.

To extend this agent program, we simple create a second agent program and specify it as an extension of the first agent program:

#agent MySubAgentProgram #extends MyAgentProgram

As a more practical example, lets look at the following agent programs:

#agent BaseAgent

+initialized : true <-
    .println("Base Agent Initialization"),
    +hook;

#agent ExtendedAgent #extends BaseAgent

+hook : true <-
    .println("Extended Agent plan");

If you use packages to organise you agent code (i.e. the agent programs are located packages that are part of your Java source code), then you need to give the fully qualified name of the parent agent program. For example, consider the following package structure:

/src
 |
 |---system
 |     |
 |     |---Main.java
 |
 |---agents
       |
       |---BaseAgent.aspeak
       |
       |---ExtendedAgent.aspeak

In this scenario, the code for the two agents would look as follows:


#agent BaseAgent

+initialized : true <-
    .println("Base Agent Initialization"),
    +hook;

#agent ExtendedAgent #extends agents.BaseAgent

+hook : true <-
    .println("Extended Agent plan");

Additionally, the Main class would include addAgent(...) methods that reference the fully qualified agent program:

import com.agentfactory.agentspeak.interpreter.AgentSpeakArchitectureFactory;
import com.agentfactory.agentspeak.debugger.AgentSpeakStateManagerFactory;
import com.agentfactory.agentspeak.debugger.inspector.AgentSpeakInspectorFactory;
import com.agentfactory.visualiser.DebuggerRunConfiguration;

public class Main {
   public static class MyConfig extends DebuggerRunConfiguration {
       public String getName() {
           return "helloworld";
       }

       public String getDomain() {
           return "ucd.ie";
       }

       public void configure() {
           addLanguageSupport(new AgentSpeakInspectorFactory(), new AgentSpeakStateManagerFactory());
           super.configure();
           addArchitectureFactory(new AgentSpeakArchitectureFactory());
           addAgent("Bob", "agents/ExtendedAgent.aspeak");
       }
   }

   public static void main(String[] args) {
       new MyConfig().configure();
   }
}

Plan Overriding

Most Object-Oriented Languages provide a mechanism to allow you to change the implementation of a given plan. In AF-AgentSpeak we provide this support through a combination of plan labelling and the introduction of an #overrides operator.