示例#1
0
文件: FORA.py 项目: Sandy4321/ufora
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())
示例#2
0
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())
示例#3
0
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)
示例#4
0
文件: ForaValue.py 项目: ufora/ufora
 def __call__(self, *args):
     return processComputationResult(
         Evaluator.evaluator().evaluate(
             self.implVal_,
             FORAValue.symbol_Call.implVal_,
             *[FORAValue(x).implVal_ for x in args]
             )
         )
示例#5
0
文件: ForaValue.py 项目: ufora/ufora
 def __getitem__(self, x):
     return processComputationResult(
         Evaluator.evaluator().evaluate(
             self.implVal_,
             FORAValue.symbol_GetItem.implVal_,
             FORAValue(x).implVal_
             )
         )
示例#6
0
文件: ForaValue.py 项目: ufora/ufora
 def __len__(self):
     return processComputationResult(
         Evaluator.evaluator().evaluate(
             FORAValue.symbol_Size.implVal_,
             FORAValue.symbol_Call.implVal_,
             self.implVal_
             )
         )
示例#7
0
 def __len__(self):
     return processComputationResult(
         Evaluator.evaluator().evaluate(
             FORAValue.symbol_Size.implVal_,
             FORAValue.symbol_Call.implVal_,
             self.implVal_
             )
         )
示例#8
0
 def __getitem__(self, x):
     return processComputationResult(
         Evaluator.evaluator().evaluate(
             self.implVal_,
             FORAValue.symbol_GetItem.implVal_,
             FORAValue(x).implVal_
             )
         )
示例#9
0
 def __call__(self, *args):
     return processComputationResult(
         Evaluator.evaluator().evaluate(
             self.implVal_,
             FORAValue.symbol_Call.implVal_,
             *[FORAValue(x).implVal_ for x in args]
             )
         )
示例#10
0
    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)))
示例#11
0
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)
示例#12
0
文件: ForaValue.py 项目: ufora/ufora
    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()
示例#13
0
    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()
示例#14
0
    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
示例#15
0
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_
                )
            )
示例#16
0
    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)
                )
            )
示例#17
0
文件: ForaValue.py 项目: ufora/ufora
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_
                )
            )
示例#18
0
    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)
示例#19
0
    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)
示例#20
0
    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_)
示例#21
0
    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_)
示例#22
0
def evaluator():
    return Evaluator.evaluator()
示例#23
0
def evaluator():
    return Evaluator.evaluator()
示例#24
0
 def tearDown(self):
     self.evaluator.teardown()
     Evaluator.swapEvaluator(self.oldEvaluator)
示例#25
0
文件: FORA.py 项目: vishnur/ufora
    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
示例#26
0
 def tearDown(self):
     self.evaluator.teardown()
     Evaluator.swapEvaluator(self.oldEvaluator)