ceptr
 All Data Structures Files Functions Variables Typedefs Macros Modules Pages
profile_example.h
Go to the documentation of this file.
1 
7 #ifndef _CEPTR_TEST_PROFILE_EXAMPLE_H
8 #define _CEPTR_TEST_PROFILE_EXAMPLE_H
9 
10 #include "../src/ceptr.h"
11 #include "../src/receptor.h"
12 #include "../src/def.h"
13 #include "../src/semtrex.h"
14 #include "spec_utils.h"
15 
16 Receptor *test_profile_receptor;
17 
18 Symbol FIRST_NAME;
19 Symbol LAST_NAME;
20 Structure NAME;
21 Symbol PROFILE_NAME;
22 
23 Symbol STREET_ADDRESS;
24 Symbol CITY;
25 Symbol STATE;
26 Symbol ZIP;
27 Symbol COUNTRY;
28 Structure ADDRESS;
29 Symbol PROFILE_ADDRESS;
30 
31 Symbol PROFILE_EMAIL;
32 Structure PROFILE;
33 Symbol USER_PROFILE;
34 
35 Symbol MAILING_LABEL;
36 
37 void _setupProfileDefs() {
38  Receptor *r = test_profile_receptor = _r_new(G_sem,TEST_RECEPTOR);
39 
40  SY(r,FIRST_NAME,CSTRING);
41  SY(r,LAST_NAME,CSTRING);
42  ST(r,NAME,2,
43  FIRST_NAME,
44  LAST_NAME
45  );
46  SY(r,PROFILE_NAME,NAME);
47 
48  SY(r,STREET_ADDRESS,CSTRING);
49  SY(r,CITY,CSTRING);
50  SY(r,STATE,CSTRING);
51  SY(r,ZIP,CSTRING);
52  SY(r,COUNTRY,CSTRING);
53 
54  ST(r,ADDRESS,5,
55  STREET_ADDRESS,
56  CITY,
57  STATE,
58  ZIP,
59  COUNTRY
60  );
61  SY(r,PROFILE_ADDRESS,ADDRESS);
62 
63  SY(r,PROFILE_EMAIL,CSTRING);
64  ST(r,PROFILE,3,
65  PROFILE_NAME,
66  PROFILE_ADDRESS,
67  PROFILE_EMAIL);
68  SY(r,USER_PROFILE,PROFILE);
69 
70  SY(r,MAILING_LABEL,CSTRING);
71 }
72 
73 void _cleanupProfileDefs() {
74  _r_free(test_profile_receptor);
75 }
76 
77 void testProfileExample() {
78  _setupProfileDefs();
79  T *t = _t_new_root(USER_PROFILE);
80  T *n = _t_newr(t,PROFILE_NAME);
81  T *a = _t_newr(t,PROFILE_ADDRESS);
82  T *e = _t_new_str(t,PROFILE_EMAIL,"test@example.com");
83  _t_new_str(n,FIRST_NAME,"Jane");
84  _t_new_str(n,LAST_NAME,"Smith");
85  _t_new_str(a,STREET_ADDRESS,"126 Main Street");
86  _t_new_str(a,CITY,"Smallville");
87  _t_new_str(a,STATE,"CA");
88  _t_new_str(a,ZIP,"12345");
89  _t_new_str(a,COUNTRY,"USA");
90 
91  SemTable *sem = test_profile_receptor->sem;
92  wjson(sem,t,"user_profile",-1);
93 
94  spec_is_str_equal(t2s(t),"(USER_PROFILE (PROFILE_NAME (FIRST_NAME:Jane) (LAST_NAME:Smith)) (PROFILE_ADDRESS (STREET_ADDRESS:126 Main Street) (CITY:Smallville) (STATE:CA) (ZIP:12345) (COUNTRY:USA)) (PROFILE_EMAIL:test@example.com))");
95 
96  T *signature = __p_make_signature("result",SIGNATURE_SYMBOL,NULL_SYMBOL,
97  "mailing_profile",SIGNATURE_STRUCTURE,CSTRING, // should actually be MAILING_LABEL symbol or something like that
98  "the_int?",SIGNATURE_STRUCTURE,INTEGER,
99  "the_profile",SIGNATURE_STRUCTURE,PROFILE,
100  NULL);
101 
102  T *code = _t_new_root(CONCAT_STR);
103 
104  int pt1[] = {2,1,1,1,TREE_PATH_TERMINATOR};
105  int pt2[] = {2,1,1,2,TREE_PATH_TERMINATOR};
106  int pt3[] = {2,1,2,1,TREE_PATH_TERMINATOR};
107  int pt4[] = {2,1,2,2,TREE_PATH_TERMINATOR};
108  int pt5[] = {2,1,2,3,TREE_PATH_TERMINATOR};
109  int pt6[] = {2,1,2,4,TREE_PATH_TERMINATOR};
110 
111  _t_news(code,RESULT_SYMBOL,MAILING_LABEL);
112  T *c = _t_newr(code,CONCAT_STR);
113  // _t_news(c,RESULT_SYMBOL,TEST_NAME_SYMBOL);
114  _t_new(c,PARAM_REF,pt1,sizeof(int)*5);
115  _t_new_str(c,TEST_STR_SYMBOL," ");
116  _t_new(c,PARAM_REF,pt2,sizeof(int)*5);
117  _t_new_str(code,TEST_STR_SYMBOL,"\\n");
118  _t_new(code,PARAM_REF,pt3,sizeof(int)*5);
119  _t_new_str(code,TEST_STR_SYMBOL,"\\n");
120  c = _t_newr(code,CONCAT_STR);
121  // _t_news(c,RESULT_SYMBOL,TEST_NAME_SYMBOL);
122  _t_new(c,PARAM_REF,pt4,sizeof(int)*5);
123  _t_new_str(c,TEST_STR_SYMBOL,", ");
124  _t_new(c,PARAM_REF,pt5,sizeof(int)*5);
125  _t_new_str(c,TEST_STR_SYMBOL," ");
126  _t_new(c,PARAM_REF,pt6,sizeof(int)*5);
127 
128  // _t_new(code,PARAM_REF,pt2,sizeof(int)*4);
129 
130  Process p = _d_define_process(sem,code,"profileToMailingLabel","given a profile produce a mailing label",signature,NULL,test_profile_receptor->context);
131 
132  T *params = _t_new_root(PARAMS);
133  _t_add(params,t);
134 
135  T *r = _p_make_run_tree(G_sem,p,params,NULL);
136  wjson(sem,r,"profile",0);
137 
138  // spec_is_str_equal(_t2s(&test_profile_defs,r),"");
139 
140  startVisdump("profile");
141  spec_is_equal(_p_reduce(sem,r),noReductionErr);
142  int pt[] = {TREE_PATH_TERMINATOR};
143 
144  _visdump(sem,_t_child(r,1),pt);
145  endVisdump();
146 
147  spec_is_str_equal(t2s(_t_child(r,1)),"(MAILING_LABEL:Jane Smith\\n126 Main Street\\nSmallville, CA 12345)");
148 
149  _t_free(r);
150  _cleanupProfileDefs();
151 }
152 
153 #endif
T * _t_new_root(Symbol symbol)
Definition: tree.c:160
Definition: ceptr_types.h:114
Process _d_define_process(SemTable *sem, T *code, char *name, char *intention, T *signature, T *link, Context c)
Definition: def.c:336
void _r_free(Receptor *r)
Definition: receptor.c:186
T * _t_child(T *t, int i)
Definition: tree.c:1251
Context context
the context this receptor's definition creates
Definition: ceptr_types.h:240
Receptor * _r_new(SemTable *sem, SemanticID r)
Creates a new receptor.
Definition: receptor.c:88
SemTable * sem
pointer back to the genotype table for this receptor's vmhost instance
Definition: ceptr_types.h:242
Error _p_reduce(SemTable *sem, T *rt)
Definition: process.c:1506
void _t_add(T *t, T *c)
Definition: tree.c:261
void _t_free(T *t)
Definition: tree.c:526
T * _p_make_run_tree(SemTable *sem, Process p, T *params, T *sem_map)
Definition: process.c:1974