Esempio n. 1
0
class ViewOfEntireCumulusSystem(ComputedGraph.Location):
    viewOfSystem_ = ComputedGraph.Mutable(object, lambda: ())
    recentGlobalUserFacingLogMessages_ = ComputedGraph.Mutable(
        object, lambda: ())
    totalMessageCountsEver_ = ComputedGraph.Mutable(object, lambda: 0)

    @ComputedGraph.ExposedProperty()
    def mostRecentMessages(self):
        return self.recentGlobalUserFacingLogMessages_

    @ComputedGraph.ExposedProperty()
    def totalMessagesEver(self):
        return self.totalMessageCountsEver_

    @ComputedGraph.ExposedFunction()
    def clearMostRecentMessages(self, arg):
        self.recentGlobalUserFacingLogMessages_ = ()

    @ComputedGraph.ExposedFunction()
    def clearAndReturnMostRecentMessages(self, arg):
        messages = self.recentGlobalUserFacingLogMessages_
        self.recentGlobalUserFacingLogMessages_ = ()
        return messages

    @ComputedGraph.ExposedProperty()
    def viewOfCumulusSystem(self):
        return self.viewOfSystem_

    @ComputedGraph.ExposedFunction()
    def pushNewGlobalUserFacingLogMessage(self, msg):
        self.totalMessageCountsEver_ = self.totalMessageCountsEver_ + 1
        self.recentGlobalUserFacingLogMessages_ = (
            self.recentGlobalUserFacingLogMessages_ + \
                ({"timestamp": msg.timestamp, "message": msg.message, "isDeveloperFacing": msg.isDeveloperFacing, },)
            )
Esempio n. 2
0
    def convertResponseToJson(self, jsonCandidate):
        try:
            def raiseException():
                guid = uuid.uuid4()

                logging.error("%s of type %s is not valid json. guid = %s", jsonCandidate, type(jsonCandidate), guid)
                raise Exceptions.SubscribableWebObjectsException("result was not valid json. Guid = %s" % guid)

            if jsonCandidate is None:
                return jsonCandidate

            if isinstance(jsonCandidate, (int,str,unicode,float,bool,long)):
                return jsonCandidate

            if isinstance(jsonCandidate, (list, tuple)):
                return [self.convertResponseToJson(r) for r in jsonCandidate]

            if isinstance(jsonCandidate, dict):
                newDict = {}
                for k,v in jsonCandidate.iteritems():
                    if not isinstance(k,str):
                        raiseException()
                    newDict[k] = self.convertResponseToJson(v)

                return newDict

            objDefPopulated = False
            try:
                if ComputedGraph.isLocation(jsonCandidate):
                    if jsonCandidate in self.objectToIdCache_:
                        objDef = {
                            'objectId_': self.lookupObjectId(jsonCandidate)
                            }
                    else:
                        objDef = {
                            "objectId_": self.lookupObjectId(jsonCandidate),
                            "objectDefinition_": {
                                'type': AllObjectClassesToExpose.typenameFromType(
                                    ComputedGraph.getLocationTypeFromLocation(jsonCandidate)
                                    ),
                                'args': jsonCandidate.__reduce__()[1][0]
                                }
                            }
                    objDefPopulated = True
                else:
                    objDef = jsonCandidate.objectDefinition_
                    objDefPopulated = True
            except AttributeError:
                objDefPopulated = False

            if objDefPopulated:
                return self.convertResponseToJson(objDef)

            raiseException()
        except:
            logging.error("%s of type %s is not valid json.", jsonCandidate, type(jsonCandidate))
            raise
Esempio n. 3
0
    def convertResponseToJson(self, jsonCandidate):
        try:

            def raiseException():
                guid = uuid.uuid4()

                logging.error("%s of type %s is not valid json. guid = %s", jsonCandidate, type(jsonCandidate), guid)
                raise Exceptions.SubscribableWebObjectsException("result was not valid json. Guid = %s" % guid)

            if jsonCandidate is None:
                return jsonCandidate

            if isinstance(jsonCandidate, (int, str, unicode, float, bool, long)):
                return jsonCandidate

            if isinstance(jsonCandidate, (list, tuple)):
                return [self.convertResponseToJson(r) for r in jsonCandidate]

            if isinstance(jsonCandidate, dict):
                newDict = {}
                for k, v in jsonCandidate.iteritems():
                    if not isinstance(k, str):
                        raiseException()
                    newDict[k] = self.convertResponseToJson(v)

                return newDict

            objDefPopulated = False
            try:
                if ComputedGraph.isLocation(jsonCandidate):
                    if jsonCandidate in self.objectToIdCache_:
                        objDef = {"objectId_": self.lookupObjectId(jsonCandidate)}
                    else:
                        objDef = {
                            "objectId_": self.lookupObjectId(jsonCandidate),
                            "objectDefinition_": {
                                "type": AllObjectClassesToExpose.typenameFromType(
                                    ComputedGraph.getLocationTypeFromLocation(jsonCandidate)
                                ),
                                "args": jsonCandidate.__reduce__()[1][0],
                            },
                        }
                    objDefPopulated = True
                else:
                    objDef = jsonCandidate.objectDefinition_
                    objDefPopulated = True
            except AttributeError:
                objDefPopulated = False

            if objDefPopulated:
                return self.convertResponseToJson(objDef)

            raiseException()
        except:
            logging.error("%s of type %s is not valid json.", jsonCandidate, type(jsonCandidate))
            raise
Esempio n. 4
0
class PersistentCacheIndex(ComputedGraph.Location):
    totalBytesInCache = ComputedGraph.Mutable(object, lambda: 0)
    totalObjectsInCache = ComputedGraph.Mutable(object, lambda: 0)
    totalComputationsInCache = ComputedGraph.Mutable(object, lambda: 0)
    totalReachableComputationsInCache = ComputedGraph.Mutable(
        object, lambda: 0)

    @ComputedGraph.ExposedProperty()
    def persistentCacheState(self):
        return {
            "totalBytesInCache":
            self.totalBytesInCache,
            "totalObjectsInCache":
            self.totalObjectsInCache,
            "totalComputationsInCache":
            self.totalComputationsInCache,
            "totalReachableComputationsInCache":
            self.totalReachableComputationsInCache
        }

    @ComputedGraph.ExposedFunction()
    def triggerGarbageCollectionImmediately(self, completePurge):
        ComputedValueGateway.getGateway(
        ).triggerPerstistentCacheGarbageCollection(
            True if completePurge else False)

    @ComputedGraph.ExposedFunction()
    def setMaxBytesInCache(self, *args):
        ComputedValueGateway.getGateway().getPersistentCacheIndex(
        ).setMaxBytesInCache(args[0])

    @ComputedGraph.ExposedProperty()
    def maxBytesInCache(self):
        if ComputedValueGateway.getGateway().getPersistentCacheIndex() is None:
            return 0

        return ComputedValueGateway.getGateway().getPersistentCacheIndex(
        ).getMaxBytesInCache()

    @ComputedGraph.Function
    def update(self):
        if ComputedValueGateway.getGateway().getPersistentCacheIndex() is None:
            return

        self.totalBytesInCache = ComputedValueGateway.getGateway(
        ).getPersistentCacheIndex().totalBytesInCache()
        self.totalObjectsInCache = ComputedValueGateway.getGateway(
        ).getPersistentCacheIndex().totalObjectsInCache()
        self.totalComputationsInCache = ComputedValueGateway.getGateway(
        ).getPersistentCacheIndex().totalComputationsInCache()
        self.totalReachableComputationsInCache = ComputedValueGateway.getGateway(
        ).getPersistentCacheIndex().totalReachableComputationsInCache()
Esempio n. 5
0
class TestCGLocation(ComputedGraph.Location):
    definition = ComputedGraph.Key(object)
    aValue = ComputedGraph.Mutable(lambda: None, exposeToProtocol=True)

    def sharedStateSubspace(self):
        return CGSS.Node.Keyspace(keyspacePath=("public", "writeable",
                                                "TestCGLocation")).subspace

    aValueInSharedState = CGSS.Property.Property(default=lambda: False,
                                                 exposeToProtocol=True)
    bValueInSharedState = CGSS.Property.Property(default=lambda: False,
                                                 exposeToProtocol=True)
    cValueInSharedState = CGSS.Property.Property(default=lambda: False,
                                                 exposeToProtocol=True)

    @ComputedGraph.ExposedProperty()
    def depth(self):
        if isinstance(self.definition, Test):
            return self.definition.depth + 1
        elif isinstance(self.definition, list):
            res = 0
            for x in self.definition:
                res += x.depth
            return res
        else:
            return 0

    @ComputedGraph.ExposedProperty()
    def testCgLocation(self):
        return self

    @ComputedGraph.ExposedFunction()
    def aFunction(self, jsonArg):
        self.aValue = jsonArg

    @ComputedGraph.Function
    def anUnexposedFunction(self, jsonArg):
        self.aValue = jsonArg

    @ComputedGraph.ExposedFunction()
    def testFunction(self, arg):
        return arg

    @ComputedGraph.ExposedFunction(wantsCallback=True)
    def aFunctionExpectingCallback(self, callback, jsonArg):
        def aThread():
            time.sleep(1)
            callback(jsonArg)

        threading.Thread(target=aThread).start()
    def waitForSync(self, predicate, *synchronizers):
        if not synchronizers:
            synchronizers = [self.synchronizer]

        ComputedGraph.assertHasGraph()
        passes = 0
        while passes < 100:
            for s in synchronizers:
                s.update()
            if predicate():
                break
            time.sleep(.1)
            passes += 1
        self.assertTrue(predicate())
Esempio n. 7
0
    def waitForSync(self, predicate, *synchronizers):
        if not synchronizers:
            synchronizers = [self.synchronizer]

        ComputedGraph.assertHasGraph()
        passes = 0
        while passes < 100:
            for s in synchronizers:
                s.update()
            if predicate():
                break
            time.sleep(.1)
            passes += 1
        self.assertTrue(predicate())
Esempio n. 8
0
class SubscriptionKeys(ComputedGraph.Location):
    subscriptionKeys = ComputedGraph.Mutable(object)

    def keys(self):
        if self.subscriptionKeys is None:
            return []
        return self.subscriptionKeys
Esempio n. 9
0
    def testCreate(self):
        testGraph = ComputedGraph()
        def thing(x):
            print "thing called for onUpdate"
        class X(Location):
            k = Key()
            v = Mutable(int, lambda: 8, onUpdate = thing)
            u = 9000
            @Function
            def testFunc(self, x):
                print 'here is testFunc'
                print 'testFunc has self.v =', self.v
                print 'testFunc has x =', x
                print 'testFunc returning', self.v + x
                return self.v + x
            @NotCached
            def testNC(self):
                return self.k*4
        with testGraph:
            n = X(k = 902)
            pass

        print "inital value: n.v =", n.v
        print "about to change value.."
        n.v = 33
        print "n.v =", n.v

        print "Accessing key.."
        print "n.k =", n.k

        print "testing func:", n.testFunc(-3)
        print n.testNC
        print n.u # 'passing class member on directly'
Esempio n. 10
0
    def testLocKey(self):
        testGraph = ComputedGraph()
        class X(Location):
            k1 = Key()
            k2 = Key()
            v1 = Mutable(int, lambda: 10)
            v2 = Mutable(int, lambda: 20)
            v3 = Mutable(dict, lambda: dict())
            def product(self):
                print "self =", self
                return self.v1*self.v2
            def func2(self):
                print "self =", self
                return 1.0*self.product
            def func3(self):
                print "self =", self
                return 1.0*self.func2
        class Y(Location):
            v = Key() #X(k1 = 0, k2 = 0) # Key which holds LocRef
            v2 = Mutable(int, lambda: 3)
            v3 = Mutable(str, lambda: str(''))
            def	number(self):
                return self.v2*self.v.func3

        with testGraph:
            nodeX1 = X(k1 = 20, k2 = 25)
            nodeX1.v3 = {'h':1, 'e':2, 'l':3, 'p':4}
            nodeY = Y(v = nodeX1)
        print nodeY.number
        print "CHANGING"
        nodeX1.v1 = 11
        print nodeY.number
Esempio n. 11
0
 def testOrphan(self):
     testGraph = ComputedGraph()
     class X(Location):
         k1 = Key()
         k2 = Key()
         v1 = Mutable(int, lambda: 10)
         v2 = Mutable(int, lambda: 20)
         def product(self):
             return self.v1*self.v2
     class Y(Location):
         k = Key()
         v = Mutable(X, lambda:  X(k1 = 0, k2 = 0))
         v2 = Mutable(int, lambda: 3)
         def	number(self):
             return self.v2*self.v.product
     with testGraph:
         nodeX1 = X(k1 = 20, k2 = 25)
         nodeX2 = X(k1 = 21, k2 = 26)
         nodeY = Y(k = 10)
     nodeX1.v1 = 11
     nodeX2.v2 = 12
     nodeY.v = nodeX1
     print nodeY.number
     print "CHANGING"
     nodeY.v = nodeX2
     print nodeY.number
Esempio n. 12
0
 def testCached(self):
     factor = 1
     val = 2
     testGraph = ComputedGraph()
     class X(Location):
         k1 = Key()
         k2 = Key()
         v1 = Mutable(int, lambda: 10)
         v2 = Mutable(int, lambda: 20)
         def product(self):
             return factor*self.v1*self.v2
     class Y(Location):
         k = Key()
         v = Mutable(X, lambda:  X(k1 = 0, k2 = 0))
         v2 = Mutable(int, lambda: 3)
         def	number(self):
             return self.v2*(self.v.v1 + self.v.v2) + val
     with testGraph:
         node = X(k1 = 20, k2 = 25)
         node2 = Y(k = 9)
     node2.v = node
     self.assert_(node.product == node.v1*node.v2, "First access to property gives the wrong value")
     self.assert_(node2.number == node2.v2*(node2.v.v1 + node2.v.v2) + val, "First access to property gives the wrong value")
     factor += 1
     val += 1
     self.assert_(node.product == node.v1*node.v2, "Property did not correctly return cached value")
     self.assert_(node2.number == node2.v2*(node2.v.v1 + node2.v.v2) + val - 1, "Property did not correctly return cached value")
     node.v1 = 90000
     self.assert_(node.product == factor*node.v1*node.v2, "After adjusting mutable, property was not recomputed")
     self.assert_(node2.number == node2.v2*(node2.v.v1 + node2.v.v2) + val, "After adjusting mutable, property was not recomputed")
Esempio n. 13
0
    def extractObjectDefinition(self, objDefJson):
        if "objectId_" in objDefJson or "objectDefinition_" in objDefJson:
            return self.convertObjectArgs(objDefJson)

        if "type" not in objDefJson or "args" not in objDefJson:
            raise MalformedMessageException("Malformed object definition given")

        objType = objDefJson["type"]
        objectArgs = objDefJson["args"]

        objectArgs = self.convertObjectArgs(objectArgs)

        if objType not in AllObjectClassesToExpose.classMap:
            raise InvalidObjectDefinitionException("Unknown object type")

        try:
            objectCls = AllObjectClassesToExpose.classMap[objType]
            result = objectCls(objectArgs)

            if not ComputedGraph.isLocation(result):
                result.__dict__["objectDefinition_"] = objDefJson

            return result
        except Exceptions.SubscribableWebObjectsException as e:
            raise InvalidObjectDefinitionException(e.message)
Esempio n. 14
0
    def extractObjectDefinition(self, objDefJson):
        if 'objectId_' in objDefJson or 'objectDefinition_' in objDefJson:
            return self.convertObjectArgs(objDefJson)

        if 'type' not in objDefJson or 'args' not in objDefJson:
            raise MalformedMessageException("Malformed object definition given")

        objType = objDefJson['type']
        objectArgs = objDefJson['args']

        objectArgs = self.convertObjectArgs(objectArgs)

        if objType not in AllObjectClassesToExpose.classMap:
            raise InvalidObjectDefinitionException("Unknown object type")

        try:
            objectCls = AllObjectClassesToExpose.classMap[objType]
            result = objectCls(objectArgs)

            if not ComputedGraph.isLocation(result):
                result.__dict__['objectDefinition_'] = objDefJson

            return result
        except Exceptions.SubscribableWebObjectsException as e:
            raise InvalidObjectDefinitionException(e.message)
Esempio n. 15
0
class PyforaDictionaryElement(PyforaComputedValue):
    #the base PyforaComputedValue of which we are one of the dictionary members
    baseCV = ComputedGraph.Key(object)

    #the keyname in self.baseCV.pyforaDictToStringDict we are tied to
    keyname = ComputedGraph.Key(object)

    def argIds(self):
        return ()

    def args(self):
        return (self.baseCV,
                ForaNative.makeSymbol("RawGetItemByString"),
                ForaNative.ImplValContainer(self.keyname))

    def __str__(self):
        return "PyforaDictionaryElement(baseCV=%s,keyname=%s)" % (self.baseCV, self.keyname)
Esempio n. 16
0
class PyforaTupleElement(PyforaComputedValue):
    #the base PyforaComputedValue of which we are one of the tuple members
    baseCV = ComputedGraph.Key(object)

    #the index in self.baseCV.pyforaTupleToTuple we are tied to
    index = ComputedGraph.Key(object)

    def argIds(self):
        return ()

    def args(self):
        return (self.baseCV,
                ForaNative.makeSymbol("RawGetItemByInt"),
                ForaNative.ImplValContainer(self.index))

    def __str__(self):
        return "PyforaTupleElement(baseCV=%s,index=%s)" % (self.baseCV, self.index)
Esempio n. 17
0
 def testMutableDefaults(self):
     testGraph = ComputedGraph()
     class X(Location):
         k1 = Key()
         k2 = Key()
         v1 = Mutable(int, lambda: 10)
         v2 = Mutable(int, lambda: 20)
     with testGraph:
         node = X(k1 = 20, k2 = 25)
     self.assert_((node.v1 == 10) and (node.v2 == 20), "Mutables are not correctly set to default values")
Esempio n. 18
0
class WriteToS3Task(ComputedGraph.Location):
    #the computation we want to write
    computedValue = object
    bucketname = object
    keyname = object

    #either None if it's uploading, or {'success': True/False, ['message': msg]}
    successOrError = ComputedGraph.Mutable(object,
                                           lambda: None,
                                           exposeToProtocol=True)

    @ComputedGraph.Function
    def trigger(self):
        if self.successOrError is not None:
            return

        if self.computedValue.valueIVC is None:
            self.successOrError = {
                'success':
                False,
                'message':
                "Tried to trigger write before calculation was finished."
            }
            return

        if not self.computedValue.valueIVC.isVectorOfChar():
            self.successOrError = {
                'success': False,
                'message': "Result should have been a string."
            }
            return
        if self.computedValue.isException:
            self.successOrError = {
                'success':
                False,
                'message':
                "Result should have been a string. Got an exception instead."
            }
            return

        def callback(result):
            if result.isSuccess():
                self.successOrError = {'success': True}
            else:
                self.successOrError = {
                    'success': False,
                    'message': str(result)
                }

        ComputedValueGateway.getGateway().createExternalIoTask(
            CumulusNative.ExternalIoTask.WriteCharBigvecToS3(
                self.computedValue.valueIVC.getVectorBigvecGuid(),
                CumulusNative.S3KeyAndCredentials(self.bucketname,
                                                  self.keyname, "", "", "")),
            callback)
Esempio n. 19
0
 def testProperty(self):
     testGraph = ComputedGraph()
     class X(Location):
         k1 = Key()
         k2 = Key()
         v1 = Mutable(int, lambda: 10)
         v2 = Mutable(int, lambda: 20)
         def product(self):
             return self.v1*self.v2
     with testGraph:
         node = X(k1 = 20, k2 = 25)
     self.assert_(node.product == node.v1 * node.v2, "Property does not return correct value")
Esempio n. 20
0
 def testInitializer(self):
     testGraph = ComputedGraph()
     class X(Location):
         k1 = Key()
         k2 = Key()
         v1 = Mutable(int, lambda: 10)
         v2 = Mutable(int, lambda: 20)
         v3 = Mutable(int, lambda: 9)
         @Initializer
         def initialize(self):
             self.v3 = 200
     with testGraph:
         node = X(k1 = 21, k2 = 25)
     self.assert_(node.v3 == 200, "Initializer function did not set the value correctly")
Esempio n. 21
0
 def testChangeMutable(self):
     testGraph = ComputedGraph()
     class X(Location):
         k1 = Key()
         k2 = Key()
         v1 = Mutable(int, lambda: 10)
         v2 = Mutable(int, lambda: 20)
         def product(self):
             return self.v1*self.v2
     with testGraph:
         node = X(k1 = 20, k2 = 25)
     node.v1 = 15
     self.assert_(node.v1 == 15, "Mutable has the wrong value after attempting to change it")
     self.assert_(node.product == node.v1 * node.v2, "Property does not return correct value after changing mutable")
Esempio n. 22
0
def setUpComputedValueTest(tester):
    tester.graph = ComputedGraph.ComputedGraph()
    tester.graph.__enter__()

    def gatewayFactory(callbackScheduler, vdm):
        return CumulusGatewayInProcess.InProcessGateway(callbackSchedulerFactory, callbackScheduler, vdm)

    tester.computedValueGateway = \
            ComputedValueGateway.CumulusComputedValueGateway(
                callbackSchedulerFactory,
                callbackScheduler,
                gatewayFactory
                )
    tester.computedValueGateway.__enter__()
Esempio n. 23
0
    def testTemp(self):
        testGraph = ComputedGraph()
        class Z(Location):
            k1 = Key()
        class X(Location):
            k1 = Key()
            k2 = Key()
            v1 = Mutable(int, lambda: 10)
            v2 = Mutable(int, lambda: 20)
            v3 = Mutable(Location, lambda: Z(k1 = 20) )
            def product(self):
                return self.v1*self.v2
            def func(self):
                return self.product + self.v3.v2
        class Y(Location):
            k = Key()
            v = Mutable(X, lambda:  X(k1 = 0, k2 = 0))
            v2 = Mutable(int, lambda: 3)
            def	number(self):
                return self.v2*self.v.product
            def yfunc(self):
                return self.v.func*self.number
        with testGraph:
            node = X(k1 = 20, k2 = 25)
            node2 = Y(k = 9)
        node2.v = node
        node.v3 = node2

        print "node2.yfunc:", node2.yfunc
        node2.v2 = 9
        print "node2.yfunc:", node2.yfunc

        self.assert_(node.product == node.v1*node.v2, "First access to property gives the wrong value")
        self.assert_(node2.number == node2.v2*node2.v.product, "First access to property gives the wrong value")
        print node2.number
        node.v1 = 15
        print node2.number
        self.assert_(node2.number == node2.v2*node2.v.product, "After adjusting mutable, property was not recomputed")
        print "UP/DOWN TEST"
        print "Up from Y number:", node2.propUp("number")
        print "Down from X v1:", node.propDown("v1")
        print "Up from X product:", node.propUp("product")
        print "UP/DOWN TEST DONE"
        print "Depth 2 prop up from number"
        l = node2.propUp("number")
        print "Level 1:", l
        for l2 in l:
            print "Level 2 from", l2, "is", l2[0].propUp(l2[2])
Esempio n. 24
0
 def testNotCached(self):
     factor = 1.0
     testGraph = ComputedGraph()
     class X(Location):
         k1 = Key()
         k2 = Key()
         v1 = Mutable(int, lambda: 10)
         v2 = Mutable(int, lambda: 20)
         @NotCached
         def product(self):
             return factor*self.v1*self.v2
     with testGraph:
         node = X(k1 = 20, k2 = 25)
     self.assert_(node.product == factor*node.v1*node.v2, "Accessing NotCached property gives the wrong value")
     factor = 2.0
     self.assert_(node.product == factor*node.v1*node.v2, "NotCached property did not change from the previous value")
Esempio n. 25
0
 def testKeys(self):
     testGraph = ComputedGraph()
     class X(Location):
         k1 = Key()
         k2 = Key()
         v1 = Mutable(int, lambda: 10)
     with testGraph:
         node = X(k1 = 20, k2 = 25)
     self.assert_(node.k1 == 20 and node.k2 == 25, "Keys do not have correct values")
     try:
         node.k1 = 31
     except:
         pass
     else:
         self.assert_(False, "Exception not thrown when attempting to change key values")
     self.assert_(node.k1 == 20 and node.k2 == 25, "Keys did not retain correct values")
Esempio n. 26
0
 def testFunction(self):
     testGraph = ComputedGraph()
     class X(Location):
         k1 = Key()
         k2 = Key()
         v1 = Mutable(int, lambda: 10)
         v2 = Mutable(int, lambda: 20)
         @Function
         def testFunc(self, val):
             return val*self.v1 + self.v2
     with testGraph:
         node = X(k1 = 20, k2 = 25)
     self.assert_(node.testFunc(3) == 3*node.v1 + node.v2, "Function returns the wrong value")
     node.v1 = 903
     node.v2 = -8
     self.assert_(node.testFunc(3) == 3*node.v1 + node.v2, "Function returns the wrong value after changing mutables")
Esempio n. 27
0
class SimpleLocation(ComputedGraph.Location):
    """A very simple computed graph location for testing"""
    value = object
    mutVal = ComputedGraph.Mutable(object, lambda: 0)

    def property1(self):
        return self.value

    def property2(self):
        return self.property1 * 2

    def mutValTimes2(self):
        return self.mutVal * 2

    def mutValTimes4(self):
        return self.mutValTimes2 * 2
Esempio n. 28
0
class ComputedValueHeldByHash(ComputedValue.ComputedValue):
    args = None
    hash = object

    valueIVC_ = ComputedGraph.Mutable(object)

    def valueIVC(self):
        if self.valueIVC_ is None:
            return ForaNative.ImplValContainer()
        return self.valueIVC_

    isException = object
    isFinished = True
    workerCount = 0
    cacheloadCount = 0
    computationStatistics = None
Esempio n. 29
0
    def handleReadMessage(self, jsonMessage, objectToRead):
        fieldFun = self.getFieldExtractorForReadMessage(jsonMessage, objectToRead)
        try:
            return [{
                "messageId": jsonMessage["messageId"],
                "responseType": "ReadResponse",
                "value": self.outgoingObjectCache.convertResponseToJson(fieldFun(objectToRead))
                }]

        except Exception as e:
            try:
                objectType = objectToRead.__location_class__ if ComputedGraph.isLocation(objectToRead) else str(objectToRead)
                logging.info("converting %s of type %s", fieldFun(objectToRead), objectType)
            except:
                pass
            return [unexpectedExceptionJson(jsonMessage, Exceptions.wrapException(e).message)]
Esempio n. 30
0
 def testLocationAccess(self):
     testGraph = ComputedGraph()
     class X(Location):
         k1 = Key()
         k2 = Key()
         v1 = Mutable(int, lambda: 10)
         v2 = Mutable(int, lambda: 20)
         def product(self):
             return self.v1*self.v2
     with testGraph:
         node = X(k1 = 20, k2 = 25)
     node.v1 = 15
     with testGraph:
         node2 = X(k1 = 20, k2 = 25)
     self.assert_(node.v1 == node2.v1, "Accessing same location through different location references gives incorrect mutable values")
     self.assert_(node.v2 == node2.v2, "Accessing same location through different location references gives incorrect mutable values")
     self.assert_(node.product == node2.product, "Accessing same location through different location references gives incorrect property values")
Esempio n. 31
0
def validateComputedValueArgs(args):
    """check that every argument to a ComputedValue is either an ImplValContainer or
        another ComputedValue"""

    if not args:
        return False
    if not isinstance(args, tuple):
        return False
    for ix, a in enumerate(args):
        if ComputedGraph.isLocation(a):
            if not issubclass(a.__location_class__, ComputedValue):
                logging.error("Failed validation of args[%s].__location_class__ = '%s'", ix, a.__location_class__)
                return False
        else:
            if not isinstance(a, (ImplValContainer_, long, int, str, bool)):
                logging.error("Failed validation of args[%s].__class__ = '%s'", ix, a.__class__)
                return False
    return True
Esempio n. 32
0
    def testLocKey2(self):
        testGraph = ComputedGraph()
        class Chr(Location):
            strIn = Key()
            chrVal = Key()
            def position(self):
                d = {}
                for s in range(len(self.strIn.v3)):
                    d[self.strIn.v3[s]] = s
                return d[self]

        class Str(Location):
            k1 = Key()
            v3 = Mutable(list, lambda: list())
            @Function
            def setString(self, strText):
                v = list()
                for i in range(len(strText)):
                    v.append(Chr(strIn = self, chrVal = strText[i]))
                self.v3 = v
            def getStr(self):
                #print self.v3
                s = ''
                for i in range(len(self.v3)):
                    s += self.v3[i].chrVal
                return s

        with testGraph:
            testStr = Str(k1 = 20)
        print "Setting string"
        testStr.setString("hello")
        print "Printing string"
        print testStr.getStr
        #print "Reprinting string"
        #print testStr.getStr
        #print "Reprinting string again"
        #print testStr.getStr
        print "position:", testStr.v3[2].position
        #print "position again:", testStr.v3[2].position
        #print "char:", testStr.v3[2].chrVal
        print "Setting string"
        testStr.setString("some other string")
        print "Printing string"
        print testStr.getStr
Esempio n. 33
0
    def unfinishedDependentCodeLocationsAsJson(self):
        res = []

        for cv in self.rootComputedValueDependencies:
            if not cv.isFinished:
                #each computed value we depend on might be a visualize. If so, we want
                #the actual script computation (if available)
                cv = cv.unwrapVisualizable

                cv = cv.unwrapMember

                #only some ComputedValue objects have this property
                loc = cv.codeLocationDefinitionAsJson
                if loc is not None:
                    res.append(loc)
                else:
                    res.append(str(ComputedGraph.getLocationTypeFromLocation(cv)))

        return tuple(res)
Esempio n. 34
0
    def handleReadMessage(self, jsonMessage, objectToRead):
        fieldFun = self.getFieldExtractorForReadMessage(jsonMessage, objectToRead)
        try:
            return [
                {
                    "messageId": jsonMessage["messageId"],
                    "responseType": "ReadResponse",
                    "value": self.outgoingObjectCache.convertResponseToJson(fieldFun(objectToRead)),
                }
            ]

        except Exception as e:
            try:
                objectType = (
                    objectToRead.__location_class__ if ComputedGraph.isLocation(objectToRead) else str(objectToRead)
                )
                logging.info("converting %s of type %s", fieldFun(objectToRead), objectType)
            except:
                pass
            return [unexpectedExceptionJson(jsonMessage, Exceptions.wrapException(e).message)]
Esempio n. 35
0
    def __init__(self, callbackSchedulerFactory, callbackScheduler, cumulusGatewayFactory):
        self.callbackScheduler = callbackScheduler
        ComputedValueGateway.__init__(self)

        Stoppable.Stoppable.__init__(self)

        CacheLoader.__init__(
            self,
            callbackSchedulerFactory,
            callbackScheduler,
            Setup.config().computedValueGatewayRAMCacheMB * 1024 * 1024
            )

        logging.info("cumulusGatewayFactory is %s", cumulusGatewayFactory)

        self.cumulusGateway = cumulusGatewayFactory(self.callbackScheduler, self.vdm)
        self.externalIoTaskCallbacks_ = {}

        self.refcountTracker = self.cumulusGateway.cumulusClient.getSystemwidePageRefcountTracker()

        self.cumulusGateway.onNewGlobalUserFacingLogMessage = self.onNewGlobalUserFacingLogMessage
        self.cumulusGateway.onExternalIoTaskCompleted = self.onExternalIoTaskCompleted
        self.cumulusGateway.onCPUCountChanged = self.onCPUCountChanged
        self.cumulusGateway.onCacheLoad = self.onCacheLoad
        self.cumulusGateway.onComputationResult = self.onComputationResult
        self.cumulusGateway.onMachineCountWentToZero = self.onMachineCountWentToZero

        self.refcountsForCompIds_ = DefaultDict.DefaultDict(lambda computedValue: 0)

        self.computedValuesForComputations = {}
        self.finishedResultsForComputations = {}

        self.curPriorityIndex = 0

        self.lock_ = threading.RLock()

        assert ComputedGraph.currentGraph() is not None

        self.cumulusGateway.onJsonViewOfSystemChanged = self.onJsonViewOfSystemChanged
Esempio n. 36
0
def isComputedValue(o):
    return ComputedGraph.isLocation(o) and issubclass(o.__location_class__, ComputedValue)
Esempio n. 37
0
def getObjectClass(o):
    if ComputedGraph.isLocation(o):
        return ComputedGraph.getLocationTypeFromLocation(o)
    return o.__class__