Monday, 10 August 2015

[git] move a folder from one repository to a branch of another repository and still keep commit history

Let’s say I want to move dir_to_move from src_repo to the branch br_target in dest_repo.
# clone a source repository  
git clone url/to/src_repo src_repo 
cd src_repo/
git remote remove origin

# filter the subfolder dir_to_move
git filter-branch --subdirectory-filter dir_to_move -- --all
mkdir dir_to_move
mv * dir_to_move/
git add .
git status
git commit -m "move dir_to_move to another repository" -a
git status

# clone a destination repository
git clone url/to/dest_repo dest_repo
# go to destination repository
cd ../dest_repo
# add a remote repository to the source repository
git remote add remote_src_repo ../src_repo/
# switch to branch target
git checkout br_target 
# pull from remote source repository 
git pull remote_src_repo master
# remove remote repository 
git remote remove remote_src_repo

# remove temporary source repository
rm ../src_repo
​

How to set Java version for Maven on OS X

Check Java Version

I use jEnv to manage different Java versions on OSX. The default is 1.8.
$ java -version
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)
However, the maven still uses Java 1.6.0.

Check Maven Version

$ mvn -v
Apache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1; 2014-12-14T17:29:23+00:00)
Maven home: /Users/ykf_2001/Programs/apache-maven/apache-maven-3.2.5
Java version: 1.6.0_65, vendor: Apple Inc.
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: en_US, platform encoding: MacRoman
OS name: "mac os x", version: "10.10.4", arch: "x86_64", family: "mac"

Set maven to use Java 1.8 temporarily

$ JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/ mvn -v  
Apache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1; 2014-12-14T17:29:23+00:00)
Maven home: /Users/ykf_2001/Programs/apache-maven/apache-maven-3.2.5
Java version: 1.8.0_45, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.10.4", arch: "x86_64", family: "mac"

Or add to ~/.mavenrc

$ echo 'export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/' >> ~/.mavenrc

Or write a bash script and export JAVA_HOME in script and then run maven

build.sh
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/
mvn -v
​

Friday, 24 July 2015

[English] indefinite vs. definite articles

Articles are so difficult for me. It is inevitable to make mistake when writing a research paper. It is also a problem always mentioned by reviewers, including my supervisor as well. It is so bad!
Besides, I also took a test from British Council. The result is no surprise. Only 7 in 12 are right.
Below is a summary from references for a/an and the articles.

Reasons to use the indefinite (a/an) article before a noun

  • countable nouns but doesn’t know exactly which one
    • I would like an apple.
  • one of a group
    • I am a student in the university.
  • a job or position
    • I am a software engineer.
  • all things of the kind
    • A computer has a CPU. (All computers have a CPU.)

Reasons to use without articles before a noun

  • plural nouns
    • Many softwares are developed to increase the productivity at work.
  • uncountable nouns
    • Information can be encoded into different formats in a computer.
    • Life is harder today than …
  • continents
    • Africa, Asia, North America, South America, Antarctica, Europe and Australia
  • languages
    • Chinese, English, French etc
    • She is fluent in French.
  • specific year
    • She was born in 1989.

Reasons to use the definite article before a noun

  • only one
    • The earth/the sun/the moon
  • before superlative adjective
    • She is the most beautiful girl in our class.
    • She is the best person I ever met.
  • only one in that place
    • The button on the right window is blue.
  • already mention it
    • This work presents a new approach to address the model checking issue. The approach is …
  • all things referred by a noun
    • The elephant is really a big animal. (Elephants are really big animals.)
  • musical instruments
    • I can play the piano.
    • The guitar is a popular musical instrument.
  • refer to a system or service
    • The police is on the way.
    • I am listening to the radio.
    • How long does it take by the bus?
  • with adjectives to talk about groups of people
    • The poor
    • The rich
    • The elderly
    • The disabled
  • no article with names
    • London is a big city.
    • Jane Austen was an English novelist.
    • We are from China.
  • names of countries contain Kingdom, States and Republic
    • the United Kingdom
    • the United States
    • the People’s Republic of China
  • names of counties are plural
    • the Netherlands, the Philippines
  • geographical features
    • the Atlantic, the Arctic Ocean, the West Lake, the Panama Canal, the Himalayas
  • newspapers
    • The Times, The Sun, The Daily Mail
  • famous buildings or works of art
    • the Taj Mahal, the Great Wall, the Mona Lisa
  • organisations
    • the United Nations, the Red Cross
  • hotels, pubs and restaurants
    • the Grand Budapest Hotel
    • no article if it’s the owner’s name: Hilton Hotel
  • families
    • the Smiths
  • decade/era
    • For people born in the 1960’s,
​

Monday, 13 July 2015

[Eiffel] design by contract


This is a very simple example. There are two levels of contract.
  • The contract for set_val routine: require states the precondition of this routine that the argument v shall not be negative; ensure enforces the postcondition of this routine that the val shall be equal to v. 
  • Class invariant enforces that for all objects/instances of this class their val shall not be negative.
The do  clause actually is an implementation of the specification, expressed by precondition and postcondition, of reset or set_val. If the implementation violates the specification, then bugs are found. This concept is similar to the refinement check in B method. When we regard class in Eiffel as a B machine, member features map to variables in B, create maps to initialisation in B and invariant to invariant. However in B method, a specification and its implementation are in different machine but Eiffel takes them together.

In B method, it can check the correctness between the implementation and the specification by a theorem prover or a model checker. In Eiffel, it might use the hybrid method (static analysis plus test run) to check it.

If we can write the specification in B method (or similar formal languages such as Z notation, VDM), then refine them to an implementation by the refinement check. Afterwards, we may translate this refinement to Eiffel language and also keep the specification. It might be a good way from a formal specification, formal verification and finally to the code.

[Eiffel] first impression

Just read a blog from professor Bertrand Meyer at CACM. For me, it is the second time to hear about Eiffel. I can not recall when to read it before but just have a bit impression of this term.
I also searched my email@University and found some information about it. One is about a seminar from the professor but unfortunately I didn’t participate. And another news is about the honorary doctorate to the professor from my university (University of York) in 2014.
When I was reading the post, I am very interested in the concept of fixing bugs automatically by AutoFix. So took some time (about 4 hours) to read the introductions of Eiffel from Wikipedia and Eiffel Software. And there are some good presentations.
Some key features of Eiffel are shown below.
​



Eiffel Language
  • Object-Oriented
    • Inheritance
    • Class
      • Feature
      • Create
      • Invariant
  • Design by Contract [Specification]
    • Precondition by "require"
    • Postcondition by "ensure"
    • Class Invariant
      • Inherited
  • Static Typing
    • Instead of dynamic typing
    • Typing check before execution and done in compiler
  • Void-Safety
    • NullPointer is a nightmare (Java, C, ...)!
    • Protect runtime errors caused by calls to null
    • An attached variable is not allowed to be set to void
  • SCOOP
    • Only add one "separate" keyword

Wednesday, 8 July 2015

Multiple Java versions management and switch on Mac OS X

For my research, I do need at least two different Java versions (6.0 and 7.0 or above) on my Mac OS X. I found this tool jEnv is very useful and the Andrew’s “ Managing multiple versions of Java on OS X” is helpful.
  • Check Current Java Version
      $ java -version
      java version "1.6.0_65"
      Java(TM) SE Runtime Environment (build 1.6.0_65-b14-466.1-11M4716)
      Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-466.1, mixed mode)
      $ /usr/libexec/java_home
      /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
      $ ls /System/Library/Java/JavaVirtualMachines/
      1.6.0.jdk
    

Installation of jEnv

  • Install
      $brew install jenv
    
      ==> Downloading https://homebrew.bintray.com/bottles/jenv-0.4.3.yosemite.bottle.tar.gz
      ######################################################################## 100.0%
      ==> Pouring jenv-0.4.3.yosemite.bottle.tar.gz
      ==> Caveats
      To enable shims and autocompletion add to your profile:
        if which jenv > /dev/null; then eval "$(jenv init -)"; fi
    
      To use Homebrew's directories rather than ~/.jenv add to your profile:
        export JENV_ROOT=/usr/local/opt/jenv
    
      ==> Summary
      🍺  /usr/local/Cellar/jenv/0.4.3: 74 files, 308K 
    
  • Follow the instruction
      $ echo 'if which jenv > /dev/null; then eval "$(jenv init -)"; fi' >> .bash_profile
    
  • Check
      $ jenv versions
      * system (set by /Users/ykf_2001/.jenv/version)
    

Installation of Java 8

  • Download and install it manually from Oracle
  • Check it
      $java -version
      java version "1.8.0_45"
      Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
      Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)
      $ /usr/libexec/java_home
      /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home
      $ ls /Library/Java/JavaVirtualMachines/
      jdk1.8.0_45.jdk
    
  • So now the OS comes with Java 8
  • List all javas installed
      $ /usr/libexec/java_home -verbose
      Matching Java Virtual Machines (3):
          1.8.0_45, x86_64:    "Java SE 8"    /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home
          1.6.0_65-b14-466.1, x86_64:    "Java SE 6"    /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
    

Add of old Java 6 and Java 8 to jEnv

    $ jenv add /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
    oracle64-1.6.0.65 added
    1.6.0.65 added
    1.6 added
    $ jenv versions
    * system (set by /Users/ykf_2001/.jenv/version)
      1.6
      1.6.0.65
      oracle64-1.6.0.65
    $ jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/
    oracle64-1.8.0.45 added
    1.8.0.45 added
    1.8 added
    $ jenv versions
    * system (set by /Users/ykf_2001/.jenv/version)
      1.6
      1.6.0.65
      1.8
      1.8.0.45
      oracle64-1.6.0.65
      oracle64-1.8.0.45

Configurations

  • Set global Java version to Java 8
      $ jenv global oracle64-1.6.0.65
      $ jenv versions
        system
        1.6
        1.6.0.65
        1.8
        1.8.0.45
      * oracle64-1.6.0.65 (set by /Users/ykf_2001/.jenv/version)
        oracle64-1.8.0.45
      $ jenv global oracle64-1.8.0.45
      $ jenv versions
        system
        1.6
        1.6.0.65
        1.8
        1.8.0.45
        oracle64-1.6.0.65
      * oracle64-1.8.0.45 (set by /Users/ykf_2001/.jenv/version)
      $ cat ~/.jenv/version
      oracle64-1.8.0.45
      $ java -version
      java version "1.8.0_45"
      Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
      Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)
    
  • Set the Java 6 for local directories
      ~/Programs/eclipse-kepler-for-czt$ jenv local oracle64-1.6.0.65
      ~/Programs/eclipse-kepler-for-czt$ jenv versions
        system
        1.6
        1.6.0.65
        1.8
        1.8.0.45
      * oracle64-1.6.0.65 (set by /Users/ykf_2001/Programs/eclipse-kepler-for-czt/.java-version)
        oracle64-1.8.0.45
      ~/Programs/eclipse-kepler-for-czt$ cd ~/Workspace/czt-code-20150622/
      ~/Workspace/czt-code-20150622$ jenv local oracle64-1.6.0.65
      ~/Workspace/czt-code-20150622$ jenv versions
        system
        1.6
        1.6.0.65
        1.8
        1.8.0.45
      * oracle64-1.6.0.65 (set by /Users/ykf_2001/Workspace/czt-code-20150622/.java-version)
        oracle64-1.8.0.45
    
​

Thursday, 21 May 2015

[Linux] A progress bar implemented in C on Linux and MacOS X

A progress bar implemented in C on Linux and MacOS X

Implementations

  • CR - carriage return (\r)
  • CSI_1 - move up one row and back to column 1, and then erase display from cursor to end of display (\033[1F\033[J)
  • CSI_2 - move up one row and back to column 1, and then erase display from start of display to cursor (\033[1F\033[1J)
  • CSI_3 - move up one row and then erase whole line (\033[1A\033[2K)
CSI Sequence Reference:

Source code


// An implementation of progress bar in C on Linux
//
// Usage:
//    ./prog [type]
//        type: CR, CSI_1, CSI_2, CSI_3
//
// Bar style: 
//  0 - 10% [====                                                 ] 
//  1 -     |====.................................................| 10%
//  2 -     [||||.................................................] 10%
//
// Implementations
//  CR    - carriage return (\r) 
//  CSI_1 - move up one row and back to column 1, and then 
//          erase display from cursor to end of display (\033[1F\033[J)
//  CSI_2 - move up one row and back to column 1, and then 
//          erase display from start of display to cursor (\033[1F\033[1J)
//  CSI_3 - move up one row and then erase whole line (\033[1A\033[2K)
//  
//  Reference for CSI sequence: man console_codes(4)
//

#include 
#include 
//#include  // usleep deprecated
#include    // nanosleep
#include    // strcmp

//#define BAR_STYLE   0
//#define BAR_STYLE   1
#define BAR_STYLE   2

typedef enum
{
    T_CR,
    T_CSI_1,
    T_CSI_2,
    T_CSI_3,
}TYPE_OF_CONTROL;

void usage()
{
    printf("./prog [type]\n");
    printf("    type: CR, CSI_1, CSI_2, CSI_3\n");
}

void prog_bar (int done, int all, int width, TYPE_OF_CONTROL t)
{
    float ration = (done*100)/(float)all;
    int d = (int)(ration * width)/100;
    int i;

#if BAR_STYLE == 0
    printf("%d%% [", (int)ration); 
    for(i = 0; i < d; i++)
        printf("=");

    for(i = d; i < width; i++)
        printf(" ");

    printf("]"); 
#elif BAR_STYLE == 1
    printf("|"); 
    for(i = 0; i < d; i++)
        printf("=");

    for(i = d; i < width; i++)
        printf(".");

    printf("| %d%% ", (int)ration); 
#elif BAR_STYLE == 2
    printf("["); 
    for(i = 0; i < d; i++)
        printf("|");

    for(i = d; i < width; i++)
        printf(".");

    printf("] %d%% ", (int)ration); 
#endif
    switch(t)
    {
        case T_CR: 
            printf("\r");
            fflush(stdout);
            break;
        case T_CSI_1:
            // move cursor up for 1 line and then erase from cursor to end of display 
            printf("\n\033[1F\033[J");
            break;
        case T_CSI_2:
            // move cursor up for 1 line and then erase from start of display to cursor
            printf("\n\033[1F\033[1J");
            break;
        case T_CSI_3:
            // move cursor up for 1 line and then erase whole line. 
            printf("\n\033[1A\033[2K");
            break;
        default:
            break;
    }
}

int main(int argc, char *argv[])
{
    int i = 0;
    const struct timespec t = {0, 100000000}; //100ms
    TYPE_OF_CONTROL ctrl_type = T_CR;

    if(0 == isatty(1))
    {
        printf("Not a terminal.\n");
        fflush(stdout);
    }

    switch (argc)
    {
        case 1:
            ctrl_type = T_CR;
            break;
        case 2:
            if (strcmp(argv[1], "CR") == 0)
            {
                ctrl_type = T_CR;
            }
            else if (strcmp(argv[1], "CSI_1") == 0)
            {
                ctrl_type = T_CSI_1;
            }
            else if (strcmp(argv[1], "CSI_2") == 0)
            {
                ctrl_type = T_CSI_2;
            }
            else if (strcmp(argv[1], "CSI_3") == 0)
            {
                ctrl_type = T_CSI_3;
            }
            else
            {
                usage(); 
                exit(-1);
            }
            break;
        default:
            usage();
            exit(-1);
    }

    for(i = 0; i <= 100; i++)
    { 
        if(isatty(1))
        { 
            prog_bar(i, 100, 100, ctrl_type); 
        } 
        nanosleep(&t, NULL); //0.1 sec
    }

    exit(0);
}

Test

MacOS X

Different terminal emulators
Apple Terminal
  • Works: CR, CSI_1, CSI_2, CSI_3
  • $echo $TERM is xterm-color
iTerm2
  • Works: CR, CSI_1, CSI_2, CSI_3
  • $echo $TERM is xterm-color
xterm
  • Works: CR, CSI_1, CSI_2, CSI_3
  • $echo $TERM is xterm

Ubuntu

virtual terminal (tty)
  • Works: CR, CSI_1, CSI_2, CSI_3
  • $echo $TERM is linux
gnome-terminal
  • Works: CR, CSI_1, CSI_2, CSI_3
  • $echo $TERM is gnome-terminal
xterm
  • Works: CR, CSI_1, CSI_2, CSI_3
  • $echo $TERM is xterm
ssh from iTerm2 on MacOS X
  • Works: CR, CSI_2, CSI_3
  • Doesn’t work: CSI_1
  • $echo $TERM is xterm-color
ssh from xterm on MacOS X
  • Works: CR, CSI_1, CSI_2, CSI_3
  • $echo $TERM is xterm
​