root / logic / trunk / src / logics / conditions.c @ 61
History | View | Annotate | Download (3.71 KB)
1 | 36 | Janez1 | #include "conditions.h" |
---|---|---|---|
2 | 40 | Janez1 | #include <stdio.h> |
3 | 44 | Janez1 | #include <math.h> |
4 | 36 | Janez1 | |
5 | 40 | Janez1 | int evaluateAdditionalConditions(cond_lst *cl, device *dev)
|
6 | 36 | Janez1 | { |
7 | 40 | Janez1 | int result = -1; |
8 | int local_result = -1; |
||
9 | cond_lst *leaf; |
||
10 | device *testDev; |
||
11 | |||
12 | //printf("dev %s\n",dev->id);
|
||
13 | if(cl->OPERATOR_TYPE == TOP_NODE)
|
||
14 | if(!cl->sub_lst) // no additional conditions |
||
15 | return 1; |
||
16 | else{}
|
||
17 | //printf("Evaluating operator %d\n", cl->OPERATOR_TYPE);
|
||
18 | while(cl->sub_lst)
|
||
19 | 36 | Janez1 | { |
20 | 40 | Janez1 | if(!cl->sub_lst->sub_lst) //leaf |
21 | 36 | Janez1 | { |
22 | 40 | Janez1 | //printf("Found leaf with opr %d\n", cl->sub_lst->OPERATOR_TYPE);
|
23 | //todo glej cez cel sub - nivo
|
||
24 | if(result==-1) //got first leaf element |
||
25 | { |
||
26 | //printf("got FIRST LEAF element with opr %d and device name %s\n", cl->sub_lst->OPERATOR_TYPE, cl->sub_lst->dev);
|
||
27 | if(0==strcmp(cl->sub_lst->dev,"time")) |
||
28 | result = evalOperators(cl->sub_lst->OPERATOR_TYPE, getLocalFormattedTime(), cl->sub_lst->value); |
||
29 | else
|
||
30 | result = evalOperators(cl->sub_lst->OPERATOR_TYPE, (*getDevice(dev, cl->sub_lst->dev)).buf, cl->sub_lst->value); |
||
31 | } |
||
32 | else
|
||
33 | { |
||
34 | //printf("got LEAF element with opr %d\n", cl->sub_lst->OPERATOR_TYPE);
|
||
35 | if(0==strcmp(cl->sub_lst->dev,"time")) |
||
36 | local_result = evalOperators(cl->sub_lst->OPERATOR_TYPE, getLocalFormattedTime(), cl->sub_lst->value); |
||
37 | else
|
||
38 | local_result = evalOperators(cl->sub_lst->OPERATOR_TYPE, (*getDevice(dev, cl->sub_lst->dev)).buf, cl->sub_lst->value); |
||
39 | result = evalOperators(cl->OPERATOR_TYPE, result, local_result); |
||
40 | } |
||
41 | 36 | Janez1 | } |
42 | 40 | Janez1 | else //not leaf |
43 | { |
||
44 | //printf("Found child with opr %d\n", cl->sub_lst->OPERATOR_TYPE);
|
||
45 | if(result==-1) //got first child element |
||
46 | { |
||
47 | //printf("got FIRST CHILD element with opr %d\n", cl->sub_lst->OPERATOR_TYPE);
|
||
48 | result = evaluateAdditionalConditions(cl->sub_lst, dev); |
||
49 | } |
||
50 | else
|
||
51 | { |
||
52 | //printf("got CHILD element with opr %d\n", cl->sub_lst->OPERATOR_TYPE);
|
||
53 | result = evaluateAdditionalConditions(cl->sub_lst, dev); |
||
54 | result = evalOperators(cl->OPERATOR_TYPE, result, local_result); |
||
55 | } |
||
56 | } |
||
57 | cl->sub_lst=cl->sub_lst->nxt_lst; |
||
58 | 36 | Janez1 | } |
59 | 40 | Janez1 | return result;
|
60 | 36 | Janez1 | } |
61 | |||
62 | 40 | Janez1 | // TODO check this one again
|
63 | float getNewWeightedMeasurement(float oldval, float newval, float alpha) |
||
64 | { |
||
65 | return oldval*alpha+newval*(1-alpha); |
||
66 | } |
||
67 | 44 | Janez1 | |
68 | int getPowerPercentage(int a, int b, int o, float *val, int func) |
||
69 | { |
||
70 | int pwr;
|
||
71 | printf( "getPowerPercentage(%d, %d, %d, %.2f, %d)\n",a,b,o,*val,func);
|
||
72 | if(func == FUNCTION_STATIC)
|
||
73 | { |
||
74 | if(*val>o) // right |
||
75 | { |
||
76 | pwr=100;
|
||
77 | } |
||
78 | else //left |
||
79 | { |
||
80 | pwr=0;
|
||
81 | } |
||
82 | } |
||
83 | else if(func == FUNCTION_LINEAR) |
||
84 | { |
||
85 | if(*val>o) // right |
||
86 | { |
||
87 | pwr=(int)((*val-o)/(b-o)*100); |
||
88 | } |
||
89 | else //left |
||
90 | { |
||
91 | pwr=(int)((1-(*val-a)/(o-a))*100); |
||
92 | } |
||
93 | } |
||
94 | else if(func == FUNCTION_LOGARITHMIC) |
||
95 | { |
||
96 | if(*val>o) // right |
||
97 | { |
||
98 | pwr=(int)((*val-o)/(b-o)*100); |
||
99 | } |
||
100 | else //left |
||
101 | { |
||
102 | pwr=(int)((1-(*val-a)/(o-a))*100); |
||
103 | } |
||
104 | } |
||
105 | else if(func == FUNCTION_QUADRATIC) |
||
106 | { |
||
107 | if(*val>o) // right |
||
108 | { |
||
109 | pwr=(int)((*val-o)/(b-o)*100); |
||
110 | } |
||
111 | else //left |
||
112 | { |
||
113 | pwr=(int)((1-(*val-a)/(o-a))*100); |
||
114 | } |
||
115 | } |
||
116 | else
|
||
117 | { |
||
118 | pwr=-1;
|
||
119 | } |
||
120 | |||
121 | if(pwr>100) |
||
122 | pwr=100;
|
||
123 | |||
124 | return pwr;
|
||
125 | } |
||
126 | 36 | Janez1 | /*TODO morda dodaj preverbo pravilnosti conditionov (kot podatkov v strukturi)*/ |