Skip to content

Commit 7f9537e

Browse files
committed
Ported and added "tutorial_1.py" from Python Examples
Added MalmoJava DLL and Jar Added compiled Tutorials jar - read README.MD for information on how to use Updated README.md to provide a little bit of information on how to use
1 parent 1a13234 commit 7f9537e

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed

MalmoJava.dll

1.6 MB
Binary file not shown.

MalmoJavaJar.jar

26.4 KB
Binary file not shown.

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
11
## Malmo Java Examples
22
Aims to port all Python examples to Java to provide more examples to people who wish to use Malmo with Java.
3+
4+
#How to use
5+
In order to execute one of these tutorials you should run the following in the command line:
6+
````
7+
java -cp MalmoJavaJar.jar;Tutorials.jar -DJava.library.path=. TUTORIAL
8+
````
9+
Example: If I wanted to run "Tutorial1" I'd enter the following command in the command line:
10+
````
11+
java -cp MalmoJavaJar.jar;Tutorials.jar -DJava.library.path=. Tutorial1
12+
````
13+
The tutorial name has to correspond the class name.

Tutorials.jar

2.08 KB
Binary file not shown.

src/Tutorial1.java

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import com.microsoft.msr.malmo.*;
2+
3+
import static java.lang.System.loadLibrary;
4+
5+
/**
6+
* A Java translation of the Python "tutorial_1" example for the Malmo platform made by Microsoft
7+
*/
8+
public class Tutorial1 {
9+
10+
static {
11+
loadLibrary("MalmoJava"); // Load the Malmo JNI
12+
}
13+
14+
public static void main(String[] argv) {
15+
AgentHost agentHost = new AgentHost();
16+
try {
17+
StringVector args = new StringVector();
18+
args.add("Tutorial1");
19+
for(String arg: argv)
20+
args.add(arg);
21+
} catch(Exception e) {
22+
System.out.format("ERROR: %s", e.getMessage());
23+
System.out.println(agentHost.getUsage());
24+
}
25+
if(agentHost.receivedArgument("help")) {
26+
System.out.println(agentHost.getUsage());
27+
System.exit(0);
28+
}
29+
30+
MissionSpec missionSpec = new MissionSpec(); // Initialize our mission
31+
MissionRecordSpec missionRecordSpec = new MissionRecordSpec("./saved_data.tgz"); // Initialize where we want to store our mission logs
32+
33+
// Try to start the mission
34+
int maxRetries = 3; // How many times will we try to start our mission
35+
for(int i=0; i<maxRetries; i++) {
36+
try {
37+
// Start the mission - hope it doesn't throw an exception
38+
agentHost.startMission(missionSpec, missionRecordSpec);
39+
break;
40+
} catch(Exception e) {
41+
// Failed to start the mission since an exception was thrown.
42+
if(i == maxRetries - 1) { // Failed to start the mission as many times as we defined in "maxRetries"
43+
System.out.format("ERROR: %s", e.getMessage());
44+
System.out.format("Couldn't start the mission after trying %d times. Exiting mission.", maxRetries);
45+
System.exit(1);
46+
}
47+
48+
System.out.format("Failed to start the mission after %d/%d attempts. Retrying in 2 seconds.", i+1, maxRetries);
49+
try {
50+
Thread.sleep(2000);
51+
} catch(InterruptedException e2) {
52+
System.out.format("ERROR: %s", e2.getMessage());
53+
System.out.println("Couldn't sleep the thread for two seconds");
54+
}
55+
}
56+
}
57+
58+
// Wait for the mission to start
59+
WorldState worldState = agentHost.getWorldState();
60+
while(!worldState.getHasMissionBegun()) { // Wait for the mission to begin
61+
System.out.print(".");
62+
try {
63+
Thread.sleep(100);
64+
} catch(InterruptedException e) {
65+
System.out.format("ERROR: %s", e.getMessage());
66+
System.out.println("Couldn't sleep the thread for 0.1 second while waiting for the mission to begin");
67+
}
68+
69+
worldState = agentHost.getWorldState();
70+
for(int i=0; i<worldState.getErrors().size(); i++) {
71+
System.out.format("ERROR: "+worldState.getErrors().get(i));
72+
}
73+
}
74+
75+
// Log that the mission started
76+
System.out.println("\nMission started");
77+
78+
// Do something. In our case, we do nothing for 10 seconds.
79+
while(worldState.getIsMissionRunning()) {
80+
System.out.print(".");
81+
82+
try {
83+
Thread.sleep(100);
84+
} catch(InterruptedException e) {
85+
System.out.format("ERROR: %s", e.getMessage());
86+
System.out.println("Couldn't sleep the thread for 0.1 second while the mission's executing");
87+
}
88+
89+
worldState = agentHost.getWorldState();
90+
for(int i=0; i<worldState.getErrors().size(); i++) {
91+
System.out.format("ERROR: "+worldState.getErrors().get(i));
92+
}
93+
}
94+
95+
System.out.println("\n\nMission has ended.");
96+
}
97+
98+
}

0 commit comments

Comments
 (0)