root / logic / trunk / src / logics / conditions.c @ 84
History | View | Annotate | Download (3.91 KB)
1 |
#include "conditions.h" |
---|---|
2 |
#include <stdio.h> |
3 |
#include <math.h> |
4 |
|
5 |
int evaluateAdditionalConditions(cond_lst *cl, device *dev)
|
6 |
{ |
7 |
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 |
{ |
20 |
if(!cl->sub_lst->sub_lst) //leaf |
21 |
{ |
22 |
//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 |
} |
42 |
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 |
} |
59 |
return result;
|
60 |
} |
61 |
|
62 |
// TODO check this one again
|
63 |
float getNewWeightedMeasurement(float oldval, float newval, float alpha) |
64 |
{ |
65 |
return oldval*alpha+newval*(1-alpha); |
66 |
} |
67 |
|
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 |
|
73 |
if(func == FUNCTION_STATIC)
|
74 |
{ |
75 |
if(*val>o) // right |
76 |
{ |
77 |
pwr=100;
|
78 |
} |
79 |
else //left |
80 |
{ |
81 |
pwr=0;
|
82 |
} |
83 |
} |
84 |
else if(func == FUNCTION_LINEAR) |
85 |
{ |
86 |
if(*val>o) // right |
87 |
{ |
88 |
if(*val >= a)
|
89 |
pwr=100;
|
90 |
else
|
91 |
pwr=(int)((*val-o)/(b-o)*100); |
92 |
} |
93 |
else //left |
94 |
{ |
95 |
if(*val <= a)
|
96 |
pwr=0;
|
97 |
else
|
98 |
pwr=(int)((1-(*val-a)/(o-a))*100); |
99 |
} |
100 |
} |
101 |
else if(func == FUNCTION_LOGARITHMIC) //TODO implement log func |
102 |
{ |
103 |
if(*val>o) // right |
104 |
{ |
105 |
pwr=(int)((*val-o)/(b-o)*100); |
106 |
} |
107 |
else //left |
108 |
{ |
109 |
pwr=(int)((1-(*val-a)/(o-a))*100); |
110 |
} |
111 |
} |
112 |
else if(func == FUNCTION_QUADRATIC) //implement square func |
113 |
{ |
114 |
if(*val>o) // right |
115 |
{ |
116 |
pwr=(int)((*val-o)/(b-o)*100); |
117 |
} |
118 |
else //left |
119 |
{ |
120 |
pwr=(int)((1-(*val-a)/(o-a))*100); |
121 |
} |
122 |
} |
123 |
else
|
124 |
{ |
125 |
pwr=-1;
|
126 |
} |
127 |
|
128 |
if(pwr>100) |
129 |
pwr=100;
|
130 |
|
131 |
return pwr;
|
132 |
} |
133 |
/*TODO morda dodaj preverbo pravilnosti conditionov (kot podatkov v strukturi)*/
|