Exemple #1
0
sc_result resolve_nrel_system_identifier(sc_memory_context const * ctx)
{
    sc_addr *results = 0;
    sc_uint32 results_count = 0;
    sc_stream *stream = sc_stream_memory_new(keynodes_str[SC_KEYNODE_NREL_SYSTEM_IDENTIFIER],
                                             (sc_uint)(sizeof(sc_uchar) * strlen(keynodes_str[SC_KEYNODE_NREL_SYSTEM_IDENTIFIER])),
                                             SC_STREAM_FLAG_READ, SC_FALSE);
    sc_uint32 i = 0;
    sc_iterator5 *it = 0;
    sc_bool found = SC_FALSE;
    sc_addr addr1, addr2;

    // try to find nrel_system_identifier strings
    if (sc_memory_find_links_with_content(ctx, stream, &results, &results_count) == SC_RESULT_OK)
    {
        for (i = 0; i < results_count; i++)
        {
            it = sc_iterator5_a_a_f_a_a_new(ctx,
                                            sc_type_node | sc_type_const | sc_type_node_norole,
                                            sc_type_arc_common | sc_type_const,
                                            results[i],
                                            sc_type_arc_pos_const_perm,
                                            sc_type_const | sc_type_node | sc_type_node_norole);

            while (sc_iterator5_next(it))
            {

                addr1 = sc_iterator5_value(it, 0);
                addr2 = sc_iterator5_value(it, 4);
                // comare begin sc-element and attribute, they must be equivalent
                if (SC_ADDR_IS_EQUAL(addr1, addr2))
                {
                    if (found == SC_FALSE)
                    {
                        sc_keynodes[SC_KEYNODE_NREL_SYSTEM_IDENTIFIER] = addr1;
                        found = SC_TRUE;
                    }else
                    {
                        sc_iterator5_free(it);
                        sc_stream_free(stream);
                        g_free(results);
                        g_error("There are more then one sc-elements with system identifier nrel_system_identifier ");
                        return SC_RESULT_ERROR;
                    }
                }
            }

            sc_iterator5_free(it);
        }

        g_free(results);
    }else
        return SC_RESULT_ERROR;

    sc_stream_free(stream);

    return found == SC_TRUE ? SC_RESULT_OK : SC_RESULT_ERROR;
}
//scp_result searchElStr5_f_f_f_a_a(scp_operand *param1, scp_operand *param2, scp_operand *param3, scp_operand *param4, scp_operand *param5){}
//scp_result searchElStr5_f_f_a_f_a(scp_operand *param1, scp_operand *param2, scp_operand *param3, scp_operand *param4, scp_operand *param5){}
//scp_result searchElStr5_f_f_a_a_f(scp_operand *param1, scp_operand *param2, scp_operand *param3, scp_operand *param4, scp_operand *param5){}
//scp_result searchElStr5_f_a_f_f_a(scp_operand *param1, scp_operand *param2, scp_operand *param3, scp_operand *param4, scp_operand *param5){}
scp_result searchElStr5_f_a_f_a_f(scp_operand *param1, scp_operand *param2, scp_operand *param3, scp_operand *param4, scp_operand *param5)
{
    sc_iterator5 *it = sc_iterator5_f_a_f_a_f_new(param1->addr, param2->element_type, param3->addr, param4->element_type, param5->addr);
    if (SC_TRUE == sc_iterator5_next(it))
    {
        param2->addr = sc_iterator5_value(it, 1);
        param4->addr = sc_iterator5_value(it, 3);
        sc_iterator5_free(it);
        return SCP_RESULT_TRUE;
    }
    sc_iterator5_free(it);
    return SCP_RESULT_FALSE;
}
Exemple #3
0
sc_result sc_helper_find_element_by_system_identifier(sc_memory_context const * ctx, const sc_char* data, sc_uint32 len, sc_addr *result_addr)
{
    sc_addr *results = 0;
    sc_uint32 results_count = 0;
    sc_stream *stream = 0;
    sc_uint32 i = 0;
    sc_iterator5 *it = 0;
    sc_bool found = SC_FALSE;

    g_assert(sc_helper_is_initialized == SC_TRUE);
    g_assert(sc_keynodes != 0);

    // try to find sc-links with that contains system identifier value
    stream = sc_stream_memory_new(data, sizeof(sc_char) * len, SC_STREAM_FLAG_READ, SC_FALSE);
    if (sc_memory_find_links_with_content(ctx, stream, &results, &results_count) == SC_RESULT_OK)
    {
        for (i = 0; i < results_count; i++)
        {
            it = sc_iterator5_a_a_f_a_f_new(ctx,
                                            0,
                                            sc_type_arc_common | sc_type_const,
                                            results[i],
                                            sc_type_arc_pos_const_perm,
                                            sc_keynodes[SC_KEYNODE_NREL_SYSTEM_IDENTIFIER]);
            if (sc_iterator5_next(it))
            {
                if (found == SC_FALSE)
                {
                    found = SC_TRUE;
                    *result_addr = sc_iterator5_value(it, 0);
                }else
                {
                    // don't foget to free allocated memory before return error
                    sc_iterator5_free(it);
                    sc_stream_free(stream);
                    g_free(results);
                    return SC_RESULT_ERROR_INVALID_STATE;
                }
            }

            sc_iterator5_free(it);
        }

        g_free(results);
    }

    sc_stream_free(stream);

    return found == SC_TRUE ? SC_RESULT_OK : SC_RESULT_ERROR;
}
/*! Deep first search to build a connective component
 * \param curr_vertex
 *          start vertex for the search
 * \param not_checked_vertices
 *          set of not checked vertices
 * \param curr_conn_comp
 *          connective component that is being built by this search
 * \param graph
 *          sc-addr of graph structure to search connective components
 */
void deep_first_search_for_find_conn_comp(sc_addr curr_vertex, sc_addr not_checked_vertices, sc_addr curr_conn_comp, sc_addr graph)
{
    sc_iterator3 *it3 = sc_iterator3_f_a_f_new(not_checked_vertices,
                                             sc_type_arc_pos_const_perm,
                                             curr_vertex);

    if (sc_iterator3_next(it3) == SC_TRUE)
        sc_memory_element_free(it3->results[1]);

    sc_iterator3_free(it3);

    sc_memory_arc_new(sc_type_arc_pos_const_perm, curr_conn_comp, curr_vertex);

    sc_iterator5 *it5 = sc_iterator5_f_a_a_a_f_new(curr_vertex,
                                                   sc_type_edge_common | sc_type_const,
                                                   sc_type_node | sc_type_const,
                                                   sc_type_arc_pos_const_perm,
                                                   graph);

    while(sc_iterator5_next(it5) == SC_TRUE)
    {
        if (sc_helper_check_arc(not_checked_vertices, it5->results[2],
                                sc_type_arc_pos_const_perm) == SC_TRUE)
            deep_first_search_for_find_conn_comp(it5->results[2], not_checked_vertices, curr_conn_comp, graph);
    }
    sc_iterator5_free(it5);

}
bool TestGraphModify::check_arc_creation()
{
    assert(SC_ADDR_IS_NOT_EMPTY(mGraphAddr));

    sc_addr arc, v1, v2;

    // create
    if (sc_graph_create_vertex(mGraphAddr, &v1) != SC_RESULT_OK)
        return false;
    if (sc_graph_create_vertex(mGraphAddr, &v2) != SC_RESULT_OK)
        return false;
    if (sc_graph_create_arc(mGraphAddr, v1, v2, &arc) != SC_RESULT_OK)
        return false;

    // check
    if (sc_graph_check_arc(mGraphAddr, arc) != SC_RESULT_OK)
        return false;

    sc_iterator5 *it5 = sc_iterator5_f_a_f_a_f_new(v1,
                                                   sc_type_arc_common | sc_type_const,
                                                   v2,
                                                   sc_type_arc_pos_const_perm,
                                                   mGraphAddr);
    int count = 0;
    while (sc_iterator5_next(it5) == SC_TRUE)
        count++;

    sc_iterator5_free(it5);

    return count == 1;
}
 void destroy()
 {
     if (mIterator)
     {
         sc_iterator5_free(mIterator);
         mIterator = 0;
     }
 }
sc_result sc_graph_find_conn_comp(sc_addr graph, sc_addr *conn_comp_set)
{
    if (sc_helper_check_arc(sc_graph_keynode_not_oriented_graph, graph, sc_type_arc_pos_const_perm) == SC_FALSE)
        return SC_RESULT_ERROR_INVALID_PARAMS;

    sc_addr not_checked_vertices, curr_vertex;

    sc_iterator3 *it3;

    sc_iterator5 *it5 = sc_iterator5_f_a_a_a_f_new(graph,
                                                   sc_type_arc_pos_const_perm,
                                                   sc_type_node | sc_type_const,
                                                   sc_type_arc_pos_const_perm,
                                                   sc_graph_keynode_rrel_vertex);

    not_checked_vertices = sc_memory_node_new(sc_type_node | sc_type_const);
    *conn_comp_set = sc_memory_node_new(sc_type_node | sc_type_const);


    while(sc_iterator5_next(it5) == SC_TRUE)
    {
        curr_vertex = it5->results[2];
        sc_memory_arc_new(sc_type_arc_pos_const_perm, not_checked_vertices, curr_vertex);
    }

    sc_iterator5_free(it5);

    it3 = sc_iterator3_f_a_a_new(not_checked_vertices,
                                 sc_type_arc_pos_const_perm,
                                 sc_type_node);

    while(sc_iterator3_next(it3) == SC_TRUE)
    {
        sc_addr curr_conn_comp = sc_memory_node_new(sc_type_node | sc_type_const);
        curr_vertex = it3->results[2];
        if(sc_helper_check_arc(not_checked_vertices, curr_vertex,
                               sc_type_arc_pos_const_perm) == SC_FALSE)
            continue;
        deep_first_search_for_find_conn_comp(curr_vertex, not_checked_vertices, curr_conn_comp, graph);
        sc_memory_arc_new(sc_type_arc_pos_const_perm, *conn_comp_set, curr_conn_comp);
    }

    sc_iterator3_free(it3);
    sc_memory_element_free(not_checked_vertices);

    return SC_RESULT_OK;
}
Exemple #8
0
sc_result search_incident_vertex_arc(sc_addr graph, sc_addr vertex, sc_addr_list **listArc)
{
    sc_iterator3 *it3;
    sc_iterator5 *it5;
    sc_type arc_type;
    sc_addr sc_graph_keynode_rrel;

    if (sc_helper_check_arc(sc_graph_keynode_not_oriented_graph, graph, sc_type_arc_pos_const_perm))
    {
        arc_type = sc_type_edge_common;
        sc_graph_keynode_rrel = sc_graph_keynode_rrel_edge;
    }
    else if (sc_helper_check_arc(sc_graph_keynode_oriented_graph, graph, sc_type_arc_pos_const_perm))
    {
        arc_type = sc_type_arc_common;
        sc_graph_keynode_rrel = sc_graph_keynode_rrel_arc;
    }


    it3 = sc_iterator3_f_a_a_new(vertex,
                                 arc_type,
                                 sc_type_node);
    while(sc_iterator3_next(it3) == SC_TRUE)
    {
        it5 = sc_iterator5_f_a_f_a_f_new(graph,
                                                       sc_type_arc_pos,
                                                       it3->results[1],
                                                       sc_type_arc_pos,
                                                       sc_graph_keynode_rrel);
        while(sc_iterator5_next(it5) == SC_TRUE)
        {
            *listArc = sc_addr_list_append(*listArc);
            (*listArc)->value = it5->results[2];
        }



    }

     sc_iterator5_free(it5);
     sc_iterator3_free(it3);

     return SC_RESULT_OK;

}
Exemple #9
0
sc_result sc_graph_find_conn_comp(sc_addr graph, sc_addr_list **conn_comp_set)
{
    sc_addr curr_vertex, graph_keynode;
    sc_addr_list *not_checked_vertices = nullptr;

    sc_iterator5 *it5 = sc_iterator5_f_a_a_a_f_new(graph,
                                                   sc_type_arc_pos_const_perm,
                                                   sc_type_node | sc_type_const,
                                                   sc_type_arc_pos_const_perm,
                                                   sc_graph_keynode_rrel_vertex);

    if (sc_helper_check_arc(sc_graph_keynode_graph, graph, sc_type_arc_pos_const_perm) == SC_FALSE)
        return SC_RESULT_ERROR_INVALID_PARAMS;

    if (sc_helper_check_arc(sc_graph_keynode_not_oriented_graph, graph, sc_type_arc_pos_const_perm) == SC_TRUE)
        graph_keynode = sc_graph_keynode_not_oriented_graph;
    else if (sc_helper_check_arc(sc_graph_keynode_oriented_graph, graph, sc_type_arc_pos_const_perm) == SC_TRUE)
        graph_keynode = sc_graph_keynode_oriented_graph;
    else return SC_RESULT_ERROR_INVALID_PARAMS;

    *conn_comp_set = nullptr;

    while(sc_iterator5_next(it5) == SC_TRUE)
    {
        curr_vertex = it5->results[2];
        not_checked_vertices = sc_addr_list_append(not_checked_vertices);
        not_checked_vertices->value = curr_vertex;
    }

    sc_iterator5_free(it5);

    while (not_checked_vertices != nullptr)
    {
        sc_addr curr_conn_comp = sc_memory_node_new(sc_type_node | sc_type_const);
        sc_memory_arc_new(sc_type_arc_pos_const_perm, sc_graph_keynode_graph, curr_conn_comp);
        sc_memory_arc_new(sc_type_arc_pos_const_perm, graph_keynode, curr_conn_comp);
        curr_vertex = not_checked_vertices->value;
        deep_first_search_for_find_conn_comp(curr_vertex, &not_checked_vertices, &curr_conn_comp, graph);
        *conn_comp_set = sc_addr_list_append(*conn_comp_set);
        (*conn_comp_set)->value = curr_conn_comp;
    }

    return SC_RESULT_OK;
}
Exemple #10
0
sc_result search_incident_vertexes(sc_addr graph, sc_addr arc, sc_addr_list **listVertex)
{
    sc_iterator3 *it3;
    sc_iterator5 *it5;
    sc_type arc_type;
   // sc_addr sc_graph_keynode_rrel;

    if (sc_helper_check_arc(sc_graph_keynode_not_oriented_graph, graph, sc_type_arc_pos_const_perm))
        arc_type = sc_type_edge_common;

    else if (sc_helper_check_arc(sc_graph_keynode_oriented_graph, graph, sc_type_arc_pos_const_perm))
        arc_type = sc_type_arc_common;

    it5 = sc_iterator5_f_a_a_a_f_new(graph,
                                                   sc_type_arc_pos,
                                                   sc_type_node,
                                                   sc_type_arc_pos,
                                                   sc_graph_keynode_rrel_vertex);


    while(sc_iterator5_next(it5) == SC_TRUE)
    {
        it3 = sc_iterator3_f_a_a_new(it5->results[2],
                                     arc_type,
                                     sc_type_node);
        while(sc_iterator3_next(it3) == SC_TRUE)
        {
            if(SC_ADDR_IS_EQUAL(it3->results[1],arc))
            {
                *listVertex = sc_addr_list_append(*listVertex);
                (*listVertex)->value = it3->results[0];
                *listVertex = sc_addr_list_append(*listVertex);
                (*listVertex)->value = it3->results[2];
            }
        }

    }

    sc_iterator5_free(it5);
    sc_iterator3_free(it3);

    return SC_RESULT_OK;
}
Exemple #11
0
/*! Deep first search to build a connective component
 * \param curr_vertex
 *          start vertex for the search
 * \param not_checked_vertices
 *          set of not checked vertices
 * \param curr_conn_comp
 *          connective component that is being built by this search
 * \param graph
 *          sc-addr of graph structure to search connective components
 */
void deep_first_search_for_find_conn_comp(sc_addr curr_vertex, sc_addr_list **not_checked_vertices, sc_addr *curr_conn_comp, sc_addr graph)
{
    sc_addr arc, rrel_keynode;
    sc_type arc_type;
    sc_iterator5 *it5;

    *not_checked_vertices = sc_addr_list_remove(*not_checked_vertices);

    arc = sc_memory_arc_new(sc_type_arc_pos_const_perm, *curr_conn_comp, curr_vertex);
    sc_memory_arc_new(sc_type_arc_pos_const_perm, sc_graph_keynode_rrel_vertex, arc);

    if (sc_helper_check_arc(sc_graph_keynode_not_oriented_graph, graph, sc_type_arc_pos_const_perm) == SC_TRUE)
    {
        arc_type = sc_type_edge_common;
        rrel_keynode = sc_graph_keynode_rrel_edge;
    }
    else if (sc_helper_check_arc(sc_graph_keynode_oriented_graph, graph, sc_type_arc_pos_const_perm) == SC_TRUE)
    {
        arc_type = sc_type_arc_common;
        rrel_keynode = sc_graph_keynode_rrel_arc;
    }

    it5 = sc_iterator5_f_a_a_a_f_new(curr_vertex,
                                     arc_type | sc_type_const,
                                     sc_type_node | sc_type_const,
                                     sc_type_arc_pos_const_perm,
                                     graph);

    while(sc_iterator5_next(it5) == SC_TRUE)
    {
        if (sc_addr_list_is_included(*not_checked_vertices, it5->results[2]) == SC_TRUE)
        {
            arc = sc_memory_arc_new(sc_type_arc_pos_const_perm, *curr_conn_comp, it5->results[1]);
            sc_memory_arc_new(sc_type_arc_pos_const_perm, rrel_keynode, arc);
            deep_first_search_for_find_conn_comp(it5->results[2], not_checked_vertices, curr_conn_comp, graph);
        }
    }
    sc_iterator5_free(it5);

}
void search_typical_sc_neighborhood(sc_addr elem, sc_addr answer, sc_bool sys_off)
{
    sc_iterator3 *it1, *it0;
    sc_iterator5 *it5;
    sc_bool found = SC_FALSE;

    // search for keynode_typical_sc_neighborhood
    it0 = sc_iterator3_a_a_f_new(sc_type_node | sc_type_const,
                                 sc_type_arc_pos_const_perm,
                                 elem);
    while (sc_iterator3_next(it0) == SC_TRUE)
    {
        if (SC_ADDR_IS_EQUAL(sc_iterator3_value(it0, 0), keynode_typical_sc_neighborhood))
        {
            found = SC_TRUE;
            // iterate input arcs for elem
            it1 = sc_iterator3_f_a_a_new(elem,
                                         sc_type_arc_pos_const_perm,
                                         0);
            while (sc_iterator3_next(it1) == SC_TRUE)
            {
                if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator3_value(it1, 1)) || IS_SYSTEM_ELEMENT(sc_iterator3_value(it1, 2))))
                    continue;

                appendIntoAnswer(answer, sc_iterator3_value(it1, 1));
                appendIntoAnswer(answer, sc_iterator3_value(it1, 2));
            }
            sc_iterator3_free(it1);

            appendIntoAnswer(answer, sc_iterator3_value(it0, 1));
            appendIntoAnswer(answer, sc_iterator3_value(it0, 0));
            continue;
        }

        if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator3_value(it0, 0)) || IS_SYSTEM_ELEMENT(sc_iterator3_value(it0, 1))))
            continue;

        it5 = sc_iterator5_f_a_f_a_f_new(keynode_sc_neighborhood,
                                         sc_type_arc_common | sc_type_const,
                                         sc_iterator3_value(it0, 0),
                                         sc_type_arc_pos_const_perm,
                                         keynode_nrel_inclusion);
        if (sc_iterator5_next(it5) == SC_TRUE)
        {
            if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 1)) || IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 3))))
                continue;

            appendIntoAnswer(answer, sc_iterator3_value(it0, 0));
            appendIntoAnswer(answer, sc_iterator3_value(it0, 1));
            appendIntoAnswer(answer, sc_iterator5_value(it5, 1));
            appendIntoAnswer(answer, sc_iterator5_value(it5, 3));
        }
        sc_iterator5_free(it5);

    }
    sc_iterator3_free(it0);
    if (found == SC_TRUE)
    {
        appendIntoAnswer(answer, keynode_typical_sc_neighborhood);
    }
}
Exemple #13
0
sc_result sc_helper_set_system_identifier(sc_memory_context const * ctx, sc_addr addr, const sc_char* data, sc_uint32 len)
{
    sc_iterator5 *it5 = 0;
    sc_addr *results = 0;
    sc_uint32 results_count = 0;
    sc_stream *stream = 0;
    sc_uint32 i = 0;
    sc_addr idtf_addr, arc_addr;

    SC_ADDR_MAKE_EMPTY(idtf_addr)
    g_assert(sc_keynodes != 0);

    // check if specified system identifier already used
    stream = sc_stream_memory_new(data, sizeof(sc_char) * len, SC_STREAM_FLAG_READ, SC_FALSE);
    if (sc_memory_find_links_with_content(ctx, stream, &results, &results_count) == SC_RESULT_OK)
    {
        for (i = 0; i < results_count; i++)
        {
            it5 = sc_iterator5_a_a_f_a_f_new(ctx,
                                             0,
                                             sc_type_arc_common | sc_type_const,
                                             results[i],
                                             sc_type_arc_pos_const_perm,
                                             sc_keynodes[SC_KEYNODE_NREL_SYSTEM_IDENTIFIER]);
            if (sc_iterator5_next(it5))
            {
                // don't foget to free allocated memory before return error
                sc_iterator5_free(it5);
                sc_stream_free(stream);
                g_free(results);
                return SC_RESULT_ERROR_INVALID_PARAMS;
            }

            sc_iterator5_free(it5);
        }

        g_free(results);
    }

    // if there are no elements with specified system identitifier, then we can use it
    idtf_addr = sc_memory_link_new(ctx);
    if (sc_memory_set_link_content(ctx, idtf_addr, stream) != SC_RESULT_OK)
    {
        sc_stream_free(stream);
        return SC_RESULT_ERROR;
    }

    // we doesn't need link data anymore
    sc_stream_free(stream);

    // setup new system identifier
    arc_addr = sc_memory_arc_new(ctx, sc_type_arc_common | sc_type_const, addr, idtf_addr);
    if (SC_ADDR_IS_EMPTY(arc_addr))
        return SC_RESULT_ERROR;

    arc_addr = sc_memory_arc_new(ctx, sc_type_arc_pos_const_perm, sc_keynodes[SC_KEYNODE_NREL_SYSTEM_IDENTIFIER], arc_addr);
    if (SC_ADDR_IS_EMPTY(arc_addr))
        return SC_RESULT_ERROR;

    return SC_RESULT_OK;
}
void search_subclasses_rec(sc_addr elem, sc_addr answer, sc_bool sys_off)
{
    sc_iterator3 *it2, *it6;
    sc_iterator5 *it5, *it_order;
    sc_type el_type;

    // iterate taxonomy
    it5 = sc_iterator5_f_a_a_a_a_new(s_default_ctx,
                                     elem,
                                     sc_type_arc_common | sc_type_const,
                                     sc_type_node | sc_type_const,
                                     sc_type_arc_pos_const_perm,
                                     sc_type_node | sc_type_const);
    while (sc_iterator5_next(it5) == SC_TRUE)
    {
        if (SC_FALSE == sc_helper_check_arc(s_default_ctx, keynode_taxonomy_relation, sc_iterator5_value(it5, 4), sc_type_arc_pos_const_perm))
            continue;
        if (SC_TRUE == sys_off && (IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 1))
                                   || IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 2))
                                   || IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 3))
                                   || IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 4))))
            continue;

        appendIntoAnswer(answer, sc_iterator5_value(it5, 1));
        appendIntoAnswer(answer, sc_iterator5_value(it5, 2));
        appendIntoAnswer(answer, sc_iterator5_value(it5, 3));
        appendIntoAnswer(answer, sc_iterator5_value(it5, 4));

        search_subclasses_rec(sc_iterator5_value(it5, 2), answer, sys_off);
    }
    sc_iterator5_free(it5);

    // iterate decomposition
    it5 = sc_iterator5_a_a_f_a_a_new(s_default_ctx,
                                     sc_type_node | sc_type_const,
                                     sc_type_arc_common | sc_type_const,
                                     elem,
                                     sc_type_arc_pos_const_perm,
                                     sc_type_node | sc_type_const);
    while (sc_iterator5_next(it5) == SC_TRUE)
    {
        if (SC_FALSE == sc_helper_check_arc(s_default_ctx, keynode_decomposition_relation, sc_iterator5_value(it5, 4), sc_type_arc_pos_const_perm))
            continue;

        if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 0))
                                   || IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 1))
                                   || IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 3))
                                   || IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 4))))
            continue;

        appendIntoAnswer(answer, sc_iterator5_value(it5, 0));
        appendIntoAnswer(answer, sc_iterator5_value(it5, 1));
        appendIntoAnswer(answer, sc_iterator5_value(it5, 3));
        appendIntoAnswer(answer, sc_iterator5_value(it5, 4));

        // iterate decomposition set elements
        it2 = sc_iterator3_f_a_a_new(s_default_ctx,
                                     sc_iterator5_value(it5, 0),
                                     sc_type_arc_pos_const_perm,
                                     0);
        while (sc_iterator3_next(it2) == SC_TRUE)
        {
            if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator3_value(it2, 1))
                                       || IS_SYSTEM_ELEMENT(sc_iterator3_value(it2, 2))))
                continue;

            // iterate order relations between elements
            it_order = sc_iterator5_f_a_a_a_a_new(s_default_ctx,
                                                  sc_iterator3_value(it2, 2),
                                                  sc_type_arc_common | sc_type_const,
                                                  sc_type_node | sc_type_const,
                                                  sc_type_arc_pos_const_perm,
                                                  sc_type_node | sc_type_const);
            while (sc_iterator5_next(it_order) == SC_TRUE)
            {
                if (SC_FALSE == sc_helper_check_arc(s_default_ctx, keynode_order_relation, sc_iterator5_value(it_order, 4), sc_type_arc_pos_const_perm))
                    continue;
                if (SC_FALSE == sc_helper_check_arc(s_default_ctx, sc_iterator5_value(it5, 0), sc_iterator5_value(it_order, 2), sc_type_arc_pos_const_perm))
                    continue;

                if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator5_value(it_order, 1))
                                           || IS_SYSTEM_ELEMENT(sc_iterator5_value(it_order, 2))
                                           || IS_SYSTEM_ELEMENT(sc_iterator5_value(it_order, 3))
                                           || IS_SYSTEM_ELEMENT(sc_iterator5_value(it_order, 4))))
                    continue;

                appendIntoAnswer(answer, sc_iterator5_value(it_order, 1));
                appendIntoAnswer(answer, sc_iterator5_value(it_order, 2));
                appendIntoAnswer(answer, sc_iterator5_value(it_order, 3));
                appendIntoAnswer(answer, sc_iterator5_value(it_order, 4));

            }
            sc_iterator5_free(it_order);

            // iterate roles of element in link
            it6 = sc_iterator3_a_a_f_new(s_default_ctx,
                                         sc_type_node | sc_type_const,
                                         sc_type_arc_pos_const_perm,
                                         sc_iterator3_value(it2, 1));
            while (sc_iterator3_next(it6) == SC_TRUE)
            {
                sc_memory_get_element_type(s_default_ctx, sc_iterator3_value(it6, 0), &el_type);
                if (!(el_type & sc_type_node_role))
                    continue;

                if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator3_value(it6, 0))
                                           || IS_SYSTEM_ELEMENT(sc_iterator3_value(it6, 1))))
                    continue;

                appendIntoAnswer(answer, sc_iterator3_value(it6, 0));
                appendIntoAnswer(answer, sc_iterator3_value(it6, 1));
            }
            sc_iterator3_free(it6);

            appendIntoAnswer(answer, sc_iterator3_value(it2, 1));
            appendIntoAnswer(answer, sc_iterator3_value(it2, 2));

            search_subclasses_rec(sc_iterator3_value(it2, 2), answer, sys_off);
        }
        sc_iterator3_free(it2);
    }
    sc_iterator5_free(it5);
}
sc_result agent_search_decomposition(const sc_event *event, sc_addr arg)
{
    sc_addr question, answer;
    sc_iterator3 *it1, *it2, *it3;
    sc_iterator5 *it5, *it_order;
    sc_bool sys_off = SC_TRUE;
    sc_type el_type;

    if (!sc_memory_get_arc_end(s_default_ctx, arg, &question))
        return SC_RESULT_ERROR_INVALID_PARAMS;

    // check question type
    if (sc_helper_check_arc(s_default_ctx, keynode_question_decomposition, question, sc_type_arc_pos_const_perm) == SC_FALSE)
        return SC_RESULT_ERROR_INVALID_TYPE;

    answer = create_answer_node();

    // get operation argument
    it1 = sc_iterator3_f_a_a_new(s_default_ctx,
                                 question,
                                 sc_type_arc_pos_const_perm,
                                 0);
    if (sc_iterator3_next(it1) == SC_TRUE)
    {
        if (IS_SYSTEM_ELEMENT(sc_iterator3_value(it1, 2)))
            sys_off = SC_FALSE;

        appendIntoAnswer(answer, sc_iterator3_value(it1, 2));

        // iterate decomposition
        it5 = sc_iterator5_a_a_f_a_a_new(s_default_ctx,
                                         sc_type_node | sc_type_const,
                                         sc_type_arc_common | sc_type_const,
                                         sc_iterator3_value(it1, 2),
                                         sc_type_arc_pos_const_perm,
                                         sc_type_node | sc_type_const);
        while (sc_iterator5_next(it5) == SC_TRUE)
        {
            if (SC_FALSE == sc_helper_check_arc(s_default_ctx, keynode_decomposition_relation, sc_iterator5_value(it5, 4), sc_type_arc_pos_const_perm))
                continue;
            if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 0))
                                       || IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 1))
                                       || IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 3))
                                       || IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 4))))
                continue;

            appendIntoAnswer(answer, sc_iterator5_value(it5, 0));
            appendIntoAnswer(answer, sc_iterator5_value(it5, 1));
            appendIntoAnswer(answer, sc_iterator5_value(it5, 3));
            appendIntoAnswer(answer, sc_iterator5_value(it5, 4));

            // iterate decomposition set elements
            it2 = sc_iterator3_f_a_a_new(s_default_ctx,
                                         sc_iterator5_value(it5, 0),
                                         sc_type_arc_pos_const_perm,
                                         0);
            while (sc_iterator3_next(it2) == SC_TRUE)
            {
                if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator3_value(it2, 1))
                                           || IS_SYSTEM_ELEMENT(sc_iterator3_value(it2, 2))))
                    continue;

                // iterate order relations between elements
                it_order = sc_iterator5_f_a_a_a_a_new(s_default_ctx,
                                                      sc_iterator3_value(it2, 2),
                                                      sc_type_arc_common | sc_type_const,
                                                      sc_type_node | sc_type_const,
                                                      sc_type_arc_pos_const_perm,
                                                      sc_type_node | sc_type_const);
                while (sc_iterator5_next(it_order) == SC_TRUE)
                {
                    if (SC_FALSE == sc_helper_check_arc(s_default_ctx, keynode_order_relation, sc_iterator5_value(it_order, 4), sc_type_arc_pos_const_perm))
                        continue;
                    if (SC_FALSE == sc_helper_check_arc(s_default_ctx, sc_iterator5_value(it5, 0), sc_iterator5_value(it_order, 2), sc_type_arc_pos_const_perm))
                        continue;

                    if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator5_value(it_order, 1))
                                               || IS_SYSTEM_ELEMENT(sc_iterator5_value(it_order, 2))
                                               || IS_SYSTEM_ELEMENT(sc_iterator5_value(it_order, 3))
                                               || IS_SYSTEM_ELEMENT(sc_iterator5_value(it_order, 4))))
                        continue;

                    appendIntoAnswer(answer, sc_iterator5_value(it_order, 1));
                    appendIntoAnswer(answer, sc_iterator5_value(it_order, 2));
                    appendIntoAnswer(answer, sc_iterator5_value(it_order, 3));
                    appendIntoAnswer(answer, sc_iterator5_value(it_order, 4));

                }
                sc_iterator5_free(it_order);

                // iterate roles of element in link
                it3 = sc_iterator3_a_a_f_new(s_default_ctx,
                                             sc_type_node | sc_type_const,
                                             sc_type_arc_pos_const_perm,
                                             sc_iterator3_value(it2, 1));
                while (sc_iterator3_next(it3) == SC_TRUE)
                {
                    sc_memory_get_element_type(s_default_ctx, sc_iterator3_value(it3, 0), &el_type);
                    if (!(el_type & sc_type_node_role))
                        continue;

                    if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator3_value(it3, 0))
                                               || IS_SYSTEM_ELEMENT(sc_iterator3_value(it3, 1))))
                        continue;

                    appendIntoAnswer(answer, sc_iterator3_value(it3, 0));
                    appendIntoAnswer(answer, sc_iterator3_value(it3, 1));
                }
                sc_iterator3_free(it3);

                appendIntoAnswer(answer, sc_iterator3_value(it2, 1));
                appendIntoAnswer(answer, sc_iterator3_value(it2, 2));
            }
            sc_iterator3_free(it2);
        }
        sc_iterator5_free(it5);
    }
    sc_iterator3_free(it1);

    connect_answer_to_question(question, answer);
    finish_question(question);

    return SC_RESULT_OK;
}
void search_superclasses_rec(sc_addr elem, sc_addr answer, sc_bool sys_off)
{
    sc_iterator3 *it3;
    sc_iterator5 *it5;

    // search taxonomy
    it5 = sc_iterator5_a_a_f_a_a_new(s_default_ctx,
                                     sc_type_node | sc_type_const,
                                     sc_type_arc_common | sc_type_const,
                                     elem,
                                     sc_type_arc_pos_const_perm,
                                     sc_type_node | sc_type_const);
    while (sc_iterator5_next(it5) == SC_TRUE)
    {
        if (SC_FALSE == sc_helper_check_arc(s_default_ctx, keynode_taxonomy_relation, sc_iterator5_value(it5, 4), sc_type_arc_pos_const_perm))
            continue;
        if (SC_TRUE == sys_off && (IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 0))
                                   || IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 1))
                                   || IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 3))
                                   || IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 4))))
            continue;

        appendIntoAnswer(answer, sc_iterator5_value(it5, 0));
        appendIntoAnswer(answer, sc_iterator5_value(it5, 1));
        appendIntoAnswer(answer, sc_iterator5_value(it5, 3));
        appendIntoAnswer(answer, sc_iterator5_value(it5, 4));

        search_superclasses_rec(sc_iterator5_value(it5, 0), answer, sys_off);
    }
    sc_iterator5_free(it5);

    // iterate input arcs
    it3 = sc_iterator3_a_a_f_new(s_default_ctx,
                                 sc_type_node | sc_type_const,
                                 sc_type_arc_pos_const_perm,
                                 elem);
    while (sc_iterator3_next(it3) == SC_TRUE)
    {
        // search all parents in quasybinary relation
        it5 = sc_iterator5_f_a_a_a_a_new(s_default_ctx,
                                         sc_iterator3_value(it3, 0),
                                         sc_type_arc_common | sc_type_const,
                                         sc_type_node | sc_type_const,
                                         sc_type_arc_pos_const_perm,
                                         sc_type_node | sc_type_const);
        while (sc_iterator5_next(it5) == SC_TRUE)
        {
            // check if it's a quasybinary relation
            if (sc_helper_check_arc(s_default_ctx, keynode_quasybinary_relation, sc_iterator5_value(it5, 4), sc_type_arc_pos_const_perm) == SC_TRUE)
            {
                if (!(sc_helper_check_arc(s_default_ctx, keynode_taxonomy_relation, sc_iterator5_value(it5, 4), sc_type_arc_pos_const_perm) == SC_TRUE
                      || sc_helper_check_arc(s_default_ctx, keynode_decomposition_relation, sc_iterator5_value(it5, 4), sc_type_arc_pos_const_perm) == SC_TRUE))
                    continue;


                if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 1))
                                           || IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 2))
                                           || IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 3))
                                           || IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 4))
                                           || IS_SYSTEM_ELEMENT(sc_iterator3_value(it3, 0))
                                           || IS_SYSTEM_ELEMENT(sc_iterator3_value(it3, 1))))
                    continue;


                appendIntoAnswer(answer, sc_iterator5_value(it5, 1));
                appendIntoAnswer(answer, sc_iterator5_value(it5, 2));
                appendIntoAnswer(answer, sc_iterator5_value(it5, 3));
                appendIntoAnswer(answer, sc_iterator5_value(it5, 4));

                appendIntoAnswer(answer, sc_iterator3_value(it3, 0));
                appendIntoAnswer(answer, sc_iterator3_value(it3, 1));

                search_superclasses_rec(sc_iterator5_value(it5, 2), answer, sys_off);
            }
        }
        sc_iterator5_free(it5);

    }
    sc_iterator3_free(it3);
}
void scp_iterator5_free(scp_iterator5 *iter)
{
    sc_iterator5_free(iter);
}
sc_result agent_search_links_of_relation_connected_with_element(const sc_event *event, sc_addr arg)
{
    sc_addr question, answer, param_elem, param_rel;
    sc_iterator3 *it1, *it2, *it3, *it4;
    sc_iterator5 *it5, *it_order;
    sc_type el_type;
    sc_bool sys_off = SC_TRUE;
    sc_bool param_elem_found = SC_FALSE, param_rel_found = SC_FALSE, found = SC_FALSE;

    if (!sc_memory_get_arc_end(arg, &question))
        return SC_RESULT_ERROR_INVALID_PARAMS;

    // check question type
    if (sc_helper_check_arc(keynode_question_search_links_of_relation_connected_with_element, question, sc_type_arc_pos_const_perm) == SC_FALSE)
        return SC_RESULT_ERROR_INVALID_TYPE;

    answer = create_answer_node();

    // get question arguments
    it5 = sc_iterator5_f_a_a_a_a_new(question,
                                     sc_type_arc_pos_const_perm,
                                     0,
                                     sc_type_arc_pos_const_perm,
                                     sc_type_node | sc_type_const);
    while (sc_iterator5_next(it5) == SC_TRUE)
    {
        if (SC_ADDR_IS_EQUAL(sc_iterator5_value(it5, 4), keynode_rrel_1))
        {
            param_elem = sc_iterator5_value(it5, 2);
            param_elem_found = SC_TRUE;
            continue;
        }
        if (SC_ADDR_IS_EQUAL(sc_iterator5_value(it5, 4), keynode_rrel_2))
        {
            param_rel = sc_iterator5_value(it5, 2);
            param_rel_found = SC_TRUE;
            continue;
        }
    }
    sc_iterator5_free(it5);
    if (param_elem_found == SC_FALSE || param_rel_found == SC_FALSE)
    {
        return SC_RESULT_ERROR;
    }

    appendIntoAnswer(answer, param_elem);

    if (IS_SYSTEM_ELEMENT(param_elem) || IS_SYSTEM_ELEMENT(param_rel))
        sys_off = SC_FALSE;

    if (SC_TRUE == sc_helper_check_arc(keynode_quasybinary_relation, param_rel, sc_type_arc_pos_const_perm))
    {
        // Search subclasses in quasybinary relation
        // Iterate input arcs of quasybinary relation
        it5 = sc_iterator5_a_a_f_a_f_new(sc_type_const,
                                         sc_type_const | sc_type_arc_common,
                                         param_elem,
                                         sc_type_arc_pos_const_perm,
                                         param_rel);
        while (sc_iterator5_next(it5) == SC_TRUE)
        {
            if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 0))
                                       || IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 1))
                                       || IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 3))))
                continue;

            found = SC_TRUE;

            appendIntoAnswer(answer, sc_iterator5_value(it5, 0));
            appendIntoAnswer(answer, sc_iterator5_value(it5, 1));
            appendIntoAnswer(answer, sc_iterator5_value(it5, 3));

            search_arc_components(sc_iterator5_value(it5, 0), answer, sys_off);

            // Iterate subclasses in quasybinary relation
            it1 = sc_iterator3_f_a_a_new(sc_iterator5_value(it5, 0),
                                         sc_type_arc_pos_const_perm,
                                         sc_type_const | sc_type_node);
            while (sc_iterator3_next(it1) == SC_TRUE)
            {
                if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator3_value(it1, 1))
                                           || IS_SYSTEM_ELEMENT(sc_iterator3_value(it1, 2))))
                    continue;

                appendIntoAnswer(answer, sc_iterator3_value(it1, 1));
                appendIntoAnswer(answer, sc_iterator3_value(it1, 2));

                search_arc_components(sc_iterator3_value(it1, 2), answer, sys_off);

                // iterate order relations between elements
                it_order = sc_iterator5_f_a_a_a_a_new(sc_iterator3_value(it1, 2),
                                                      sc_type_arc_common | sc_type_const,
                                                      sc_type_node | sc_type_const,
                                                      sc_type_arc_pos_const_perm,
                                                      sc_type_node | sc_type_const);
                while (sc_iterator5_next(it_order) == SC_TRUE)
                {
                    if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator5_value(it_order, 1)) || IS_SYSTEM_ELEMENT(sc_iterator5_value(it_order, 2))
                                               || IS_SYSTEM_ELEMENT(sc_iterator5_value(it_order, 3)) || IS_SYSTEM_ELEMENT(sc_iterator5_value(it_order, 4))))
                        continue;

                    if (SC_FALSE == sc_helper_check_arc(keynode_order_relation, sc_iterator5_value(it_order, 4), sc_type_arc_pos_const_perm))
                        continue;
                    if (SC_FALSE == sc_helper_check_arc(sc_iterator5_value(it5, 0), sc_iterator5_value(it_order, 2), sc_type_arc_pos_const_perm))
                        continue;

                    appendIntoAnswer(answer, sc_iterator5_value(it_order, 1));
                    appendIntoAnswer(answer, sc_iterator5_value(it_order, 2));
                    appendIntoAnswer(answer, sc_iterator5_value(it_order, 3));
                    appendIntoAnswer(answer, sc_iterator5_value(it_order, 4));
                }
                sc_iterator5_free(it_order);

                // iterate roles of element in link
                it2 = sc_iterator3_a_a_f_new(sc_type_node | sc_type_const,
                                             sc_type_arc_pos_const_perm,
                                             sc_iterator3_value(it1, 1));
                while (sc_iterator3_next(it2) == SC_TRUE)
                {
                    sc_memory_get_element_type(sc_iterator3_value(it2, 0), &el_type);
                    if (!(el_type & sc_type_node_role))
                        continue;

                    if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator3_value(it2, 0))
                                               || IS_SYSTEM_ELEMENT(sc_iterator3_value(it2, 1))))
                        continue;

                    appendIntoAnswer(answer, sc_iterator3_value(it2, 0));
                    appendIntoAnswer(answer, sc_iterator3_value(it2, 1));
                }
                sc_iterator3_free(it2);
            }
            sc_iterator3_free(it1);
        }
        sc_iterator5_free(it5);

        // Iterate input arcs of quasybinary relation
        it1 = sc_iterator3_a_a_f_new(sc_type_const | sc_type_node,
                                     sc_type_arc_pos_const_perm,
                                     param_elem);
        while (sc_iterator3_next(it1) == SC_TRUE)
        {
            if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator3_value(it1, 0))
                                       || IS_SYSTEM_ELEMENT(sc_iterator3_value(it1, 1))))
                continue;

            // search all parents in quasybinary relation
            it5 = sc_iterator5_f_a_a_a_f_new(sc_iterator3_value(it1, 0),
                                             sc_type_arc_common | sc_type_const,
                                             sc_type_node | sc_type_const,
                                             sc_type_arc_pos_const_perm,
                                             param_rel);
            if (sc_iterator5_next(it5) == SC_TRUE)
            {
                if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 1))
                                           || IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 2))
                                           || IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 3))))
                    continue;

                appendIntoAnswer(answer, sc_iterator5_value(it5, 1));
                appendIntoAnswer(answer, sc_iterator5_value(it5, 2));
                appendIntoAnswer(answer, sc_iterator5_value(it5, 3));

                search_arc_components(sc_iterator5_value(it5, 2), answer, sys_off);

                appendIntoAnswer(answer, sc_iterator3_value(it1, 0));
                appendIntoAnswer(answer, sc_iterator3_value(it1, 1));

                search_arc_components(sc_iterator3_value(it1, 0), answer, sys_off);
            }
            sc_iterator5_free(it5);
        }
        sc_iterator3_free(it1);
    }
    else
    {
        // Iterate output arcs of given relation
        it5 = sc_iterator5_f_a_a_a_f_new(param_elem,
                                         sc_type_const,
                                         sc_type_const,
                                         sc_type_arc_pos_const_perm,
                                         param_rel);
        while (sc_iterator5_next(it5) == SC_TRUE)
        {
            if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 1))
                                       || IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 2))
                                       || IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 3))))
                continue;

            found = SC_TRUE;

            appendIntoAnswer(answer, sc_iterator5_value(it5, 1));
            appendIntoAnswer(answer, sc_iterator5_value(it5, 2));
            appendIntoAnswer(answer, sc_iterator5_value(it5, 3));

            search_arc_components(sc_iterator5_value(it5, 2), answer, sys_off);
        }
        sc_iterator5_free(it5);

        // Iterate input arcs of given relation
        it5 = sc_iterator5_a_a_f_a_f_new(sc_type_const,
                                         sc_type_const,
                                         param_elem,
                                         sc_type_arc_pos_const_perm,
                                         param_rel);
        while (sc_iterator5_next(it5) == SC_TRUE)
        {
            if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 0))
                                       || IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 1))
                                       || IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 3))))
                continue;

            appendIntoAnswer(answer, sc_iterator5_value(it5, 0));
            appendIntoAnswer(answer, sc_iterator5_value(it5, 1));
            appendIntoAnswer(answer, sc_iterator5_value(it5, 3));

            search_arc_components(sc_iterator5_value(it5, 0), answer, sys_off);
        }
        sc_iterator5_free(it5);

        // Iterate input arcs for input element
        it1 = sc_iterator3_a_a_f_new(sc_type_const,
                                     sc_type_arc_pos_const_perm,
                                     param_elem);
        while (sc_iterator3_next(it1) == SC_TRUE)
        {
            if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator3_value(it1, 0))
                                       || IS_SYSTEM_ELEMENT(sc_iterator3_value(it1, 1))))
                continue;

            // Iterate input arcs for input element
            it2 = sc_iterator3_f_a_f_new(param_rel,
                                         sc_type_arc_pos_const_perm,
                                         sc_iterator3_value(it1, 0));
            if (sc_iterator3_next(it2) == SC_TRUE)
            {
                if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator3_value(it2, 1))))
                    continue;

                appendIntoAnswer(answer, sc_iterator3_value(it2, 1));
                appendIntoAnswer(answer, sc_iterator3_value(it1, 0));

                // Iterate elements of fonnd link of given relation
                it3 = sc_iterator3_f_a_a_new(sc_iterator3_value(it1, 0),
                                             sc_type_arc_pos_const_perm,
                                             sc_type_const);
                while (sc_iterator3_next(it3) == SC_TRUE)
                {
                    if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator3_value(it3, 1))
                                               || IS_SYSTEM_ELEMENT(sc_iterator3_value(it3, 2))))
                        continue;

                    appendIntoAnswer(answer, sc_iterator3_value(it3, 1));
                    appendIntoAnswer(answer, sc_iterator3_value(it3, 2));

                    search_arc_components(sc_iterator3_value(it3, 2), answer, sys_off);

                    // Iterate role relations
                    it4 = sc_iterator3_a_a_f_new(sc_type_const | sc_type_node,
                                                 sc_type_arc_pos_const_perm,
                                                 sc_iterator3_value(it3, 1));
                    while (sc_iterator3_next(it4) == SC_TRUE)
                    {
                        if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator3_value(it4, 0))
                                                   || IS_SYSTEM_ELEMENT(sc_iterator3_value(it4, 1))))
                            continue;

                        appendIntoAnswer(answer, sc_iterator3_value(it4, 0));
                        appendIntoAnswer(answer, sc_iterator3_value(it4, 1));

                        search_arc_components(sc_iterator3_value(it4, 0), answer, sys_off);
                    }
                    sc_iterator3_free(it4);
                }
                sc_iterator3_free(it3);
            }
            sc_iterator3_free(it2);
        }
        sc_iterator3_free(it1);
    }

    if (found == SC_TRUE)
    {
        appendIntoAnswer(answer, param_rel);
    }

    connect_answer_to_question(question, answer);
    finish_question(question);

    return SC_RESULT_OK;
}
void search_translation(sc_addr elem, sc_addr answer, sc_bool sys_off)
{
    sc_iterator5 *it5;
    sc_iterator3 *it3, *it4;
    sc_bool found = SC_FALSE;

    // iterate translations of sc-element
    it5 = sc_iterator5_a_a_f_a_f_new(sc_type_node | sc_type_const,
                                     sc_type_arc_common | sc_type_const,
                                     elem,
                                     sc_type_arc_pos_const_perm,
                                     keynode_nrel_translation);
    while (sc_iterator5_next(it5) == SC_TRUE)
    {
        found = SC_TRUE;

        if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 0))
                                   || IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 1))
                                   || IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 3))))
            continue;

        appendIntoAnswer(answer, sc_iterator5_value(it5, 0));
        appendIntoAnswer(answer, sc_iterator5_value(it5, 1));
        appendIntoAnswer(answer, sc_iterator5_value(it5, 3));

        // iterate translation sc-links
        it3 = sc_iterator3_f_a_a_new(sc_iterator5_value(it5, 0),
                                     sc_type_arc_pos_const_perm,
                                     0);
        while (sc_iterator3_next(it3) == SC_TRUE)
        {
            if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator3_value(it3, 1)) || IS_SYSTEM_ELEMENT(sc_iterator3_value(it3, 2))))
                continue;

            // iterate input arcs for link
            it4 = sc_iterator3_a_a_f_new(sc_type_node,
                                         sc_type_arc_pos_const_perm,
                                         sc_iterator3_value(it3, 2));
            while (sc_iterator3_next(it4) == SC_TRUE)
            {
                if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator3_value(it4, 1)) || IS_SYSTEM_ELEMENT(sc_iterator3_value(it4, 0))))
                    continue;
                if (sc_helper_check_arc(keynode_languages, sc_iterator3_value(it4, 0), sc_type_arc_pos_const_perm) == SC_TRUE)
                {
                    appendIntoAnswer(answer, sc_iterator3_value(it4, 0));
                    appendIntoAnswer(answer, sc_iterator3_value(it4, 1));
                }
            }
            sc_iterator3_free(it4);

            // iterate input arcs for arc
            it4 = sc_iterator3_a_a_f_new(sc_type_node,
                                         sc_type_arc_pos_const_perm,
                                         sc_iterator3_value(it3, 1));
            while (sc_iterator3_next(it4) == SC_TRUE)
            {
                if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator3_value(it4, 0)) || IS_SYSTEM_ELEMENT(sc_iterator3_value(it4, 1))))
                    continue;

                appendIntoAnswer(answer, sc_iterator3_value(it4, 0));
                appendIntoAnswer(answer, sc_iterator3_value(it4, 1));
            }
            sc_iterator3_free(it4);

            appendIntoAnswer(answer, sc_iterator3_value(it3, 1));
            appendIntoAnswer(answer, sc_iterator3_value(it3, 2));
        }
        sc_iterator3_free(it3);

    }
    sc_iterator5_free(it5);

    if (found == SC_TRUE)
    {
        appendIntoAnswer(answer, keynode_nrel_translation);
    }
}
sc_result agent_search_full_semantic_neighborhood(const sc_event *event, sc_addr arg)
{
    sc_addr question, answer;
    sc_iterator3 *it1, *it2, *it3, *it4, *it6;
    sc_iterator5 *it5, *it_order, *it_order2;
    sc_type el_type;
    sc_bool sys_off = SC_TRUE;
    sc_bool key_order_found = SC_FALSE;

    if (!sc_memory_get_arc_end(arg, &question))
        return SC_RESULT_ERROR_INVALID_PARAMS;

    // check question type
    if (sc_helper_check_arc(keynode_question_full_semantic_neighborhood, question, sc_type_arc_pos_const_perm) == SC_FALSE)
        return SC_RESULT_ERROR_INVALID_TYPE;

    answer = create_answer_node();

    // get question argument
    it1 = sc_iterator3_f_a_a_new(question, sc_type_arc_pos_const_perm, 0);
    if (sc_iterator3_next(it1) == SC_TRUE)
    {
        if (IS_SYSTEM_ELEMENT(sc_iterator3_value(it1, 2)))
            sys_off = SC_FALSE;

        appendIntoAnswer(answer, sc_iterator3_value(it1, 2));

        search_translation(sc_iterator3_value(it1, 2), answer, sys_off);
        search_arc_components(sc_iterator3_value(it1, 2), answer, sys_off);

        // iterate input arcs
        it2 = sc_iterator3_a_a_f_new(0,
                                     0,
                                     sc_iterator3_value(it1, 2));
        while (sc_iterator3_next(it2) == SC_TRUE)
        {
            if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator3_value(it2, 0)) || IS_SYSTEM_ELEMENT(sc_iterator3_value(it2, 1))))
                continue;

            appendIntoAnswer(answer, sc_iterator3_value(it2, 0));
            appendIntoAnswer(answer, sc_iterator3_value(it2, 1));

            search_arc_components(sc_iterator3_value(it2, 0), answer, sys_off);

            // iterate input arcs into found arc, to find relations
            it3 = sc_iterator3_a_a_f_new(sc_type_node,
                                         sc_type_arc_pos_const_perm,
                                         sc_iterator3_value(it2, 1));
            while (sc_iterator3_next(it3) == SC_TRUE)
            {
                sc_memory_get_element_type(sc_iterator3_value(it3, 0), &el_type);
                if (!(el_type & (sc_type_node_norole | sc_type_node_role)))
                    continue;

                if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator3_value(it3, 1)) || IS_SYSTEM_ELEMENT(sc_iterator3_value(it3, 0))))
                    continue;

                appendIntoAnswer(answer, sc_iterator3_value(it3, 0));
                appendIntoAnswer(answer, sc_iterator3_value(it3, 1));

                search_arc_components(sc_iterator3_value(it3, 0), answer, sys_off);

                // search typical sc-neighborhood if necessary
                if (SC_ADDR_IS_EQUAL(keynode_rrel_key_sc_element, sc_iterator3_value(it3, 0)))
                {
                    search_typical_sc_neighborhood(sc_iterator3_value(it2, 0), answer, sys_off);
                }

                // check if it's a quasy binary relation
                if (sc_helper_check_arc(keynode_quasybinary_relation, sc_iterator3_value(it3, 0), sc_type_arc_pos_const_perm) == SC_TRUE)
                {
                    // iterate elements of relation
                    it4 = sc_iterator3_f_a_a_new(sc_iterator3_value(it2, 0), sc_type_arc_pos_const_perm, 0);
                    while (sc_iterator3_next(it4) == SC_TRUE)
                    {
                        if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator3_value(it4, 1)) || IS_SYSTEM_ELEMENT(sc_iterator3_value(it4, 2))))
                            continue;

                        appendIntoAnswer(answer, sc_iterator3_value(it4, 1));
                        appendIntoAnswer(answer, sc_iterator3_value(it4, 2));

                        search_arc_components(sc_iterator3_value(it4, 2), answer, sys_off);

                        // iterate order relations between elements
                        it_order = sc_iterator5_f_a_a_a_a_new(sc_iterator3_value(it4, 2),
                                                              sc_type_arc_common | sc_type_const,
                                                              sc_type_node | sc_type_const,
                                                              sc_type_arc_pos_const_perm,
                                                              sc_type_node | sc_type_const);
                        while (sc_iterator5_next(it_order) == SC_TRUE)
                        {
                            if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator5_value(it_order, 1)) || IS_SYSTEM_ELEMENT(sc_iterator5_value(it_order, 2))
                                                       || IS_SYSTEM_ELEMENT(sc_iterator5_value(it_order, 3)) || IS_SYSTEM_ELEMENT(sc_iterator5_value(it_order, 4))))
                                continue;

                            if (SC_FALSE == sc_helper_check_arc(keynode_order_relation, sc_iterator5_value(it_order, 4), sc_type_arc_pos_const_perm))
                                continue;
                            if (SC_FALSE == sc_helper_check_arc(sc_iterator3_value(it2, 0), sc_iterator5_value(it_order, 2), sc_type_arc_pos_const_perm))
                                continue;

                            appendIntoAnswer(answer, sc_iterator5_value(it_order, 1));
                            appendIntoAnswer(answer, sc_iterator5_value(it_order, 2));
                            appendIntoAnswer(answer, sc_iterator5_value(it_order, 3));
                            appendIntoAnswer(answer, sc_iterator5_value(it_order, 4));

                        }
                        sc_iterator5_free(it_order);

                        // iterate roles of element in link
                        it6 = sc_iterator3_a_a_f_new(sc_type_node | sc_type_const,
                                                     sc_type_arc_pos_const_perm,
                                                     sc_iterator3_value(it4, 1));
                        while (sc_iterator3_next(it6) == SC_TRUE)
                        {
                            sc_memory_get_element_type(sc_iterator3_value(it6, 0), &el_type);
                            if (!(el_type & sc_type_node_role))
                                continue;

                            if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator3_value(it6, 0))
                                                       || IS_SYSTEM_ELEMENT(sc_iterator3_value(it6, 1))))
                                continue;

                            appendIntoAnswer(answer, sc_iterator3_value(it6, 0));
                            appendIntoAnswer(answer, sc_iterator3_value(it6, 1));

                            search_arc_components(sc_iterator3_value(it6, 0), answer, sys_off);
                        }
                        sc_iterator3_free(it6);

                    }
                    sc_iterator3_free(it4);
                }
            }
            sc_iterator3_free(it3);

            // search all parents in quasybinary relation
            it5 = sc_iterator5_f_a_a_a_a_new(sc_iterator3_value(it2, 0),
                                             sc_type_arc_common | sc_type_const,
                                             sc_type_node | sc_type_const,
                                             sc_type_arc_pos_const_perm,
                                             sc_type_node | sc_type_const);
            while (sc_iterator5_next(it5) == SC_TRUE)
            {
                // check if it's a quasy binary relation
                if (sc_helper_check_arc(keynode_quasybinary_relation, sc_iterator5_value(it5, 4), sc_type_arc_pos_const_perm) == SC_TRUE)
                {
                    if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 1))
                                               || IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 2))
                                               || IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 3))
                                               || IS_SYSTEM_ELEMENT(sc_iterator5_value(it5, 4))))
                        continue;

                    appendIntoAnswer(answer, sc_iterator5_value(it5, 1));
                    appendIntoAnswer(answer, sc_iterator5_value(it5, 2));
                    appendIntoAnswer(answer, sc_iterator5_value(it5, 3));
                    appendIntoAnswer(answer, sc_iterator5_value(it5, 4));

                    search_arc_components(sc_iterator5_value(it5, 2), answer, sys_off);
                }
            }
            sc_iterator5_free(it5);

            // search translation for element
            search_translation(sc_iterator3_value(it2, 0), answer, sys_off);

            // search non-binary relation link
            search_nonbinary_relation(sc_iterator3_value(it2, 0), answer, sys_off);
        }
        sc_iterator3_free(it2);

        // iterate output arcs
        it2 = sc_iterator3_f_a_a_new(sc_iterator3_value(it1, 2),
                                     0,
                                     0);
        while (sc_iterator3_next(it2) == SC_TRUE)
        {
            if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator3_value(it2, 1)) || IS_SYSTEM_ELEMENT(sc_iterator3_value(it2, 2))))
                continue;

            appendIntoAnswer(answer, sc_iterator3_value(it2, 1));
            appendIntoAnswer(answer, sc_iterator3_value(it2, 2));

            search_arc_components(sc_iterator3_value(it2, 2), answer, sys_off);

            // iterate input arcs into found arc, to find relations
            it3 = sc_iterator3_a_a_f_new(sc_type_node,
                                         sc_type_arc_pos_const_perm,
                                         sc_iterator3_value(it2, 1));
            while (sc_iterator3_next(it3) == SC_TRUE)
            {
                sc_memory_get_element_type(sc_iterator3_value(it3, 0), &el_type);
                if (!(el_type & (sc_type_node_norole | sc_type_node_role)))
                    continue;

                if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator3_value(it3, 1)) || IS_SYSTEM_ELEMENT(sc_iterator3_value(it3, 0))))
                    continue;

                appendIntoAnswer(answer, sc_iterator3_value(it3, 0));
                appendIntoAnswer(answer, sc_iterator3_value(it3, 1));

                // search of key sc-elements order
                if (SC_ADDR_IS_EQUAL(sc_iterator3_value(it3, 0), keynode_rrel_key_sc_element))
                {
                    it_order2 = sc_iterator5_f_a_a_a_f_new(sc_iterator3_value(it2, 1),
                                                           sc_type_arc_common | sc_type_const,
                                                           sc_type_arc_pos_const_perm,
                                                           sc_type_arc_pos_const_perm,
                                                           keynode_nrel_key_sc_element_base_order);
                    while (sc_iterator5_next(it_order2) == SC_TRUE)
                    {
                        if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator5_value(it_order2, 1)) || IS_SYSTEM_ELEMENT(sc_iterator5_value(it_order2, 3))))
                            continue;

                        appendIntoAnswer(answer, sc_iterator5_value(it_order2, 1));
                        appendIntoAnswer(answer, sc_iterator5_value(it_order2, 3));
                        if (SC_FALSE == key_order_found)
                        {
                            key_order_found = SC_TRUE;
                            appendIntoAnswer(answer, keynode_nrel_key_sc_element_base_order);
                        }
                    }
                    sc_iterator5_free(it_order2);
                }

                // search translation for element
                search_translation(sc_iterator3_value(it3, 0), answer, sys_off);
            }
            sc_iterator3_free(it3);

            // search translation for element
            search_translation(sc_iterator3_value(it2, 2), answer, sys_off);

            // check if element is an sc-link
            if (SC_RESULT_OK == sc_memory_get_element_type(sc_iterator3_value(it2, 2), &el_type) &&
                (el_type | sc_type_link))
            {
                // iterate input arcs for link
                it3 = sc_iterator3_a_a_f_new(sc_type_node | sc_type_const,
                                             sc_type_arc_pos_const_perm,
                                             sc_iterator3_value(it2, 2));
                while (sc_iterator3_next(it3) == SC_TRUE)
                {
                    if (sc_helper_check_arc(keynode_languages, sc_iterator3_value(it3, 0), sc_type_arc_pos_const_perm) == SC_TRUE)
                    {
                        if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator3_value(it3, 1)) || IS_SYSTEM_ELEMENT(sc_iterator3_value(it3, 0))))
                            continue;

                        appendIntoAnswer(answer, sc_iterator3_value(it3, 0));
                        appendIntoAnswer(answer, sc_iterator3_value(it3, 1));

                        search_arc_components(sc_iterator3_value(it3, 0), answer, sys_off);
                    }
                }
                sc_iterator3_free(it3);
            }
        }
        sc_iterator3_free(it2);

    }
    sc_iterator3_free(it1);

    connect_answer_to_question(question, answer);
    finish_question(question);

    return SC_RESULT_OK;
}