Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern "C" {
- #include "sc_memory_headers.h"
- #include "sc_helper.h"
- #include "utils.h"
- }
- #include <stdio.h>
- #include <iostream>
- #include <glib.h>
- #include <unistd.h>
- #include <assert.h>
- #include <vector>
- #include <algorithm>
- using namespace std;
- sc_memory_context *context;
- sc_addr graph, rrel_arcs, rrel_nodes, visit, curr_vertex, father, nrel_weight, weight_of_path,
- nrel_srdiam, nrel_idtf, ref, antiref, partref, arcs;
- int amOfNodes = 0;
- int amOfPairs = 0;
- vector <sc_addr> nodes;
- vector <sc_addr> allNodes;
- vector <sc_addr> allArcs;
- vector <sc_addr> checked;
- vector <sc_addr> result1;
- vector <sc_addr> result2;
- vector <int> paths;
- sc_bool find_vertex_in_setChecked(sc_addr element, vector <sc_addr> set) // еÑÑ‚ÑŒ ли узел в множеÑтве?
- {
- if (false == set.empty()){
- for (int i = 0; i < set.size(); i++){
- if (SC_TRUE == SC_ADDR_IS_EQUAL(set.at(i), element)) return SC_TRUE;
- }
- }
- return SC_FALSE;
- }
- int getWeight(sc_addr v1, sc_addr v2){//ok
- int result = 0;
- sc_addr node_weight, weight, arc;
- sc_iterator5 *it1 = sc_iterator5_f_a_f_a_f_new(context,
- v1,
- sc_type_arc_common,
- v2,
- sc_type_arc_pos_const_perm, arcs);
- if (SC_TRUE == sc_iterator5_next(it1)){
- arc = sc_iterator5_value(it1, 1);
- }
- 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);
- if (SC_TRUE == sc_iterator5_next(it)){
- node_weight = sc_iterator5_value(it, 2);
- }
- // printEl(context, node_weight);
- sc_iterator5 *itTwo = sc_iterator5_f_a_a_a_f_new(context,node_weight,sc_type_arc_common,
- 0,sc_type_arc_pos_const_perm,nrel_idtf);
- if (SC_TRUE == sc_iterator5_next(itTwo)) weight = sc_iterator5_value(itTwo, 2);
- result = getInt(context, weight);
- sc_iterator5_free(it);
- sc_iterator5_free(itTwo);
- return result;
- }
- int getWeight(sc_addr arc){//ok
- int result = 0;
- sc_addr node_weight, weight;
- 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);
- if (SC_TRUE == sc_iterator5_next(it)){
- node_weight = sc_iterator5_value(it, 2);
- }
- sc_iterator5 *itTwo = sc_iterator5_f_a_a_a_f_new(context,node_weight,sc_type_arc_common,
- 0,sc_type_arc_pos_const_perm,nrel_idtf);
- if (SC_TRUE == sc_iterator5_next(itTwo)) weight = sc_iterator5_value(itTwo, 2);
- result = getInt(context, weight);
- sc_iterator5_free(it);
- sc_iterator5_free(itTwo);
- return result;
- }
- bool isIncidence (sc_addr v1, sc_addr v2){
- if (SC_FALSE == SC_ADDR_IS_EQUAL(v1, v2)){
- sc_iterator3 *it = sc_iterator3_f_a_a_new(context, v1, sc_type_arc_common, 0);
- while (SC_TRUE == sc_iterator3_next(it))
- if (SC_TRUE == SC_ADDR_IS_EQUAL(v2, sc_iterator3_value(it,2))){
- sc_iterator3_free(it);
- return SC_TRUE;
- }
- sc_iterator3_free(it);
- return SC_FALSE;
- }else{
- return SC_FALSE;
- }
- }
- void pr (){//ok
- for (int j = 0; j < allNodes.size(); j++)
- for(int i = 0; i < allNodes.size(); i++){
- printEl(context, allNodes.at(j));
- cout << " " ;
- printEl(context, allNodes.at(i));
- cout << getWeight(allNodes.at(i), allNodes.at(j));
- cout << endl;
- }
- }
- void findAllNodes (){//ok
- sc_addr setNodes;
- sc_iterator5 *grToNodes = sc_iterator5_f_a_a_a_f_new(context,graph,sc_type_arc_pos_const_perm,
- 0,sc_type_arc_pos_const_perm,rrel_nodes);
- if (SC_TRUE == sc_iterator5_next(grToNodes)) {
- setNodes = sc_iterator5_value(grToNodes, 2);
- }
- sc_iterator5 *grToArcs = sc_iterator5_f_a_a_a_f_new(context,graph,sc_type_arc_pos_const_perm,
- 0,sc_type_arc_pos_const_perm,rrel_arcs);
- if (SC_TRUE == sc_iterator5_next(grToArcs)) {
- arcs = sc_iterator5_value(grToArcs, 2);
- }
- sc_iterator3 *nodes = sc_iterator3_f_a_a_new(context, setNodes, sc_type_arc_pos_const_perm, 0);
- while (SC_TRUE == sc_iterator3_next(nodes)) {
- allNodes.push_back(sc_iterator3_value(nodes, 2));
- }
- for(int i = 0; i < allNodes.size(); i++){
- printEl(context, allNodes.at(i));
- cout << endl;
- }
- sc_iterator3_free(nodes);
- sc_iterator5_free(grToNodes);
- }
- void findAllArcs (){//ok
- sc_addr setArcs;
- sc_iterator5 *grToArcs = sc_iterator5_f_a_a_a_f_new(context,graph,sc_type_arc_pos_const_perm,
- 0,sc_type_arc_pos_const_perm,rrel_arcs);
- if (SC_TRUE == sc_iterator5_next(grToArcs)) {
- setArcs = sc_iterator5_value(grToArcs, 2);
- }
- sc_iterator3 *arcs = sc_iterator3_f_a_a_new(context, setArcs, sc_type_arc_pos_const_perm, 0);
- while (SC_TRUE == sc_iterator3_next(arcs)) {
- allArcs.push_back(sc_iterator3_value(arcs, 2));
- }
- for(int i = 0; i < allArcs.size(); i++){
- printEl(context, allArcs.at(i));
- cout << endl;
- }
- sc_iterator3_free(arcs);
- sc_iterator5_free(grToArcs);
- }
- int findShortestPath (){//need change
- int result = 1000;
- // cout << "print paths" << endl;
- // for (unsigned int i = 0; i < paths.size(); i++)
- // cout << paths.at(i) << endl;
- // cout << "---------" << endl;
- for (unsigned int i = 0; i < paths.size(); i++)
- if (paths.at(i) < result)
- result = paths.at(i);
- if (result == 1000) result = 0;
- paths.clear();
- return result;
- }
- void get_edge_vertexes(sc_addr edge, sc_addr &v1, sc_addr &v2)
- {
- sc_memory_get_arc_begin(context, edge, &v1);
- sc_memory_get_arc_end(context, edge, &v2);
- }
- sc_addr get_other_vertex_incidence_edge(sc_addr edge, sc_addr vertex)
- {
- sc_addr v1, v2, empty;
- empty.seg = 0;
- empty.offset = 0;
- get_edge_vertexes(edge, v1, v2);
- if ((SC_ADDR_IS_EQUAL(vertex, v1)) || (SC_ADDR_IS_EQUAL(vertex, v2))) {
- if (SC_ADDR_IS_EQUAL(vertex, v1)) {
- return v2;
- } else {
- return v1;
- }
- }
- return empty;
- }
- int getIndex (sc_addr elem){
- for (unsigned int i = 0; i < allNodes.size(); i++){
- if (SC_ADDR_IS_EQUAL(elem, allNodes.at(i))){
- return i;
- }
- }
- }
- void findPathsBetweenTwoNodes (sc_addr beginNode, sc_addr endNode, int counter){
- int buf = 0;
- sc_addr nextNode;
- 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);
- while (SC_TRUE == sc_iterator5_next(it)){
- if (SC_TRUE == find_vertex_in_setChecked(sc_iterator5_value(it, 2), checked))
- continue;
- nextNode = sc_iterator5_value(it, 2);
- buf = getWeight(beginNode, nextNode);
- counter += buf;
- //cout << counter << endl;
- if (SC_TRUE == SC_ADDR_IS_EQUAL(endNode, nextNode)){
- paths.push_back(counter);
- sc_iterator5_free(it);
- //cout << "ok" << endl;
- return;
- }
- checked.push_back(nextNode);
- findPathsBetweenTwoNodes(nextNode, endNode, counter);
- //cout << "COUNTER " << counter << endl;
- checked.pop_back();
- counter = counter - buf;
- }
- counter = 0;
- sc_iterator5_free(it);
- return;
- }
- void findAllMinPaths (int somePath){
- findAllNodes();
- int result = 0, counter = 0;
- for (unsigned int i = 0; i < allNodes.size(); i++){
- for (unsigned int j = 0; j < allNodes.size(); j++){
- checked.push_back(allNodes.at(i));
- findPathsBetweenTwoNodes(allNodes.at(i), allNodes.at(j), counter);
- result = findShortestPath();
- if (result == somePath){
- //addResultInKB(allNodes.at(i), allNodes.at(j));
- amOfPairs++;
- result1.push_back(allNodes.at(i));
- result2.push_back(allNodes.at(j));
- printEl(context, allNodes.at(i));
- cout << "-->";
- printEl(context, allNodes.at(j));
- cout << endl;
- }
- checked.clear();
- }
- }
- }
- void run_test(char number_test, int somePath)
- {
- weight_of_path = sc_memory_node_new(context, sc_type_const);
- char gr[3] = "Gx";
- gr[1] = number_test;
- sc_helper_resolve_system_identifier(context, gr, &graph);
- sc_helper_resolve_system_identifier(context, "rrel_arcs", &rrel_arcs);
- sc_helper_resolve_system_identifier(context, "rrel_nodes", &rrel_nodes);
- sc_helper_resolve_system_identifier(context, "nrel_idtf", &nrel_idtf);
- sc_helper_resolve_system_identifier(context, "nrel_weight", &nrel_weight);
- // sc_addr idtf_path;
- // idtf_path = genIntNode(context, somePath);
- // sc_memory_arc_new(context, sc_type_arc_common, weight_of_path, idtf_path);
- // sc_iterator3 *it = sc_iterator3_f_a_f_new(context, weight_of_path, 0, idtf_path);
- // sc_addr arc1;
- // if (SC_TRUE == sc_iterator3_next(it)) arc1 = sc_iterator3_value(it, 1);
- // sc_memory_arc_new(context, sc_type_arc_pos_const_perm, nrel_idtf, arc1);
- // pr();
- // cout << "Graph: ";
- // print_graph();
- // cout << "Find minimal path from '" << beg_vertex << "' to '"
- // << end_vertex << "'" << endl;
- // sc_addr lebel = find_min_path(beg, end);
- cout << "Pair of nodes with path " << somePath << ":" << endl;
- // findAllNodes();
- // for (int j = 0; j < allNodes.size(); j++)
- // for (int i = 0; i < allNodes.size(); i++){
- // if (SC_TRUE == isIncidence(allNodes.at(i),allNodes.at(j))){
- // cout << "incidence ";
- // printEl(context, allNodes.at(i));
- // cout << " ";
- // printEl(context, allNodes.at(j));
- // int k = getWeight(allNodes.at(i), allNodes.at(j));
- // cout << " weigt " << k;
- // cout << endl;
- // }
- // else{
- // cout << "noincidence ";
- // printEl(context, allNodes.at(i));
- // cout << " ";
- // printEl(context, allNodes.at(j));
- // cout << endl;
- // }
- // }
- findAllMinPaths(somePath);
- // for (unsigned int var = 0; var < result1.size(); var++)
- // addResultInKB(result1.at(var),result2.at(var));
- cout << "Amount of pairs G" << number_test << " is " << amOfPairs/2 << endl;
- sc_addr answ = sc_memory_node_new(context, sc_type_const);
- answ = genIntNode(context, amOfPairs/2);
- sc_memory_arc_new(context, sc_type_arc_common, graph, answ);
- amOfPairs = 0;
- allNodes.clear();
- checked.clear();
- paths.clear();
- }
- int main()
- {
- sc_memory_params params;
- sc_memory_params_clear(¶ms);
- params.repo_path = "/home/wcobalt/Files/BSUIR/ostis/kb.bin";
- params.config_file = "/home/wcobalt/Files/BSUIR/ostis/config/sc-web.ini";
- params.ext_path = "/home/wcobalt/Files/BSUIR/ostis/sc-machine/bin/extensions";
- params.clear = SC_FALSE;
- sc_memory_initialize(¶ms);
- context = sc_memory_context_new(sc_access_lvl_make_max);
- //////////////////////////////////////////////////////////////////////////////////
- run_test('0', 2);
- run_test('2', 4);
- run_test('3', 3);
- run_test('4', 2);
- run_test('5', 5);
- cout << "The end" << endl;
- sc_memory_context_free(context);
- sc_memory_shutdown(SC_FALSE);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement