Friday, July 21, 2017

The need for immutability

Immutable objects are entities that in the eyes of the external observer their state doesn't change. This doesn't strictly mean that internally the object doesn't change. It rather means, that as far as the API consumer is aware, any exposed method can be called without affecting the outcome of any future calls of all the exposed methods of the same instance.

This might be a confusing definition at first but let's take it bit by bit. First, let's look at the seemingly heretic statement that an immutable object can change internally.

Consider the String class in Java. It is without doubt an immutable object. It encapsulates a char array which it protects by copying it every time the API consumer requests for it. Any instance method of the String class that does string operations gives a new String. If you look closely in the source code though, there is one field (in Java 8) that changes; the hash.

Calling the hashCode method of a string has a side effect. It caches the hashCode result so it won't have to compute it again. This is invisible to the external observer as all the future method calls will return the same result both before and after calling the hashCode method. Even the memory usage remains constant as the 4 bytes of the primitive int are already reserved.

This is not even a problem for multi-threading. Hash doesn't strictly need to be volatile, it will either be 0 and thus re-computed (it's not a real problem if it's computed multiple times in parallel or before the threads get the updated version of it) or not. There is no middle state since writing to an int is atomic.

Immutable objects are enjoying such optimisation delights specifically because they are immutable; You can call hash code a million times, you'll get the same result, cached or not.

Strings can also be interned ( a pool of re-usable strings) and Integers (Integer.class) are cached (from -128 to 127). Yes, immutability can also enable easy memory optimisations.

API considerations


Who is the consumer of the object? There are three types of consumers, the developer that interacts with an object via its API, the object itself including internally defined classes and a special case of consumers that break the immutability contract because "they know what they are doing".


The developer as a user


The first consumer is the one you need to worry about the most. Every public method you define in your class is a contract between you and the API user. If the public methods change the state, you need not only maintaining them but also handling all state errors at every entry point.

To demonstrate this, let's have a look at my favourite example:

final class Dog {
    public final int barkLevel;
    public final String name;
    public Dog(int barkLevel, String name) {
        if (barkLevel < 0)
            throw new IllegalArgumentException("Bark level cannot be a negative number");
        if (name == null || name.isEmpty())
            throw new IllegalArgumentException("A dog needs a name");
        this.name = name;
        this.barkLevel = barkLevel;
    }
}

This is an immutable dog. Once it's created neither its name nor its barkLevel can change. Of course this is not true in real life and we'll come back to that.

The benefit here is that given any Dog instance, it can be safely used forever. There is no way, as far as the external observer is aware, that you have a Dog that doesn't have a name or the bark level is negative. So the rest of the codebase need not worry about any validations of any Dog property.

In real life, the bark level can change (even the name in rare cases). But despite the fact that you can easily model real life in OOP, a computer program remains a different world with its own domain and semantics. Here the semantics clash a bit. Having no setter, the model says this dog will forever have this bark level. In the software world we can model this with a with method and give a new dog, preserving the real life semantics.

Also remember that this is not a real dog but it's the idea of what our program thinks of a dog. Thus, you can think of getting a new idea of what the dog is, instead of thinking in terms of physically getting a new dog because the bark level changed.

Now consider having this type of implementation, which is the commonest among Java codebases:

class Dog {
    private int barkLevel;
    private String name;
 
    public int getBarkLevel() { return barkLevel;}
    public String getName() { return name;}
    public void setBarkLevel(int barkLevel) { this.barkLevel = barkLevel;}
    public void setName(String name) {this.name = name;}

}

You can argue that you can add validation in every setter method. Even so, that means at any point, your dog instance can break and you'll need a new dog or go back to the previous one. How do you manage failures? You need to remember previous states and recover the dog. Or put logic that doesn't change the state to an erroneous one. All these just add more technical depth.

Builders

The second consumer which is also important is the object's class definition itself. It can incorporate all the mutability needs of the object in order to create the pre-defined immutable object.

For instance, consider that you have quite a few object properties. Calling a constructor with more than 3 parameters (or any method in fact) is inconvenient. What you need is a builder.

The builder will manage the mutability, you can call method after method defining the desirable state of the object. This is the concept of the string builder as well. That way you also tackle some performance considerations where you won't need to create and destroy N objects for N properties.

Creating a builder though is often a burden since you need to write a lot of boilerplate code. Fear not:  there are libraries to generate that code for you on compile time (e.g. Lombok for Java).

What about changing a single property? For POJOS it's tempting to have setter methods because they seem cheap and they change the state of a single object, no copies involved. To write a method that gives you the same object with a single property changed is again involving a lot of boilerplate code which also doesn't seem efficient.

All these though can be automated, either by macros or by using libraries such as Lombok. In Lombok's case you can just use @Wither which gives you a with method for every parameter that you want to be changeable. The performance overhead is minimal, since the copy of the properties is shallow.

Deserialising objects


We finally have the case of the third consumer. The one that breaks our immutability contract because it knows what it's doing. There is a way to even avoid that but we'll discuss it at the end.

Such consumers are normally serialisation libraries, such as Gson for Json or Hibernate for database entities. What they do with the most common configuration is instantiate an object and for each field they'll try to find a setter method that matches the field name prefixed with set in camel case. Configured appropriately for immutable objects they will instantiate the object and reflectively assign a value to each object field. Even final fields can be altered on runtime - in the case of Java at least.

Now the assumption here is that the immutability breaks in a limited scope; the method that does the deserialisation. At the end you will get a reference of a deserialised object and not a reference of an object which is being deserialised.

Given the right configuration some libraries allow calling the constructor with all the arguments needed directly. For example, Jackson has a set of annotations that you can use to map each json field with a constructor parameter.

Semantics


We've seen the 3 consumers, now we need to go back to the most important one; The human developers. So far we've seen that the benefit we give to them is not to worry about an object being in an invalid state.

Immutable objects give great semantics to the external observer as well. Consider the following definitions of an Exception:

final class JsonTypeCastingDecodingFailure extends Exception {
    public JsonTypeCastingDecodingFailure(String fieldName, Class expectedType, Class found) {
        super(String.format("%s cannot be casted to %s from %s", fieldName, expectedType.getName(), found.getName()));
    }
}

class JsonTypeCastingDecodingFailure extends Exception {
    private String message;
    public JsonTypeCastingDecodingFailure(String fieldName, Class expectedType, Class found) {
        this.messageString.format("%s cannot be casted to %s from %s", fieldName, expectedType.getName(), found.getName());
    }
    public void setMessage(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }
}


The second definition makes no sense. What are the semantics of having an error that you allow someone to alter its message?

Semantics are often ignored during programming. Most developers have been using the same wrong things over and over again until they have become the normal; adding getters and setters is one of them, post-fixing Exception at every exception class name is another (but this is a story for another time).


Summary


Every public method provided is a contract, it has a purpose and a meaning and allows interpretations which are sometimes the wrong ones; semantics are the thing that everyone cares only when they have inherited legacy code.

State changes need to be managed in a restricted area where a Facade provides the minimum API to do one thing and the internal state is encapsulated and protected.

My final advice is an old but often neglected one: Make something public if and only if it needs to be public. A setter method will never pass that condition.

Friday, May 6, 2016

Memory efficient serialization for Android



VSerializer

A library to serialize and deserialize objects with minimum memory usage.

Gradle dependencies

allprojects {
        repositories {
            ...
            maven { url "https://jitpack.io" }
        }
}
dependencies {
    compile 'com.github.vaslabs:VSerializer:1.0'
}

Example

VSerializer vSerializer = new AlphabeticalSerializer();
TestUtils.AllEncapsulatedData allEncapsulatedData = new TestUtils.AllEncapsulatedData();
allEncapsulatedData.a = -1L;
allEncapsulatedData.b = 1;
allEncapsulatedData.c = 127;
allEncapsulatedData.d = -32768;
allEncapsulatedData.e = true;
allEncapsulatedData.f = 'h';

byte[] data = vSerializer.serialize(allEncapsulatedData);

TestUtils.AllEncapsulatedData recoveredData = 
    vSerializer.deserialise(data, TestUtils.AllEncapsulatedData.class);

Motivation

Memory on Android is precious. Every application should be using the minimum available memory both volatile and persistent. However, the complexity of doing such a thing is too much for the average developer that wants to ship the application as fast as possible. The aim of this library is to automate the whole process and replace ideally the default serialization mechanism.
That can achieve:
  • Lazy compression and decompression on the fly to keep volatile memory usage low for objects that are not used frequently (e.g. cached objects with low hit/miss ratio).
  • Occupying less persistent memory when saving objects on disk.

How does it work?

This project is under development and very young. However, you can use it if you are curious or you want to be a step ahead by following the examples in the unit test classes.

Advantages

  • A lot less memory usage when serializing objects compared to JVM or json.
  • Faster processing for serialization/deserialization
  • Extensible: will be able to easily encrypt and decrypt your serialized objects

Disadvantages

  • Less forgiving for changed classes. A mechanism to manage changes will be in place but since the meta data for the classes won't be carried over it will never be the same as the defaults.
  • Does not maintain the object graph meaning that a cyclic data structure will not be possible to be serialized.

Use case

  • Any data structure that matches a timestamp with other primitive values would be highly optimised in terms of space when saving the data using this approach. You can save millions of key/value pairs for data like timestamp/location history graph.
  • Short lived cache data are in less danger to cause problems when you do class changes. You can benefit by reducing the memory usage in your caching mechanism and not worry much about versioning problems.

Get the code from: https://github.com/vaslabs/VSerializer

Sunday, January 10, 2016

Trackpa: Never lose your grandparents

Or that was the initial concept. You can track a phone's location via sms and you have the option for encryption, so your location won't leak here and there.








Get it from:

https://play.google.com/store/apps/details?id=com.vaslabs.trackpa

Receiver:
https://play.google.com/store/apps/details?id=com.vaslabs.trackpa_receiver


I would have the receiver free as well, but I have to use google maps API service, so this is mainly to avoid spam and api charges or cover them if the need arises. You can get it for free by compiling the source code (see below) using your own API keys.

Source code:

https://github.com/vaslabs/trackpa
https://github.com/vaslabs/trackpa_receiver

Sunday, October 18, 2015

Java: When the compiler crashes the plane

Software design principles on compiled programming languages tend to have one rule in common; Compiler errors over runtime errors. A religiously followed rule. It's a dogma (although based on legitimate reasons). You should always program in a way that most errors would be reported from the compiler and your logic tested by unit testing. Sometimes little things slip through though.

The following scenario describes an easy to do mistake in Java and highlights some good practice to avoid crashing that plane.

Pre-requisites:



Open intelliJ and setup a java project with the following (adapt to match your system):





The example code is this:

import java.util.concurrent.ConcurrentHashMap;


public class FunWithMaps {

    public static void main(String[] args) {
        ConcurrentHashMap map = new ConcurrentHashMap();

        map.put("1", 1);

        System.out.println(map.keySet().getClass());
    }
}

Run. You'll get the following error:

Exception in thread "main" 
java.lang.NoSuchMethodError: 
java.util.concurrent.ConcurrentHashMap.keySet()Ljava/util/concurrent/ConcurrentHashMap$KeySetView;

So, the compiler used java 8 API and didn't complain despite the fact that we set it 
to compile to java 7. But that was the bytecode version and the java 8 API that was in
the compile classpath didn't cause any problems. 

Consider that this scenario is what the continuous integration (CI) might have. 
This imaginary CI system builds your production level code but you - as a programmer - 
have no control over it. Then, the ConcurrentHashMap code would succeed on your IDE 
(because you would be compiling with java 7 targeting java 7) but the CI would be compiling 
java 8 generating java 7 bytecode without having java 7 API in the classpath. The runtime environment
 would use java 7.

You wouldn't know, the compiler wouldn't know, and the crash would rely on the testing environment
 to be caught. That scenario might cause you a late runtime crash on a live environment.

Let's see the bytecode a bit and see if we get what we expect, i.e. a call to the keySet method that returns the KeySetView.

Open your generated class file with Java bytecode editor.



On line 13, this is quite obvious. When running with java 7 you don't get any error until line 13 is executed by jvm and it tries to find that method, which doesn't exist on java 7. So how can we avoid this with a bit of good practice (although, having in compile time classpath an API of a different version that the one on runtime is the most obvious mistake that needs fixing). But we want our code to be as safe as possible and work even in situations where simple API changes won't affect it.

Let's change the left hand side to the interface definition. The Map.

 import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


public class FunWithMaps {

    public static void main(String[] args) {
        Map map = new ConcurrentHashMap();

        map.put("1", 1);

        System.out.println(map.keySet().getClass());
    }
}

Before running it, make a build and see the bytecode. It's now a bit different.



As expected, the compiler now generated the bytecode according to the interface visibility of the method.
If you run it now with java 7 it will not fail and will output:
class java.util.concurrent.ConcurrentHashMap$KeySet

Switch run configurations to jre 8. Run again and you get what you expected:
class java.util.concurrent.ConcurrentHashMap$KeySetView


It's always a good practice to define your variables with the highest superclass or interface 
possible, especially if you are using an external API (which is pretty much always the case). 
Interfaces rarely change or at least they change less frequently from implementation code.


That should as well settle the argument of using on the left hand side the instantiating class 
in the variable definition or their superclass (interface they implement).

Sunday, August 30, 2015

Programming with the metric system - Draft ideas

In a paper called 'Software Development for Infrastructure' Bjarne Stroustrup presented the new features of C++11 with some interesting examples. The most fascinating one was derived from the NASA accident in September of 1999. The root of the accident was a mismanagement of the metric system units due to a poorly designed API that basically was relying on comments.

I'm currently working on a project that requires managing metric units correctly. The language used is Java. Also, I have developed an external, open source library, that provides the essential API for managing metric units. It's a very young project so it supports a very small range of metric units (only those needed by the bigger project) but it is sufficient to demonstrate the basic design principles for managing metric units.

You can find the library here.

Let's examine it's usage in a few examples. Let's say we want to keep track of velocity.

Actually, this is already provided in the library.

public final class VelocityUnit {
    public final DistanceUnit DISTANCE_UNIT;
    public final TimeUnit TIME_UNIT;

    public final double DISTANCE_VALUE;

    public VelocityUnit(DistanceUnit distance_unit, TimeUnit time_unit, double distance_value) {
        DISTANCE_UNIT = distance_unit;
        TIME_UNIT = time_unit;
        DISTANCE_VALUE = distance_value;
    }

    public VelocityUnit convert(DistanceUnit distance_unit, TimeUnit time_unit) {

        if (distance_unit == DISTANCE_UNIT && TIME_UNIT == time_unit)
            return this;

        double newTimeUnitWorthOfCurrentTimeUnit = 1/time_unit.convert(1, TIME_UNIT);

        double newTotalDistance = DISTANCE_VALUE*newTimeUnitWorthOfCurrentTimeUnit;

        double newDistanceValue = distance_unit.convert(DISTANCE_UNIT, newTotalDistance);

        return new VelocityUnit(distance_unit, time_unit, newDistanceValue);

    }


    public String getMetricSignature() {
        return DISTANCE_UNIT.signature + "/" + TIME_UNIT.signature;
    }

    public String toString() {
        return String.format("%.2f%s",DISTANCE_VALUE, getMetricSignature());
    }

}
In the constructor, the programmer passes the metric unit of the distance and the time
This implementation is quite simple, so it gets the difference in distance as a third parameter 
and it assumes the value of time is a single unit of whatever metric is passed. 
You can then convert it to use different distance units or time units accordingly. 

But that doesn't tell much about differences between two people, what about hiding the 
information from the third party developer? Let's see another example now of how the 
developers will get what they expect by interacting with a black box class. In the example below, 
the developer can put values in whatever metric unit they like and get it back in whatever 
format they like. The magic is by telling the class to explicitly work with only one metric unit.

package com.vaslabs.units.examples;

import com.vaslabs.units.DistanceUnit;

public class ExampleDistanceCalculation {

    private final DistanceUnit PREF_DISTANCE_UNIT = DistanceUnit.METERS;

    private double pointA;
    private double pointB;

    public ExampleDistanceCalculation() {

    }

    public void setPointA(double value, DistanceUnit distanceUnit) {
        pointA = DistanceUnit.PREF_DISTANCE_UNIT.convert(distanceUnit, value);
    }


    public void setPointB(double value, DistanceUnit distanceUnit) {
        pointB = DistanceUnit.PREF_DISTANCE_UNIT.convert(distanceUnit, value);
    }

    public double getDistance(DistanceUnit distanceUnit) {
        return distanceUnit.convert(DistanceUnit.PREF_DISTANCE_UNIT, (pointB - pointA));
    }
}
The ExampleDistanceCalculation will work with meters while the third party developers can 
choose their own metric system. For instance, you can have a sensor and some software that 
give you values in centimeters. You can have a class like the above as a middleware 
(with CM instead of METERS) and allow all the other developers to work on the metric unit of 
their preference. It is also useful when delivering to the userland, as users may have different 
preferences on metric units.

Saturday, April 26, 2014

Pi-web-agent Quokka

The pi-web-agent version 0.2 codenamed Quokka has been released since the 24th of April. It provides a better user interface which is faster and more interactive and some extra cool features such as:

  • Pi camera controller (take snapshots or watch a live stream).
  • File manager - browse and download files. 
  • Radio - stream from internet radio or other audio by providing the URL.
The firewall management was also improved which allows now to control access from various protocols and IP addresses.

A video that demonstrates the application:


How to get it:

Download the application from pi store: http://store.raspberrypi.com/projects/pi-web-agent

Give a like to the developers and their project: https://www.facebook.com/pages/Raspberry-Pi-Web-Agent/481006072007776

Monday, April 7, 2014

The next day for your business: Windows XP?

I should have written this article about a year ago to give to someone that cares the time to plan ahead. Frankly, I don't care much. If you have a business and you are technologically impaired it's your fault.

Computers are not an unnecessary "shit that I have to buy, just don't spend much". They are your records, data center, analysis tools and your professional image all in one box. When you've installed or bought computers with Windows XP you did the right thing. They were the best you could find, a value for money deal like no any other. The main reason for that was the Microsoft monopoly. Linux distributions were good but you couldn't find the tools you needed easily, and Apple oh Apple. . .

 But in 2014 things are different. You can have a free office solution that may lack the User Interface eye candy of Microsoft's but it does the job and guess what: it's free. Also you have a large range of free Linux based Operating Systems you can use. People tend to agree that Ubuntu or Linux Mint are the most user friendly ones.

But if you feel that open source and free software is "insecure and vulnerable and Jesus everyone can see the code, is that even safe?" you can buy from Red Hat and have the support you used to have with Microsoft. Which in fact you didn't have, but this time it will be a real thing.

 So before upgrading to Windows 7 or 8 (Jesus are you thinking "what about Vista?" now?) or buying new machines, think about spending a fifth of that money to install a free operating system (which is also safer) and train your employees to use them. If they can't learn it, fire them and get new ones.

 So what are the benefits of Linux based Operating Systems?

 Remember when you needed to update manually Firefox, Google chrome and a bunch of other applications that weren't Microsoft's? Well, no more. Every application (assuming you've installed them correctly which requires an IQ roughly above the 20's) gets updated automatically along with the system updates. And guess what: If you screw up or something breaks, you can roll back (again, if you have the IQ index mentioned before).

"But why do I need to keep updating?". Well that's the reason you are switching from XP to something else right? Anyway, most of your employees play solitaire or they are on Facebook. So get them something that's free and actually works.

Friday, December 27, 2013

The pi-web-agent

Remember the Hackmanchester winning project pi-web-agent? Well, we released the first version of the pi-web-agent in the pistore. Here is the wiki page of the project as generated and exported from our github repository


pi-web-agent

The pi-web-agent is a web application that aims to provide a more user friendly way of interacting with the Raspberry Pi and performing basic tasks by eliminating the need of using the command line directly.

How to use

After starting the pi-web-agent service by executing run.sh or sudo /etc/init.d/pi-web-agent start , you can access the application with your browser via either https://raspberrypi:8003 or https://ip_address_of_your_pi:8003 if your internet router does not resolve hostnames to IPs. To access the application inside your Pi just access the local host without https: http://127.0.0.1:8004

Provided functionalities

The web application currently provides the following functionalities:

  • Firewall management by controlling the iptables.
  • A package management system for installing useful applications easily.
  • Service management for starting or stopping services
  • Update management for updating the underlying Linux distribution with a simple click
  • GPIO management for controlling the pins on the Raspberry Pi (special thanks to the author of wiringPi for his excellent open source program)
  • General purpose information of the system (memory usage, disk capacity, ip, cronjobs, swap usage)
  • Tightvnc is provided, by setting up a vncboot service and enabling users to use tightvnc java applet to access the system by the tightvnc viewer (special thanks to tightvnc for their open source tightvnc client)
  • Power management for rebooting or powering off the system with a simple click

Firewall management

Currently the Firewall management section displays the current state of the iptables. Enabling input for altering the iptables state is under development

Package management

The package management provides a list with useful packages and a short description. You can request an uninstall or install of the application by simply clicking on the switch button.

Service management

Service management allows you to stop or start services. Only services with known state are shown.

Update management

The update management aims to arrange or the hassle about updates for you. It takes care of checking for updates and notifies you on the live information feed. The update section also provides information of weather there is an update or not and if yes, it provides a list of packages with there description that need update. The update can be initiated with a simple click of a button at the end of that list.

GPIO management

The GPIO management provides access to the General Purpose Input Output pins on the Raspberry Pi. You can convert a pin to input or output and activate outputs. Currently only GPIO0-GPIO7 pins are available. The solution is under development to provide more functionality on the second release.

VNC

VNC is very important because most users want to access their pi from their laptop and have an image of the desktop in their screen. That's why the application has the tightvnc server as a dependency and provides the tightvnc client java applet. The whole vnc solution is pre-setup and only clicking at the vnc section should work. The tightvnc service on the RPi should be started manually because you need to setup a password.

Requirements

Currently the web application agent supports the Raspberry Pi with Raspbian installed. Any debian based Linux distribution should also work but is not thoroughly tested yet.

Authors

Vasilis Nicolaou, Angelos Georgiadis, Georgios Chairepetis, Kyriacos Georgiou and Maria Charalambous

License

GPLv2. Imported projects have their own license.

Developer information

Please consult the README file in order to setup an environment for testing purposes of the application. Note that architecture specific code won't work (just the GPIO for the moment). The application is based on the micro-CernVM web appliance agent developed at CERN by Vasilis Nicolaou and documentation section contains documents for that web application but are highly relevant to the forked version (the pi-web-agent)

Documentation

Report (only relevant information of the web application, ignore update management section)

Presentation (first 9 slides)

Follow usr/share/pi-web-agent/doc for documentation on key python modules.

Thursday, January 31, 2013

A Dropbox client for the raspberryPI

Overview

Raspybox is a minimalistic but powerful dropbox client written in python for the RaspberryPI.

How to use it

There are three forms that the program can be executed. The simplest is the graphical way. First you have to execute raspberryDropy.py and type login. Follow the instructions and when you finish press ctrl+D to exit. Now you can use it in any of the three forms.

Setting things up

You need to install the dropbox sdk for python (which can be found in dropbox developer page) and create your own app. Download all the files from the file directory of this project and place them in a directory. Create an app for the whole dropbox and copy paste the keys in raspberryDropy.py accordingly.

User Interfece

To execute the user interface simply type ./raspberryDropy_gui.py . The user interface should come up after a few seconds.
You can also use some shortcuts instead of the menu:
For upload press insert key.
For delete press delete key.
To refresh press F5
To navigate through your directories use the arrow keys and enter or double mouse click to either enter a directory or download a file.
There will be some messages in the console, don't worry about them. If the program crashes send the output as a bug report to the developer. This is because it is the initial version, so it might have some serious bugs.

Dropbox command line

You can also execute the raspberryDropy.py and use the dropbox from the command line. Type help to view the necessary commands.

Alternative command line

Because the RaspberryPI is very useful when using scripts and command line fire and forget programs such a functionality is supported. Instead of running ./raspberryDropy.py to access command line, you can supply a dropbox command as argument. Then the program will start-up execute the command, display the output and die. Be ware that the cache can't work on such situations. You can modify it to keep a cache on a file instead of the memory or use your own in your script.
This part however needs further development and consideration, so the command line can be used efficiently and fast enough (for example it makes no sense to run a command for changing directory, it will be of no use for this purpose)

Contribution

Currently the code is not very well commented, but it should not be very difficult to understand it, since it's a small program. However, a commenting will be done soon, I hope. If you want to contribute to this program, please send a request.

Currently, the target groups are developers, testers and PI hobbyists, but anyone can have a go with it.
You can find everything here:   http://sourceforge.net/p/raspybox/wiki/Home/

Wednesday, January 2, 2013

Finding probabilities for RISK

During holidays we find ourselves playing board games. One of the best out there is RISK. During game-play, one might wonder what are the probabilities for each battle scenario. Moreover, if that someone is, well, a geek, he might write a program during game-play to support his army with a little of, em, technology.
So, do we know an equation to find those probabilities? No. Probably we'll find one with a little google search, but why not study it using a more programming method? We have the technology, we know a little bit of programming, so, let's take our chances.

  1. import java.util.Arrays;
  2. public class RiskProbability
  3. {
  4. private static int[] diceThrow(int n)
  5. {
  6. int[] dice = new int[n];
  7. for (int i = 0; i < dice.length; i++)
  8. dice[i] = (int)(Math.random()*6) + 1;
  9. Arrays.sort(dice);
  10. return dice;
  11. }
  12. public enum winner {ATTACKER, DEFENDER, TIE};
  13. private static winner battle(int a, int d)
  14. {
  15. int[] attacker = diceThrow(a);
  16. int[] defender = diceThrow(d);
  17. int aSoldiersLost=0;
  18. int dSoldiersLost=0; 
  19. int j=defender.length - 1;
  20. for (int i = attacker.length - 1; i >= 0 && j >= 0 ; i--)
  21. {
  22. if (attacker[i] > defender[j--])
  23. dSoldiersLost++;
  24. else
  25. aSoldiersLost++;
  26. }
  27. if (aSoldiersLost > dSoldiersLost)
  28. return winner.DEFENDER;
  29. else if (dSoldiersLost > aSoldiersLost)
  30. return winner.ATTACKER;
  31. else
  32. return winner.TIE;
  33. public static void main(String[] args)
  34. {
  35. int experimentSize = Integer.parseInt(args[0]);
  36. int attWins = 0, defWins = 0, ties = 0;
  37. int attStyle = Integer.parseInt(args[1]);
  38. int defStyle = Integer.parseInt(args[2]);
  39. for (int ex = 1; ex <= experimentSize; ex++)
  40. {
  41. winner w = battle(attStyle, defStyle);
  42. switch (w)
  43. {
  44. case ATTACKER: attWins++; break;
  45. case DEFENDER: defWins++; break;
  46. case TIE: ties++; break;
  47. defaultbreak;
  48. }//switch
  49. }
  50. System.out.println("Experiment is with " + attStyle + " attackers and " + defStyle + " 
  51. defenders " + experimentSize + " times");
  52. System.out.println((attWins/(double)experimentSize) + " attackers");
  53. System.out.println((defWins/(double)experimentSize) + " defenders");
  54. System.out.println((ties/(double)experimentSize) + " ties");
  55. }//main
  56. }
Let's explain the code a bit.
Lines 4-11 is the definition of a throw dice function. You specify the number of dice to throw, and a sorted array of the values of each die is returned. Watch out because the dice are sorted in ascending order.

Line 12 is an enumeration to be used for specifying the winner.
Line 13 is the definition of the battle function that takes two arguments, the number of dice the attacker will use and those of the defender as a second argument. Then the diceThrow is called and the battle begins on lines 20-26 until one runs out of dice.
Line 34 is the main method where the battle is initiated for 'experimentSize' number of times using the specified number of dice for each player.
To run this properly you have to pass 3 integer arguments, the experiment size (1000000 should be more than enough), the number of dice of the attacker and the number of dice of the defender. These are the results obtained by using the above program:

$ java RiskProbability 1000000 3 2
Experiment is with 3 attackers and 2 defenders 1000000 times
0.371734 attackers
0.291923 defenders
0.336343 ties

java RiskProbability 1000000 2 2
Experiment is with 2 attackers and 2 defenders 1000000 times
0.227412 attackers
0.448301 defenders
0.324287 ties

$ java RiskProbability 1000000 3 1
Experiment is with 3 attackers and 1 defenders 1000000 times
0.66038 attackers
0.33962 defenders
0.0 ties

$ java RiskProbability 1000000 2 1
Experiment is with 2 attackers and 1 defenders 1000000 times
0.578971 attackers
0.421029 defenders
0.0 ties

$ java RiskProbability 1000000 1 1
Experiment is with 1 attackers and 1 defenders 1000000 times
0.416221 attackers
0.583779 defenders
0.0 ties

$ java RiskProbability 1000000 1 2
Experiment is with 1 attackers and 2 defenders 1000000 times
0.25389 attackers
0.74611 defenders
0.0 ties

So, the next time you'll play RISK, you'll know.

 

Tuesday, December 25, 2012

Linux: The power of Open Source

Everyone that had a go with Linux, has an idea of what Open Source Software means. For those who don't, open source means that the code of the programming language that the software is written and in general the pre-compiled units of it are available to the public.
This gives a lot of advantages. Many programmers can contribute to the code or users that know a couple of things in programming can modify a piece of code and re-compile it to cover their needs and then maybe share it with others that have the same needs. That's how every open source community has been working so far.
We've all heard about it, but few of us have seen it. How do we modify the code? How do we find it? There are a lot of possibilities and options here. For example, most recent programs for Ubuntu are written in Python (e.g. Gwibber). In those programs it won't be difficult to find the code, since Python is not compiled but it's an interpreter based environment. In other words, you can see the code if you open the program with a text editor.

Let's see the real stuff now with an example. We came along the program "fortune" recently and tried to run fortune -f in Fedora, which displays the files that fortune looks at to generate a fortune cookie. We wanted to pass the output data through pipe but it didn't work:
fortune -f | grep humour
After a couple of failures, we assumed that the output was redirected to STDERR instead of STDOUT. But, this is unacceptable code practice. Why send data that are normal output to the error stream? So we downloaded the fortune source rpm package. Here is the process that followed:


  • Find the repository of the fedora-mod.rpm, google is your friend.
  • Find the source code which is in form fortune-mod.src.rpm
    Create a folder in your home directory (/home/username/) named rpmbuild and inside it a directory named SOURCES 
  • Install developer tools and recode-devel: sudo yum install @development-tools recode-devel
  • Extract the rpm in the /home/username/SOURCE/ directory.
  • Modify the code, in that case we found fortune.c and changed in the function print_list() every fprintf(stderr... to fprintf(stdout... (yes it was that simple)
  • To compile it and pack it run rpmbuild -ba fortune-mod.spec.
You can find the rpm now in the rpmbuild/RPMS directory from where you can install it but first run yum remove fortune-mod if you already have it install in your machine.


We've found the source from here

Thursday, October 11, 2012

Android presentation

Download the android presentation (08/10/2012- Man-UP) from here.

Sunday, January 8, 2012

jlab : Your own Java Testing Lab by VasLabs for you!

What is the way of programming big programs? You create object classes and test them individually. You configure them to work as you want and then you put them together to form a nice new shiny program.
Most of the programmers use IDE's to do that. However, if you are a hardcore of the kind, you may wish to create your own environment just as how you like it to be.
VasLabs has created a small script to give you ideas and/or help you start building your own lab environment to do what everyone does in labs; tests!
The philosophy behind it is that for every object class you get a test class. And for every test you do, you can view a log file containing the results. Then you can compare it with the expected results by reading it or using cmp, or even better, you could initially create a file that contains the expected results and then use jlab to run the test by comparing for you both results.
The program works on your current directory. I.e. you should change directory to the directory you want to build your lab and then run jlab.
Here is the manual of the program (you can also view it by running jlab without arguments):

To use this program use the format jlab --argument [file]
-----------------------------------
The list of available arguments are:
--initialise
Initialises your lab environment in your current directory creating the appropriate folders

--create [file1 ... fileN]
Creates the given class names and puts them in the src directory. It also opens them using gedit

--create_tests
Creates a test class for each class in the src directory. It then opens them with gedit

--compile
Compiles every src file in all directories and puts the .class files in the bin directory

--run_tests
Runs all tests and puts the results in log files. Then it opens the log files with gedit

--test file expectedResults
Runs the given test file and compares its results with the given expectedResults file. If there is a difference it opens the results with gedit

Requires: gedit text editor

Author: Vasilis Nicolaou
Distributed by: vaslabs
User Agreement:

This script shall be used with care by people who know what they are doing. This program intends to help building a java lab. By java lab
we mean a virtual environment that behaves like IDE with the difference that offers simple functionalities for testing java object classes.
The author has no responsibility for any loss of data or damage caused by using this script.
You can edit and re-distribute this script as you wish. If you do not agree delete this script file from you computer.

Tip: You can create aliases for every different functionality and put it in .my_bashrc
08/01/2012



Using this idea you can build a similar lab for every language or facility you want.
Download it from here.
This is written in bash, and therefore can be run only on Unix based OS which have bash installed.
By VasLabs


Tuesday, December 13, 2011

The great convert tool of ImageMagick


It's of those days where you are just wondering why you are failing to program as usual. Your mind is sleepy, your thinking slow. While drinking a cup of coffee in order to awake your brain (even if you know that in such situations coffee just fails) you listen something not so important (as most things sound at the beginning). "I have a large pdf file how do I reduce its size?".
(I want to inform you now that this article is not about pdf resizing).
Then, as the coffee sinks in, along with this words, there is another question on the horizon. Can you reduce anything without losing something? Of course not. Anyway, that's not my point.

I decided to try and find how to do that, with linux, using scripts. After a couple of google searches I found ghostscript. It was quite a fail, since the pdf was at its minimum size that ghostscript could convert, so it made it bigger.

Then, I looked at my pictures. I always do this when I fail to find a solution for something. Great images, with high resolution. Hang on! What about making every pdf page as a jpg file and then reducing the resolution of those images, and after that putting all these images back as pdf pages?

If you have the knowledge of a normal user this could mean hours of work, especially if pdf has 100 pages. Imagine having at the end another bigger pdf file.

So, I realised that it was a lot of work. What about using a script to do that? Here it comes the convert of ImageMagick!

I might failed to find an optimised solution for the pdf problem, but I've found a great tool! Convert is a terminal program created by ImageMagick that can convert any image file into another by user's will. In addition, it is open source, written in C.

You can reduce your size of your images, transform them, skew them, flip them, change colours with mapping and more more advance image things! It's like having photoshop in a small program, without the heavy brashes and tools. Type the commands, you have your image converted. Just visit man pages on your linux. Or visit  http://www.imagemagick.org/script/convert.php to download a version with GUI or for a different operating system.

For the story, I used this to convert my pdf:
convert input.pdf -scale 500 output.pdf
And got a smaller file with a terrible resolution.
Anyone knows anything about it?

That's a story about how something irrelevant results to an exploration of another irrelevant thing (but useful).


So, even if you are going to fail, never mind, explore! Even if it's pangolins! :)

Monday, November 14, 2011

Computer Synesthesia

Synesthesia (also spelled synæsthesia or synaesthesia, plural synesthesiae or synaesthesiae), from the ancient Greek σύν (syn), "together," and αἴσθησις (aisthēsis), "sensation," is a neurologically based condition in which stimulation of one sensory or cognitive pathway leads to automatic, involuntary experiences in a second sensory or cognitive pathway. People who report such experiences are known as synesthetes. (http://en.wikipedia.org/wiki/Synesthesia)

Here, we are going to illustrate Synesthesia as a situation where a person (in this case the computer) hears images. I.e. instead of seeing them, the pathway goes from the eyes to the part of the brain that understands sounds (or something like that :))

How is it like to hear images? Let's hear one.

You have to have this tools:
A linux distribution
The pacat program (check if you have it, otherwise install it)
A little C (so we need gcc compiler)

Here is the code for reading a file. We are going to use it to read image files:

#include
int main(int argc, char *argv[])
{
int t = 0;
FILE *input;
input = fopen(argv[1], "r");
while (t != -1)
{t = getc(input); putchar(t);}
fclose(input);
}

Save it as sounds.c and compile it using gcc sounds.c -o sounds
Then use
sounds mypicture.png | pacat --format u8 --rate 8000

And you can hear your pictures and images :P Don't expect the sweetest melody :P


Saturday, June 11, 2011

C : thank you for the memories

C is in general a quite free language, in a sense that it usually compiles! However, when it comes to running, even the most experienced programmers get errors or values that they wouldn't expect.
The most common error is a segmentation fault. This happens when your program tries to write or read a memory location out of its bounds, so the operating system does not allow it.
In addition, sometimes, you get some unexpected values, because you accidentally read a wrong memory location that belongs to your program, so the operating system does not block it. Furthermore,somebody can write accidentally a piece of code that might cause the program to freeze:
Imagine you want to create an array of length 10 and initialise it with 0 values.
What would happen if you write this (by mistake of course)

int main()
{
   
    int a[10];
    int i = 0;
    for (;i<=10; i++)
            a[i] = 0;
   
}



Why your program freezes? What would happen if you read the array without putting initial values in it if you print it out?
Can you guess what would happen if you read a[10]?
Try it! It's really fun!
by VasLabs

Wednesday, June 8, 2011

Text File Encryptor

Here is the idea. Create a simple program that changes the bytes of a file using a passphrase in a such way that they can be revered to their initial state.
Of course this program is not suitable for commercial use because the encryption is very simple. But you get the idea.
The program is written in java and you can download the source code and the compiled program from there:

ByteReader source code
ByteReader byte code
ByteEncryptor source code
ByteEncryptor byte code
DataEncryptor source code
DataEncryptor byte code


The process is:
We have an object ByteReader that reads the contents of a text file using a BufferedReader which wraps a FileReader. It reads it line by line to avoid reading dumb null bytes (it happened when we used InputStreamReader, no idea why!
After that we convert each character of the line to an int! And then we store it in a variable such that b = object ByteReader and b.bytes[][] = thearray of ByteReader that keeps the bytes read.

Then we create another object, a ByteEncryptor which takes as one of its arguments the byte reader and according to another argument password and whether to subtract from the value of the bytes or add(i.e. 0 or 1) as another argument. It also takes the number of lines and the destination to store the encrypted data.

It encrypts each byte with the simplest calculation that exists!
for each byte
byte = byte + (passphrase.hashCode() % 91)
and decrypts
byte = byte - (passphrase.hashCode() % 91)

Some notes to have in mind!
1) Never use this program on important data! They might be corrupt! Use it only on test data that you don't mind to lose!
2) It fails to decrypt images, videos, pdf and similar formats. Do you suspect why?
3) Never use this program to encrypt data and feel safe! Can you think of a simple program that it can decrypt these data without knowing the password? If yes, let others know! (We know :) )

To run the program open a terminal the directory you saved the files and then run
java DataEncryptor yourFileYouWantToEncrypt destinationFile password 1/0

1 is for adding, ie encryption, 0 is for subtracting, ie decryption.
Do not run it the first time with the value 0, you may not be able to recover it, and also remember the notes above.
If you put less arguments you will get an ArrayIndexOutOfBoundsException, i.e. the program will crash.
You can make whatever changes you like on this program since it's just a simple silly one that aims to excite your imagination for building similar things, more sophisticated. So, it is not licensed and there is no copyright.

Enjoy!!!