Added functionality to store new states in database, and pull new states from database for frontier.

This commit is contained in:
Noah L. Schrick 2021-11-03 16:50:10 -05:00
parent 9f2759661e
commit 45672eac67
19 changed files with 683 additions and 50 deletions

View File

@ -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
####################

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Generated by graphviz version 2.49.0 (0)
<!-- Generated by graphviz version 2.49.3 (0)
-->
<!-- Title: G Pages: 1 -->
<svg width="4591pt" height="653pt"

Before

Width:  |  Height:  |  Size: 239 KiB

After

Width:  |  Height:  |  Size: 239 KiB

Binary file not shown.

View File

@ -1,2 +1,3 @@
cmake -DCMAKE_BUILD_TYPE=Debug ../ -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
make ag_gen
make decode

BIN
build/decode Executable file

Binary file not shown.

385
build/file.txt Normal file
View File

@ -0,0 +1,385 @@
0: quality:,6=6 : 20
topology:car1->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

View File

@ -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

View File

@ -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

View File

@ -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:
.

View File

@ -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);

View File

@ -9,7 +9,7 @@
#include <boost/functional/hash.hpp>
#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.
*/

View File

@ -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;
};

View File

@ -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
*/

View File

@ -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<Quality> q);

View File

@ -7,6 +7,8 @@
#include <getopt.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
@ -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<std::string> &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,7 +494,8 @@ int main(int argc, char *argv[]) {
boost::property_tree::ptree pt;
boost::property_tree::ini_parser::read_ini("config.ini", pt);
/*
if (use_postgres)
{
std::string dbName = pt.get<std::string>("database.name");
std::string host = pt.get<std::string>("database.host");
std::string port = pt.get<std::string>("database.port");
@ -495,7 +513,16 @@ int main(int argc, char *argv[]) {
printf("Finished db connection\n");
printf("---------->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: "<<abc<<"\n";
//std::cout<<"Fact: "<<abc<<"\n";
//std::cout<<"Hash number: "<<_instance.facts[abc]<<"\n";
//std::cout<<"Operator: " <<abc[3]<<"\n";
//std::cout<<"Operator: " <<fetch_all_qualities(_instance.facts).get_op();
@ -572,7 +604,15 @@ int main(int argc, char *argv[]) {
auto ex = fetch_all_exploits(); //make a copy of initial exploits
}
else {
std::cout << parsednm << std::endl;
std::cout << "PRINTING PARSEDNM" << std::endl;
const char delim = '),';
std::vector<std::string> out;
tokenize(parsednm, delim, out);
for (auto &s: out)
{
std::cout << s <<std::endl;
}
std::cout << "Not yet implemented." << std::endl;
return(1);
}

BIN
src/tools/decode Executable file

Binary file not shown.

View File

@ -69,16 +69,17 @@ std::string find_one_impl(std::vector<std::string> &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<std::string> &str_vector, int index) {
void find_all(std::vector<std::string> &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<std::string>(config_section + ".host");
std::string port = pt.get<std::string>(config_section + ".port");
std::string dbName = pt.get<std::string>(config_section + ".db");
std::string username = pt.get<std::string>(config_section + ".username");
std::string password = pt.get<std::string>(config_section + ".password");
std::string dbName = pt.get<std::string>("database.name");
std::string host = pt.get<std::string>("database.host");
std::string port = pt.get<std::string>("database.port");
std::string username = pt.get<std::string>("database.username");
std::string password = pt.get<std::string>("database.password");
std::cout<<dbName<<std::endl;
std::cout<<host<<std::endl;
std::cout<<port<<std::endl;
std::cout<<username<<std::endl;
std::cout<<password<<std::endl;
std::cout << dbName << "\n";
init_db("postgresql://" + username + "@" + host + ":" + port + "/" + dbName);
init_db("dbname="+dbName+" user="+username+" host="+host+" port="+port+" password="+password);
std::cout << "Finished db connection." << std::endl;
// Keyvalue kv = fetch_facts();
// std::vector<std::string> str_vector = kv.get_str_vector();
std::vector<std::string> 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<int>(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;
}

View File

@ -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 <fstream>
@ -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<fis/4+((k==3)?(fis%4):0); j++){
//for (int j = 0, sql_index = 0; j < factbase_items.size(); ++j) {
auto fbi = factbase_items[j+k*(fis/4)];
@ -642,6 +645,15 @@ void save_ag_to_db(AGGenInstance &instance, bool save_keyvalue){
auto quals = std::get<0>(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<Quality> qualities;
std::vector<Topology> 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<Row> 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<Row> 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;
}

View File

@ -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<int>, std::vector<std::array<int, 4>>>;
@ -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