Esempio n. 1
0
scp_result resolve_scp_event_type(sc_memory_context *context, scp_operand *element, sc_event_type *type)
{
    if (SCP_RESULT_TRUE == ifCoin(context, &sc_event_add_input_arc, element))
    {
        *type = SC_EVENT_ADD_INPUT_ARC;
        return SCP_RESULT_TRUE;
    }
    if (SCP_RESULT_TRUE == ifCoin(context, &sc_event_add_output_arc, element))
    {
        *type = SC_EVENT_ADD_OUTPUT_ARC;
        return SCP_RESULT_TRUE;
    }
    if (SCP_RESULT_TRUE == ifCoin(context, &sc_event_add_input_arc, element))
    {
        *type = SC_EVENT_ADD_INPUT_ARC;
        return SCP_RESULT_TRUE;
    }
    if (SCP_RESULT_TRUE == ifCoin(context, &sc_event_remove_input_arc, element))
    {
        *type = SC_EVENT_REMOVE_INPUT_ARC;
        return SCP_RESULT_TRUE;
    }
    if (SCP_RESULT_TRUE == ifCoin(context, &sc_event_remove_output_arc, element))
    {
        *type = SC_EVENT_REMOVE_OUTPUT_ARC;
        return SCP_RESULT_TRUE;
    }
    return SCP_RESULT_FALSE;
}
void append_all_relation_elements_to_hash_with_modifiers(sc_memory_context *context, GHashTable *table, scp_operand *set, scp_operand *parameter_set, scp_operand *const_set, scp_operand *vars_set)
{
    scp_operand arc1, arc2, curr_operand, operand_arc, modifier, modifier_arc,
                operand_element, operand_element_arc, operand_element_modifier, operand_element_modifier_arc;
    scp_iterator3 *it1, *it2, *it3;
    MAKE_DEFAULT_ARC_ASSIGN(arc1);
    MAKE_DEFAULT_ARC_ASSIGN(arc2);
    MAKE_DEFAULT_ARC_ASSIGN(operand_arc);
    MAKE_DEFAULT_ARC_ASSIGN(modifier_arc);
    MAKE_DEFAULT_ARC_ASSIGN(operand_element_arc);
    MAKE_DEFAULT_ARC_ASSIGN(operand_element_modifier_arc);
    modifier_arc.erase = SCP_TRUE;
    operand_element_modifier_arc.erase = SCP_TRUE;
    MAKE_DEFAULT_OPERAND_ASSIGN(curr_operand);
    MAKE_DEFAULT_OPERAND_ASSIGN(operand_element);
    MAKE_DEFAULT_OPERAND_ASSIGN(operand_element_modifier);
    MAKE_DEFAULT_OPERAND_ASSIGN(modifier);
    // Operands loop
    it1 = scp_iterator3_new(context, set, &arc1, &curr_operand);
    while (SCP_RESULT_TRUE == scp_iterator3_next(context, it1, set, &operand_arc, &curr_operand))
    {
        curr_operand.param_type = SCP_FIXED;
        operand_arc.param_type = SCP_FIXED;

        append_to_hash(table, &curr_operand);
        append_to_hash(table, &operand_arc);

        // Operand modifiers loop
        it2 = scp_iterator3_new(context, &modifier, &arc1, &operand_arc);
        while (SCP_RESULT_TRUE == scp_iterator3_next(context, it2, &modifier, &modifier_arc, &operand_arc))
        {
            modifier.param_type = SCP_FIXED;
            modifier_arc.param_type = SCP_FIXED;

            append_to_hash(table, &modifier_arc);

            modifier_arc.param_type = SCP_ASSIGN;
            modifier.param_type = SCP_ASSIGN;
        }
        scp_iterator3_free(it2);

        // Operand elements loop
        it2 = scp_iterator3_new(context, &curr_operand, &operand_element_arc, &operand_element);
        while (SCP_RESULT_TRUE == scp_iterator3_next(context, it2, &curr_operand, &operand_element_arc, &operand_element))
        {
            operand_element.param_type = SCP_FIXED;
            operand_element_arc.param_type = SCP_FIXED;

            append_to_hash(table, &operand_element);
            append_to_hash(table, &operand_element_arc);

            // Operand element modifiers loop
            it3 = scp_iterator3_new(context, &operand_element_modifier, &operand_element_modifier_arc, &operand_element_arc);
            while (SCP_RESULT_TRUE == scp_iterator3_next(context, it3, &operand_element_modifier, &operand_element_modifier_arc, &operand_element_arc))
            {
                operand_element_modifier.param_type = SCP_FIXED;
                operand_element_modifier_arc.param_type = SCP_FIXED;

                // Variable case
                if (SCP_RESULT_TRUE == ifCoin(context, &rrel_scp_var, &operand_element_modifier))
                {
                    if (SCP_RESULT_TRUE == searchElStr5(context, parameter_set, &arc1, &operand_element, &arc2, &rrel_in))
                    {
                        if (SCP_RESULT_TRUE == ifVarAssign(context, &modifier_arc))
                        {
                            eraseEl(context, &operand_element_modifier_arc);
                        }
                        genElStr3(context, &rrel_scp_const, &arc1, &operand_element_arc);

                        arc1.param_type = SCP_FIXED;
                        append_to_hash(table, &arc1);
                        arc1.param_type = SCP_ASSIGN;

                        genElStr3(context, const_set, &arc1, &operand_element);
                    }
                    else
                    {
                        append_to_hash(table, &operand_element_modifier_arc);
                        genElStr3(context, vars_set, &arc1, &operand_element);
                    }
                }
                else
                {
                    append_to_hash(table, &operand_element_modifier_arc);
                    // Constant case
                    if (SCP_RESULT_TRUE == ifCoin(context, &rrel_scp_const, &operand_element_modifier))
                    {
                        genElStr3(context, const_set, &arc1, &operand_element);
                    }
                }

                operand_element_modifier_arc.param_type = SCP_ASSIGN;
                operand_element_modifier.param_type = SCP_ASSIGN;
            }
            scp_iterator3_free(it3);

            operand_element_arc.param_type = SCP_ASSIGN;
            operand_element.param_type = SCP_ASSIGN;
        }
        scp_iterator3_free(it2);

        operand_arc.param_type = SCP_ASSIGN;
        curr_operand.param_type = SCP_ASSIGN;
    }
    scp_iterator3_free(it1);
}
scp_result gen_system_structures(sc_memory_context *context, scp_operand *operator_set, scp_operand *parameter_set, scp_operand *vars_set, scp_operand *const_set, scp_operand *copying_consts_set, scp_operand *operators_copying_pattern)
{
    scp_operand arc1, arc2, curr_operator, operator_arc, operator_type, operator_input, curr_operand, operand_arc, modifier, modifier_arc,
                operand_arc_pattern;
    scp_iterator3 *it, *it1, *it2;
    GHashTable *table = g_hash_table_new(NULL, NULL);
    scp_bool cop_const = SCP_FALSE, is_const = SCP_FALSE, order_rel = SCP_FALSE;

    MAKE_DEFAULT_ARC_ASSIGN(arc1);
    MAKE_DEFAULT_ARC_ASSIGN(arc2);
    MAKE_DEFAULT_ARC_ASSIGN(operator_arc);
    MAKE_DEFAULT_OPERAND_ASSIGN(operand_arc);
    MAKE_DEFAULT_ARC_ASSIGN(modifier_arc);
    modifier_arc.erase = SCP_TRUE;
    MAKE_DEFAULT_OPERAND_ASSIGN(curr_operator);
    MAKE_DEFAULT_OPERAND_ASSIGN(curr_operand);
    MAKE_DEFAULT_OPERAND_ASSIGN(operator_arc);
    MAKE_DEFAULT_OPERAND_ASSIGN(operator_type);
    MAKE_DEFAULT_OPERAND_ASSIGN(operator_input);
    MAKE_DEFAULT_OPERAND_ASSIGN(modifier);
    MAKE_DEFAULT_OPERAND_FIXED(operand_arc_pattern);
    operand_arc_pattern.element_type = scp_type_arc_common | scp_type_const;
    append_to_hash(table, operator_set);
    it = scp_iterator3_new(context, operator_set, &operator_arc, &curr_operator);
    while (SCP_RESULT_TRUE == scp_iterator3_next(context, it, operator_set, &operator_arc, &curr_operator))
    {
        curr_operator.param_type = SCP_FIXED;
        operator_arc.param_type = SCP_FIXED;

        if (SCP_RESULT_TRUE == searchElStr3(context, &rrel_init, &arc1, &operator_arc))
        {
            append_to_hash(table, &arc1);
        }

        append_to_hash(table, &curr_operator);
        append_to_hash(table, &operator_arc);

        // Operator type loop
        operator_type.param_type = SCP_ASSIGN;
        operator_input.param_type = SCP_ASSIGN;
        it1 = scp_iterator3_new(context, &operator_input, &arc1, &curr_operator);
        while (SCP_RESULT_TRUE == scp_iterator3_next(context, it1, &operator_input, &arc1, &curr_operator))
        {
            operator_input.param_type = SCP_FIXED;

            if (SCP_RESULT_TRUE == searchElStr3(context, &scp_operator_atomic_type, &arc2, &operator_input))
            {
                append_to_hash(table, &arc1);
                operator_type.addr = operator_input.addr;
                operator_input.param_type = SCP_ASSIGN;
                continue;
            }

            if (SCP_RESULT_TRUE == ifCoin(context, &scp_operator_executable_after_all_previous, &operator_input)
                || SCP_RESULT_TRUE == ifCoin(context, &scp_operator_executable_after_one_of_previous, &operator_input))
            {
                append_to_hash(table, &arc1);
                operator_input.param_type = SCP_ASSIGN;
                continue;
            }

            operator_input.param_type = SCP_ASSIGN;
        }
        scp_iterator3_free(it1);
        operator_type.param_type = SCP_FIXED;

        // Operands loop
        it1 = scp_iterator3_new(context, &curr_operator, &operand_arc, &curr_operand);
        while (SCP_RESULT_TRUE == scp_iterator3_next(context, it1, &curr_operator, &operand_arc, &curr_operand))
        {
            curr_operand.param_type = SCP_FIXED;
            operand_arc.param_type = SCP_FIXED;
            cop_const = SCP_FALSE;
            is_const = SCP_FALSE;
            order_rel = SCP_FALSE;

            //Order relation case
            operand_arc_pattern.addr = operand_arc.addr;
            if (SCP_TRUE == ifType(context, &operand_arc_pattern))
            {
                if (SCP_TRUE != searchElStr3(context, operator_set, &arc1, &curr_operand))
                {
                    operand_arc.param_type = SCP_ASSIGN;
                    curr_operand.param_type = SCP_ASSIGN;
                    continue;
                }
                else
                {
                    order_rel = SCP_TRUE;
                }
            }

            append_to_hash(table, &curr_operand);
            append_to_hash(table, &operand_arc);

            // Operand modifiers loop
            it2 = scp_iterator3_new(context, &modifier, &modifier_arc, &operand_arc);
            while (SCP_RESULT_TRUE == scp_iterator3_next(context, it2, &modifier, &modifier_arc, &operand_arc))
            {
                modifier.param_type = SCP_FIXED;
                modifier_arc.param_type = SCP_FIXED;

                if (SCP_TRUE == order_rel)
                {
                    if (SCP_TRUE == ifCoin(context, &nrel_goto, &modifier)
                        || SCP_TRUE == ifCoin(context, &nrel_else, &modifier)
                        || SCP_TRUE == ifCoin(context, &nrel_then, &modifier)
                        || SCP_TRUE == ifCoin(context, &nrel_error, &modifier))
                    {
                        append_to_hash(table, &modifier_arc);
                        modifier_arc.param_type = SCP_ASSIGN;
                        modifier.param_type = SCP_ASSIGN;
                        break;
                    }
                    else
                    {
                        modifier_arc.param_type = SCP_ASSIGN;
                        modifier.param_type = SCP_ASSIGN;
                        continue;
                    }
                }

                if (SCP_RESULT_TRUE == ifCoin(context, &operator_type, &op_call)
                    && SCP_RESULT_TRUE == ifCoin(context, &modifier, ordinal_rrels + 2))
                {
                    cop_const = SCP_TRUE;
                    append_all_set_elements_to_hash_with_modifiers(context, table, &curr_operand, parameter_set, const_set, vars_set);
                }

                if (((SCP_RESULT_TRUE == ifCoin(context, &operator_type, &op_sys_gen)) || (SCP_RESULT_TRUE == ifCoin(context, &operator_type, &op_sys_search))) &&
                    ((SCP_RESULT_TRUE == ifCoin(context, &modifier, ordinal_rrels + 2)) || (SCP_RESULT_TRUE == ifCoin(context, &modifier, ordinal_rrels + 3))))
                {
                    cop_const = SCP_TRUE;
                    append_all_relation_elements_to_hash_with_modifiers(context, table, &curr_operand, parameter_set, const_set, vars_set);
                }

                // Variable case
                if (SCP_RESULT_TRUE == ifCoin(context, &rrel_scp_var, &modifier))
                {
                    if (SCP_RESULT_TRUE == searchElStr5(context, parameter_set, &arc1, &curr_operand, &arc2, &rrel_in))
                    {
                        if (SCP_RESULT_TRUE == ifVarAssign(context, &modifier_arc))
                        {
                            eraseEl(context, &modifier_arc);
                        }
                        genElStr3(context, &rrel_scp_const, &arc1, &operand_arc);

                        arc1.param_type = SCP_FIXED;
                        append_to_hash(table, &arc1);
                        arc1.param_type = SCP_ASSIGN;

                        genElStr3(context, const_set, &arc1, &curr_operand);
                    }
                    else
                    {
                        append_to_hash(table, &modifier_arc);
                        genElStr3(context, vars_set, &arc1, &curr_operand);
                    }
                }
                else
                {
                    //!TODO Check unnesseccary arcs
                    append_to_hash(table, &modifier_arc);
                    // Constant case
                    if (SCP_RESULT_TRUE == ifCoin(context, &rrel_scp_const, &modifier))
                    {
                        is_const = SCP_TRUE;
                    }
                }

                modifier_arc.param_type = SCP_ASSIGN;
                modifier.param_type = SCP_ASSIGN;
            }
            scp_iterator3_free(it2);

            if (SCP_TRUE == order_rel)
            {
                operand_arc.param_type = SCP_ASSIGN;
                curr_operand.param_type = SCP_ASSIGN;
                continue;
            }

            if (is_const == SCP_TRUE)
            {
                if (cop_const == SCP_TRUE)
                {
                    genElStr3(context, copying_consts_set, &arc1, &curr_operand);
                }
                else
                {
                    genElStr3(context, const_set, &arc1, &curr_operand);
                }
            }

            operand_arc.param_type = SCP_ASSIGN;
            curr_operand.param_type = SCP_ASSIGN;
        }
        scp_iterator3_free(it1);

        operator_arc.param_type = SCP_ASSIGN;
        curr_operator.param_type = SCP_ASSIGN;
    }
    scp_iterator3_free(it);

    cantorize_set(context, const_set);
    cantorize_set(context, copying_consts_set);
    cantorize_set(context, vars_set);
    //printEl(copying_consts_set);
    //printEl(context, vars_set);printEl(context, vars_set);
    //printEl(context, const_set);
    //printf("SIZE: %d\n", g_hash_table_size(table));
    gen_set_from_hash(table, operators_copying_pattern);
    g_hash_table_destroy(table);
    return SCP_RESULT_TRUE;
}
sc_result destroy_scp_process(const sc_event *event, sc_addr arg)
{
    scp_operand arc1, arc2, node1, curr_operator, node3, scp_process_node, operator_type, question_node, call_parameters;
    scp_iterator3 *it;
    GHashTable *params = nullptr;
    scp_result params_found;
    MAKE_DEFAULT_OPERAND_FIXED(arc1);
    MAKE_DEFAULT_NODE_ASSIGN(scp_process_node);
    arc1.addr = arg;
    arc1.element_type = scp_type_arc_pos_const_perm;
    if (SCP_RESULT_TRUE != ifType(s_default_ctx, &arc1))
    {
        return SC_RESULT_OK;
    }

    MAKE_DEFAULT_OPERAND_ASSIGN(node1);
    if (SCP_RESULT_TRUE != searchElStr3(s_default_ctx, &node1, &arc1, &scp_process_node))
    {
        return SC_RESULT_ERROR;
    }

    MAKE_DEFAULT_ARC_ASSIGN(arc1);
    scp_process_node.param_type = SCP_FIXED;

    MAKE_DEFAULT_ARC_ASSIGN(arc2);
    MAKE_DEFAULT_OPERAND_ASSIGN(curr_operator);
    MAKE_DEFAULT_OPERAND_ASSIGN(question_node);
    MAKE_DEFAULT_OPERAND_ASSIGN(call_parameters);
    MAKE_DEFAULT_OPERAND_ASSIGN(node3);
    node1.erase = SCP_TRUE;
    curr_operator.erase = SCP_TRUE;
    node3.erase = SCP_TRUE;

    MAKE_DEFAULT_OPERAND_ASSIGN(question_node);
    MAKE_COMMON_ARC_ASSIGN(arc1);
    params_found = searchElStr5(s_default_ctx, &question_node, &arc1, &scp_process_node, &arc2, &nrel_scp_process);
    question_node.param_type = SCP_FIXED;

    MAKE_DEFAULT_ARC_ASSIGN(arc1);
    if (params_found == SCP_RESULT_TRUE)
    {
        MAKE_DEFAULT_OPERAND_ASSIGN(call_parameters);
        params_found = searchElStr5(s_default_ctx, &question_node, &arc1, &call_parameters, &arc2, &ordinal_rrels[2]);
        call_parameters.param_type = SCP_FIXED;
        if (params_found == SCP_RESULT_TRUE)
        {
            params = g_hash_table_new(NULL, NULL);
            load_set_to_hash(s_default_ctx, &call_parameters, params);
        }
    }

    MAKE_DEFAULT_NODE_ASSIGN(operator_type);

    it = scp_iterator3_new(s_default_ctx, &scp_process_node, &arc1, &curr_operator);
    while (SCP_RESULT_TRUE == scp_iterator3_next(s_default_ctx, it, &scp_process_node, &arc1, &curr_operator))
    {
        curr_operator.param_type = SCP_FIXED;
        if (SCP_RESULT_TRUE != resolve_operator_type(s_default_ctx, &curr_operator, &operator_type))
        {
            curr_operator.param_type = SCP_ASSIGN;
            delete_vars_from_set(s_default_ctx, &curr_operator, params);
            eraseEl(s_default_ctx, &curr_operator);
            continue;
        }
        operator_type.param_type = SCP_FIXED;

        if (SCP_RESULT_TRUE == ifCoin(s_default_ctx, &operator_type, &op_call))
        {
            //printf("OPERATOR CALL\n");
            if (SCP_RESULT_TRUE == searchElStr5(s_default_ctx, &curr_operator, &arc1, &node3, &arc2, &(ordinal_rrels[2])))
            {
                node3.param_type = SCP_FIXED;
                delete_vars_from_set(s_default_ctx, &node3, params);
                eraseEl(s_default_ctx, &node3);
                node3.param_type = SCP_ASSIGN;
            }
            delete_vars_from_set(s_default_ctx, &curr_operator, params);
            eraseEl(s_default_ctx, &curr_operator);
            operator_type.param_type = SCP_ASSIGN;
            curr_operator.param_type = SCP_ASSIGN;
            continue;
        }

        if (SCP_RESULT_TRUE == ifCoin(s_default_ctx, &operator_type, &op_sys_gen) ||
            SCP_RESULT_TRUE == ifCoin(s_default_ctx, &operator_type, &op_sys_search))
        {
            if (SCP_RESULT_TRUE == searchElStr5(s_default_ctx, &curr_operator, &arc1, &node3, &arc2, &(ordinal_rrels[2])))
            {
                node3.param_type = SCP_FIXED;
                delete_vars_from_relation(s_default_ctx, &node3, params);
                eraseEl(s_default_ctx, &node3);
                node3.param_type = SCP_ASSIGN;
            }
            if (SCP_RESULT_TRUE == searchElStr5(s_default_ctx, &curr_operator, &arc1, &node3, &arc2, &(ordinal_rrels[3])))
            {
                node3.param_type = SCP_FIXED;
                delete_vars_from_relation(s_default_ctx, &node3, params);
                eraseEl(s_default_ctx, &node3);
                node3.param_type = SCP_ASSIGN;
            }
            delete_vars_from_set(s_default_ctx, &curr_operator, params);
            eraseEl(s_default_ctx, &curr_operator);
            operator_type.param_type = SCP_ASSIGN;
            curr_operator.param_type = SCP_ASSIGN;
            continue;
        }
        delete_vars_from_set(s_default_ctx, &curr_operator, params);
        eraseEl(s_default_ctx, &curr_operator);
        operator_type.param_type = SCP_ASSIGN;
        curr_operator.param_type = SCP_ASSIGN;
    }
    scp_iterator3_free(it);
    scp_process_node.erase = SCP_TRUE;
    eraseEl(s_default_ctx, &scp_process_node);

    if (SCP_RESULT_TRUE == params_found)
    {
        g_hash_table_destroy(params);
    }

    //printf("PROCESS DESTROYED SUCCESSFULLY\n");
    return SC_RESULT_OK;
}