main.cpp
Go to the documentation of this file.
00001 /*
00002  * main.cpp
00003  *
00004  *  Created on: Oct 17, 2013
00005  *      Author: blackpc
00006  */
00007 
00008 
00009 #include <python2.7/Python.h>
00010 #include <scriptable_monitor/scriptable_monitor.h>
00011 #include <iostream>
00012 
00013 #include <scriptable_monitor/ScriptHost.h>
00014 #include <scriptable_monitor/SomeInternalFunctionExample.h>
00015 
00016 
00017 using namespace std;
00018 
00019 void rosTopicListenerTest() {
00020         cout << "Starting..." << endl;
00021 
00022         RosTopicListener::start();
00023         sleep(3);
00024 
00025         RosTopicListener::addTopic("/scan/ranges[0:1]");
00026         RosTopicListener::addTopic("/scan/ranges[1:2]");
00027         RosTopicListener::addTopic("/scan/ranges[2:3]");
00028 
00029 
00030         sleep(7);
00031         RosTopicListener::stop();
00032 
00033         cout << "Values:" << endl;
00034         cout << RosTopicListener::getTopicValue("/scan/ranges[0:1]") << endl;
00035         cout << RosTopicListener::getTopicValue("/scan/ranges[1:2]") << endl;
00036         cout << RosTopicListener::getTopicValue("/scan/ranges[2:3]") << endl;
00037 
00038         cout << "Done" << endl;
00039 }
00040 
00041 void internalFunctionsTest() {
00042         vector<string> names = InternalFunctionsManager::getFunctionNames();
00043 
00044         cout << "Available functions:" << endl;
00045 
00046         for (size_t i = 0; i < names.size(); i++)
00047                 cout << " - " << names[i] << endl;
00048 }
00049 
00050 void scriptingTest() {
00051         string s =
00052                         "#! name script_name\n"
00053                         "#! type predicate\n"
00054                         "#! author Bla bla bablalovich\n"
00055                         "core1 = {/diagntostic/cpu/core1}\n"
00056                         "core2 = {/diagntostic/cpu/core2}\n"
00057                         "core3 = {/diagntostic/cpu/core3}\n"
00058                         "core4 = {/diagntostic/cpu/core4}\n"
00059                         "cpu_th = {/config/cpu/cpu_threshold}\n"
00060                         "core_th = {/config/cpu/core_threshold}\n"
00061                         "average = average(core1, core2, core3, core4)\n"
00062                         "average < cpu_th\n"
00063                         "core1 < core_th\n"
00064                         "core2 < core_th\n"
00065                         "core3 < core_th\n"
00066                         "core4 < core_th\n";
00067 
00068         string errorScript =
00069                         "#! name script_name2\n"
00070                         "#! type predicate\n"
00071                         "core1x x core2x\n";
00072 
00073         set<string> internalFunctions;
00074         internalFunctions.insert("average");
00075 
00076         map<string, string> topics;
00077         topics["/diagntostic/cpu/core1"] = "1";
00078         topics["/diagntostic/cpu/core2"] = "1";
00079         topics["/diagntostic/cpu/core3"] = "1";
00080         topics["/diagntostic/cpu/core4"] = "1";
00081         topics["/diagntostic/cpu/cpu"] = "1";
00082         topics["/config/cpu/cpu_threshold"] = "20";
00083         topics["/config/cpu/core_threshold"] = "20";
00084 
00085         PredicatesScript ps(s, internalFunctions);
00086         PredicatesScript psError(errorScript, internalFunctions);
00087 
00088         PythonScript py = ps.getPythonScript();
00089         PythonScript pyError = psError.getPythonScript();
00090 
00091         ScriptExecuter pe;
00092         CompilationResult result;
00093 
00094         result = pe.compile(pyError);
00095         cout << "Compilation result = " << (result.success ? "SUCCESS" : "FAILED") << endl;
00096 
00097         result = pe.compile(py);
00098         cout << "Compilation result = " << (result.success ? "SUCCESS" : "FAILED") << endl;
00099 
00100 //      pe.simulate(py);
00101 //      pe.execute(py, topics);
00102 }
00103 
00104 void scriptHostTest() {
00105 
00106         ScriptHost host;
00107 
00108         string s1 =     "#! type predicate\n"
00109                                 "#! name predicate_script1\n"
00110                                 "#! interval 2\n"
00111                                 "core1 = {/scan/ranges[0:1]}\n"
00112                                 "core2 = {/scan/ranges[1:2]}\n"
00113                                 "core3 = {/scan/ranges[2:3]}\n"
00114                                 "core4 = {/scan/ranges[3:4]}\n"
00115                                 "cpu_th = {/scan/ranges[4:5]}\n"
00116                                 "core_th = {/scan/ranges[5:6]}\n"
00117                                 "average = SomeFunction(core1, core2, core3, core4)\n"
00118                                 "average < cpu_th\n"
00119                                 "core1 < core_th\n"
00120                                 "core2 < core_th\n"
00121                                 "core3 > core_th\n"
00122                                 "core4 < core_th\n";
00123 
00124         string s2 =     "#! type predicate\n"
00125                                 "#! name predicate_script2\n"
00126                                 "#! interval 3\n"
00127                                 "core1 = {/scan/ranges[0:1]}\n"
00128                                 "core2 = {/scan/ranges[1:2]}\n"
00129                                 "core3 = {/scan/ranges[2:3]}\n"
00130                                 "core4 = {/scan/ranges[3:4]}\n"
00131                                 "cpu_th = {/scan/ranges[4:5]}\n"
00132                                 "core_th = {/scan/ranges[5:6]}\n"
00133                                 "average = SomeFunction(core1, core2, core3, core4)\n"
00134                                 "average < cpu_th\n"
00135                                 "core1 < core_th\n"
00136                                 "core2 < core_th\n"
00137                                 "core3 > core_th\n"
00138                                 "core4 < core_th\n";
00139 
00140 
00141         host.start();
00142         cout << "Running scripts" << endl;
00143 
00144         sleep(1);
00145 
00146         host.addScript(s1);
00147         host.addScript(s2);
00148 
00149         sleep(5);
00150 
00151         host.stop();
00152 
00153         cout << "Done" << endl;
00154 }
00155 
00156 void yamlTest() {
00157          stringstream yaml_stream;
00158          yaml_stream << "[1.5462267]";
00159          YAML::Parser parser(yaml_stream);
00160          YAML::Node document;
00161          parser.GetNextDocument(document);
00162 
00163          cout << document.size() << endl;
00164 }
00165 
00166 int main(int argc, char **argv) {
00167 //      RosTopicListener::start();
00168 
00169 //      yamlTest();
00170 //      return 0;
00171 
00172         scriptHostTest();
00173         return 0;
00174 
00175 //      internalFunctionsTest();
00176 //      return 0;
00177 //
00178 //      rosTopicListenerTest();
00179 //      return 0;
00180 
00181 //      scriptingTest();
00182 //      return 0;
00183 
00184 }


scriptable_monitor
Author(s):
autogenerated on Wed Aug 26 2015 16:21:30