def import_classes_dir(classes_dir): for file in os.listdir(classes_dir): if file.endswith('.py'): for cls in inspect.getmembers(importlib.import_module(file[:-3]), inspect.isclass): if cls[1] is not Predicate: ASPMapper.get_instance().register_class(cls[1])
def test_shortest_path(self): try: handler = DesktopHandler(DLV2DesktopService(self.getPath())) ASPMapper.get_instance().register_class(Edge) ASPMapper.get_instance().register_class(Path) inputProgram = ASPInputProgram() self.source = 0 # source node self.destination = 7 # destination node rules = "source(" + str(self.source) + "). destination(" + str( self.destination) + ")." rules += "path(X,Y,W) | notPath(X,Y,W) :- source(X), edge(X,Y,W)." rules += "path(X,Y,W) | notPath(X,Y,W) :- path(_,X,_), edge(X,Y,W), not to(X)." rules += "visited(X) :- path(_,X,_)." rules += ":- destination(X), not visited(X)." rules += ":~ path(X,Y,W). [W@1 ,X,Y]" inputProgram.add_program(rules) inputProgram.add_objects_input(self.getEdges()) handler.add_program(inputProgram) answerSets = handler.start_sync() self.assertIsNotNone(answerSets) self.assertTrue(isinstance(answerSets, Output), "Error, result object is not Output") self.assertTrue(answerSets.get_errors() == "", "Found error:\n" + str(answerSets.get_errors())) self.assertTrue(len(answerSets.get_optimal_answer_sets()) != 0) answerSet = answerSets.get_optimal_answer_sets()[0] path = [] # edges in the shortest path (unsorted) sum_ = 0 # total weight of the path for obj in answerSet.get_atoms(): if isinstance(obj, Path): path.append(obj) sum_ += int(obj.get_weight()) sortedPath = [] # edges in the shortest path (sorted) sortedPath.append(self.source) self.join(self.source, path, sortedPath) # sorts the edges self.show(sortedPath, sum_) # shows the path except Exception as e: print(str(e))
def runTest(self): instance = ASPMapper.get_instance() try: instance.register_class(Cell) obj = instance.get_object("cell(1,2,5)") self.assertTrue(isinstance(obj, Cell)) self.assertEqual(1, obj.get_row()) self.assertEqual(2, obj.get_column()) self.assertEqual('5', obj.get_value().value) print(instance.get_string(obj)) self.assertEqual("cell(1,2,5)", instance.get_string(obj)) instance.unregister_class(Cell) noneObject = instance.get_object("cell(1,2,5)") self.assertIsNone(noneObject) except Exception as e: self.fail(str(e))
def add_object_input(self, input_obj): """Transforms a given Object class into an InputProgram and adds it to the current _programs. The parameter input_obj is an object to be transformed. """ self.add_program(ASPMapper.get_instance().get_string(input_obj) + ".")
def get_atoms(self): """Return atoms stored in __atoms The method return a set of Object filled with atoms data """ if not self.__atoms: mapper = ASPMapper.get_instance() for atom in self.__value: obj = mapper.get_object(atom) if obj is not None: self.__atoms.add(obj) return self.__atoms
def __init__(self) -> None: # Instantiate the Handler. self.handler = DesktopHandler(DLV2DesktopService(os.path.join(dirname, "dlv2.exe"))) # Register input facts to provide to DLV2. ASPMapper.get_instance().register_class(MatrixCellPredicate) ASPMapper.get_instance().register_class(ShapePredicate) ASPMapper.get_instance().register_class(InCellPredicate)
self.lo.count_down() def get_output(self): return self.ans mc = MyCalback() handler.start_async(mc) print("asincrono") lock.await() out = mc.get_output() mapp = ASPMapper.get_instance() if not isinstance(out, Output): raise "error" if (len(out.get_answer_sets()) != 0): ans = out.get_answer_sets()[0] Matrix = [[0 for x in range(w)] for y in range(h)] for obj in ans.get_atoms(): Matrix[obj.get_row()][obj.get_column()] = obj.get_value() tmp="" for i in range(9):
def run(self) -> None: stop = False while not stop: gameInstance.lock.acquireReadLock() isGrass = gameInstance.getElement(self.__bomb.get_i(), self.__bomb.get_j()) == GRASS if isGrass: self.__bombs.remove(self.__bomb) stop = True gameInstance.lock.releaseReadLock() # === FUNCTIONS === (lower_case names) ASPMapper.get_instance().register_class(InputPointType) ASPMapper.get_instance().register_class(Path) ASPMapper.get_instance().register_class(Distance) ASPMapper.get_instance().register_class(InputBomb) ASPMapper.get_instance().register_class(EnemyBomb) ASPMapper.get_instance().register_class(BreakBomb) ASPMapper.get_instance().register_class(AdjacentPlayerAndEnemy) def addBombEnemy(bombs: ListBomb, bomb: InputBomb) -> None: if bomb not in bombs: bombs.append(bomb) CheckBomb(bombs, bomb).start() def moveEnemyFromPath(path: Path, lastPositionsEnemy: dict) -> None:
def load_classes(classes): for cls in classes: ASPMapper.get_instance().register_class(cls)