예제 #1
0
scp_result searchSetStr3_f_a_a(sc_memory_context *context, scp_operand *param1, scp_operand *param2, scp_operand *param3, scp_operand *sets)
{
    scp_result res = SCP_RESULT_FALSE;
    if (sets[1].set == SCP_TRUE && sets[1].param_type == SCP_ASSIGN)
    {
        sets[1].addr = sc_memory_node_new(context, sc_type_node | sc_type_const);
    }
    if (sets[2].set == SCP_TRUE && sets[2].param_type == SCP_ASSIGN)
    {
        sets[2].addr = sc_memory_node_new(context, sc_type_node | sc_type_const);
    }
    sc_iterator3 *it = sc_iterator3_f_a_a_new(context, param1->addr, param2->element_type, param3->element_type);
    while (SC_TRUE == sc_iterator3_next(it))
    {
        res = SCP_RESULT_TRUE;
        param2->addr = sc_iterator3_value(it, 1);
        param3->addr = sc_iterator3_value(it, 2);
        if (sets[1].set == SCP_TRUE)
        {
            sc_memory_arc_new(context, sc_type_arc_pos_const_perm, sets[1].addr, param2->addr);
        }
        if (sets[2].set == SCP_TRUE)
        {
            sc_memory_arc_new(context, sc_type_arc_pos_const_perm, sets[2].addr, param3->addr);
        }
    }
    sc_iterator3_free(it);
    return res;
}
예제 #2
0
sc_result question_search_all_output_arcs(sc_event *event, sc_addr arg)
{
    sc_addr question_node;
    sc_addr question_argument;
    sc_addr answer_node;
    sc_addr value;
    sc_bool arg_exist = SC_FALSE;
    sc_iterator3 *it = 0;
    sc_uint32 i = 0;

    if (sc_memory_get_arc_end(arg, &question_node) != SC_RESULT_OK)
        return SC_RESULT_ERROR;

    // check question type
//    if (sc_helper_check_arc(question_) == SC_FALSE)
//        return SC_RESULT_ERROR_INVALID_PARAMS;

    // get question argument
    it = sc_iterator3_f_a_a_new(question_node, sc_type_arc_pos_const_perm, 0);
    while (sc_iterator3_next(it))
    {
        question_argument = sc_iterator3_value(it, 2);
        arg_exist = SC_TRUE;
    }

    sc_iterator3_free(it);

    // make answer
    answer_node = sc_memory_node_new(sc_type_node | sc_type_const);

    if (arg_exist == SC_TRUE)
    {
        // iterate all output arcs and append them into result
        it = sc_iterator3_f_a_a_new(question_argument, 0, 0);
        while (sc_iterator3_next(it))
        {
            for (i = 0; i < 3; ++i)
            {
                value = sc_iterator3_value(it, i);
                sc_memory_arc_new(sc_type_arc_pos_const_perm, answer_node, value);
            }
        }
        sc_iterator3_free(it);
    }

    // connect question with answer
    value = sc_memory_arc_new(sc_type_arc_common | sc_type_const, question_node, answer_node);
    sc_memory_arc_new(sc_type_arc_pos_const_perm, q_keynode_question_nrel_answer, value);

    // change question state
    sc_memory_element_free(arg);
    sc_memory_arc_new(sc_type_arc_pos_const_perm, q_keynode_question_finished, question_node);

    return SC_RESULT_OK;
}
//scp_result searchElStr5_a_a_a_f_a(scp_operand *param1, scp_operand *param2, scp_operand *param3, scp_operand *param4, scp_operand *param5){}
scp_result searchElStr5_a_a_a_a_f(scp_operand *param1, scp_operand *param2, scp_operand *param3, scp_operand *param4, scp_operand *param5)
{
    sc_iterator3 *it = sc_iterator3_f_a_a_new(param5->addr, param4->element_type, param2->element_type);
    if (SC_TRUE == sc_iterator3_next(it))
    {
        param4->addr = sc_iterator3_value(it, 1);
        param2->addr = sc_iterator3_value(it, 2);
        sc_memory_get_arc_begin(param2->addr, &(param1->addr));
        sc_memory_get_arc_end(param2->addr, &(param3->addr));
        sc_iterator3_free(it);
        return SCP_RESULT_TRUE;
    }
    sc_iterator3_free(it);
    return SCP_RESULT_FALSE;
}
예제 #4
0
/*
 * returns SC_FALSE if element can't be found otherwise SC_TRUE
*/
sc_bool get_const_element(sc_memory_context *context, sc_addr pattern, sc_addr *result)
{
    sc_iterator3 *it = sc_iterator3_f_a_a_new(context, pattern, sc_type_arc_pos_const_perm, sc_type_const);
    if (SC_TRUE == sc_iterator3_next(it))
    {
        *result = sc_iterator3_value(it, 2);
        sc_iterator3_free(it);
        return SC_TRUE;
    }
    sc_iterator3_free(it);
    return SC_FALSE;
}
예제 #5
0
void copy_incident_connectors(sc_addr dest, sc_addr source)
{
    sc_addr new_connector;
    sc_type conn_type;
    sc_iterator3 *it;

    // Output connectors loop
    it = sc_iterator3_f_a_a_new(source, 0, 0);
    while (sc_iterator3_next(it))
    {
        if (SC_RESULT_OK != sc_memory_get_element_type(sc_iterator3_value(it, 1), &conn_type))
            continue;
        new_connector = sc_memory_arc_new(conn_type, dest, sc_iterator3_value(it, 2));
        copy_incident_connectors(new_connector, sc_iterator3_value(it, 1));
    }
    sc_iterator3_free(it);

    // Input connectors loop
    it = sc_iterator3_a_a_f_new(0, 0, source);
    while (sc_iterator3_next(it))
    {
        if (SC_RESULT_OK != sc_memory_get_element_type(sc_iterator3_value(it, 1), &conn_type))
            continue;
        new_connector = sc_memory_arc_new(conn_type, sc_iterator3_value(it, 0), dest);
        copy_incident_connectors(new_connector, sc_iterator3_value(it, 1));
    }
    sc_iterator3_free(it);
}
예제 #6
0
void finish_question(sc_addr question)
{
    sc_iterator3 *it = nullptr;
    sc_addr arc;

    it = sc_iterator3_f_a_f_new(keynode_question_initiated, sc_type_arc_pos_const_perm, question);
    while (sc_iterator3_next(it))
        sc_memory_element_free(sc_iterator3_value(it, 1));
    sc_iterator3_free(it);

    arc = sc_memory_arc_new(sc_type_arc_pos_const_perm, keynode_question_finished, question);
    SYSTEM_ELEMENT(arc);
}
예제 #7
0
sc_result agent_search_all_superclasses_in_quasybinary_relation(const sc_event *event, sc_addr arg)
{
    sc_addr question, answer;
    sc_iterator3 *it1;
    sc_bool sys_off = SC_TRUE;

    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_search_all_superclasses_in_quasybinary_relation, 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));

        search_superclasses_rec(sc_iterator3_value(it1, 2), answer, sys_off);
    }
    sc_iterator3_free(it1);

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

    return SC_RESULT_OK;
}
sc_bool copy_set_into_hash(sc_memory_context *context, sc_addr set, sc_type arc_type, sc_type end_type, sc_type_hash *table, sc_uint *var_count)
{
    sc_addr addr3;
    sc_iterator3 *it = sc_iterator3_f_a_a_new(context, set, arc_type, end_type);
    if (it == 0)
    {
        return SC_FALSE;
    }
    while (SC_TRUE == sc_iterator3_next(it))
    {
        addr3 = sc_iterator3_value(it, 2);
        table->insert(sc_hash_pair(SC_ADDR_LOCAL_TO_INT(addr3), addr3));

        sc_type element_type;
        if (sc_memory_get_element_type(context, addr3, &element_type) != SC_RESULT_OK) {continue;}
        if ((sc_type_var & element_type) == sc_type_var)
        {
            (*var_count)++;
        }
    }
    sc_iterator3_free(it);
    return SC_TRUE;
}
scp_result printEl(scp_operand *param)
{
    sc_addr element = param->addr;
    sc_addr addr2, addr3;
    sc_addr idtf;
    sc_iterator3 *it = nullptr;
    sc_uint32 out_c = 0, in_c = 0;
    if (SC_FALSE == sc_memory_is_element(param->addr))
    {
        return print_error("printEl", "Parameter has not value");
    }
    if (SC_RESULT_OK == sc_helper_get_system_identifier(element, &idtf))
    {
        sc_stream *stream;
        sc_uint32 length = 0, read_length = 0;
        sc_char *data;
        sc_memory_get_link_content(idtf, &stream);
        sc_stream_get_length(stream, &length);
        data = calloc(length + 1, sizeof(sc_char));
        sc_stream_read_data(stream, data, length, &read_length);
        data[length] = '\0';
        printf("\nPrint element: %s =\n", data);
        sc_stream_free(stream);
        free(data);
    }
    else
    {
        printf("\nPrint element: %u|%u =\n", element.seg, element.offset);
    }

    it = sc_iterator3_a_a_f_new(0, 0, element);
    if (it == 0)
    {
        return SCP_RESULT_ERROR;
    }
    printf("Input arcs:\n");
    while (SC_TRUE == sc_iterator3_next(it))
    {
        in_c++;
        addr2 = sc_iterator3_value(it, 0);
        addr3 = sc_iterator3_value(it, 1);

        if (SC_RESULT_OK == sc_helper_get_system_identifier(addr3, &idtf))
        {
            sc_stream *stream;
            sc_uint32 length = 0, read_length = 0;
            sc_char *data;
            sc_memory_get_link_content(idtf, &stream);
            sc_stream_get_length(stream, &length);
            data = calloc(length + 1, sizeof(sc_char));
            sc_stream_read_data(stream, data, length, &read_length);
            data[length] = '\0';
            if (SCP_RESULT_TRUE == check_type(addr3, scp_type_arc_access))
            {
                printf("\t%s <- ", data);
            }
            else
            {
                printf("\t%s <= ", data);
            }
            sc_stream_free(stream);
            free(data);
        }
        else
        {
            if (SCP_RESULT_TRUE == check_type(addr3, scp_type_arc_access))
            {
                printf("\t%u|%u <- ", addr3.seg, addr3.offset);
            }
            else
            {
                printf("\t%u|%u <= ", addr3.seg, addr3.offset);
            }
        }
        if (SC_RESULT_OK == sc_helper_get_system_identifier(addr2, &idtf))
        {
            sc_stream *stream;
            sc_uint32 length = 0, read_length = 0;
            sc_char *data;
            sc_memory_get_link_content(idtf, &stream);
            sc_stream_get_length(stream, &length);
            data = calloc(length + 1, sizeof(sc_char));
            sc_stream_read_data(stream, data, length, &read_length);
            data[length] = '\0';
            printf("%s;\n", data);
            sc_stream_free(stream);
            free(data);
        }
        else
        {
            printf("%u|%u;\n", addr2.seg, addr2.offset);
        }
    }
    sc_iterator3_free(it);
    printf("Total input arcs: %d\n", in_c);

    it = sc_iterator3_f_a_a_new(element, 0, 0);
    if (it == 0)
    {
        return SCP_RESULT_ERROR;
    }
    printf("Output arcs:\n");
    while (SC_TRUE == sc_iterator3_next(it))
    {
        out_c++;
        addr2 = sc_iterator3_value(it, 1);
        addr3 = sc_iterator3_value(it, 2);

        if (SC_RESULT_OK == sc_helper_get_system_identifier(addr2, &idtf))
        {
            sc_stream *stream;
            sc_uint32 length = 0, read_length = 0;
            sc_char *data;
            sc_memory_get_link_content(idtf, &stream);
            sc_stream_get_length(stream, &length);
            data = calloc(length + 1, sizeof(sc_char));
            sc_stream_read_data(stream, data, length, &read_length);
            data[length] = '\0';
            if (SCP_RESULT_TRUE == check_type(addr2, scp_type_arc_access))
            {
                printf("\t%s -> ", data);
            }
            else
            {
                printf("\t%s => ", data);
            }
            sc_stream_free(stream);
            free(data);
        }
        else
        {
            if (SCP_RESULT_TRUE == check_type(addr2, scp_type_arc_access))
            {
                printf("\t%u|%u -> ", addr2.seg, addr2.offset);
            }
            else
            {
                printf("\t%u|%u => ", addr2.seg, addr2.offset);
            }

        }
        if (SC_RESULT_OK == sc_helper_get_system_identifier(addr3, &idtf))
        {
            sc_stream *stream;
            sc_uint32 length = 0, read_length = 0;
            sc_char *data;
            sc_memory_get_link_content(idtf, &stream);
            sc_stream_get_length(stream, &length);
            data = calloc(length + 1, sizeof(sc_char));
            sc_stream_read_data(stream, data, length, &read_length);
            data[length] = '\0';
            printf("%s;\n", data);
            sc_stream_free(stream);
            free(data);
        }
        else
        {
            printf("%u|%u;\n", addr3.seg, addr3.offset);
        }
    }
    sc_iterator3_free(it);
    printf("Total output arcs: %d\n", out_c);
    return SCP_RESULT_TRUE;
}
scp_result scp_iterator3_next(scp_iterator3 *iter, scp_operand *param1, scp_operand *param2, scp_operand *param3)
{
    sc_uint32 fixed1 = 0;
    sc_uint32 fixed2 = 0;
    sc_uint32 fixed3 = 0;
    sc_uint32 fixed = 0;
    if (param2->param_type == SCP_FIXED)
    {
        return print_error("SCP ITERATOR 3 NEXT", "Parameter 2 must have ASSIGN modifier");
    }
    if (param1->param_type == SCP_FIXED)
    {
        if (SC_FALSE == sc_memory_is_element(param1->addr))
        {
            return print_error("SCP ITERATOR 3 NEXT", "Parameter 1 has modifier FIXED, but has not value");
        }
        fixed1 = 0x1;
    }
    if (param3->param_type == SCP_FIXED)
    {
        if (SC_FALSE == sc_memory_is_element(param3->addr))
        {
            return print_error("SCP ITERATOR 3 NEXT", "Parameter 3 has modifier FIXED, but has not value");
        }
        fixed3 = 0x100;
    }
    fixed = (fixed1 | fixed2 | fixed3);
    switch (fixed)
    {
        case 0x001:
            if (iter->type != sc_iterator3_f_a_a)
            {
                return print_error("SCP ITERATOR 3 NEXT", "Iterator type and parameter type combination doesn't match");
            }
            else
            {
                if (SC_TRUE == sc_iterator3_next(iter))
                {
                    param2->addr = sc_iterator3_value(iter, 1);
                    param3->addr = sc_iterator3_value(iter, 2);
                    return SCP_RESULT_TRUE;
                }
                else
                {
                    return SCP_RESULT_FALSE;
                }
            }
        case 0x100:
            if (iter->type != sc_iterator3_a_a_f)
            {
                return print_error("SCP ITERATOR 3 NEXT", "Iterator type and parameter type combination doesn't match");
            }
            else
            {
                if (SC_TRUE == sc_iterator3_next(iter))
                {
                    param1->addr = sc_iterator3_value(iter, 0);
                    param2->addr = sc_iterator3_value(iter, 1);
                    return SCP_RESULT_TRUE;
                }
                else
                {
                    return SCP_RESULT_FALSE;
                }
            }
        case 0x101:
            if (iter->type != sc_iterator3_f_a_f)
            {
                return print_error("SCP ITERATOR 3 NEXT", "Iterator type and parameter type combination doesn't match");
            }
            else
            {
                if (SC_TRUE == sc_iterator3_next(iter))
                {
                    param2->addr = sc_iterator3_value(iter, 1);
                    return SCP_RESULT_TRUE;
                }
                else
                {
                    return SCP_RESULT_FALSE;
                }
            }
        default:
            return print_error("SCP ITERATOR 3 NEXT", "Unsupported parameter type combination");
    }
    return SCP_RESULT_ERROR;
}
sc_bool check_coherence(sc_memory_context *context, sc_addr element, sc_addr sc_pattern, sc_addr checked_arc, sc_bool output, sc_type_result *result, sc_type_result *inp_result)
{
    sc_addr addr1, addr2, temp;

    sc_iterator3 *it_pattern_arc = sc_iterator3_f_a_a_new(context, element, 0, 0);
    if (it_pattern_arc == null_ptr) {return SC_FALSE;}
    while (SC_TRUE == sc_iterator3_next(it_pattern_arc))
    {
        addr2 = sc_iterator3_value(it_pattern_arc, 1);
        if (SC_TRUE == sc_helper_check_arc(context, sc_pattern, addr2, sc_type_arc_pos_const_perm))
        {
            if (SC_TRUE == output && SC_ADDR_IS_EQUAL(checked_arc, addr2))
            {
                continue;
            }

            if (SC_TRUE == find_result_pair_for_var(result, addr2, &temp) &&
                SC_FALSE == find_result_pair_for_var(inp_result, addr2, &temp))
            {
                sc_iterator3_free(it_pattern_arc);
                return SC_FALSE;
            }
        }
    }
    sc_iterator3_free(it_pattern_arc);

    it_pattern_arc = sc_iterator3_a_a_f_new(context, 0, 0, element);
    if (it_pattern_arc == null_ptr) {return SC_FALSE;}
    while (SC_TRUE == sc_iterator3_next(it_pattern_arc))
    {
        addr1 = sc_iterator3_value(it_pattern_arc, 0);
        if (SC_ADDR_IS_EQUAL(addr1, sc_pattern)) continue;
        addr2 = sc_iterator3_value(it_pattern_arc, 1);
        if (SC_TRUE == sc_helper_check_arc(context, sc_pattern, addr2, sc_type_arc_pos_const_perm))
        {
            if (SC_FALSE == output && SC_ADDR_IS_EQUAL(checked_arc, addr2))
            {
                continue;
            }

            if (SC_TRUE == find_result_pair_for_var(result, addr2, &temp) &&
                SC_FALSE == find_result_pair_for_var(inp_result, addr2, &temp))
            {
                sc_iterator3_free(it_pattern_arc);
                return SC_FALSE;
            }
        }
    }
    sc_iterator3_free(it_pattern_arc);

    sc_type element_type;

    if (SC_RESULT_OK != sc_memory_get_element_type(context, element, &element_type)) {return SC_FALSE;}
    if ((sc_type_node & element_type) != sc_type_node)
    {
        sc_addr end, begin;
        if (SC_RESULT_OK != sc_memory_get_arc_begin(context, element, &begin)) {return SC_FALSE;}
        if (SC_RESULT_OK != sc_memory_get_arc_end(context, element, &end)) {return SC_FALSE;}

        if (SC_TRUE == check_coherence(context, begin, sc_pattern, element, SC_TRUE, result, inp_result) &&
            SC_TRUE == check_coherence(context, end, sc_pattern, element, SC_FALSE, result, inp_result))
            return SC_TRUE;
        else
            return SC_FALSE;
    }
    return SC_TRUE;
}
예제 #12
0
void test8()
{
    sc_addr node[10], arc[10][10], addr_arc;
    sc_uint32 i, j;
    sc_iterator3 *it = 0;

    printf("Segments count: %d\n", sc_storage_get_segments_count());
    print_storage_statistics();

    timer = g_timer_new();

    printf("Create 10 nodes and 100 arcs, that connect nodes each other\n");
    for (i = 0; i < 10; i++)
        node[i] = sc_memory_node_new(sc_type_node);

    for (i = 0; i < 10; i++)
        for (j = 0; j < 10; j++)
            arc[i][j] = sc_memory_arc_new(0, node[i], node[j]);

    printf("Segments count: %d\n", sc_storage_get_segments_count());
    print_storage_statistics();

    printf("Delete 5 nodes\n");
    for (i = 0; i < 5; i++)
        sc_memory_element_free(node[i]);

    // iterate element for check
    it = sc_iterator3_f_a_a_new(node[9], 0, 0);
    while (sc_iterator3_next(it) == SC_TRUE)
    {
        addr_arc = sc_iterator3_value(it, 1);
        printf("Arc: %d, %d\n", addr_arc.seg, addr_arc.offset);
    }
    sc_iterator3_free(it);

    printf("Segments count: %d\n", sc_storage_get_segments_count());
    print_storage_statistics();

    g_timer_stop(timer);

    printf("Collect and delete garbage...\n");
    g_timer_reset(timer);
    g_timer_start(timer);

    sc_storage_update_segments();

    g_timer_stop(timer);

    // iterate element for check
    it = sc_iterator3_f_a_a_new(node[9], 0, 0);
    while (sc_iterator3_next(it) == SC_TRUE)
    {
        addr_arc = sc_iterator3_value(it, 1);
        printf("Arc: %d, %d\n", addr_arc.seg, addr_arc.offset);
    }
    sc_iterator3_free(it);

    printf("Elapsed time: %f\n", g_timer_elapsed(timer, 0));
    printf("Segments count: %d\n", sc_storage_get_segments_count());
    print_storage_statistics();

    g_timer_destroy(timer);

}
예제 #13
0
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);
}
예제 #14
0
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);
}
예제 #15
0
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;
}
예제 #16
0
파일: test.cpp 프로젝트: drax08/sc-machine
void test4()
{
    sc_addr node[10], arc[10][10];
    sc_uint32 i, j;
    sc_iterator3 *it = 0;
    sc_addr addr1, addr2, addr3;

    printf("Create 10 nodes and 100 arcs, that connect nodes each other\n");
    for (i = 0; i < 10; i++)
        node[i] = sc_storage_element_new(sc_type_node);

    for (i = 0; i < 10; i++)
        for (j = 0; j < 10; j++)
            arc[i][j] = sc_storage_arc_new(0, node[i], node[j]);

    print_storage_statistics();
    printf("---\nTest iterator3_f_a_a\n");
    for (i = 0; i < 10; i++)
    {
        it = sc_iterator3_f_a_a_new(node[i], 0, 0);
        g_assert(it != 0);

        printf("Node %d:\n", i);
        while (sc_iterator3_next(it))
        {
            addr1 = sc_iterator3_value(it, 0);
            addr2 = sc_iterator3_value(it, 1);
            addr3 = sc_iterator3_value(it, 2);

            printf("\t%u, %u; %u, %u; %u, %u\n", addr1.seg, addr1.offset, addr2.seg, addr2.offset, addr3.seg, addr3.offset);
        }

        sc_iterator3_free(it);
    }

    printf("---\nTest iterator3_a_a_f\n");
    for (i = 0; i < 10; i++)
    {
        it = sc_iterator3_a_a_f_new(0, 0, node[i]);
        g_assert(it != 0);

        printf("Node %d:\n", i);
        while (sc_iterator3_next(it))
        {
            addr1 = sc_iterator3_value(it, 0);
            addr2 = sc_iterator3_value(it, 1);
            addr3 = sc_iterator3_value(it, 2);

            printf("\t%u, %u; %u, %u; %u, %u\n", addr1.seg, addr1.offset, addr2.seg, addr2.offset, addr3.seg, addr3.offset);
        }

        sc_iterator3_free(it);
    }

    printf("---\nTest iterator3_f_a_f\n");
    for (i = 0; i < 10; i++)
    {
        it = sc_iterator3_f_a_f_new(node[i], 0, node[9 - i]);
        g_assert(it != 0);

        printf("Node %d:\n", i);
        while (sc_iterator3_next(it))
        {
            addr1 = sc_iterator3_value(it, 0);
            addr2 = sc_iterator3_value(it, 1);
            addr3 = sc_iterator3_value(it, 2);

            printf("\t%u, %u; %u, %u; %u, %u\n", addr1.seg, addr1.offset, addr2.seg, addr2.offset, addr3.seg, addr3.offset);
        }

        sc_iterator3_free(it);
    }

    // iterator allocations
    printf("---\nTest iterator allocation(dealocation) speed...\n");

    timer = g_timer_new();

    g_timer_reset(timer);
    g_timer_start(timer);

    for (i = 0; i < iterator_alloc_count; i++)
    {
        it = sc_iterator3_f_a_a_new(node[0], 0, 0);
        sc_iterator3_free(it);
    }

    g_timer_stop(timer);
    printf("Allocated iterators: %d\n", iterator_alloc_count);
    printf("Allocation/deallocation per second: %f\n", iterator_alloc_count / g_timer_elapsed(timer, 0));

    g_timer_destroy(timer);

}
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;
}
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);
    }
}
void search_nonbinary_relation(sc_addr elem, sc_addr answer, sc_bool sys_off)
{
    sc_iterator3 *it1, *it2, *it3;
    sc_type el_type;

    // iterate input arcs for elem
    it1 = sc_iterator3_a_a_f_new(sc_type_node | sc_type_const,
                                 sc_type_arc_pos_const_perm,
                                 elem);
    while (sc_iterator3_next(it1) == SC_TRUE)
    {
        // if elem is a link of non-binary relation
        if (SC_TRUE == sc_helper_check_arc(keynode_nonbinary_relation, sc_iterator3_value(it1, 0), sc_type_arc_pos_const_perm))
        {
            if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator3_value(it1, 0)) || IS_SYSTEM_ELEMENT(sc_iterator3_value(it1, 1))))
                continue;

            // iterate other elements of link
            it2 = sc_iterator3_f_a_a_new(elem,
                                         sc_type_arc_pos_const_perm,
                                         sc_type_node | sc_type_const);
            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 attributes of link
                it3 = sc_iterator3_a_a_f_new(sc_type_node | sc_type_const,
                                             sc_type_arc_pos_const_perm,
                                             sc_iterator3_value(it2, 1));
                while (sc_iterator3_next(it3) == SC_TRUE)
                {
                    if (sys_off == SC_TRUE && (IS_SYSTEM_ELEMENT(sc_iterator3_value(it3, 0)) || IS_SYSTEM_ELEMENT(sc_iterator3_value(it3, 1))))
                        continue;

                    sc_memory_get_element_type(sc_iterator3_value(it3, 0), &el_type);
                    if (!(el_type & (sc_type_node_norole | sc_type_node_role)))
                        continue;

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

            appendIntoAnswer(answer, sc_iterator3_value(it1, 0));
            appendIntoAnswer(answer, sc_iterator3_value(it1, 1));
        }
    }
    sc_iterator3_free(it1);
}
예제 #20
0
sc_bool system_sys_search_recurse(sc_addr sc_pattern, sc_type_hash pattern, sc_addr curr_const_element, sc_addr curr_pattern_element, sc_type_result *inp_result, sc_type_result_vector *out_common_result, int element_number)
{
    sc_type input_element_type;
    if (sc_memory_get_element_type(curr_pattern_element, &input_element_type) != SC_RESULT_OK) {return SC_FALSE;}
    if (element_number == 2 && (sc_type_node & input_element_type) != sc_type_node)
    {
        //!Input element is arc
        sc_addr const_element, pattern_element, temp, end_pattern_element, end_const_element;
        sc_type pattern_element_type, end_pattern_element_type;
        sc_memory_get_arc_begin(curr_const_element, &const_element);
        sc_memory_get_arc_begin(curr_pattern_element, &pattern_element);
        sc_memory_get_arc_end(curr_const_element, &end_const_element);
        sc_memory_get_arc_end(curr_pattern_element, &end_pattern_element);

        if (sc_memory_get_element_type(pattern_element, &pattern_element_type) != SC_RESULT_OK) {return SC_TRUE;}
        if ((sc_type_const & pattern_element_type) == sc_type_const)
        {
            if (SC_ADDR_IS_NOT_EQUAL(const_element, pattern_element))
            {
                inp_result->erase(curr_pattern_element);
                return SC_TRUE;
            }
        }
        else if (SC_TRUE == find_result_pair_for_var(inp_result, pattern_element, &temp))
        {
            if (SC_ADDR_IS_NOT_EQUAL(const_element, temp))
            {
                inp_result->erase(curr_pattern_element);
                return SC_TRUE;
            }
        }

        if (sc_memory_get_element_type(end_pattern_element, &end_pattern_element_type) != SC_RESULT_OK) {return SC_TRUE;}
        if ((sc_type_const & end_pattern_element_type) == sc_type_const)
        {
            if (SC_ADDR_IS_NOT_EQUAL(end_const_element, end_pattern_element))
            {
                inp_result->erase(curr_pattern_element);
                return SC_TRUE;
            }
        }
        else if (SC_TRUE == find_result_pair_for_var(inp_result, end_pattern_element, &temp))
        {
            if (SC_ADDR_IS_NOT_EQUAL(end_const_element, temp))
            {
                inp_result->erase(curr_pattern_element);
                return SC_TRUE;
            }
        }
        if (SC_ADDR_IS_NOT_EQUAL(const_element, pattern_element))
        {
            inp_result->insert(sc_addr_pair(pattern_element, const_element));
        }
        pattern.insert(sc_hash_pair(SC_ADDR_LOCAL_TO_INT(curr_pattern_element), curr_pattern_element));
        system_sys_search_recurse(sc_pattern, pattern, const_element, pattern_element, inp_result, out_common_result, 2);
        return SC_TRUE;
    }

    sc_addr addr1, addr2, temp, temp1;
    int out_arc_count = 0;

    sc_type_result_vector common_result;
    sc_type_result_vector del_result;
    sc_type_result inp_result_copy = *inp_result;
    common_result.push_back(inp_result);

    //Pattern arcs list

    sc_addr_vector pattern_arc_set;

    sc_iterator3 *it_pattern_arc = sc_iterator3_f_a_a_new(curr_pattern_element, 0, 0);
    if (it_pattern_arc == nullptr) {return SC_FALSE;}
    while (SC_TRUE == sc_iterator3_next(it_pattern_arc))
    {
        addr2 = sc_iterator3_value(it_pattern_arc, 1);
        if (pattern.find(SC_ADDR_LOCAL_TO_INT(addr2)) != pattern.end())
        {
            out_arc_count++;
            pattern_arc_set.push_back(addr2);
        }
    }
    sc_iterator3_free(it_pattern_arc);

    it_pattern_arc = sc_iterator3_a_a_f_new(0, 0, curr_pattern_element);
    if (it_pattern_arc == nullptr) {return SC_FALSE;}
    while (SC_TRUE == sc_iterator3_next(it_pattern_arc))
    {
        addr1 = sc_iterator3_value(it_pattern_arc, 0);
        if (SC_ADDR_IS_EQUAL(addr1, sc_pattern)) continue;
        addr2 = sc_iterator3_value(it_pattern_arc, 1);
        if (pattern.find(SC_ADDR_LOCAL_TO_INT(addr2)) != pattern.end())
        {
            pattern_arc_set.push_back(addr2);
        }
    }
    sc_iterator3_free(it_pattern_arc);

    sc_addr pattern_arc;
    sc_addr const_arc;
    sc_addr next_pattern_element;
    sc_addr next_const_element;
    sc_bool out_arc_flag = SC_TRUE;
    sc_type_result_vector new_common_result;
    sc_type_result_vector next_common_result;
    sc_type_result_vector next_common_result_arc;
    sc_type_result_vector next_common_result1;

    sc_bool pattern_arc_is_const_or_has_value = SC_FALSE;
    sc_bool pattern_is_const_or_has_value = SC_FALSE;

    //Pattern arcs loop
    for (sc_uint i = 0; i < pattern_arc_set.size(); i++)
    {
        pattern_arc = pattern_arc_set[i];
        out_arc_flag = SC_TRUE;

        //!check pattern_arc type
        sc_type pattern_arc_type;
        if (sc_memory_get_element_type(pattern_arc, &pattern_arc_type) != SC_RESULT_OK)
            continue;
        if ((sc_type_const & pattern_arc_type) == sc_type_const)
            continue;

        pattern_arc_is_const_or_has_value = SC_FALSE;
        pattern_is_const_or_has_value = SC_FALSE;

        if (out_arc_count > 0)
        {
            if (SC_RESULT_OK != sc_memory_get_arc_end(pattern_arc, &next_pattern_element))
                continue;
            out_arc_count--;
        }
        else
        {
            if (SC_RESULT_OK != sc_memory_get_arc_begin(pattern_arc, &next_pattern_element))
                continue;
            out_arc_flag = SC_FALSE;
        }

        if (pattern.find(SC_ADDR_LOCAL_TO_INT(next_pattern_element)) == pattern.end())
        {
            continue;
        }

        sc_type_result::iterator arc_it = inp_result_copy.find(pattern_arc);
        if (arc_it != inp_result_copy.end())
        {
            const_arc = (*arc_it).second;
            pattern_arc_is_const_or_has_value = SC_TRUE;
            if (out_arc_flag == SC_TRUE)
            {
                if (SC_RESULT_OK != sc_memory_get_arc_end(const_arc, &next_const_element))
                    continue;
            }
            else
            {
                if (SC_RESULT_OK != sc_memory_get_arc_begin(const_arc, &next_const_element))
                    continue;
            }
        }

        //!check next_pattern_element type
        sc_type next_pattern_element_type;
        if (sc_memory_get_element_type(next_pattern_element, &next_pattern_element_type) != SC_RESULT_OK) {continue;}
        if ((sc_type_const & next_pattern_element_type) == sc_type_const)
        {
            if (pattern_arc_is_const_or_has_value == SC_TRUE)
            {
                if (!SC_ADDR_IS_EQUAL(next_const_element, next_pattern_element))
                {
                    continue;
                }
            }
            else
            {
                next_const_element = next_pattern_element;
            }
            pattern_is_const_or_has_value = SC_TRUE;
        }
        else
        {
            sc_type_result::iterator it = inp_result_copy.find(next_pattern_element);
            if (it != inp_result_copy.end())
            {
                if (pattern_arc_is_const_or_has_value == SC_TRUE)
                {
                    if (!SC_ADDR_IS_EQUAL(next_const_element, (*it).second))
                    {
                        continue;
                    }
                }
                else
                {
                    next_const_element = (*it).second;
                }
                pattern_is_const_or_has_value = SC_TRUE;
            }
        }

        pattern.erase(SC_ADDR_LOCAL_TO_INT(next_pattern_element));

        //!const arc loop
        sc_addr_vector const_arc_set;

        if (pattern_arc_is_const_or_has_value == SC_FALSE)
        {
            sc_iterator3 *it_const_arc;
            sc_type const_arc_type = ((~sc_type_var & pattern_arc_type) | sc_type_const);
            if (out_arc_flag == SC_TRUE)
            {
                if (pattern_is_const_or_has_value == SC_TRUE)
                {
                    it_const_arc = sc_iterator3_f_a_f_new(curr_const_element, const_arc_type, next_const_element);
                }
                else
                {
                    sc_type next_const_element_type = ((~sc_type_var & next_pattern_element_type) | sc_type_const);
                    it_const_arc = sc_iterator3_f_a_a_new(curr_const_element, const_arc_type, next_const_element_type);
                }
            }
            else
            {
                if (pattern_is_const_or_has_value == SC_TRUE)
                {
                    it_const_arc = sc_iterator3_f_a_f_new(next_const_element, const_arc_type, curr_const_element);
                }
                else
                {
                    sc_type next_const_element_type = ((~sc_type_var & next_pattern_element_type) | sc_type_const);
                    it_const_arc = sc_iterator3_a_a_f_new(next_const_element_type, const_arc_type, curr_const_element);
                }
            }
            while (SC_TRUE == sc_iterator3_next(it_const_arc))
            {
                if (out_arc_flag == SC_FALSE)
                {
                    addr1 = sc_iterator3_value(it_const_arc, 0);
                    if (SC_ADDR_IS_EQUAL(addr1, sc_pattern))
                        continue;
                }
                addr2 = sc_iterator3_value(it_const_arc, 1);
                const_arc_set.push_back(addr2);
            }
            sc_iterator3_free(it_const_arc);
        }
        else
        {
            const_arc_set.push_back(const_arc);
        }

        //printf("ELEMENT %u|%u CONST ARCS COUNT:%d\n", curr_const_element.seg, curr_const_element.offset, const_arc_set.size());

        for (sc_uint j = 0; j < const_arc_set.size(); j++)
        {
            const_arc = const_arc_set[j];

            if (pattern_arc_is_const_or_has_value == SC_FALSE)
            {
                if (out_arc_flag == SC_TRUE)
                {
                    if (SC_RESULT_OK != sc_memory_get_arc_end(const_arc, &next_const_element))
                        continue;
                }
                else
                {
                    if (SC_RESULT_OK != sc_memory_get_arc_begin(const_arc, &next_const_element))
                        continue;
                }
            }

            if (pattern_is_const_or_has_value == SC_FALSE && pattern.find(SC_ADDR_LOCAL_TO_INT(next_const_element)) != pattern.end())
            {
                continue;
            }

            //!Results loop
            for (sc_uint k = 0; k < common_result.size(); k++)
            {
                sc_type_result *result = common_result[k];
                sc_bool gen_arc = SC_FALSE;

                if (pattern_arc_is_const_or_has_value == SC_FALSE)
                {
                    if (SC_TRUE == find_result_pair_for_const(result, const_arc, &temp))
                        continue;


                    if (SC_TRUE == find_result_pair_for_var(result, pattern_arc, &temp))
                    {
                        //!Gen new result
                        if (SC_FALSE == pattern_is_const_or_has_value
                            && SC_TRUE == find_result_pair_for_const(result, next_const_element, &temp1)
                            && SC_ADDR_IS_NOT_EQUAL(temp1, next_pattern_element))
                        {
                            continue;
                        }
                        sc_type_result *new_result = new sc_type_result();
                        //class_count++;
                        (*new_result) = (*result);
                        new_common_result.push_back(new_result);
                        result = new_result;
                        result->erase(pattern_arc);
                        if (pattern_is_const_or_has_value == SC_FALSE)
                        {
                            result->erase(next_pattern_element);
                        }
                    }

                    gen_arc = SC_TRUE;
                }

                //!Genering pair for 3rd element
                if (pattern_is_const_or_has_value == SC_FALSE
                    && SC_FALSE == find_result_pair_for_var(result, next_pattern_element, &temp))
                {
                    //!Genering pair for 2nd element
                    if (gen_arc == SC_TRUE)
                    {
                        result->insert(sc_addr_pair(pattern_arc, const_arc));
                        pattern.erase(SC_ADDR_LOCAL_TO_INT(pattern_arc));
                    }
                    result->insert(sc_addr_pair(next_pattern_element, next_const_element));
                    pattern.erase(SC_ADDR_LOCAL_TO_INT(next_pattern_element));
                }
                else
                {
                    //!Genering pair for 2nd element
                    if (gen_arc == SC_TRUE)
                    {
                        result->insert(sc_addr_pair(pattern_arc, const_arc));
                        pattern.erase(SC_ADDR_LOCAL_TO_INT(pattern_arc));
                    }
                }

                sc_type_result *recurse_result = new sc_type_result();
                (*recurse_result) = (*result);
                del_result.push_back(result);

                system_sys_search_recurse(sc_pattern, pattern, const_arc, pattern_arc,
                                          recurse_result, &next_common_result_arc, 1);

                for (sc_uint kk = 0; kk < next_common_result_arc.size(); kk++)
                {
                    sc_type_result *element_result = next_common_result_arc[kk];
                    system_sys_search_recurse(sc_pattern, pattern, next_const_element, next_pattern_element,
                                              element_result, &next_common_result1, 2);
                    if (!next_common_result1.empty())
                    {
                        next_common_result.insert(next_common_result.end(),
                                                  next_common_result1.begin(), next_common_result1.end());
                        next_common_result1.clear();
                    }
                    else
                    {
                        next_common_result.push_back(element_result);
                    }
                }

                pattern.insert(sc_hash_pair(SC_ADDR_LOCAL_TO_INT(pattern_arc), pattern_arc));
                pattern.insert(sc_hash_pair(SC_ADDR_LOCAL_TO_INT(next_pattern_element), next_pattern_element));

                out_common_result->insert(out_common_result->end(), next_common_result.begin(), next_common_result.end());
                new_common_result.insert(new_common_result.end(), next_common_result.begin(), next_common_result.end());
                cantorize_result_vector(&new_common_result);
                next_common_result.clear();
                next_common_result_arc.clear();
            }
        }//const loop

        common_result.insert(common_result.begin(), new_common_result.begin(), new_common_result.end());

        new_common_result.clear();
        remove_all_elements(&del_result, &common_result);
        remove_all_elements(&del_result, out_common_result);
        free_result_vector(&del_result);
    }//pattern loop

    out_common_result->insert(out_common_result->end(), common_result.begin(), common_result.end());
    common_result.clear();
    cantorize_result_vector(out_common_result);

    return SC_TRUE;
}
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);
    }
}
예제 #22
0
sc_bool system_sys_search_recurse(sc_memory_context *context, sc_addr sc_pattern, sc_type_hash *pattern, sc_addr curr_const_element, sc_addr curr_pattern_element, sc_type_result *inp_result, sc_type_result_vector *out_common_result, int level)
{
    sc_addr addr1, addr2, temp, temp1;
    int out_arc_count = 0;

    sc_type_result_vector common_result;
    sc_type_result_vector del_result;
    sc_type_result inp_result_copy = *inp_result;
    common_result.push_back(inp_result);

    //Pattern arcs list
    sc_addr_vector pattern_arc_set;

    sc_iterator3 *it_pattern_arc = sc_iterator3_f_a_a_new(context, curr_pattern_element, 0, 0);
    if (it_pattern_arc == null_ptr) {return SC_FALSE;}
    while (SC_TRUE == sc_iterator3_next(it_pattern_arc))
    {
        addr2 = sc_iterator3_value(it_pattern_arc, 1);
        if (pattern->find(SC_ADDR_LOCAL_TO_INT(addr2)) != pattern->end())
        {
            out_arc_count++;
            pattern_arc_set.push_back(addr2);
        }
    }
    sc_iterator3_free(it_pattern_arc);

    it_pattern_arc = sc_iterator3_a_a_f_new(context, 0, 0, curr_pattern_element);
    if (it_pattern_arc == null_ptr) {return SC_FALSE;}
    while (SC_TRUE == sc_iterator3_next(it_pattern_arc))
    {
        addr1 = sc_iterator3_value(it_pattern_arc, 0);
        if (SC_ADDR_IS_EQUAL(addr1, sc_pattern)) continue;
        addr2 = sc_iterator3_value(it_pattern_arc, 1);
        if (pattern->find(SC_ADDR_LOCAL_TO_INT(addr2)) != pattern->end())
        {
            pattern_arc_set.push_back(addr2);
        }
    }
    sc_iterator3_free(it_pattern_arc);

    sc_addr pattern_arc;
    sc_addr const_arc;
    sc_addr next_pattern_element;
    sc_addr next_const_element;
    sc_addr next_pattern_element_begin, next_pattern_element_end;
    sc_addr next_const_element_begin, next_const_element_end;
    sc_bool out_arc_flag = SC_TRUE;
    sc_type_result_vector new_common_result;
    sc_type_result_vector next_common_result;
    sc_type_result_vector next_common_result_arc;
    sc_type_result_vector next_common_result_begin;
    sc_type_result_vector next_common_result_end;
    sc_type_result_vector next_common_result_element;

    sc_bool next_pattern_element_is_node = SC_FALSE;
    sc_type next_pattern_element_type;
    sc_type next_pattern_element_begin_type;
    sc_type next_pattern_element_end_type;

    sc_bool pattern_arc_is_const_or_has_value = SC_FALSE;
    sc_bool pattern_is_const_or_has_value = SC_FALSE;
    sc_bool pattern_begin_is_const_or_has_value = SC_FALSE;
    sc_bool pattern_end_is_const_or_has_value = SC_FALSE;

    //Pattern arcs loop
    for (sc_uint i = 0; i < pattern_arc_set.size(); i++)
    {
        pattern_arc = pattern_arc_set[i];
        out_arc_flag = SC_TRUE;

        //!check pattern_arc type
        sc_type pattern_arc_type;
        if (sc_memory_get_element_type(context, pattern_arc, &pattern_arc_type) != SC_RESULT_OK)
            continue;
        if ((sc_type_const & pattern_arc_type) == sc_type_const)
            continue;

        pattern_arc_is_const_or_has_value = SC_FALSE;
        pattern_is_const_or_has_value = SC_FALSE;

        if (out_arc_count > 0)
        {
            if (SC_RESULT_OK != sc_memory_get_arc_end(context, pattern_arc, &next_pattern_element))
                continue;
            out_arc_count--;
        }
        else
        {
            if (SC_RESULT_OK != sc_memory_get_arc_begin(context, pattern_arc, &next_pattern_element))
                continue;
            out_arc_flag = SC_FALSE;
        }

        if (sc_memory_get_element_type(context, next_pattern_element, &next_pattern_element_type) != SC_RESULT_OK) {continue;}
        if ((sc_type_node & next_pattern_element_type) == sc_type_node)
        {
            next_pattern_element_is_node = SC_TRUE;
        }
        else
        {
            next_pattern_element_is_node = SC_FALSE;
            if (SC_RESULT_OK != sc_memory_get_arc_begin(context, next_pattern_element, &next_pattern_element_begin)) {continue;}
            if (SC_RESULT_OK != sc_memory_get_arc_end(context, next_pattern_element, &next_pattern_element_end)) {continue;}
        }

        if (pattern->find(SC_ADDR_LOCAL_TO_INT(next_pattern_element)) == pattern->end())
        {
            continue;
        }
        if (next_pattern_element_is_node == SC_FALSE)
        {
            if (pattern->find(SC_ADDR_LOCAL_TO_INT(next_pattern_element_begin)) == pattern->end())
            {
                continue;
            }
            if (pattern->find(SC_ADDR_LOCAL_TO_INT(next_pattern_element_end)) == pattern->end())
            {
                continue;
            }
        }

        sc_type_result::iterator arc_it = inp_result_copy.find(pattern_arc);
        if (arc_it != inp_result_copy.end())
        {
            const_arc = (*arc_it).second;
            pattern_arc_is_const_or_has_value = SC_TRUE;
            if (out_arc_flag == SC_TRUE)
            {
                if (SC_RESULT_OK != sc_memory_get_arc_end(context, const_arc, &next_const_element))
                    continue;
            }
            else
            {
                if (SC_RESULT_OK != sc_memory_get_arc_begin(context, const_arc, &next_const_element))
                    continue;
            }
        }

        //!check next_pattern_element type
        if ((sc_type_const & next_pattern_element_type) == sc_type_const)
        {
            if (pattern_arc_is_const_or_has_value == SC_TRUE)
            {
                if (!SC_ADDR_IS_EQUAL(next_const_element, next_pattern_element))
                {
                    continue;
                }
            }
            else
            {
                next_const_element = next_pattern_element;
            }
            pattern_is_const_or_has_value = SC_TRUE;
        }
        else
        {
            sc_type_result::iterator it = inp_result_copy.find(next_pattern_element);
            if (it != inp_result_copy.end())
            {
                if (pattern_arc_is_const_or_has_value == SC_TRUE)
                {
                    if (!SC_ADDR_IS_EQUAL(next_const_element, (*it).second))
                    {
                        continue;
                    }
                }
                else
                {
                    next_const_element = (*it).second;
                }
                pattern_is_const_or_has_value = SC_TRUE;
            }
        }

        if (next_pattern_element_is_node == SC_FALSE)
        {
            pattern_begin_is_const_or_has_value = SC_FALSE;
            pattern_end_is_const_or_has_value = SC_FALSE;
            if (sc_memory_get_element_type(context, next_pattern_element_begin, &next_pattern_element_begin_type) != SC_RESULT_OK) {continue;}
            if (sc_memory_get_element_type(context, next_pattern_element_end, &next_pattern_element_end_type) != SC_RESULT_OK) {continue;}

            if (SC_TRUE == pattern_arc_is_const_or_has_value || SC_TRUE == pattern_is_const_or_has_value)
            {
                if (SC_RESULT_OK != sc_memory_get_arc_begin(context, next_const_element, &next_const_element_begin)) {continue;}
                if (SC_RESULT_OK != sc_memory_get_arc_end(context, next_const_element, &next_const_element_end)) {continue;}
            }

            //!check next_pattern_element_begin type
            if ((sc_type_const & next_pattern_element_begin_type) == sc_type_const)
            {
                if (SC_TRUE == pattern_arc_is_const_or_has_value || SC_TRUE == pattern_is_const_or_has_value)
                {
                    if (!SC_ADDR_IS_EQUAL(next_const_element_begin, next_pattern_element_begin))
                    {
                        continue;
                    }
                }
                else
                {
                    next_const_element_begin = next_pattern_element_begin;
                }
                pattern_begin_is_const_or_has_value = SC_TRUE;
            }
            else
            {
                sc_type_result::iterator it = inp_result_copy.find(next_pattern_element_begin);
                if (it != inp_result_copy.end())
                {
                    if (SC_TRUE == pattern_arc_is_const_or_has_value || SC_TRUE == pattern_is_const_or_has_value)
                    {
                        if (!SC_ADDR_IS_EQUAL(next_const_element_begin, (*it).second))
                        {
                            continue;
                        }
                    }
                    else
                    {
                        next_const_element_begin = (*it).second;
                    }
                    pattern_begin_is_const_or_has_value = SC_TRUE;
                }
            }

            //!check next_pattern_element_end type
            if ((sc_type_const & next_pattern_element_end_type) == sc_type_const)
            {
                if (SC_TRUE == pattern_arc_is_const_or_has_value || SC_TRUE == pattern_is_const_or_has_value)
                {
                    if (!SC_ADDR_IS_EQUAL(next_const_element_end, next_pattern_element_end))
                    {
                        continue;
                    }
                }
                else
                {
                    next_const_element_end = next_pattern_element_end;
                }
                pattern_end_is_const_or_has_value = SC_TRUE;
            }
            else
            {
                sc_type_result::iterator it = inp_result_copy.find(next_pattern_element_end);
                if (it != inp_result_copy.end())
                {
                    if (SC_TRUE == pattern_arc_is_const_or_has_value || SC_TRUE == pattern_is_const_or_has_value)
                    {
                        if (!SC_ADDR_IS_EQUAL(next_const_element_end, (*it).second))
                        {
                            continue;
                        }
                    }
                    else
                    {
                        next_const_element_end = (*it).second;
                    }
                    pattern_end_is_const_or_has_value = SC_TRUE;
                }
            }
        }

        //!const arc loop
        sc_addr_vector const_arc_set;

        if (pattern_arc_is_const_or_has_value == SC_FALSE)
        {
            sc_iterator3 *it_const_arc;
            sc_type const_arc_type = ((~sc_type_var & pattern_arc_type) | sc_type_const);
            if (out_arc_flag == SC_TRUE)
            {
                if (pattern_is_const_or_has_value == SC_TRUE)
                {
                    it_const_arc = sc_iterator3_f_a_f_new(context, curr_const_element, const_arc_type, next_const_element);
                }
                else
                {
                    sc_type next_const_element_type = ((~sc_type_var & next_pattern_element_type) | sc_type_const);
                    if ((sc_type_node & next_pattern_element_type) == sc_type_node)
                    {
                        next_const_element_type = next_const_element_type & sc_type_link;
                    }
                    it_const_arc = sc_iterator3_f_a_a_new(context, curr_const_element, const_arc_type, next_const_element_type);
                }
            }
            else
            {
                if (pattern_is_const_or_has_value == SC_TRUE)
                {
                    it_const_arc = sc_iterator3_f_a_f_new(context, next_const_element, const_arc_type, curr_const_element);
                }
                else
                {
                    sc_type next_const_element_type = ((~sc_type_var & next_pattern_element_type) | sc_type_const);
                    if ((sc_type_node & next_pattern_element_type) == sc_type_node)
                    {
                        next_const_element_type = next_const_element_type & sc_type_link;
                    }
                    it_const_arc = sc_iterator3_a_a_f_new(context, next_const_element_type, const_arc_type, curr_const_element);
                }
            }
            while (SC_TRUE == sc_iterator3_next(it_const_arc))
            {
                if (out_arc_flag == SC_FALSE)
                {
                    addr1 = sc_iterator3_value(it_const_arc, 0);
                    if (SC_ADDR_IS_EQUAL(addr1, sc_pattern))
                        continue;
                }
                addr2 = sc_iterator3_value(it_const_arc, 1);
                const_arc_set.push_back(addr2);
            }
            sc_iterator3_free(it_const_arc);
        }
        else
        {
            const_arc_set.push_back(const_arc);
        }

        for (sc_uint j = 0; j < const_arc_set.size(); j++)
        {
            const_arc = const_arc_set[j];

            if (pattern_arc_is_const_or_has_value == SC_FALSE)
            {
                if (out_arc_flag == SC_TRUE)
                {
                    if (SC_RESULT_OK != sc_memory_get_arc_end(context, const_arc, &next_const_element))
                        continue;
                }
                else
                {
                    if (SC_RESULT_OK != sc_memory_get_arc_begin(context, const_arc, &next_const_element))
                        continue;
                }
            }

            //!TODO Optimize
            if (SC_FALSE == next_pattern_element_is_node)
            {
                sc_addr next_const_element_begin1, next_const_element_end1;
                if (SC_RESULT_OK != sc_memory_get_arc_begin(context, next_const_element, &next_const_element_begin1)) {continue;}
                if (SC_RESULT_OK != sc_memory_get_arc_end(context, next_const_element, &next_const_element_end1)) {continue;}
                if (SC_TRUE == pattern_begin_is_const_or_has_value && SC_ADDR_IS_NOT_EQUAL(next_const_element_begin, next_const_element_begin1))
                {
                    continue;
                }
                if (SC_TRUE == pattern_end_is_const_or_has_value && SC_ADDR_IS_NOT_EQUAL(next_const_element_end, next_const_element_end1))
                {
                    continue;
                }
                next_const_element_begin = next_const_element_begin1;
                next_const_element_end = next_const_element_end1;
            }

            //!Results loop
            for (sc_uint k = 0; k < common_result.size(); k++)
            {
                sc_type_result *result = common_result[k];

                if (pattern_arc_is_const_or_has_value == SC_FALSE)
                {
                    if (SC_TRUE == find_result_pair_for_const(result, const_arc, &temp))
                        continue;


                    if (SC_FALSE == pattern_is_const_or_has_value
                        && SC_TRUE == find_result_pair_for_const(result, next_const_element, &temp1)
                        && SC_ADDR_IS_NOT_EQUAL(temp1, next_pattern_element))
                    {
                        continue;
                    }
                    if (SC_FALSE == next_pattern_element_is_node)
                    {
                        if (SC_FALSE == pattern_begin_is_const_or_has_value
                            && SC_TRUE == find_result_pair_for_const(result, next_const_element_begin, &temp1)
                            && SC_ADDR_IS_NOT_EQUAL(temp1, next_pattern_element_begin))
                        {
                            continue;
                        }
                        if (SC_FALSE == pattern_end_is_const_or_has_value
                            && SC_TRUE == find_result_pair_for_const(result, next_const_element_end, &temp1)
                            && SC_ADDR_IS_NOT_EQUAL(temp1, next_pattern_element_end))
                        {
                            continue;
                        }
                    }

                    if (SC_TRUE == find_result_pair_for_var(result, pattern_arc, &temp))
                    {
                        //!Gen new result

                        if (SC_FALSE == check_coherence(context, next_pattern_element, sc_pattern, pattern_arc, !out_arc_flag, result, &inp_result_copy))
                        {
                            continue;
                        }

                        sc_type_result *new_result = new sc_type_result();

                        (*new_result) = (*result);
                        new_common_result.push_back(new_result);
                        result = new_result;
                        result->erase(pattern_arc);
                        if (pattern_is_const_or_has_value == SC_FALSE)
                        {
                            result->erase(next_pattern_element);
                        }
                        if (SC_FALSE == next_pattern_element_is_node)
                        {
                            if (pattern_begin_is_const_or_has_value == SC_FALSE)
                            {
                                result->erase(next_pattern_element_begin);
                            }
                            if (pattern_end_is_const_or_has_value == SC_FALSE)
                            {
                                result->erase(next_pattern_element_end);
                            }
                        }
                    }
                    else
                    {
                        //! TODO Add flags to not add already existing elements
                        if (SC_FALSE == pattern_is_const_or_has_value
                            && SC_TRUE == find_result_pair_for_var(result, next_pattern_element, &temp1)
                            && SC_ADDR_IS_NOT_EQUAL(temp1, next_const_element))
                        {
                            continue;
                        }
                        if (SC_FALSE == next_pattern_element_is_node)
                        {
                            if (SC_FALSE == pattern_begin_is_const_or_has_value
                                && SC_TRUE == find_result_pair_for_var(result, next_pattern_element_begin, &temp1)
                                && SC_ADDR_IS_NOT_EQUAL(temp1, next_const_element_begin))
                            {
                                continue;
                            }
                            if (SC_FALSE == pattern_end_is_const_or_has_value
                                && SC_TRUE == find_result_pair_for_var(result, next_pattern_element_end, &temp1)
                                && SC_ADDR_IS_NOT_EQUAL(temp1, next_const_element_end))
                            {
                                continue;
                            }
                        }
                    }

                    //!Genering pair for 2nd element
                    result->insert(sc_addr_pair(pattern_arc, const_arc));

                    //!Genering pair for 3rd element
                    if (pattern_is_const_or_has_value == SC_FALSE)
                    {
                        result->insert(sc_addr_pair(next_pattern_element, next_const_element));
                    }

                    //! Generating pair for next pattern element begin and end
                    if (SC_FALSE == next_pattern_element_is_node)
                    {
                        if (pattern_begin_is_const_or_has_value == SC_FALSE)
                        {
                            result->insert(sc_addr_pair(next_pattern_element_begin, next_const_element_begin));
                        }
                        if (pattern_end_is_const_or_has_value == SC_FALSE)
                        {
                            result->insert(sc_addr_pair(next_pattern_element_end, next_const_element_end));
                        }
                    }
                }
                else
                {
                    if (SC_TRUE == find_result_pair_for_const(result, const_arc, &temp1)
                        && SC_ADDR_IS_NOT_EQUAL(temp1, pattern_arc))
                    {
                        continue;
                    }
                    if (SC_TRUE == find_result_pair_for_var(result, pattern_arc, &temp1)
                        && SC_ADDR_IS_NOT_EQUAL(temp1, const_arc))
                    {
                        continue;
                    }
                    if (SC_FALSE == pattern_is_const_or_has_value
                        && SC_TRUE == find_result_pair_for_const(result, next_const_element, &temp1)
                        && SC_ADDR_IS_NOT_EQUAL(temp1, next_pattern_element))
                    {
                        continue;
                    }
                    if (SC_FALSE == next_pattern_element_is_node)
                    {
                        if (SC_FALSE == pattern_begin_is_const_or_has_value
                            && SC_TRUE == find_result_pair_for_const(result, next_const_element_begin, &temp1)
                            && SC_ADDR_IS_NOT_EQUAL(temp1, next_pattern_element_begin))
                        {
                            continue;
                        }
                        if (SC_FALSE == pattern_end_is_const_or_has_value
                            && SC_TRUE == find_result_pair_for_const(result, next_const_element_end, &temp1)
                            && SC_ADDR_IS_NOT_EQUAL(temp1, next_pattern_element_end))
                        {
                            continue;
                        }
                    }
                    if (SC_FALSE == pattern_is_const_or_has_value
                        && SC_TRUE == find_result_pair_for_var(result, next_pattern_element, &temp1)
                        && SC_ADDR_IS_NOT_EQUAL(temp1, next_const_element))
                    {
                        continue;
                    }
                    if (SC_FALSE == next_pattern_element_is_node)
                    {
                        if (SC_FALSE == pattern_begin_is_const_or_has_value
                            && SC_TRUE == find_result_pair_for_var(result, next_pattern_element_begin, &temp1)
                            && SC_ADDR_IS_NOT_EQUAL(temp1, next_const_element_begin))
                        {
                            continue;
                        }
                        if (SC_FALSE == pattern_end_is_const_or_has_value
                            && SC_TRUE == find_result_pair_for_var(result, next_pattern_element_end, &temp1)
                            && SC_ADDR_IS_NOT_EQUAL(temp1, next_const_element_end))
                        {
                            continue;
                        }
                    }
                    //!Genering pair for 3rd element
                    if (pattern_is_const_or_has_value == SC_FALSE)
                    {
                        result->insert(sc_addr_pair(next_pattern_element, next_const_element));
                    }

                    //! Generating pair for next pattern element begin and end
                    if (SC_FALSE == next_pattern_element_is_node)
                    {
                        if (pattern_begin_is_const_or_has_value == SC_FALSE)
                        {
                            result->insert(sc_addr_pair(next_pattern_element_begin, next_const_element_begin));
                        }
                        if (pattern_end_is_const_or_has_value == SC_FALSE)
                        {
                            result->insert(sc_addr_pair(next_pattern_element_end, next_const_element_end));
                        }
                    }
                }

                sc_type_result *arc_result = new sc_type_result();
                (*arc_result) = (*result);
                del_result.push_back(result);

                pattern->erase(SC_ADDR_LOCAL_TO_INT(pattern_arc));
                pattern->erase(SC_ADDR_LOCAL_TO_INT(next_pattern_element));
                if (next_pattern_element_is_node == SC_FALSE)
                {
                    pattern->erase(SC_ADDR_LOCAL_TO_INT(next_pattern_element_begin));
                    pattern->erase(SC_ADDR_LOCAL_TO_INT(next_pattern_element_end));
                }

                system_sys_search_recurse(context, sc_pattern, pattern, const_arc, pattern_arc,
                                          arc_result, &next_common_result_arc, level + 1);

                for (sc_uint kk = 0; kk < next_common_result_arc.size(); kk++)
                {
                    sc_type_result *element_result = next_common_result_arc[kk];
                    system_sys_search_recurse(context, sc_pattern, pattern, next_const_element, next_pattern_element,
                                              element_result, &next_common_result_element, level + 1);

                    if (SC_FALSE == next_pattern_element_is_node)
                    {
                        //! Recurse for begin element
                        for (sc_uint kk_begin = 0; kk_begin < next_common_result_element.size(); kk_begin++)
                        {
                            sc_type_result *begin_result = next_common_result_element[kk_begin];
                            system_sys_search_recurse(context, sc_pattern, pattern, next_const_element_begin, next_pattern_element_begin,
                                                      begin_result, &next_common_result_begin, level + 1);

                            //! Recurse for end element
                            for (sc_uint kk_end = 0; kk_end < next_common_result_begin.size(); kk_end++)
                            {
                                sc_type_result *end_result = next_common_result_begin[kk_end];
                                system_sys_search_recurse(context, sc_pattern, pattern, next_const_element_end, next_pattern_element_end,
                                                          end_result, &next_common_result_end, level + 1);

                                next_common_result.insert(next_common_result.end(),
                                                          next_common_result_end.begin(), next_common_result_end.end());
                                next_common_result_end.clear();
                            }

                            next_common_result_begin.clear();
                        }
                        next_common_result_element.clear();
                    }
                    else
                    {
                        next_common_result.insert(next_common_result.end(),
                                                  next_common_result_element.begin(), next_common_result_element.end());
                        next_common_result_element.clear();
                    }
                }

                pattern->insert(sc_hash_pair(SC_ADDR_LOCAL_TO_INT(pattern_arc), pattern_arc));
                pattern->insert(sc_hash_pair(SC_ADDR_LOCAL_TO_INT(next_pattern_element), next_pattern_element));
                if (next_pattern_element_is_node == SC_FALSE)
                {
                    pattern->insert(sc_hash_pair(SC_ADDR_LOCAL_TO_INT(next_pattern_element_begin), next_pattern_element_begin));
                    pattern->insert(sc_hash_pair(SC_ADDR_LOCAL_TO_INT(next_pattern_element_end), next_pattern_element_end));
                }

                out_common_result->insert(out_common_result->end(), next_common_result.begin(), next_common_result.end());
                new_common_result.insert(new_common_result.end(), next_common_result.begin(), next_common_result.end());
                next_common_result.clear();
                next_common_result_arc.clear();
            }
        }//const loop

        common_result.insert(common_result.begin(), new_common_result.begin(), new_common_result.end());

        new_common_result.clear();
        remove_all_elements(&del_result, &common_result);
        remove_all_elements(&del_result, out_common_result);
        free_result_vector(&del_result);
    }//pattern loop

    out_common_result->insert(out_common_result->end(), common_result.begin(), common_result.end());
    common_result.clear();
    cantorize_result_vector(out_common_result);

    return SC_TRUE;
}
예제 #23
0
	_SC_EXTERN ScAddr value(sc_uint8 idx) const
    {
        check_expr(idx < 3);
        check_expr(isValid());
		return ScAddr(sc_iterator3_value(mIterator, idx));
    }
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;
}