Category Archives: AjSharp

First Ideas for a Claim Rule Engine

I am looking for applications of my scripting languages. I want to use AjLang, the core of AjSharp, written in C#. One use case could be a claim rule engine. To understand what I have in mind, and the federated security jargon/landscape, see:

The Role of the Claims Engine
The Role of the Claim Rule Language
The Role of Claim Rules
The Role of the Claims Pipeline
The Role of Claims

Given a set of input claims, the system should produce a set of output claims, using user defined rules. A rule could be written as:

rule RuleEmail
   description “….”
   claim cmail
   cmail in InputClaims
   cmail.Type == “email” 
   claim cname = new Claim(…. )
   // more cname processing, maybe extracting a value from email
end rule

Something like my example at but more oriented to C# syntax (i.e. the use of native .EndsWith in the above example)

If I had omitted cmail in InputClaims, the claim will be searched in all claim sets.

Additional item: to have a web interface to edit and browse rules.

The rules would be executed in order. I could write rule sets, to group rules to be executed. For example: Identity Provider rule set, to be applied to the claim set provider by an IP, i.e. Windows Live, Yahoo, etc. Or a Relying Party rule set, defined by application: accounting system, human resource system, finance, etc..

In the “then” part, additional functions could be call. The functions would be added by the developer to the rule engine environment, maybe with dynamic loading of a library. Then, the actions will be extensible by code. The “when” part could use additional predicates, too. But the base idea is to have an initial set of predicates and actions that cover the main use cases: testing property values, creating new claims, setting their property values, adding to a claim set (InputClaims, OutputClaims or any other set defined by the programmer).

One thing to decide: what happens if there are TWO or more claims that satisfy then when clause? the rule should fire only once? or n times? I remember the cut operator from Prolog, but I guess it could be simpler. I should explore the use cases.

No time allocated for these ideas, yet. But keep tuned!

Angel “Java” Lopez

Running AjSharp in Azure

My weekend code kata was something I was thinking since last year: run AjSharp in Azure Worker Roles. The idea is: a worker role instance can receives text via queue messages containing AjSharp code, and execute it. The output is send as a message to other queue.

The result was committed in my AjCodeKata project: you must download trunk\Azure\AzureAjSharp AND trunk\AjLanguage (where AjSharp projects reside).

The solution:

The projects:

AzureAjSharp.WorkerRole: sample worker role, with these lines added:

CloudStorageAccount account = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
Processor processor = new Processor(account);

Azure.AjSharp: the class library. It contains Processor class. The constructors need a cloud account and the names of: requests queue, responses queue and blob container. The request queue has messages with AjSharp code to execute. Response queue has the output text of such executions. The above processor.Start() command initiates the read and process of AjSharp code.

AzureAjSharp.Console: it reads lines from console, and when it reads a “send” line, the text is converted to a cloud message, sending it to the request queue. It has a thread that reads the response queue and prints the results.

AzureLibrary: auxiliar classes.

AjSharpVS2010, AjLanguageVS2010: AjSharp implementation.

When I run the console application, I can send AjSharp code to worker roles:

And more: AjSharp supports Include(“filetobeincluded”); where the file contains AjSharp code. I modified the launch of AjSharp machine to have an Include subroutine implementation that reads the content from a blob container.

A graph:

Then, I uploaded some simple code (the files are in Examples folder in Azure.AjSharp project) to ajsfiles blob container (DevStorage in this test):

(I’m using Neudesic Azure Storage Explorer, but I could use CloudBerry Explorer for Azure Storage: it supports folders in a tree).

This is the test running (using Include) HelloWorld.ajs, and ForOneToTen.ajs:

Next steps:

– Write more utilities in AjSharp, to be included if they are needed: file and directory utilities, download and upload of blobs, send and receive message using queues, broadcast messages to all worker instances, download and load of assemblies, etc. Sky is the limit! 😉

Then, you (or your program) can dinamically send tasks and receive results. Nice to have: Guids to identify tasks and their results; web interface; results stored as blob texts; cache (and flush) of included blob files, etc…

Keep tuned!

Angel “Java” Lopez

Server and Client in Distributed AjSharp

In my previous posts:

I wrote a brief description of what I want for distributed AjSharp (my open source interpreter), and few screenshots of running programs. Let examine and run a first concrete example.

As usual, you can launch the interactive interpreter with


You can add a list of .ajs program files to read and run inmediatly:

AjSharp.Console program1.ajs program2.ajs

The idea in this first distributed example is to launch two instances of the interpreter, and then, start a server in the first one, and start a client of that server in the second one. After connection is done, the client can execute commands in the server.

First, the server code:

server = new RemotingHostServer(10000, "Server");
PrintLine("Server started");

The built-in class RemotingHostServer starts a remoting server connection. The first parameter is the TCP port, and the second parameter the logic name of the server.

Now, the code for the client node:

remote = new RemotingHostClient("localhost", 10000, "Server");
at remote PrintLine("New Node");
nodeid = System.Guid.NewGuid().ToString();
// evaluate a subroutine at server
at remote 
		for (k=1; k<=10; k++)
			PrintLine("Hello, server, from node " + id);
	with (nodeid); // local parameter going to server

The object RemotingHostClient connects with a server. Its constructor takes three parameters: the machine name, the TCP port to use, and the logical name of the server. Using these parameters, it builds a remoting address, using a TCP channel.

Now, you have a connection with the server. The at command execute a command in a remote server. The syntax is:

at <server> <cmd>


at remote PrintLine(“New Node”);

prints a message in the server console, not in the client. The other variant:

at <server> <functionorsub> with (<parameters>);

evaluates a function or subroutine at the server, passing the parameter list, evaluated in the client environment.

I prepared a packaged example, you can download from my Skydrive:

In a console, execute


This command starts a new console, executing Server.ajs code.

To run a node


This is the typical output at the server side:

As usual, you can get the complete source from:

under trunk/AjLanguage. More distributed examples are comming: execute a local source code file at the server, do something at server when a new node is running, distributed agents, etc…

Keep tuned!

Angel “Java” Lopez

Memory transactions in AjSharp using References

Last year, I met Clojure, a Lisp dialect compiled to Java. I was coding AjSharpure (see AjLisp Family: Implementing Lisp Interpreters in C#), a Clojure-like interpreter (not compiler), in .NET (if you want a more complete implementation, there is a ClojureCLR, implemented in .NET compiled using DLR, Dynamic Language Runtime).

Clojure has many striking features, specially in persistent structures (“inmutable” list, dictionaries, trees…), concurrency, agents, and more. One feature is STM, software transactional memory. More about these topics in links at end of this post. According to:

In computer science, software transactional memory (STM) is a concurrency control mechanism analogous to database transactions for controlling access to shared memory in concurrent computing. It is an alternative to lock-based synchronization. A transaction in this context is a piece of code that executes a series of reads and writes to shared memory. These reads and writes logically occur at a single instant in time; intermediate states are not visible to other (successful) transactions.

Some weeks ago, I read:

Clojure concurrency Part 4

The article describe the behavior of “ref”, “dosync” and related operations. I wanted to experiment with such features, but in my AjSharp interpreter. So, I wrote a Reference class, and added transaction support to the language.

Now, I can write:

reference = new Reference();
reference <- 1;
result = @reference;

The Reference class is a built-in one, in AjLanguage. Using <- operator is equivalent to ref.SetValue(newvalue). Now, I added the @ operator to get the value, and it is equivalent to ref.GetValue() (Incidentally, GetValue and SetValue are the method of the interface for Channels and Futures, see:

AjSharp: Implementing Futures

Channels and Go Routines in AjSharp (Part 1)

Channels and Go Routines in AjSharp (Part 2)

In those posts, I still wrote <-channel or <-future to GET the value of a channel or future; now, I deprecated such notation, in favor of @channel, @future, that is, in my opinion, more clear).

I decided to make explicit the get/set of a reference, instead of make it transparent. But, what is the purpose? Well, now, I can use the new transaction keyword:

channel1 = new Channel();
end1 = new Channel();
end2 = new Channel();
reference1 = new Reference();
reference2 = new Reference();
reference1 <- 1;
reference2 <- 2;
go transaction 
  reference1 <- @channel1; 
  result2original = @reference2; 
  end1 <- @reference1; 
go transaction 
  channel1 <- @reference2; 
  reference2 <- @reference2 + 1; 
  result1original = @reference1; 
  end2 <- @reference2; 
result1 = @end1; // 2
result2 = @end2; // 3

The two transactions are launched via go command (read the above cited posts about go routines). The first transaction wait for a value from the second one, using the channel in variable channel1. The values in reference1, reference2 in first transactions are the original one, although the second transaction is changing them. That is, a transaction sees an snapshot of its reference values, taken at the beginning of its life. A change of a reference value during a transaction,  is committed at the end of the transaction, and then, it became visible to the rest of of application (but the still running transactions are viewing the snapshot value). If a transaction change a value altered by other concurrent transaction, an exception will be raised.

My implementation is a naive one. I guess I could improve its efficiency. But now, all tests are in green, and it’s good enough for my experiments. Internally, each reference has a current visible value, an updated value (if exists) corresponding only to one or zero transaction, and a list of snapshot value, only to feed still running transaction. A Machine instance (associated by running thread) keeps a list of reference with snapshots, and a list of still running transactions.

My links:

As usual, you can download the code from:

under trunk/AjLanguage.

Keep tuned!

Angel “Java” Lopez

Distributed AjSharp: a roadmap

Last weekend, I added distributed features to my open source interpreter AjSharp. I wrote about them in:

Distributed AjSharp: First Steps

In these days, I wrote more code to support distributed running of an AjSharp application. It’s a work in progress. In this post, I want to write down the list of features that I like to have (some of these points are implemented in the trunk, but I should review, test and post about them):

– Start a local host in an address

– Connect to a remote host by address

– Connect to a remote host and send the address of another (local or remote) host (register the new host in the remote host)

– Execute a local callback when a remote host sends a the address of a host to my local host (OnRegisterHost).

– Send a local defined command to a remote host and execute it in the remote host.

– Send a local defined expression to a remote host and evaluate it in the remote host, returning the new value.

– Send a local defined function, subroutine, to a remote host, send its parameters, and evaluate the function/subroutine in the remote host.

– Send a text to a remote host, parse and execute it as a command.

– Send a text to a remote host, parse and evaluate it as an expression, returning a value.

– Send the local file content to a remote host, parse and execute it.

– Get a list of local host register in the current machine

– Get a list of remote host in the current machine

– Set a remote variable, sending a local evaluated value

– Send a copy of a DynamicObject to remote host (I should resolve what happens when the DynamicObject has an associated class)

– Send an object reference of a DynamicObject to remote host, to use there as a proxy to the original object still residing in local host.

– Send a copy of an agent to a remote host, and start it.

– Export a local file with source code to the remote host, to use in Include(“…”) commands to execute in remote host.

– Export a local assembly to remote host (I should decide if I need this feature)

Machine is a base object that keeps the global environment (Machine.Current.Environment), with the name/values of global variables, class definitions, global functions, etc. Host is the object that exposes a machine to other hosts.

Another point:

– A proof-of-concept sample application: a distributed web crawler, or a distributed genetic algorithm.

Add all these features to go routines, futures, channels, and agents, and we have an interesting soup.

Keep tuned!

Angel “Java” Lopez

Distributed AjSharp: First steps

It was a great code kata this weekend. At Saturday afternoon, I started to play with serialization of commands in my AjSharp interpreter:

It was using Windows Communication Foundation, simple BasicHttpBindind and address. I choose to serialize commands and expressions in byte arrays using BinaryFormatter, and send them via basic WCF (I don’t like to struggle with WCF serialization).

Then, yesterday (sunday) I write an implementation using remoting. This is a better approach in this situation: remoting out-of-the-box support for graph serialization, and management of MarshalByRefObject, is good for my project.

My first attemps using remoting:

Note the use of new keywords expression and command. This sentence:

a = expression b+c;

assign a to-be-evaluate expression b+c to local variable a. You can evaluate with:

result = a.Evaluate(Machine.Environment);

(Machine is the current running machine, with its global .Environment, where there are the bindings for b and c).

The command:

a = command PrintLine(“Hello, world”);

doesn’t print a message. It store in local variable a command, to be execute with:


In the above example, yYou can start a server programmatically. And you can create a client proxy to a server. Then, you can invoke commands, or evaluate expressions in the other server. After then, I added call functions, subs, in the other server, passing arguments from the local run. The main interface is IHost:

    public interface IHost
        // Host Id
        Guid Id { get; }
        // Host Invocation
        void Execute(ICommand command);
        object Evaluate(IExpression expression);
        object Invoke(ICallable function, params object[] arguments);
        // Others... Work in progress... to review

A host runs a machine, and expose access to invocation and evaluation via its interface. There are local host (class Host), or hosts that expose its functionality via remoting (RemotingHostServer) and the corresponding client proxy (RemotingHostClient). I’m working in WCF too: there is a WcfHostServer and WcfHostClient, but I should struggle with serialization issues, yet.

I should add passing stirng as command/expression (it could be an easier way to invoke the other part), so the server could parse them and execute/evaluate.

After mi initial successful attempts, I added syntax sugar, it’s nice to have at as new keyword in AjSharp:

You can invoke a command in other server:

at <host> <command>;

or you can evaluate an expression in other server:

localvar = at <host> <expression>;

I should work more in serialization. I resolved part of the callback to the server. That is:

at host { adam = new DynamicObject(); adam.Name = “Adam”; adam.Age = 800; }

myadam = at host adam;

adam is created in the remote host. myadam, using remoting, is a transparent proxy to a DynamicObject still resident at host (My decision was that the dynamic objects are MarshalByRefObject; I could change this selection in the future: make dynamic objects serializable by default, and add obj.AsProxy() to generate a MarshalByRefObject explicitly). But the returned transparent proxy cannot be invoked directly: it has no associated communication channel. So, I added some wrapper to it at client side. If something is invoked in myadam, as in:


myadam.Name = “NewAdam”;

the server object is invoked, and the operation is executed at server.

As usual, you can download the code from:

under trunk/AjLanguage.

I’m proud of these advances in my interpreter. It was only an idea, but now, a distributed language was born. It’s a baby, but it MY baby, and it was fun to code all this.

Keep tuned!

Angel “Java” Lopez

Scripting WatiN using AjSharp

AjSharp is an interpreter I wrote using C#. It’s a dynamic language, with dynamic classes and objects, functional values, channels and goroutines, agents-alike entities, access to native .NET object. It was create as a derivative of my previous work on AjBasic, the dynamic language used in AjGenesis, code generation engine. My plan is to unify both language using AjLanguage core, and add them to AjGenesis, allowing other template language to be used as plugins. But all that is still only a plan.

The code of this post is in the trunk of my AjCodeKatas Google Project:

under trunk/AjLanguage/Src/AjSharp.Examples

One of the objetives in the design of AjSharp, is to serve as a light shell to invoke .NET libraries. As a proof of concept, I wrote a simple class to access WatiN, the web automation test library. This is the simple class I’m using:

class Wat
  var Browser;
  function Wat()
    WatiN.Core.Settings.WaitForCompleteTimeOut = 480;
    this.Browser = new WatiN.Core.IE();
  sub Browse(url)
  function Button(name)
    button = this.Browser.Button(name);
    if (button.Exists)
      return button;
    button = this.Browser.Button(WatiN.Core.Find.ByName(name));
    if (button.Exists)
      return button;
    button = this.Browser.Button(WatiN.Core.Find.ByValue(name));
    if (button.Exists)
      return button;
    return null;
  function TextField(name)
    textfield = this.Browser.TextField(name);
    if (textfield.Exists)
      return textfield;
    textfield = this.Browser.TextField(WatiN.Core.Find.ByName(name));
    if (textfield.Exists)
      return textfield;
    return null;
  function Link(name)
    link = this.Browser.Link(name);
    if (link.Exists)
      return link;
    link = this.Browser.Link(WatiN.Core.Find.ByUrl(name));
    return link;
    if (link.Exists)
      return link;
    return null;
  function Contains(text)
    return this.Browser.ContainsText(text);
  sub Close()

You can extend it to support other features of WantiN browser. An example of use:

wat = new Wat();
wat.TextField("q").TypeText("Angel Java Lopez");
wat.Button("Google Search").Click();
Assert(wat.Contains(""), "No content");
Assert(wat.Contains("Java"), "No content");
Assert(wat.Link("").Exists, "No link 4");
Assert(wat.Contains("Ciencia"), "No content 2");

The only problem I had to invoke WatiN, it was its requirement of run inside an STAThread. So, I changed the main method of AjSharp.Console, adding STAThread attribute to it. I’m using this kind of script to test a real web application that is being developed in an agile team. I could add support for exceptions, and logging of messages.

You have more robust utilities to use over WatiN, like Wax, that enables to write tests using Excel spreedsheets. But now, the above scripting core is good enough to the simple initial tests we are using, and it was a good exercise to test AjSharp abilities in a real requirement.

Stay tuned!

Angel “Java” Lopez

Web Crawler using Agents and AjSharp

Last year, I wrote a web crawler example using messages, details in post:

Distributed Web Crawler using AjMessages

Before that, I wrote other examples, using DSS/CCR, included in Microsoft Robotics Developer Studio:

Distributed Agents using DSS/VPL
Web Crawler example using DSS (Decentralized Software Services)

Now, I wrote a local (not distributed) example, using agents in AjSharp. Remember, each agent runs in its own thread, and its invocation are queued, so they are execute one by one. More about agents in AjSharp:

Agents in AjSharp (Part 1)
Agents in AjSharp (Part 2)

Yesterday, I took code from my previous examples, and rearrange it in AjSharp agents. This is the result. First, an object to build an agent network, and launch the process:

// Build and launch agents
object WebCrawler
  sub Process(url, fn)
    uri = new System.Uri(url);
    downloader = new Downloader();
    harvester = new Harvester();
    resolver = new Resolver(uri,5);
    processor = new Processor(fn);
    downloader.Harvester = harvester;
    downloader.Processor = processor;
    harvester.Resolver = resolver;
    resolver.Downloader = downloader;
    downloader.Process(uri, 0);

The downloader takes an URI, download its content, and send it to two associated agents: Processor and Harvester. Depth parameter is used later, indicating the depth of this URI in the crawling process:

// Downloads a page
agent Downloader 
  sub Process(uri,depth)
    client = new System.Net.WebClient();
    content = client.DownloadString(uri);
    PrintLine("Downloaded: " + uri);
    this.Processor.Process(uri, content);

The Processor executes a user function/routine, receiving the URI and its retrieved content:

// Process the content retrieved
agent Processor
  function Processor(fn)
    this.fn = fn; // function to invoke
  sub Process(uri, content)
    // Add your logic
    this.fn(uri, content);

The Harvester detects other links in content, and send them, one by one, to a new agent, the Resolver:

// Get links from page
agent Harvester
  sub Process(uri,depth,content)
    matches = System.Text.RegularExpressions.Regex.Matches(content, "href=\\s*\"([^&\"]*)\"");
    results = new List();
    foreach (match in matches) {
      value = match.Groups[1].Value;
      if (!results.Contains(value))
    foreach (result in results) 
      if (result.StartsWith("http"))
        this.Resolver.Process(new System.Uri(result), depth+1);

The Resolver keeps a list of processed URIs, and filter those ones that are not belong to original host (I keep the webcrawling process in the site of the first URI):

// Filter invalid or already processed links
agent Resolver
  var processed = new List();  
  function Resolver(uri,maxdepth)
  { = uri.Host;
    this.maxdepth = maxdepth;
  sub Process(uri,depth) 
    if (depth > this.maxdepth)
    if (uri.Host !=
    if (uri.Scheme != System.Uri.UriSchemeHttp && uri.Scheme != System.Uri.UriSchemeHttps)
    if (processed.Contains(uri))
    PrintLine("New Link: " + uri);

Finally, example of use, creating and launching two agent sets dedicated to webcrawling:

// Example
WebCrawler.Process("", function(uri,content) { PrintLine("From ajlopez.wordpress "+uri);});
WebCrawler.Process("", function(uri,content) { PrintLine("From ajlopez.zoomblog "+uri);});

You can download AjSharp from trunk:

The code is in Examples/WebCrawler.ajs in AjSharp.Console project. After compiling it, you can run the web crawling using command line:

AjSharp.Console Examples/WebCrawler.ajs

Partial ouput:

You can see the example with beautiful formatting at Pastie

Next step: use distributed agents. There is two ways to explore: one, declare some agents as distributed, via additional code, or configuration, without changing the original code; two, write distribution all explicit in code.

Keep tuned!

Angel “Java” Lopez

Variable-Length arguments in AjSharp

In my experiments with agents, using AjSharp interpreter, I found that I missed one feature: variable number of arguments. So, back to quick coding, test-driven-dev hat on, and today, during a light breakfast, I wrote the code to support such feature.

I had to implement two facets of the feature. First, the declaration of the variable length argument:

function FirstParameter(pars...)
	if (pars.Length>0)
		return pars[0];
	return null;
function SecondParameter(pars...)
	if (pars.Length>1)
		return pars[1];
	return null;
result = FirstParameter(1,2,3);		// 1
result2 = SecondParameter(1,2,3);	// 2
result3 = SecondParameter();		// null

The other side, what if I want to use the received pars… as the parameters to give to another functions? The received pars… is an array of objects. I use the same … (three points) operator, but now, in the invocation side:

function FirstParameter(pars...)
	if (pars.Length>0)
		return pars[0];
	return null;
function SecondParameter(pars...)
	if (pars.Length>1)
		return pars[1];
	return null;
function JustDoIt(fn,pars...)
	return fn(pars...);
result = JustDoIt(FirstParameter,1,2,3);		// 1
result2 = JustDoIt(SecondParameter,1,2,3);	// 2
result3 = JustDoIt(SecondParameter);		// null

Curiously, two days ago, I found the language


that compiles to normal JavaScript. In that page, I found a term new for me: Splats for the operator … (three points). I decided to use that operator some weeks ago, I can’t remember what was the source of such idea.

I should employ this feature in some agent example.

Keep tuned!

Angel "Java" Lopez

Agents in AjSharp (Part 2)

I implemented an agent-like class in my open source AjSharp interpreter. In the previous post:

Agentis in AjSharp (Part 1)

I described the implementation and main features. Two points to remember:

– Each agent runs in its own thread
– Each agent has a queue to receive its invocations (method name and arguments)

I want to show two simple examples of its use. First, agents can be chained, implementing a message-passing-like way of process.

agent IncrementAgent 
  function IncrementAgent(next, channel)
  { = channel; = next;
  sub Process(value)
    newvalue = value + 1;
    if (channel)
      channel <- newvalue;
    if (next)
channel = new Channel();
thirdAgent = new IncrementAgent(null, channel);
secondAgent = new IncrementAgent(thirdAgent, channel);
firstAgent = new IncrementAgent(secondAgent, channel);
for (k = 1; k <= 3; k++)
  result = result + <-channel; 
// result == 1 + 2 + 3 == 6

Note that each agent is an element of a chain of agents, and the use of a channel to collect the results. Agents can be combined in a more complex way.

I implemented the Collatz problem using a round of two agents:

if (number % 2)
      this.Next.Process(number, list);
    number = number / 2;
    this.Next.Process(number, list);
agent OddAgent 
  sub Process(number, list)
    if ((number % 2)==0)
      this.Next.Process(number, list);
    if (number == 1) 
      this.Channel <- list;
    number = number * 3 + 1;
    this.Next.Process(number, list);
channel = new Channel();
even = new EvenAgent();
odd = new OddAgent();
even.Channel = channel;
even.Next = odd;
odd.Channel = channel;
odd.Next = even;
even.Process(22, new List());
result = <-channel;
// result = { 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 }

More about Collatz conjeture:

Collatz 3n+1 Problem Structure

Programming 3n+1 problem

These are simple examples. I want to implement a web crawler, a web scrapping, or a parallel genetic algorithm, using agents. And the next step: distributed agents.

Keep tuned!

Angel “Java” Lopez