Skip to content

Commit

Permalink
Merge branch 'noxsdgen' into cmakecsharp
Browse files Browse the repository at this point in the history
  • Loading branch information
AndKram committed Apr 25, 2018
2 parents 0a45d73 + d018901 commit 611ea6d
Show file tree
Hide file tree
Showing 27 changed files with 1,167 additions and 1,016 deletions.
5 changes: 1 addition & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ else()
endif()

# suggested install paths - see readme.md
if( WIN32)
set( XSD_ROOT "C:/Program Files (x86)/CodeSynthesis XSD 4.0" )
else()
if( NOT WIN32)
set( ALE_ROOT_DIR ~/ALE )
endif()

Expand Down Expand Up @@ -146,7 +144,6 @@ if( INCLUDE_PYTHON )
endif()

find_package( SWIG REQUIRED )
find_package( Xsd REQUIRED )

if( INCLUDE_ALE )
find_package(ALE QUIET)
Expand Down
2 changes: 0 additions & 2 deletions Malmo/samples/Cpp_examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ set( CPP_SAMPLE_SOURCES

set( MALMO_INCLUDE_FOLDERS
${CMAKE_CURRENT_SOURCE_DIR}/../../src
${CMAKE_CURRENT_BINARY_DIR}/../../src
${Boost_INCLUDE_DIR}
${XSD_INCLUDE_DIRS}
)
include_directories( ${MALMO_INCLUDE_FOLDERS} )

Expand Down
5 changes: 1 addition & 4 deletions Malmo/samples/Cpp_examples/CMakeLists.txt.in
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake )
SET(Boost_ADDITIONAL_VERSIONS "1.65" "1.65.0")
SET(Boost_ADDITIONAL_VERSIONS "1.66" "1.66.0")
SET(Boost_ADDITIONAL_VERSIONS "1.67" "1.67.0")

if( MSVC )
set( Boost_USE_STATIC_LIBS ON )
find_package( Boost COMPONENTS chrono date_time filesystem iostreams program_options regex system thread zlib REQUIRED )
Expand All @@ -39,8 +40,6 @@ else()
find_package( Boost COMPONENTS chrono date_time filesystem iostreams program_options regex system thread REQUIRED )
endif()

find_package( Xsd REQUIRED )

find_library( Malmo_LIBRARIES Malmo ${CMAKE_SOURCE_DIR}/lib )

# ------------------- Settings --------------------------------------
Expand All @@ -61,11 +60,9 @@ add_executable( run_mission ${SOURCES} )
include_directories(
${CMAKE_SOURCE_DIR}/include
${Boost_INCLUDE_DIR}
${XSD_INCLUDE_DIRS}
)

target_link_libraries( run_mission
${Malmo_LIBRARIES}
${Boost_LIBRARIES}
${XSD_LIBRARIES}
)
12 changes: 6 additions & 6 deletions Malmo/samples/Python_examples/two_diggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
</ServerInitialConditions>
<ServerHandlers>
<FlatWorldGenerator forceReset="true" generatorString="3;7,220*1,5*3,2;3;,biome_1" seed=""/>
<ServerQuitFromTimeUp description="" timeLimitMs="10000"/>
<ServerQuitFromTimeUp description="" timeLimitMs="20000"/>
<ServerQuitWhenAnyAgentFinishes description=""/>
</ServerHandlers>
</ServerSection>
Expand Down Expand Up @@ -187,8 +187,8 @@ def safeWaitForStart(agent_hosts):
reward2 = sum(reward.getValue() for reward in world_state2.rewards)
print('Agent 1 received',reward1)
print('Agent 2 received',reward2)
assert reward1 == expected_reward1, 'ERROR: agent 1 should have received a reward of '+str(expected_reward1)+', not '+str(reward1)
assert reward2 == expected_reward2, 'ERROR: agent 2 should have received a reward of '+str(expected_reward2)+', not '+str(reward2)
assert reward1 == expected_reward1, ('ERROR: agent 1 should have received a reward of '+str(expected_reward1)+', not '+str(reward1))
assert reward2 == expected_reward2, ('ERROR: agent 2 should have received a reward of '+str(expected_reward2)+', not '+str(reward2))

# -- set up another mission, with continuous actions --

Expand All @@ -209,7 +209,7 @@ def safeWaitForStart(agent_hosts):
<DrawBlock type="sand" x="-2" y="227" z="3" />
<DrawBlock type="sponge" x="1" y="227" z="3" />
</DrawingDecorator>
<ServerQuitFromTimeUp description="" timeLimitMs="10000"/>
<ServerQuitFromTimeUp description="" timeLimitMs="20000"/>
<ServerQuitWhenAnyAgentFinishes description=""/>
</ServerHandlers>
</ServerSection>
Expand Down Expand Up @@ -288,5 +288,5 @@ def safeWaitForStart(agent_hosts):
reward2 = sum(reward.getValue() for reward in world_state2.rewards)
print('Agent 1 received',reward1)
print('Agent 2 received',reward2)
assert reward1 == expected_reward1, 'ERROR: agent 1 should have received a reward of '+str(expected_reward1)+', not '+str(reward1)
assert reward2 == expected_reward2, 'ERROR: agent 2 should have received a reward of '+str(expected_reward2)+', not '+str(reward2)
assert reward1 == expected_reward1, ('ERROR2: agent 1 should have received a reward of '+str(expected_reward1)+', not '+str(reward1))
assert reward2 == expected_reward2, ('ERROR2: agent 2 should have received a reward of '+str(expected_reward2)+', not '+str(reward2))
106 changes: 30 additions & 76 deletions Malmo/src/AgentHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
#include "AgentHost.h"

// Local:
#include "FindSchemaFile.h"
#include "TCPClient.h"
#include "WorldState.h"
#include "Init.h"
#include "Logger.h"
#include "MissionEndedXML.h"
#include "FindSchemaFile.h"

// Boost:
#include <boost/bind.hpp>
Expand All @@ -39,9 +39,6 @@
#include <boost/algorithm/string.hpp>
#include <boost/regex.hpp>

// Schemas:
#include <MissionEnded.h>

// STL:
#include <exception>
#include <sstream>
Expand All @@ -63,8 +60,6 @@ namespace malmo
, observations_policy(LATEST_OBSERVATION_ONLY)
, current_role( 0 )
{
initialiser::initXSD();

this->addOptionalFlag("help,h", "show description of allowed options");
this->addOptionalFlag("test", "run this as an integration test");

Expand Down Expand Up @@ -302,17 +297,6 @@ namespace malmo
const bool prettyPrint = false;
const std::string generated_xml = this->current_mission_init->getAsXML( prettyPrint );

try {
const bool validate = true;
MissionInitSpec test_output(generated_xml,validate);
}
catch (xml_schema::exception& e)
{
std::ostringstream oss;
oss << "Internal error: the XML we generate does not validate: " << e.what() << "\n" << e << "\n" << generated_xml << std::endl;
throw std::runtime_error(oss.str());
}

return generated_xml;
}

Expand Down Expand Up @@ -626,56 +610,31 @@ namespace malmo
std::string root_node_name(pt.front().first.data());

if( !this->world_state.is_mission_running && root_node_name == "MissionInit" ) {
try {
const bool validate = true;
this->current_mission_init = boost::make_shared<MissionInitSpec>(xml.text,validate);
this->world_state.is_mission_running = true;
this->world_state.has_mission_begun = true;
}
catch (const xml_schema::exception& e) {
std::ostringstream oss;
oss << "Error parsing MissionInit message XML: " << e.what() << " : " << e << ":" << xml.text.substr(0, 20) << "...";
TimestampedString error_message(xml);
error_message.text = oss.str();
this->world_state.errors.push_back( boost::make_shared<TimestampedString>( error_message ) );
return;
}
const bool validate = true;
this->current_mission_init = boost::make_shared<MissionInitSpec>(xml.text,validate);
this->world_state.is_mission_running = true;
this->world_state.has_mission_begun = true;
this->openCommandsConnection();
}
else if( root_node_name == "MissionEnded" ) {

try {
const bool validate = true;

xml_schema::properties props;
props.schema_location(xml_namespace, FindSchemaFile("MissionEnded.xsd"));

xml_schema::flags flags = xml_schema::flags::dont_initialize;
if( !validate )
flags = flags | xml_schema::flags::dont_validate;

std::istringstream iss(xml.text);
std::unique_ptr<malmo::schemas::MissionEnded> mission_ended = malmo::schemas::MissionEnded_(iss, flags, props);

switch( mission_ended->Status() ) {
case malmo::schemas::MissionResult::ENDED:
case malmo::schemas::MissionResult::PLAYER_DIED:
break;
default:
{
std::ostringstream oss;
oss << "Mission ended abnormally: " << mission_ended->HumanReadableStatus();
TimestampedString error_message(xml);
error_message.text = oss.str();
this->world_state.errors.push_back( boost::make_shared<TimestampedString>( error_message ) );
}
break;
MissionEndedXML mission_ended(xml.text);
const std::string status = mission_ended.getStatus();

if (status != MissionEndedXML::ENDED && status != MissionEndedXML::PLAYER_DIED) {
std::ostringstream oss;
oss << "Mission ended abnormally: " << mission_ended.getHumanReadableStatus();
TimestampedString error_message(xml);
error_message.text = oss.str();
this->world_state.errors.push_back(boost::make_shared<TimestampedString>(error_message));
}

if (this->world_state.is_mission_running) {
schemas::MissionEnded::Reward_optional final_reward_optional = mission_ended->Reward();
if( final_reward_optional.present() ) {
TimestampedReward final_reward(xml.timestamp,final_reward_optional.get());
const RewardXML& reward = mission_ended.getReward();

if (reward.size() != 0) {
TimestampedReward final_reward(xml.timestamp, reward);
this->processReceivedReward(final_reward);
this->rewards_server->recordMessage(TimestampedString(xml.timestamp, final_reward.getAsSimpleString()));
}
Expand All @@ -686,33 +645,28 @@ namespace malmo

// Add some diagnostics of our own before this gets to the agent:
if (this->video_server || this->luminance_server || this->depth_server || this->colourmap_server) {
for (auto &vd : mission_ended->MissionDiagnostics().VideoData()) {
for (auto &vd : mission_ended.videoDataAttributes()) {
boost::shared_ptr<VideoServer> vs = 0;
if (vd.frameType() == "VIDEO")
if (vd.frame_type == "VIDEO")
vs = this->video_server;
else if (vd.frameType() == "DEPTH_MAP")
else if (vd.frame_type == "DEPTH_MAP")
vs = this->depth_server;
else if (vd.frameType() == "LUMINANCE")
else if (vd.frame_type == "LUMINANCE")
vs = this->luminance_server;
else if (vd.frameType() == "COLOUR_MAP")
else if (vd.frame_type == "COLOUR_MAP")
vs = this->colourmap_server;
if (vs) {
vd.framesReceived(vs->receivedFrames());
vd.framesWritten(vs->writtenFrames());
vd.frames_received = vs->receivedFrames();
vd.frames_written = vs->writtenFrames();
}
}
std::ostringstream oss;
xml_schema::namespace_infomap map;
map[""].name = xml_namespace;
map[""].schema = "MissionEnded.xsd";
xml_schema::flags flags = xml_schema::flags::dont_initialize;
malmo::schemas::MissionEnded_(oss, *mission_ended, map, "UTF-8", flags);
xml.text = oss.str();

xml.text = mission_ended.toXml();
}
}
catch (const xml_schema::exception& e) {
catch (const std::exception& e) {
std::ostringstream oss;
oss << "Error parsing MissionEnded message XML: " << e.what() << " : " << e << ":" << xml.text.substr(0, 20) << "...";
oss << "Error processing MissionEnded message XML: " << e.what() << " : " << ":" << xml.text.substr(0, 20) << "...";
TimestampedString error_message(xml);
error_message.text = oss.str();
this->world_state.errors.push_back( boost::make_shared<TimestampedString>( error_message ) );
Expand Down
40 changes: 10 additions & 30 deletions Malmo/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ set( SOURCES
ClientConnection.cpp
ClientInfo.cpp
ClientPool.cpp
FindSchemaFile.cpp
Init.cpp
Logger.cpp
FindSchemaFile.cpp
RewardXML.cpp
MissionInitXML.cpp
MissionEndedXML.cpp
MissionInitSpec.cpp
MissionRecord.cpp
MissionRecordSpec.cpp
Expand All @@ -42,11 +44,6 @@ set( SOURCES
BmpFrameWriter.cpp
VideoServer.cpp
WorldState.cpp
${CMAKE_CURRENT_BINARY_DIR}/Mission.cpp
${CMAKE_CURRENT_BINARY_DIR}/MissionEnded.cpp
${CMAKE_CURRENT_BINARY_DIR}/MissionHandlers.cpp
${CMAKE_CURRENT_BINARY_DIR}/MissionInit.cpp
${CMAKE_CURRENT_BINARY_DIR}/Types.cpp
)

set( HEADERS
Expand All @@ -55,9 +52,12 @@ set( HEADERS
ClientConnection.h
ClientInfo.h
ClientPool.h
FindSchemaFile.h
Init.h
Logger.h
FindSchemaFile.h
XMLParseException.h
RewardXML.h
MissionInitXML.h
MissionEndedXML.h
MissionInitSpec.h
MissionRecord.h
MissionRecordSpec.h
Expand All @@ -76,11 +76,6 @@ set( HEADERS
BmpFrameWriter.h
VideoServer.h
WorldState.h
${CMAKE_CURRENT_BINARY_DIR}/Mission.h
${CMAKE_CURRENT_BINARY_DIR}/MissionEnded.h
${CMAKE_CURRENT_BINARY_DIR}/MissionHandlers.h
${CMAKE_CURRENT_BINARY_DIR}/MissionInit.h
${CMAKE_CURRENT_BINARY_DIR}/Types.h
)

if( MSVC )
Expand All @@ -98,23 +93,10 @@ endif()

add_definitions( -DMALMO_VERSION=${MALMO_VERSION} )

add_custom_command( # Run CodeSynthesis on the .xsd files to generate C++ sources we can use
OUTPUT Mission.h Mission.cpp MissionHandlers.h MissionHandlers.cpp MissionEnded.h MissionEnded.cpp MissionInit.h MissionInit.cpp Types.h Types.cpp
COMMAND ${XSD_EXECUTABLE} cxx-tree --generate-polymorphic --namespace-map http://ProjectMalmo.microsoft.com=malmo::schemas
--root-element Mission --root-element MissionInit --root-element MissionEnded --root-element Reward
--generate-serialization --hxx-suffix .h --cxx-suffix .cpp #--std c++11
${CMAKE_SOURCE_DIR}/Schemas/Mission.xsd ${CMAKE_SOURCE_DIR}/Schemas/MissionEnded.xsd ${CMAKE_SOURCE_DIR}/Schemas/MissionHandlers.xsd
${CMAKE_SOURCE_DIR}/Schemas/MissionInit.xsd ${CMAKE_SOURCE_DIR}/Schemas/Types.xsd
DEPENDS ${CMAKE_SOURCE_DIR}/Schemas/Mission.xsd ${CMAKE_SOURCE_DIR}/Schemas/MissionEnded.xsd ${CMAKE_SOURCE_DIR}/Schemas/MissionHandlers.xsd
${CMAKE_SOURCE_DIR}/Schemas/MissionInit.xsd ${CMAKE_SOURCE_DIR}/Schemas/Types.xsd
COMMENT "Generating C++ from XSD files..."
)

set( MALMO_INCLUDE_FOLDERS
${CMAKE_CURRENT_SOURCE_DIR}
${Boost_INCLUDE_DIR}
${XSD_INCLUDE_DIRS}
${CMAKE_CURRENT_BINARY_DIR}
#${CMAKE_CURRENT_BINARY_DIR}
${ZLIB_INCLUDE_DIRS}
)
if ( INCLUDE_ALE )
Expand All @@ -127,7 +109,6 @@ add_library( Malmo STATIC ${SOURCES} ${HEADERS} )

set( MALMO_LINK_LIBRARIES
${Boost_LIBRARIES}
${XSD_LIBRARIES}
${ZLIB_LIBRARIES}
)
if( INCLUDE_ALE )
Expand All @@ -149,7 +130,6 @@ target_link_libraries( Malmo ${MALMO_LINK_LIBRARIES} )

install( FILES ${HEADERS} DESTINATION Cpp_Examples/include )
install( TARGETS Malmo DESTINATION Cpp_Examples/lib )
install( FILES ${CMAKE_SOURCE_DIR}/cmake/FindXsd.cmake DESTINATION Cpp_Examples/cmake )

# -------------------- Walk the subdirectories --------------------
if( INCLUDE_CSHARP )
Expand Down
4 changes: 0 additions & 4 deletions Malmo/src/CSharpWrapper/MalmoNETNative.i
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,6 @@ public:
%exception MissionSpec(const std::string& rawMissionXML,bool validate) %{
try {
$action
} catch (const xml_schema::exception& e) {
std::ostringstream oss;
oss << "Caught xml_schema::exception: " << e.what() << "\n" << e;
SWIG_CSharpSetPendingException(SWIG_CSharpApplicationException, oss.str().c_str());
} catch (const std::runtime_error& e) {
std::ostringstream oss;
oss << "Caught std::runtime_error: " << e.what();
Expand Down
5 changes: 0 additions & 5 deletions Malmo/src/JavaWrapper/MalmoJava.i
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,6 @@ public:
%javaexception("java.lang.Exception") MissionSpec(const std::string& rawMissionXML,bool validate) %{
try {
$action
} catch (const xml_schema::exception& e) {
std::ostringstream oss;
oss << "Caught xml_schema::exception: " << e.what() << "\n" << e;
jclass clazz = jenv->FindClass("java/lang/Exception");
jenv->ThrowNew(clazz, oss.str().c_str());
} catch (const std::runtime_error& e) {
std::ostringstream oss;
oss << "Caught std::runtime_error: " << e.what();
Expand Down
Loading

0 comments on commit 611ea6d

Please sign in to comment.