Example #1
0
    def __init__(self, notation, cam):
        """
        Parses the passed notation and saves values into members.

        @sfunc: Represents the function that returns the next given state.
        @ruleset: A created ruleset that matches always
        @offsets: Represents the Moore neighborhood corresponding to the given CAM
        """
        self.sfunc = None
        self.offsets = c.Configuration.moore(cam.master)
        self.ruleset = r.Ruleset(r.Ruleset.Method.ALWAYS_PASS)

        if re.match(CAMParser.MCELL_FORMAT, notation):
            x, y = notation.split('/')
            if all(map(self._numasc, [x, y])):
                self.sfunc = self._mcell(x, y)
            else:
                raise ValueError("Non-ascending values in MCELL format")

        elif re.match(CAMParser.RLE_FORMAT, notation):
            B, S = map(lambda x: x[1:], notation.split('/'))
            if all(map(self._numasc, [B, S])):
                self.sfunc = self._mcell(S, B)
            else:
                raise ValueError("Non-ascending values in RLE format")

        else:
            raise ValueError("No supported format passed to parser.")

        # Add configuration to given CAM
        config = c.Configuration(self.sfunc, plane=cam.master, offsets=self.offsets)
        self.ruleset.configurations.append(config)
Example #2
0
def pre_rule():
    print('single: generating rule set...')

    traffic_pkl = setting.SINGLE_TRAFFIC_LOGFILE
    rate_arr = [0.1 * i for i in range(1, 10)]
    for rate in rate_arr:
        ruleset_pkl = 'single_rule_{}.pkl'.format(rate)
        print(ruleset_pkl)
        rs = ruleset.Ruleset()
        rs.generate_ruleset_from_traffic(traffic_pkl, 24, rate)
        element.serialize(rs, ruleset_pkl)
    return
Example #3
0
    def test_alwaysPassRuleset(self):
        """

        """
        # No configurations
        tmp_r = r.Ruleset(r.Ruleset.Method.ALWAYS_PASS)
        tmp_p = self.plane2d.bits.copy()
        tmp_r.apply_to(self.plane2d)
        assert tmp_p == self.plane2d.bits

        # One configuration
        tmp_r.configurations.append(self.config)
        tmp_r.apply_to(self.plane2d)
        assert tmp_p != self.plane2d.bits
        assert self.plane2d.bits.count() == 100 * 100
Example #4
0
def pre_classbench_rule():
    print('generating classbench rule set...')

    rs = ruleset.Ruleset()
    rs.generate_ruleset_from_classbench(setting.CB_RULE,
                                        setting.CB_TRACE,
                                        minpri=8)
    cnt = 0
    tot = len(rs.ruleset)
    for (priority, _) in rs.ruleset:
        if priority == 32: cnt += 1
    print(cnt)
    print(tot)

    element.serialize(rs, setting.CB_RULE_PKL)

    return
Example #5
0
    def test_matchRuleset(self):
        """

        """
        # No configurations
        tmp_r = r.Ruleset(r.Ruleset.Method.MATCH)
        tmp_p = self.plane2d.bits.copy()
        tmp_r.apply_to(self.plane2d)
        assert tmp_p == self.plane2d.bits

        # One configuration
        tmp_r.configurations.append(self.config)
        tmp_r.apply_to(self.plane2d)
        assert tmp_p == self.plane2d.bits
        self.plane2d[[(-1, 0), (0, 1), (1, 1)]] = 1
        tmp_r.apply_to(self.plane2d)
        assert self.plane2d.bits.count() == 4
Example #6
0
def get_rulesets(ruledir, recurse):
    """
    List of ruleset objects extracted from the yaml directory
    """
    if os.path.isdir(ruledir) and recurse:
        yaml_files = [
            y for x in os.walk(ruledir)
            for y in glob(os.path.join(x[0], '*.yaml'))
        ]
    elif os.path.isdir(ruledir) and not recurse:
        yaml_files = get_files(ruledir, 'yaml')
    elif os.path.isfile(ruledir):
        yaml_files = [ruledir]
    extracted_files = extract_yaml(yaml_files)
    rulesets = []
    for extracted_yaml in extracted_files:
        rulesets.append(ruleset.Ruleset(extracted_yaml))
    return rulesets
Example #7
0
    def test_tolerateRuleset(self):
        """

        """
        # No configurations
        tmp_r = r.Ruleset(r.Ruleset.Method.TOLERATE)
        tmp_p = self.plane2d.bits.copy()
        tmp_r.apply_to(self.plane2d)
        assert tmp_p == self.plane2d.bits

        # One configuration
        tmp_r.configurations.append(self.config)
        tmp_r.apply_to(self.plane2d, 0.5)
        assert tmp_p == self.plane2d.bits

        self.plane2d[(-1, 0)] = 1
        tmp_r.apply_to(self.plane2d, 0.5)
        assert self.plane2d.bits.count() == 4
Example #8
0
def get_rulesets(ruledir, recurse):
    """
    List of ruleset objects extracted from the yaml directory
    """
    if os.path.isdir(ruledir) and recurse:
        yaml_files = []
        for root, dirs, files in os.walk(ruledir):
            for name in files:
                filename, file_extension = os.path.splitext(name)
                if file_extension == '.yaml':
                    yaml_files.append(os.path.join(root, name))
    if os.path.isdir(ruledir) and not recurse:
        yaml_files = util.get_files(ruledir, 'yaml')
    elif os.path.isfile(ruledir):
        yaml_files = [ruledir]
    extracted_files = util.extract_yaml(yaml_files)
    rulesets = []
    for extracted_yaml in extracted_files:
        rulesets.append(ruleset.Ruleset(extracted_yaml))
    return rulesets
Example #9
0
def pre_brain(pkl_file):
    print('generating brain rule and trace...')

    print('brain: transform {} into {}'.format(pkl_file,
                                               setting.BRAIN_TRAFFIC_LOGFILE))

    topo = setting.BRAIN

    n = network.Network(topo)
    n.generate_real_traffic(pkl_file)
    n.traffic.serialize(setting.BRAIN_TRAFFIC_LOGFILE)
    n.traffic.print_traffic_data(setting.BRAIN_TRAFFIC_DATA)

    print('flow number: {}'.format(n.traffic.flownum[-1]))

    print('brain: generating rule set...')

    traffic_pkl = setting.BRAIN_TRAFFIC_LOGFILE
    ruleset_pkl = setting.BRAIN_RULE_PKL
    rs = ruleset.Ruleset()
    rs.generate_ruleset_from_traffic(traffic_pkl, 24, 0.5)
    element.serialize(rs, ruleset_pkl)

    return
Example #10
0
# ----- PGLOBALS VARIABLES -----

# debug flag 
# rintd, assertd (often used as function I/O checks) and other potential statements only execute if this flag is enabled
# DEBUG generally causes more verbose output
# DEUBG makes the program more likely to halt due to unexpected inputs/outputs/conditions
# considering these factors, enabling DEBUG may cause the script to run significantly slower
# if DEBUG is disabled, it may prevent timing.py timing, or it may override this and continue functioning regardless of DEBUG
DEBUG = False

# main pokepy client
client = pokepy.V2Client()

# ruleset
rs = ruleset.Ruleset(0)

# main list of teams used by players
teams = []

# number of teams to build per instance of "teammake" run
default_num_teams = 2

# name of "Pokedex" folder
dex_folder = "../res/dex_entries/"

# list of string pokemon types
all_type_names = ['bug', 'dark', 'dragon', 'electric', 'fighting', 'fire', 'flying', 'ghost', 'grass', 'ground', 'ice', 'normal', 
'poison', 'psychic', 'rock', 'steel', 'water']

# ideally write this out to a file somewhere or cache it or smt