aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2007-03-22 01:16:48 +0000
committerDavid Robillard <d@drobilla.net>2007-03-22 01:16:48 +0000
commitb80654a5193536050c8f8e494ba5fd402b7a18aa (patch)
treee2f74b7341590587d32b01cc0595efd15e3ae59c /src
parent9de593d8780ec6fec43703df7a532e65baa44510 (diff)
downloadmachina-b80654a5193536050c8f8e494ba5fd402b7a18aa.tar.gz
machina-b80654a5193536050c8f8e494ba5fd402b7a18aa.tar.bz2
machina-b80654a5193536050c8f8e494ba5fd402b7a18aa.zip
Cleaned up loading code.
Preliminary action loading code (queried from RDF but not actually created yet). Fixed URI/filename handling so command line 'machina' accepts either. git-svn-id: http://svn.drobilla.net/lad/machina@367 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'src')
-rw-r--r--src/engine/Engine.cpp9
-rw-r--r--src/engine/JackDriver.cpp3
-rw-r--r--src/engine/Loader.cpp190
-rw-r--r--src/engine/Makefile.am2
-rw-r--r--src/engine/machina/JackDriver.hpp1
-rw-r--r--src/gui/MachinaCanvas.cpp8
-rw-r--r--src/gui/MachinaGUI.cpp48
-rw-r--r--src/gui/MachinaGUI.hpp2
-rw-r--r--src/gui/main.cpp5
-rw-r--r--src/machina.cpp4
10 files changed, 138 insertions, 134 deletions
diff --git a/src/engine/Engine.cpp b/src/engine/Engine.cpp
index 5991276..fede33f 100644
--- a/src/engine/Engine.cpp
+++ b/src/engine/Engine.cpp
@@ -30,10 +30,11 @@ namespace Machina {
SharedPtr<Machine>
Engine::load_machine(const Glib::ustring& uri)
{
- Loader l; // FIXME: namespaces?
- SharedPtr<Machine> m = l.load(uri);
- m->activate();
- _driver->set_machine(m);
+ SharedPtr<Machine> m = Loader().load(uri);
+ if (m) {
+ m->activate();
+ _driver->set_machine(m);
+ }
return m;
}
diff --git a/src/engine/JackDriver.cpp b/src/engine/JackDriver.cpp
index ee5a0d6..4f08543 100644
--- a/src/engine/JackDriver.cpp
+++ b/src/engine/JackDriver.cpp
@@ -26,7 +26,8 @@ namespace Machina {
JackDriver::JackDriver(SharedPtr<Machine> machine)
- : _machine_changed(0)
+ : Driver(machine)
+ , _machine_changed(0)
, _machine(machine)
, _input_port(NULL)
, _output_port(NULL)
diff --git a/src/engine/Loader.cpp b/src/engine/Loader.cpp
index edc4ac4..5c5ccdb 100644
--- a/src/engine/Loader.cpp
+++ b/src/engine/Loader.cpp
@@ -44,9 +44,9 @@ Loader::Loader(SharedPtr<Namespaces> namespaces)
}
-/** Load (create) all objects from an RDF into the engine.
+/** Load (create) all objects from RDF into the engine.
*
- * @param uri URI of machine (e.g. resolvable URI to an RDF document).
+ * @param uri URI of machine (resolvable URI to an RDF document).
* @return Loaded Machine.
*/
SharedPtr<Machine>
@@ -57,135 +57,114 @@ Loader::load(const Glib::ustring& uri)
rasqal_init();
- raptor_uri* base_uri = raptor_new_uri((const unsigned char*)"file:.");
-
- raptor_uri* document_raptor_uri = raptor_new_uri_relative_to_base(
- base_uri, (const unsigned char*)uri.c_str());
-
- //raptor_uri* document_raptor_uri = raptor_new_uri_for_retrieval(rel_uri);
- //raptor_free_uri(rel_uri);
-
- if (!document_raptor_uri)
- return machine; // NULL
-
- machine = SharedPtr<Machine>(new Machine());
-
- //Glib::ustring document_uri = (const char*)document_uri_str;
- const Glib::ustring& document_uri = uri;
+ Glib::ustring document_uri = uri;
+
+ // If "URI" doesn't contain a colon, try to resolve as a filename
+ if (uri.find(":") == Glib::ustring::npos) {
+ raptor_uri* base_uri = raptor_new_uri((const unsigned char*)"file:.");
+ raptor_uri* document_raptor_uri = raptor_new_uri_relative_to_base(
+ base_uri, (const unsigned char*)uri.c_str());
+ if (document_raptor_uri) {
+ document_uri = (char*)raptor_uri_as_string(document_raptor_uri);
+ raptor_free_uri(document_raptor_uri);
+ raptor_free_uri(base_uri);
+ } else {
+ raptor_free_uri(base_uri);
+ return machine; // NULL
+ }
+ }
- //string machine_uri = "<> ";
- //string machine_uri = string("<") + document_uri + "> ";
- string machine_uri = "?foo ";
+ const Glib::ustring machine_uri = "<>";
cout << "[Loader] Loading " << machine_uri << " from " << document_uri << endl;
+ machine = SharedPtr<Machine>(new Machine());
+
typedef std::map<string, SharedPtr<Node> > Created;
Created created;
/* Get initial nodes */
-
+
Raul::RDFQuery query = Raul::RDFQuery(*_namespaces, Glib::ustring(
- "SELECT DISTINCT ?initialNode ?duration FROM <")
- + document_uri + "> WHERE {\n" +
- machine_uri + " :initialNode ?initialNode .\n"
- "?initialNode :duration ?duration .\n"
- "}\n");
+ "SELECT DISTINCT ?initialNode ?duration FROM <")
+ + document_uri + "> WHERE {\n" +
+ machine_uri + " :initialNode ?initialNode .\n"
+ "?initialNode :duration ?duration .\n"
+ "}\n");
RDFQuery::Results results = query.run(document_uri);
for (RDFQuery::Results::iterator i = results.begin(); i != results.end(); ++i) {
- const Glib::ustring& node_uri = (*i)["initialNode"];
- const Glib::ustring& duration = (*i)["duration"];
-
- raptor_uri* node_raptor_uri
- = raptor_new_uri((const unsigned char*)node_uri.c_str());
-
- char* node_name = (char*)
- raptor_uri_to_relative_uri_string(document_raptor_uri, node_raptor_uri);
-
- cout << "Initial: " << node_name << " - " << duration << endl;
-
- cerr << "FIXME: load\n";
-/*
- SharedPtr<Node> node = SharedPtr<Node>(_node_factory->create_node(
- node_name,
- strtol(midi_note.c_str(), NULL, 10),
- strtol(duration.c_str(), NULL, 10)));
-
- node->set_initial(true);
- //machine->add_node(string(node_name).substr(1), node); // (chop leading "#")
- machine->add_node(node);
-
- created.insert(std::make_pair(node_uri.collate_key(), node));
- */
-
+ const Glib::ustring& node_id = (*i)["initialNode"];
+ const Glib::ustring& duration = (*i)["duration"];
+
+ cout << "Initial: " << node_id << " - " << duration << endl;
+
SharedPtr<Node> node(new Node(strtod(duration.c_str(), NULL), true));
machine->add_node(node);
- created.insert(std::make_pair(node_uri.collate_key(), node));
-
- raptor_free_uri(node_raptor_uri);
- free(node_name);
+ created.insert(std::make_pair(node_id.collate_key(), node));
}
-
-
- /* Get remaining nodes */
-
+
+
+ /* Get remaining (non-initial) nodes */
+
query = Raul::RDFQuery(*_namespaces, Glib::ustring(
- "SELECT DISTINCT ?node ?duration FROM <")
- + document_uri + "> WHERE {\n" +
- machine_uri + " :node ?node .\n"
- "?node :duration ?duration .\n"
- "}\n");
+ "SELECT DISTINCT ?node ?duration FROM <")
+ + document_uri + "> WHERE {\n" +
+ machine_uri + " :node ?node .\n"
+ "?node :duration ?duration .\n"
+ "}\n");
results = query.run(document_uri);
for (RDFQuery::Results::iterator i = results.begin(); i != results.end(); ++i) {
- const Glib::ustring& node_uri = (*i)["node"];
- const Glib::ustring& duration = (*i)["duration"];
-
- raptor_uri* node_raptor_uri
- = raptor_new_uri((const unsigned char*)node_uri.c_str());
-
- char* node_name = (char*)
- raptor_uri_to_relative_uri_string(document_raptor_uri, node_raptor_uri);
-
- cout << "Node: " << node_name << " - " << duration << endl;
-
- cerr << "FIXME: load (2)\n";
- /*
- SharedPtr<Node> node = SharedPtr<Node>(_node_factory->create_node(
- node_name,
- strtol(midi_note.c_str(), NULL, 10),
- strtol(duration.c_str(), NULL, 10)));
-
-
- if (created.find(node_uri) == created.end()) {
- //cout << "Node: " << node_name << ": " << midi_note << " - " << duration << endl;
- //machine->add_node(string(node_name).substr(1), node); // (chop leading "#")
- machine->add_node(node);
- created.insert(std::make_pair(node_uri.collate_key(), node));
+ const Glib::ustring& node_id = (*i)["node"];
+ const Glib::ustring& duration = (*i)["duration"];
+
+ cout << "Node: " << node_id << " - " << duration << endl;
+
+ if (created.find(node_id.collate_key()) == created.end()) {
+ SharedPtr<Node> node(new Node(strtod(duration.c_str(), NULL), true));
+ machine->add_node(node);
+ created.insert(std::make_pair(node_id.collate_key(), node));
+ } else {
+ cout << "Already created, skipping." << endl;
}
- */
- SharedPtr<Node> node(new Node(strtod(duration.c_str(), NULL), true));
- machine->add_node(node);
- created.insert(std::make_pair(node_uri.collate_key(), node));
+ }
+
+
+ /* Get actions */
+
+ query = Raul::RDFQuery(*_namespaces, Glib::ustring(
+ "SELECT DISTINCT ?node ?enter_action ?exit_action FROM <")
+ + document_uri + "> WHERE {\n" +
+ "?node :enterAction ?enter_action ;\n"
+ " :exitAction ?exit_action .\n"
+ "}\n");
+
+ results = query.run(document_uri);
+
+ for (RDFQuery::Results::iterator i = results.begin(); i != results.end(); ++i) {
+ const Glib::ustring& node = (*i)["node"];
+ const Glib::ustring& enter_action = (*i)["enter_action"];
+ const Glib::ustring& exit_action = (*i)["exit_action"];
+
+ cout << "Node: " << node << ", Action: " << enter_action << " .. " << exit_action <<endl;
- raptor_free_uri(node_raptor_uri);
- free(node_name);
}
/* Get edges */
query = Raul::RDFQuery(*_namespaces, Glib::ustring(
- "SELECT DISTINCT ?edge ?src ?dst ?prob FROM <")
- + document_uri + "> WHERE {\n" +
- machine_uri + " :edge ?edge .\n"
- "?edge :tail ?src ;\n"
- " :head ?dst ;\n"
- " :probability ?prob .\n }");
-
+ "SELECT DISTINCT ?edge ?src ?dst ?prob FROM <")
+ + document_uri + "> WHERE {\n" +
+ machine_uri + " :edge ?edge .\n"
+ "?edge :tail ?src ;\n"
+ " :head ?dst ;\n"
+ " :probability ?prob .\n }");
+
results = query.run(document_uri);
for (RDFQuery::Results::iterator i = results.begin(); i != results.end(); ++i) {
@@ -199,7 +178,7 @@ Loader::load(const Glib::ustring& uri)
if (src_i != created.end() && dst_i != created.end()) {
const SharedPtr<Node> src = src_i->second;
const SharedPtr<Node> dst = dst_i->second;
-
+
SharedPtr<Edge> edge(new Edge(src, dst));
edge->set_probability(prob);
src->add_outgoing_edge(edge);
@@ -210,12 +189,9 @@ Loader::load(const Glib::ustring& uri)
}
}
-
- //free(document_uri_str);
- raptor_free_uri(document_raptor_uri);
- raptor_free_uri(base_uri);
- machine->reset();
+ if (machine)
+ machine->reset();
return machine;
}
diff --git a/src/engine/Makefile.am b/src/engine/Makefile.am
index eab7970..8d41dd6 100644
--- a/src/engine/Makefile.am
+++ b/src/engine/Makefile.am
@@ -17,5 +17,5 @@ libmachina_la_SOURCES = \
LearnRequest.cpp
if WITH_JACK
- JackDriver.cpp
+libmachina_la_SOURCES += JackDriver.cpp
endif
diff --git a/src/engine/machina/JackDriver.hpp b/src/engine/machina/JackDriver.hpp
index 13005c0..e5d0abb 100644
--- a/src/engine/machina/JackDriver.hpp
+++ b/src/engine/machina/JackDriver.hpp
@@ -44,6 +44,7 @@ class JackDriver : public Raul::JackDriver,
public boost::enable_shared_from_this<JackDriver> {
public:
JackDriver(SharedPtr<Machine> machine = SharedPtr<Machine>());
+ ~JackDriver();
void attach(const std::string& client_name);
void detach();
diff --git a/src/gui/MachinaCanvas.cpp b/src/gui/MachinaCanvas.cpp
index 34db1c1..10334d0 100644
--- a/src/gui/MachinaCanvas.cpp
+++ b/src/gui/MachinaCanvas.cpp
@@ -84,8 +84,10 @@ MachinaCanvas::canvas_event(GdkEvent* event)
{
static int last = 0;
- assert(event);
-
+ SharedPtr<Machina::Machine> machine = _app->machine();
+ if (!machine)
+ return false;
+
if (event->type == GDK_BUTTON_RELEASE
&& event->button.state & GDK_CONTROL_MASK) {
@@ -106,7 +108,7 @@ MachinaCanvas::canvas_event(GdkEvent* event)
view->resize();
view->raise_to_top();
- _app->machine()->add_node(node);
+ machine->add_node(node);
}
}
diff --git a/src/gui/MachinaGUI.cpp b/src/gui/MachinaGUI.cpp
index 83a7504..0ab4356 100644
--- a/src/gui/MachinaGUI.cpp
+++ b/src/gui/MachinaGUI.cpp
@@ -54,7 +54,8 @@ MachinaGUI::MachinaGUI(SharedPtr<Machina::Engine> engine)
fs.open(glade_filename.c_str());
if (fs.fail()) {
- cerr << "Unable to find machina.glade in current directory or " << PKGDATADIR << "." << endl;
+ cerr << "Unable to find machina.glade in current directory or "
+ << PKGDATADIR << "." << endl;
exit(EXIT_FAILURE);
}
fs.close();
@@ -330,10 +331,12 @@ MachinaGUI::menu_file_open()
const int result = dialog.run();
if (result == Gtk::RESPONSE_OK) {
- SharedPtr<Machina::Machine> new_machine = _engine->load_machine(dialog.get_uri());
- _canvas->destroy();
- _canvas->build(new_machine);
- _save_filename = dialog.get_filename();
+ _save_uri = dialog.get_uri();
+ SharedPtr<Machina::Machine> new_machine = _engine->load_machine(_save_uri);
+ if (new_machine) {
+ _canvas->destroy();
+ _canvas->build(new_machine);
+ }
}
}
@@ -341,13 +344,21 @@ MachinaGUI::menu_file_open()
void
MachinaGUI::menu_file_save()
{
- if (_save_filename == "") {
+ if (_save_uri == "" || _save_uri.substr(0, 5) != "file:") {
menu_file_save_as();
} else {
+ char* save_filename = raptor_uri_uri_string_to_filename(
+ (const unsigned char*)_save_uri.c_str());
+ if (!save_filename) {
+ cerr << "ERROR: Unable to create filename from \"" << _save_uri << "\"" << endl;
+ menu_file_save_as();
+ }
Raul::RDFWriter writer;
- writer.start_to_filename(_save_filename);
+ cout << "Writing machine to " << save_filename << endl;
+ writer.start_to_filename(save_filename);
machine()->write_state(writer);
writer.finish();
+ free(save_filename);
}
}
@@ -355,20 +366,24 @@ MachinaGUI::menu_file_save()
void
MachinaGUI::menu_file_save_as()
{
- Gtk::FileChooserDialog dialog(*_main_window, "Save Machine", Gtk::FILE_CHOOSER_ACTION_SAVE);
+ Gtk::FileChooserDialog dialog(*_main_window, "Save Machine",
+ Gtk::FILE_CHOOSER_ACTION_SAVE);
dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
dialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK);
- if (_save_filename.length() > 0)
- dialog.set_filename(_save_filename);
+ if (_save_uri.length() > 0)
+ dialog.set_uri(_save_uri);
const int result = dialog.run();
- assert(result == Gtk::RESPONSE_OK || result == Gtk::RESPONSE_CANCEL || result == Gtk::RESPONSE_NONE);
+ assert(result == Gtk::RESPONSE_OK
+ || result == Gtk::RESPONSE_CANCEL
+ || result == Gtk::RESPONSE_NONE);
if (result == Gtk::RESPONSE_OK) {
string filename = dialog.get_filename();
+
if (filename.length() < 8 || filename.substr(filename.length()-8) != ".machina")
filename += ".machina";
@@ -394,7 +409,7 @@ MachinaGUI::menu_file_save_as()
writer.start_to_filename(filename);
machine()->write_state(writer);
writer.finish();
- _save_filename = filename;
+ _save_uri = dialog.get_uri();
}
}
}
@@ -403,7 +418,8 @@ MachinaGUI::menu_file_save_as()
void
MachinaGUI::menu_import_midi()
{
- Gtk::FileChooserDialog dialog(*_main_window, "Learn from MIDI file", Gtk::FILE_CHOOSER_ACTION_OPEN);
+ Gtk::FileChooserDialog dialog(*_main_window, "Learn from MIDI file",
+ Gtk::FILE_CHOOSER_ACTION_OPEN);
dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
@@ -444,7 +460,8 @@ MachinaGUI::menu_import_midi()
void
MachinaGUI::menu_export_midi()
{
- Gtk::FileChooserDialog dialog(*_main_window, "Export to a MIDI file", Gtk::FILE_CHOOSER_ACTION_SAVE);
+ Gtk::FileChooserDialog dialog(*_main_window, "Export to a MIDI file",
+ Gtk::FILE_CHOOSER_ACTION_SAVE);
dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
dialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK);
@@ -468,7 +485,8 @@ MachinaGUI::menu_export_midi()
void
MachinaGUI::menu_export_graphviz()
{
- Gtk::FileChooserDialog dialog(*_main_window, "Export to a GraphViz DOT file", Gtk::FILE_CHOOSER_ACTION_SAVE);
+ Gtk::FileChooserDialog dialog(*_main_window, "Export to a GraphViz DOT file",
+ Gtk::FILE_CHOOSER_ACTION_SAVE);
dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
dialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK);
diff --git a/src/gui/MachinaGUI.hpp b/src/gui/MachinaGUI.hpp
index 6677d24..3c16f86 100644
--- a/src/gui/MachinaGUI.hpp
+++ b/src/gui/MachinaGUI.hpp
@@ -84,7 +84,7 @@ protected:
bool _refresh;
- string _save_filename;
+ string _save_uri;
boost::shared_ptr<MachinaCanvas> _canvas;
boost::shared_ptr<Machina::Engine> _engine;
diff --git a/src/gui/main.cpp b/src/gui/main.cpp
index 5f208b4..6f1fc4d 100644
--- a/src/gui/main.cpp
+++ b/src/gui/main.cpp
@@ -45,11 +45,14 @@ main(int argc, char** argv)
machine = file_driver->learn(filename);
}
+ if (!machine)
+ machine = SharedPtr<Machine>(new Machine());
+
// Build engine
SharedPtr<Driver> driver;
#ifdef WITH_JACK
driver = SharedPtr<Driver>(new JackDriver(machine));
- driver->attach("machina");
+ ((JackDriver*)driver.get())->attach("machina");
#endif
if (!driver)
driver = SharedPtr<Driver>(new SMFDriver(machine));
diff --git a/src/machina.cpp b/src/machina.cpp
index f04a646..42f45d4 100644
--- a/src/machina.cpp
+++ b/src/machina.cpp
@@ -56,12 +56,14 @@ main(int argc, char** argv)
Engine engine(driver);
- // FIXME: Would be nice if this could take URIs on the cmd line
+ /*// FIXME: Would be nice if this could take URIs on the cmd line
char* uri = (char*)calloc(6 + strlen(argv[1]), sizeof(char));
strcpy(uri, "file:");
strcat(uri, argv[1]);
engine.load_machine(uri);
free(uri);
+*/
+ engine.load_machine(argv[1]);
driver->attach("machina");