Skip to content

Commit

Permalink
Fixes and DSL improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Bathtor committed May 4, 2019
1 parent 2d05486 commit 65d4471
Show file tree
Hide file tree
Showing 15 changed files with 297 additions and 60 deletions.
3 changes: 2 additions & 1 deletion core/kompics-scala.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ resolvers += Resolver.bintrayRepo("kompics", "Maven")
libraryDependencies += "se.sics.kompics" % "kompics-core" % "1.1.0-SNAPSHOT"
libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.9.+"
libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaVersion.value
libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.5"
libraryDependencies += "org.scala-lang.modules" %% "scala-java8-compat" % "0.9.0"
libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.5" % "test"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % "test"
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.2.+" % "test"

Expand Down
37 changes: 37 additions & 0 deletions core/src/main/scala/se/sics/kompics/ComponentCoreScala.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

/*
* This file is part of the Kompics component model runtime.
*
* Copyright (C) 2009 Swedish Institute of Computer Science (SICS)
* Copyright (C) 2009 Royal Institute of Technology (KTH)
*
* Kompics is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package se.sics.kompics

import se.sics.kompics.config.ConfigUpdate

/**
* A small adapter class for Java -> Scala
*
* @author Lars Kroll {@literal <lkroll@kth.se>}
*/
protected[kompics] abstract class ComponentCoreScala extends ComponentCore {
override private[kompics] def doConfigUpdate(update: ConfigUpdate): Unit = {
this.doConfigUpdateScala(update);
}

protected[kompics] def doConfigUpdateScala(update: ConfigUpdate): Unit;
}
2 changes: 0 additions & 2 deletions core/src/main/scala/se/sics/kompics/sl/AnyPort.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
*/
package se.sics.kompics.sl

import se.sics.kompics.KompicsEvent

trait AnyPort {
def uponEvent(handler: Handler): Handler;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ package se.sics.kompics.sl
import scala.reflect.runtime.universe._

import se.sics.kompics.{ PortType, Positive, Negative, PortCore, ControlPort, Fault, ConfigurationException }
import se.sics.kompics.{ ComponentCore, Channel, LoopbackPort, KompicsEvent, Component }
import se.sics.kompics.{ ComponentCore, Channel, LoopbackPort, Component }
import se.sics.kompics.{ Handler => JHandler, ComponentDefinition => JCD, Init => JInit }
import se.sics.kompics.config.ConfigUpdate
import org.slf4j.{ Logger => JLogger, MDC };
Expand Down
10 changes: 2 additions & 8 deletions core/src/main/scala/se/sics/kompics/sl/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package se.sics.kompics.sl

import scala.language.implicitConversions
import scala.reflect.runtime.universe._
import scala.compat.java8.OptionConverters._

class Config(val original: se.sics.kompics.config.Config) {
import Config._
Expand All @@ -31,7 +32,7 @@ class Config(val original: se.sics.kompics.config.Config) {
def readValue[T: TypeTag](key: String): Option[T] = {
val valueType = typeOf[T];
val valueClass = asJavaClass[T](valueType);
original.readValue(key, valueClass);
original.readValue(key, valueClass).asScala;
}

@throws(classOf[ClassCastException])
Expand All @@ -45,11 +46,4 @@ class Config(val original: se.sics.kompics.config.Config) {
object Config {

implicit def jconf2SConf(c: se.sics.kompics.config.Config): Config = new Config(c);
implicit def optional2Option[T](o: com.google.common.base.Optional[T]): Option[T] = {
if (o.isPresent()) {
return Some(o.get);
} else {
return None
}
}
}
76 changes: 76 additions & 0 deletions core/src/main/scala/se/sics/kompics/sl/Kompics.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* This file is part of the Kompics component model runtime.
*
* Copyright (C) 2009 Swedish Institute of Computer Science (SICS)
* Copyright (C) 2009 Royal Institute of Technology (KTH)
*
* Kompics is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package se.sics.kompics.sl

import se.sics.kompics.{ Kompics => JKompics, Scheduler, FaultHandler, Init => JInit }
import se.sics.kompics.config.{ Config => JConfig }

/**
* Convenient object to forward static members of `se.sics.kompics.Kompics`.
*
* @author Lars Kroll {@literal <lkroll@kth.se>}
*/
object Kompics {
val SHUTDOWN_TIMEOUT = JKompics.SHUTDOWN_TIMEOUT;
val logger = JKompics.logger;
val maxNumOfExecutedEvents = JKompics.maxNumOfExecutedEvents;

def scheduler_=(sched: Scheduler): Unit = JKompics.setScheduler(sched);

def scheduler: Scheduler = JKompics.getScheduler;

def faultHandler_=(fh: FaultHandler): Unit = JKompics.setFaultHandler(fh);

def resetFaultHandler(): Unit = JKompics.resetFaultHandler();

def faultHandler: FaultHandler = JKompics.getFaultHandler;

def config_=(conf: JConfig): Unit = JKompics.setConfig(conf);

def resetConfig(): Unit = JKompics.resetConfig();

def config: JConfig = JKompics.getConfig;

def isOn: Boolean = JKompics.isOn();

def createAndStart[C <: ComponentDefinition](main: Class[C]): Unit = JKompics.createAndStart[C](main);

def createAndStart[C <: ComponentDefinition](main: Class[C], init: JInit[C]): Unit = JKompics.createAndStart[C](main, init);

def createAndStart[C <: ComponentDefinition](main: Class[C], workers: Int): Unit = JKompics.createAndStart[C](main, workers);

def createAndStart[C <: ComponentDefinition](main: Class[C], init: JInit[C], workers: Int): Unit = JKompics.createAndStart[C](main, init, workers);

def createAndStart[C <: ComponentDefinition](main: Class[C], workers: Int, maxEventExecuteNumber: Int): Unit = JKompics.createAndStart[C](main, workers, maxEventExecuteNumber);

def createAndStart[C <: ComponentDefinition](main: Class[C], init: JInit[C], workers: Int, maxEventExecuteNumber: Int): Unit = JKompics.createAndStart[C](main, init, workers, maxEventExecuteNumber);

def asyncShutdown(): Unit = JKompics.asyncShutdown();

def shutdown(): Unit = JKompics.shutdown();

def forceShutdown(): Unit = JKompics.forceShutdown();

@throws(classOf[InterruptedException])
def waitForTermination(): Unit = JKompics.waitForTermination();

def logStats(): Unit = JKompics.logStats();
}
20 changes: 17 additions & 3 deletions core/src/main/scala/se/sics/kompics/sl/NegativePort.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ package se.sics.kompics.sl
import scala.language.implicitConversions
import scala.language.existentials
import se.sics.kompics.{
KompicsEvent,
Component,
Channel,
PortType,
Expand All @@ -45,15 +44,26 @@ import se.sics.kompics.{ Handler => JHandler }
*/
trait NegativePort[P <: PortType] extends Negative[P] with AnyPort {

/**
* Create a bidirectional channel to the `component`.
*/
def ++(component: Component): Channel[P];

/**
* Create a bidirectional channel to each `Component` in `components`.
*/
def ++(components: Component*): Seq[Channel[P]];

/**
* Get the positive pair/dual.
*/
def dualPositive: PositivePort[P];
}

/**
* The <code>NegativeWrapper</code> class.
*
* @author Lars Kroll <lkr@lars-kroll.com>
* @author Lars Kroll <lkroll@kth.se>
* @version $Id: $
*/
class NegativeWrapper[P <: PortType](original: PortCore[P]) extends NegativePort[P] {
Expand Down Expand Up @@ -124,12 +134,16 @@ class NegativeWrapper[P <: PortType](original: PortCore[P]) extends NegativePort
def ++(components: Component*): Seq[Channel[P]] = {
components.map(++);
}

override def dualPositive: PositivePort[P] = {
PositivePort.port2positive(this.getPair())
}
}

/**
* The <code>NegativePort</code> object.
*
* @author Lars Kroll <lkr@lars-kroll.com>
* @author Lars Kroll <lkroll@kth.se>
* @version $Id: $
*/
object NegativePort {
Expand Down
1 change: 0 additions & 1 deletion core/src/main/scala/se/sics/kompics/sl/Port.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
*/
package se.sics.kompics.sl

import se.sics.kompics.KompicsEvent
import scala.reflect.runtime.universe._

/**
Expand Down
24 changes: 19 additions & 5 deletions core/src/main/scala/se/sics/kompics/sl/PositivePort.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ package se.sics.kompics.sl
import scala.language.implicitConversions
import scala.language.existentials
import se.sics.kompics.{
KompicsEvent,
Component,
Channel,
PortType,
Expand All @@ -45,15 +44,26 @@ import se.sics.kompics.{ Handler => JHandler }
*/
trait PositivePort[P <: PortType] extends Positive[P] with AnyPort {

/**
* Create a bidirectional channel to the `component`.
*/
def --(component: Component): Channel[P];

/**
* Create a bidirectional channel to each `Component` in `components`.
*/
def --(components: Component*): Seq[Channel[P]];

/**
* Get the negative pair/dual.
*/
def dualNegative: NegativePort[P];
}

/**
* The <code>PositiveWrapper</code> class.
*
* @author Lars Kroll <lkr@lars-kroll.com>
* @author Lars Kroll <lkroll@kth.se>
* @version $Id: $
*/
class PositiveWrapper[P <: PortType](original: PortCore[P]) extends PositivePort[P] {
Expand Down Expand Up @@ -110,7 +120,7 @@ class PositiveWrapper[P <: PortType](original: PortCore[P]) extends PositivePort
throw new ConfigurationException("Can't use closure based handlers on non ScalaPort");
}

def --(component: Component): Channel[P] = {
override def --(component: Component): Channel[P] = {
val negativePort: Negative[_ <: P] = component.getNegative(original.getPortType().getClass());
negativePort match {
case neg: PortCore[P] => {
Expand All @@ -121,15 +131,19 @@ class PositiveWrapper[P <: PortType](original: PortCore[P]) extends PositivePort
}
}

def --(components: Component*): Seq[Channel[P]] = {
override def --(components: Component*): Seq[Channel[P]] = {
components.map(--);
}

override def dualNegative: NegativePort[P] = {
NegativePort.port2negative(this.getPair())
}
}

/**
* The <code>PositivePort</code> object.
*
* @author Lars Kroll <lkr@lars-kroll.com>
* @author Lars Kroll <lkroll@kth.se>
* @version $Id: $
*/
object PositivePort {
Expand Down
Loading

0 comments on commit 65d4471

Please sign in to comment.