Recommend this page to a friend! |
Classes of Jorge Castro | Chaos Machine One | README.md | Download |
|
DownloadChaosMachineOne for PHPA controlled random generator data for PHP. It doesn't chart visually, the objective is to generate values to store into the database (mysql)
What is the objective?Sometimes we want to generate fake values for the database that are controlled and consistent. So, this library tries to create an ordered chaos. Let's say the next exercise. We want to generate random values for a new system (sales) If we generate random values, the chart would look like > ->gen('when _index<200 then idtable.value=random(-10,10,0.2)') Why is it so random? it is because they are random values (doh). So, they are right but they don't looks real because there is not a trend or a natural flow of information, it is just static noise. Then, let's generate the same value with a sine (for example, let's say that there is a cycle of sales) > ->gen('when _index<200 then idtable.add=sin(0,0,10,30)') The chart has a trend but it is too predictable. So, let's add all factors. > ->gen('when _index<200 then idtable.value=random(-10,10,0.2) and idtable.add=sin(0,0,10,30)') While this chart is far from real, but it is not TOO RANDOM and it has a trend. fields> field($name,$type,$special='database',$initValue=0,$min=PHP_INT_MIN,$max=PHP_INT_MAX) fields are our values. They could be numeric, date and string.
Examples: > "set field.value=20" // it sets the value of the field to 20 > "set field.speed=3" // it sets the speed of the field by 3. The field increases the value every cycle by 3 > "set field.accel=1" // it sets the acceleration of the field by 1. The field increases the speed every cycle by 1 > "set field.value=30 and field.speed=1" // the value is always 30, no matter the speed
genIt generates a value using the Minilang syntaxis. The syntaxis is as follow:
minilang| Variable | Explanation | Example | |----------------------------------------------------|-----------------------------------------------------|--------------------------------------| | $var | it is php global variable. | $var="hi" | | field | it is an minilang variable. | field | | field.value it is the value of a column (if any) | field.value="hi" | | | _index | indicates the current index (current number of row) | _index<200 | | 20 , 20.50 | its a fixed value | 20,50.3 | | "text",'text' | it is fixed string value | "text",'text' | | fn() | its a function | myfunction(value), some.myfunction() | Reserved variables| Reserved word | Explanation | |---------------|------------------------------------------------------------------------------| | null | null value | | false | false value | | true | true value | | on | 1 | | off | 0 | | undef | -1 (for undefined) | | flip | (special value). It inverts a value ON<->OFF<br>Used as value.flip | | now | returns the current timestamp (integer) | | timer | returns the current timestamp (integer) | | interval | returns the interval (in seconds) between the last change and now | | fullinterval | returns the interval (in seconds) between the start of the process and now | | _index | returns the current index (the current counter of row) | | always | reserved word. It is used as "when always" | Examples: myvar = is a variable > when always set myvar.value=null > when _index<20 set myvar.value=false Limits. * Parenthesis are now allowed (unless it is defines a function). > a1=(20+30) is not allowed however a1=20 , a1+30 is allowed * The syntax is divided in two parts, one of the logic (when) and other is the set (then) * Conditions are evaluated in order. If one condition is meet, then it stop other evaluations. > when a1=1 then b1=1 > when a1<10 then b1=10 // if a1=1 then this expression is not evaluated. LogicIt sets the condition(s) separated by and or or > It is not allowed to use more than operator for logic. a1=20+30 is not allowed. Range Functions (numbers)Functions that generates a range of values ramp($fromX, $toX, $fromY, $toY)It generates a ramp values (lineal values)
log($startX,$startY,$scale=1)It generates log values > ->gen('when _index<200 then idtable.value=log(0,0,100)') exp($startX,$startY,$scale=1)It generates exponential values. The scale is for division of Y > ->gen('when _index<200 then idtable.value=exp(0,0,10)') sin($startX,$startY,$speed=1,$scale=1)It generates a sinuzoid values. The angle is calculated with the current index x the speed (in degree) > ->gen('when _index<200 then idtable.value=sin(0,0,1,1)') > ->gen('when _index<200 then idtable.value=sin(0,0,10,1)') atan($centerX,$startY,$speed=1,$scale=1)It generates arc-tangent values
parabola($centerX,$startY,$scaleA=1,$scaleB=1,$scale=1)It generates a parabola. It is possible to invert the parabola by changing the scaleA by negative > ->gen('when _index<200 then idtable.value=parabola(50,0,1,1,1)') > ->gen('when _index<200 then idtable.value=parabola(50,0,-1,1,1)') > ->gen('when _index<200 then idtable.value=parabola(50,2500,-1,1,1)') bell($centerX, $startY, $sigma=1, $scaleY=1)It generates a bell values, sigma is the "wide" of the bell. > ->gen('when _index<=360 then idtable.value=bell(50,0,30,100)') > ->gen('when _index<=360 then idtable.value=bell(50,0,30,100)') Fixed functions (numbers)Functions that generates a single value randomprop(...$args)It generates a random value by using different proportions or probabilities. > randomprop(1,2,3,30,50,20)
> ->gen('when _index<200 then idtable.value=randomprop(1,2,3,30,50,20)') > ->gen('when always then idtable.value=randomprop(idtable,null,1,1)) // there is a 50% chance the value is null random($from,$to,$jump=1,$prob0=null,$prob1=null,$prob2=null)It generates a random value from $from to $to.
> ->gen('when _index<200 then idtable.value=random(-10,10,0.2)') Optionally, you could add a probability for each segment. > random(0,100,1,10,20,70)
> idtable.value=random(0,200,1,80,10,10) > idtable.value=random(0,200,1,10,80,10) > idtable.value=random(0,200,1,10,10,80) field.speed=xxxxIt sets the speed of the field. If the field has speed then it's value it's increased for each iteraction. field.accel=xxxxIt sets the acceleration of a field. If the field has acceleration then it's speed it's increased for each iteraction. field.value=xxxxIt sets the value of a field field.getvalueIt returns the value of a field field.valueabsIt transform the value of a field to absolute (always positive) field.day , field.month , field.year, field.hour, field.minute, field.weekdayIt returns the current part of the date (day,month,year,hour,minute and weekday). > It is used for field of the type datetime. > It is not for set, no matter the value. > Weekday = 1= monday, 7=sunday field.stop=xxxxIt sets or returns the value of a field, it also marks the speed and acceleration to zero. field.add=xxxxIt adds a value to a field. If the field is of the type datetime, then you could add an "hour","minute" and "day"
field.concat=xxxxIt concatenates a value to a field.
field.skip=xxxIt skips a value to the next value. It is used for date. | value | |--------------| | hour | | day | | month | | monday | | tuesday | | wednesday | | thursday | | friday | | saturday | | sunday |
Arrays and texts->setArray('arrayname',[])It sets an array. If the array is associative, then the value is the probability of selection.
->setFormat('formatName',[])It sets a format (template) to merge different arrays. The arrays are marked as {{name-of-the-array}}. If the array is not defined then it returns the value of a field. If the array is associative then it returns a value according it's probability.
randomarray("arrayname",'field'=null)it returns a random row inside of the array declared with setArray(). If the array is associative then it returns a value according it's probability. If the array is a list of objects, then it returns the value of the field.
randomformat($nameFormat)It generates a random text using a mixes of format and different arrays.
randomtext($starting,$arrayName,$paragraph,$wordMinimum,$wordMaximum)It generates a random text by using the array with name $arrayName. The text could starts with a default text. If $paragraph is not 0, then it could generates paragraph (line carriage)
randommask($mask,$arrayName='')It generates a text based on a mask
> randommask("##-00 uu ll \0 - oo (a)","lastName") // where lastName is an array File featuresarrayFromFolder()It reads a folder and returns an array of files. The reading is not recursive and it could be filtered by extension. > $filesWithoutExtension=$chaos->arrayFromFolder($localfolder,'jpg',false); Script function destionationArray.copyfilefrom()=originArray.getvalueIt copies a file from a location to another.
version
|