Institut de Robòtica i Informàtica Industrial
KRD Group

The CuikSuite Project

variables.c

Go to the documentation of this file.
00001 #include "variables.h"
00002 
00003 #include "error.h"
00004 #include "boolean.h"
00005 #include "defines.h"
00006 
00007 #include <stdlib.h>
00008 #include <string.h>
00009 
00010 
00021 /*
00022  * Init the set of variables.
00023  */
00024 void InitVariables(Tvariables *vs)
00025 {
00026   unsigned int i;
00027 
00028   vs->max_variables=INIT_NUM_VARIABLES;
00029   NEW(vs->vs,vs->max_variables,Tvariable *);
00030   vs->n=0; /*total*/
00031   vs->sys=0; /*system*/
00032   vs->sec=0; /*secondary*/
00033   vs->dum=0; /*dummies*/
00034   vs->car=0; /*cartesian*/
00035 
00036   for(i=0;i<vs->max_variables;i++)
00037     vs->vs[i]=NULL;
00038 }
00039 
00040 void CopyVariables(Tvariables *vs_dst,Tvariables *vs_src)
00041 {
00042   unsigned int i;
00043 
00044   vs_dst->sys=vs_src->sys;
00045   vs_dst->sec=vs_src->sec;
00046   vs_dst->dum=vs_src->dum;
00047   vs_dst->car=vs_src->car;
00048 
00049   vs_dst->n=vs_src->n;
00050 
00051   vs_dst->max_variables=vs_src->max_variables;
00052   NEW(vs_dst->vs,vs_dst->max_variables,Tvariable *);
00053 
00054   for(i=0;i<vs_src->max_variables;i++)
00055     {
00056       if (vs_src->vs[i]!=NULL)
00057         {
00058           NEW(vs_dst->vs[i],1,Tvariable);
00059           CopyVariable(vs_dst->vs[i],vs_src->vs[i]);
00060         }
00061       else
00062         vs_dst->vs[i]=NULL;
00063     }
00064 }
00065 
00066 /*
00067  * Returns the number of variables included in the set 'vs'
00068  */
00069 unsigned int NVariables(Tvariables *vs)
00070 {
00071   return(vs->n);
00072 }
00073 
00074 /*
00075  * Returns the number of cartesian  variables
00076  */
00077 unsigned int GetNumCartesianVariables(Tvariables *vs)
00078 {
00079   return(vs->car);
00080 }
00081 
00082 unsigned int GetNumDummyVariables(Tvariables *vs)
00083 {
00084   return(vs->dum);
00085 }
00086 
00087 /*
00088  * Returns the number of system  variables
00089  */
00090 unsigned int GetNumSystemVariables(Tvariables *vs)
00091 {
00092   return(vs->sys);
00093 }
00094 
00095 /*
00096  * Returns the number of secondary  variables
00097  */
00098 unsigned int GetNumSecondaryVariables(Tvariables *vs)
00099 {
00100   return(vs->sec);
00101 }
00102 
00103 boolean IsSystemVariable(unsigned int n,Tvariables *vs)
00104 {  
00105   return((n<vs->n)&&(GetVariableType(vs->vs[n])==SYSTEM_VAR));
00106 }
00107 
00108 boolean IsSecondaryVariable(unsigned int n,Tvariables *vs)
00109 {  
00110   return((n<vs->n)&&(GetVariableType(vs->vs[n])==SECONDARY_VAR));
00111 }
00112 
00113 boolean IsDummyVariable(unsigned int n,Tvariables *vs)
00114 {
00115   return((n<vs->n)&&(GetVariableType(vs->vs[n])==DUMMY_VAR));
00116 }
00117 
00118 boolean IsCartesianVariable(unsigned int n,Tvariables *vs)
00119 {
00120   return((n<vs->n)&&(GetVariableType(vs->vs[n])==CARTESIAN_VAR));
00121 }
00122 
00123 unsigned int GetVariableTypeN(unsigned int n,Tvariables *vs)
00124 {
00125   if (n>=vs->n)
00126     Error("Unknown variable ID in GetVariableTypeN");
00127 
00128   return(GetVariableType(vs->vs[n]));
00129 }
00130 
00131 /*
00132  * Add a new variable (pointed by 'v') to the set 'vs'.
00133  * If 'v' has the same name of an already existing variable in 'vs' and error is issued (and
00134  * the program stopped).
00135  * Returns the ID assigned to this variable (the position where it is inserted) and not its order
00136  * in the set (first, second,...)
00137  */
00138 unsigned int AddVariable(Tvariable *v,Tvariables *vs)
00139 {
00140   if (GetVariableWithName(GetVariableName(v),vs)!=NULL)
00141     Error("Duplicated variable");
00142   else
00143     {
00144       unsigned int t;
00145       
00146       /*look for a  free position*/
00147       if (vs->n==vs->max_variables)
00148         {
00149           unsigned int i,k;
00150 
00151           k=vs->max_variables;
00152           MEM_DUP(vs->vs,vs->max_variables,Tvariable *);
00153           for(i=k;i<vs->max_variables;i++)
00154             vs->vs[i]=NULL;
00155         }
00156 
00157       NEW(vs->vs[vs->n],1,Tvariable);
00158       CopyVariable(vs->vs[vs->n],v);
00159 
00160       t=GetVariableType(v);
00161 
00162       if (t==SYSTEM_VAR) vs->sys++;
00163       if (t==SECONDARY_VAR) vs->sec++;
00164       if (t==DUMMY_VAR) vs->dum++;
00165       if (t==CARTESIAN_VAR) vs->car++;
00166       vs->n++;
00167     }
00168   return(vs->n-1);
00169 }
00170 
00171 /*
00172  * Returns a pointer to the variable description of the variable with the given 'name'
00173  * If there is no variable with that name NULL is returned.
00174  */
00175 Tvariable *GetVariableWithName(char *name,Tvariables *vs)
00176 {
00177   unsigned int i;
00178 
00179   i=GetVariableID(name,vs);
00180   if (i==NO_UINT)
00181     return(NULL);
00182   else
00183     return(vs->vs[i]);
00184 }
00185 
00186 /*
00187  * Returns a pointer to the variable with n
00188  */ 
00189 Tvariable *GetVariable(unsigned int n,Tvariables *vs)
00190 {
00191   if (n>=vs->n)
00192     Error("Undefined variable referenced in function GetVariable");
00193 
00194   return(vs->vs[n]);    
00195 }
00196 
00197 /*
00198  * Returns the ID of variable with name 'name'.
00199  * returns -1 if there is no variable with this name
00200  */
00201 unsigned int GetVariableID(char *name,Tvariables *vs)
00202 {
00203   boolean found;
00204   unsigned int i;
00205 
00206   found=FALSE;
00207   i=0;
00208   while((!found)&&(i<vs->n))
00209     {
00210       if (strcmp(name,GetVariableName(vs->vs[i]))==0)
00211         found=TRUE;
00212       else
00213         i++;
00214     }
00215   
00216   if (found)
00217     return(i);
00218   else
00219     return(NO_UINT);
00220 }
00221 
00222 void GetVariableNames(char **varNames,Tvariables *vs)
00223 {
00224   unsigned int i;
00225   
00226   for(i=0;i<vs->n;i++)
00227     varNames[i]=GetVariableName(vs->vs[i]);
00228 }
00229 
00230 /*
00231  * Remove a variable from the set.
00232  * WARNING: if you remove a variable from a set used in a
00233  * cuiksystem without removing the variable from all the
00234  * equations (for instance, by fixing it to zero) you
00235  * will get generate an inconsistency on the cuiksystem
00236  */
00237 void RemoveVariable(unsigned int n,Tvariables *vs)
00238 { 
00239   if (n<vs->n)
00240     {
00241       unsigned int i,t;
00242       
00243       t=GetVariableType(vs->vs[n]);
00244       
00245       DeleteVariable(vs->vs[n]);
00246       free(vs->vs[n]);
00247 
00248       for(i=n+1;i<vs->n;i++)
00249         vs->vs[i-1]=vs->vs[i];
00250       vs->vs[vs->n-1]=NULL;
00251 
00252       if (t==SYSTEM_VAR) vs->sys--;
00253       if (t==SECONDARY_VAR) vs->sec--;
00254       if (t==DUMMY_VAR) vs->dum--;
00255       if (t==CARTESIAN_VAR) vs->car--;
00256       vs->n--;
00257     }
00258 }
00259 
00260 /*
00261   Define the initial search space from a set of variables
00262 */
00263 void BoxFromVariables(Tbox *b,Tvariables *vs)
00264 {
00265   unsigned int i,n;
00266 
00267   n=NVariables(vs);
00268   InitBox(n,NULL,b);
00269 
00270   for(i=0;i<n;i++) 
00271     SetBoxInterval(i,GetVariableInterval(GetVariable(i,vs)),b);
00272 }
00273 
00274 /*
00275  * Prints the set of variables 'vs' on the file 'f'
00276  */
00277 void PrintVariables(FILE *f,Tvariables *vs)
00278 {
00279   unsigned int i;
00280   unsigned int t,tn;
00281 
00282   for(i=0;i<vs->n;i++)
00283     {
00284       tn=GetVariableType(GetVariable(i,vs));
00285       if ((i==0)||(t!=tn))
00286         {
00287           switch (tn)
00288             {
00289             case SYSTEM_VAR:
00290               fprintf(f,"\n[SYSTEM VARS]\n");
00291               break;
00292             case SECONDARY_VAR:
00293               fprintf(f,"\n[SECONDARY VARS]\n");
00294               break;
00295             case DUMMY_VAR:
00296               fprintf(f,"\n[DUMMY VARS]\n");
00297               break;
00298             case CARTESIAN_VAR:
00299               fprintf(f,"\n[CARTESIAN VARS]\n");
00300               break;
00301             default:
00302               Error("Unknown variable type in PrintVariables");
00303             }
00304         }
00305       t=tn;
00306       fprintf(f,"   ");PrintVariable(f,vs->vs[i]);
00307     }
00308 }
00309 
00310 /*
00311  * Deletes the set of variables 'vs' (recursively deleting all the included variables).
00312  */
00313 void DeleteVariables(Tvariables *vs)
00314 {
00315   unsigned int i;
00316 
00317   for(i=0;i<vs->n;i++)
00318     {
00319       DeleteVariable(vs->vs[i]);
00320       free(vs->vs[i]);
00321     }
00322   free(vs->vs);
00323   vs->vs=NULL;
00324 }
00325