Thursday, 6 February 2014

[Makefile] filter unnecessary string and extract id number from a list of strings

For example, here’s a list of strings tst2b tst2222 tst22b 222b test tst22b2 tst222_ tst2333 tsta223. I want to only keep the string tst2222 tst2333 which starts with tst and follows with number, and no other string allowed. Besides, I will extract the id 2222 and 2333, and finally make them a comma separate list 2222,2333.
TEST_NAMES := tst2b tst2222 tst22b 222b test tst22b2 tst222_ tst2333 tsta223

# get the test id but excluded the case which doesn't have prefixed 'tst'
TST_IDS := $(foreach test,$(filter tst%,$(TEST_NAMES)), $(subst tst,,$(test)))

comma := ,
empty :=
space := $(empty) # 
# Comma separate test id lists, such as 123,2256,2256b
TST_IDS_COMMA_T := $(subst $(space),$(comma),$(strip $(TST_IDS)))

# Only keep the numbers and exclude the test which has the word [a-zA-Z_] in its id.
TST_IDS_COMMA := $(shell echo $(TST_IDS_COMMA_T) | sed 's/[^,]*[a-zA-Z_][^,]*//g; s/,,*,/,/g; s/^,//g; s/,$$//g')
  • filter function will only keep the string which begins with tst
  • subst will replace tst with empty, which removes the tst
  • foreach will recursively loop each test after filter
  • Second subst replace space ` with comma,`
  • The $TST_IDS_COMMA_T is like 2222,22b2,2333. sed will substitute the string which has alphabet [a-zA-z] and underline _ with empty, then replace multiple comma such as ,,, with one comma ,. The leading comma , and comma in the end of line are also removed.

[Java] Visitor Design Pattern in Java

Visitor Design Pattern

In object-oriented programming and software engineering, the visitor design pattern is a way of separating an algorithm from an object structure on which it operates. A practical result of this separation is the ability to add new operations to existing object structures without modifying those structures. It is one way to easily follow the open/closed principle. (from Wikipedia)
  • Open/Closed Principle
In object-oriented programming, the open/closed principle states “software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification“;[1] that is, such an entity can allow its behaviour to be modified without altering its source code. This is especially valuable in a production environment, where changes to source code may necessitate code reviews, unit tests, and other such procedures to qualify it for use in a product: code obeying the principle doesn’t change when it is extended, and therefore needs no such effort. (from Wikipedia)

When to use the visitor pattern

  1. Current class and object structure is fixed, and can’t make changes to it. But you do need to add new operations to that hierarchy. The dilemma is that you want to add operations to the base class, but you can not modify the base class
  2. Some unrelated operations need to be added on objects in an object structure, but you don’t want to add each operation in each object to let the class messed, and hope a good structure of class
  3. Object structure is seldom changed, but the operations may be added or modified frequently. Similar to 1
  4. Classes in object structure have difference interfaces and you expect that which operation is performed depends on its concrete class.

How to use visitor pattern (Take AST as an instance)

  • All nodes have the similar operations (such as getDeclName, getSchText). For example, DeclNamePrint will traverse AST and print the word-parts of all declaring names (DeclName)
  • To add this operation, define a new Visitor class first

Right Example

CZT (Community Z Tool) project uses the visitor pattern to separate the structure of a set of AST (Annotated Syntax Tree) node from the operations performed on these nodes.
  • CZT has the defined inheritance hierarchy, and different kinds of nodes, such as paragraph, schema, and reference.

Reference:

[Perl] Retrieve bug status from Bugzilla server

Bugzilla provides an initial script called bz_webservice_demo.pl which shows how to talk to Bugzilla via XMLRPC.
The option --bug_id can take one bug id as a parameter to the script, such as --bug_id 1212. So if we want to retrieve the status for multiple bugs, we have to call the script multiple times. It’s time consuming. It’s better to call the script once and retrieve the status for a list of bugs.
Look at the original code for the handle of bug_id. The web service method used to get status is called Bug.get.
  • The ids parameter is an array of numbers and strings. So we can pass multiple bugs together in ids.
  • Besides, this method can accept standard include_fields and exclude_fields arguments as well.

if ($bug_id) {
    $soapresult = $proxy->call('Bug.get', { ids => [$bug_id] });
    _die_on_fault($soapresult);
    $result = $soapresult->result;
    my $bug = $result->{bugs}->[0];
    foreach my $field (keys(%$bug)) {
        my $value = $bug->{$field};
        if (ref($value) eq 'HASH') {
            foreach (keys %$value) {
                print "$_: " . $value->{$_} . "\n";
            }
        }
        else {
            print "$field: $value\n";
        }
    }
}
My approach is to extend --bug_id to take a comma separate list of bugs, then convert the list to an array for the parameter ids. In addition, since I only want the status, I can use the include_fields to select the field status and id only.
  • Why need id field? Since we return the status for many bugs, we need to check which id is each status linked to.
Another question is about how to get the value of status and id fields from returns. The reference of Bug.get method shows
bugs: An array of hashes that contains information about the bugs with the valid ids. Each hash contains the following items:
id [int]: The unique numeric id of this bug.
status [string]: The current status of the bug.
We can use the perl debug to check the structure of return bugs.
  • run perl script in debug mode
    $ perl -d -e bz_webservice_demo.pl --uri https://mycompany.com/bugzilla/xmlrpc.cgi --bug_id 121,131,141
  • use b to set breakpoint
  • use r to run it
  • use x $result to show the result
Finally I use the following script to display the status for multiple bugs.
my @bugs;

if ($bug_id) {
    @bugs = split(/,/, $bug_id);
    my $b = $_;
    $soapresult = $proxy->call('Bug.get', { ids => [@bugs], include_fields => ['id', 'status']});
    _die_on_fault($soapresult);
    $result = $soapresult->result;
    my $bugs = $result->{bugs};
    foreach my $bug (@$bugs) {
        print "status: $bug->{id} $bug->{status}\n";
    }
}

Wednesday, 5 February 2014

[English Writing] Good references

Reference

In particular, his calculations suggest, the firn of the Wilkins, Larsen C, George VI and Cosgrove shelves in west Antarctica will be saturated with meltwater by the end of the centurya finding which echoes that of a study led by Jan van Angelen, also of Utrecht University, that was published last year in Geophysical Research Letters. [P67, The Economist (Feb 07, 2014)]
This, as they reported in December’s Nature Geoscience, contains some 140 bil- lion tonnes of liquid water. [P67, The Economist (Feb 07, 2014)]

Further explanation or description

Parenthesis

There is, for example, little Neanderthal DNA on the X chromosome (which, along with the Y chromosome, determines an individual’s sex). [P66, The Economist (Feb 07, 2014)]
Dr Forster and his colleagues think the aquifer they have found covers an area of 70,000 square kilometres (about the size of Ireland). [P67, The Economist (Feb 07, 2014)]

em dash (—)

For instance, genes affecting the production of keratin—an important component of hair and skin—showed more Neanderthal influence than most. [P66, The Economist (Feb 07, 2014)]
This would not matter as a one-off, but if it happened repeatedly as new firn formed the consequences could be seriousparticularly if, in escaping, it caused more hydrofracturing or acted as a lubricant that encouraged previously stable ice to slip from its bed- rock into the sea. [P67, The Economist (Feb 07, 2014)]
How likely that sort of thing is to happen—and thus how much of a threat firn really is—has yet to be determined. [P67, The Economist (Feb 07, 2014)]

Enumerate

And two studies, one just published in Nature, and one in Science, have now looked in detail at this miscegenation, and tried to understand its consequences. [P66, The Economist (Feb 07, 2014)]

Emphasise

But the fact that so little is known about it emphasises a wider point. [P67, The Economist (Feb 07, 2014)]

Misc

Crucially, though the amount of Neanderthal DNA in any individual is small, the exact bits vary a lot from person to person. [P67, The Economist (Feb 07, 2014)]
Technically, Neanderthals may be gone. But their DNA ghosts linger on. [P67, The Economist (Feb 07, 2014)]