Мне нужно получить доступ к значению из одного класса с помощью AID - PullRequest
0 голосов
/ 16 мая 2019

Я застрял с проблемой нефритового агента, которая не позволяет мне продолжать практику подписи.

Проблема в следующем:

У меня есть два основных агента: продавец и покупатель.Там может быть более одного каждого.Они общаются с протоколами JADE.Продавец говорит цену товара, а покупатель его покупает.Итак, я решил внедрить HashMap в агенте покупателя, потому что у продавца есть связанная цена, поэтому, когда покупатель хочет найти агентов продавца, они сохраняются в хэш-карте.В хэш-карте ключом является AID агента, а значением - цена продукта, который он продает (это локальная переменная продавца).Но когда я хочу получить доступ к цене продукта, я не знаю, как его получить.


Агент продавца.

public class SellerAgent extends Agent {

    private ArrayList<String> messages;  // Messages between them are stored in ArrayList.
    int price;  // Price of the product.


    // setup() and takeDown().
    @Override
    protected void setup() {
        messages= new ArrayList();
        price = rnd.nextInt(50);

        // Agent registration
        ServiceDescription sd = new ServiceDescription();
        sd.setType("GUI");
        sd.setName("Seller");

        DFAgentDescription dfd = new DFAgentDescription();
        dfd.setName(getAID());
        dfd.addServices(sd);

        try {
            DFService.register(this, dfd);
        } catch (FIPAException fe) {
            fe.printStackTrace();
        }
    }

    @Override
    protected void takeDown() {
        try {
            DFService.deregister(this);
        } catch (FIPAException fe) {
            fe.printStackTrace();
        }

        System.out.println("END OF " + this.getName());
    }

    public Integer getPrice() { return price; }
}

Агент покупателя.

public class BuyerAgent extends Agent {

    private HashMap<AID, Integer> sellers;  // Sellers.
    private ArrayList<String> messages;   // Messages between them are stored in ArrayList.


    @Override
    protected void setup() {
        sellers = new HashMap();
        messages = new ArrayList();

        // Agent registration
        ServiceDescription sd = new ServiceDescription();
        sd.setType("GUI");
        sd.setName("Buye");

        DFAgentDescription dfd = new DFAgentDescription();
        dfd.setName(getAID());
        dfd.addServices(sd);

        try {
                DFService.register(this, dfd);
        } catch (FIPAException fe) {
                fe.printStackTrace();
        }

        // SearchAgents Task.
        addBehaviour(new SearchAgents(this, 5000));           // Search Seller Agent.
    }

    @Override
    protected void takeDown() {
        try {
            DFService.deregister(this);
        } catch (FIPAException fe) {
            fe.printStackTrace();
        }

        System.out.println("END OF " + this.getName());
    }


    public class SearchAgents extends TickerBehaviour {

        public SearchAgents(Agent a, long period) {
            super(a, period);
        }

        @Override
        protected void onTick() {
            DFAgentDescription template;
            ServiceDescription sd;
            DFAgentDescription[] result;

            // We search Seller Agents.
            sd = new ServiceDescription();
            sd.setName("Seller");

            template = new DFAgentDescription();
            template.addServices(sd);

            try {
                result = DFService.search(myAgent, template);

                if (result.length > 0) {
                    agricultores.clear();
                    for (int i = 0; i < result.length; ++i) 
                        seller.put(result[i].getName(), i); /*HERE IS THE PROBLEM, value needs to be price of seller product, not i*/
                } else {
                    // No agents where found.
                    agricultores.clear();
                    System.out.println("Not found.");
                } 
            } catch (FIPAException fe) {
                fe.printStackTrace();
            }
        }
    }
}
...