tag:blogger.com,1999:blog-72378508392779869822024-02-19T18:36:28.680-08:00codes/scriptsvergelhttp://www.blogger.com/profile/03369295300635181590noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-7237850839277986982.post-3631328630760794782020-07-27T23:43:00.002-07:002020-07-28T04:43:51.501-07:00Switching to JavaScript- All variables are untype. Use 'let' to to declare variables.<br />
<div>
- Array variables are reference. Use 'const' to avoid unintended referencing array variables.</div>
<div>
- Beware of methods that mutates its own object.</div>
<div>
- reverseary</div>
<div>
- JavaScript object is diction or map.</div>
<div>
<h4>
Equality comparison</h4>
The abstract equality comparison (==) will perform a type conversion and compare the values. While the strict equality comparison (===) compares the values without type conversion. So, the triple equal signs are often use for value comparison.<br />
<br />
<h4>
Mutating methods</h4>
These are methods that should not be chained.<br />
<br />
<ul>
<li>push</li>
<li>pop</li>
<li>shift</li>
<li>unshift</li>
<li>reverse</li>
</ul>
</div>
vergelhttp://www.blogger.com/profile/03369295300635181590noreply@blogger.com0tag:blogger.com,1999:blog-7237850839277986982.post-68151098127726747992015-03-07T00:07:00.002-08:002015-03-07T04:03:25.683-08:00How to get the LHS/condition/constraint of Drool's rules file?Below is the codes to extract rule information from drl file. Although Drools was not designed to parse drl files, there are special cases that I need get the LHS of all rules. In my case, these are unit testing rules files and converting rules file to other format.
<br />
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;">KieServices kieServices = KieServices.Factory.get();
KieResources kieResources = kieServices.getResources();
Resource resource = kieResources.newClassPathResource("Simple.drl");
DrlParser parser = new DrlParser();
PackageDescr packageDescr = parser.parse(resource);
List<ruledescr> rules = packageDescr.getRules();
for (RuleDescr rule : rules) {
if (rule.isRule()) {
for (BaseDescr desc : rule.getLhs().getDescrs()) {
PatternDescr p = (PatternDescr) desc;
System.out.println("id = " + p.getIdentifier()
+ ", ObjectType = " + p.getObjectType()
+ ", Constraint = " + p.getConstraint().getDescrs());
}
}
}</ruledescr></code>
</pre>
Simple.drl file:
<br />
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;">import com.bunchofcodes.Drools.ExtractRules.Message;
rule "Hello World"
when
m : Message( status == Message.HELLO )
then
System.out.println("Hello");
end
rule "GoodBye"
when
m : Message( status == Message.GOODBYE )
then
System.out.println("Good bye");
end</code>
</pre>
Here is the output:
<br />
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;">id = m, ObjectType = Message, Constraint = [status == Message.HELLO]
id = m, ObjectType = Message, Constraint = [status == Message.GOODBYE]</code>
</pre>
vergelhttp://www.blogger.com/profile/03369295300635181590noreply@blogger.com0tag:blogger.com,1999:blog-7237850839277986982.post-91812998213863384042014-02-03T19:22:00.003-08:002017-01-21T18:48:12.444-08:00C++ Rants<br />
<ol>
<li>Compiler warning: "warning: this decimal constant is unsigned only in ISO C90" on assigning max literal constants to variables:
<br />
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> unsigned int myInt1 = 4294967295;
int myInt2 = -4294967295;
</code></pre>
<br />
Solution: Add literal suffix u for unsigned int, l for long and ul for unsigned long. There is no literal suffix for signed int, so there is still a warning for signed int.<br />
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> unsigned int myInt1 = 4294967295u;
</code></pre>
<br />
</li>
<li>Literal string is considered as boolean. Codes below will print "bool value: 1".
<pre style="background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_zQSBfGjZA4gsup97TV0kgpftFuHyrMekIwPccxabYhT83gqSTP0MAef_P7MwQQ-VdONeFusErzsW7MdLET5u800PYLAjQu3cnrLG1dBn0no8Tpcnr9weA3jfwhEla3luYNMyro-fCors/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> #include <iostream>
using namespace std;
void myPrint(string value) {
cout << "string value: " << value << endl;
}
void myPrint(bool value) {
cout << "bool value: " << value << endl;
}
int main() {
myPrint("hello");
return 0;
}
</code></pre>
</li>
</ol>
vergelhttp://www.blogger.com/profile/03369295300635181590noreply@blogger.com0tag:blogger.com,1999:blog-7237850839277986982.post-86720377816882290162013-06-10T20:52:00.001-07:002017-01-21T19:00:18.740-08:00Commonly use obscure git commands<ul>
<li>List files of a commit, ARG is commit hash</li>
<ul>
<li>git diff-tree --no-commit-id --name-only -r ARG</li>
</ul>
</ul>
<ul>
</ul>
vergelhttp://www.blogger.com/profile/03369295300635181590noreply@blogger.com0tag:blogger.com,1999:blog-7237850839277986982.post-42191040845102684002012-11-26T05:57:00.000-08:002013-01-07T14:16:57.085-08:00Linux for my home use<div>
Its been several months that I have been using Linux on my old home computer. I am using it for surfing the internet, reading emails, listening to online radio and playing 720p mkv.</div>
<div>
<br /></div>
<div>
Below is my installation flow for CentOS 6.3.</div>
<ol>
<li>Download and install CentOS 6.3 Desktop</li>
<li>Update CentOS:</li>
yum update
<li>Add EPEL repo (<a href="http://fedoraproject.org/wiki/EPEL">http://fedoraproject.org/wiki/EPEL</a>)</li>
<li>Add RPMForge repo (<a href="http://wiki.centos.org/AdditionalResources/Repositories/RPMForge">http://wiki.centos.org/AdditionalResources/Repositories/RPMForge</a>)
</li>
<li>Install additional packages:</li>
yum install mplayer smplayer gconf-editor unrar
<li>Install Google Chrome (<a href="http://www.google.com/chrome">http://www.google.com/chrome</a>)</li>
<li>Install Skype (<a href="http://wiki.centos.org/HowTos/Skype">http://wiki.centos.org/HowTos/Skype</a>)</li>
<ul>
<li>If there is an audio playback problem, install pulseaudio-libs.i686.</li>
</ul>
<li>Download wallpapers and setup background xml file</li>
<li>Install compiz-fusion:</li>
yum install compiz-fusion-extras compiz-manager compiz-fusion-extras libcompizconfig ccsm compiz-gnome compiz-fusion-extras-gnome fusion-icon
<li>Set compiz as window manager:</li>
<ol type="a">
<li>Run gconf-editor and set /desktop/gnome/session/required_components/windowmanager to compiz-gtk
</li>
<li>Edit /usr/bin/compiz-gtk, change the compiz arguments to:</li>
exec compiz --loose-binding --ignore-desktop-hints gnomecompat ccp $@
</ol>
<li>Install Cairo-dock 3.1.x (<a href="http://glx-dock.org/">http://glx-dock.org/</a>)</li>
</ol>
<div>
<div>
Problems that I have encountered:</div>
<div>
<ul>
<li>I have accidentally corrupted my Linux partition because of a typo error in running mkntfs. Be very careful in running commands under root.</li>
<li>I have to build and install proprietary drivers every time I updated Linux kernel.</li>
<li>Linux Flash is slow.</li>
<li>MTP is slow and unstable.</li>
<li>Linux radeon driver does not support my ATI graphics card and proprietary driver is slow.</li>
</ul>
<div>
<br /></div>
<div>
</div>
</div>
</div>
vergelhttp://www.blogger.com/profile/03369295300635181590noreply@blogger.com1tag:blogger.com,1999:blog-7237850839277986982.post-65960436769939826962012-06-23T08:01:00.001-07:002012-06-24T22:12:28.534-07:00How to setup init script?<br />
<span style="background-color: white;">A typical init script provides start, stop, restart, and status service to daemon program.</span><br />
<br />
The <i>start service</i> will run the daemon program and create a lock file in /var/lock/subsys. This lock file will <span style="background-color: white;">be use by status service.</span><br />
<br />
<br />
To avoid mulitple instance of the deamon program, create a pid file so that the daemon function can check the process id. The deamon function will execute the daemon program if there is no pid file or there is pid file but has invalid process id. Its up to daemon program or init script to create the pid file.<br />
<br />
<span style="font-size: small;"><span style="font-family: "Courier New",Courier,monospace;">start() {</span><br style="font-family: "Courier New",Courier,monospace;" /><span style="font-family: "Courier New",Courier,monospace;"> echo -n $"Starting $PROG_NAME: "</span><br style="font-family: "Courier New",Courier,monospace;" /><br style="font-family: "Courier New",Courier,monospace;" /><span style="font-family: "Courier New",Courier,monospace;"> # start daemon</span><br style="font-family: "Courier New",Courier,monospace;" /><span style="font-family: "Courier New",Courier,monospace;"> daemon $DAEMON</span><br style="font-family: "Courier New",Courier,monospace;" /><span style="font-family: "Courier New",Courier,monospace;"> RETVAL=$?</span><br style="font-family: "Courier New",Courier,monospace;" /><span style="font-family: "Courier New",Courier,monospace;"> echo</span><br style="font-family: "Courier New",Courier,monospace;" /><span style="font-family: "Courier New",Courier,monospace;"> [ $RETVAL -eq 0 ] && touch $PROG_LOCK_FILE</span><br style="font-family: "Courier New",Courier,monospace;" /><span style="font-family: "Courier New",Courier,monospace;"> return $RETVAL</span></span><br />
<span style="font-family: "Courier New",Courier,monospace; font-size: small;">} </span><br />
<br />
The <i>stop service</i> will kill the daemon program, delete the lock file and pid file. killproc function will delete the pid file.<br />
<br />
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;">stop() {<br /> # stop daemon<br /> echo -n $"Stopping $PROG_NAME: "<br /> killproc $DAEMON<br /> RETVAL=$?<br /> echo<br /> [ $RETVAL = 0 ] && rm -f $PROG_LOCK_FILE</span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"> return $RETVAL<br />} </span></div>
<br />
The <i>restart service</i> basically calls stop and start services.<br />
<span style="font-size: small;"><br /></span><br />
<span style="font-family: "Courier New",Courier,monospace; font-size: small;"> restart)<br /> stop<br /> start<br /> ;;</span><br />
<br />
The <i>status service</i> will tell if the daemon is running or stopped. This depends on the lock file and process id of the daemon program. The process id is checked using pid file and using the pidof.<br />
<br />
<span style="font-size: small;"><b style="background-color: white; font-family: "Courier New",Courier,monospace;"> </b><span style="background-color: white; font-family: "Courier New",Courier,monospace;"> status)<br /> status $DAEMON<br /> RETVAL=$?<br /> ;;</span></span><br />
<span style="font-size: small;"><span style="background-color: white; font-family: "Courier New",Courier,monospace;"> </span></span><b style="background-color: white;"><br /></b><br />
About pidof:<br />
<ul>
<li><span style="background-color: white;">if the daemon program is a script that is using env to call the interpreter (e.g. #!/bin/env ruby, #!/bin/env python), the pidof will fail to return process id of the script.</span></li>
</ul>
<b style="background-color: white;"></b><br />
<b>To setup the daemon program to run as user account:</b><br />
<ol>
<li><span style="background-color: white;">specify the user name in daemon, status and killproc functions</span></li>
<li><span style="background-color: white;">if the pid file is created by the daemon program, specify the location of pid file</span></li>
</ol>
<br />
<b style="background-color: white;">Complete init script:</b><br />
<br />
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;">#!/bin/sh<br />#<br /># "$Id: mydaemon,v 1.10 2012/06/19 10:10:10 $"<br />#<br /># Startup/shutdown script for the mydaemon.<br />#<br /># Linux chkconfig stuff:<br />#<br /># chkconfig: 2345 99 00<br /># description: Startup/shutdown script for the mydaemon.<br /><br />. /etc/rc.d/init.d/functions<br /><br />export DISPLAY=:0<br /><br />DAEMON=/path/to/mydaemond<br />PROG_NAME=mydaemon<br />PROG_LOCK_FILE=/var/lock/subsys/mydaemond<br />RETVAL=0</span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><br /></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;">start() {<br /> echo -n $"Starting $PROG_NAME: "<br /><br /> # start daemon<br /> daemon $DAEMON<br /> RETVAL=$?<br /> echo<br /> [ $RETVAL -eq 0 ] && touch $PROG_LOCK_FILE<br /> return $RETVAL<br />} <br /><br />stop() {<br /> # stop daemon<br /> echo -n $"Stopping $PROG_NAME: "<br /> killproc $DAEMON<br /> RETVAL=$?<br /> echo<br /> [ $RETVAL = 0 ] && rm -f $PROG_LOCK_FILE<br /> return $RETVAL<br />} <br /><br />case "$1" in<br /> start)<br /> start<br /> ;;<br /> stop)<br /> stop<br /> ;;<br /> restart)<br /> stop<br /> start<br /> ;;<br /> status)<br /> status $DAEMON<br /> RETVAL=$?<br /> ;;<br /> *)<br /> echo $"Usage: $0 {start|stop|restart|status}"<br /> exit 2<br />esac</span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><br /></span></div>
<span style="font-family: "Courier New",Courier,monospace; font-size: small;">exit $RETVAL</span>vergelhttp://www.blogger.com/profile/03369295300635181590noreply@blogger.com0