changeset 894:836ea837cae9

curvecalc just barely running its subexprs and outputting levels Ignore-this: 76cbc3bf4174e6620058945cfa229dce
author Drew Perttula <drewp@bigasterisk.com>
date Sun, 09 Jun 2013 01:02:13 +0000
parents 1b19038f42e2
children de9b9d0eed62
files bin/curvecalc light9/curvecalc/output.py light9/curvecalc/subterm.py light9/curvecalc/subtermview.py light9/rdfdb/currentstategraphapi.py light9/rdfdb/grapheditapi.py
diffstat 6 files changed, 79 insertions(+), 82 deletions(-) [+]
line wrap: on
line diff
--- a/bin/curvecalc	Sat Jun 08 23:57:33 2013 +0000
+++ b/bin/curvecalc	Sun Jun 09 01:02:13 2013 +0000
@@ -197,7 +197,7 @@
         
         for st in set(self.graph.objects(song, L9['subterm'])):
             log.info("song %s has subterm %s", song, st)
-            term = Subterm(self.graph, st, self.songSubtermsContext())
+            term = Subterm(self.graph, st, self.songSubtermsContext(), self.curveset)
             add_one_subterm(term, self.curveset, master)
 
         master.show_all()
@@ -368,7 +368,7 @@
     graph.addHandler(curvesetReload)
         
     log.debug("startup: output %s", time.time() - startTime)
-    out = Output(music)
+    out = Output(graph, session, music, curveset)
 
     mt = MaxTime(graph, session)
     dispatcher.connect(lambda: mt.get(), "get max time", weak=False)
--- a/light9/curvecalc/output.py	Sat Jun 08 23:57:33 2013 +0000
+++ b/light9/curvecalc/output.py	Sun Jun 09 01:02:13 2013 +0000
@@ -1,16 +1,18 @@
 import time, logging
 from twisted.internet import reactor
 from light9 import Submaster, dmxclient
+from light9.namespaces import L9
+from light9.curvecalc.subterm import Subterm
+
 from louie import dispatcher
 log = logging.getLogger("output")
 
-firstWarn = False
-
-class Output:
+class Output(object):
     lastsendtime=0
     lastsendlevs=None
-    def __init__(self, music):
-        self.music = music
+    def __init__(self, graph, session, music, curveset):
+        self.graph, self.session, self.music = graph, session, music
+        self.curveset = curveset
 
         self.recent_t=[]
         self.later = None
@@ -46,18 +48,19 @@
         dispatcher.send("update period", val=period)
         self.send_dmx(t)
         
-    def send_dmx(self,t):
-        global firstWarn
-        if not firstWarn:
-            log.warn("skipping Output.send_dmx")
-            firstWarn = True
-        return
+    def send_dmx(self, t):
         dispatcher.send("curves to sliders", t=t)
         scaledsubs=[]
-        # this needs to use graph instead
-        for st in self.subterms:
-            scl = st.scaled(t)
-            scaledsubs.append(scl)
+
+        with self.graph.currentState() as current:
+            song = current.value(self.session, L9['currentSong'])
+            for st in current.objects(song, L9['subterm']):
+                # this is getting especially broken to have Output
+                # being able to remake the Subterm on each frame. Some
+                # object should maintain all the subterms for us.
+                scl = Subterm(self.graph, st, None, self.curveset).scaled(current, t)
+                scaledsubs.append(scl)
+                
         out = Submaster.sub_maxes(*scaledsubs)
         levs = out.get_levels()
         now=time.time()
--- a/light9/curvecalc/subterm.py	Sat Jun 08 23:57:33 2013 +0000
+++ b/light9/curvecalc/subterm.py	Sun Jun 09 01:02:13 2013 +0000
@@ -71,49 +71,16 @@
 
 exprglo = Expr()
         
-class Subexpr:
-    curveset = None
-    def __init__(self,curveset,expr="",graph=None):
+class Subterm(object):
+    """one Submaster and its expression evaluator"""
+    def __init__(self, graph, subterm, saveContext, curveset):
+        self.graph, self.uri = graph, subterm
+        self.saveContext = saveContext
         self.curveset = curveset
-        self.lasteval = None
-        self.expr = expr
-        self.graph = graph
+        self.ensureExpression(saveContext)
+
         self._smooth_random_items = [random.random() for x in range(100)]
         self.submasters = Submaster.get_global_submasters(self.graph)
-
-    def eval(self,t):
-        if self.expr=="":
-            dispatcher.send("expr_error",sender=self,exc="no expr, using 0")
-            return 0
-        glo = self.curveset.globalsdict()
-        glo['t'] = t
-
-        glo = exprglo.exprGlobals(glo, t)
-        glo['getsub'] = lambda name: self.submasters.get_sub_by_name(name)
-        
-        try:
-            self.lasteval = eval(self.expr,glo)
-        except Exception,e:
-            dispatcher.send("expr_error",sender=self,exc=e)
-        else:
-            dispatcher.send("expr_error",sender=self,exc="ok")
-        return self.lasteval
-
-    def expr():
-        doc = "python expression for level as a function of t, using curves"
-        def fget(self):
-            return self._expr
-        def fset(self, value):
-            self._expr = value
-            dispatcher("expr_changed",sender=self)
-        return locals()
-    expr = property(**expr())
-
-class Subterm(object):
-    """one Submaster and its Subexpr"""
-    def __init__(self, graph, subterm, saveCtx):
-        self.graph, self.uri = graph, subterm
-        self.ensureExpression(saveCtx)
         
     def ensureExpression(self, saveCtx):
         with self.graph.currentState() as current:
@@ -121,11 +88,9 @@
                 self.graph.patch(Patch(addQuads=[
                     (self.uri, L9['expression'], Literal("..."), saveCtx),
                     ]))
-    
-    def scaled(self, t):
-        log.warn("skipping Subterm.scaled")
-        return 0
-        subexpr_eval = self.subexpr.eval(t)
+
+    def scaled(self, current, t):
+        subexpr_eval = self.eval(current, t)
         # we prevent any exceptions from escaping, since they cause us to
         # stop sending levels
         try:
@@ -135,10 +100,36 @@
             else:
                 # otherwise, return our submaster multiplied by the value 
                 # returned
-                return self.submaster * subexpr_eval
+                subUri = current.value(self.uri, L9['sub'])
+                sub = self.submasters.get_sub_by_uri(subUri)
+                return sub * subexpr_eval
         except Exception, e:
-            dispatcher.send("expr_error", sender=self.subexpr, exc=str(e))
+            dispatcher.send("expr_error", sender=self.uri, exc=repr(e))
             return Submaster.Submaster(name='Error: %s' % str(e), levels={})
+    
+    def eval(self, current, t):
+        """current graph is being passed as an optimization. It should be
+        equivalent to use self.graph in here."""
+
+        expr = current.value(self.uri, L9['expression'])
+        
+        if not expr:
+            dispatcher.send("expr_error", sender=self.uri, exc="no expr, using 0")
+            return 0
+        glo = self.curveset.globalsdict()
+        glo['t'] = t
+
+        glo = exprglo.exprGlobals(glo, t)
+        glo['getsub'] = lambda name: self.submasters.get_sub_by_name(name)
+        
+        try:
+            self.lasteval = eval(expr, glo)
+        except Exception,e:
+            dispatcher.send("expr_error", sender=self.uri, exc=e)
+            return Submaster.Submaster("zero", {})
+        else:
+            dispatcher.send("expr_error", sender=self.uri, exc="ok")
+        return self.lasteval
 
     def __repr__(self):
         return "<Subterm %s>" % self.uri
--- a/light9/curvecalc/subtermview.py	Sat Jun 08 23:57:33 2013 +0000
+++ b/light9/curvecalc/subtermview.py	Sun Jun 09 01:02:13 2013 +0000
@@ -1,9 +1,7 @@
 import gtk, logging
 from louie import dispatcher
-from rdflib import URIRef
-from light9 import Submaster
+from rdflib import Literal
 from light9.namespaces import L9
-from light9.curvecalc.subterm import Subterm, Subexpr
 log = logging.getLogger()
 
 # inspired by http://www.daa.com.au/pipermail/pygtk/2008-August/015772.html
@@ -11,8 +9,9 @@
 keep = []
 
 class Subexprview(object):
-    def __init__(self, graph, ownerSubterm):
+    def __init__(self, graph, ownerSubterm, saveContext):
         self.graph, self.ownerSubterm = graph, ownerSubterm
+        self.saveContext = saveContext
 
         self.box = gtk.HBox()
 
@@ -24,11 +23,9 @@
         self.box.pack_start(self.error, expand=False)
 
         self.entry.set_buffer(self.entryBuffer)
-        self.expr_changed()
+        self.graph.addHandler(self.set_expression_from_graph)
         self.entryBuffer.connect("deleted-text", self.entry_changed)
         self.entryBuffer.connect("inserted-text", self.entry_changed)
-        dispatcher.connect(self.expr_changed, "expr_changed",
-                           sender=self.ownerSubterm)
 
         dispatcher.connect(self.exprError, "expr_error", sender=self.ownerSubterm)
         keep.append(self.__dict__)
@@ -36,17 +33,20 @@
     def exprError(self, exc):
         self.error.set_text(str(exc))
         
-    def expr_changed(self):
-        log.warn("skip expr_changed")
-        return
-        e = str(self.subexpr.expr)
+    def set_expression_from_graph(self):
+        e = str(self.graph.value(self.ownerSubterm, L9['expression']))
+        print "was going to set to %r" % e
+
         if e != self.entryBuffer.get_text():
             self.entryBuffer.set_text(e, len(e))
             
     def entry_changed(self, *args):
-        log.warn("skip entry_changed")
+        log.info("want to patch to %r", self.entryBuffer.get_text())
         return
-        self.subexpr.expr = self.entryBuffer.get_text()
+        self.graph.patchObject(self.saveContext,
+                               self.ownerSubterm,
+                               L9['expression'],
+                               Literal(self.entryBuffer.get_text()))
             
 class Subtermview(object):
     """
@@ -59,7 +59,7 @@
         self.label = gtk.Label("sub")
         self.graph.addHandler(self.setName)
         
-        sev = Subexprview(self.graph, self.subterm.uri)
+        sev = Subexprview(self.graph, self.subterm.uri, self.subterm.saveContext)
         self.exprView = sev.box
 
     def setName(self):
--- a/light9/rdfdb/currentstategraphapi.py	Sat Jun 08 23:57:33 2013 +0000
+++ b/light9/rdfdb/currentstategraphapi.py	Sun Jun 09 01:02:13 2013 +0000
@@ -20,7 +20,7 @@
                 # writes/patches happening while reads are being
                 # done. Typical usage will do some reads on this graph
                 # before moving on to writes.
-                
+
                 g = ConjunctiveGraph()
                 for s,p,o,c in self._graph.quads((None,None,None)):
                     g.store.add((s,p,o), c)
--- a/light9/rdfdb/grapheditapi.py	Sat Jun 08 23:57:33 2013 +0000
+++ b/light9/rdfdb/grapheditapi.py	Sun Jun 09 01:02:13 2013 +0000
@@ -1,7 +1,8 @@
-import random
+import random, logging
 from itertools import chain
 from rdflib import URIRef, RDF
 from light9.rdfdb.patch import Patch
+log = logging.getLogger('graphedit')
 
 class GraphEditApi(object):
     """
@@ -22,10 +23,12 @@
                                      context=context):
             existing.append(spo+(context,))
         # what layer is supposed to cull out no-op changes?
-        self.patch(Patch(
+        p = Patch(
             delQuads=existing,
             addQuads=([(subject, predicate, newObject, context)]
-                      if newObject is not None else [])))
+                      if newObject is not None else []))
+        log.info("patchObject %r" % p.jsonRepr)
+        self.patch(p)
 
     def patchMapping(self, context, subject, predicate, nodeClass, keyPred, valuePred, newKey, newValue):
         """