def __init__(self, space, planning_mode=False): #self.deduction_types = ['SubsetLink', 'ImplicationLink', 'InheritanceLink', 'PredictiveImplicationLink'] self.deduction_types = [ 'ImplicationLink', 'InheritanceLink', 'PredictiveImplicationLink' ] self.pd = dict() self.results = [] self.space = space self.planning_mode = planning_mode self.viz = PLNviz(space) self.viz.connect() self.setup_rules() self.trace = Trace_Chainer() self.bc_later = OrderedSet() # simple statistics self.num_app_pdns_per_level = Counter() self.num_uses_per_rule_per_level = defaultdict(Counter) global _line _line = 1
def __new__(cls, id=None, parent=None, children=[], **kw): """ Takes care of Widget instances creation. Should not need to be overrien likely. """ obj = view.Renderable.__new__(cls, id, parent, children, **kw) obj.__dict__.update(v for v in kw.iteritems() if not v[0].startswith('_')) obj.orig_kw = kw.copy() if id is not None and not valid_id(id): raise ValueError("%r is not a valid id for a Widget" % id) obj._id = id obj._serial = serial_generator.next() # Attach the widget to its parent if parent is not None: if parent._is_initialized: raise WidgetInitialized obj.parent = weakref.proxy(parent) obj.parent.children.append(obj) # Append children passed as args or defined in the class, former # override later obj.c = obj.children = WidgetBunch() if isclass(children) and issubclass(children, WidgetsList): children = children() if not [obj._append_child(c) for c in children]: cls_children = cls._cls_children if isclass(cls_children) and issubclass(cls_children, WidgetsList): cls_children = cls_children() [obj._append_child(c) for c in cls_children] # Copy mutable attrs from __class__ into self, if not found in self # set to None for name in chain(cls.params, ['css', 'javascript']): try: attr = getattr(obj, name, None) if isinstance(attr, (list, dict)): attr = copy(attr) setattr(obj, name, attr) except AttributeError: # In case we try to set a read-only property pass # Initialize the static js calls list obj._js_calls = [] # Initialize resources OrderedSet obj._resources = OrderedSet() # Set default css class for the widget if not getattr(obj, 'css_class', None): obj.css_class = obj.__class__.__name__.lower() return obj
def retrieve_resources(self): """ Returns a dict keyed by location with ordered collections of resources from this widget and its children as values. """ from tw.api import locations resources = dict((k, OrderedSet()) for k in locations) for r in self._resources: resources[r.location].add(r) return resources
def __init__(self, space, planning_mode=False, allowed_rule_names=set(), use_fc=False): #self.deduction_types = ['SubsetLink', 'ImplicationLink', 'InheritanceLink', 'PredictiveImplicationLink'] self.deduction_types = [ 'ImplicationLink', 'InheritanceLink', 'PredictiveImplicationLink' ] self.use_fc = use_fc self.pd = dict() self.results = [] self.space = space self.planning_mode = planning_mode self.viz = PLNviz(space) self.viz.connect() self.setup_rules(allowed_rule_names) self.trace = Trace_Chainer() self.bc_later = OrderedSet() # If you do BC, it is necessary to put an App for every Atom available. # Let's suppose that means every Rule to start with... for rule in self.rules: self.bc_later.append(rule) # simple statistics self.num_app_pdns_per_level = Counter() self.num_uses_per_rule_per_level = defaultdict(Counter) global _line _line = 1
def retrieve_javascript(self): oset = OrderedSet() for child in self: oset.add_all(child.retrieve_javascript()) return oset
def retrieve_css(self): oset = OrderedSet() for child in self: oset.add_all(child.retrieve_css()) return oset
def bc(self, target, nsteps=2000, nresults=1): try: #import prof3d; prof3d.profile_me() #try: tvs = self.get_tvs(target) print "Existing target truth values:", map(str, tvs) #if len(tvs): # raise NotImplementedError('Cannot revise multiple TruthValues, so no point in doing this search') #log.info(format_log('bc', target)) self.bc_later = OrderedSet([target]) self.results = [] self.target = target # Have a sentinel application at the top self.root = T('WIN') dummy_app = rules.Rule(self.root, [target], name='producing target') # is this line necessary? self.rules.append(dummy_app) self.bc_later = OrderedSet([dummy_app]) self.add_app_to_pd(dummy_app) # viz - visualize the root self.viz.outputTarget(target, None, 0, 'TARGET') steps_so_far = 0 start = time() while self.bc_later and steps_so_far < nsteps and ( nresults == 0 or len(self.results) < nresults): self.bc_step() steps_so_far += 1 #msg = '%s goals expanded, %s remaining, %s Proof DAG Nodes' % (len(self.bc_before), len(self.bc_later), len(self.pd)) msg = 'done %s steps, %s goals remaining, %s Proof DAG Nodes' % ( steps_so_far, len(self.bc_later), len(self.pd)) #log.info(format_log(msg)) #log.info(format_log('time taken', time() - start)) #log.info(format_log('PD:')) #for pdn in self.pd: # log.info(format_log(len(pdn.args),str(pdn))) # Always print it at the end, so you can easily check the (combinatorial) efficiency of all tests after a change print msg for res in self.results: print 'Inference trail:' trail = self.trail(res) self.print_tree(trail) self.update_global_stats(trail) #print 'Action plan (if applicable):' #print self.extract_plan(trail) # for res in self.results: # self.viz_proof_tree(self.trail(res)) ret = [] for tr in self.results: # Create the Atom (before this point it's just a Tree) atom = atom_from_tree(tr, self.space) # haxx atom.tv = self.get_tvs(tr)[0] #ret.append(atom.h) ret.append(atom) # pprint(self.num_app_pdns_per_level) # pprint(self.num_uses_per_rule_per_level) # # pprint(Chainer.successful_num_app_pdns_per_level) # pprint(Chainer.successful_num_uses_per_rule_per_level) return ret except Exception, e: import traceback, pdb #pdb.set_trace() print traceback.format_exc(10) # Start the post-mortem debugger #pdb.pm() return []
def retrieve_resources(self): from tw.core.resources import merge_resources, locations resources = dict((k, OrderedSet()) for k in locations) for w in self: merge_resources(resources, w.retrieve_resources()) return resources
class Widget(view.Renderable): """ Base class for all widgets. Example: .. sourcecode:: python >>> w = Widget('foo') >>> w.id 'foo' >>> w = Widget('foo', children=[Widget('c1'), Widget('c2')]) >>> [c.id for c in w.children] ['foo_c1', 'foo_c2'] >>> [c.parent.id for c in w.children] ['foo', 'foo'] It is a **must** that all initial state is entirely determined by the arguments to this function. This means that two widgets (of the same class) that receive the same parameters must behave in exactly the same way. You should not rely on external sources inside __init__ to set initial state. If you need to fetch data from external sources, do it at :meth:`update_params` instead. Essential pre, and post initialization is done in :meth:`__new__` and :meth:`post_init` respectively. :meth:`post_init` is guaranteed to run after the instance finishes initialization and it's behavior is rather special as all post_init's in mro will be called to have a chance to set final state in the instance. Basic pre-initialization consists of binding all kw arguments to the widget instance, attaching the widget to it's parent (if given), attaching the children and copying mutable arguments listed at :attr:`params` from the class to the instance to avoid accidental manipulation. .. sourcecode:: python >>> w = Widget('foo', a=1, b=2) >>> w.id 'foo' >>> w.a 1 >>> w.b 2 Basic post-initialization consists of caching required CSS and JS resources and setting the widget as initialized preventing further modification of it's attributes. .. sourcecode:: python >>> w = Widget('foo', a='1', b='2') >>> w.a = 'bar' Traceback (most recent call last): ... WidgetLocked: The widget is locked. It's unthread-safe to alter it's attributes after initialization. Widget attributes can only be modified in this method because widgets should behave in a state-less way as they are shared among threads for multiple requests. Per request modification of variables sent to the template should be done inside :meth:`update_params` and all state flowing from parent to children should occur inside that dict. Widgets should be instantiated at import time and reused among requests, most widgets allow overriding most of their parameters (not neccesarily all of them) at display time to change behavior. You should try avoiding instantiating widgets for every request as their initialization could be quite expensive for heavily nested widgets. Request-local storage provided by the hosting framework in ``tw.framework.request`` can be used to pass state among widgets which don't share the same root. """ __metaclass__ = WidgetType parent = None default = None params = { 'id': ('The id of this widget. This id is used to reference a widget ' 'from its parent ``children`` attribute and is usually the ' 'DOM id of outermost HTML tag of the widget.'), 'css_class': 'Main CSS class for this widget', 'css_classes': 'A list with extra css classes for the widget.' } css = [] javascript = [] css_classes = [] _is_initialized = False _is_locked = False def displays_on(self): if self.is_root: return tw.framework.default_view else: return self.parent.engine_name displays_on = property(displays_on, doc="""\ Where the widget is being displayed on """) #XXX: Some of these properties could be implemented as static attributes def id(self): return '_'.join( reversed([w.id_path_elem for w in self.path if w.id_path_elem])) or None id = property(id, doc="""\ The calculated id of the widget. This string will provide a unique id for each widget in the tree in a format which allows to re-recreate the nested structure. Example:: >>> A = Widget("A", children=[ ... Widget("B", children=[ ... Widget("C") ... ]) ... ]) ... >>> C = A.c.B.c.C >>> C.id 'A_B_C' """) def key(self): return '.' + '.'.join( reversed([w.id_path_elem for w in self.path if w.id_path_elem][:-1])) or None key = property(key, doc="""\ A string that can be used as a key to index the dictionary of parameters sent to the root widget so it reaches this widget when displaying. Example:: >>> A = Widget("A", children=[ ... Widget("B", children=[ ... Widget("C") ... ]) ... ]) ... >>> C = A.c.B.c.C >>> C.key '.B.C' """) def path(self): item = self while item: yield item item = item.parent path = property(path, doc="""\ Iterates a walk from this widget to the root of the tree """) @property def id_path_elem(self): return self._id def root(self): return list(self.path)[-1] root = property(root, doc="The root of this widget tree") def is_root(self): return self.parent is None is_root = property(is_root, doc="True if the widget doesn't have a parent") def __new__(cls, id=None, parent=None, children=[], **kw): """ Takes care of Widget instances creation.Should not need to be overridden likely. """ obj = view.Renderable.__new__(cls, **kw) # The previous version of this used the __dict__ attribute to update # things, but that doesn't fire any fset properties, and can actually # lose data if a property is set, the following will not behave like # that for k, v in kw.iteritems(): if not k.startswith('_'): try: setattr(obj, k, v) except AttributeError, e: #skip setting the value of a read only property pass obj.orig_kw = kw.copy() if id is not None and not valid_id(id): raise ValueError("%r is not a valid id for a Widget" % id) obj._id = id obj._serial = serial_generator.next() # Attach the widget to its parent if parent is not None: if parent._is_initialized: raise WidgetInitialized obj.parent = weakref.proxy(parent) obj.parent.children.append(obj) # Append children passed as args or defined in the class, former # override later obj.c = obj.children = WidgetBunch() if isclass(children) and issubclass(children, WidgetsList): children = children() if not [obj._append_child(c) for c in children]: cls_children = cls._cls_children if isclass(cls_children) and issubclass(cls_children, WidgetsList): cls_children = cls_children() [obj._append_child(c) for c in cls_children] # Copy mutable attrs from __class__ into self, if not found in self # set to None for name in chain(cls.params, ['css', 'javascript']): try: attr = getattr(obj, name, None) if isinstance(attr, (list, dict)): attr = copy(attr) setattr(obj, name, attr) except AttributeError: # In case we try to set a read-only property pass # Initialize the static js calls list obj._js_calls = [] # Initialize resources OrderedSet obj._resources = OrderedSet() # Set default css class for the widget if not getattr(obj, 'css_class', None): obj.css_class = obj.__class__.__name__.lower() return obj
class Chainer: # Useful for testing, where you make multiple Chainer objects for separate AtomSpaces successful_num_app_pdns_per_level = Counter() successful_num_uses_per_rule_per_level = defaultdict(Counter) def __init__(self, space, planning_mode=False, allowed_rule_names=set(), use_fc=False): #self.deduction_types = ['SubsetLink', 'ImplicationLink', 'InheritanceLink', 'PredictiveImplicationLink'] self.deduction_types = [ 'ImplicationLink', 'InheritanceLink', 'PredictiveImplicationLink' ] self.use_fc = use_fc self.pd = dict() self.results = [] self.space = space self.planning_mode = planning_mode self.viz = PLNviz(space) self.viz.connect() self.setup_rules(allowed_rule_names) self.trace = Trace_Chainer() self.bc_later = OrderedSet() # If you do BC, it is necessary to put an App for every Atom available. # Let's suppose that means every Rule to start with... for rule in self.rules: self.bc_later.append(rule) # simple statistics self.num_app_pdns_per_level = Counter() self.num_uses_per_rule_per_level = defaultdict(Counter) global _line _line = 1 #profiler.add_function(self.bc) #@profile def bc(self, target, nsteps=2000, nresults=1): try: #import prof3d; prof3d.profile_me() #try: tvs = self.get_tvs(target) print "Existing target truth values:", map(str, tvs) #if len(tvs): # raise NotImplementedError('Cannot revise multiple TruthValues, so no point in doing this search') #log.info(format_log('bc', target)) #self.bc_later = OrderedSet([target]) self.results = [] self.target = target # Have a sentinel application at the top self.root = T('WIN') dummy_app = rules.Rule(self.root, [target], name='producing target') # is this line necessary? self.rules.append(dummy_app) #self.bc_later = OrderedSet([dummy_app]) self.bc_later.append(dummy_app) self.add_app_to_pd(dummy_app) # viz - visualize the root self.viz.outputTarget(target, None, 0, 'TARGET') steps_so_far = 0 start = time() import pdb pdb.set_trace() while self.bc_later and steps_so_far < nsteps and ( nresults == 0 or len(self.results) < nresults): if self.use_fc: self.fc_step() else: self.bc_step() steps_so_far += 1 #msg = '%s goals expanded, %s remaining, %s Proof DAG Nodes' % (len(self.bc_before), len(self.bc_later), len(self.pd)) msg = 'done %s steps, %s goals remaining, %s Proof DAG Nodes' % ( steps_so_far, len(self.bc_later), len(self.pd)) #log.info(format_log(msg)) #log.info(format_log('time taken', time() - start)) #log.info(format_log('PD:')) #for pdn in self.pd: # log.info(format_log(len(pdn.args),str(pdn))) # Always print it at the end, so you can easily check the (combinatorial) efficiency of all tests after a change print msg for res in self.results: print 'Inference trail:' trail = self.trail(res) self.print_tree(trail) self.update_global_stats(trail) #print 'Action plan (if applicable):' #print self.extract_plan(trail) # for res in self.results: # self.viz_proof_tree(self.trail(res)) ret = [] for tr in self.results: # Create the Atom (before this point it's just a Tree) atom = atom_from_tree(tr, self.space) # haxx atom.tv = self.get_tvs(tr)[0] #ret.append(atom.h) ret.append(atom) # pprint(self.num_app_pdns_per_level) # pprint(self.num_uses_per_rule_per_level) # # pprint(Chainer.successful_num_app_pdns_per_level) # pprint(Chainer.successful_num_uses_per_rule_per_level) return ret except Exception, e: import traceback, pdb #pdb.set_trace() print traceback.format_exc(10) # Start the post-mortem debugger #pdb.pm() return []