def GliderTest(): def dieFunction(state): return 0 def stayFunction(state): return state def birthFunction(state): return 1 TestRule = rule.Rule(True, 3, 2, [1, 1], False, dieFunction) TestRule.addRule([2], stayFunction) TestRule.addRule([3], birthFunction) TestRule = fileSystem.loadRule(fileSystem.getProjectRoot() + 'Python/Rules/conways.rule') TestMap = map.Map([60, 60], [True, True], 0) TestMap[2][1] = 1 TestMap[3][2] = 1 TestMap[4][2] = 1 TestMap[2][3] = 1 TestMap[3][3] = 1 TestMap.print2D() TestTime = Time(TestMap, TestRule) while True: TestTime.update({'draw': True})
def __init__(self, dimensions, times): # initialize shared mem # TODO: if we want to support multiple maps # we have to generate unique names for the # shared memory for each map, and pass # those as a parameter to the C++ code memsize = sizeof(TransferData) # Create new empty file to back memory map on disk fd = os.open(getProjectRoot() + 'tmp/3DLifeShmem', os.O_CREAT | os.O_TRUNC | os.O_RDWR) # Zero out the file to ensure it's the right size assert os.write(fd, b'\x00' * memsize) == memsize # Create the mmap instace with the following params: # fd: File descriptor which backs the mapping or -1 for anonymous mapping # length: Must in multiples of mmap.PAGESIZE (usually 4 KB) # flags: MAP_SHARED means other processes can share this mmap # prot: PROT_WRITE means this process can write to this mmap if platform == "linux" or platform == "linux2": # linux self.shmem = mmap.mmap(fd, memsize, mmap.MAP_SHARED, mmap.PROT_WRITE) elif platform == "win32": # self.shmem = mmap.mmap(fd, memsize, access=mmap.ACCESS_WRITE) self.shmem = mmap.mmap(fd, memsize) # Windows... # self.shmem = mmap(-1, sizeof(self.transferData), "TransferDataSHMEM") self.setDimensions(dimensions, times)
def createMap(self): if self.dimensions == len(self.wrap) and self.dimensions == len( self.axisLengths): fname = QFileDialog.getSaveFileName(self, 'Save Map', fileSystem.getProjectRoot(), "Map files (*.map)") if fname[0]: print(self.axisLengths, self.wrap, self.outerState) newMap = map.Map(self.axisLengths, self.wrap, self.outerState) newMap.saveMap(fname[0]) self.parent.map = fileSystem.loadMap(fname[0]) self.parent.mapNameL.setText("Map: " + fname[0][fname[0].rfind('/') + 1:len(fname[0])]) self.parent.statusBar().showMessage( "Map: " + fname[0][fname[0].rfind('/') + 1:len(fname[0])]) self.close()
def exportRule(self): fname = QFileDialog.getSaveFileName(self, 'Save Rule', fileSystem.getProjectRoot(), "Rule files (*.rule)") if fname[0]: f = open(fname[0], 'w+') f.write('moorelian:' + str(self.moorelian).upper() + '\n') f.write('neighbourhoodSize:' + str(self.neighbourhoodSize) + '\n') f.write('nStates:' + str(self.nStates) + '\n') s = '' for i in self.centre: s += str(i) + ', ' s = s[:-2] f.write('center:' + s + '\n') f.write('countCenter:' + str(self.countCentre).upper() + '\n') f.write('\n') f.write("usePositionTree:" + str(self.usePositionTree).upper() + '\n') f.write('baseRule:' + str(self.baseRule) + '\n') for s in self.conditionSet: f.write(s + '\n') f.write('\n') f.write( "CenterStates(seperated by commas):NeighbourNumbers(seperated by commas):ResultingState ##Don't remove this line##" ) f.close() self.parent.rule = fileSystem.loadRule(fname[0]) self.parent.ruleNameL.setText("Rule: " + fname[0][fname[0].rfind('/') + 1:len(fname[0])]) self.parent.statusBar().showMessage("Rule: " + fname[0][fname[0].rfind('/') + 1:len(fname[0])]) self.close()
def exportMap(self): self._pause() if self.time is not None: fname = QFileDialog.getSaveFileName(self, 'Export Map', fileSystem.getProjectRoot(), "Map files (*.map)") if fname[0]: self.time.maps[self.time.turnN].saveMap(fname[0])
def importMap(self): fname = QFileDialog.getOpenFileName(self, 'Import Map', fileSystem.getProjectRoot(), "Map files (*.map)") if fname[0]: self.map = fileSystem.loadMap(fname[0]) self.mapNameL.setText("Map: " + fname[0][fname[0].rfind('/') + 1:len(fname[0])]) self.statusBar().showMessage("Map: " + fname[0][fname[0].rfind('/') + 1:len(fname[0])] + " has been imported")
def importRule(self): fname = QFileDialog.getOpenFileName(self, 'Import Rule', fileSystem.getProjectRoot(), "Rule files (*.rule)") if fname[0]: self.rule = fileSystem.loadRule(fname[0]) self.ruleNameL.setText("Rule: " + fname[0][fname[0].rfind('/') + 1:len(fname[0])]) self.statusBar().showMessage("Rule: " + fname[0][fname[0].rfind('/') + 1:len(fname[0])] + " has been imported")
def importRule(self): fname = QFileDialog.getOpenFileName(self, 'Import Rule', fileSystem.getProjectRoot(), "Rule files (*.rule)") if fname[0]: f = open(fname[0], "r") moore = f.readline().split(':')[1] moore = moore.replace('\n', '') if moore.lower() == 'true': moore = True else: moore = False if moore: self.moorelian = True self.mooreBoxVal.setText("Mode: Moorelian") else: self.moorelian = False self.mooreBoxVal.setText("Mode: Von Neumann") nhsize = int(f.readline().split(':')[1]) self.neighbourhoodSize = nhsize self.NHSizeVal.setText("Size: " + str(nhsize)) nstates = int(f.readline().split(':')[1]) self.nStates = nstates self.nStateVal.setText("States: " + str(nstates)) centre = f.readline().split(':')[1].split(', ') for i in range(len(centre)): centre[i] = int(centre[i]) self.centre = centre self.centreVal.setText("Centre: " + str(centre)) self.dimensions = len(centre) self.dimensionsVal.setText("Dimensions: " + str(len(centre))) countcentre = f.readline().split(':')[1] countcentre = countcentre.replace('\n', '') if countcentre.lower() == 'true': countcentre = True else: countcentre = False if not countcentre: self.countCentre = False self.countCentreVal.setText("Count Centre: False") else: self.countCentre = True self.countCentreVal.setText("Count Centre: True") f.readline() f.readline() #use position tree baserule = int(f.readline().split(':')[1]) self.baseRule = baserule self.baseRuleVal.setText("Base Condition: " + str(baserule)) while True: line = f.readline() if line == '\n': break condition = line.strip('\n') self.conditionSet.append(condition) self.conditionSetVal.setText("Number of conditions: " + str(len(self.conditionSet))) label = QLabel(condition) self.ruleLayout.addWidget(label) self.conditionLabels.append(label)