How can i resolve these errors got in VS Code for running a java code in JADE platform

102 views Asked by At
import jade.core.Agent;
import jade.core.Runtime;
import jade.core.Profile;
import jade.core.ProfileImpl;
import jade.core.behaviours.*;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
public class Main {
public static void main(String[] args) {
    Runtime rt = Runtime.instance();
    Profile p = new ProfileImpl();
    AgentContainer container = rt.createMainContainer(p);
    
    try {
        AgentController seller = jade.core.container.createNewAgent("Seller", SellerAgent.class, null);
        seller.start();
    
        String[] buyerArgs = new String[]{"A"};
        AgentController buyer1 = container.createNewAgent("B1", BuyerAgent.class, buyerArgs);
        buyer1.start();
    
        buyerArgs[0] = "B";
        AgentController buyer2 = container.createNewAgent("B2", BuyerAgent.class, buyerArgs);
        buyer2.start();
    
        buyerArgs[0] = "A";
        AgentController buyer3 = container.createNewAgent("B3", BuyerAgent.class, buyerArgs);
        buyer3.start();
    
        buyerArgs[0] = "B";
        AgentController buyer4 = container.createNewAgent("B4", BuyerAgent.class, buyerArgs);
        buyer4.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

public class BuyerAgent extends Agent {
    private String requestedProduct;
    private String sellerName;
    private List<String> purchasedProducts = new ArrayList<>();

    protected void setup() {
        // Get the requested product name as an argument
        Object[] args = getArguments();
        if (args != null && args.length > 0) {
            requestedProduct = (String) args[0];
        }

        // Add the behavior to request the product from the seller
        addBehaviour(new RequestProductBehaviour());
    }

    private class RequestProductBehaviour extends OneShotBehaviour {
        public void action() {
            ACLMessage message = new ACLMessage(ACLMessage.REQUEST);
            message.setContent(requestedProduct);
            message.addReceiver(sellerName);
            myAgent.send(message);

            // Wait for the seller's response
            addBehaviour(new PurchaseResponseBehaviour());
        }
    }

    private class PurchaseResponseBehaviour extends SimpleBehaviour {
        private boolean receivedResponse = false;

        public void action() {
            MessageTemplate mt = MessageTemplate.MatchPerformative(ACLMessage.INFORM);
            ACLMessage message = myAgent.receive(mt);
            if (message != null) {
                // Product was available and was sold to this buyer
                purchasedProducts.add(requestedProduct);
                receivedResponse = true;
            } else {
                block();
            }
        }

        public boolean done() {
            if (receivedResponse) {
                System.out.println(getLocalName() + " purchased: " + purchasedProducts);
            }
            return receivedResponse;
        }
    }
}

public class SellerAgent extends Agent {
    private int itemsA = 2;
    private int itemsB = 2;
    private List<String> buyers = new ArrayList<>();

    protected void setup() {
        // Add the behavior to handle product request messages
        addBehaviour(new HandleRequestBehaviour());
    }

    private class HandleRequestBehaviour extends CyclicBehaviour {
        public void action() {
            MessageTemplate mt = MessageTemplate.MatchPerformative(ACLMessage.REQUEST);
            ACLMessage message = myAgent.receive(mt);
            if (message != null) {
                String product = message.getContent();
                String buyerName = message.getSender().getLocalName();
                ACLMessage reply = message.createReply();

                if (buyerName.equals("B3") && product.equals("B")) {
                    // Seller cannot sell product B to buyer B3
                    reply.setPerformative(ACLMessage.FAILURE);
                    reply.setContent("Not allowed");
                } else if (product.equals("A") && itemsA > 0) {
                    itemsA--;
                    reply.setPerformative(ACLMessage.INFORM);
                    buyers.add(buyerName);
                } else if (product.equals("B") && itemsB >
                0) {
                    itemsB--;
                    reply.setPerformative(ACLMessage.INFORM);
                    buyers.add(buyerName);
                } else {
                    reply.setPerformative(ACLMessage.FAILURE);
                    reply.setContent("Not available");
                }

                myAgent.send(reply);
            } else {
                block();
            }
        }
    }

    protected void takeDown() {
        System.out.println("Seller " + getLocalName() + " sold to: " + buyers);
    }
}


This code gives me errors below:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
        The method instance() is undefined for the type Runtime
        Profile cannot be resolved to a type
        ProfileImpl cannot be resolved to a type
        AgentContainer cannot be resolved to a type
        AgentController cannot be resolved to a type
        jade cannot be resolved
        AgentController cannot be resolved to a type
        AgentController cannot be resolved to a type
        AgentController cannot be resolved to a type
        AgentController cannot be resolved to a type

        at Main.main(main.java:10)
1

There are 1 answers

0
Hc. On

You have a dependency problem, your project cannot find Jade and its associated classes. You should add Jade using maven so that your IDE can update the missing libraries.

Repository to add to your pom

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

Dependency to add to your pom

<dependency>
    <groupId>com.gitlab.jade-project</groupId>
    <artifactId>jade</artifactId>
    <version>master-SNAPSHOT</version>
</dependency>

See : https://jade-project.gitlab.io/page/download/