Example #1
0
 def test_CurrentScopeChange(self):
     Scope("MAIN", self.MM)
     Scope("A", self.MM)
     prevScope = self.MM.getCurrentScope()
     self.MM.changeCurrentScope("MAIN")
     self.assertNotEqual(prevScope, self.MM.getCurrentScope())
     self.assertEqual(self.MM.getCurrentScope().moduleName, "MAIN")
Example #2
0
    def test_ImportAListOfConstructsIsPossible(self):
        MM = ModulesManager()
        scopeM = Scope("MAIN",
                       MM,
                       exports=[
                           ScopeExport(Scope.PROMISE_TYPE_GLOBAL,
                                       ["?*a*", "?*b*", "?*c*"])
                       ])

        scopeM.globalsvars.addDefinition(
            GlobalVarDefinition(scopeM.moduleName, "?*a*", object()))
        scopeM.globalsvars.addDefinition(
            GlobalVarDefinition(scopeM.moduleName, "?*b*", object()))
        scopeM.globalsvars.addDefinition(
            GlobalVarDefinition(scopeM.moduleName, "?*c*", object()))

        scope1 = Scope("FIRST",
                       MM,
                       imports=[
                           ScopeImport("MAIN", Scope.PROMISE_TYPE_GLOBAL,
                                       ["?*a*", "?*b*"])
                       ])

        self.assertTrue(scope1.globalsvars.has("?*a*"))
        self.assertTrue(scope1.globalsvars.has("?*b*"))
        self.assertFalse(scope1.globalsvars.has("?*c*"))
Example #3
0
 def test_ExportEverythingIsImportableAlways(self):
     scope = Scope("MAIN", ModulesManager(), exports=[
                         ScopeExport(Scope.PROMISE_NAME_ALL, Scope.PROMISE_NAME_ALL),
                     ])
     
     self.assertTrue(scope.isImportable(Scope.PROMISE_TYPE_TEMPLATE, "template"))
     self.assertTrue(scope.isImportable(Scope.PROMISE_TYPE_FUNCTION, "function"))
     self.assertTrue(scope.isImportable(Scope.PROMISE_TYPE_GLOBAL, "*global*"))
Example #4
0
 def test_ExportNoneForATypeRemoveEverythingForTheTypeIfPreviousExportEverythingExists(self):
     scope = Scope("MAIN", ModulesManager(), exports=[
                         ScopeExport(Scope.PROMISE_NAME_ALL, Scope.PROMISE_NAME_ALL),
                         ScopeExport(Scope.PROMISE_TYPE_GLOBAL, Scope.PROMISE_NAME_NONE)
                     ])
     
     self.assertTrue(scope.isImportable(Scope.PROMISE_TYPE_TEMPLATE, "template"))
     self.assertTrue(scope.isImportable(Scope.PROMISE_TYPE_FUNCTION, "function"))
     self.assertFalse(scope.isImportable(Scope.PROMISE_TYPE_GLOBAL, "*global*"))
Example #5
0
 def test_ExportAListOfConstructsIsPossible(self):
     scope = Scope("MAIN", ModulesManager(), exports=[
                         ScopeExport(Scope.PROMISE_TYPE_GLOBAL, ["?*a*","?*b*","?*c*" ])
                     ])
     
     self.assertTrue(scope.isImportable(Scope.PROMISE_TYPE_GLOBAL, "?*a*"))
     self.assertTrue(scope.isImportable(Scope.PROMISE_TYPE_GLOBAL, "?*b*"))
     self.assertTrue(scope.isImportable(Scope.PROMISE_TYPE_GLOBAL, "?*c*"))
     self.assertFalse(scope.isImportable(Scope.PROMISE_TYPE_GLOBAL, "?*unknown*"))
Example #6
0
 def test_NotExportedDefinitionIsNotImportable(self):
     scope = Scope("MAIN", ModulesManager(), exports=[
                         ScopeExport(Scope.PROMISE_TYPE_TEMPLATE, 'template-other'),
                         ScopeExport(Scope.PROMISE_TYPE_FUNCTION, 'function-other'),
                         ScopeExport(Scope.PROMISE_TYPE_GLOBAL, '*global-other*')
                     ])
     
     self.assertFalse(scope.isImportable(Scope.PROMISE_TYPE_TEMPLATE, "template"))
     self.assertFalse(scope.isImportable(Scope.PROMISE_TYPE_FUNCTION, "function"))
     self.assertFalse(scope.isImportable(Scope.PROMISE_TYPE_GLOBAL, "*global*"))
Example #7
0
 def test_ExportNoneRemoveEverything(self):
     scope = Scope("MAIN", ModulesManager(), exports=[
                         ScopeExport(Scope.PROMISE_TYPE_TEMPLATE, 'template'),
                         ScopeExport(Scope.PROMISE_TYPE_FUNCTION, 'function'),
                         ScopeExport(Scope.PROMISE_TYPE_GLOBAL, '*global*'),
                         ScopeExport(Scope.PROMISE_NAME_NONE, Scope.PROMISE_NAME_NONE)
                     ])
     
     self.assertFalse(scope.isImportable(Scope.PROMISE_TYPE_TEMPLATE, "template"))
     self.assertFalse(scope.isImportable(Scope.PROMISE_TYPE_FUNCTION, "function"))
     self.assertFalse(scope.isImportable(Scope.PROMISE_TYPE_GLOBAL, "*global*"))
Example #8
0
 def test_PushMore(self):
     
     Scope("A", self.theEnv.modulesManager)
     Scope("B", self.theEnv.modulesManager)
     Scope("C", self.theEnv.modulesManager)
     self.theEnv.modulesManager.changeCurrentScope("MAIN")
     
     self.assertTrue(self.forInput(types.Symbol("A"), types.Symbol("B"), types.Symbol("C"))
                     .expect(types.Symbol("TRUE")))
     
     self.assertEqual(self.theEnv.network.agenda.focusStack, ["MAIN", "C", "B", "A"])
Example #9
0
    def test_GuardOnRecursiveInclusion(self):

        MM = ModulesManager()

        scopeM = Scope("MAIN",
                       MM,
                       exports=[
                           ScopeExport(Scope.PROMISE_NAME_ALL,
                                       Scope.PROMISE_NAME_ALL)
                       ])

        scope1 = Scope("FIRST",
                       MM,
                       exports=[
                           ScopeExport(Scope.PROMISE_NAME_ALL,
                                       Scope.PROMISE_NAME_ALL)
                       ],
                       imports=[
                           ScopeImport("MAIN", Scope.PROMISE_NAME_ALL,
                                       Scope.PROMISE_NAME_ALL),
                       ])

        scope2 = Scope("SECOND",
                       MM,
                       exports=[
                           ScopeExport(Scope.PROMISE_NAME_ALL,
                                       Scope.PROMISE_NAME_ALL)
                       ],
                       imports=[
                           ScopeImport("MAIN", Scope.PROMISE_NAME_ALL,
                                       Scope.PROMISE_NAME_ALL),
                           ScopeImport("FIRST", Scope.PROMISE_NAME_ALL,
                                       Scope.PROMISE_NAME_ALL)
                       ])

        scopeM.globalsvars.addDefinition(
            GlobalVarDefinition(scopeM.moduleName, "?*A*", object()))

        self.assertTrue(scopeM.globalsvars.has("?*A*"))
        self.assertTrue(scope1.globalsvars.has("?*A*"))
        self.assertTrue(scope2.globalsvars.has("?*A*"))
        self.assertEqual(
            scopeM.globalsvars.getDefinition("?*A*").moduleName, "MAIN")
        self.assertEqual(
            scope1.globalsvars.getDefinition("?*A*").moduleName, "MAIN")
        self.assertEqual(
            scope2.globalsvars.getDefinition("?*A*").moduleName, "MAIN")
        self.assertEqual(scopeM.globalsvars.getDefinition("?*A*"),
                         scope1.globalsvars.getDefinition("?*A*"))
        self.assertEqual(scopeM.globalsvars.getDefinition("?*A*"),
                         scope2.globalsvars.getDefinition("?*A*"))
        self.assertEqual(scope1.globalsvars.getDefinition("?*A*"),
                         scope2.globalsvars.getDefinition("?*A*"))
Example #10
0
    def test_ExportAListOfConstructsIsPossible(self):
        scope = Scope("MAIN",
                      ModulesManager(),
                      exports=[
                          ScopeExport(Scope.PROMISE_TYPE_GLOBAL,
                                      ["?*a*", "?*b*", "?*c*"])
                      ])

        self.assertTrue(scope.isImportable(Scope.PROMISE_TYPE_GLOBAL, "?*a*"))
        self.assertTrue(scope.isImportable(Scope.PROMISE_TYPE_GLOBAL, "?*b*"))
        self.assertTrue(scope.isImportable(Scope.PROMISE_TYPE_GLOBAL, "?*c*"))
        self.assertFalse(
            scope.isImportable(Scope.PROMISE_TYPE_GLOBAL, "?*unknown*"))
Example #11
0
    def test_ExportEverythingIsImportableAlways(self):
        scope = Scope("MAIN",
                      ModulesManager(),
                      exports=[
                          ScopeExport(Scope.PROMISE_NAME_ALL,
                                      Scope.PROMISE_NAME_ALL),
                      ])

        self.assertTrue(
            scope.isImportable(Scope.PROMISE_TYPE_TEMPLATE, "template"))
        self.assertTrue(
            scope.isImportable(Scope.PROMISE_TYPE_FUNCTION, "function"))
        self.assertTrue(
            scope.isImportable(Scope.PROMISE_TYPE_GLOBAL, "*global*"))
Example #12
0
    def test_ExportedDefinitionIsImportable(self):
        scope = Scope("MAIN",
                      ModulesManager(),
                      exports=[
                          ScopeExport(Scope.PROMISE_TYPE_TEMPLATE, 'template'),
                          ScopeExport(Scope.PROMISE_TYPE_FUNCTION, 'function'),
                          ScopeExport(Scope.PROMISE_TYPE_GLOBAL, '*global*')
                      ])

        self.assertTrue(
            scope.isImportable(Scope.PROMISE_TYPE_TEMPLATE, "template"))
        self.assertTrue(
            scope.isImportable(Scope.PROMISE_TYPE_FUNCTION, "function"))
        self.assertTrue(
            scope.isImportable(Scope.PROMISE_TYPE_GLOBAL, "*global*"))
Example #13
0
    def test_RaiseErrorOnMultipleDefinitionOfTheSameModule(self):
        Scope("MAIN", self.MM)

        self.assertRaisesRegexp(
            ModulesManagerRedefinitionError,
            "Cannot redefine defmodule MAIN while it is in use", Scope, "MAIN",
            self.MM)
Example #14
0
    def test_ListenerCleanupOnScopeCreationFail(self):

        MM = ModulesManager()

        scopeM = Scope("MAIN",
                       MM,
                       exports=[
                           ScopeExport(Scope.PROMISE_NAME_ALL,
                                       Scope.PROMISE_NAME_ALL)
                       ])

        scopeM.globalsvars.addDefinition(
            GlobalVarDefinition(scopeM.moduleName, "?*A*", object()))

        scope1 = Scope("FIRST",
                       MM,
                       exports=[
                           ScopeExport(Scope.PROMISE_NAME_ALL,
                                       Scope.PROMISE_NAME_ALL)
                       ])

        scope1.globalsvars.addDefinition(
            GlobalVarDefinition(scope1.moduleName, "?*A*", object()))

        self.assertRaises(ScopeDefinitionConflict,
                          Scope,
                          "SECOND",
                          MM,
                          exports=[
                              ScopeExport(Scope.PROMISE_NAME_ALL,
                                          Scope.PROMISE_NAME_ALL)
                          ],
                          imports=[
                              ScopeImport("MAIN", Scope.PROMISE_NAME_ALL,
                                          Scope.PROMISE_NAME_ALL),
                              ScopeImport("FIRST", Scope.PROMISE_NAME_ALL,
                                          Scope.PROMISE_NAME_ALL)
                          ])

        self.assertEqual(
            len(
                scope1.globalsvars.getObservers(
                    scope1.globalsvars.EVENT_NEW_DEFINITION)), 0)
        self.assertEqual(
            len(
                scopeM.globalsvars.getObservers(
                    scopeM.globalsvars.EVENT_NEW_DEFINITION)), 0)
Example #15
0
    def test_ExportNoneForATypeRemoveEverythingForTheTypeIfPreviousExportEverythingExists(
            self):
        scope = Scope("MAIN",
                      ModulesManager(),
                      exports=[
                          ScopeExport(Scope.PROMISE_NAME_ALL,
                                      Scope.PROMISE_NAME_ALL),
                          ScopeExport(Scope.PROMISE_TYPE_GLOBAL,
                                      Scope.PROMISE_NAME_NONE)
                      ])

        self.assertTrue(
            scope.isImportable(Scope.PROMISE_TYPE_TEMPLATE, "template"))
        self.assertTrue(
            scope.isImportable(Scope.PROMISE_TYPE_FUNCTION, "function"))
        self.assertFalse(
            scope.isImportable(Scope.PROMISE_TYPE_GLOBAL, "*global*"))
Example #16
0
    def test_ExportNoneRemoveEverything(self):
        scope = Scope("MAIN",
                      ModulesManager(),
                      exports=[
                          ScopeExport(Scope.PROMISE_TYPE_TEMPLATE, 'template'),
                          ScopeExport(Scope.PROMISE_TYPE_FUNCTION, 'function'),
                          ScopeExport(Scope.PROMISE_TYPE_GLOBAL, '*global*'),
                          ScopeExport(Scope.PROMISE_NAME_NONE,
                                      Scope.PROMISE_NAME_NONE)
                      ])

        self.assertFalse(
            scope.isImportable(Scope.PROMISE_TYPE_TEMPLATE, "template"))
        self.assertFalse(
            scope.isImportable(Scope.PROMISE_TYPE_FUNCTION, "function"))
        self.assertFalse(
            scope.isImportable(Scope.PROMISE_TYPE_GLOBAL, "*global*"))
 def addMainScope(self):
     '''
     Register a MAIN scope, exporting everything
     Same effect of
         (defmodule MAIN (export ?ALL))
     '''
     from myclips.Scope import Scope, ScopeExport
     Scope("MAIN", self, exports=[
             ScopeExport(Scope.PROMISE_NAME_ALL, Scope.PROMISE_NAME_ALL)
         ]) 
     # add the (initial-fact) template to the MAIN scope
     from myclips.TemplatesManager import TemplateDefinition
     self.currentScope.templates.addDefinition(TemplateDefinition("MAIN", "initial-fact", None))
Example #18
0
    def test_ImportDefinitionFromAnotherScopeWithObserver(self):
        MM = ModulesManager()

        scope1 = Scope("MAIN",
                       MM,
                       exports=[
                           ScopeExport(Scope.PROMISE_NAME_ALL,
                                       Scope.PROMISE_NAME_ALL)
                       ])

        scope2 = Scope("SECOND",
                       MM,
                       imports=[
                           ScopeImport("MAIN", Scope.PROMISE_NAME_ALL,
                                       Scope.PROMISE_NAME_ALL)
                       ])

        scope1.globalsvars.addDefinition(
            GlobalVarDefinition(scope1.moduleName, "?*A*", object()))

        self.assertTrue(scope2.globalsvars.has("?*A*"))
        self.assertEqual(
            scope2.globalsvars.getDefinition("?*A*").moduleName, "MAIN")
Example #19
0
    def __init__(self,
                 moduleName,
                 modulesManager,
                 specifications=None,
                 comment=None):
        moduleName = moduleName.evaluate() if isinstance(
            moduleName, BaseParsedType) else moduleName
        comment = comment.evaluate().strip('"') if isinstance(
            comment, BaseParsedType) else comment
        specifications = specifications if isinstance(specifications,
                                                      list) else []

        ParsedType.__init__(self, moduleName)
        self.moduleName = moduleName
        self.comment = comment
        self.specifications = specifications

        # time to create che new Scope

        try:
            Scope(moduleName,
                  modulesManager,
                  imports=[
                      ScopeImport(imp.moduleName, imp.item.portType,
                                  imp.item.portNames)
                      for imp in self.specifications
                      if isinstance(imp, ImportSpecification)
                  ],
                  exports=[
                      ScopeExport(exp.item.portType, exp.item.portNames)
                      for exp in self.specifications
                      if isinstance(exp, ExportSpecification)
                  ])
        except Exception, e:
            # causes of failure:
            #    moduleName already exists
            #    import from unknown module
            #    import of an unknown construct
            #    name conflicts
            # i can use the original name
            raise TypeInstanceCreationError(e.args[0])
Example #20
0
    def test_DefGlobalConstructParser(self):
        Scope("MODULE", self.parser.getModulesManager())

        res = self._testImpl(
            'ConstructParser', r"""
        (defglobal MODULE 
            ?*A* = B
        )
        """).asList()

        self.assertIsInstance(res[0], types.DefGlobalConstruct)
        self.assertEqual(res[0].scope.moduleName, "MODULE")
        self.assertEqual(len(res[0].assignments), 1)
        self.assertEqual(
            len([
                True for x in res[0].assignments
                if not isinstance(x, types.GlobalAssignment)
            ]), 0)
        allGlobals = self.parser.getModulesManager(
        ).currentScope.globalsvars.definitions
        self.assertEqual(len(allGlobals), 1)
        self.assertEqual(allGlobals[0], "?*A*")
    def test_CanRedefineFunctionUntilDefinitionIsImported(self):
        self.scope.functions.addDefinition(
                FunctionDefinition(self.scope.moduleName, "NuovaFunzione", object(), Symbol)
            )
        
        self.assertIsInstance(self.scope.functions.getDefinition("NuovaFunzione"),
                                FunctionDefinition)
        self.assertEqual(self.scope.functions.getDefinition("NuovaFunzione").returnTypes,
                            tuple([Symbol]))
        
        self.scope.functions.addDefinition(
                FunctionDefinition(self.scope.moduleName, "NuovaFunzione", object(), Integer)
            )
        self.assertEqual(self.scope.functions.getDefinition("NuovaFunzione").returnTypes,
                            tuple([Integer]))

        Scope("OTHER", self.MM, imports=[
                ScopeImport("MAIN", Scope.PROMISE_TYPE_FUNCTION, "NuovaFunzione")
            ])
        
        self.assertRaisesRegexp(MultipleDefinitionError, "Cannot redefine deffunction \w+::\w+ while it is in use", self.scope.functions.addDefinition,
                FunctionDefinition(self.scope.moduleName, "NuovaFunzione", object(), Integer)
            )
Example #22
0
    def test_AddNewScope(self):
        Scope("MAIN", self.MM)

        self.assertTrue(self.MM.isDefined("MAIN"))
Example #23
0
    def test_AutoChangeScopeOnAddScope(self):
        prevScope = self.MM.getCurrentScope()

        Scope("MAIN", self.MM)

        self.assertNotEqual(prevScope, self.MM.getCurrentScope())
Example #24
0
    def test_SimpleScopeGeneration(self):
        scope = Scope("MAIN", ModulesManager())

        self.assertIsInstance(scope, Scope)
        self.assertEqual(scope.moduleName, "MAIN")