Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.

Commit c50fe17

Browse files
committed
add more app commands
1 parent c78a5cc commit c50fe17

File tree

11 files changed

+353
-37
lines changed

11 files changed

+353
-37
lines changed

README.md

+44
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,50 @@ Type the command `jason`:
6969

7070
the `<TAB>` key is your new 'mouse' to explore the system.
7171

72+
### Create Applications
73+
74+
New Jason applications can be created with:
75+
76+
```
77+
jason app create app1 --console
78+
cd app1
79+
jason app1.mas2j
80+
```
81+
82+
The first command creates a Jason application identified by `app1` with two agents and a shared environment. The third command executes the application. The output:
83+
84+
```
85+
[bob] hello world.
86+
[alice] hello world.
87+
```
88+
89+
You can add more agents in the project with:
90+
91+
```
92+
jason app add-agent karlos
93+
```
94+
To run agent karlos, there are two options: stop MAS and run it again; or add karlos in the running MAS:
95+
96+
```
97+
jason agent start --source="src/agt/karlos.asl" --mas-name="app1" jomi
98+
```
99+
100+
You can add Gradle scripts for the application with:
101+
102+
```
103+
jason app add-gradle
104+
```
105+
106+
More details of the commands with:
107+
108+
```
109+
jason app
110+
```
111+
112+
113+
Then use your preferred IDE to edit the sources of the application in the `src` folder.
114+
115+
72116
### Scripts
73117

74118
Create a script file, for instance, a file called `hello.jcli` with content:

build.gradle

+13-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ defaultTasks 'build'
66

77
apply plugin: 'java'
88

9-
version '1.0.0-SNAPSHOT'
9+
version '3.2-SNAPSHOT' // use the jason version
1010
group 'org.jason'
1111

1212
java {
@@ -42,6 +42,7 @@ dependencies {
4242
implementation 'org.jline:jline:3.22.0'
4343
implementation 'org.fusesource.jansi:jansi:2.4.0'
4444

45+
implementation 'org.gradle:gradle-tooling-api:7.6'
4546
//testImplementation group: 'junit', name: 'junit', version: '4.12'
4647
}
4748

@@ -65,6 +66,10 @@ task run (type: JavaExec, dependsOn: 'build') {
6566
classpath sourceSets.main.runtimeClasspath
6667
}
6768

69+
clean {
70+
delete 'bin'
71+
delete 'build'
72+
}
6873

6974
task uberJar(type: Jar, dependsOn: 'classes') {
7075
description 'creates a single runnable jar file with all dependencies'
@@ -80,6 +85,13 @@ task uberJar(type: Jar, dependsOn: 'classes') {
8085
destinationDirectory = file('build/bin')
8186
from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
8287
with jar
88+
89+
doLast {
90+
copy {
91+
from 'src/main/resources/scripts'
92+
into 'build/bin'
93+
}
94+
}
8395
}
8496

8597
task createBin (dependsOn: 'uberJar') {

docs/commands.md

+11
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,15 @@ jason agent start bob { +!g <- .print(ok). }
8484

8585
Type `jason agent stop --help` for details of `stop` command ...
8686

87+
## Application level
8788

89+
```
90+
Usage: jason app (create | compile | add-agent | add-gradle )
91+
commands to handle applications
92+
93+
Commands:
94+
create creates the files of a new application
95+
compile compiles the java classes of an application
96+
add-agent adds a new agent into the application
97+
add-gradle adds a Gradle script for the application
98+
```

src/main/java/jason/cli/JasonCommands.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void setReader(LineReader reader) {
5656

5757
class VersionProvider implements IVersionProvider {
5858
public String[] getVersion() {
59-
return new String[] { "Jason " + Config.get().getJasonVersion() };
59+
return new String[] { "Jason CLI " + Config.get().getJasonVersion() };
6060
}
6161
}
6262

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package jason.cli.app;
2+
3+
import picocli.CommandLine;
4+
import picocli.CommandLine.Command;
5+
6+
import java.io.*;
7+
8+
9+
@Command(
10+
name = "add-agent",
11+
description = "adds a new agent into the application"
12+
)
13+
public class AddAgent extends Common implements Runnable {
14+
15+
@CommandLine.Parameters(paramLabel = "<agent name>", defaultValue = "",
16+
arity = "1")
17+
String agName;
18+
19+
20+
@CommandLine.ParentCommand
21+
protected Application parent;
22+
23+
@Override
24+
public void run() {
25+
if (agName.isEmpty()) {
26+
parent.parent.errorMsg("the name of the agent should be informed, e.g., 'app add-agent karlos'.");
27+
return;
28+
}
29+
30+
try {
31+
var file = getProjectFile("");
32+
if (file == null) {
33+
parent.parent.errorMsg("can not find a .mas2j file in the current directory!");
34+
return;
35+
}
36+
37+
var agFile = new File("src/agt/"+agName+".asl");
38+
if (agFile.exists()) {
39+
parent.parent.errorMsg("a file '"+agFile+"' exists already, chose another name for the agent.");
40+
return;
41+
}
42+
43+
Create.copyFile("", "agent", agName, agFile, true);
44+
addAgInProject(file, agName);
45+
parent.parent.println("agent "+agName+" ("+agFile+") was included in "+file);
46+
} catch(Exception e) {
47+
parent.parent.errorMsg("error adding agent:\n" + e);
48+
}
49+
}
50+
51+
private void addAgInProject(File file, String agName) {
52+
// read and change
53+
var text = new StringBuilder();
54+
try (var in = new BufferedReader(new FileReader(file) )) {
55+
String l = in.readLine();
56+
while (l != null) {
57+
if (l.contains("agents:"))
58+
l = l.replace("agents: ", "agents: "+agName+";\n ");
59+
text.append(l+"\n");
60+
l = in.readLine();
61+
}
62+
} catch (IOException e) {
63+
e.printStackTrace();
64+
}
65+
66+
// write
67+
try (var out = new BufferedWriter(new FileWriter(file))) {
68+
out.append(text.toString());
69+
} catch (IOException e) {
70+
e.printStackTrace();
71+
}
72+
}
73+
}
74+

src/main/java/jason/cli/app/Application.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package jason.cli.app;
22

33
import jason.cli.JasonCommands;
4-
import jason.cli.agent.*;
54
import picocli.CommandLine;
65
import picocli.CommandLine.Command;
76

87
@Command(
98
name = "app",
10-
description = "commands to handle aplications",
11-
subcommands = { Compile.class },
12-
synopsisSubcommandLabel = "(create | compile | add-agent | set-env | add-gradle )"
9+
description = "commands to handle applications",
10+
subcommands = { Create.class, Compile.class, AddAgent.class, Gradle.class },
11+
synopsisSubcommandLabel = "(create | compile | add-agent | add-gradle )"
1312
)
1413
public class Application {
1514

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package jason.cli.app;
2+
3+
import jason.util.CreateNewProject;
4+
5+
import java.io.File;
6+
7+
8+
public class Common {
9+
protected File getProjectFile(String masName) {
10+
if (!masName.endsWith(".mas2j"))
11+
masName += ".temp.mas2j";
12+
13+
var f = new File(masName);
14+
if (f.exists())
15+
return f;
16+
17+
// find a .mas2j file in current directory
18+
for (var nf: new File(".").listFiles()) {
19+
if (nf.getName().endsWith(".mas2j"))
20+
return nf;
21+
}
22+
23+
return null;
24+
}
25+
protected File createTempProjectFile(String masName) {
26+
var f = new File( ".temp.mas2j");
27+
CreateNewProject.copyFile("temp", "project", f, true);
28+
return f;
29+
}
30+
}
31+

src/main/java/jason/cli/app/Compile.java

+5-29
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
package jason.cli.app;
22

3-
import jason.asSemantics.Intention;
4-
import jason.cli.agent.Agent;
5-
import jason.cli.mas.RunningMASs;
63
import jason.mas2j.MAS2JProject;
74
import jason.mas2j.parser.mas2j;
85
import jason.util.Config;
9-
import jason.util.CreateNewProject;
106
import picocli.CommandLine;
117
import picocli.CommandLine.Command;
128

139
import java.io.File;
1410
import java.io.FileReader;
15-
import java.rmi.RemoteException;
1611

1712

1813
@Command(
1914
name = "compile",
2015
description = "compiles the java classes of an application"
2116
)
22-
public class Compile implements Runnable {
17+
public class Compile extends Common implements Runnable {
2318

2419
@CommandLine.Parameters(paramLabel = "<MAS name>", defaultValue = "",
2520
arity = "0..1")
@@ -59,31 +54,12 @@ public void run() {
5954
launcher.writeScripts(false, false);
6055
launcher.setTask("compile");
6156
launcher.run();
62-
} catch(Exception e) {
63-
System.err.println("parsing errors found... \n" + e);
64-
}
65-
}
6657

67-
private File getProjectFile(String masName) {
68-
var f = new File(masName+".mas2j");
69-
if (f.exists())
70-
return f;
71-
72-
// find a .mas2j file in current directory
73-
for (var nf: new File(".").listFiles()) {
74-
if (nf.getName().endsWith(".mas2j"))
75-
return nf;
58+
if (file.getName().equals(".temp.mas2j"))
59+
file.delete();
60+
} catch(Exception e) {
61+
parent.parent.errorMsg("error compiling:\n" + e);
7662
}
77-
78-
return null;
79-
}
80-
81-
private File createTempProjectFile(String masName) {
82-
var f = new File( ".temp.mas2j");
83-
CreateNewProject.copyFile("temp", "project", f, true);
84-
return f;
8563
}
86-
87-
8864
}
8965

0 commit comments

Comments
 (0)