Monday, July 27, 2020

Switching to JavaScript

- All variables are untype. Use 'let' to  to declare variables.
- Array variables are reference. Use 'const' to avoid unintended referencing array variables.
- Beware of methods that mutates its own object.
  - reverseary
- JavaScript object is diction or map.

Equality comparison

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.

Mutating methods

These are methods that should not be chained.

  • push
  • pop
  • shift
  • unshift
  • reverse

Saturday, March 7, 2015

How 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.
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 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());
      }                                                                
   }                                                                    
}
Simple.drl file:
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
Here is the output:
id = m, ObjectType = Message, Constraint = [status == Message.HELLO]
id = m, ObjectType = Message, Constraint = [status == Message.GOODBYE]

Monday, February 3, 2014

C++ Rants


  1. Compiler warning: "warning: this decimal constant is unsigned only in ISO C90" on assigning max literal constants to variables:
     unsigned int myInt1 = 4294967295;  
     int myInt2 = -4294967295;  
    

    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.
     unsigned int myInt1 = 4294967295u;  
    

  2. Literal string is considered as boolean. Codes below will print "bool value: 1".
     #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;  
     }  
    

Monday, June 10, 2013

Commonly use obscure git commands

  • List files of a commit, ARG is commit hash
    • git diff-tree --no-commit-id --name-only -r ARG

Monday, November 26, 2012

Linux for my home use

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.

Below is my installation flow for CentOS 6.3.
  1. Download and install CentOS 6.3 Desktop
  2. Update CentOS:
  3. yum update
  4. Add EPEL repo (http://fedoraproject.org/wiki/EPEL)
  5. Add RPMForge repo (http://wiki.centos.org/AdditionalResources/Repositories/RPMForge)
  6. Install additional packages:
  7. yum install mplayer smplayer gconf-editor unrar
  8. Install Google Chrome (http://www.google.com/chrome)
  9. Install Skype (http://wiki.centos.org/HowTos/Skype)
    • If there is an audio playback problem, install pulseaudio-libs.i686.
  10. Download wallpapers and setup background xml file
  11. Install compiz-fusion:
  12. yum install compiz-fusion-extras compiz-manager compiz-fusion-extras libcompizconfig ccsm compiz-gnome compiz-fusion-extras-gnome fusion-icon
  13. Set compiz as window manager:
    1. Run gconf-editor and set /desktop/gnome/session/required_components/windowmanager to compiz-gtk
    2. Edit /usr/bin/compiz-gtk, change the compiz arguments to:
    3. exec compiz --loose-binding --ignore-desktop-hints gnomecompat ccp $@
  14. Install Cairo-dock 3.1.x (http://glx-dock.org/)
Problems that I have encountered:
  • I have accidentally corrupted my Linux partition because of a typo error in running mkntfs. Be very careful in running commands under root.
  • I have to build and install proprietary drivers every time I updated Linux kernel.
  • Linux Flash is slow.
  • MTP is slow and unstable.
  • Linux radeon driver does not support my ATI graphics card and proprietary driver is slow.

Saturday, June 23, 2012

How to setup init script?


A typical init script provides start, stop, restart, and status service to daemon program.

The start service will run the daemon program and create a lock file in /var/lock/subsys. This lock file will be use by status service.


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.

start() {
        echo -n $"Starting $PROG_NAME: "

        # start daemon
        daemon $DAEMON
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch $PROG_LOCK_FILE
        return $RETVAL



The stop service will kill the daemon program, delete the lock file and pid file. killproc function will delete the pid file.

stop() {
        # stop daemon
        echo -n $"Stopping $PROG_NAME: "
        killproc $DAEMON
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f $PROG_LOCK_FILE
        return $RETVAL

The restart service basically calls stop and start services.


  restart)
        stop
        start
        ;;


The status service 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.

   status)
        status $DAEMON
        RETVAL=$?
        ;;

 

About pidof:
  • 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.

To setup the daemon program to run as user account:
  1. specify the user name in daemon, status and killproc functions
  2. if the pid file is created by the daemon program, specify the location of pid file

Complete init script:

#!/bin/sh
#
# "$Id: mydaemon,v 1.10 2012/06/19 10:10:10 $"
#
#   Startup/shutdown script for the mydaemon.
#
#   Linux chkconfig stuff:
#
#   chkconfig: 2345 99 00
#   description: Startup/shutdown script for the mydaemon.

. /etc/rc.d/init.d/functions

export DISPLAY=:0

DAEMON=/path/to/mydaemond
PROG_NAME=mydaemon
PROG_LOCK_FILE=/var/lock/subsys/mydaemond
RETVAL=0

start() {
        echo -n $"Starting $PROG_NAME: "

        # start daemon
        daemon $DAEMON
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch $PROG_LOCK_FILE
        return $RETVAL
}      

stop() {
        # stop daemon
        echo -n $"Stopping $PROG_NAME: "
        killproc $DAEMON
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f $PROG_LOCK_FILE
        return $RETVAL
}      

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        stop
        start
        ;;
  status)
        status $DAEMON
        RETVAL=$?
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|status}"
        exit 2
esac

exit $RETVAL