def initialize(setupObjectToUse = None, useLocalEvaluator = True): global _builtin if _builtin is not None: return Runtime.initialize(setupObjectToUse) ModuleImporter.initialize(setupObjectToUse) Evaluator.initialize(setupObjectToUse, useLocalEvaluator) _builtin = ForaValue.FORAValue(ModuleImporter.builtinModuleImplVal())
def initialize(setupObjectToUse=None, useLocalEvaluator=True): global _builtin if _builtin is not None: return Runtime.initialize(setupObjectToUse) ModuleImporter.initialize(setupObjectToUse) Evaluator.initialize(setupObjectToUse, useLocalEvaluator) _builtin = ForaValue.FORAValue(ModuleImporter.builtinModuleImplVal())
def main(parsedArguments): isLocalEvaluator = True with createViewFactory(): FORA.initialize(useLocalEvaluator=isLocalEvaluator) locals = {} try: for code in parsedArguments.expressionsToEvaluate: eval_expression(code, locals, parsedArguments.shouldPrintEvaluationTime) for a in parsedArguments.files: with open(a) as f: code = f.read() eval_expression(code, locals, parsedArguments.shouldPrintEvaluationTime) if parsedArguments.repeaters: while True: for r in parsedArguments.repeaters: eval_expression(r, locals, parsedArguments.shouldPrintEvaluationTime) Evaluator.evaluator().flush() if parsedArguments.alwaysRunInterpreted or ( not parsedArguments.expressionsToEvaluate and not parsedArguments.files and not parsedArguments.repeaters): for code, index in interactive_expressions(): try: eval_expression(code, locals, parsedArguments.shouldPrintEvaluationTime, index) except KeyboardInterrupt: print "\n\n\t[Interrupted]\n" except KeyboardInterrupt: print "\n\n\t[Interrupted]\n" except Exceptions.FatalException as ex: print "\nERROR: " + ex.message print "Exiting FORA." import os os._exit(os.EX_UNAVAILABLE) time.sleep(.5)
def __call__(self, *args): return processComputationResult( Evaluator.evaluator().evaluate( self.implVal_, FORAValue.symbol_Call.implVal_, *[FORAValue(x).implVal_ for x in args] ) )
def __getitem__(self, x): return processComputationResult( Evaluator.evaluator().evaluate( self.implVal_, FORAValue.symbol_GetItem.implVal_, FORAValue(x).implVal_ ) )
def __len__(self): return processComputationResult( Evaluator.evaluator().evaluate( FORAValue.symbol_Size.implVal_, FORAValue.symbol_Call.implVal_, self.implVal_ ) )
def __getattr__(self, attr): if attr.startswith('__') and attr.endswith('__'): raise AttributeError( ) # we don't want to intercept special python methods return processComputationResult(Evaluator.evaluator().evaluate( self.implVal_, FORAValue.symbol_Member.implVal_, ForaNative.makeSymbol(attr)))
def main(parsedArguments): isLocalEvaluator = True with createViewFactory(): FORA.initialize(useLocalEvaluator=isLocalEvaluator) locals = {} try: for code in parsedArguments.expressionsToEvaluate: eval_expression(code, locals, parsedArguments.shouldPrintEvaluationTime) for a in parsedArguments.files: with open(a) as f: code = f.read() eval_expression(code, locals, parsedArguments.shouldPrintEvaluationTime) if parsedArguments.repeaters: while True: for r in parsedArguments.repeaters: eval_expression(r, locals, parsedArguments.shouldPrintEvaluationTime) Evaluator.evaluator().flush() if parsedArguments.alwaysRunInterpreted or ( not parsedArguments.expressionsToEvaluate and not parsedArguments.files and not parsedArguments.repeaters ): for code, index in interactive_expressions(): try: eval_expression(code, locals, parsedArguments.shouldPrintEvaluationTime, index) except KeyboardInterrupt: print "\n\n\t[Interrupted]\n" except KeyboardInterrupt: print "\n\n\t[Interrupted]\n" except Exceptions.FatalException as ex: print "\nERROR: " + ex.message print "Exiting FORA." import os os._exit(os.EX_UNAVAILABLE) time.sleep(.5)
def __getitem__(self, x): try: value = Evaluator.evaluator().getVDM().extractVectorItem(self.implVal_, x) if value is not None: return FORAValue(value).toPythonObject() else: return FORAValue.__getitem__(self, x) except: raise IndexError()
def evaluate(self, expr, args, assignments, lets, binding, statementTerm): exprAsFunction = expr.toFunction() args = [ exprAsFunction.implVal_, ForaValue.FORAValue.symbol_Call.implVal_ ] + args res = Evaluator.evaluator().evaluate(*args) # res is a ComputationResult instance, defined in ufora/FORA/Core/ComputationResult.hppml #@type ComputationResult = # Exception of ImplValContainer exception # -| Result of ImplValContainer result # -| Failure of ErrorState error resVal = None if res.isResult(): resVal = res.asResult.result elif res.isException(): resVal = res.asException.exception elif res.isFailure(): raise ForaValue.FORAFailure(res.asFailure.error) # At this point, resVal is an ImplValContainer resVal = ForaValue.FORAValue(resVal).implVal_ boundValues = {} if res.isException(): #the exception it a tuple ((exception, (a1, a2, ...)), stacktrace) #we want to propagate (exception, stacktrace) exceptionAndVariables, stacktrace = resVal exception, variableAssignments = exceptionAndVariables #iterate through the binding and update the original for ix, a in enumerate(assignments): boundValues[a] = binding[a] = variableAssignments[ix] boundValues['result'] = ForaValue.FORAException( (exception, ForaValue.FORAValue(stacktrace))) else: #packAssignedVarsIntoTuple puts the result in the first tuple element #and the assigned variables in the second actualResult, boundSymbolValues, assignedOutputs = resVal #iterate through the binding and update the original for ix, a in enumerate(lets): boundValues[a] = binding[a] = boundSymbolValues[ix] for ix, a in enumerate(assignments): boundValues[a] = binding[a] = assignedOutputs[ix] boundValues['result'] = actualResult return boundValues
def binaryOp_(lhs, rhs, op): """given lhs, rhs, and op, which are all FORAValues, evaluate a binary operator call""" return processComputationResult( Evaluator.evaluator().evaluate( lhs.implVal_, FORAValue.symbol_Operator.implVal_, op.implVal_, rhs.implVal_ ) )
def __getattr__(self, attr): if attr.startswith('__') and attr.endswith('__'): raise AttributeError() # we don't want to intercept special python methods return processComputationResult( Evaluator.evaluator().evaluate( self.implVal_, FORAValue.symbol_Member.implVal_, ForaNative.makeSymbol(attr) ) )
def setUp(self): self.callbackScheduler = CallbackScheduler.singletonForTesting() def createStorage(vdm): self.simpleOfflineCache = CumulusNative.SimpleOfflineCache( self.callbackScheduler, 1000000000) return self.simpleOfflineCache self.evaluator = LocalEvaluator.LocalEvaluator( createStorage, 2000000, maxPageSizeInBytes=100000) self.oldEvaluator = Evaluator.swapEvaluator(self.evaluator)
def setUp(self): self.callbackScheduler = CallbackScheduler.singletonForTesting() def createStorage(vdm): self.simpleOfflineCache = CumulusNative.SimpleOfflineCache(self.callbackScheduler, 1000000000) return self.simpleOfflineCache self.evaluator = LocalEvaluator.LocalEvaluator( createStorage, 2000000, maxPageSizeInBytes = 100000 ) self.oldEvaluator = Evaluator.swapEvaluator(self.evaluator)
def setUp(self): self.callbackScheduler = CallbackScheduler.singletonForTesting() self.runtime = Runtime.getMainRuntime() self.axioms = self.runtime.getAxioms() self.native_runtime = self.runtime.getTypedForaCompiler() self.vals_to_test = self.loadValuesFromFile(os.path.join(os.path.split(__file__)[0], "AxiomJOA_test.txt")) self.evaluator = LocalEvaluator.LocalEvaluator( lambda vdm: CumulusNative.SimpleOfflineCache(self.callbackScheduler, 1000000000), 10000000, maxPageSizeInBytes = 100000 ) self.oldEvaluator = Evaluator.swapEvaluator(self.evaluator) self.knownModulesAsConstantJOVs = dict() self.knownModulesAsConstantJOVs["builtin"] = \ FORANative.JudgmentOnValue.Constant(FORA.builtin().implVal_)
def setUp(self): self.callbackScheduler = CallbackScheduler.singletonForTesting() self.runtime = Runtime.getMainRuntime() self.axioms = self.runtime.getAxioms() self.native_runtime = self.runtime.getTypedForaCompiler() self.vals_to_test = self.loadValuesFromFile( os.path.join(os.path.split(__file__)[0], "AxiomJOA_test.txt")) self.evaluator = LocalEvaluator.LocalEvaluator( lambda vdm: CumulusNative.SimpleOfflineCache( self.callbackScheduler, 1000000000), 10000000, maxPageSizeInBytes=100000) self.oldEvaluator = Evaluator.swapEvaluator(self.evaluator) self.knownModulesAsConstantJOVs = dict() self.knownModulesAsConstantJOVs["builtin"] = \ FORANative.JudgmentOnValue.Constant(FORA.builtin().implVal_)
def evaluator(): return Evaluator.evaluator()
def tearDown(self): self.evaluator.teardown() Evaluator.swapEvaluator(self.oldEvaluator)
def evaluate(self, expr, args, assignments, lets, binding, statementTerm): exprAsFunction = expr.toFunction() args = [exprAsFunction.implVal_, ForaValue.FORAValue.symbol_Call.implVal_] + args res = Evaluator.evaluator().evaluate(*args) # res is a ComputationResult instance, defined in ufora/FORA/Core/ComputationResult.hppml #@type ComputationResult = # Exception of ImplValContainer exception, ImplValContainer computationLog # -| Result of ImplValContainer result, ImplValContainer computationLog # -| Failure of ErrorState error resVal = None logs = None if res.isResult(): resVal = res.asResult.result logs = res.asResult.computationLog elif res.isException(): resVal = res.asException.exception logs = res.asException.computationLog elif res.isFailure(): raise ForaValue.FORAFailure(res.asFailure.error) # At this point, resVal and logs are both ImplValContainers. resVal = ForaValue.FORAValue(resVal).implVal_ if logs is not None and logs.isVector() and logs.getVectorSize() > 0: if logs.getVectorSize() > 50: for ix in range(50): print "log> " + ForaValue.FORAValue(logs)[ix] print " and", logs.getVectorSize() - 50, "additional log messages..." else: print "log> " + ForaValue.FORAValue("\nlog> ").join(ForaValue.FORAValue(logs)) boundValues = {} if res.isException(): #the exception it a tuple ((exception, (a1, a2, ...)), stacktrace) #we want to propagate (exception, stacktrace) exceptionAndVariables, stacktrace = resVal exception, variableAssignments = exceptionAndVariables #iterate through the binding and update the original for ix, a in enumerate(assignments): boundValues[a] = binding[a] = variableAssignments[ix] boundValues['result'] = ForaValue.FORAException((exception, ForaValue.FORAValue(stacktrace))) else: #packAssignedVarsIntoTuple puts the result in the first tuple element #and the assigned variables in the second actualResult, boundSymbolValues, assignedOutputs = resVal #iterate through the binding and update the original for ix, a in enumerate(lets): boundValues[a] = binding[a] = boundSymbolValues[ix] for ix, a in enumerate(assignments): boundValues[a] = binding[a] = assignedOutputs[ix] boundValues['result'] = actualResult return boundValues