Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.89 KB | None | 0 0
  1. extern "C" {
  2. #include "sc_memory_headers.h"
  3. #include "sc_helper.h"
  4. #include "utils.h"
  5. }
  6.  
  7. #include <stdio.h>
  8. #include <iostream>
  9. #include <glib.h>
  10. #include <unistd.h>
  11. #include <assert.h>
  12. #include <vector>
  13. #include <algorithm>
  14.  
  15. using namespace std;
  16.  
  17. sc_memory_context *context;
  18.  
  19. sc_addr graph, rrel_arcs, rrel_nodes, visit, curr_vertex, father, nrel_weight, weight_of_path,
  20.         nrel_srdiam, nrel_idtf, ref, antiref, partref, arcs;
  21.  
  22. int amOfNodes = 0;
  23.  
  24. int amOfPairs = 0;
  25.  
  26. vector <sc_addr> nodes;
  27. vector <sc_addr> allNodes;
  28. vector <sc_addr> allArcs;
  29. vector <sc_addr> checked;
  30. vector <sc_addr> result1;
  31. vector <sc_addr> result2;
  32. vector <int> paths;
  33.  
  34.  
  35.  
  36. sc_bool find_vertex_in_setChecked(sc_addr element, vector <sc_addr> set) // есть ли узел в множестве?
  37. {
  38.     if (false == set.empty()){
  39.         for (int i = 0; i < set.size(); i++){
  40.             if (SC_TRUE == SC_ADDR_IS_EQUAL(set.at(i), element)) return SC_TRUE;
  41.         }
  42.     }
  43.     return SC_FALSE;
  44. }
  45.  
  46.  
  47. int getWeight(sc_addr v1, sc_addr v2){//ok
  48.     int result = 0;
  49.     sc_addr node_weight, weight, arc;
  50.     sc_iterator5 *it1 = sc_iterator5_f_a_f_a_f_new(context,
  51.                                                v1,
  52.                                                sc_type_arc_common,
  53.                                                v2,
  54.                                                sc_type_arc_pos_const_perm, arcs);
  55.     if (SC_TRUE == sc_iterator5_next(it1)){
  56.         arc = sc_iterator5_value(it1, 1);
  57.     }
  58.     sc_iterator5 *it = sc_iterator5_f_a_a_a_f_new(context, arc, sc_type_arc_common, 0, sc_type_arc_pos_const_perm, nrel_weight);
  59.     if (SC_TRUE == sc_iterator5_next(it)){
  60.         node_weight = sc_iterator5_value(it, 2);
  61.     }
  62. //    printEl(context, node_weight);
  63.     sc_iterator5 *itTwo = sc_iterator5_f_a_a_a_f_new(context,node_weight,sc_type_arc_common,
  64.                                                      0,sc_type_arc_pos_const_perm,nrel_idtf);
  65.     if (SC_TRUE == sc_iterator5_next(itTwo)) weight = sc_iterator5_value(itTwo, 2);
  66.     result = getInt(context, weight);
  67.     sc_iterator5_free(it);
  68.     sc_iterator5_free(itTwo);
  69.     return result;
  70. }
  71.  
  72. int getWeight(sc_addr arc){//ok
  73.     int result = 0;
  74.     sc_addr node_weight, weight;
  75.     sc_iterator5 *it = sc_iterator5_f_a_a_a_f_new(context, arc, sc_type_arc_common, 0, sc_type_arc_pos_const_perm, nrel_weight);
  76.     if (SC_TRUE == sc_iterator5_next(it)){
  77.         node_weight = sc_iterator5_value(it, 2);
  78.     }
  79.     sc_iterator5 *itTwo = sc_iterator5_f_a_a_a_f_new(context,node_weight,sc_type_arc_common,
  80.                                                      0,sc_type_arc_pos_const_perm,nrel_idtf);
  81.     if (SC_TRUE == sc_iterator5_next(itTwo)) weight = sc_iterator5_value(itTwo, 2);
  82.     result = getInt(context, weight);
  83.     sc_iterator5_free(it);
  84.     sc_iterator5_free(itTwo);
  85.     return result;
  86. }
  87.  
  88.  
  89. bool isIncidence (sc_addr v1, sc_addr v2){
  90.     if (SC_FALSE == SC_ADDR_IS_EQUAL(v1, v2)){
  91.         sc_iterator3 *it = sc_iterator3_f_a_a_new(context, v1, sc_type_arc_common, 0);
  92.         while (SC_TRUE == sc_iterator3_next(it))
  93.             if (SC_TRUE == SC_ADDR_IS_EQUAL(v2, sc_iterator3_value(it,2))){
  94.                 sc_iterator3_free(it);
  95.                 return SC_TRUE;
  96.             }
  97.         sc_iterator3_free(it);
  98.         return SC_FALSE;
  99.     }else{
  100.         return SC_FALSE;
  101.     }
  102. }
  103.  
  104. void pr (){//ok
  105.     for (int j = 0; j < allNodes.size(); j++)
  106.         for(int i = 0; i < allNodes.size(); i++){
  107.             printEl(context, allNodes.at(j));
  108.             cout << " " ;
  109.             printEl(context, allNodes.at(i));
  110.             cout << getWeight(allNodes.at(i), allNodes.at(j));
  111.             cout << endl;
  112.         }
  113. }
  114.  
  115.  
  116. void findAllNodes (){//ok
  117.     sc_addr setNodes;
  118.     sc_iterator5 *grToNodes = sc_iterator5_f_a_a_a_f_new(context,graph,sc_type_arc_pos_const_perm,
  119.                                                          0,sc_type_arc_pos_const_perm,rrel_nodes);
  120.     if (SC_TRUE == sc_iterator5_next(grToNodes)) {
  121.         setNodes = sc_iterator5_value(grToNodes, 2);
  122.     }
  123.  
  124.     sc_iterator5 *grToArcs = sc_iterator5_f_a_a_a_f_new(context,graph,sc_type_arc_pos_const_perm,
  125.                                                          0,sc_type_arc_pos_const_perm,rrel_arcs);
  126.     if (SC_TRUE == sc_iterator5_next(grToArcs)) {
  127.         arcs = sc_iterator5_value(grToArcs, 2);
  128.     }
  129.  
  130.     sc_iterator3 *nodes = sc_iterator3_f_a_a_new(context, setNodes, sc_type_arc_pos_const_perm, 0);
  131.     while (SC_TRUE == sc_iterator3_next(nodes)) {
  132.         allNodes.push_back(sc_iterator3_value(nodes, 2));
  133.     }
  134.     for(int i = 0; i < allNodes.size(); i++){
  135.         printEl(context, allNodes.at(i));
  136.         cout << endl;
  137.     }
  138.     sc_iterator3_free(nodes);
  139.     sc_iterator5_free(grToNodes);
  140. }
  141.  
  142.  
  143. void findAllArcs (){//ok
  144.     sc_addr setArcs;
  145.     sc_iterator5 *grToArcs = sc_iterator5_f_a_a_a_f_new(context,graph,sc_type_arc_pos_const_perm,
  146.                                                         0,sc_type_arc_pos_const_perm,rrel_arcs);
  147.     if (SC_TRUE == sc_iterator5_next(grToArcs)) {
  148.         setArcs = sc_iterator5_value(grToArcs, 2);
  149.     }
  150.     sc_iterator3 *arcs = sc_iterator3_f_a_a_new(context, setArcs, sc_type_arc_pos_const_perm, 0);
  151.     while (SC_TRUE == sc_iterator3_next(arcs)) {
  152.         allArcs.push_back(sc_iterator3_value(arcs, 2));
  153.     }
  154.     for(int i = 0; i < allArcs.size(); i++){
  155.         printEl(context, allArcs.at(i));
  156.         cout << endl;
  157.     }
  158.     sc_iterator3_free(arcs);
  159.     sc_iterator5_free(grToArcs);
  160. }
  161.  
  162.  
  163.  
  164. int findShortestPath (){//need change
  165.     int result = 1000;
  166. //    cout << "print paths" << endl;
  167. //    for (unsigned int i = 0; i < paths.size(); i++)
  168. //        cout << paths.at(i) << endl;
  169. //    cout << "---------" << endl;
  170.     for (unsigned int i = 0; i < paths.size(); i++)
  171.         if (paths.at(i) < result)
  172.             result = paths.at(i);
  173.     if (result == 1000) result = 0;
  174.     paths.clear();
  175.     return result;
  176. }
  177.  
  178.  
  179. void get_edge_vertexes(sc_addr edge, sc_addr &v1, sc_addr &v2)
  180. {
  181.     sc_memory_get_arc_begin(context, edge, &v1);
  182.     sc_memory_get_arc_end(context, edge, &v2);
  183. }
  184.  
  185. sc_addr get_other_vertex_incidence_edge(sc_addr edge, sc_addr vertex)
  186. {
  187.     sc_addr v1, v2, empty;
  188.     empty.seg = 0;
  189.     empty.offset = 0;
  190.  
  191.     get_edge_vertexes(edge, v1, v2);
  192.     if ((SC_ADDR_IS_EQUAL(vertex, v1)) || (SC_ADDR_IS_EQUAL(vertex, v2))) {
  193.         if (SC_ADDR_IS_EQUAL(vertex, v1)) {
  194.             return v2;
  195.         } else {
  196.             return v1;
  197.         }
  198.     }
  199.     return empty;
  200. }
  201.  
  202.  
  203.  
  204. int getIndex (sc_addr elem){
  205.     for (unsigned int i = 0; i < allNodes.size(); i++){
  206.         if (SC_ADDR_IS_EQUAL(elem, allNodes.at(i))){
  207.             return i;
  208.         }
  209.     }
  210. }
  211.  
  212.  
  213.  
  214. void findPathsBetweenTwoNodes (sc_addr beginNode, sc_addr endNode, int counter){
  215.     int buf = 0;
  216.     sc_addr nextNode;
  217.     sc_iterator5 *it = sc_iterator5_f_a_a_a_f_new(context, beginNode, sc_type_arc_common, 0, sc_type_arc_pos_const_perm, arcs);
  218.     while (SC_TRUE == sc_iterator5_next(it)){
  219.         if (SC_TRUE == find_vertex_in_setChecked(sc_iterator5_value(it, 2), checked))
  220.             continue;
  221.             nextNode = sc_iterator5_value(it, 2);
  222.  
  223.             buf = getWeight(beginNode, nextNode);
  224.  
  225.             counter += buf;
  226.         //cout << counter << endl;
  227.         if (SC_TRUE == SC_ADDR_IS_EQUAL(endNode, nextNode)){
  228.             paths.push_back(counter);
  229.             sc_iterator5_free(it);
  230.             //cout << "ok" << endl;
  231.             return;
  232.         }
  233.  
  234.         checked.push_back(nextNode);
  235.         findPathsBetweenTwoNodes(nextNode, endNode, counter);
  236.         //cout << "COUNTER " << counter << endl;
  237.         checked.pop_back();
  238.         counter = counter - buf;
  239.     }
  240.     counter = 0;
  241.     sc_iterator5_free(it);
  242.     return;
  243. }
  244.  
  245.  
  246. void findAllMinPaths (int somePath){
  247.     findAllNodes();
  248.     int result = 0, counter = 0;
  249.     for (unsigned int i = 0; i < allNodes.size(); i++){
  250.         for (unsigned int j = 0; j < allNodes.size(); j++){
  251.             checked.push_back(allNodes.at(i));
  252.             findPathsBetweenTwoNodes(allNodes.at(i), allNodes.at(j), counter);
  253.  
  254.             result = findShortestPath();
  255.  
  256.             if (result == somePath){
  257.                 //addResultInKB(allNodes.at(i), allNodes.at(j));
  258.                 amOfPairs++;
  259.                 result1.push_back(allNodes.at(i));
  260.                 result2.push_back(allNodes.at(j));
  261.                 printEl(context, allNodes.at(i));
  262.                 cout << "-->";
  263.                 printEl(context, allNodes.at(j));
  264.                 cout << endl;
  265.             }
  266.             checked.clear();
  267.         }
  268.     }
  269. }
  270.  
  271.  
  272. void run_test(char number_test, int somePath)
  273. {
  274.  
  275.     weight_of_path = sc_memory_node_new(context, sc_type_const);
  276.  
  277.     char gr[3] = "Gx";
  278.     gr[1] = number_test;
  279.     sc_helper_resolve_system_identifier(context, gr, &graph);
  280.     sc_helper_resolve_system_identifier(context, "rrel_arcs", &rrel_arcs);
  281.     sc_helper_resolve_system_identifier(context, "rrel_nodes", &rrel_nodes);
  282.     sc_helper_resolve_system_identifier(context, "nrel_idtf", &nrel_idtf);
  283.     sc_helper_resolve_system_identifier(context, "nrel_weight", &nrel_weight);
  284. //    sc_addr idtf_path;
  285. //    idtf_path = genIntNode(context, somePath);
  286. //    sc_memory_arc_new(context, sc_type_arc_common, weight_of_path, idtf_path);
  287. //    sc_iterator3 *it = sc_iterator3_f_a_f_new(context, weight_of_path, 0, idtf_path);
  288. //    sc_addr arc1;
  289. //    if (SC_TRUE == sc_iterator3_next(it)) arc1 = sc_iterator3_value(it, 1);
  290. //    sc_memory_arc_new(context, sc_type_arc_pos_const_perm, nrel_idtf, arc1);
  291.  
  292. //    pr();
  293. //    cout << "Graph: ";
  294.  
  295. //    print_graph();
  296.  
  297. //    cout << "Find minimal path from '" << beg_vertex << "' to '"
  298. //         << end_vertex << "'" << endl;
  299. //    sc_addr lebel = find_min_path(beg, end);
  300.  
  301.     cout << "Pair of nodes with path " << somePath << ":" << endl;
  302.  
  303. //      findAllNodes();
  304.  
  305. //    for (int j = 0; j < allNodes.size(); j++)
  306. //        for (int i = 0; i < allNodes.size(); i++){
  307. //            if (SC_TRUE == isIncidence(allNodes.at(i),allNodes.at(j))){
  308. //                cout << "incidence ";
  309. //                printEl(context, allNodes.at(i));
  310. //                cout << " ";
  311. //                printEl(context, allNodes.at(j));
  312. //                int k = getWeight(allNodes.at(i), allNodes.at(j));
  313. //                cout << " weigt " << k;
  314. //                cout << endl;
  315. //            }
  316.  
  317. //            else{
  318. //                cout << "noincidence ";
  319. //                printEl(context, allNodes.at(i));
  320. //                cout << " ";
  321. //                printEl(context, allNodes.at(j));
  322. //                cout << endl;
  323. //            }
  324.  
  325. //        }
  326.  
  327.     findAllMinPaths(somePath);
  328.     // for (unsigned int var = 0; var < result1.size(); var++)
  329.     //    addResultInKB(result1.at(var),result2.at(var));
  330.  
  331.     cout << "Amount of pairs G" << number_test << " is " << amOfPairs/2 << endl;
  332.  
  333.     sc_addr answ = sc_memory_node_new(context, sc_type_const);
  334.     answ = genIntNode(context, amOfPairs/2);
  335.     sc_memory_arc_new(context, sc_type_arc_common, graph, answ);
  336.  
  337.     amOfPairs = 0;
  338.     allNodes.clear();
  339.     checked.clear();
  340.     paths.clear();
  341. }
  342.  
  343. int main()
  344. {
  345.     sc_memory_params params;
  346.  
  347.     sc_memory_params_clear(&params);
  348.     params.repo_path = "/home/wcobalt/Files/BSUIR/ostis/kb.bin";
  349.     params.config_file = "/home/wcobalt/Files/BSUIR/ostis/config/sc-web.ini";
  350.     params.ext_path = "/home/wcobalt/Files/BSUIR/ostis/sc-machine/bin/extensions";
  351.     params.clear = SC_FALSE;
  352.  
  353.     sc_memory_initialize(&params);
  354.  
  355.     context = sc_memory_context_new(sc_access_lvl_make_max);
  356.  
  357.  
  358.     //////////////////////////////////////////////////////////////////////////////////
  359.     run_test('0', 2);
  360.     run_test('2', 4);
  361.     run_test('3', 3);
  362.     run_test('4', 2);
  363.     run_test('5', 5);
  364.  
  365.     cout << "The end" << endl;
  366.  
  367.     sc_memory_context_free(context);
  368.  
  369.     sc_memory_shutdown(SC_FALSE);
  370.  
  371.     return 0;
  372. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement