def test_resolver(self): def myresolver(model=None, component=None, context=None, resolver_args=None): """ new resolver function """ if resolver_args == None: ev_filter = None else: ev_filter = resolver_args.get('evidence_filter') component.evidencelist = component.filterevidence( model=model, context=context, resolver_args=resolver_args) if len(component.evidencelist) > 0: component.value = component.evidencelist[-1]['value'] return component self.um.resolverlist["myresolver"] = myresolver ev = client.Evidence(evidence_type="explicit", value="Alice") self.um.tell(context=["test"], componentid='firstname', evidence=ev) ev = client.Evidence(evidence_type="explicit", value="Smith") reslist = self.um.ask(context=["test"], view='fullname', resolver={ "resolver": "myresolver", "evidence_filter": "all" }) r = [r.value for r in reslist] rc = [u'Alice', u'Smith'] self.assertItemsEqual(r, rc)
def test_subscribe(self): # create a piece of evidence with Alice as value ev = client.Evidence(evidence_type="explicit", value="Alice") self.um.tell(context=["test"], componentid='firstname', evidence=ev) res = self.um.ask(context=["test"], view=['firstname']) self.assertEqual(res[0].value, u'Alice') ev = client.Evidence(evidence_type="explicit", value="Smith") self.um.tell(context=["test"], componentid='lastname', evidence=ev) ev = client.Evidence(evidence_type="explicit", value="female") self.um.tell(context=["test"], componentid='gender', evidence=ev) appdetails = self.um.registerapp(app="MySubscription", desc="My Subscription", password="******") self.assertEqual(appdetails['description'], u'My Subscription') apps = self.um.listapps() self.assertIn(u'MySubscription', apps.keys()) self.um.setpermission(context=["test"], app="MySubscription", permissions={'ask':True, 'tell':True}) sub = """<jamesuther/test/gender> ~ '.*' : TELL jamesuther/test/firstname, explicit:<jamesuther/Personal/firstname>""" token = self.um.subscribe(context=["test"], view=['gender'], subscription={'user':'******', 'password':'******', 'statement':sub}) ev = client.Evidence(evidence_type="explicit", value="male") self.um.tell(context=["test"], componentid='gender', evidence=ev) # subscription should have fired. firstname should now be James res = self.um.ask(context=["test"], view=['firstname']) self.assertEqual(res[0].value, u'James') self.um.delete_sub(context=['test'], componentid = 'gender', subname = token)
def test_ask_comboview(self): ev = client.Evidence(evidence_type="explicit", value="Alice") # tell this as user alice's first name self.um.tell(context=["test"], componentid='firstname', evidence=ev) ev = client.Evidence(evidence_type="explicit", value="Smith") self.um.tell(context=["test"], componentid='lastname', evidence=ev) res = self.um.ask(context=["test"], view=['firstname', 'lastname']) self.assertEqual(res[0].value, u'Alice') self.assertEqual(res[1].value, u'Smith')
def test_ask_fullname_resolver(self): ev = client.Evidence(evidence_type="explicit", value="Alice") # tell this as user alice's first name self.um.tell(context=["test"], componentid='firstname', evidence=ev) ev = client.Evidence(evidence_type="explicit", value="Smith") self.um.tell(context=["test"], componentid='lastname', evidence=ev) res = self.um.ask(context=["test"], view='fullname', resolver={'evidence_filter': "last1"}) self.assertEqual(res[0].value, u'Alice') self.assertEqual(res[1].value, u'Smith')
def test_connect(self): appdetails = self.um.registerapp(app="MyHealth", desc="My Health Manager", password="******") self.assertEqual(appdetails['description'], u'My Health Manager') apps = self.um.listapps() self.assertIn(u'MyHealth', apps.keys()) self.um.setpermission(context=["test"], app="MyHealth", permissions={ 'ask': True, 'tell': False }) perms = self.um.getpermission(context=["test"], app="MyHealth") self.assertEquals(perms, {'ask': True, 'tell': False}) cli = app_client.Model(self.server_uri, model='jamesuther', app='MyHealth', password='******') # create a piece of evidence with Alice as value ev = client.Evidence(evidence_type="explicit", value="Alice") # tell this as user alice's first name self.um.tell(context=["test"], componentid='firstname', evidence=ev) res = cli.ask(context=["test"], view=['firstname']) self.assertEqual(res[0].value, u'Alice') ev = client.Evidence(evidence_type="explicit", value="Smith") # tell not yet enabled with self.assertRaises(Exception) as e: cli.tell(context=["test"], componentid='lastname', evidence=ev) self.um.setpermission(context=["test"], app="MyHealth", permissions={ 'ask': True, 'tell': True }) cli.tell(context=["test"], componentid='lastname', evidence=ev) # works now res = self.um.ask(context=['test'], view=['lastname']) self.assertEquals(res[0].value, u'Smith') self.um.deleteapp(app="MyHealth") apps = self.um.listapps() self.assertNotIn(u'MyHealth', apps) with self.assertRaises(Exception) as e: res = cli.ask(context=["test"], view=['firstname'])
def test_ask_tell(self): # create a piece of evidence with Alice as value ev = client.Evidence(evidence_type="explicit", value="Alice") # tell this as user alice's first name self.um.tell(context=["test"], componentid='firstname', evidence=ev) res = self.um.ask(context=["test"], view=['firstname']) self.assertEqual(res[0].value, u'Alice') ev = client.Evidence(evidence_type="explicit", value="Smith") self.um.tell(context=["test"], componentid='lastname', evidence=ev) ev = client.Evidence(evidence_type="explicit", value="female") self.um.tell(context=["test"], componentid='gender', evidence=ev)
def send(self, v, t): ev = client.Evidence(source='activity_monitor', evidence_type="explicit", value=v, time=t) self.q.put(ev) logging.debug('put %s' % (ev))
def test_export(self): ev = client.Evidence(evidence_type="explicit", value="Alice") # tell this as user alice's first name self.um.tell(context=["test"], componentid='firstname', evidence=ev) m = self.um.export_model(context=['test'], resolver={'evidence_filter': 'all'}) tempfile_path = tempfile.mkstemp()[1] f = open(tempfile_path, 'w') f.write(m) f.close() cobj = client.Context(Identifier='test-import', Description='a test component') t = self.um.mkcontext(context=[], contextobj=cobj) modeljson = open(tempfile_path, 'r').read() modeljson = json.loads(modeljson) self.assertEquals(modeljson['components']['firstname']['value'], 'Alice') self.um.import_model(context=['test-import'], partial_model=modeljson) reslist = self.um.ask(context=['test-import', 'test'], view='fullname') r = [r.value for r in reslist] rc = [u'Alice', u'Smith'] self.assertItemsEqual(r, rc)
def log_me(self, item): if cherrypy.session.get('um') == None: raise cherrypy.HTTPError(400, 'Log in first.') um = cherrypy.session.get('um') ev = client.Evidence(source='llum-log', evidence_type="explicit", value=item, time=time.time()) um.tell(context=['Apps','Logging'], componentid='logged_items', evidence=ev) raise cherrypy.HTTPRedirect('/')
def do_tell(self, line): """tell component_name will prompt for component value etc""" line = line.split() if len(line) != 1: print "usage: tell component" return compname = line[0] val = raw_input("Value? ") print "Evidence type:" for et in client.Evidence.EvidenceTypes: print client.Evidence.EvidenceTypes.index(et), et etindex = raw_input("Index? [0]") if etindex == '': etindex = 0 else: etindex = int(etindex) if (etindex < 0) or (etindex > len(client.Evidence.EvidenceTypes) - 1): print "Index out of range" return etype = client.Evidence.EvidenceTypes[etindex] source = self.username flags = [] while True: flag = raw_input("Evidence flag? (CR for none)") if flag == '': break flags.append(flag) print "Tell value=%s, type=%s, flags=%s, source=%s, context=%s, component=%s " % ( val, etype, flags, source, self.context, compname) ok = raw_input("Ok?[N] ") if ok != 'Y': return ev = client.Evidence(source=source, evidence_type=etype, flags=flags, value=val) try: self.um.tell(context=self.context, componentid=compname, evidence=ev) except ValueError, e: print "tell failed: %s" % (e)
def test_make_del_age(self): cobj = client.Component(Identifier="age", component_type="attribute", Description="age", goals=[['Personal', 'Health', 'weight']], value_type="number") res = self.um.mkcomponent(context=["test"], componentobj=cobj) ev = client.Evidence(evidence_type="explicit", value=17) self.um.tell(context=["test"], componentid='age', evidence=ev) reslist = self.um.ask(context=["test"], view=['age'], resolver={'evidence_filter': "all"}) self.assertEqual(reslist[0].value, 17) self.um.delcomponent(context=["test"], componentid="age") info = self.um.ask(context=["test"], showcontexts=True) (cobjlist, contexts, theviews, thesubs) = info self.assertNotIn(u'age', cobjlist)
def log_me(self): if self.request.method != 'POST': self.abort(400, detail="POST only") session = get_current_session() if session.get('connection') == None: self.abort(400, detail='Log in first.') logging.debug('logme %s', self.request.get('item')) connection = pickle.loads(session.get('connection')) um = client.Access( connection=connection, http=httplib2.Http(disable_ssl_certificate_validation=True), test=False) item = self.request.get('item') ev = client.Evidence(source='llum-log', evidence_type="explicit", value=item, time=time.time()) um.tell(context=['Apps', 'Logging'], componentid='logged_items', evidence=ev) self.response.write(item)