예제 #1
0
        def mini_calc(branch, info):
            # Mini calcualtions
            a_loc, opr, b_loc, dp = branch
            opr = opr.replace(" ", "")
            raw_a = dive_for_dot_values(a_loc,
                                        info,
                                        DEBUG=SUPER_DEBUG,
                                        as_val=1)
            if isinstance(b_loc, float) or isinstance(b_loc, int):
                raw_b = b_loc
            else:
                raw_b = dive_for_dot_values(b_loc,
                                            info,
                                            DEBUG=SUPER_DEBUG,
                                            as_val=1)
            bv = float(raw_b)
            av = float(raw_a)

            if opr == "-":
                final = av - bv
            elif opr == "+":
                final = av + bv
            else:
                print("<SECONDARY SLOT GETV> unknown opr {}".format(opr))
                final = av
            final = cbround(final, dp)  # Round to specified dp
            if SUPER_DEBUG: print("<SS MINI CALC>", av, opr, bv, "=", final)
            return final
예제 #2
0
        def _check_condition(an):
            csk = cstate["key"]
            curr_info = cinfo
            t_slots = an["trigger_slots"]
            t_states = an["trigger_states"]

            trigger_states = t_states
            correct_state = "_ANY" in trigger_states or csk in trigger_states

            if correct_state:
                slots_satisfied = False
                for ts in t_slots:
                    slot_is_AND = (ts.get("eval",
                                          "AND") == "AND")  # Default is AND
                    sn = ts.get("slotname", "")
                    ex_val = ts["value"]
                    info_vd = cu.dive_for_dot_values(sn, curr_info)
                    if len(info_vd) == 0:
                        if SUPER_DEBUG:
                            print("<ANNOUNCE> Slot", sn, "not found in info")
                    info_val = info_vd.get(sn, "")

                    if info_val == ex_val:
                        slots_satisfied = True
                    elif slot_is_AND:
                        # ALL slot values must be satisfied
                        return False  # One fail all fail

                return slots_satisfied
            return False
예제 #3
0
            def tree_search(tree, t_info):
                any_val_key = "_ANY"
                for slotname, sub_dict in list(tree.items()):
                    curr_d = t_info
                    # If finds something, returns. If not, breaks.
                    while True:
                        dive_dir = dive_for_dot_values(slotname,
                                                       curr_d)  # As dict
                        if dive_dir == {}:
                            # IF not found
                            # if SUPER_DEBUG: print("<SS TREE> ERROR {} not found".format(str(slotname)))
                            slot_val = ""
                            break

                        loc, slot_val = list(dive_dir.items())[0]
                        slot_val = str(
                            slot_val
                        )  # To convert ints to strings. I.e. for hours

                        # if SUPER_DEBUG: print("<SS TREE> Current slot:", loc, "| val:", slot_val)
                        # Check if key is in the subdict
                        if slot_val in sub_dict:
                            ss_branch = sub_dict[slot_val]
                            if isinstance(ss_branch, dict):
                                # Is a subtree
                                # if SUPER_DEBUG: print("<SS TREE> Found a subtree",ss_branch, "in",sub_dict)
                                slotname, sub_dict = list(ss_branch.items())[0]
                                continue

                            else:
                                # Is a leaf
                                out = get_value(ss_branch, t_info)
                                # Cut from info
                                pp = cu.dotpop(loc, curr_d)
                                # if SUPER_DEBUG: print("<SS TREE> pop leaf",pp)
                                return (True, out)
                        else:
                            # Fallback and look for _ANY match
                            a_branch = sub_dict.get(any_val_key, -1)
                            if not a_branch:
                                # Search failed
                                # if SUPER_DEBUG: print("<SS TREE> Val:", slot_val, "not found in:", sub_dict)
                                break

                            if isinstance(a_branch, dict):
                                # Is a _ANY branch
                                slotname, sub_dict = list(a_branch.items())[0]
                                continue

                            else:
                                # Is a _ANY leaf
                                out = get_value(a_branch, t_info)
                                # Cut from info
                                pp = cu.dotpop(loc, curr_d)
                                # if SUPER_DEBUG: print("<SS TREE> pop _ANY leaf",pp)
                                return (True, out)

                # if SUPER_DEBUG: print("<SECONDARY SLOT> TREE SEARCH FAILED",tree)
                return (False, "")
예제 #4
0
        def get_variables(f, enh):
            def add_formula_conditional_vars(f, vd):
                ret = {}
                # This assumes all conditions are joined by AND
                conds = f.get("conditions", [])
                for cond in conds:
                    k, v, setval = cond
                    vkey, tval, fval = setval

                    if not k in vd:
                        print("<COND VALS> WARNING {} not in info".format(k))
                        met = False
                    else:
                        if isinstance(v, list):
                            for val in v:
                                met = (vd[k] == val)
                                if met: break
                        else:
                            met = (vd[k] == v)  # Simple match

                    ret[vkey] = tval if met else fval

                vd.update(ret)
                return

            # Fetch mandatory values
            reqvar_key = "req_vars"
            opvar_key = "optional_vars"
            req_vars = f.get(reqvar_key, [])
            vd = cu.dive_for_dot_values(req_vars, enh)

            # Fetch optional values
            op_vars = f.get(opvar_key, [])  # If not found, value = 0
            op_vars_d = cu.dive_for_dot_values(op_vars, enh, failzero=True)
            vd.update(op_vars_d)

            # Fetch conditional values
            add_formula_conditional_vars(f, vd)
            if self.SUPER_DEBUG: print("<RESOLVE FORMULA> Value Dict", vd)
            return vd
예제 #5
0
            def get_leaf_value(branch, info):
                if isinstance(branch, list):
                    final = cu.dive_for_dot_values(branch,
                                                   info,
                                                   DEBUG=SUPER_DEBUG,
                                                   as_val=1)

                    if final == {}:
                        if DEBUG:
                            print("<SECONDARY SLOT GETV> {} not found in info".
                                  format(branch))
                        final = ""
                    return final

                # If not, is a raw value
                return branch
예제 #6
0
        def get_value(branch, info):
            if isinstance(branch, list):
                minicalc_bool = len(branch) > 1

                if minicalc_bool:
                    final = mini_calc(branch, info)
                else:
                    final = dive_for_dot_values(branch,
                                                info,
                                                DEBUG=SUPER_DEBUG,
                                                as_val=1)

                if final == {}:
                    if DEBUG:
                        print("<SECONDARY SLOT GETV> {} not found in info".
                              format(branch))
                    final = ""
                return final

            # If not, is a raw value
            return branch
예제 #7
0
            def tree_search(tree, t_info):
                for slotname, sub_dict in list(tree.items()):
                    dive_dict = cu.dive_for_dot_values(slotname,
                                                       t_info)  # As dict
                    if dive_dict == {}:
                        # IF not found
                        if SUPER_DEBUG:
                            print("<TREE> ERROR {} not found".format(
                                str(slotname)))
                        slot_val = ""
                        break

                    loc, slot_val = list(dive_dict.items())[0]
                    slot_val = str(
                        slot_val
                    )  # Convert to strings because json keys are strings. I.e. for hours

                    if SUPER_DEBUG:
                        print("<TREE> Current slot:", loc, "| val:", slot_val)

                    # Check if curr_info detail's value is in the subdict
                    if slot_val in sub_dict:
                        matched_branch = sub_dict[slot_val]
                        if isinstance(matched_branch, dict):
                            # Is a subtree
                            sub_tree = matched_branch
                            if SUPER_DEBUG:
                                print("<TREE> Subtree found. Searching:",
                                      str(sub_dict))
                            found, returned = tree_search(sub_tree, t_info)
                            if found:
                                return (found, returned)
                            continue

                        else:
                            # Is a leaf
                            out = get_leaf_value(matched_branch, t_info)
                            pp = cu.dotpop(loc, t_info)  # Cut from info
                            if SUPER_DEBUG: print("<TREE> pop leaf", pp)
                            return (True, out)
                    else:
                        # Fallback and look for _ANY match
                        a_branch = sub_dict.get(any_val_key, -1)
                        if not a_branch:
                            # Search failed
                            if SUPER_DEBUG:
                                print("<TREE> Val:", slot_val, "not found in:",
                                      sub_dict)
                            break

                        if isinstance(a_branch, dict):
                            # Is a _ANY branch
                            slotname, sub_tree = list(a_branch.items())[0]
                            found, returned = tree_search(sub_tree, t_info)
                            if found:
                                return (found, returned)
                            continue

                        else:
                            # Is a _ANY leaf
                            out = get_leaf_value(a_branch, t_info)
                            # Cut from info
                            pp = cu.dotpop(loc, t_info)
                            if SUPER_DEBUG: print("<TREE> pop _ANY leaf", pp)
                            return (True, out)

                return (False, "")