Pages

Friday, May 10, 2013

The idea behind alloy

The importance of abstraction

Software is built on abstractions.

No amount of refactoring can rescue a system built on flawed concepts.

First, you design the abstractions, then you develop its embodiments in code. Unfortunately, this approach rarely works.

The problem is wishful thinking. When you implement the abstractions, they turn out to be incoherent and inconsistent.

There are two solutions to the solution of wishful thinking: “extreme programming” and “formal specification”.

Formal specification has two obstacles: succinct mathematical notations hard to understand and the lack of tools. Theorem provers demand effort more than is feasible for most software projects.

Alloy replaces conventional analysis based on theorem proving with a fully automatic analysis that gives immediate feedback. Unlike theorem proving, this analysis is not “complete”: it examines only a finite space of cases.

Unlike testing, this analysis requires no test cases. The users instead provides a property to be checked.

Except from Daniel Jackson’s Software Abstraction book Chapter 1.

You can see an example from this blog post.

Tuesday, January 15, 2013

Headless RCP - issue using other plugins

Issues and Solutions

Most of the issues you encounter when you work with headless RCP is highly likely with the “additial plug-ins”.

Missing constraints

Theoretically, if you have run your RCP in IDE, it should run as standalone without a problem. If you have some issues such as “Missing constraints” You can use this site.

http://stackoverflow.com/questions/14366421/the-bundle-xyz-could-not-resolved-reason-missing-constraint-import-package-a/14367997#14367997

Unbound class path container error

You get this error because your java library is wrongly setup.

Go to Add Library session in Build Path/Configure Build Path and set from “Execution environment” to “Workspace …”.

ScreenShot2013-01-16at11.54.58AM-2013-01-15-15-57.png

http://stackoverflow.com/questions/6798281/unbound-class-path-container-error-in-eclipse

IllegalStateException: “Workbench has not been created yet”

This error message is misleading, and it actually means “there is some race condition happening”.

You can solve this issue by

  1. add -clean option to the parameter
  2. Following the “NoClassFoundError case 1”, that is remove all the dependencies and add them again. When you just keep adding dependencies for whatever reasons you may end up this state.

http://waheedtechblog.blogspot.com/2011/11/javalangillegalstateexception-workbench.html

NoClassFoundError case 1

You add this, and that, and suddenly you got an error “NoClassFound”, what you can do is go to the product file, open the Dependencies. Delete all the plug-ins using “Remove” and “Add Required plug-ins” with the application that you are going to make standalone

NoClassFoundError case 2

You successfully created the plugin (utilities for example), but you may have this kind of error “NoClassDefFoundError”, when you execute the plugin that contains other plugin.

ScreenShot2013-01-15at3.56.41PM-2013-01-15-15-57.png

First thing you need to understand is that reference is built up already for you.

Lets’s say in the course of plugin development, you add “utilities” in “Dependencies”.

ScreenShot2013-01-15at4.03.34PM-2013-01-15-15-57.png

It makes your plugin know how to refer to the utilities during its build, as it finds the utilities, it can build the plugin successfully.

ScreenShot2013-01-15at4.02.43PM-2013-01-15-15-57.png

The issue is actually not in the user’s side, but in the provider’s side. You had to export the package in the “utilities” plugin.

ScreenShot2013-01-15at4.01.14PM-2013-01-15-15-57.png

ScreenShot2013-01-15at4.01.24PM-2013-01-15-15-57.png

Sunday, December 30, 2012

SOLID principle in OOP programming

http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)

  • Single responsibility principle (SRP)
    • An object should have only a single responsibility.
  • Open/closed principle (OCP)
    • Software entitles … should be open for extension, but closed for modification.
  • Liskov substitution principle (LSP)
    • Objects in a program should be replaceable with instance of their subtypes without altering the correctness of that program.
  • Interface segregation principle (ISP)
    • Many client specific interfaces are better one general purpose interface
  • Dependency inversion principle (DIP
    • Do not depend upon concretions. One should depend upon abstraction.

Closure in Java using Anonymous Inner Class - example

Java doesn't support closure, but using Anonymous Inner Class, we can experience closure in Java.

Here is the code.
class Action {
	public void doAction() {
		System.out.println("Hello, world");
	}
}

class A {
	public void doStuff(Action a)
	{
		a.doAction();
	}
    public void method() {
        final int i = 0;

        doStuff(new Action() {
            public void doAction() {
                System.out.println(i);   // or whatever
            }
        });
    }
}

class Closure {
	public static void main(String[] args) {
		new A().method();
	}
}
You should be careful that the local variable in the method() method should be final in order to prevent this error message.

Closure.java:17: error: local variable i is accessed from within inner class; needs to be declared final System.out.println(i); // or whatever ^ 1 error

Instead of giving closure function to doStuff() method, anonymous inner class is given. In its implementation, doStuff() uses methods in A class as if it's closure functions. You can give different code block to doStuff() if necessary.

Compared to python example or lisp example, Java needs full support closure support to make the code simpler and easier to read.

References

Closure in Lisp - example

This is a code that I borrowed from the book Practical Common Lips for closure in Lisp.
(defun artist-selector (artist)
  #'(lambda (cd) (equal (gets cd :artist) artist)))
In the code, artist-selector returns a closure that closes a variable "artist" over it. We can use this closure as a parameter to "select" function. Lisp supports high order function decades ago to enable it possible. "*db*" is a global variable.
(defun select (selector-fn)
  (remove-if-not selector-fn *db*))
The usage of "select" method is as follows. You can create another closure with different parameter to the "artist-selector" function.
(select (artist-selector "Dixie Chicks"))

References

Closure in Python - example

Closure is a "function (code block)" with a "referencing environment". I interpret it as a function pointer with free variables. In Python terms, a variable defined in an enclosing function is a free variable inside any nested functions.
In this example, printer is a closure as it points to a code block with a free variable of "msg". The variable msg is closed over the printer() function.
def make_printer(msg):
    def printer():
        print msg
    return printer

printer = make_printer('Foo!')
printer()
This printer method is not a closure but nested function, as in this case "msg" variable is nothing but a local variable that is not "closed".
def make_printer(msg):
    def printer(msg=msg):
        print msg
    return printer

printer = make_printer("Foo!")
printer() # "Foo!"
printer("Hello") # --> "Hello"

References

Polymorphism in OOP

Polymorphism in OOP is the ability of objects belonging to different types to respond to method, filed, or property calls of the same name. The caller doesn't have to know the type of the callee so that the exact behavior is determined at run-time.

For strong type languages, polymorphism usually means that type A derives from type B or C, In weakly typed languages types are implicitly polymorphic: duck typing allows polymorphism without inheritance.

There are some disputes whether overloading and overriding is a part of polymorphism.
  • Wikipedia explains they are not the same. (Polymorphism is not the same as method overloading or method overriding.1 Polymorphism is only concerned with the application of specific implementations to an interface or a more generic base class.)
  • Some of OOP books explain "method overloading" as an example of polymorphism.
  • This paper explains overloading as a part of polymorphism
  • This post explains that overriding is a part of polymorphism, whereas overloading is not.

Generic programming is also known as "parametric polymorphism".

References

  1. Polymorphism in object-oriented programming
  2. OBJECT.__class__ is CLASS and "isinstance(OBJECT, CLASS)" for polymorphism in python
  3. Duck typing
  4. Function overloading
  5. Method Overriding
  6. Is Method Overloading considered polymorphism?
  7. Polymorphism vs Overriding vs Overloading