Saturday, April 12, 2014

PROL is now an OSS project

I had got a request from a Japan graduate student to open sources of my Prol engine (it is a small Prolog engine written in Java with embedded GUI editor), so that now it is an OSS project under Apache License 2.0
If you are going to use the engine then please keep in mind that it was developed for academic purposes and to check some ideas and tested not very well.

Monday, December 9, 2013

Pulling and updating of bunch of Mercurial projects in the same folder

A Useful bash script allows pulling and updating of bunch of Mercurial (Hg) projects placed in the same folder. These project repositories should desire the same credentials for authentication.

 #!/bin/bash  
 set -e  
 echo  
 echo "The Script allows to make bunch pulling and updating of all Hg projects found in the current folder"  
 echo "Author: Igor Maznitsa (http://www.igormaznitsa.com)"  
 echo  
 echo -n "Enter name, followed by [ENTER]: "  
 read hgUserName  
 echo -n "Enter password, followed by [ENTER]:"  
 read -s hgUserPassword  
 echo  
 folderCounter=0  
 for dir in $(ls -d */)  
      do  
           echo ----------------------------------  
           echo "Detected directory $dir"  
           if [ -d "./$dir/.hg" ]; then  
             echo "Making pull and update"  
             defaultPath=$(grep "default" "./$dir.hg/hgrc")  
             extractedPath=${defaultPath#*default*=}  
             extractedPath="${extractedPath#"${extractedPath%%[![:space:]]*}"}"  
             extractedPath="${extractedPath%"${extractedPath##*[![:space:]]}"}"  
             echo "Path $extractedPath"  
             hg --config auth.rc.prefix="$extractedPath" --config auth.rc.username="$hgUserName" --config auth.rc.password="$hgUserPassword" -R "./$dir" pull -u  
             echo  
             folderCounter=$[$folderCounter+1]  
           else  
             echo "WARNING: Directory $dir is not Hg repository"  
             echo  
           fi  
      done  
 echo "The Script work has been completed"  
 echo "Updated $folderCounter folder(s)"  
 exit 0  

Monday, July 15, 2013

Java performance Mind Map is published on GitHub

I have published my Java performance mind map on GitHub as an OSS project, the link is  https://github.com/raydac/Java-performance-mind-map . The Map can be edited in a great tool called XMind (http://www.xmind.net).

Sunday, February 17, 2013

Java Comment Preprocessor 5.3

I am glad to notify all that the new 5.3 version of my Java Comment Preprocessor is out. It is the most powerful preprocessor for Java with support of Ant, Maven and CLI and I guess that it is the one of the oldest Java preprocessors. In the new version I have added the feature to keep preprocessed non-active strings as commented ones in the output to give the possibility to save the line numeration in the resulting sources. The Mode is turned on through the /k directive for CLI, or through the keepLines parameter for Maven and ANT.

Wednesday, September 12, 2012

NetBeans 7.2, workaround for the predefined "Idea" keymap issue

   I use the NetBeans IDE for long years and I was very waiting for he new 7,2 version, unfortunately I have found a terrible critical issue for me in the IDE - a NullPointerException is being thrown when I am trying to change the keyboard map to the IDEA in the "Options->Keymap".
   It is very critical for me and I am sure that the feature is the most important for any IDE. The 7.2 version has been published already more than 2 months and I read that the bug has been fixed but the corrections will be published only in the future version. I tried to use the development version where the bug has been fixed, but the developer version contains a lot of other bugs and can't be used for development (for instance there are a lot of exceptions when you are trying to work in CSS ans JS editors).
  Fortunately I found some workaround for the keymap issue in NB 7.2:
1. Open Tools->Options->Keymap
2. Press "Manage profiles"
3. Choose the "Idea" in the list and press "Duplicate"
4. Select the new duplicated "Idea" profile
5. Restart the IDE
After the restart I have the IDEA key-map working on my machine without any exception.

Sunday, July 22, 2012

Visual testing of a triangle filling graphic method

Some time ago I was invited by a big company for a technical interview where I was asked by a test engineer - "how to write test for a method filling a triangle area?".. I didn't find the answer for the provided time (some inappropriate ideas like to use a neural network chased each other in my brain), but the problem was very interesting for me and an idea dawned me on the next day..
Idea: A Triangle is a very easy shape but it is very hard to distinguish the shape in an image by a computer, but a rectangle is much easier to be processed by a computer, thus we should just make a rectangle from two triangles and then we will check that the rectangle area is presented and artifacts-free, also we can check that there are not any points outside of the area..
I have written some Java code to check the idea. There is not a method in the java.awt.Graphics object to fill a triangle thus I have written such one:
public class TriangleFiller {
    /**
     * The method fills a triangle area and we check that the method fills a triangle
     * @param graphics the graphics context
     * @param xcoords an array contains the x coordinates for vertices (must have 3 positions)
     * @param ycoords an array contains the y coordinates for vertices (must have 3 positions)
     */
    public static void fillTriangle(final Graphics graphics, int[] xcoords, int[] ycoords) {
        if (xcoords.length != 3 || ycoords.length != 3) {
            throw new IllegalArgumentException("Triangle must have 3 points");
        }
        final Polygon polygon = new Polygon(xcoords, ycoords, 3);
        // we need use draw+fill because the fill operation fills the inside area
        graphics.drawPolygon(polygon);
        graphics.fillPolygon(polygon);
    }
}

Then I wrote a unit test to make the "visual check" of the method just on an image and it works well:
package com.igormaznitsa.testtriangle;

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;
import static org.junit.Assert.*;
import org.junit.Test;

public class TriangleFillerTest {
    
    private boolean checkTriangleCornerPoints(final BufferedImage baseRGBImage, final int pointColor, final int[] xVerticies, final int[] yVerticies) {
        for (int pointIndex = 0; pointIndex < 3; pointIndex++) {
            final int x = xVerticies[pointIndex];
            final int y = yVerticies[pointIndex];

            if ((baseRGBImage.getRGB(x, y) & 0xFFFFFF) != pointColor) {
                return false;
            }
        }
        return true;
    }

    private boolean checkRectangleAreaHasBeenFilledOnly(final BufferedImage baseRGBImage, final int fillColor, final int backgroundColor, final int areaLeftX, final int areaTopY, final int areaWidth, final int areaHeight) {
        final int areaRightX = areaLeftX + areaWidth;
        final int areaBottomY = areaTopY + areaHeight;

        final int imagewidth = baseRGBImage.getWidth();
        final int imageheight = baseRGBImage.getHeight();

        for (int y = 0; y < imageheight; y++) {
            for (int x = 0; x < imagewidth; x++) {
                final int rgb = baseRGBImage.getRGB(x, y) & 0xFFFFFF;
                final boolean notInArea = x < areaLeftX || x > areaRightX || y < areaTopY || y > areaBottomY;
                if (notInArea) {
                    assertEquals("Must be background color "+backgroundColor, backgroundColor, rgb);
                } else {
                    assertEquals("Must be fill color " + fillColor, fillColor, rgb);
                }
            }
        }
        return true;
    }
    
    @Test
    public void testFillTriangle_VisualTest() throws Exception {
        // the graphic log image file
        final File gfxLogFile = new File("./testimage.png");

        // create a RGB memory rendered image which will be our base for the test
        final BufferedImage baseTestImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
        final Graphics2D gfx = (Graphics2D) baseTestImage.getGraphics();

        // we must disable antialasing for the graphics to avoid artefacts
        ((Graphics2D) gfx).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);

        final int backColor = 0x000000;
        final int shapeColor = 0xFFFFFF;

        // fill the image by our background color
        gfx.setColor(new Color(backColor));
        gfx.clearRect(0, 0, 200, 200);

        // set the shape color
        gfx.setColor(new Color(shapeColor));

        // our triangles, we use non-equaterial triangles, to make a rectangle, not a square
        final int[] firstTriangleX = new int[]{10, 10, 50};
        final int[] firstTriangleY = new int[]{10, 150, 150};
        final int[] secondTriangleX = new int[]{10, 50, 50};
        final int[] secondTriangleY = new int[]{10, 10, 150};

        // draw the first triangle
        TriangleFiller.fillTriangle(gfx, firstTriangleX, firstTriangleY);

        // save the current graphic state as an image (like log)
        ImageIO.write(baseTestImage, "png", gfxLogFile);

        assertTrue("The image must have set the points of vertices", checkTriangleCornerPoints(baseTestImage, shapeColor, firstTriangleX, firstTriangleY));
  
        // draw the second triangle, to get a filled rectangular area on the image
        TriangleFiller.fillTriangle(gfx, secondTriangleX, secondTriangleY);

        gfx.dispose();

        // save the current graphic state as an image (like log)
        ImageIO.write(baseTestImage, "png", gfxLogFile);

        assertTrue("Only the rectangle area must be filled, without artefacts",checkRectangleAreaHasBeenFilledOnly(baseTestImage, shapeColor, backColor, 10, 10, 40, 140));
    }
}

Sunday, June 17, 2012

Java Performance mind map

On the last week I had visited our local Oracle branch (where smart guys develop very important parts of JVM) because there was very interesting presentation about Java Performance. I made some mind map based on the presentation and verbal information from speakers and translated it into English (because the original presentation was in Russian). You can see the Mind Map image through the link http://igormaznitsa.com/mindmaps/JavaPerformanceMindMap.png

I have used below information sources to make the mind map:
 - the JEEconf 2012 presentation of Aleksander Shipilev and Sergey Kuksenko
 - the Russian presentation  of Aleksander Shipilev and Sergey Kuksenko on JUG.RU (14 june 2012)
 - the high level of their mind map included into  the presentation (they used the map as the speech plan)   

also there are a lot of interesting stuff on the home page of Aleksander Shipilev (http://shipilev.net) but mainly that information in Russian.