diff --git a/CMakeLists.txt b/CMakeLists.txt index fdc9be0..92c77e8 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,6 +50,7 @@ find_package(Boost REQUIRED) find_package(OpenSSL) find_package(Doxygen) + pkg_check_modules(CMOCKA cmocka) pkg_check_modules(CPPREDIS cpp_redis) @@ -105,6 +106,15 @@ ADD_FLEX_BISON_DEPENDENCY(xp_scanner xp_parser) #add_executable(xp_test ${FLEX_xp_scanner_OUTPUTS} ${BISON_xp_parser_OUTPUTS} ${utils_src}) #target_include_directories(xp_test PRIVATE ${CMAKE_CURRENT_BINARY_DIR} "${CMAKE_SOURCE_DIR}/src/compiler/xp-parser") +#################### +### LIBPQXX +#################### +#find_path(libpqxx_INCLUDE_DIR /pqxx/pqxx) +#include(SelectLibraryConfigurations) +#find_library(libpqxx_LIBRARY_RELEASE NAMES libpqxx) +#find_library(libpqxx_LIBRARY_DEBUG NAMES libpqxxd) +#select_library_configurations(libpqxx) + #################### ### Main application #################### diff --git a/build/ag.svg b/build/ag.svg index 55151b3..d54a7d0 100644 --- a/build/ag.svg +++ b/build/ag.svg @@ -1,7 +1,7 @@ - car2,road= : 5348290845540352 + +1: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +2: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +3: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +4: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +5: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +6: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +7: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +8: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +9: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +10: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +11: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +12: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +13: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +14: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +15: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +16: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +17: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +18: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +19: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +20: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +21: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +22: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +23: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +24: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +25: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +26: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +27: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +28: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +29: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +30: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +31: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +32: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +33: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +34: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +35: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +36: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +37: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +38: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +39: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +40: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +41: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +42: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +43: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +44: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +45: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +46: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +47: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +48: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +49: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +50: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +51: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +52: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +53: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +54: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +55: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +56: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +57: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +58: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +59: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +60: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +61: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +62: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +63: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +64: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +65: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +66: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +67: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +68: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +69: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +70: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +71: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +72: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +73: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +74: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +75: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +76: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +77: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +78: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +79: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +80: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +81: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +82: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +83: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +84: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +85: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +86: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +87: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +88: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +89: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +90: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +91: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +92: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +93: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +94: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +95: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +96: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +97: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +98: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +99: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +100: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +101: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +102: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +103: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +104: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +105: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +106: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +107: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +108: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +109: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +110: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +111: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +112: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +113: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +114: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +115: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +116: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +117: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +118: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +119: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +120: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +121: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +122: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +123: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +124: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +125: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +126: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + +127: quality:,6=6 : 20 + topology:car1->car2,road= : 5348290845540352 + + diff --git a/build/run.sh b/build/run.sh index ea928ee..7eb472a 100755 --- a/build/run.sh +++ b/build/run.sh @@ -3,7 +3,7 @@ sudo -u postgres -i sh -c "createdb ag_gen ag_gen" psql ag_gen < schema.sql #./ag_gen -n ../car_examples/generic_timeline_maintenance.nm -x ../car_examples/tmp.xp -g DOTFILE.dot -t 1 -q 1 #./ag_gen -n ../car_examples/generic_maintenance.nm -x ../car_examples/generic_maintenance.xp -g DOTFILE.dot -t 1 -q 1 -./ag_gen -n ../fpga_examples/Network_5/car_network5.nm -x ../fpga_examples/Network_5/car_network5.xp -g DOTFILE.dot -t 1 -q 1 +./ag_gen -n ../fpga_examples/Network_2/car_network2.nm -x ../fpga_examples/Network_2/car_network2.xp -g DOTFILE.dot -t 1 -q 1 -p #./ag_gen -n ../car_examples/generic_timeline_maintenance.nm -x ../car_examples/sync_timeline_maintenance.xp -g DOTFILE.dot -t 1 -q 1 # python3 ../tools/color_code.py ../build/states.txt DOTFILE.dot new.dot diff --git a/build/schema.sql b/build/schema.sql index 477f300..b2d2c5e 100644 --- a/build/schema.sql +++ b/build/schema.sql @@ -341,6 +341,16 @@ CREATE TABLE public.quality ( ALTER TABLE public.quality OWNER TO noah; +CREATE TABLE public.unex_state_q ( + fid integer NOT NULL, + asset_id integer NOT NULL, + property text NOT NULL, + op text, + value text +); + +ALTER TABLE public.unex_state_q OWNER TO noah; + -- -- Name: topology; Type: TABLE; Schema: public; Owner: noah -- @@ -354,6 +364,18 @@ CREATE TABLE public.topology ( value text ); +CREATE TABLE public.unex_state_t ( + fid integer NOT NULL, + asset_from_id integer NOT NULL, + asset_to_id integer NOT NULL, + direction text, + property text NOT NULL, + op text, + value text +); + +ALTER TABLE public.unex_state_t OWNER TO noah; + ALTER TABLE public.topology OWNER TO noah; @@ -636,6 +658,7 @@ GRANT ALL ON TABLE public.keyvalue TO ag_gen; GRANT ALL ON TABLE public.quality TO ag_gen; +GRANT ALL ON TABLE public.unex_state_q TO ag_gen; -- -- Name: TABLE topology; Type: ACL; Schema: public; Owner: noah @@ -643,6 +666,8 @@ GRANT ALL ON TABLE public.quality TO ag_gen; GRANT ALL ON TABLE public.topology TO ag_gen; +GRANT ALL ON TABLE public.unex_state_t TO ag_gen; + -- -- PostgreSQL database dump complete diff --git a/fpga_examples/Network_2/car_network2.nm b/fpga_examples/Network_2/car_network2.nm index 10f0066..721ffa1 100644 --- a/fpga_examples/Network_2/car_network2.nm +++ b/fpga_examples/Network_2/car_network2.nm @@ -2,7 +2,7 @@ network model = assets: # Cars car1; - #car2; + car2; facts: quality:car1,brake_months=6; @@ -16,17 +16,17 @@ network model = quality:car1,vacuum_vio=false; quality:car1,compliance_vio=false; - #quality:car2,brake_months=6; - #quality:car2,exhaust_months=12; - #quality:car2,ac_odometer=120000; - #quality:car2,vacuum_odometer=120000; - #quality:car2,engine=diesel; - #quality:car2,brake_vio=false; - #quality:car2,exhaust_vio=false; - #quality:car2,ac_vio=false; - #quality:car2,vacuum_vio=false; - #quality:car2,compliance_vio=false; + quality:car2,brake_months=6; + quality:car2,exhaust_months=12; + quality:car2,ac_odometer=120000; + quality:car2,vacuum_odometer=120000; + quality:car2,engine=diesel; + quality:car2,brake_vio=false; + quality:car2,exhaust_vio=false; + quality:car2,ac_vio=false; + quality:car2,vacuum_vio=false; + quality:car2,compliance_vio=false; - topology:car1<->car1,road; + topology:car1<->car2,road; tags: . diff --git a/src/ag_gen/ag_gen.cpp b/src/ag_gen/ag_gen.cpp index a1aa7a0..149dbe1 100755 --- a/src/ag_gen/ag_gen.cpp +++ b/src/ag_gen/ag_gen.cpp @@ -544,7 +544,12 @@ AGGenInstance &AGGen::generate(bool batch_process, int batch_size, int numThrd, instance.factbase_items.push_back(new_items); instance.factbases.push_back(new_state.get_factbase()); hash_map.insert(std::make_pair(new_state.get_hash(instance.facts), new_state.get_id())); + + //localFrontier.emplace_front(new_state); + save_unexplored_to_db(new_state); + NetworkState new_state = fetch_unexplored(instance.facts); localFrontier.emplace_front(new_state); + Edge ed(current_state.get_id(), new_state.get_id(), exploit, assetGroup); ed.set_id(); instance.edges.push_back(ed); diff --git a/src/ag_gen/factbase.cpp b/src/ag_gen/factbase.cpp index 6580442..65a5d01 100755 --- a/src/ag_gen/factbase.cpp +++ b/src/ag_gen/factbase.cpp @@ -9,7 +9,7 @@ #include #include "ag_gen.h" -#include "quality.h" //NOAH +#include "quality.h" using namespace std; @@ -33,6 +33,10 @@ void Factbase::set_id() { id = current_id++; } +void Factbase::force_set_id(int i) { + id = i; +} + /** * @return The current Factbase ID. */ diff --git a/src/ag_gen/factbase.h b/src/ag_gen/factbase.h index 495a18e..04eff84 100755 --- a/src/ag_gen/factbase.h +++ b/src/ag_gen/factbase.h @@ -44,6 +44,7 @@ class Factbase { void print() const; void set_id(); + void force_set_id(int i); int get_id() const; size_t hash(Keyvalue &factlist) const; }; diff --git a/src/ag_gen/network_state.cpp b/src/ag_gen/network_state.cpp index 09f3e27..eed68fd 100755 --- a/src/ag_gen/network_state.cpp +++ b/src/ag_gen/network_state.cpp @@ -32,6 +32,8 @@ NetworkState::NetworkState(const NetworkState &ns) = default; */ void NetworkState::set_id() { factbase.set_id(); } +void NetworkState::force_set_id(int i) { factbase.force_set_id(i); } + /** * @return The ID of the NetworkState */ diff --git a/src/ag_gen/network_state.h b/src/ag_gen/network_state.h index 15eb55c..952a9ae 100755 --- a/src/ag_gen/network_state.h +++ b/src/ag_gen/network_state.h @@ -33,6 +33,7 @@ class NetworkState { size_t get_hash(Keyvalue &factlist) const; void set_id(); + void force_set_id(int i); int get_id(); void add_qualities(std::vector q); diff --git a/src/main.cpp b/src/main.cpp index 223b02b..6b81feb 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,6 +7,8 @@ #include #include #include +#include +#include #include #include #include @@ -360,6 +362,21 @@ std::string parse_xp(std::string &filename) { return output; } +/** + * @brief Parse a string based on delim. + */ +void tokenize(std::string const &str, const char delim, std::vector &out) +{ + //Construct stream from given string + std::stringstream ss(str); + + std::string s; + while (std::getline(ss, s, delim)) + { + out.push_back(s); + } +} + /** * @brief Prints command line usage information. */ @@ -420,7 +437,7 @@ int main(int argc, char *argv[]) { bool use_postgres = false; int opt; - while ((opt = getopt(argc, argv, "rb:g:dhc:n:x:t:q:")) != -1) { + while ((opt = getopt(argc, argv, "rb:g:dhc:n:x:t:q:p")) != -1) { switch (opt) { case 'g': should_graph = true; @@ -477,25 +494,35 @@ int main(int argc, char *argv[]) { boost::property_tree::ptree pt; boost::property_tree::ini_parser::read_ini("config.ini", pt); -/* - std::string dbName = pt.get("database.name"); - std::string host = pt.get("database.host"); - std::string port = pt.get("database.port"); - std::string username = pt.get("database.username"); - std::string password = pt.get("database.password"); - std::cout<Initialization and Connecting to DB took %lf ms.<----------- \n",tdiff2); - printf("\n"); -*/ + if (use_postgres) + { + std::string dbName = pt.get("database.name"); + std::string host = pt.get("database.host"); + std::string port = pt.get("database.port"); + std::string username = pt.get("database.username"); + std::string password = pt.get("database.password"); + std::cout<Initialization and Connecting to DB took %lf ms.<----------- \n",tdiff2); + printf("\n"); + } + else + { + + std::cout << "No" << std::endl; + //pqxx::connection c; + //pqxx::work w(c); + } + + //int a2=1; //a2=a2+1; //while(a2); @@ -548,6 +575,11 @@ int main(int argc, char *argv[]) { printf("\n"); } + else + { + std::cout << "Importing into Mock" << std::endl; + } + //------------------------------------------ //program block 3: //------------------------------------------ @@ -560,7 +592,7 @@ int main(int argc, char *argv[]) { //The above function call returned an Keyvalue object and assigned the object to facts. The object mainly contains hash table and string vector based on all initial property and value. //the following 4 lines can be used to check the content of the facts. It is based on the initial property and value from table quality, postcondition and topology. //for(std::string abc: _instance.facts.get_str_vector()){ - // std::cout<<"Fact: "< out; + tokenize(parsednm, delim, out); + + for (auto &s: out) + { + std::cout << s < &str_vector, int index) { output << "\tquality:"; Asset asset = assets[qual.dec.asset_id]; + //Asset asset = assets[0]; + std::cout<< asset.get_name()<< std::endl; output << asset.get_name() << ","; - std::string attr = str_vector[qual.dec.attr]; std::string val = str_vector[qual.dec.val]; - output << attr << "=" << val; output << " : " << qual.enc << "\n"; } + while(!pqt.empty()) { EncodedTopology topo = pqt.top(); pqt.pop(); @@ -109,7 +110,6 @@ void find_one(std::vector &str_vector, int index) { void find_all(std::vector &str_vector) { int max = get_max_factbase_id(); - std::ofstream output("file.txt"); for(auto i=0; i<=max; ++i) { @@ -156,26 +156,31 @@ int main(int argc, char *argv[]) { boost::property_tree::ptree pt; boost::property_tree::ini_parser::read_ini("config.ini", pt); - std::string host = pt.get(config_section + ".host"); - std::string port = pt.get(config_section + ".port"); - std::string dbName = pt.get(config_section + ".db"); - std::string username = pt.get(config_section + ".username"); - std::string password = pt.get(config_section + ".password"); + std::string dbName = pt.get("database.name"); + std::string host = pt.get("database.host"); + std::string port = pt.get("database.port"); + std::string username = pt.get("database.username"); + std::string password = pt.get("database.password"); + std::cout< str_vector = kv.get_str_vector(); std::vector str_vector = fetch_keyvalues(); + std::cout<<"Fetched keyvalues." << std::endl; if (opt_index.empty()) { find_all(str_vector); } else { try { auto index = boost::lexical_cast(opt_index); - find_one(str_vector, index); } catch (boost::bad_lexical_cast &e) { print_usage(); return 2; @@ -183,5 +188,6 @@ int main(int argc, char *argv[]) { } // All done :) + std::cout << "Finished decoding. See output file.txt for results." << std::endl; return 0; } diff --git a/src/util/db_functions.cpp b/src/util/db_functions.cpp index 681a891..4b1d1c1 100755 --- a/src/util/db_functions.cpp +++ b/src/util/db_functions.cpp @@ -21,6 +21,7 @@ #include "../ag_gen/topology.h" #include "../ag_gen/asset.h" #include "../ag_gen/factbase.h" +#include "../ag_gen/network_state.h" #include @@ -628,12 +629,14 @@ void save_ag_to_db(AGGenInstance &instance, bool save_keyvalue){ //default(none) shared(factbase_items,fis,db,db2,db3,db4,std::cout) schedule(static,1) #pragma omp parallel for num_threads(1)\ default(none) shared(factbase_items,fis,db,std::cout) - for(int k=0;k<4;k++){ + //for(int k=0;k<4;k++){ + for(int k=0; k<1; k++) { std::string item_sql_query = "INSERT INTO factbase_item VALUES "; std::string quality_sql_query = ""; std::string topology_sql_query = ""; int sql_index=0; + /* for (int j = 0; j(items); auto topo = std::get<1>(items); for (auto qi : quals) { + */ + + for (int j = 0, sql_index = 0; j < factbase_items.size(); ++j) { + auto fbi = factbase_items[j]; + int id = std::get<1>(fbi); + auto items = std::get<0>(fbi); + auto quals = std::get<0>(items); + auto topo = std::get<1>(items); + for (auto qi : quals) { if (sql_index == 0) quality_sql_query += "(" + std::to_string(id) + "," + std::to_string(qi.get_encoding()) + @@ -668,7 +680,7 @@ void save_ag_to_db(AGGenInstance &instance, bool save_keyvalue){ } } - item_sql_query += quality_sql_query + topology_sql_query + ";"; + item_sql_query += quality_sql_query + topology_sql_query + "ON CONFLICT DO NOTHING;"; int thrd_num=omp_get_thread_num(); if(thrd_num==0){ db.exec("BEGIN;"); @@ -850,3 +862,139 @@ void save_ag_to_db(AGGenInstance &instance, bool save_keyvalue){ gettimeofday(&t2,NULL); printf("The saving of keyvalue took %lf ms\n",(t2.tv_sec-t1.tv_sec)*1000.0+(t2.tv_usec-t1.tv_usec)/1000.0); } + +void save_unexplored_to_db(NetworkState netstate) { + int fid = netstate.get_id(); + const Factbase fb = netstate.get_factbase(); + + auto facts_tuple = fb.get_facts_tuple(); + auto qualities = std::get<0>(facts_tuple); + auto topologies = std::get<1>(facts_tuple); + + std::string qual_sql_query = "INSERT INTO unex_state_q VALUES "; + std::string top_sql_query = "INSERT INTO unex_state_t VALUES "; + + int i = 0; + + for(auto qual : qualities) { + auto asset_id = qual.get_asset_id(); + auto name = qual.get_name(); + auto op = qual.get_op(); + auto val = qual.get_value(); + + if (i==0) { + qual_sql_query += "(" + std::to_string(fid) + "," \ + + std::to_string(asset_id) + "," + "'" + name + "'" + ","\ + + "'" + op +"'" + "," + "'" + val + "'" + ")"; + + } + else { + qual_sql_query += ",(" + std::to_string(fid) + "," \ + + std::to_string(asset_id) + "," + "'" + name + "'" + ","\ + + "'" + op +"'" + "," + "'" + val + "'" + ")"; + } + + ++i; + } + + i = 0; + + for (auto ti : topologies) { + auto from_id = ti.get_from_asset_id(); + auto to_id = ti.get_to_asset_id(); + auto dir = ti.get_dir(); + auto prop = ti.get_property(); + auto op = ti.get_op(); + auto val = ti.get_value(); + + if (i == 0) { + top_sql_query += "(" + std::to_string(fid) + "," \ + + std::to_string(from_id) + ","\ + + std::to_string(to_id) + ","\ + + std::to_string(dir) + ","\ + + "'" + prop + "'" + ","\ + + "'" + op + "'" + ","\ + + "'" + val + "'" + ")"; + } + else { + top_sql_query += ",(" + std::to_string(fid) + "," \ + + std::to_string(from_id) + ","\ + + std::to_string(to_id) + ","\ + + std::to_string(dir) + ","\ + + "'" + prop + "'" + ","\ + + "'" + op + "'" + ","\ + + "'" + val + "'" + ")"; + } + + ++i; + } + + qual_sql_query += " ON CONFLICT DO NOTHING;"; + top_sql_query += " ON CONFLICT DO NOTHING;"; + + struct timeval t1,t2; + gettimeofday(&t1,NULL); + + db.execAsync(qual_sql_query); + db.execAsync(top_sql_query); + + gettimeofday(&t2,NULL); + //printf("The saving of unexplored states took %lf ms\n",(t2.tv_sec-t1.tv_sec)*1000.0+(t2.tv_usec-t1.tv_usec)/1000.0); +} + +//Fetch Qualities, Topologies, reform into a factbase, then return the factbase +NetworkState fetch_unexplored(Keyvalue &facts){ + std::vector qualities; + std::vector topologies; + int id; + + //Pulls the fid from the first row in the unex_state Table, deletes all rows with this fid, then returns all deleted rows + std::string qdel_str = "DELETE FROM unex_state_q WHERE fid=(SELECT fid FROM unex_state_q LIMIT 1) RETURNING *;"; + std::string tdel_str = "DELETE FROM unex_state_t WHERE fid=(SELECT fid FROM unex_state_t LIMIT 1) RETURNING *;"; + + std::vector qrows = db.exec(qdel_str); + + for (auto &row : qrows) { + int fid = std::stoi(row[0]); + int asset_id = std::stoi(row[1]); + std::string property = row[2]; + std::string op = row[3]; + std::string value = row[4]; + + Quality qual(asset_id, property, op, value, facts); //associate each record from unex_state table with the facts(the hash_table and fact string vector). + qualities.push_back(qual); + id = fid; + } + + std::vector trows = db.exec(tdel_str); + for (auto &row : trows) { + //int fid = std::stoi(row[0]); + int from_asset = std::stoi(row[1]); + int to_asset = std::stoi(row[2]); + std::string dir_str = row[3]; + + DIRECTION_T dir; //DIRECTION_T is a user defined enum type (in topology.h), it has three different values: FORWARD_T 0, BACKWARD_T 1, and BIDIRECTION_T 2 + if(dir_str == "->" || dir_str == "0") { + dir = FORWARD_T; + } else if (dir_str == "<-" || dir_str == "1") { + dir = BACKWARD_T; + } else if (dir_str == "<->" || dir_str == "2") { + dir = BIDIRECTION_T; + } else { + std::cerr << "Unknown direction '" << dir_str << "'" << std::endl; + exit(1); + } + + std::string property = row[4]; + std::string op = row[5]; + std::string value = row[6]; + + Topology t(from_asset, to_asset, dir, property, op, value, facts); //associate each record from topology table with the facts + topologies.push_back(t); + } + + NetworkState fetched_state(qualities, topologies); + fetched_state.force_set_id(id); + + return fetched_state; +} \ No newline at end of file diff --git a/src/util/db_functions.h b/src/util/db_functions.h index d9cfd0d..aebd00b 100755 --- a/src/util/db_functions.h +++ b/src/util/db_functions.h @@ -10,6 +10,7 @@ #include "keyvalue.h" #include "db.h" + #include "../ag_gen/ag_gen.h" #include "../ag_gen/exploit.h" #include "../ag_gen/edge.h" @@ -17,6 +18,7 @@ #include "../ag_gen/topology.h" #include "../ag_gen/asset.h" #include "../ag_gen/factbase.h" +#include "../ag_gen/network_state.h" using GraphInfo = std::pair, std::vector>>; @@ -75,4 +77,7 @@ void save_ag_to_db(AGGenInstance &instance, bool save_keyvalue); // void test_create(); +void save_unexplored_to_db(NetworkState netstate); +NetworkState fetch_unexplored(Keyvalue &facts); + #endif