Skip to content

Commit

Permalink
add runAsAg in RTS
Browse files Browse the repository at this point in the history
  • Loading branch information
jomifred committed Apr 11, 2024
1 parent c9f1b4b commit 86275ee
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
43 changes: 24 additions & 19 deletions jason-cli/src/main/java/jason/cli/agent/RunAsAgent.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
package jason.cli.agent;

import jason.architecture.AgArch;
import jason.asSemantics.IntendedMeans;
import jason.asSemantics.Intention;
import jason.asSemantics.Option;
import jason.asSemantics.Unifier;
import jason.asSyntax.ASSyntax;
import jason.asSyntax.Plan;
import jason.asSyntax.PlanBody;
import jason.cli.mas.RunningMASs;
import jason.stdlib.print_unifier;
import picocli.CommandLine;
import picocli.CommandLine.Command;

import java.rmi.RemoteException;
import java.util.List;


@Command(
name = "run-as",
description = "executes commands for an agent"
)
public class RunAsAgent implements Runnable {
public class RunAsAgent extends BaseAgent implements Runnable {

@CommandLine.Parameters(paramLabel = "<agent name>", defaultValue = "",
arity = "1",
description = "agent unique identification")
String agName;

@CommandLine.Option(names = { "--mas-name" }, paramLabel = "<mas name>", defaultValue = "", description = "MAS identification where to find the agent")
String masName;

@CommandLine.Parameters(hidden = true) // "hidden": don't show this parameter in usage help message
List<String> allParameters; // no "index" attribute: captures _all_ arguments

Expand All @@ -35,35 +30,45 @@ public class RunAsAgent implements Runnable {

@Override
public void run() {
if (!RunningMASs.hasLocalRunningMAS()) {
parent.parent.errorMsg("no local running MAS, so, no agent to run the commands.");
return;
}
masName = testMasName(masName, parent.parent);
testRunningMAS(masName, parent.parent);

if (agName.isEmpty()) {
parent.parent.errorMsg("the name of the agent should be informed, e.g., 'agent run-as bob { .print(oi) }'.");
return;
}
if (!RunningMASs.hasAgent(null, agName)) {

if (!RunningMASs.hasAgent(masName, agName)) {
parent.parent.errorMsg("the agent with name " + agName + " is not running!");
return;
}

String code = "";
if (allParameters.size()>0) {
if (!allParameters.isEmpty()) {
var last = allParameters.get( allParameters.size()-1).trim();

if (last.startsWith("{")) {
code = last.substring(1,last.length()-1).trim();
}
if (last.startsWith("}")) {
code = allParameters.get( allParameters.size()-2).trim();
}
}

if (!code.isEmpty()) {
execCmd(code, RunningMASs.getLocalRunningMAS().getAg(agName));
try {
var r = RunningMASs.getRTS(masName).runAsAgent(agName, code);
parent.parent.println(r);
} catch (RemoteException e) {
parent.parent.errorMsg("Error executing "+code+"\n"+e);
e.printStackTrace();
}
} else {
parent.parent.errorMsg("no code to execute was informed. E.g., 'agent run-as bob { .print(oi) }' ");
}
}

void execCmd(String sCmd, AgArch ag) {
/*void execCmd(String sCmd, AgArch ag) {
try {
sCmd = sCmd.trim();
if (sCmd.endsWith("."))
Expand Down Expand Up @@ -97,7 +102,7 @@ void execCmd(String sCmd, AgArch ag) {
} catch (Exception e) {
parent.parent.errorMsg("Error parsing "+sCmd+"\n"+e);
}
}
}*/

}

1 change: 1 addition & 0 deletions jason-interpreter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ publishing {


task publishMavenGitHub(dependsOn: [ 'build', 'publishToMavenLocal']) {
group = "publishing"
doLast {
def wdir = System.getProperty("user.home")+'/.m2/repository/io/github/jason-lang/jason-interpreter' //org/jason/jason'
def rdir = project.projectDir.absolutePath+'/../../jacamo-mvn-repo'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public String runAsAgent(String agName, String code) {
} catch (Exception e) {
return "Error parsing "+code+"\n"+e;
}
return "ok";
return "execution ok";
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ default public void clone(Agent source, List<String> archClasses, String agName)
default public String loadASL(String agName, String code, String sourceId, boolean replace) throws RemoteException { return "not implemented"; }

/** agent agName executes the code (in a new intention) */
//public String runAsAgent(String agName, String code);
default public String runAsAgent(String agName, String code) throws RemoteException { return "not implemented"; }

/** Stops all MAS (the agents, the environment, the controller, ...) */
public void stopMAS(int deadline, boolean stopJVM, int exitValue) throws RemoteException, Exception;
Expand Down

0 comments on commit 86275ee

Please sign in to comment.