Chapter 3. Edit - Compile - Run

While the instruction executer can be handy for quickly testing short pieces of code, it is of course not suited to create real applications. So, let's see now how the usual cycle of coding-compiling-executing-debugging works in Obix.

To keep things simple, we stay with our "Hello world!" example. But this time we will create the source code file that contains the instruction, call the compiler and then execute the code.

As we simply want to execute one instruction, the easiest way to do this is by creating a service with one command that contains the instruction.

Obix source code is stored as plain text in files. Each file contains one root software element (RSE), which is a type, a factory or a service. The file name must be the same as the RSE's prefixed identifier, and the file name extension must be osc. Hence, type employee would be saved in file ty_employee.osc, a factory implementing this type could be saved in file fa_employee.osc, and so on. For more information about source code files please refer to the section called “Source code files” in the programming manual.

Source code can be created with any text file editor. By default, Obix calls Emacs on Linux and Unix systems, and Notepad on Windows systems. At the end of this chapter is a note about how to change these settings so you can use your preferred editor.

To create a new source code object, select Source code / Create new ... from the main menu. After the text editor opens, enter the following source code:

service tutorial_examples

   command display_hello
      script
         se_console.co_message ( "Hello world!" )
      end script
   end command

end service
[Note]Note

Some elements are always optional in Obix source code:

  • Writing the prefix of a prefixed identifier is optional. However, if ambiguity arises then the compiler generates an error and forces us to prefix the identifier. To increase understandability it is often better to prefix identifiers, even if they are not required. For example, instead of writing:

    console.message ( "Hello world!" )

    code becomes more understandable with prefixes:

    se_console.co_message ( "Hello world!" )

    because now it is clear that console is a service (se_), and not a variable, for example, and that message is a service command (co_).

    For more information about prefixed identifiers please refer to the section called “Prefixed identifier”

  • Repeating the element name after keyword end is optional for every element in a script (e.g. script and end script are both allowed).

  • White space (i.e. spaces and tabs) is optional too

Hence, the above script could also be written more concisely like this:

service tutorial_examples

   command display_hello
      script
         console.message("Hello world!")
      end
   end

end

Save the text in file se_tutorial_examples.osc into subdirectory programming/source_code/li_explore of Obix's root directory (e.g. /usr/local/obix/programming/source_code/li_explore/se_tutorial_examples.osc on Linux, and c:\program files\obix\programming\source_code\li_explore\se_tutorial_examples.osc on Windows)

Now compile the source code by selecting Compiler / Compile from the menu. If no error is detected then the system console displays:

In case of a coding error, a popup window like the following appears:

and the list of errors detected by the compiler is displayed on the system console.

For example, the following error message is displayed if we forget to close the string with ":

[Note]Note

The | symbol displayed at the end of the instruction indicates the position of the error. This error position indicator is especially useful in longer instructions and helps to quickly locate the error.

The last step is to tell Obix to execute our service command when we launch the application. This is done by modifying another service command that exists already and that was created when Obix was installed on your system: command start of service explore.

Select Source code / Open ... from the menu. Then open file li_explore/se_explore.osc. Modify command start by adding the instruction se_tutorial_examples.co_start (or tutorial_examples.start if you prefer). The code should look like this:

service explore

   command start
      script
         se_tutorial_examples.co_display_hello
      end script
   end command

end service

Save the file.

We have to compile again, because source code has been changed. Select Compiler / Compile all and run from the menu. The run command implicitly calls se_explore.co_start.

The system console displays the result:

[Note]Note

You can change the editor used for creating source code by modifying file mini_IDE_config.xml which is located in subdirectory system of Obix's root directory (e.g. /usr/local/obix/system/mini_IDE_config.xml on Linux and Unix, and c:\program files\obix\system\mini_IDE_config.xml on Windows). As the name suggests it, mini_IDE_config.xml is an XML file containing the miniIDE's parameters. The content of this file is self-explanatory, and most parameters are followed by a <note> tag that explains the role of the parameter. Caution: Currently, this is the only way for changing parameters: by manually modifying an XML file! Hence care should be taken not to corrupt the XML file. Configuration will be more user-friendly and robust in a future version.

Instead of creating and opening .osc files through Obix's menu, you can of course also manipulate these files by using your operating system's file manager.

[Note]Note

If your editor has a configurable text highlighting feature (different colors for keywords, literals, comments and other text), then you may want to copy a list of Obix's keywords to the system clipboard and then import them into your editor. You can do this by selecting Utilities / Keywords to clipboard ... from the menu.

[Note]Note

There is currently no built-in support for automatic deployment. Obix simply creates Java .class files in directory obix/programming/default_app_/java/build/web/WEB-INF/classes. These files can manually be integrated like any other Java .class files (e.g. compressed in a .jar file) and they can be run on any system with a Java Virtual Machine (JVM).

Now we know how to create, compile and execute programs.

if you_are_tired_now then
   switch_off_your_computer
   relax
else
   lets_do_more_useful_stuff_and_discover_new_exiting_ways_of_programming
end if