《关于多代理系统的研究》04

本篇主要讲解一下capability,也就是Agent的重用。

1. 简单介绍

重用性是软件工程领域一个很重要的特性,一次开发,多处使用。在Jadex中,使用的是BDI capabilities。一个BDI capability表示为一个模块包含beliefs,goals和plans,就像一个正常的agent一样。但是不具备单独行动的能力。
使用方法:类。
capability实现重用,那么就应该理所当然的声明为一个单独的类。然后作为一个完整的模块在agent中使用。

2. 代码实例

首先创建一个capability文件,命名没有要求。但是规范的话,还是在名字之后加上capability,方便区分。
文件名:TranslateCapa.java

package a2;

import java.util.HashMap;
import java.util.Map;

import jadex.bdiv3.annotation.Belief;
import jadex.bdiv3.annotation.Capability;
import jadex.bdiv3.annotation.Goal;
import jadex.bdiv3.annotation.GoalParameter;
import jadex.bdiv3.annotation.GoalResult;
import jadex.bdiv3.annotation.Plan;
import jadex.bdiv3.annotation.Trigger;

@Capability
public class TranslateCapa {
    @Belief
    protected Map<String, String> wordTable;

    public TranslateCapa() {
        // TODO Auto-generated constructor stub
        this.wordTable = new HashMap<String, String>();
        wordTable.put("milk", "牛奶");
        wordTable.put("banana", "香蕉");
        wordTable.put("school", "学校");
        wordTable.put("teacher", "老师");
        wordTable.put("science", "科学");
    }

    @Goal
    public class TranslateGoal {
        @GoalParameter
        protected String eword;
        @GoalResult
        protected String cnword;

        public TranslateGoal(String eword) {
            this.eword = eword;
        }
    }

    @Plan(trigger = @Trigger(goals = TranslateGoal.class))
    public String translatePlan(String ewordString) {
        return wordTable.get(ewordString);
    }
}

然后创建一个BDI文件,直接把capability嵌入到Agent的一个成员使用。非常简单方便。

package a2;

import jadex.bdiv3.BDIAgent;
import jadex.bdiv3.annotation.Capability;
import jadex.micro.annotation.Agent;
import jadex.micro.annotation.AgentBody;
import jadex.micro.annotation.Description;

@Agent
@Description("<h1>Using Capability</h1>")
public class TranslateBDI {
    @Agent
    protected BDIAgent translateAgent;

    @Capability
    protected TranslateCapa capa = new TranslateCapa();

    @AgentBody
    public void body(){
        String eword = "teacher";
        String cnword = (String) translateAgent.dispatchTopLevelGoal(capa.new TranslateGoal(eword)).get();
        System.out.println("翻译完成:" + eword + "---->" + cnword);
    }
}

总结一下:capability就是一个模块,大家都可以调用来使用。好处减少了开发的资源浪费。但是需要很好的设计基础,才可以使用capability。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值