def onFileExport(self): # Check for uncompiled changes retval = None if self.editSpace.up_to_date is False: if DEBUG: print("Code is not compiled. Compile before export.") retval = handleCompileMsg() if retval == QMessageBox.Cancel: return if retval == QMessageBox.Yes: if DEBUG: print('Compiling code before exporting') try: self.onCompile() except Exception as ex: print('Couldn\'t compile before exporting') return try: fname, filter = QFileDialog.getSaveFileName(self, 'Export to file') if fname == "": if DEBUG: print("Cancel clicked") return Storage.exportAIML(fname, self.editSpace.aiml) # save as an aiml file # Display Dialog exportSuccessful() except Exception as ex: print("Exception caught trying to export") print(ex) handleError(ex)
def test_topic_import(self): imported = Storage.importAIML('./test_aimls/mexican_food') Storage.exportAIML('./test_aimls/mexican_food_exp', imported) exported = Storage.importAIML('./test_aimls/mexican_food_exp') # print(f'TEST:\n{imported}') # print(f'EXPECTED:\n{exported}') self.assertEqual(str(imported), str(exported))
def test_parsing_commented_tree(self): parser = ET.XMLParser(target=CommentedTreeBuilder()) tree = ET.parse('test_aimls/utils.aiml', parser) tree.write('test_aimls/out.aiml', xml_declaration=True, encoding='UTF-8') util = Storage.importAIML('./test_aimls/utils') out = Storage.importAIML('./test_aimls/out') self.assertEqual(str(util), str(out))
def test_export(self): #NOTE: Works with make_aiml2 but NOT make_aiml() might have # something to do with the topic tag ac = AimlCreator() export = ac.make_aiml2() Storage.exportAIML('./test_aimls/exporting', export) imported = Storage.importAIML('./test_aimls/exporting') self.assertEqual(str(export), str(imported))
def test_comments_util_import(self): imported = Storage.importAIML('./test_aimls/utils') Storage.exportAIML('./test_aimls/utils_exp', imported) exported = Storage.importAIML('./test_aimls/utils_exp') print(f'TEST:\n{imported}') print(f'EXPECTED:\n{exported}') self.maxDiff = None self.assertEqual(str(imported),str(exported))
def test_import(self): ac = AimlCreator() expected = ac.make_aiml2() #NOTE: Make sure to not have the '.aiml' after file name. # Causes an aborted core dump. Why? imported = Storage.importAIML('./test_aimls/atomic') self.assertEqual(str(expected), str(imported))
def test_make_cat_start_compile(self): ac = AimlCreator() expected_aiml = ac.make_cat_star() # print('EXPECTED:\n{}'.format(expected_aiml)) test_aiml = Storage.compileToAIML(str(expected_aiml)) # print('TEST:\n{}'.format(test_aiml)) self.assertEqual(str(test_aiml), str(expected_aiml))
def onFileImport(self): try: fname, filter = QFileDialog.getOpenFileName(self, "Import File") if fname == "": if DEBUG: print("Cancel was clicked") return yoffset = -4000 if DEBUG: print("fname: " + fname) self.filename = os.path.splitext(fname)[ 0] # removing extension from path name aiml = Storage.importAIML(self.filename) # import the aiml file numCats = 0 if DEBUG: print(f"aiml tags:\n{aiml.tags}") topics = [] for cat in aiml.tags: if cat.type == "topic": topics.append(cat) continue self.catCreated.emit(cat) numCats = numCats + 1 for cat in topics: self.catCreated.emit(cat) numCats = numCats + 1 if DEBUG: print("Finished creating " + str(numCats) + " categories") if DEBUG: print("file import successful") self.onCompile() if DEBUG: print("Compile after import sucessful!") except Exception as ex: handleError(ex) print(ex)
def test_import_jupiter(self): app = QApplication(sys.argv) wnd = EditorWindow() wnd.show() wnd.onFileImport() aiml = wnd.editSpace.aiml imported = Storage.importAIML('./test_aimls/jupiter') self.assertEqual(str(aiml), str(imported))
def onCompile(self): if DEBUG: print("Compile Pressed!!!") str_to_parse = self.editSpace.editSpace.toPlainText( ) # Grabs text from the text display if DEBUG: print("text to compile:\n{}".format(str_to_parse)) if str_to_parse.isspace(): handleError( "There is nothing to compile. You must at least have one tag pair to successfully compile your work." ) return try: aiml = Storage.compileToAIML(str_to_parse) if aiml == -1: if DEBUG: print("did not compile properly") return if DEBUG: print("compiling complete") # clearing before updating graph view. if DEBUG: print("Clearing scene") self.editSpace.graphview.scene.clear() self.editSpace.graphview.scene.grScene.clear() # Updating code editor self.editSpace.aiml = aiml if DEBUG: print(f"new model for the aiml:\n{self.editSpace.aiml}") for cat in self.editSpace.aiml.tags: self.editSpace.create_category_graph_view( cat) # Sending categories to be drawn on graph view if DEBUG: print("finished redrawing nodes") self.editSpace.up_to_date = True if DEBUG: print("set up_to_date to True") self.editSpace.tabs.setStyleSheet('') if DEBUG: print("set style sheet for tabs.") # Display dialog compileSuccessful() except Exception as ex: print("Exception caught trying to compile project") print(ex) handleError(ex)
def onFileImport(self): try: fname, filter = QFileDialog.getOpenFileName(self, "Import File") yoffset = -4000 print("fname: " + fname) self.filename = os.path.splitext(fname)[ 0] # removing extension from path name aiml = Storage.importAIML(self.filename) # import the aiml file numCats = 0 print("aiml tags: " + str(aiml.tags)) for cat in aiml.tags: if cat.type == "topic": print("found topic!") for tag in cat.tags: if tag.type == "category": print("tag is a category") self.catCreated.emit( tag) # emitting signal to EditorWidget numCats = numCats + 1 self.editSpace.aiml.append(cat) if cat.type == "category": print("tag is a category") self.catCreated.emit( cat) # emitting signal to EditorWidget numCats = numCats + 1 print("Finished creating " + str(numCats) + " categories") # for node in self.editSpace.scene.nodes: # x = node.grNode.x() # node.setPos(x, yoffset) # yoffset = yoffset + 500 print("file import successful") except Exception as ex: handleError(ex) print(ex)
from Model.Data import * import Utils.Storage as Storage comment_handler = CommentedTreeBuilder() parser = ET.XMLParser(target=comment_handler) with open('test_aimls/utils.aiml', 'r') as f: tree = ET.parse(f, parser) root = tree.getroot() print(root.tag) for child in root: print("child.tag: {}".format(child.tag)) print("child.text: {}".format(child.text)) # print("child.tags: {}".format(child.tags)) tag_obj = Storage.decode_tag(child.tag.lower()) print(tag_obj) raise exception # ET.dump(tree)
def test_save_restore_aiml(self): ac = AimlCreator() aiml = ac.make_aiml1() Storage.save('test_pickle/test1', aiml) aiml2 = Storage.restore('test_pickle/test1') self.assertEqual(str(aiml), str(aiml2))
# create AIML structure aiml = AIML().append(Category().append( Pattern().append("START SESSION 1 *")).append(Template().append( Think().append(Set("username").append("star")).append( Set("topic").append("Session 1"))).append( "Ok. Let's begin our session. How are you doing today <star/>?" ).append(Oob().append(Robot())))).append( Topic("session").append(Category().append( Pattern().append("*")).append(Template().append( Think().append(Set("data").append("<star/>"))).append( Condition("getsetimnet").append( ConditionItem("verypositive").append( "I am happy").append(Oob().append( Robot().append(Options().append( Option("Yes")).append( Option("No")))))). append( ConditionItem("positive").append( "I am not as happy")))))) # print it as a reference print(aiml) Storage.save('test2', aiml) # save as a pickle file aiml2 = Storage.restore('test2') # restore the pickle print("####################restored pickle file#######################") print(aiml2) # print for validation Storage.exportAIML('test2', aiml2) # save as an aiml file aiml4 = Storage.importAIML('test2') # import the aiml file print(aiml4) # print for validation
import os, sys sys.path.append(os.path.abspath('..')) import Utils.Storage as Storage from Model.Data import * aiml = AIML() category = Category() pattern = Pattern() template = Template() random = Random() comment = Comment() li1 = ConditionItem() li2 = ConditionItem() li3 = ConditionItem() li4 = ConditionItem() oob = Oob() robot = Robot() aiml.append( category.append(pattern).append( template.append( random.append(li1).append( comment.append(li2.append('hi')).append( li3.append('hi')).append(li4.append('hi')))).append( oob.append(robot)))) Storage.exportAIML('test_multiline_comments', aiml) print(aiml)
def onFileExport(self): fname, filter = QFileDialog.getSaveFileName(self, 'Export to file') Storage.exportAIML(fname, self.editSpace.aiml) # save as an aiml file
def test_import_jupiter(self): imported = Storage.importAIML('./test_aimls/jupiter') Storage.exportAIML('./test_aimls/jupiter_exp', imported) exported = Storage.importAIML('./test_aimls/jupiter_exp') self.assertEqual(str(imported),str(exported))