Hello World in Kompics Scala¶
As we did in section Hello World in Kompics, we will start introducing the Kompics Scala DSL by giving a Hello World example.
SBT Setup¶
Set up a new SBT project and follow the instructions in Repositories and Getting Started to include the necessary dependencies. For this simple example no additional Java API modules will be needed. We are, however, adding logback classic to the dependencies, in order to get some logging output from Kompics. An example logback configuration file can be found in the full download package below.
The resulting helloworld.sbt
might look something like:
name := "Hello World"
organization := "se.sics.test"
version := "1.1"
scalaVersion := "2.12.8"
resolvers += Resolver.bintrayRepo("kompics", "Maven")
libraryDependencies += "se.sics.kompics" %% "kompics-scala" % "1.1.0"
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.2.3"
Hello World Component¶
As in the Java version we only have a very simple component HelloWorldC
whose sole handler reacts to Start
events on the control port (which is called ctrl
in Scala). Notice however, that now the event type Start
is simply pattern matched and the actual handle function starts afterwards. Kompics Scala’s pattern matching handlers will be described in detail in section (Handler Execution in Java and Scala).
The HelloWorld
object acts as the Main Class for our project and simply starts Kompics in the usual way with the the component as its only argument.
package se.sics.test
import se.sics.kompics.sl._
import se.sics.kompics.{ Start, Kompics, KompicsEvent }
object HelloWorld extends App {
Kompics.createAndStart(classOf[HelloWorldC]);
}
class HelloWorldC extends ComponentDefinition {
ctrl uponEvent {
case _: Start => handle {
println("Hello World!");
Kompics.asyncShutdown();
}
}
}
Note
As opposed to the Java API, handler subscription is done directly by the uponEvent
where the handler is also created. The method returns the created handler, so it can be unsubscribed later if so desired.
Compiling and Running¶
To compile run the source code with SBT do:
sbt
> run
This is all. Say hello to Kompics Scala ;)