예제 #1
0
파일: mnrl.py 프로젝트: tjt7a/mnrl
    def addBoolean(self,
                   booleanType,
                   id=None,
                   report=False,
                   reportEnable=MNRLDefs.ENABLE_ALWAYS,
                   enable=MNRLDefs.ENABLE_ON_START_AND_ACTIVATE_IN,
                   reportId=None,
                   attributes={}):
        """Create a Boolean node, add it to the network, and return it"""
        id = self._getUniqueNodeId(id)

        try:
            number_of_ports = MNRLDefs.BOOLEAN_TYPES[booleanType]
        except KeyError:
            raise mnrlerror.InvalidGateType(booleanType)

        boolean = Boolean(booleanType,
                          portCount=number_of_ports,
                          id=id,
                          enable=enable,
                          report=report,
                          reportEnable=reportEnable,
                          reportId=reportId,
                          attributes=attributes)

        self.nodes[id] = boolean

        return boolean
예제 #2
0
파일: mnrl.py 프로젝트: tjt7a/mnrl
    def decode(self, json_string):
        default_obj = super(MNRLDecoder, self).decode(json_string)

        # build up a proper MNRL representation
        mnrl_obj = MNRLNetwork(default_obj['id'])

        # build up the mnrl network in two passes
        # 1. add all the nodes
        # 2. add all the connections

        for n in default_obj['nodes']:
            # for each node in the network, add it to the network
            if n['type'] == "state":
                node = State(n['attributes']['symbolSet'],
                             enable=MNRLDefs.fromMNRLEnable(n['enable']),
                             id=n['id'],
                             report=n['report'],
                             reportEnable=MNRLDefs.fromMNRLReportEnable(
                                 n.get('reportEnable', "always")),
                             latched=n['attributes']['latched']
                             if 'latched' in n['attributes'] else False,
                             reportId=n['attributes']['reportId']
                             if 'reportId' in n['attributes'] else None,
                             attributes=n['attributes'])
            elif n['type'] == "hState":
                node = HState(n['attributes']['symbolSet'],
                              enable=MNRLDefs.fromMNRLEnable(n['enable']),
                              id=n['id'],
                              report=n['report'],
                              reportEnable=MNRLDefs.fromMNRLReportEnable(
                                  n.get('reportEnable', "always")),
                              latched=n['attributes']['latched']
                              if 'latched' in n['attributes'] else False,
                              reportId=n['attributes']['reportId']
                              if 'reportId' in n['attributes'] else None,
                              attributes=n['attributes'])
            elif n['type'] == "upCounter":
                node = UpCounter(n['attributes']['threshold'],
                                 mode=MNRLDefs.fromMNRLCounterMode(
                                     n['attributes']['mode']),
                                 id=n['id'],
                                 report=n['report'],
                                 reportEnable=MNRLDefs.fromMNRLReportEnable(
                                     n.get('reportEnable', "always")),
                                 reportId=n['attributes']['reportId']
                                 if 'reportId' in n['attributes'] else None,
                                 attributes=n['attributes'])
            elif n['type'] == "Boolean":
                if n['attributes']['gateType'] not in MNRLDefs.BOOLEAN_TYPES:
                    raise mnrlerror.InvalidGateType(
                        n['attributes']['gateType'])
                node = Boolean(n['attributes']['gateType'],
                               portCounte=MNRLDefs.BOOLEAN_TYPES[
                                   n['attributes']['gateType']],
                               id=n['id'],
                               enable=MNRLDefs.fromMNRLEnable(n['enable']),
                               report=n['report'],
                               reportEnable=MNRLDefs.fromMNRLReportEnable(
                                   n.get('reportEnable', "always")),
                               reportId=n['attributes']['reportId']
                               if 'reportId' in n['attributes'] else None,
                               attributes=n['attributes'])
            else:
                # convert input defs into format needed for constructor
                ins = list()
                for k in n['inputDefs']:
                    ins.append((k['portId'], k['width']))

                # convert output defs into format needed for constructor
                outs = list()
                for k in n['outputDefs']:
                    outs.append((k['portId'], k['width']))

                node = MNRLNode(id=n['id'],
                                enable=MNRLDefs.fromMNRLEnable(n['enable']),
                                report=n['report'],
                                reportEnable=MNRLDefs.fromMNRLReportEnable(
                                    n.get('reportEnable', "always")),
                                inputDefs=ins,
                                outputDefs=outs,
                                attributes=n['attributes'])

            # add the node to the network
            mnrl_obj.addNode(node)

        for n in default_obj['nodes']:
            # for each node, add all the connections
            for k in n['outputDefs']:
                # for each output port
                for c in k['activate']:
                    mnrl_obj.addConnection((n['id'], k['portId']),
                                           (c['id'], c['portId']))

        return mnrl_obj