Sunday, 22 June 2008
Day 14 - Finished the painting
Posted by
mue
at
9:18 PM
0
Comments
Link
Saturday, 14 June 2008
Day 6 - Time for something complete different
(define (insert customer: Customer into-database: Database)
(let ((PCustomer (prepare customer: Customer))
(OpenDatabase (open database: Database)))
(...)))
(map fun: (|item: Item| (print Item)) on-list: MyList)
Wednesday, 14 May 2008
Erlang development progress
{ok, Reply} = cellmb:request(Ctx, configuration, read, {my_app, my_group, path, to, my, var})my_cfg:read(Ctx, {my_app, my_group, path, to, my, var}, State).{ok, Reply} = cellmb:request(Ctx, three, do_something, Args)
Posted by
mue
at
3:12 PM
0
Comments
Link
Labels: erlang
Saturday, 26 April 2008
Erlang for the OO-minded
public class Adder {
private int a, b;
public void setA(int anA) {
a = anA
}
public void setB(int aB) {
b = aB
}
public int result() {
return a + b
}
}Adder myAdder = new Adder();
myAdder.setA(1);
myAdder.setB(2);
System.out.println(myAdder.result());
create() ->
{0, 0}.
set_a(A, {_oldA, B}) ->
{A, B}.
set_b(B, {A, _oldB}) ->
{A, B}.
result({A, B}) ->
A + B.
A1 = adder:create(),
A2 = adder:set_a(1, A1),
A3 = adder:set_b(2, A2),
io:format("~w", [adder:result(A3)]).
create() ->
spawn(?MODULE, loop, [0, 0]).
set_a(Pid, A) ->
Pid ! {set_a, A}.
set_b(Pid, B) ->
Pid ! {set_b, B}.
result(Pid) ->
Pid ! {result, self()},
receive
{response, Value} -> Value
end.
loop(A, B) ->
receive
{set_a, newA} ->
loop(newA, B);
{set_b, newB} ->
loop(A, newB);
{result, Pid} ->
Pid ! {response, A + B}
loop(A, B)
end.
Pid = adder2.create(),
adder2:set_a(Pid, 1),
adder2:set_b(Pid, 2),
io:format("~w", [adder2:result(Pid)]).

create(Node) ->
spawn(Node, ?MODULE, loop, [0, 0]).
Pid = adder2.create(my_node@my-server.in.my.net)
-module(adder).
-export([add/1]).
add(List) ->
add(List, 0).
add([Head|Tail], Acc) ->
add(Tail, Acc + Head);
add([], Acc) ->
Acc.
string_append(String, Float) when is_float(Float) ->
...;
string_append(String, Integer) when is_integer(Integer) ->
...;
string_append(String, Tuple) when is_tuple(Tuple) ->
...
loop(State) ->
receive
{withdraw, Amount, Account, Lo, Hi} when Amount =< Lo ->
% Perform a standard withdraw.
...;
{withdraw, Amount, Account, Lo, Hi} when Amount > Hi ->
% Perform a special customer approval before the withdraw.
...;
{withdraw, Amount, Account, Lo, Hi} ->
% Perform a simple customer approval for withdrawals between lo and hi.
...
end.
-module(server).
-export([start/2, stop/1, call/2]).
start(Module, Args) ->
% Call init/1 in Module.
% It has to return an initial state.
State = Module:init(Args),
spawn(?MODULE, loop, [Module, State]).
stop(Pid) ->
Pid ! stop,
ok.
call(Pid, Msg) ->
Pid ! {call, Msg, self()},
receive
{response, Value} -> Value
end.
loop(Module, State) ->
receive
{call, Msg, Pid} ->
% Call function handle/2 in Module.
% It has to return {Value, NewState}.
{Value, NewState} = Module:handle(Msg, State),
Pid ! {response, Value},
loop(Module, NewState);
stop ->
% Call function terminate/1 in Module.
Module:terminate(State)
end.
-module(account_server).
-export([init/1, handle/2, terminate/1]).
init(Args) ->
% Create an initial state, e.g. a database connection.
...
handle({open, Account}, State) ->
...,
{0, NewState};
handle({withdraw, Amount, Account}, State) ->
...,
{Balance, NewState};
handle({deposit, Amount, Account}, State) ->
...,
{Balance, NewState};
handle({balance, Account}, State) ->
...,
{Balance, NewState}.
terminate(State) ->
...,
ok.
Pid = server:start(account_server, DatabaseName),
server:call(Pid, {open, 4711}),
server:call(Pid, {deposit, 1000.0, 4711}),
server:call(Pid, {withdraw, 250.0, 4711}),
Balance = server:call(Pid, {balance, 4711}),
% Balance now should be 750.0.
server:stop(Pid).
Posted by
mue
at
11:36 PM
2
Comments
Link
Labels: erlang
Friday, 18 April 2008
Exciting days
Posted by
mue
at
4:03 PM
0
Comments
Link
Monday, 7 April 2008
Ideas for an Erlang Object System
ObjRef = eos:new(my_module) or ObjRef = eos:new(my_module, Args) would create an instance as a new linked process. The initialization could use my_module:new(Args, InitialState). The initial state would be the result of a recursive initialization through calling my_module:parent_module() and initializing those modules. This behaviour, calling parent_module/0 until it returns undefined or doesn't exist in a module, would the basis for inheritence.Result = eos:invoke(ObjRef, my_method, Args) would lead to the call of my_module:my_method(Args, State) and has to be answered with Result or {Result, NewState}. The function invoke/3 would look for the function inside the module and, if it isn't exported in that module, recursively in the parent modules. If it can't be found it would try to invoke does_not_understand/2 the same way. If even this function can't be found the system should raise an error.eos:dispose(ObjRef) or automatically using the typical Erlang mechanism of linked processes and their notification. Alltogether this system is really simple and it definitely doesn't compete with the standard OO languages. But it may help some experienced OO developers to feel more homelike in the Erlang world. What do you say?
Posted by
mue
at
5:20 PM
0
Comments
Link
Labels: erlang
Sunday, 6 April 2008
COP - The better way of OOP?
my_object(State) ->
receive
{method_a, Arg1, Arg2} ->
NewState = do_method_a(State, Arg1, Arg2),
my_object(NewState);
{method_b, Arg1} ->
NewState = do_method_b(State, Arg1),
my_object(NewState);
{'EXIT', Pid, Reason} ->
NewState = handle_exit(Pid, Reason),
my_object(NewState)
end.
cellmb:subscribe(service_name, my_service_module)
cellmb:publish(Ctx, service_name, do_something, Args)
my_service_module:do_something(Ctx, Args, State).
Posted by
mue
at
12:00 AM
2
Comments
Link
Labels: erlang
Saturday, 16 February 2008
Erlang guards
Posted by
mue
at
10:52 PM
0
Comments
Link
Labels: erlang
Saturday, 9 February 2008
Experiences made with Erlang while developing the CEL
- the CELSML for parsing documents in the Simple Markup Language,
- the CELSTH to convert SML into HTML,
- the CELETM for execution time monitoring,
- some helpful utility functions in CELUTL, and
- the Lightweight Message Bus CELLMB.
Posted by
mue
at
12:28 AM
0
Comments
Link
Labels: erlang
Thursday, 24 January 2008
Lightweight Message Bus development and test progress
Posted by
mue
at
11:03 PM
0
Comments
Link
Sunday, 20 January 2008
Ah, I've got it
Posted by
mue
at
9:37 PM
0
Comments
Link
Labels: erlang
Thursday, 10 January 2008
Success through simplicity
The first tests run fine and I will now add more complex test scenarios with a higher number of subscribing services and parallel calls. So I'll get a nice reliable infrastructure to run my business components.
Posted by
mue
at
10:16 PM
0
Comments
Link
Labels: erlang
Wednesday, 2 January 2008
Making it right
The result has been a runtime of 1.6 seconds on my MacBook and 6 seconds on his notebook. He then tried different algorithms, all of them with even worse results. His approaches all based on the Sieve of Erastothenes, which relies on creating new lists through filtering. So his algorithms traversed the lists over and over again, filtering the elements through fun expressions and creating the result lists through appending the elements.
Knowing about Erlangs concept of linked lists, the resulting performance problems when appending data at the end, and the worse performance of fun expressions compared to functions the result isn't astoundingly. So what's the bottom line? For a given problem, regardless if it shall be solved functional, object-oriented, logical, sequential or concurrent, local or distributed, you've got to know the boundaries of your tool and your environment to make it right. In this case, where he was learning the language, it has been OK. But violating constraints in real-world solutions often leads to huge problems when the system is productive.
And how to choose the right tool for a given problem, that's another topic I'll treat later. *smile*
Posted by
mue
at
11:44 PM
0
Comments
Link
Labels: erlang
Tuesday, 13 November 2007
Lightweight Message Bus
The CELLMB now uses this pool for the internal dispatching of messages with a given context, verb, and noun to subscribed processes. Those can be single and pooled processes using the CELSPP. For a simple development of those processes the module CELLMBSVC provides a generic behaviour. Just the callbacks init, handle and terminate have to be implemented - of course beside possibly needed helper functions.
The next step will be the implementation of the unit tests for the CELLMB and then the first services for my Erlang Business Library (EBL) which I'll use in my different portal projects. Those will handle configuration, authentication, authorization, client management, user management, and addresses based on vCards. They all will use Mnesia as their persistency backend and will be loose-coupled using the CELLMB for communication.
Posted by
mue
at
9:29 AM
0
Comments
Link
Labels: erlang, software architecture
Saturday, 27 October 2007
Ending the silence
During this time I've mostly been busy starting a larger project at my employer. My role here is the system architect and I'm also take part in rolling out the requirement engineering together with the standards definition and tool evaluation. We decided us for the IRqA, a really nice tool, especially the next release. Visure gave us a little preview. The tool is more powerful than DOORS, cheaper, and the service is really, really good. So now we're on our way, analysing our customers requirements and writing fine use cases. *smile*
A large part of my time at home went into my current article about continuations. Even if it is a small article it had cost a lot of work. I've had much troubles to find the right words to demonstrate this technique in a practical way. But know it is finished and delivered and I can concentrate on my next article about Erlang.
Beside doing serious things I've also wasted some time playing around with my new gadget. My new mobile phone should be a smartphone. And after comparing several models trying to find out what exactly I want I've decide for the Nokia E61i. It's a real nice device, not too large, pwerful, and playing wonderful together with my Mac - I've allready installed a neat OS X theme - and some WLANs where I've tried it.So now I can go on developing in Erlang and write about it - here and in my next article. This system still fascinates me and I see a very large potential for future applications. Even if it is not Erlang itself its technology will influence many others. The current discussions on the Squeak lists demonstrate this allready.
Posted by
mue
at
10:52 PM
0
Comments
Link
Labels: erlang, misc, requirements
Sunday, 7 October 2007
Comments wanted
Posted by
mue
at
6:15 PM
0
Comments
Link
Labels: erlang
Sunday, 30 September 2007
Small features leading to more reliabilty
Measuring = celetm:begin_measuring({?MODULE, my_block}),
do_this(),
do_that(),
celetm:end_measuring(Measuring).
celetm:measure({?MODULE, my_func}, fun() -> my_func() end).celetm:retrieve() or displayed in a table in the shell with celetm:io(). Options for sorting and filtering or output to an i/o device exist. So what's the special feature here? My old solutions allways had two central tables, one as a buffer for collecting the single measurings, one for the accumulated measurings. The access to both have been controlled by synchronize blocks or semaphores. With a high load calling processes, e.g. in a server, would have to wait, a typical bottleneck. The operation here is fast enough, so I never had this problem. But it's easy to imagine similar situations with longer lasting central functions. The Erlang solution uses a gen_server as a backend, and celetm:end_measuring() is an asynchronous cast message. So it's a fire-and-forget for the calling process. The only synchronous functions are celetm:retrieve() and celetm:io(). So only they would have to wait. And thanks to the Erlang messaging the implementation has been a really easy job with the shortest code ever.
Posted by
mue
at
11:21 PM
0
Comments
Link
Labels: erlang
Thursday, 27 September 2007
Future plans
The first project I'll realize in Erlang is the above mentioned CEL. I've got such libraries for almost every language I use as a stack of useful features I need. You allready read about the simple markup language (CELSML), which is a library for the creation, parsing, searching and later manipulation of SML documents, which is a lightweighted alternative to XML. CELSTH is based on this library and contains an extensible SML-to-HTML converter. Another helpful library is the execution time monitor CELETM, I've also implemented this in almost every environment I use. *smile* It allows simple execution time measurings and accumulation of logical blocks. The biggest part of the CEL will be the lighweight message bus CELLMB. It will provide the asynchronous distribution of messages to registered processes, realized using the publish/subscribe paradigm and based on a context, a verb, a noun, and optional metadata. So a request check permission sent in a context authorization will be answered by a registered authorization service process (it may catch all verb-noun combinations or they may be splitted to different processes, this depends on the individual needs), but also listened and logged by an audit service process. So flexible loose coupled architectures are possible. They benefit very much from the Erlang features (processes, messaging, distribution). Last currently known part of the CEL is a set of utilities in the CELUTL module. It provides a ping/pong to monitor processes or a GUID generator.
The first greater application developed simultaneous to the CEL is the Dynamic Content Processor (DCP) Release 2. This is a small content management system containing a blog and some more dynamic features I need. It will be based on Yaws and Mnesia and replace the software behind this blog, www.tideland.biz, and some wikis like wiki.tideland.biz. Here I'll see how Erlang is the right tool for my future projects. Two planned ones - currently I'm compiling the requirements - are the Train of Thoughts (TOT) and a portal for the collection, searching, viewing, and comparing whisky tasing notes. All three, the DCP, TOT, and the whisky portal, need a reliable platform.
Posted by
mue
at
8:09 PM
0
Comments
Link
Labels: erlang
Tuesday, 25 September 2007
Power of Erlang pattern matching
{node, Tag, Qualifier, Childs}{text, Data}FUNCTION node_matches(Test, Tag, Qualifier)
IF Test HAS TYPE node THEN
IF (Test.Tag EQUALS Tag) OR (Tag EQUALS "*") THEN
IF (Test.Qualifier EQUALS Qualifier) OR (Qualifier EQUALS "*") THEN
RETURN true
END
END
END
RETURN false
END
node_matches({node, Tag, Qualifier, _Childs}, Tag, Qualifier) ->
true;
node_matches({node, Tag, _Qualifier, _Childs}, Tag, ?WILDCARD) ->
true;
node_matches({node, _Tag, Qualifier, _Childs}, ?WILDCARD, Qualifier) ->
true;
node_matches({node, _Tag, _Qualifier, _Childs}, ?WILDCARD, ?WILDCARD) ->
true;
node_matches(_Node, _Tag, _Qualifier) ->
false.
Posted by
mue
at
11:11 PM
0
Comments
Link
Labels: erlang
Monday, 24 September 2007
Erlang development continued
I've started with the simple markup language, SML. I allready wrote about it. It is a really nice approach how the parser reads the document and sends events in form of messages to a background builder process. And the last message returns the constructed document. In the standard library this is a structure almost like the XML DOM, but much more lightweighted. It really astonished me how easy it is to parralelize typical serial problems.
The next step has been the HTML builder. It uses the same approach like above to produce HTML. No full blown HTML, but fragments to be inserted into templates. It shall be used in the DCP. This library is part of the CEL and can be extended with a callback function. This allows the DCP - or other apps - to extend the SML-to-HTML converter easily.
Beside the processes and the asynchronous messages the pattern matching is really helping. It makes the receiving of the messages and the definition of small and neat handling and poweful callback functions simple.
I'll now continue to develop the converter together with some more unit tests. Once again they help a lot. After that I'll develop something similar to my Smalltalk Lightweight Application Server, based on the Erlang processes and asynchronous messaging. So again, stay tuned.
Posted by
mue
at
9:06 PM
0
Comments
Link
Labels: erlang