Behrooz
Behrooz

Reputation: 1955

JASON (AgentSpeak) how to use external actions with variables

I know how to define an external action that is atomic (has to arguments) such as "sitDown" and then manually code it in the Environment Java file within the execute action method.

However, if I define an external action, is there a way for it to accept arguments? Let us say I want to define an action that can be invoked in agent code this way:

destroy(4, 7);

In my environment class, how would I retrieve those values within the execute action method?

Upvotes: 3

Views: 865

Answers (2)

Jomi Hubner
Jomi Hubner

Reputation: 141

If not using Cartago environment, but the Jason Environment, you can get the values of the arguments using the Structure class API. For example:

class ... extends Environment {
   ...
   public boolean executeAction(String ag, Structure action) {
      NumberTerm arg0 = (NumberTerm)action.getTerm(0);
      int vl = (int)arg0.solve();
   }
   ...

The Jason API is available here.

Upvotes: 1

Cleber Jorge Amaral
Cleber Jorge Amaral

Reputation: 1442

Yes, you can send arguments to the environment as well as get feedbacks.

For example, in an application, a method that is changing a LED of a robot could be like this (this belongs to a class that extends Artifact - CArtAgO):

@OPERATION 
void changeLedPin(String newState) throws Exception {
     try {
        /** put gpio HIGH */
        if (newState.equals("high")) {
            logger.info("Changing pin to HIGH!");
            ledPin.high();
        }

        /** put gpio LOW */
        if (newState.equals("low")) {
            logger.info("Changing pin to LOW!");
            ledPin.low();
        }
     } catch (Exception e) {
        e.printStackTrace();
   }
}

Jason's agent code to invoke this external action could be like this:

!start.

+!start <- 
    changeLedPin(low).

For feedbacks you can use OpFeedbackParam sending in Jason code a variable to unify. The artifact method is something like this:

@OPERATION 
void inc(OpFeedbackParam<String> value) {
    /* some code */
}

Sources: https://github.com/cleberjamaral/goldminers/blob/master/src/env/mining/Raspi.java https://github.com/cleberjamaral/camel-artifact/blob/master/camelJaCaMoRobot/src/env/artifacts/Counter.java

Upvotes: 1

Related Questions