Getting Started

This is a step-by-step tutorial to demonstrate how to write agents in C# using the Neokernel.

Tutorial Page 1
Tutorial Page 2

Before You Continue

You need the following software to write, compile and run the Neokernel examples on this page:

Microsoft .NET framework

Microsoft Visual Studio.NET

Neokernel Web Server

To compile agents for the Neokernel, you must have the Neokernel namespace (e.g. the file neokernel.dll) included in the references for your project. The documentation has more information about compiling and debugging your agents.

Hello World!

This tutorial starts out with the classic "Hello, world!" example. The HelloWorldAgent is a simple example of an agent that follows Neokernel programming conventions. In Visual Studio .NET, create a new project of type "Class Library" and insert a new file named HelloWorldAgent.cs into the project. Then, type (or paste) the following source code into the file:

using Agent = com.neokernel.nk.Agent;
namespace HelloWorldAgent
{
/// <summary>
/// Everyone's favorite written as a Neokernel agent.
/// </summary>
public class HelloWorldAgent : Agent
{
public override void start()
{
println("Hello, World!");
}
}
}


The class HelloWorldAgent needs to extend a class called Agent found in the com.neokernel.nk package, so it includes an import statement for this class. The Agent class provides for a start() method where the agent will begin execution. The HelloWorldAgent overrides this with its own start method that prints the message "Hello, world!" to the console before exiting.

The println method is a convenience method provided by the Agent class that is used for reporting events to the console.

Compiling and Running Hello World!

Now that you have created the HelloWorldAgent.cs source file containing the source code listed above, the next task is compile it. The process of compiling checks your code for errors and then outputs a compiled .dll or executable file to the directory specified in the project settings. First, add the neokernel.dll file to your project references so the compiler can find the Neokernel libraries you are importing.

To import the Neokernel libraries into your project, follow these steps:
Right click on the References folder in your Solution Explorer window and select Add Reference.
In the window that appears, select the .NET tab.
Click the Browse button and navigate to the \lib directory where the file neokernel.dll is located.
Select the neokernel.dll file and click OK.

Now that you have imported the Neokernel .dll library, you are ready to build your project. Select Build Solution from the Build menu. The following picture shows the output from compiling HelloWorldAgent.cs within Visual Studio:


At this point, your HelloWorldAgent should be compiled. If you did not receive any errors after using the Build Solution command, you should find the file HelloWorldAgent.dll created by the compiler in your project's \bin\Debug\ directory.


If you don't see the HelloWorldAgent.dll file, it is probably because you have compiler errors. Read the error output carefully, and correct the problems in the HelloWorldAgent.cs file before attempting to Build Solution again. Don't worry if you get errors the first time -- compiling .NET code is sometimes a trial and error process.

Running the HelloWorld Agent

Now that you have compiled the HelloWorldAgent.dll, you are ready to run it. First, put a copy of the HelloWorldAgent.dll file in the same directory as the neokernel.exe exectuable file. Next, open a command prompt to that directory and type the command neokernel. You will see output similar to the following:

Congratulations, you have just built your first Neokernel agent! You are ready for the next part of the Tutorial.