Friday 7 June 2013

Perl script to extract and present hex colors found in arbitrary text

The following script searches STDIN for 3 or 6 symbol hex values and prints a html structure that represents those values as colors on page.
#!/usr/bin/perl -w
use Set::Scalar;

$uniqueColors = Set::Scalar->new;
while( defined( $_ = <STDIN> ) ){
 while ( $_ =~/(#[a-fA-F0-9]{1,6})/g ){
  $uniqueColors->insert( $1 )
 }
}
print <<HEADER;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
   <HEAD>
      <TITLE>Found HEX colors</TITLE>
   </HEAD>
   <BODY>
HEADER
while ( defined( my $color = $uniqueColors->each ) ){
 print "<p style=\"background: " . $color . "\">" . $color . "</p>\n"
}
print "</BODY>
</HTML>"
Used this one to parse color-theme-tangotango.el emacs color theme file.
To view example output hit "read more".

Interesting remarks on using standard libraries

While searching for implementation of Sets in Perl bumped into these interesting arguments for using standard libraries on StackOverflow.

Use one of the many Set modules on CPAN. Judging from your example, Set::Light or Set::Scalar seem appropriate.


I can defend this advice with the usual arguments pro CPAN (disregarding possible synergy effects).

  1. How can we know that look-up is all that is needed, both now and in the future? Experience teaches that even the simplest programs expand and sprawl. Using a module would anticipate that.
  2. An API is much nicer for maintenance, or people who need to read and understand the code in general, than an ad-hoc implementation as it allows to think about partial problems at different levels of abstraction.
  3. Related to that, if it turns out that the overhead is undesirable, it is easy to go from a module to a simple by removing indirections or paring data structures and source code. But on the other hand, if one would need more features, it is moderately more difficult to achieve the other way around.
  4. CPAN modules are already tested and to some extent thoroughly debugged, perhaps also the API underwent improvement steps over the time, whereas with ad-hoc, programmers usually implement the first design that comes to mind.

Rarely it turns out that picking a module at the beginning is the wrong choice.

Thursday 6 June 2013

Oracle SQL selects to get familiar with table naming standard and to find junk tables in scheme

Run following selects to discover prefix and suffix hierarchies and to get count of particular prefix/suffix occurrences. That will help you become familiar with naming standard and to find suspicious prefixes/suffixes that belong to tables that probably store junk data.

Wednesday 5 June 2013

Highlight empty tables in PowerDesigner model with VB Script

The wonderful thing about PowerDesigner automation is that you can actually connect to database from VB Script to get some crucial information that is unavailable (or was not captured) at a reverse engineering phase.
The following script queries Oracle ALL_TABLES system view to find out if table in the model stores any data. Then those tables that are empty are highlighted with thick maroon frame.

Monday 3 June 2013

Working with PowerDesigner models through VB interface

PowerDesigner has great automation abilities. The easiest way to work with a model programmatically is to use PowerDesigner's embedded Visual Basic script interface and to run scripts through 'Edit/Run script' window.
Here's an example script that uses given color to highlight tables that have comments or that have at least one column with a comment:
Dim model
Set model = ActiveModel
For each table in model.Tables
   doPaint = false
   tableComment = Trim( table.Comment )
   if ( "" <> tableComment ) then
      doPaint = true
   end if

   if ( not doPaint ) then
      For each column in table.Columns
         comment = Trim( column.Comment )
         if ( "" <> comment ) then
            doPaint = true
            Exit For
         end if
      Next   
   end if
   
   if ( doPaint ) then
      For each symbol in table.Symbols
         symbol.BrushStyle = 6 'Gradient 
         symbol.GradientFillMode = 64
         symbol.FillColor = RGB( 252, 178, 104 ) 
         symbol.GradientEndColor = RGB( 255, 255, 255 )
      Next
   end if
Next
More info about PowerDesigner automation at SyBooks and PowerDesigner's 'Metadata Objects' help file.
Tip: when reverse engineering big unfamiliar scheme you can easily select and move apart particular symbols by adjusting their 'Position' property. For example you can select and move tables with particular prefix:
Dim model
Set model = ActiveModel
PREFIX = "hh_"
For each table in model.Tables
   tableName = LCase( Trim( table.Name ) )
   if ( PREFIX = Left( tableName, Len( prefix) ) ) then
      For each symbol in table.Symbols
         symbol.Position = NewPoint( 0, 0 )
      Next
   end if
Next
After desired symbols are moved to the (0,0) position they can be selected and arranged with 'Symbol->Auto layout command'.
This simple solution of how to distill big scheme came to me after I had reversed database with 2K of tables with a few foreign key constants defined. I faced a brick wall of tables arranged by their column count. All semantics were encoded in table names so I just had to set apart tables with equal prefixes. I've searched for a function to add symbols to selection but with no success. So I decided to move desired symbols to the center of workspace and then to select them with single mouse movement. That's when 'Auto layout' command came in very handy.