spray.json._ Scala Examples
The following examples show how to use spray.json._.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: TrueGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.binary import be.cetic.tsimulus.generators.Generator import be.cetic.tsimulus.timeseries.binary.TrueTimeSeries import spray.json.{JsObject, JsString, JsValue, _} class TrueGenerator(name: Option[String]) extends Generator[Boolean](name, "true") { override def timeseries(generators: (String) => Generator[Any]) = { new TrueTimeSeries() } override def toString = "True(" + name + ")" override def equals(o: Any) = o match { case that: TrueGenerator => that.name == this.name case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object TrueGenerator { def apply(value: JsValue): TrueGenerator = { val fields = value.asJsObject.fields val name = fields.get("name").map { case JsString(x) => x } new TrueGenerator(name) } }
Example 2
Source File: DayOfYearGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.dt import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.dt.{DayOfWeekTimeSeries, DayOfYearTimeSeries} import org.joda.time.LocalDateTime import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _} class DayOfYearGenerator(name: Option[String], base: Either[String, Generator[LocalDateTime]]) extends Generator[Int](name, "doy") { override def timeseries(generators: String => Generator[Any]) = { val ts = Model.generator(generators)(base).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]] new DayOfYearTimeSeries(ts) } override def toString = "DayOfYearGenerator()" override def equals(o: Any) = o match { case that: DayOfYearGenerator => that.name == this.name case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "base" -> either2json(base) ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object DayOfYearGenerator extends DefaultJsonProtocol with TimeToJson { def apply(name: Option[String], base: String) = new DayOfYearGenerator(name, Left(base)) def apply(name: Option[String], base: Generator[LocalDateTime]) = new DayOfYearGenerator(name, Right(base)) def apply(json: JsValue): DayOfYearGenerator = { val fields = json.asJsObject.fields val name = fields.get("name") .map(f => f match { case JsString(x) => x }) val base = fields("base") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]]) } new DayOfYearGenerator(name, base) } }
Example 3
Source File: MillisecondTimeGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.dt import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.dt.MillisecondTimeSeries import org.joda.time.LocalDateTime import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _} class MillisecondTimeGenerator(name: Option[String], val base: Either[String, Generator[LocalDateTime]]) extends Generator[Int](name, "ms") { override def timeseries(generators: String => Generator[Any]) = { val ts = Model.generator(generators)(base).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]] new MillisecondTimeSeries(ts) } override def toString = "MillisecondTimeGenerator()" override def equals(o: Any) = o match { case that: MillisecondTimeGenerator => (that.name == this.name) && (that.base == this.base) case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "base" -> either2json(base) ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object MillisecondTimeGenerator extends DefaultJsonProtocol with TimeToJson { def apply(name: Option[String], base: String) = new MillisecondTimeGenerator(name, Left(base)) def apply(name: Option[String], base: Generator[LocalDateTime]) = new MillisecondTimeGenerator(name, Right(base)) def apply(json: JsValue): MillisecondTimeGenerator = { val fields = json.asJsObject.fields val name = fields.get("name") .map(f => f match { case JsString(x) => x }) val base = fields("base") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]]) } new MillisecondTimeGenerator(name, base) } }
Example 4
Source File: MinuteGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.dt import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.dt.{MillisecondTimeSeries, MinuteTimeSeries} import org.joda.time.LocalDateTime import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _} class MinuteGenerator(name: Option[String], val base: Either[String, Generator[LocalDateTime]]) extends Generator[Int](name, "minute") { override def timeseries(generators: String => Generator[Any]) = { val ts = Model.generator(generators)(base).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]] new MinuteTimeSeries(ts) } override def toString = "MinuteGenerator()" override def equals(o: Any) = o match { case that: MinuteGenerator => (that.name == this.name) && (that.base == this.base) case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "base" -> either2json(base) ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object MinuteGenerator extends DefaultJsonProtocol with TimeToJson { def apply(name: Option[String], base: String) = new MinuteGenerator(name, Left(base)) def apply(name: Option[String], base: Generator[LocalDateTime]) = new MinuteGenerator(name, Right(base)) def apply(json: JsValue): MinuteGenerator = { val fields = json.asJsObject.fields val name = fields.get("name") .map(f => f match { case JsString(x) => x }) val base = fields("base") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]]) } new MinuteGenerator(name, base) } }
Example 5
Source File: ConstantGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.primary import be.cetic.tsimulus.generators.Generator import be.cetic.tsimulus.timeseries.primary.ConstantTimeSeries import spray.json.{JsNumber, JsString, JsValue, _} class ConstantGenerator(name: Option[String], val value: Double) extends Generator[Double](name, "constant") { override def timeseries(generators: String => Generator[Any]) = ConstantTimeSeries(value) override def toString = "Constant(" + name + ", " + value + ")" override def equals(o: Any) = o match { case that: ConstantGenerator => that.name == this.name && that.value == this.value case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "value" -> value.toJson ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object ConstantGenerator { def apply(json: JsValue): ConstantGenerator = { val fields = json.asJsObject.fields val name = fields.get("name").map { case JsString(x) => x } val value = fields("value") match { case JsNumber(n) => n.toDouble } new ConstantGenerator(name, value) } }
Example 6
Source File: WeeklyGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.primary import java.security.InvalidParameterException import be.cetic.tsimulus.generators.Generator import be.cetic.tsimulus.timeseries.primary.WeeklyTimeSeries import org.joda.time.DateTimeConstants import spray.json.{JsNumber, JsObject, JsString, JsValue, _} class WeeklyGenerator(name: Option[String], val points: Map[String, Double]) extends Generator[Double](name, "weekly") { override def timeseries(generators: String => Generator[Any]) = { def day = (s: String) => s match { case "monday" => DateTimeConstants.MONDAY case "tuesday" => DateTimeConstants.TUESDAY case "wednesday" => DateTimeConstants.WEDNESDAY case "thursday" => DateTimeConstants.THURSDAY case "friday" => DateTimeConstants.FRIDAY case "saturday" => DateTimeConstants.SATURDAY case "sunday" => DateTimeConstants.SUNDAY case _ => throw new InvalidParameterException(s"'${s}' is not a valid day name.") } WeeklyTimeSeries(points map {case (k,v) => (day(k), v)}) } override def toString = "WeeklyGenerator(" + name + "," + points + ")" override def equals(o: Any) = o match { case that: WeeklyGenerator => that.name == this.name && that.points == this.points case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "points" -> points.toJson ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object WeeklyGenerator { def apply(value: JsValue): WeeklyGenerator = { val fields = value.asJsObject.fields val name = fields.get("name").map { case JsString(x) => x } val points = value.asJsObject.fields("points") match { case JsObject(x) => x case _ => throw new ClassCastException } val r = points map { case (k,v) => (k, v match { case JsNumber(x) => x.toDouble })} val validDayNames = List("monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday") val unmatchingDayNames = r.keySet.filterNot(validDayNames contains _) if(!unmatchingDayNames.isEmpty) throw new InvalidParameterException("The following day names are not valid: " + unmatchingDayNames) new WeeklyGenerator(name, r) } }
Example 7
Source File: LesserThanGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.binary import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.Generator import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.binary.LesserThanTimeSeries import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, RootJsonFormat, _} class LesserThanGenerator( name: Option[String], val a: Either[String, Generator[Any]], val b: Either[String, Generator[Any]], val strict: Option[Boolean]) extends Generator[Any](name, "lesser-than") { override def timeseries(generators: (String) => Generator[Any]) = { val first = Model.generator(generators)(a).timeseries(generators) match { case t: TimeSeries[Double] => t } val second = Model.generator(generators)(b).timeseries(generators) match { case t: TimeSeries[Double] => t } new LesserThanTimeSeries(first, second, strict match { case None => true case Some(x) => x }) } override def toString = "LesserThan(" + name + ", " + a + ", " + b + ", " + strict + ")" override def equals(o: Any) = o match { case that: LesserThanGenerator => that.name == this.name && that.a == this.a && that.b == this.b && that.strict == this.strict case _ => false } override def toJson: JsValue = { val _a = a match { case Left(s) => s.toJson case Right(g) => g.toJson } val _b = b match { case Left(s) => s.toJson case Right(g) => g.toJson } var t = Map( "a" -> _a, "b" -> _b, "type" -> `type`.toJson ) if(name.isDefined) t = t.updated("name", name.toJson) if(strict.isDefined) t = t.updated("strict", strict.toJson) new JsObject(t) } } object LesserThanGenerator extends DefaultJsonProtocol { def apply(value: JsValue): LesserThanGenerator = { val fields = value.asJsObject.fields val name = fields.get("name").map(_.convertTo[String]) val strict = fields.get("strict").map(_.convertTo[Boolean]) val a = fields("a") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } val b = fields("b") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } new LesserThanGenerator(name, a, b, strict) } }
Example 8
Source File: ImpliesGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.binary import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.Generator import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.binary.{AndTimeSeries, ImpliesTimeSeries} import spray.json.{JsObject, JsString, JsValue, _} class ImpliesGenerator(name: Option[String], val a: Either[String, Generator[Any]], val b: Either[String, Generator[Any]]) extends Generator[Any](name, "then") { override def timeseries(generators: (String) => Generator[Any]) = { val first = Model.generator(generators)(a).timeseries(generators) match { case t: TimeSeries[Boolean] => t } val second = Model.generator(generators)(b).timeseries(generators) match { case t: TimeSeries[Boolean] => t } new ImpliesTimeSeries(first, second) } override def toString = "Implies(" + name + ", " + a + ", " + b + ")" override def equals(o: Any) = o match { case that: ImpliesGenerator => that.name == this.name && that.a == this.a && that.b == this.b case _ => false } override def toJson: JsValue = { val _a = a match { case Left(s) => s.toJson case Right(g) => g.toJson } val _b = b match { case Left(s) => s.toJson case Right(g) => g.toJson } var t = Map( "a" -> _a, "b" -> _b, "type" -> `type`.toJson ) if(name.isDefined) t = t.updated("name", name.toJson) new JsObject(t) } } object ImpliesGenerator extends DefaultJsonProtocol { def apply(value: JsValue): ImpliesGenerator = { val fields = value.asJsObject.fields val name = fields.get("name").map(_.convertTo[String]) val a = fields("a") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } val b = fields("b") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } new ImpliesGenerator(name, a, b) } }
Example 9
Source File: XorGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.binary import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.Generator import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.binary.XorTimeSeries import spray.json.{JsObject, JsString, JsValue, _} class XorGenerator(name: Option[String], val a: Either[String, Generator[Any]], val b: Either[String, Generator[Any]]) extends Generator[Any](name, "xor") { override def timeseries(generators: (String) => Generator[Any]) = { val first = Model.generator(generators)(a).timeseries(generators) match { case t: TimeSeries[Boolean] => t } val second = Model.generator(generators)(b).timeseries(generators) match { case t: TimeSeries[Boolean] => t } new XorTimeSeries(first, second) } override def toString = "Xor(" + name + ", " + a + ", " + b + ")" override def equals(o: Any) = o match { case that: XorGenerator => that.name == this.name && that.a == this.a && that.b == this.b case _ => false } override def toJson: JsValue = { val _a = a match { case Left(s) => s.toJson case Right(g) => g.toJson } val _b = b match { case Left(s) => s.toJson case Right(g) => g.toJson } var t = Map( "a" -> _a, "b" -> _b, "type" -> `type`.toJson ) if(name.isDefined) t = t.updated("name", name.toJson) new JsObject(t) } } object XorGenerator extends DefaultJsonProtocol { def apply(value: JsValue): XorGenerator = { val fields = value.asJsObject.fields val name = fields.get("name").map(_.convertTo[String]) val a = fields("a") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } val b = fields("b") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } new XorGenerator(name, a, b) } }
Example 10
Source File: FalseGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.binary import be.cetic.tsimulus.generators.Generator import be.cetic.tsimulus.timeseries.binary.FalseTimeSeries import spray.json.{JsObject, JsString, JsValue, _} class FalseGenerator(name: Option[String]) extends Generator[Boolean](name, "false") { override def timeseries(generators: (String) => Generator[Any]) = new FalseTimeSeries() override def toString = "False(" + name + ")" override def equals(o: Any) = o match { case that: FalseGenerator => that.name == this.name case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object FalseGenerator { def apply(value: JsValue): FalseGenerator = { val fields = value.asJsObject.fields val name = fields.get("name").map { case JsString(x) => x } new FalseGenerator(name) } }
Example 11
Source File: AndGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.binary import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.Generator import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.binary.AndTimeSeries import spray.json.{JsObject, JsString, JsValue, _} class AndGenerator(name: Option[String], val a: Either[String, Generator[Any]], val b: Either[String, Generator[Any]]) extends Generator[Any](name, "and") { override def timeseries(generators: (String) => Generator[Any]) = { val first = Model.generator(generators)(a).timeseries(generators) match { case t: TimeSeries[Boolean] => t } val second = Model.generator(generators)(b).timeseries(generators) match { case t: TimeSeries[Boolean] => t } new AndTimeSeries(first, second) } override def toString = "And(" + name + ", " + a + ", " + b + ")" override def equals(o: Any) = o match { case that: AndGenerator => that.name == this.name && that.a == this.a && that.b == this.b case _ => false } override def toJson: JsValue = { val _a = a match { case Left(s) => s.toJson case Right(g) => g.toJson } val _b = b match { case Left(s) => s.toJson case Right(g) => g.toJson } var t = Map( "a" -> _a, "b" -> _b, "type" -> `type`.toJson ) if(name.isDefined) t = t.updated("name", name.toJson) new JsObject(t) } } object AndGenerator extends DefaultJsonProtocol { def apply(value: JsValue): AndGenerator = { val fields = value.asJsObject.fields val name = fields.get("name").map(_.convertTo[String]) val a = fields("a") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } val b = fields("b") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } new AndGenerator(name, a, b) } }
Example 12
Source File: LogisticGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.binary import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.binary.LogisticTimeSeries import spray.json.{JsObject, JsString, JsValue, _} import scala.util.Random class LogisticGenerator(name: Option[String], val generator: Either[String, Generator[Any]], val location: Double, val scale: Double, val seed: Option[Int]) extends Generator[Boolean](name, "logistic") { override def timeseries(generators: (String) => Generator[Any]) = { Model.generator(generators)(generator).timeseries(generators) match { case dTS: TimeSeries[Double] => LogisticTimeSeries(dTS, location, scale, seed.getOrElse(Random.nextInt())) case other => throw new ClassCastException(other.toString) } } override def toString = "Logistic(" + name + ", " + generator + ", " + location + ", " + scale + ", " + seed + ")" override def equals(o: Any) = o match { case that: LogisticGenerator => that.name == this.name && that.generator == this.generator && that.location == this.location && that.scale == this.scale && that.seed == this.seed case _ => false } override def toJson: JsValue = { val _generator = (generator match { case Left(s) => s.toJson case Right(g) => g.toJson }).toJson val t = Map( "type" -> `type`.toJson, "generator" -> _generator, "location" -> location.toJson, "scale" -> scale.toJson, "seed" -> seed.toJson ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object LogisticGenerator extends TimeToJson { def apply(value: JsValue): LogisticGenerator = { val fields = value.asJsObject.fields val name = fields.get("name").map { case JsString(x) => x } val generator = fields("generator") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } val location = fields("location").convertTo[Double] val scale = fields("scale").convertTo[Double] val seed = fields.get("seed").map(_.convertTo[Int]) new LogisticGenerator(name, generator, location, scale, seed) } }
Example 13
Source File: NotGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.binary import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.Generator import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.binary.NotTimeSeries import spray.json.{JsObject, JsString, JsValue, _} class NotGenerator(name: Option[String], val generator: Either[String, Generator[Any]]) extends Generator[Any](name, "or") { override def timeseries(generators: (String) => Generator[Any]) = { val base = Model.generator(generators)(generator).timeseries(generators) match { case t: TimeSeries[Boolean] => t } NotTimeSeries(base) } override def toString = "Not(" + name + ", " + generator + ")" override def equals(o: Any) = o match { case that: NotGenerator => that.name == this.name && that.generator == this.generator case _ => false } override def toJson: JsValue = { val _generator = generator match { case Left(s) => s.toJson case Right(g) => g.toJson } var t = Map( "generator" -> _generator, "type" -> `type`.toJson ) if(name.isDefined) t = t.updated("name", name.toJson) new JsObject(t) } } object NotGenerator extends DefaultJsonProtocol { def apply(value: JsValue): NotGenerator = { val fields = value.asJsObject.fields val name = fields.get("name").map(_.convertTo[String]) val generator = fields("generator") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } new NotGenerator(name, generator) } }
Example 14
Source File: EquivGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.binary import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.Generator import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.binary.{AndTimeSeries, EquivTimeSeries} import spray.json.{JsObject, JsString, JsValue, _} class EquivGenerator(name: Option[String], val a: Either[String, Generator[Any]], val b: Either[String, Generator[Any]]) extends Generator[Any](name, "Equiv") { override def timeseries(generators: (String) => Generator[Any]) = { val first = Model.generator(generators)(a).timeseries(generators) match { case t: TimeSeries[Boolean] => t } val second = Model.generator(generators)(b).timeseries(generators) match { case t: TimeSeries[Boolean] => t } new EquivTimeSeries(first, second) } override def toString = "Equiv(" + name + ", " + a + ", " + b + ")" override def equals(o: Any) = o match { case that: EquivGenerator => that.name == this.name && that.a == this.a && that.b == this.b case _ => false } override def toJson: JsValue = { val _a = a match { case Left(s) => s.toJson case Right(g) => g.toJson } val _b = b match { case Left(s) => s.toJson case Right(g) => g.toJson } var t = Map( "a" -> _a, "b" -> _b, "type" -> `type`.toJson ) if(name.isDefined) t = t.updated("name", name.toJson) new JsObject(t) } } object EquivGenerator extends DefaultJsonProtocol { def apply(value: JsValue): EquivGenerator = { val fields = value.asJsObject.fields val name = fields.get("name").map(_.convertTo[String]) val a = fields("a") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } val b = fields("b") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } new EquivGenerator(name, a, b) } }
Example 15
Source File: GreaterThanGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.binary import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.Generator import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.binary.GreaterThanTimeSeries import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _} class GreaterThanGenerator( name: Option[String], val a: Either[String, Generator[Any]], val b: Either[String, Generator[Any]], val strict: Option[Boolean]) extends Generator[Any](name, "greater-than") { override def timeseries(generators: (String) => Generator[Any]) = { val first = Model.generator(generators)(a).timeseries(generators) match { case t: TimeSeries[Double] => t } val second = Model.generator(generators)(b).timeseries(generators) match { case t: TimeSeries[Double] => t } new GreaterThanTimeSeries(first, second, strict match { case None => true case Some(x) => x }) } override def toString = "GreaterThan(" + name + ", " + a + ", " + b + ", " + strict + ")" override def equals(o: Any) = o match { case that: GreaterThanGenerator => that.name == this.name && that.a == this.a && that.b == this.b && that.strict == this.strict case _ => false } override def toJson: JsValue = { val _a = a match { case Left(s) => s.toJson case Right(g) => g.toJson } val _b = b match { case Left(s) => s.toJson case Right(g) => g.toJson } var t = Map( "a" -> _a, "b" -> _b, "type" -> `type`.toJson ) if(name.isDefined) t = t.updated("name", name.toJson) if(strict.isDefined) t = t.updated("strict", strict.toJson) new JsObject(t) } } object GreaterThanGenerator extends DefaultJsonProtocol { def apply(value: JsValue): GreaterThanGenerator = { val fields = value.asJsObject.fields val name = fields.get("name").map(_.convertTo[String]) val strict = fields.get("strict").map(_.convertTo[Boolean]) val a = fields("a") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } val b = fields("b") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } new GreaterThanGenerator(name, a, b, strict) } }
Example 16
Source File: OrGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.binary import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.Generator import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.binary.OrTimeSeries import spray.json.{JsObject, JsString, JsValue, _} class OrGenerator(name: Option[String], val a: Either[String, Generator[Any]], val b: Either[String, Generator[Any]]) extends Generator[Any](name, "or") { override def timeseries(generators: (String) => Generator[Any]) = { val first = Model.generator(generators)(a).timeseries(generators) match { case t: TimeSeries[Boolean] => t } val second = Model.generator(generators)(b).timeseries(generators) match { case t: TimeSeries[Boolean] => t } new OrTimeSeries(first, second) } override def toString = "Or(" + name + ", " + a + ", " + b + ")" override def equals(o: Any) = o match { case that: OrGenerator => that.name == this.name && that.a == this.a && that.b == this.b case _ => false } override def toJson: JsValue = { val _a = a match { case Left(s) => s.toJson case Right(g) => g.toJson } val _b = b match { case Left(s) => s.toJson case Right(g) => g.toJson } var t = Map( "a" -> _a, "b" -> _b, "type" -> `type`.toJson ) if(name.isDefined) t = t.updated("name", name.toJson) new JsObject(t) } } object OrGenerator extends DefaultJsonProtocol { def apply(value: JsValue): OrGenerator = { val fields = value.asJsObject.fields val name = fields.get("name").map(_.convertTo[String]) val a = fields("a") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } val b = fields("b") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } new OrGenerator(name, a, b) } }
Example 17
Source File: WeekGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.dt import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.dt.{MinuteTimeSeries, WeekTimeSeries} import org.joda.time.LocalDateTime import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _} class WeekGenerator(name: Option[String], val base: Either[String, Generator[LocalDateTime]]) extends Generator[Int](name, "week") { override def timeseries(generators: String => Generator[Any]) = { val ts = Model.generator(generators)(base).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]] new WeekTimeSeries(ts) } override def toString = "WeekGenerator()" override def equals(o: Any) = o match { case that: WeekGenerator => (that.name == this.name) && (that.base == this.base) case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "base" -> either2json(base) ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object WeekGenerator extends DefaultJsonProtocol with TimeToJson { def apply(name: Option[String], base: String) = new WeekGenerator(name, Left(base)) def apply(name: Option[String], base: Generator[LocalDateTime]) = new WeekGenerator(name, Right(base)) def apply(json: JsValue): WeekGenerator = { val fields = json.asJsObject.fields val name = fields.get("name") .map(f => f match { case JsString(x) => x }) val base = fields("base") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]]) } new WeekGenerator(name, base) } }
Example 18
Source File: TimeShiftGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.composite import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.composite.TimeShiftTimeSeries import com.github.nscala_time.time.Imports._ import org.joda.time.Duration import spray.json.{JsObject, JsString, JsValue, _} class TimeShiftGenerator(name: Option[String], val generator: Either[String, Generator[Any]], val shift: Duration) extends Generator[Any](name, "time-shift") with TimeToJson { override def timeseries(generators: (String) => Generator[Any]) = { val ts = Model.generator(generators)(generator).timeseries(generators) TimeShiftTimeSeries(ts, shift) } override def toString = "TimeShift(" + name + ", " + generator + ", " + shift.getMillis + ")" override def equals(o: Any) = o match { case that: TimeShiftGenerator => that.name == this.name && that.shift == this.shift case _ => false } override def toJson: JsValue = { var t = Map( "generator" -> either2json(generator), "shift" -> DurationFormat.write(shift), "type" -> `type`.toJson ) if(name.isDefined) t = t.updated("name", name.toJson) new JsObject(t) } } object TimeShiftGenerator extends DefaultJsonProtocol with TimeToJson { def apply(value: JsValue): TimeShiftGenerator = { val fields = value.asJsObject.fields val name = fields.get("name").map(_.convertTo[String]) val generator = fields("generator") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } val shift = fields("shift").convertTo[Duration] new TimeShiftGenerator(name, generator, shift) } }
Example 19
Source File: CorrelatedGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.composite import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.Generator import be.cetic.tsimulus.timeseries.composite.CorrelatedTimeSeries import spray.json.{JsObject, JsString, JsValue, _} import scala.util.Random class CorrelatedGenerator(name: Option[String], val generator: Either[String, Generator[Any]], val coef: Double) extends Generator[Double](name, "correlated") { override def timeseries(generators: (String) => Generator[Any]) = { Model.generator(generators)(generator) match { case dDouble : Generator[Double] => CorrelatedTimeSeries(dDouble.timeseries(generators), Random.nextInt(), coef) case _ => throw new ClassCastException } } override def toString = "Correlated(" + name + ", " + generator + ", " + coef + ")" override def equals(o: Any) = o match { case that: CorrelatedGenerator => that.name == this.name && that.generator == this.generator && that.coef == this.coef case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "generator" -> either2json(generator), "coef" -> coef.toJson ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object CorrelatedGenerator extends DefaultJsonProtocol { def apply(value: JsValue): CorrelatedGenerator = { val fields = value.asJsObject.fields val name = fields.get("name").map { case JsString(x) => x } val `type` = fields("type").convertTo[String] val generator = fields("generator") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } val coef = fields("coef").convertTo[Double] new CorrelatedGenerator(name, generator, coef) } }
Example 20
Source File: FunctionGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.composite import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.Generator import be.cetic.tsimulus.timeseries.composite.FunctionTimeSeries import spray.json.{DefaultJsonProtocol, JsNumber, JsObject, JsString, JsValue, _} class FunctionGenerator(name: Option[String], val generator: Either[String, Generator[Any]], val slope: Double, val intercept: Double) extends Generator[Double](name, "function") { override def timeseries(generators: String => Generator[Any]) = { Model.generator(generators)(generator) match { // Could also be expressed as a Sum(Times(generator, Constant(slope), intercept) case g: Generator[Double] => FunctionTimeSeries[Double](g.timeseries(generators), (t,v) => Some(slope * v + intercept)) case _ => throw new ClassCastException } } override def toString = "Function(" + name + ", " + generator + ", " + slope + ", " + intercept + ")" override def equals(o: Any) = o match { case that: FunctionGenerator => (that.name == this.name && that.generator == this.generator && that.slope == this.slope && that.intercept == this.intercept) case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "generator" -> either2json(generator), "slope" -> slope.toJson, "intercept" -> intercept.toJson ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object FunctionGenerator { def apply(json: JsValue): FunctionGenerator = { val fields = json.asJsObject.fields val name = json.asJsObject.fields.get("name").map { case JsString(x) => x } val generator = fields("generator") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } val slope = fields("slope") match { case JsNumber(n) => n.toDouble } val intercept = fields("intercept") match { case JsNumber(n) => n.toDouble } new FunctionGenerator(name, generator, slope, intercept) } }
Example 21
Source File: BinaryTransitionGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.composite import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.composite.TransitionTimeSeries import org.joda.time.{Duration, LocalDateTime} import spray.json.{JsObject, JsString, JsValue, _} class BinaryTransitionGenerator(name: Option[String], val first: Either[String, Generator[Any]], val second: Either[String, Generator[Any]], val time: LocalDateTime) extends Generator[Boolean](name, "binary-transition") { override def timeseries(generators: (String) => Generator[Any]) = { val firstBase = Model.generator(generators)(first).timeseries(generators) match { case t: TimeSeries[Boolean] => t } val secondBase = Model.generator(generators)(second).timeseries(generators) match { case t: TimeSeries[Boolean] => t } TransitionTimeSeries[Boolean](firstBase, secondBase, time, None) } override def toString = "BinaryTransitionGenerator(" + name + "," + first + "," + second + "," + time + ")" override def equals(o: Any) = o match { case that: BinaryTransitionGenerator => that.name == this.name && that.first == this.first && that.second == this.second && that.time == this.time case _ => false } override def toJson: JsValue = { val _first = (first match { case Left(s) => s.toJson case Right(g) => g.toJson }).toJson val _second = (second match { case Left(s) => s.toJson case Right(g) => g.toJson }).toJson var t = Map( "type" -> `type`.toJson, "first" -> _first, "second" -> _second, "time" -> time.toJson ) if(name.isDefined) t = t.updated("name", name.get.toJson) new JsObject(t) } } object BinaryTransitionGenerator extends TimeToJson { def apply(value: JsValue): BinaryTransitionGenerator = { val fields = value.asJsObject.fields val name = fields.get("name").map { case JsString(x) => x } val first = fields("first") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } val second = fields("second") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } val time = fields("time").convertTo[LocalDateTime] new BinaryTransitionGenerator(name, first, second, time) } }
Example 22
Source File: DivideGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.composite import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.Generator import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.composite.DivideTimeSeries import spray.json.{JsObject, JsString, JsValue, _} class DivideGenerator(name: Option[String], val numerator: Either[String, Generator[Any]], val denominator: Either[String, Generator[Any]]) extends Generator[Double](name, "divide") { override def timeseries(gen: String => Generator[Any]) = { val num = Model.generator(gen)(numerator).timeseries(gen) match { case t: TimeSeries[Double] => t } val den = Model.generator(gen)(denominator).timeseries(gen) match { case t: TimeSeries[Double] => t } new DivideTimeSeries(num, den) } override def toString = "Divide(" + name + ", " + numerator + ", " + denominator + ")" override def equals(o: Any) = o match { case that: DivideGenerator => that.name == this.name && that.numerator == this.numerator && that.denominator == this.denominator case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "numerator" -> either2json(numerator), "denominator" -> either2json(denominator) ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object DivideGenerator { def apply(value: JsValue): DivideGenerator = { val fields = value.asJsObject.fields val name = fields.get("name").map { case JsString(x) => x } val numerator = fields("numerator") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } val denominator = fields("denominator") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } new DivideGenerator(name, numerator, denominator) } }
Example 23
Source File: AggregateGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.composite import be.cetic.tsimulus.config._ import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.composite.AggregationTimeSeries import spray.json.{JsArray, JsObject, JsString, JsValue, _} class AggregateGenerator[U](name: Option[String], val aggregator: String, val generators: Seq[Either[String, Generator[Any]]]) extends Generator[U](name, "aggregate") { override def timeseries(gen: String => Generator[Any]) = { val agg = aggregationFunction(aggregator) val ts = generators.map { case Left(s) => gen(s).timeseries(gen) case Right(g) => g.timeseries(gen) } val series = ts flatMap { case d : TimeSeries[Double] => Some(d) case _ => None } new AggregationTimeSeries[Double, U](agg, series) } override def toString = "Aggregate(" + name + ", " + aggregator + ", " + generators.mkString("[", ", ", "]") + ")" override def equals(o: Any) = o match { case that: AggregateGenerator[U] => that.name == this.name && that.aggregator == this.aggregator && that.generators == this.generators case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "aggregator" -> aggregator.toJson, "generators" -> generators.map(either2json).toJson ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object AggregateGenerator extends DefaultJsonProtocol { def apply[U](value: JsValue): AggregateGenerator[U] = { val fields = value.asJsObject.fields val name = fields.get("name").map { case JsString(x) => x } val aggregator = fields("aggregator").convertTo[String] val generators = fields("generators") match { case JsArray(x) => x.map { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) }.toList } new AggregateGenerator(name, aggregator, generators) } }
Example 24
Source File: ConditionalGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.composite import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.composite.ConditionalTimeSeries import be.cetic.tsimulus.timeseries.missing.UndefinedTimeSeries import spray.json.{JsObject, JsString, JsValue, _} class ConditionalGenerator(name: Option[String], val condition: Either[String, Generator[Any]], val success: Either[String, Generator[Any]], val failure: Option[Either[String, Generator[Any]]]) extends Generator[Any](name, "conditional") { override def timeseries(generators: (String) => Generator[Any]) = { val cond = Model.generator(generators)(condition).timeseries(generators) match { case t: TimeSeries[Boolean] => t } val a = Model.generator(generators)(success).timeseries(generators) match { case t: TimeSeries[Any] => t } val b = failure.map(f => Model.generator(generators)(f).timeseries(generators) match { case t: TimeSeries[Any] => t }).getOrElse(new UndefinedTimeSeries()) ConditionalTimeSeries(cond, a, b) } override def toString = "Conditional(" + name + ", " + condition + ", " + success + ", " + failure + ")" override def equals(o: Any) = o match { case that: ConditionalGenerator => that.name == this.name && that.condition == this.condition && that.success == this.success && that.failure == this.failure case _ => false } override def toJson: JsValue = { var t = Map( "type" -> `type`.toJson, "condition" -> either2json(condition), "success" -> either2json(success) ) if(failure.isDefined) { val _failure = (failure.get match { case Left(s) => s.toJson case Right(g) => g.toJson }).toJson t = t.updated("failure", _failure) } new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object ConditionalGenerator { def apply(value: JsValue): ConditionalGenerator = { val fields = value.asJsObject.fields val name = fields.get("name").map { case JsString(x) => x } val condition = fields("condition") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } val success = fields("success") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } val failure = if(fields.contains("failure")) fields("failure") match { case JsString(s) => Some(Left(s)) case g => Some(Right(GeneratorFormat.read(g))) } else None new ConditionalGenerator(name, condition, success, failure) } }
Example 25
Source File: PartialGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.missing import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.missing.PartialTimeSeries import org.joda.time.LocalDateTime import spray.json.{JsObject, JsString, JsValue, _} class PartialGenerator(name: Option[String], val generator: Either[String, Generator[Any]], val from: Option[LocalDateTime], val to: Option[LocalDateTime], val missingRate: Option[Double]) extends Generator[Any](name, "partial") { override def timeseries(generators: (String) => Generator[Any]) = { val ts = Model.generator(generators)(generator).timeseries(generators) PartialTimeSeries(ts, from, to, missingRate) } override def toString = "Partial(" + name + ", " + generator + ", " + from + ", " + to + ", " + missingRate + ")" override def equals(o: Any) = o match { case that: PartialGenerator => that.name == this.name && that.generator == this.generator && that.from == this.from && that.to == this.to && that.missingRate == this.missingRate case _ => false } override def toJson: JsValue = { var t = Map( "type" -> `type`.toJson, "generator" -> either2json(generator), "from" -> from.toJson, "to" -> to.toJson ) if(missingRate.isDefined) t = t.updated("missing-rate" , missingRate.toJson) if(name.isDefined) t = t.updated("name", name.toJson) new JsObject(t) } } object PartialGenerator extends DefaultJsonProtocol with TimeToJson { def apply(value: JsValue): PartialGenerator = { val fields = value.asJsObject.fields val name = fields.get("name").map { case JsString(x) => x } val generator = fields("generator") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } val from = fields.get("from").map(_.convertTo[LocalDateTime]) val to = fields.get("to").map(_.convertTo[LocalDateTime]) val missingRate = fields.get("missing-rate").map(_.convertTo[Double]) new PartialGenerator(name, generator, from, to, missingRate) } }
Example 26
Source File: LimitedGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.missing import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.missing.LimitedTimeSeries import org.joda.time.LocalDateTime import spray.json.{JsObject, JsString, JsValue, _} class LimitedGenerator(name: Option[String], val generator: Either[String, Generator[Any]], val from: Option[LocalDateTime], val to: Option[LocalDateTime]) extends Generator[Any](name, "limited") { override def timeseries(generators: (String) => Generator[Any]) = LimitedTimeSeries(Model.generator(generators)(generator).timeseries(generators), from, to) override def toString = "Limited(" + name + ", " + generator + ", " + from + ", " + to + ")" override def equals(o: Any) = o match { case that: LimitedGenerator => that.name == this.name && that.generator == this.generator && that.from == this.from && that.to == this.to case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "generator" -> either2json(generator), "from" -> from.get.toJson, "to" -> to.get.toJson ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object LimitedGenerator extends DefaultJsonProtocol with TimeToJson { def apply(value: JsValue): LimitedGenerator = { val fields = value.asJsObject.fields val name = fields.get("name").map { case JsString(x) => x } val generator = fields("generator") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } val from = fields.get("from").map(_.convertTo[LocalDateTime]) val to = fields.get("to").map(_.convertTo[LocalDateTime]) val missingRate = fields.get("missing-rate").map(_.convertTo[Double]) new LimitedGenerator(name, generator, from, to) } }
Example 27
Source File: DefaultGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.missing import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.missing.DefaultTimeSeries import spray.json.{JsArray, JsObject, JsString, JsValue, _} class DefaultGenerator(name: Option[String], val gens: Seq[Either[String, Generator[Any]]]) extends Generator[Any](name, "first-of") { override def timeseries(generators: (String) => Generator[Any]) = { val underlyings = gens.map(g => Model.generator(generators)(g).timeseries(generators) match { case t: TimeSeries[Any] => t }) DefaultTimeSeries(underlyings) } override def toString = "UndefinedGenerator(" + name + "," + gens + ")" override def equals(o: Any) = o match { case that: DefaultGenerator => that.gens == this.gens case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "generators" -> gens.map(either2json).toJson ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object DefaultGenerator { def apply(value: JsValue): DefaultGenerator = { val fields = value.asJsObject.fields val name = fields.get("name").map { case JsString(x) => x } val generators = fields("generators") match { case JsArray(l) => l.map { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } } new DefaultGenerator(name, generators) } }
Example 28
Source File: UndefinedGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.missing import be.cetic.tsimulus.generators.Generator import be.cetic.tsimulus.timeseries.missing.UndefinedTimeSeries import spray.json.{JsObject, JsString, JsValue, _} class UndefinedGenerator(name: Option[String]) extends Generator[Any](name, "undefined") { override def timeseries(generators: (String) => Generator[Any]) = new UndefinedTimeSeries() override def toString = "UndefinedGenerator(" + name + ")" override def equals(o: Any) = o match { case that: UndefinedGenerator => that.name == this.name case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object UndefinedGenerator { def apply(value: JsValue): UndefinedGenerator = { val fields = value.asJsObject.fields val name = fields.get("name").map { case JsString(x) => x } new UndefinedGenerator(name) } }
Example 29
Source File: TimeToJson.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators import com.github.nscala_time.time.Imports._ import org.joda.time.format.DateTimeFormatterBuilder import org.joda.time.{Duration, LocalDateTime, LocalTime} import spray.json.{JsString, JsValue, RootJsonFormat, _} trait TimeToJson extends DefaultJsonProtocol { val dtf = DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss.SSS") val ttf = DateTimeFormat.forPattern("HH:mm:ss.SSS") val datetimeFormatter = { val parsers = Array( DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss.SSS").getParser, DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss").getParser ) new DateTimeFormatterBuilder().append(null, parsers).toFormatter() } val timeFormatter = { val parsers = Array( DateTimeFormat.forPattern("HH:mm:ss.SSS").getParser, DateTimeFormat.forPattern("HH:mm:ss").getParser ) new DateTimeFormatterBuilder().append(null, parsers).toFormatter() } implicit object LocalDateTimeJsonFormat extends RootJsonFormat[LocalDateTime] { def write(d: LocalDateTime) = JsString(dtf.print(d)) def read(value: JsValue) = value match { case JsString(s) => datetimeFormatter.parseLocalDateTime(s) case unrecognized => serializationError(s"Serialization problem $unrecognized") } } implicit object LocalTimeJsonFormat extends RootJsonFormat[LocalTime] { def write(t: LocalTime) = JsString(ttf.print(t)) def read(value: JsValue) = value match { case JsString(s) => timeFormatter.parseLocalTime(s) case unknown => deserializationError(s"unknown LocalTime object: $unknown") } } implicit object DurationFormat extends RootJsonFormat[Duration] { def write(d: Duration) = d.getMillis.toJson def read(value: JsValue) = new Duration(value.toString.toLong) } def either2json(element: Either[String,Generator[Any]]) = element match { case Left(s) => s.toJson case Right(g) => g.toJson } }
Example 30
Source File: WorkflowParamSpec.scala From seahorse with Apache License 2.0 | 5 votes |
package ai.deepsense.deeplang.params import spray.json.{JsString, _} import ai.deepsense.deeplang.DOperationCategories import ai.deepsense.deeplang.catalogs.SortPriority import ai.deepsense.deeplang.catalogs.doperations.DOperationsCatalog import ai.deepsense.deeplang.doperations.custom.{Sink, Source} import ai.deepsense.deeplang.params.custom.InnerWorkflow import ai.deepsense.models.json.graph.GraphJsonProtocol.GraphReader class WorkflowParamSpec extends AbstractParamSpec[InnerWorkflow, WorkflowParam] { override def className: String = "WorkflowParam" override def graphReader: GraphReader = { val catalog = DOperationsCatalog() catalog.registerDOperation(DOperationCategories.IO, () => Source(), SortPriority.coreDefault) catalog.registerDOperation(DOperationCategories.IO, () => Sink(), SortPriority.coreDefault) new GraphReader(catalog) } override def paramFixture: (WorkflowParam, JsValue) = { val description = "Workflow parameter description" val param = WorkflowParam( name = "Workflow parameter name", description = Some(description)) val expectedJson = JsObject( "type" -> JsString("workflow"), "name" -> JsString(param.name), "description" -> JsString(description), "isGriddable" -> JsFalse, "default" -> JsNull ) (param, expectedJson) } override def valueFixture: (InnerWorkflow, JsValue) = { val innerWorkflow = InnerWorkflow.empty val sourceNode = JsObject( "id" -> JsString(innerWorkflow.source.id.toString), "operation" -> JsObject( "id" -> JsString(Source.id.toString), "name" -> JsString("Source") ), "parameters" -> JsObject() ) val sinkNode = JsObject( "id" -> JsString(innerWorkflow.sink.id.toString), "operation" -> JsObject( "id" -> JsString(Sink.id.toString), "name" -> JsString("Sink") ), "parameters" -> JsObject() ) val workflow = JsObject( "nodes" -> JsArray(sourceNode, sinkNode), "connections" -> JsArray() ) val value = JsObject( "workflow" -> workflow, "thirdPartyData" -> JsObject(), "publicParams" -> JsArray() ) (innerWorkflow, value) } }
Example 31
Source File: MistHttpInterface.scala From mist with Apache License 2.0 | 5 votes |
package io.hydrosphere.mist import java.nio.file.{Files, Path} import io.hydrosphere.mist.master.JobResult import io.hydrosphere.mist.master.models.FunctionConfig import scalaj.http._ case class MistHttpInterface( host: String, port: Int, timeout: Int = 120 ) { import io.hydrosphere.mist.master.interfaces.JsonCodecs._ import spray.json.{enrichString, _} def runJob(routeId: String, params: (String, Any)*): JobResult = callV2Api(routeId, params.toMap) def uploadArtifact(name: String, file: Path): Unit = { val bytes = Files.readAllBytes(file) val req = Http(s"http://$host:$port/v2/api/artifacts") .postMulti(MultiPart("file", name, "application/octet-stream", bytes)) val resp = req.asBytes if (resp.code != 200) throw new RuntimeException(s"File $file uploading failed. Code: ${resp.code}, body: ${resp.body}") } def status: String = { val req = Http(s"http://$host:$port/v2/api/status") new String(req.asBytes.body) } def createFunction(ep: FunctionConfig): FunctionConfig = { val req = Http(s"http://$host:$port/v2/api/functions") .postData(ep.toJson) val resp = req.asString if (resp.code == 200) resp.body.parseJson.convertTo[FunctionConfig] else throw new RuntimeException(s"Function creation failed. Code: ${resp.code}, body: ${resp.body}") } def callV2Api( functionId: String, params: Map[String, Any] ): JobResult = { val millis = timeout * 1000 val url = s"http://$host:$port/v2/api/functions/$functionId/jobs?force=true" val req = Http(url) .timeout(millis, millis) .header("Content-Type", "application/json") .postData(params.toJson) val resp = req.asString if (resp.code == 200) resp.body.parseJson.convertTo[JobResult] else throw new RuntimeException(s"Job failed body ${resp.body}") } sealed trait ActionType case object Execute extends ActionType case object Serve extends ActionType }
Example 32
Source File: Utils.scala From graphcool-framework with Apache License 2.0 | 5 votes |
package cool.graph import com.google.common.base.CaseFormat import spray.json.{DefaultJsonProtocol, _} object Utils { def camelToUpperUnderscore(str: String): String = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, str) } case class Timing(name: String, duration: Long) object TimingProtocol extends DefaultJsonProtocol { implicit val timingFormat: RootJsonFormat[Timing] = jsonFormat2(Timing) } object JsonFormats { implicit object CaseClassFormat extends JsonFormat[Product] { def write(x: Product): JsValue = { val values = x.productIterator.toList val fields = x.getClass.getDeclaredFields def getIdValue(p: Product): Option[Any] = { val values = p.productIterator.toList val fields = p.getClass.getDeclaredFields fields.zipWithIndex.find(_._1.getName == "id").map(z => values(z._2)) } val map: Map[String, Any] = values.zipWithIndex.map { case (v, i) => val key = fields(i).getName val value = v match { case v: Product if !v.isInstanceOf[Option[_]] => getIdValue(v).getOrElse("...") case Some(v: Product) => getIdValue(v).getOrElse("...") case v => v } key -> value }.toMap AnyJsonFormat.write(map) } def read(value: JsValue) = throw new UnsupportedOperationException() } implicit object AnyJsonFormat extends JsonFormat[Any] { def write(x: Any): JsValue = x match { case m: Map[_, _] => JsObject(m.asInstanceOf[Map[String, Any]].mapValues(write)) case l: List[Any] => JsArray(l.map(write).toVector) case n: Int => JsNumber(n) case n: Long => JsNumber(n) case n: Double => JsNumber(n) case s: String => JsString(s) case true => JsTrue case false => JsFalse case v: JsValue => v case null => JsNull case r => JsString(r.toString) } def read(value: JsValue) = throw new UnsupportedOperationException() } class AnyJsonWriter extends JsonWriter[Map[String, Any]] { override def write(obj: Map[String, Any]): JsValue = AnyJsonFormat.write(obj) } class SeqAnyJsonWriter[T <: Any] extends JsonWriter[Seq[Map[String, T]]] { override def write(objs: Seq[Map[String, T]]): JsValue = new JsArray(objs.map(obj => AnyJsonFormat.write(obj)).toVector) } }
Example 33
Source File: Data.scala From ohara with Apache License 2.0 | 5 votes |
package oharastream.ohara.client.configurator import oharastream.ohara.common.setting.ObjectKey import spray.json.{JsValue, _} import spray.json.DefaultJsonProtocol._ private[this] def matchSetting(settings: Map[String, JsValue], key: String, value: String): Boolean = try if (value.toLowerCase == "none") !settings.contains(key) else settings.get(key).exists { // it is impossible to have JsNull since our json format does a great job :) case JsString(s) => s == value case JsNumber(i) => i == BigDecimal(value) case JsBoolean(b) => b == value.toBoolean case js: JsArray => value.parseJson match { // include the part of elements => true // otherwise => false case other: JsArray => other.elements.forall(v => js.elements.contains(v)) case _ => false } case js: JsObject => value.parseJson match { case other: JsObject => other.fields.forall { case (k, v) => matchSetting(js.fields, k, v match { case JsString(s) => s case _ => v.toString() }) } case _ => false } case JsNull => value.toLowerCase == "none" case _ => false } catch { case _: Throwable => false } }
Example 34
Source File: ValidationApi.scala From ohara with Apache License 2.0 | 5 votes |
package oharastream.ohara.client.configurator import oharastream.ohara.common.util.CommonUtils import oharastream.ohara.kafka.connector.json.SettingInfo import spray.json.{RootJsonFormat, _} import scala.concurrent.{ExecutionContext, Future} object ValidationApi { val VALIDATION_KIND: String = "validate" private[this] implicit val SETTING_INFO_FORMAT: RootJsonFormat[SettingInfo] = new RootJsonFormat[SettingInfo] { override def write(obj: SettingInfo): JsValue = obj.toJsonString.parseJson override def read(json: JsValue): SettingInfo = SettingInfo.ofJson(json.toString()) } def connectorRequest: ConnectorRequest } def access: Access = new Access(VALIDATION_KIND) { override def connectorRequest: ConnectorRequest = new ConnectorRequest { override def verify()(implicit executionContext: ExecutionContext): Future[SettingInfo] = exec.put[oharastream.ohara.client.configurator.ConnectorApi.Creation, SettingInfo, ErrorApi.Error]( s"$url/${ConnectorApi.KIND}", creation ) } } }
Example 35
Source File: HydraJsonSupport.scala From hydra with Apache License 2.0 | 5 votes |
package hydra.core.marshallers import java.io.{PrintWriter, StringWriter} import java.util.UUID import akka.actor.ActorPath import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport import akka.http.scaladsl.model.StatusCode import hydra.common.util.Resource._ import org.joda.time.DateTime import org.joda.time.format.ISODateTimeFormat import spray.json.{JsString, _} import scala.util.{Failure, Success, Try} implicit def tryWriter[R: JsonWriter]: RootJsonWriter[Try[R]] = new RootJsonWriter[Try[R]] { override def write(responseTry: Try[R]): JsValue = { responseTry match { case Success(r) => JsObject("success" -> r.toJson) case Failure(t) => JsObject("failure" -> t.toJson) } } } implicit object StreamTypeFormat extends RootJsonFormat[StreamType] { def read(json: JsValue): StreamType = json match { case JsString("Notification") => Notification case JsString("History") => History case JsString("CurrentState") => CurrentState case JsString("Telemetry") => Telemetry case _ => { import scala.reflect.runtime.{universe => ru} val tpe = ru.typeOf[StreamType] val clazz = tpe.typeSymbol.asClass throw new DeserializationException( s"expected a streamType of ${clazz.knownDirectSubclasses}, but got $json" ) } } def write(obj: StreamType): JsValue = { JsString(obj.toString) } } implicit val genericErrorFormat = jsonFormat2(GenericError) implicit val topicCreationMetadataFormat = jsonFormat8(TopicMetadataRequest) implicit val genericSchemaFormat = jsonFormat2(GenericSchema) } case class GenericError(status: Int, errorMessage: String) case class TopicMetadataRequest( schema: JsObject, streamType: StreamType, derived: Boolean, deprecated: Option[Boolean], dataClassification: String, contact: String, additionalDocumentation: Option[String], notes: Option[String] ) case class GenericSchema(name: String, namespace: String) { def subject = s"$namespace.$name" } sealed trait StreamType case object Notification extends StreamType case object CurrentState extends StreamType case object History extends StreamType case object Telemetry extends StreamType
Example 36
Source File: ClickhouseQueryExecutor.scala From clickhouse-scala-client with GNU Lesser General Public License v3.0 | 5 votes |
package com.crobox.clickhouse.dsl.execution import akka.stream.scaladsl.Source import com.crobox.clickhouse.ClickhouseClient import com.crobox.clickhouse.dsl.language.{ClickhouseTokenizerModule, TokenizerModule} import com.crobox.clickhouse.dsl.{Query, Table} import com.crobox.clickhouse.internal.QuerySettings import com.crobox.clickhouse.internal.progress.QueryProgress.QueryProgress import spray.json.{JsonReader, _} import scala.concurrent.{ExecutionContext, Future} trait ClickhouseQueryExecutor extends QueryExecutor { self: TokenizerModule => implicit val client: ClickhouseClient def execute[V: JsonReader](query: Query)(implicit executionContext: ExecutionContext, settings: QuerySettings = QuerySettings()): Future[QueryResult[V]] = { import QueryResult._ val queryResult = client.query(toSql(query.internalQuery)) queryResult.map(_.parseJson.convertTo[QueryResult[V]]) } def executeWithProgress[V: JsonReader]( query: Query )(implicit executionContext: ExecutionContext, settings: QuerySettings = QuerySettings()): Source[QueryProgress, Future[QueryResult[V]]] = { import QueryResult._ val queryResult = client.queryWithProgress(toSql(query.internalQuery)) queryResult.mapMaterializedValue(_.map(_.parseJson.convertTo[QueryResult[V]])) } override def insert[V: JsonWriter]( table: Table, values: Seq[V] )(implicit executionContext: ExecutionContext, settings: QuerySettings = QuerySettings()): Future[String] = Future { values.map(_.toJson.compactPrint).mkString("\n") + "\n" }.flatMap( entity => client.execute(s"INSERT INTO ${table.quoted} FORMAT JSONEachRow", entity) ) } object ClickhouseQueryExecutor { def default(clickhouseClient: ClickhouseClient): ClickhouseQueryExecutor = new DefaultClickhouseQueryExecutor(clickhouseClient) } class DefaultClickhouseQueryExecutor(override val client: ClickhouseClient) extends ClickhouseQueryExecutor with ClickhouseTokenizerModule
Example 37
Source File: package.scala From clickhouse-scala-client with GNU Lesser General Public License v3.0 | 5 votes |
package com.crobox.clickhouse.dsl import spray.json.{JsonReader, _} package object execution { case class Statistic(rowsRead: Long, rowsBeforeLimit: Long) case class ResultColumnType(name: String, columnType: String) case class ResultMeta(columnTypes: Seq[ResultColumnType]) case class QueryResult[V](rows: Seq[V], meta: Option[ResultMeta] = None, statistic: Option[Statistic] = None) { def size: Int = rows.size } object QueryResult { implicit def format[V: JsonReader] = new JsonReader[QueryResult[V]] { override def read(json: JsValue): QueryResult[V] = { val jsObject = json.asJsObject val rows = jsObject.getFields("data") match { case Seq(JsArray(results)) => results.map(_.convertTo[V]) } val meta = jsObject.fields.get("meta").map { case JsArray(columnDefinitions) => ResultMeta(columnDefinitions.map(_.asJsObject.getFields("name", "type") match { case Seq(JsString(name), JsString(colType)) => ResultColumnType(name, colType) })) } val statistic = jsObject.getFields("rows_before_limit_at_least", "rows") match { case Seq(JsNumber(limit), JsNumber(rowsRead)) => Some(Statistic(rowsRead.longValue, limit.longValue)) case _ => None } QueryResult(rows, meta, statistic) } } } }
Example 38
Source File: ClickhouseJsonSupport.scala From clickhouse-scala-client with GNU Lesser General Public License v3.0 | 5 votes |
package com.crobox.clickhouse.dsl.marshalling import com.crobox.clickhouse.time.IntervalStart import org.joda.time.format.{DateTimeFormatter, DateTimeFormatterBuilder, ISODateTimeFormat} import org.joda.time.{DateTime, DateTimeZone} import spray.json.{JsNumber, JsString, JsValue, JsonFormat, deserializationError, _} import scala.util.Try trait ClickhouseJsonSupport { override def read(json: JsValue): IntervalStart = json match { case JsString(value) => value match { case month(relativeMonth, timezoneId) => new DateTime(UnixStartTimeWithoutTimeZone) .withZoneRetainFields(DateTimeZone.forID(timezoneId)) .plusMonths(relativeMonth.toInt - RelativeMonthsSinceUnixStart) .withZone(DateTimeZone.UTC) case date(dateOnly, timezoneId) => //should handle quarter and year grouping as it returns a date formatter .parseDateTime(dateOnly) .withZoneRetainFields(DateTimeZone.forID(timezoneId)) .withZone(DateTimeZone.UTC) case msTimestamp(millis) => new DateTime(millis.toLong, DateTimeZone.UTC) case timestamp(secs) => new DateTime(secs.toLong * 1000, DateTimeZone.UTC) case _ => // sometimes clickhouse mistakenly returns a long / int value as JsString. Therefor, first try to // parse it as a long... val dateTime = Try { new DateTime(value.toLong, DateTimeZone.UTC) }.toOption // continue with parsing using the formatter dateTime.getOrElse { try { formatter.parseDateTime(value) } catch { case _: IllegalArgumentException => error(s"Couldn't parse $value into valid date time") case _: UnsupportedOperationException => error("Unsupported operation, programmatic misconfiguration?") } } } case JsNumber(millis) => new DateTime(millis.longValue, DateTimeZone.UTC) case _ => throw DeserializationException(s"Unknown date format read from clickhouse for $json") } def error(v: Any): DateTime = { val example = readFormatter.print(0) deserializationError( f"'$v' is not a valid date value. Dates must be in compact ISO-8601 format, e.g. '$example'" ) } } } object ClickhouseJsonSupport extends DefaultJsonProtocol with ClickhouseJsonSupport
Example 39
Source File: Unmarshallers.scala From JustinDB with Apache License 2.0 | 5 votes |
package justin.httpapi import java.util.UUID import akka.http.scaladsl.unmarshalling._ import akka.stream.Materializer import spray.json.{JsString, JsValue, JsonFormat, _} import scala.concurrent.{ExecutionContext, Future} import scala.util.{Failure, Success, Try} object Unmarshallers { implicit val UuidFormat = new JsonFormat[UUID] { override def read(json: JsValue): UUID = { json match { case JsString(uuid) => Try(UUID.fromString(uuid)) match { case Success(parsedUuid) => parsedUuid case Failure(_) => deserializationError("UUID could not be created from given string") } case _ => deserializationError("UUID could not be converted to UUID object.") } } override def write(obj: UUID): JsValue = JsString(obj.toString) } object UUIDUnmarshaller extends FromStringUnmarshaller[UUID] { override def apply(value: String)(implicit ec: ExecutionContext, materializer: Materializer): Future[UUID] = { Future.apply(UUID.fromString(value)) } } }
Example 40
Source File: JsonMessageBuilderWithoutHeadersTest.scala From reactive-activemq with Apache License 2.0 | 5 votes |
package akka.stream.integration.builder import akka.camel.CamelMessage import akka.stream.integration.{ CamelMessageBuilder, JsonCamelMessageBuilder, TestSpec } import spray.json.{ DefaultJsonProtocol, _ } object JsonMessageBuilderWithoutHeadersTest extends DefaultJsonProtocol { case class MessageReceived(fileName: String, timestamp: Long) implicit val messageReceivedJsonFormat = jsonFormat2(MessageReceived) implicit val messageReceivedJsonCamelMessageBuilder = JsonCamelMessageBuilder.jsonMessageBuilder[MessageReceived] } class JsonMessageBuilderWithoutHeadersTest extends TestSpec { import JsonMessageBuilderWithoutHeadersTest._ it should "build a CamelMessage without headers" in { val msg = MessageReceived("fileName.txt", 1) val camelMessage = implicitly[CamelMessageBuilder[MessageReceived]].build(msg) camelMessage shouldEqual CamelMessage(msg.toJson.compactPrint, Map.empty[String, Any]) } }
Example 41
Source File: JsonSupport.scala From mleap with Apache License 2.0 | 5 votes |
package ml.combust.mleap.tensor import spray.json.DefaultJsonProtocol._ import spray.json.{JsObject, _} import scala.reflect.ClassTag trait JsonSupport { implicit def mleapArrayFormat[T: JsonFormat: ClassTag]: RootJsonFormat[Array[T]] = new RootJsonFormat[Array[T]] { val base = implicitly[JsonFormat[T]] override def write(obj: Array[T]): JsValue = { JsArray(obj.map(base.write): _*) } override def read(json: JsValue): Array[T] = json match { case json: JsArray => val elements = json.elements val size = elements.size val values = new Array[T](size) (0 until size).foreach(i => values(i) = base.read(elements(i))) values case _ => deserializationError("invalid array") } } implicit def mleapDenseTensorFormat[T: JsonFormat: ClassTag]: RootJsonFormat[DenseTensor[T]] = jsonFormat[Array[T], Seq[Int], DenseTensor[T]](DenseTensor[T], "values", "dimensions") implicit def mleapSparseTensorFormat[T: JsonFormat: ClassTag]: RootJsonFormat[SparseTensor[T]] = jsonFormat[Seq[Seq[Int]], Array[T], Seq[Int], SparseTensor[T]](SparseTensor[T], "indices", "values", "dimensions") implicit def mleapTensorFormat[T: JsonFormat: ClassTag]: RootJsonFormat[Tensor[T]] = new RootJsonFormat[Tensor[T]] { override def write(obj: Tensor[T]): JsValue = obj match { case obj: DenseTensor[_] => obj.asInstanceOf[DenseTensor[T]].toJson case obj: SparseTensor[_] => obj.asInstanceOf[SparseTensor[T]].toJson } override def read(json: JsValue): Tensor[T] = json match { case json: JsObject => if(json.fields.contains("indices")) { mleapSparseTensorFormat[T].read(json) } else { mleapDenseTensorFormat[T].read(json) } case _ => deserializationError("invalid tensor") } } } object JsonSupport extends JsonSupport
Example 42
Source File: LowLevelServer.scala From akka-http-test with Apache License 2.0 | 5 votes |
package com.github.dnvriend.component.lowlevelserver import akka.NotUsed import akka.actor.{ ActorSystem, Props } import akka.event.{ Logging, LoggingAdapter } import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.pattern.ask import akka.stream.scaladsl.{ Flow, Sink, Source } import akka.stream.{ ActorMaterializer, Materializer } import akka.util.Timeout import com.github.dnvriend.component.lowlevelserver.dto.{ Person, PersonWithId } import com.github.dnvriend.component.lowlevelserver.marshaller.Marshaller import com.github.dnvriend.component.lowlevelserver.repository.PersonRepository import spray.json.{ DefaultJsonProtocol, _ } import scala.concurrent.duration._ import scala.concurrent.{ ExecutionContext, Future } class LowLevelServer(implicit val system: ActorSystem, mat: Materializer, ec: ExecutionContext, log: LoggingAdapter, timeout: Timeout) extends DefaultJsonProtocol with Marshaller { val personDb = system.actorOf(Props[PersonRepository]) def debug(t: Any)(implicit log: LoggingAdapter = null): Unit = if (Option(log).isEmpty) println(t) else log.debug(t.toString) def http200Okay(req: HttpRequest): HttpResponse = HttpResponse(StatusCodes.OK) def http200AsyncOkay(req: HttpRequest): Future[HttpResponse] = Future(http200Okay(req)) val http200OkayFlow: Flow[HttpRequest, HttpResponse, NotUsed] = Flow[HttpRequest].map { req => HttpResponse(StatusCodes.OK) } val serverSource: Source[Http.IncomingConnection, Future[Http.ServerBinding]] = Http().bind(interface = "localhost", port = 8080) val binding: Future[Http.ServerBinding] = serverSource.to(Sink.foreach { conn => // conn.handleWith(http200OkayFlow) // conn.handleWithSyncHandler(http200Okay) // conn.handleWithAsyncHandler(http200AsyncOkay, 8) conn.handleWithAsyncHandler(personRequestHandler) }).run() def personRequestHandler(req: HttpRequest): Future[HttpResponse] = req match { case HttpRequest(HttpMethods.GET, Uri.Path("/api/person"), _, _, _) => for { xs <- (personDb ? "findAll").mapTo[List[PersonWithId]] entity = HttpEntity(ContentTypes.`application/json`, xs.toJson.compactPrint) } yield HttpResponse(StatusCodes.OK, entity = entity) case HttpRequest(HttpMethods.POST, Uri.Path("/api/person"), _, ent, _) => for { strictEntity <- ent.toStrict(1.second) person <- (personDb ? strictEntity.data.utf8String.parseJson.convertTo[Person]).mapTo[PersonWithId] } yield HttpResponse(StatusCodes.OK, entity = person.toJson.compactPrint) case req => req.discardEntityBytes() Future.successful(HttpResponse(StatusCodes.NotFound)) } } object LowLevelServerLauncher extends App with DefaultJsonProtocol { // setting up some machinery implicit val system: ActorSystem = ActorSystem() implicit val mat: Materializer = ActorMaterializer() implicit val ec: ExecutionContext = system.dispatcher implicit val log: LoggingAdapter = Logging(system, this.getClass) implicit val timeout: Timeout = Timeout(10.seconds) new LowLevelServer() }
Example 43
Source File: GameData.scala From eventsourcing-intro with Apache License 2.0 | 5 votes |
package eu.reactivesystems.league.impl import eu.reactivesystems.league.api.Game import spray.json.{JsValue, _} case class GameData(home: ClubData, away: ClubData)(val round: Int, val homeGoals: Int, val awayGoals: Int) object GameData { def apply(game: Game) = new GameData(ClubData(game.home), ClubData(game.away))(game.round, game.homeGoals, game.awayGoals) implicit object GameDataFormat extends RootJsonFormat[GameData] { def write(g: GameData) = JsObject( "home" -> g.home.toJson, "away" -> g.away.toJson, "round" -> JsNumber(g.round), "homeGoals" -> JsNumber(g.homeGoals), "awayGoals" -> JsNumber(g.awayGoals) ) def read(value: JsValue) = { value.asJsObject.getFields("home", "away", "round", "homeGoals", "awayGoals") match { case Seq(home @ JsObject(_), away @ JsObject(_), JsNumber(round), JsNumber(homeGoals), JsNumber(awayGoals)) => new GameData(home.convertTo[ClubData], away.convertTo[ClubData])( round.toInt, homeGoals.toInt, awayGoals.toInt) case _ => throw new DeserializationException("Color expected") } } } }
Example 44
Source File: IAMRole_UT.scala From cloudformation-template-generator with BSD 3-Clause "New" or "Revised" License | 5 votes |
package com.monsanto.arch.cloudformation.model.resource import com.monsanto.arch.cloudformation.model.ResourceRef import org.scalatest.{FunSpec, Matchers} import spray.json.{JsObject, JsString, _} class IAMRole_UT extends FunSpec with Matchers { describe("AWS::IAM::Role") { it("should handle both AWS Managed and Customer policies into valid json") { val customerPolicy = `AWS::IAM::ManagedPolicy`("customer-policy", PolicyDocument(Seq())) val awsPolicy = AWSManagedPolicy("AdministratorAccess") val fakePolicyDoc = PolicyDocument(Seq( PolicyStatement( "Allow", Some(DefinedPrincipal(Map("Service" -> Seq("config.amazonaws.com")))), Seq("sts:AssumeRole") ) )) val expectedJson = JsObject( "name" -> JsString("role"), "AssumeRolePolicyDocument" -> fakePolicyDoc.toJson, "ManagedPolicyArns" -> JsArray( JsObject("Ref" -> JsString("customer-policy")), JsString("arn:aws:iam::aws:policy/AdministratorAccess") ) ) val role = `AWS::IAM::Role`( "role", fakePolicyDoc, ManagedPolicyArns = Some(Seq(ResourceRef(customerPolicy), awsPolicy)) ) role.toJson should be(expectedJson) } } }
Example 45
Source File: Subnet_Parameter_List_UT.scala From cloudformation-template-generator with BSD 3-Clause "New" or "Revised" License | 5 votes |
package com.monsanto.arch.cloudformation.model.resource import com.monsanto.arch.cloudformation.model._ import org.scalatest.{FunSpec, Matchers} import spray.json.{JsNumber, JsString, _} class Subnet_Parameter_List_UT extends FunSpec with Matchers { describe("AWS::EC2::Subnet_Parameter_List") { it("should serialize into valid json") { val subnetListParam = `AWS::EC2::Subnet_Parameter_List`("subnets", "Select subnets where the RDS instances should be created") val expectedJson = JsObject( "subnets" -> JsObject( "Description" -> JsString("Select subnets where the RDS instances should be created"), "Type" -> JsString("List<AWS::EC2::Subnet::Id>") ) ) Seq[Parameter](subnetListParam).toJson should be (expectedJson) } it("should serialize into valid json as InputParameter") { val subnetListParam = `AWS::EC2::Subnet_Parameter_List`("subnets", "Select subnets where the RDS instances should be created") val expectedJson = JsObject( "ParameterKey" -> JsString("subnets"), "ParameterValue" -> JsString("") ) val inputParam = InputParameter.templateParameterToInputParameter(Some(Seq(subnetListParam))) inputParam.get(0).toJson should be (expectedJson) } it("can be passed as ParameterRef to AWS::RDS::DBSubnetGroup") { val subnetListParam = `AWS::EC2::Subnet_Parameter_List`("subnets", "Select subnets where the RDS instances should be created") val dbSubnetGroup = `AWS::RDS::DBSubnetGroup`( name = "dbSubnetGroup", DBSubnetGroupDescription = "DB subnet group", SubnetIds = ParameterRef(subnetListParam) ) val expected = JsObject( "dbSubnetGroup" -> JsObject( "Type" -> JsString("AWS::RDS::DBSubnetGroup"), "Properties" -> JsObject( "DBSubnetGroupDescription" -> JsString("DB subnet group"), "SubnetIds" -> JsObject("Ref" -> JsString("subnets")) ) ) ) Seq[Resource[_]](dbSubnetGroup).toJson should be (expected) } } }
Example 46
Source File: LogData.scala From graphcool-framework with Apache License 2.0 | 5 votes |
package cool.graph.shared.logging import cool.graph.JsonFormats import spray.json.{DefaultJsonProtocol, _} object LogKey extends Enumeration { val RequestNew = Value("request/new") val RequestQuery = Value("request/query") val RequestComplete = Value("request/complete") val RequestMetricsFields = Value("request/metrics/fields") val RequestMetricsSql = Value("request/metrics/sql") val RequestMetricsMutactions = Value("request/metrics/mutactions") val UnhandledError = Value("error/unhandled") val HandledError = Value("error/handled") val MutactionWebhook = Value("mutaction/webhook") val AlgoliaSyncQuery = Value("mutaction/algoliaSyncQuery") val ActionHandlerWebhookComplete = Value("action_handler/webhook/complete") val IntegrityViolation = Value("integrity/violation") val RequestProxyBegin = Value("request/proxy/begin") val RequestProxyData = Value("request/proxy/data") } case class LogData( key: LogKey.Value, requestId: String, clientId: Option[String] = None, projectId: Option[String] = None, message: Option[String] = None, payload: Option[Map[String, Any]] = None ) { import LogFormats._ lazy val json: String = this.toJson(logDataFormat).compactPrint } object LogFormats extends DefaultJsonProtocol { import JsonFormats.AnyJsonFormat implicit object LogKeyJsonFormat extends RootJsonFormat[LogKey.Value] { def write(obj: LogKey.Value): JsValue = JsString(obj.toString) def read(json: JsValue): LogKey.Value = json match { case JsString(str) => LogKey.withName(str) case _ => throw new DeserializationException("Enum string expected") } } implicit val logDataFormat: RootJsonFormat[LogData] = jsonFormat(LogData, "log_key", "request_id", "client_id", "project_id", "message", "payload") }
Example 47
Source File: DevFunctionEnvironment.scala From graphcool-framework with Apache License 2.0 | 5 votes |
package cool.graph.shared.functions.dev import akka.actor.ActorSystem import akka.stream.ActorMaterializer import cool.graph.akkautil.http.SimpleHttpClient import cool.graph.cuid.Cuid import cool.graph.shared.functions._ import cool.graph.shared.models.Project import spray.json.{JsArray, JsObject, JsString, _} import scala.concurrent.Future import scala.util.{Failure, Success, Try} case class DevFunctionEnvironment()(implicit system: ActorSystem, materializer: ActorMaterializer) extends FunctionEnvironment { import Conversions._ import system.dispatcher private val httpClient = SimpleHttpClient() override def pickDeploymentAccount(): Option[String] = None val functionEndpointInternal: String = sys.env.getOrElse("FUNCTION_ENDPOINT_INTERNAL", sys.error("FUNCTION_ENDPOINT_INTERNAL env var required for dev function deployment.")).stripSuffix("/") val functionEndpointExternal: String = sys.env.getOrElse("FUNCTION_ENDPOINT_EXTERNAL", sys.error("FUNCTION_ENDPOINT_EXTERNAL env var required for dev function deployment.")).stripSuffix("/") override def getTemporaryUploadUrl(project: Project): String = { val deployId = Cuid.createCuid() s"$functionEndpointExternal/functions/files/${project.id}/$deployId" } override def deploy(project: Project, externalFile: ExternalFile, name: String): Future[DeployResponse] = { httpClient .postJson(s"$functionEndpointInternal/functions/deploy/${project.id}", DeploymentInput(externalFile.url, externalFile.devHandler, name)) .map { response => response.bodyAs[StatusResponse] match { case Success(status) => if (status.success) { DeploySuccess() } else { DeployFailure(new Exception(status.error.getOrElse(""))) } case Failure(e) => DeployFailure(e) } } .recover { case e: Throwable => DeployFailure(e) } } override def invoke(project: Project, name: String, event: String): Future[InvokeResponse] = { httpClient .postJson(s"$functionEndpointInternal/functions/invoke/${project.id}", FunctionInvocation(name, event)) .map { response => response.bodyAs[FunctionInvocationResult] match { case Success(result) => val returnValue = Try { result.value.map(_.toString).getOrElse("").parseJson } match { case Success(parsedJson) => parsedJson case Failure(_) => JsObject("error" -> JsString("Function did not return a valid response. Check your function code / logs.")) } val output = JsObject( "logs" -> JsArray( JsObject("stdout" -> JsString(result.stdout.getOrElse(""))), JsObject("stderr" -> JsString(result.stderr.getOrElse(""))), JsObject("error" -> JsString(result.error.getOrElse(""))) ), "response" -> returnValue ).compactPrint if (result.success) { InvokeSuccess(output) } else { InvokeFailure(new Exception(output)) } case Failure(e) => InvokeFailure(e) } } .recover { case e: Throwable => InvokeFailure(e) } } }
Example 48
Source File: ValidatorTask.scala From ohara with Apache License 2.0 | 5 votes |
package oharastream.ohara.connector.validation import java.util import java.util.concurrent.TimeUnit import oharastream.ohara.client.configurator.InspectApi.{RdbInfo, RdbQuery} import oharastream.ohara.client.configurator.{ErrorApi, InspectApi} import oharastream.ohara.client.database.DatabaseClient import oharastream.ohara.common.data.Serializer import oharastream.ohara.common.util.VersionUtils import org.apache.kafka.connect.data.Schema import org.apache.kafka.connect.source.{SourceRecord, SourceTask} import spray.json.{JsObject, _} import scala.jdk.CollectionConverters._ class ValidatorTask extends SourceTask { private[this] var done = false private[this] var props: Map[String, String] = _ private[this] val topic: String = InspectApi.INTERNAL_TOPIC_KEY.topicNameOnKafka private[this] var requestId: String = _ override def start(props: util.Map[String, String]): Unit = { this.props = props.asScala.toMap requestId = require(InspectApi.REQUEST_ID) } override def poll(): util.List[SourceRecord] = if (done) { // just wait the configurator to close this connector TimeUnit.SECONDS.sleep(2) null } else try information match { case query: RdbQuery => toSourceRecord(validate(query)) } catch { case e: Throwable => toSourceRecord(ErrorApi.of(e)) } finally done = true override def stop(): Unit = { // do nothing } override def version(): String = VersionUtils.VERSION private[this] def validate(query: RdbQuery): RdbInfo = { val client = DatabaseClient.builder.url(query.url).user(query.user).password(query.password).build try RdbInfo( name = client.databaseType, tables = client.tableQuery .catalog(query.catalogPattern.orNull) .schema(query.schemaPattern.orNull) .tableName(query.tableName.orNull) .execute() ) finally client.close() } private[this] def toJsObject: JsObject = props(InspectApi.SETTINGS_KEY).parseJson.asJsObject private[this] def information = require(InspectApi.TARGET_KEY) match { case InspectApi.RDB_KIND => InspectApi.RDB_QUERY_FORMAT.read(toJsObject) case other: String => throw new IllegalArgumentException( s"valid targets are ${InspectApi.RDB_KIND}. current is $other" ) } private[this] def toSourceRecord(data: Object): util.List[SourceRecord] = util.Arrays.asList( new SourceRecord( null, null, topic, Schema.BYTES_SCHEMA, Serializer.STRING.to(requestId), Schema.BYTES_SCHEMA, Serializer.OBJECT.to(data) ) ) private[this] def require(key: String): String = props.getOrElse(key, throw new IllegalArgumentException(s"the $key is required")) }
Example 49
Source File: JsonYQLParser.scala From CSYE7200_Old with MIT License | 5 votes |
package edu.neu.coe.csye7200.actors import akka.actor.ActorRef import edu.neu.coe.csye7200.model.{Model, YQLModel} import spray.http._ import scala.util._ class JsonYQLParser(blackboard: ActorRef) extends BlackboardActor(blackboard) { val model: Model = new YQLModel override def receive: PartialFunction[Any, Unit] = { case ContentMessage(entity) => log.debug("JsonYQLParser received ContentMessage") JsonYQLParser.decode(entity) match { case Right(response) => processQuote(response.query.results.quote) case Left(message) => log.warning(message.toString) } case m => super.receive(m) } def processQuote(quotes: Seq[Map[String, Option[String]]]): Unit = quotes foreach { q => processInstrument(q) } def processInstrument(quote: Map[String, Option[String]]): Unit = model.getKey("symbol") match { case Some(s) => quote.get(s) match { case Some(Some(symbol)) => updateMarket(symbol, quote) case _ => log.warning(s"symbol $s is undefined") } case _ => log.warning("'symbol' is undefined in model") } def updateMarket(symbol: String, quote: Map[String, Option[String]]): Unit = blackboard ! KnowledgeUpdate(model, symbol, quote flatMap { case (k, Some(v)) => Option(k -> v); case _ => None }) } object JsonYQLParser { import spray.httpx.SprayJsonSupport._ import spray.httpx.unmarshalling._ import spray.json.{DefaultJsonProtocol, _} case class Response(query: Query) case class Query(count: Int, created: String, lang: String, diagnostics: Option[Diagnostics], results: Results) case class Diagnostics(url: Seq[Map[String, String]], publiclyCallable: String, `user-time`: String, `service-time`: String, `build-version`: String, query: DiagnosticsQuery, cache: DiagnosticsCache, javascript: DiagnosticsJavascript) case class DiagnosticsQuery(`execution-start-time`: String, `execution-stop-time`: String, `execution-time`: String, params: String, content: String) case class DiagnosticsCache(`execution-start-time`: String, `execution-stop-time`: String, `execution-time`: String, method: String, `type`: String, content: String) case class DiagnosticsJavascript(`execution-start-time`: String, `execution-stop-time`: String, `execution-time`: String, `instructions-used`: String, `table-name`: String) case class Results(quote: Seq[Map[String, Option[String]]]) { def get(index: Int, key: String): Option[String] = { Try { quote(index) } match { case Success(y) => y.get(key) match { case Some(x) => x; case None => None } case Failure(_) => None } } } object MyJsonProtocol extends DefaultJsonProtocol with NullOptions { implicit val diagnosticsQueryFormat: RootJsonFormat[DiagnosticsQuery] = jsonFormat5(DiagnosticsQuery) implicit val diagnosticsCacheFormat: RootJsonFormat[DiagnosticsCache] = jsonFormat6(DiagnosticsCache) implicit val diagnosticsJavascriptFormat: RootJsonFormat[DiagnosticsJavascript] = jsonFormat5(DiagnosticsJavascript) implicit val diagnosticsFormat: RootJsonFormat[Diagnostics] = jsonFormat8(Diagnostics) implicit val resultsFormat: RootJsonFormat[Results] = jsonFormat1(Results) implicit val queryFormat: RootJsonFormat[Query] = jsonFormat5(Query) implicit val entityFormat: RootJsonFormat[Response] = jsonFormat1(Response) } import MyJsonProtocol._ def decode(entity: HttpEntity): Deserialized[Response] = entity.as[Response] }
Example 50
Source File: JsonGoogleOptionParser.scala From CSYE7200_Old with MIT License | 5 votes |
package edu.neu.coe.csye7200.actors import akka.actor.ActorRef import edu.neu.coe.csye7200.model.{GoogleOptionModel, Model} import spray.http._ import scala.util._ def decode(entity: HttpEntity): Deserialized[OptionChain] = { // import spray.httpx.unmarshalling._ val contentType = ContentType(MediaTypes.`application/json`, HttpCharsets.`UTF-8`) entity match { case HttpEntity.NonEmpty(`contentType`, y) => HttpEntity(contentType, fix(y)).as[OptionChain] case HttpEntity.NonEmpty(s, _) => Left(MalformedContent(s"entity content type: $s")) case _ => Left(MalformedContent("logic error")) } } def fix(data: HttpData): Array[Byte] = fix(data.asString).getBytes def fix(s: String): String = """([^,{:\s]+):""".r.replaceAllIn(s, """"$1":""") }
Example 51
Source File: JsonGoogleParser.scala From CSYE7200_Old with MIT License | 5 votes |
package edu.neu.coe.csye7200.actors import akka.actor.ActorRef import edu.neu.coe.csye7200.model.{GoogleModel, Model} import spray.http._ import scala.util._ def decode(entity: HttpEntity): Deserialized[Results] = { import spray.httpx.unmarshalling._ val mediaTypeTextHtml = MediaTypes.`text/html` val mediaTypeJson = MediaTypes.`application/json` val contentTypeJson = ContentType(mediaTypeJson, HttpCharsets.`UTF-8`) // val contentTypeText = ContentType(mediaTypeTextHtml, HttpCharsets.`ISO-8859-1`) entity match { case HttpEntity.NonEmpty(`contentTypeJson`, _) => entity.as[Results] case HttpEntity.NonEmpty(ContentType(`mediaTypeTextHtml`, x), y) => HttpEntity(ContentType(mediaTypeJson, x), fix(y)).as[Results] case HttpEntity.NonEmpty(x, _) => Left(MalformedContent(s"logic error: contentType=$x")) case _ => Left(MalformedContent("logic error")) } } def fix(data: HttpData): Array[Byte] = fix(data.asString).getBytes def fix(s: String): String = s.substring(3) }
Example 52
Source File: MapArrayParam.scala From mmlspark with MIT License | 5 votes |
// Copyright (C) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. See LICENSE in project root for information. package org.apache.spark.ml.param import spray.json.{DefaultJsonProtocol, _} import scala.collection.JavaConverters._ import scala.collection.immutable.Map import scala.collection.mutable object MapArrayJsonProtocol extends DefaultJsonProtocol { implicit object MapJsonFormat extends JsonFormat[Map[String, Seq[String]]] { def write(m: Map[String, Seq[String]]): JsValue = { JsObject(m.mapValues { case v: Seq[String] => seqFormat[String].write(v) case default => serializationError(s"Unable to serialize $default") }) } def read(value: JsValue): Map[String, Seq[String]] = value.asInstanceOf[JsObject].fields.map(kvp => { val convValue = kvp._2 match { case v: JsValue => seqFormat[String].read(v) case default => deserializationError(s"Unable to deserialize $default") } (kvp._1, convValue) }) } } def w(value: java.util.HashMap[String, java.util.List[String]]): ParamPair[Map[String, Seq[String]]] = { val mutMap = mutable.Map[String, Seq[String]]() for (key <- value.keySet().asScala) { val list = value.get(key).asScala mutMap(key) = list } w(mutMap.toMap) } override def jsonEncode(value: Map[String, Seq[String]]): String = { val convertedMap = value.map(kvp => (kvp._1, kvp._2.toArray)) val json = convertedMap.toJson json.prettyPrint } override def jsonDecode(json: String): Map[String, Seq[String]] = { val jsonValue = json.parseJson jsonValue.convertTo[Map[String, Seq[String]]] } }
Example 53
Source File: ArrayMapParam.scala From mmlspark with MIT License | 5 votes |
// Copyright (C) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. See LICENSE in project root for information. package org.apache.spark.ml.param import org.apache.spark.ml.util.Identifiable import spray.json.{DefaultJsonProtocol, _} import scala.collection.immutable.Map object ArrayMapJsonProtocol extends DefaultJsonProtocol { implicit object MapJsonFormat extends JsonFormat[Map[String, Any]] { def write(m: Map[String, Any]): JsValue = { JsObject(m.mapValues { case v: Int => JsNumber(v) case v: Double => JsNumber(v) case v: String => JsString(v) case true => JsTrue case false => JsFalse case v: Map[_, _] => write(v.asInstanceOf[Map[String, Any]]) case default => serializationError(s"Unable to serialize $default") }) } def read(value: JsValue): Map[String, Any] = value.asInstanceOf[JsObject].fields.map(kvp => { val convValue = kvp._2 match { case JsNumber(n) => if (n.isValidInt) n.intValue().asInstanceOf[Any] else n.toDouble.asInstanceOf[Any] case JsString(s) => s case JsTrue => true case JsFalse => false case v: JsValue => read(v) case default => deserializationError(s"Unable to deserialize $default") } (kvp._1, convValue) }) } } override def w(value: Array[Map[String, Any]]): ParamPair[Array[Map[String, Any]]] = super.w(value) override def jsonEncode(value: Array[Map[String, Any]]): String = { val json = value.toSeq.toJson json.prettyPrint } override def jsonDecode(json: String): Array[Map[String, Any]] = { val jsonValue = json.parseJson jsonValue.convertTo[Seq[Map[String, Any]]].toArray } }
Example 54
Source File: Configuration.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.config import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.TimeSeries import com.github.nscala_time.time.Imports._ import org.joda.time.LocalDateTime import spray.json.{JsArray, JsObject, JsValue, _} case class Configuration(generators: Option[Seq[Generator[Any]]], series: Seq[Series[Any]], from: LocalDateTime, to: LocalDateTime) extends TimeToJson { def timeSeries: Map[String, (TimeSeries[Any], Duration)] = { val memory = firstOrderGenerators series.map(s => { val duration = s.frequency val generator = Model.generator(memory)(s.generator) s.name -> (generator.timeseries(memory), duration) }).toMap } def firstOrderGenerators: Map[String, Generator[Any]] = { generators match { case None => Map() case Some(gens) => { val memory = scala.collection.mutable.Map[String, Generator[Any]]() gens.foreach(g => { memory.put(g.name.get, g) }) memory.toMap } } } def toJson: JsValue = { new JsObject(Map( "generators" -> generators.map(g => g.map(_.toJson)).toJson, "exported" -> series.map(s => s.toJson).toJson, "from" -> from.toJson, "to" -> to.toJson )) } } object Configuration extends TimeToJson { def apply(value: JsValue): Configuration = { val fields = value.asJsObject.fields val generators = fields.get("generators").map { case JsArray(l) => l.map(GeneratorFormat.read) case _ => throw new ClassCastException } val series = fields("exported") match { case JsArray(x) => x.map(Series[Any](_)).toSeq case _ => throw new ClassCastException } val from = fields("from").convertTo[LocalDateTime] val to = fields("to").convertTo[LocalDateTime] Configuration(generators, series, from, to) } }
Example 55
Source File: Series.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.config import be.cetic.tsimulus.generators.{Generator, TimeToJson} import com.github.nscala_time.time.Imports._ import org.joda.time.Duration import spray.json.{JsObject, JsString, JsValue, _} case class Series[T](name: String, generator: Either[String, Generator[Any]], frequency: Duration) extends TimeToJson { def toJson: JsValue = { val _generator = generator match { case Left(s) => s.toJson case Right(g) => g.toJson } new JsObject(Map( "name" -> name.toJson, "generator" -> _generator, "frequency" -> frequency.toJson )) } } object Series extends TimeToJson { def apply[T](value: JsValue): Series[T] = { val fields = value.asJsObject.fields val generator = fields("generator") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } val frequency = fields("frequency").convertTo[Duration] val name = fields("name").convertTo[String] Series(name, generator, frequency) } }
Example 56
Source File: Models.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.config import be.cetic.tsimulus.generators._ import be.cetic.tsimulus.generators.binary._ import be.cetic.tsimulus.generators.composite._ import be.cetic.tsimulus.generators.missing.{DefaultGenerator, LimitedGenerator, PartialGenerator, UndefinedGenerator} import be.cetic.tsimulus.generators.primary._ import be.cetic.tsimulus.generators.dt._ import org.joda.time.LocalDateTime import spray.json.{JsString, _} object Model { def generator(generators: String => Generator[Any])(element: Either[String, Generator[Any]]): Generator[Any] = element match { case Left(s) => generators(s) case Right(g) => g } } case class ARMAModel(phi: Option[Seq[Double]], theta: Option[Seq[Double]], std: Double, c: Double, seed: Option[Long]) object GeneratorFormat extends JsonFormat[Generator[Any]] { def deserializationError(s: String): Generator[Any] = throw DeserializationException(s) def serializationError(s: String): JsValue = throw new SerializationException(s) override def read(json: JsValue): Generator[Any] = json match { case known:JsObject if known.fields.contains("type") => known.fields("type") match{ case JsString("arma") => ARMAGenerator(known) case JsString("daily") => DailyGenerator(known) case JsString("weekly") => WeeklyGenerator(known) case JsString("monthly") => MonthlyGenerator(known) case JsString("yearly") => YearlyGenerator(known) case JsString("constant") => ConstantGenerator(known) case JsString("aggregate") => AggregateGenerator(known) case JsString("divide") => DivideGenerator(known) case JsString("correlated") => CorrelatedGenerator(known) case JsString("logistic") => LogisticGenerator(known) case JsString("conditional") => ConditionalGenerator(known) case JsString("true") => TrueGenerator(known) case JsString("false") => FalseGenerator(known) case JsString("transition") => TransitionGenerator(known) case JsString("binary-transition") => BinaryTransitionGenerator(known) case JsString("window") => SlidingWindowGenerator(known) case JsString("limited") => LimitedGenerator(known) case JsString("partial") => PartialGenerator(known) case JsString("time-shift") => TimeShiftGenerator(known) case JsString("function") => FunctionGenerator(known) case JsString("and") => AndGenerator(known) case JsString("or") => OrGenerator(known) case JsString("not") => NotGenerator(known) case JsString("xor") => XorGenerator(known) case JsString("implies") => ImpliesGenerator(known) case JsString("equiv") => EquivGenerator(known) case JsString("undefined") => UndefinedGenerator(known) case JsString("first-of") => DefaultGenerator(known) case JsString("greater-than") => GreaterThanGenerator(known) case JsString("lesser-than") => LesserThanGenerator(known) case JsString("gaussian") => GaussianNoiseGenerator(known) case JsString("year") => YearGenerator(known) case JsString("month") => MonthGenerator(known) case JsString("dom") => DayOfMonthGenerator(known) case JsString("hour") => HourGenerator(known) case JsString("minute") => MinuteGenerator(known) case JsString("second") => SecondTimeGenerator(known) case JsString("ms") => MillisecondTimeGenerator(known) case JsString("week") => WeekGenerator(known) case JsString("dow") => DayOfWeekGenerator(known) case JsString("doy") => DayOfYearGenerator(known) case JsString("now") => NowGenerator(known) case JsString("dt::diff") => DateTimeDifferenceGenerator(known) case JsString("sinus") => SinusGenerator(known) case unknown => deserializationError(s"unknown Generator object: $unknown") } case unknown => deserializationError(s"unknown Generator object: $unknown") } override def write(obj: Generator[Any]): JsValue = obj.toJson }
Example 57
Source File: MonthGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.dt import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.dt.MonthTimeSeries import org.joda.time.LocalDateTime import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _} class MonthGenerator(name: Option[String], val base: Either[String, Generator[LocalDateTime]]) extends Generator[Int](name, "month") { override def timeseries(generators: String => Generator[Any]) = { val ts = Model.generator(generators)(base).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]] new MonthTimeSeries(ts) } override def toString = "MonthGenerator()" override def equals(o: Any) = o match { case that: MonthGenerator => (that.name == this.name) && (that.base == this.base) case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "base" -> either2json(base) ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object MonthGenerator extends DefaultJsonProtocol with TimeToJson { def apply(name: Option[String], base: String) = new MonthGenerator(name, Left(base)) def apply(name: Option[String], base: Generator[LocalDateTime]) = new MonthGenerator(name, Right(base)) def apply(json: JsValue): MonthGenerator = { val fields = json.asJsObject.fields val name = fields.get("name") .map(f => f match { case JsString(x) => x }) val base = fields("base") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]]) } new MonthGenerator(name, base) } }
Example 58
Source File: HourGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.dt import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.dt.{DayOfYearTimeSeries, HourTimeSeries} import org.joda.time.LocalDateTime import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _} class HourGenerator(name: Option[String], base: Either[String, Generator[LocalDateTime]]) extends Generator[Int](name, "hour") { override def timeseries(generators: String => Generator[Any]) = { val ts = Model.generator(generators)(base).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]] new HourTimeSeries(ts) } override def toString = "HourGenerator()" override def equals(o: Any) = o match { case that: HourGenerator => that.name == this.name case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "base" -> either2json(base) ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object HourGenerator extends DefaultJsonProtocol with TimeToJson { def apply(name: Option[String], base: String) = new HourGenerator(name, Left(base)) def apply(name: Option[String], base: Generator[LocalDateTime]) = new HourGenerator(name, Right(base)) def apply(json: JsValue): HourGenerator = { val fields = json.asJsObject.fields val name = fields.get("name") .map(f => f match { case JsString(x) => x }) val base = fields("base") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]]) } new HourGenerator(name, base) } }
Example 59
Source File: DateTimeDifferenceGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.dt import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.dt.DateTimeDifferenceTimeSeries import org.joda.time.{Duration, LocalDateTime} import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _} class DateTimeDifferenceGenerator(name: Option[String], val a: Either[String, Generator[LocalDateTime]], val b: Either[String, Generator[LocalDateTime]]) extends Generator[Duration](name, "dt::diff") { override def timeseries(generators: String => Generator[Any]) = { val aTS = Model.generator(generators)(a).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]] val bTS = Model.generator(generators)(b).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]] new DateTimeDifferenceTimeSeries(aTS, bTS) } override def toString = s"DateTimeDifferenceGenerator(${a}, ${b})" override def equals(o: Any) = o match { case that: DateTimeDifferenceGenerator => that.name == this.name && this.a == that.a && this.b == that.b case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "a" -> either2json(a), "b" -> either2json(b) ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object DateTimeDifferenceGenerator extends DefaultJsonProtocol with TimeToJson { def apply(name: Option[String], a: String, b: String) = new DateTimeDifferenceGenerator(name, Left(a), Left(b)) def apply(name: Option[String], a: String, b: Generator[LocalDateTime]) = new DateTimeDifferenceGenerator(name, Left(a), Right(b)) def apply(name: Option[String], a: Generator[LocalDateTime], b: String) = new DateTimeDifferenceGenerator(name, Right(a), Left(b)) def apply(name: Option[String], a: Generator[LocalDateTime], b: Generator[LocalDateTime]) = new DateTimeDifferenceGenerator(name, Right(a), Right(b)) def apply(json: JsValue): DateTimeDifferenceGenerator = { val fields = json.asJsObject.fields val name = fields.get("name") .map(f => f match { case JsString(x) => x }) val a = fields("a") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]]) } val b = fields("b") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]]) } new DateTimeDifferenceGenerator(name, a, b) } }
Example 60
Source File: SecondTimeGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.dt import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.dt.{MinuteTimeSeries, SecondTimeSeries} import org.joda.time.LocalDateTime import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _} class SecondTimeGenerator(name: Option[String], val base: Either[String, Generator[LocalDateTime]]) extends Generator[Int](name, "second") { override def timeseries(generators: String => Generator[Any]) = { val ts = Model.generator(generators)(base).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]] new SecondTimeSeries(ts) } override def toString = "SecondTimeGenerator()" override def equals(o: Any) = o match { case that: SecondTimeGenerator => (that.name == this.name) && (that.base == this.base) case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "base" -> either2json(base) ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object SecondTimeGenerator extends DefaultJsonProtocol with TimeToJson { def apply(name: Option[String], base: String) = new SecondTimeGenerator(name, Left(base)) def apply(name: Option[String], base: Generator[LocalDateTime]) = new SecondTimeGenerator(name, Right(base)) def apply(json: JsValue): SecondTimeGenerator = { val fields = json.asJsObject.fields val name = fields.get("name") .map(f => f match { case JsString(x) => x }) val base = fields("base") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]]) } new SecondTimeGenerator(name, base) } }
Example 61
Source File: DayOfWeekGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.dt import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.dt.DayOfWeekTimeSeries import org.joda.time.LocalDateTime import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _} class DayOfWeekGenerator(name: Option[String], base: Either[String, Generator[LocalDateTime]]) extends Generator[Int](name, "dow") { override def timeseries(generators: String => Generator[Any]) = { val ts = Model.generator(generators)(base).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]] new DayOfWeekTimeSeries(ts) } override def toString = "DayOfWeekGenerator()" override def equals(o: Any) = o match { case that: DayOfWeekGenerator => that.name == this.name case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "base" -> either2json(base) ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object DayOfWeekGenerator extends DefaultJsonProtocol with TimeToJson { def apply(name: Option[String], base: String) = new DayOfWeekGenerator(name, Left(base)) def apply(name: Option[String], base: Generator[LocalDateTime]) = new DayOfWeekGenerator(name, Right(base)) def apply(json: JsValue): DayOfWeekGenerator = { val fields = json.asJsObject.fields val name = fields.get("name") .map(f => f match { case JsString(x) => x }) val base = fields("base") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]]) } new DayOfWeekGenerator(name, base) } }
Example 62
Source File: DayOfMonthGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.dt import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.composite.TimeShiftTimeSeries import be.cetic.tsimulus.timeseries.dt.{DayOfMonthTimeSeries, MonthTimeSeries} import org.joda.time.LocalDateTime import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _} class DayOfMonthGenerator(name: Option[String], val base: Either[String, Generator[LocalDateTime]]) extends Generator[Int](name, "dom") { override def timeseries(generators: String => Generator[Any]) = { val ts = Model.generator(generators)(base).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]] new DayOfMonthTimeSeries(ts) } override def toString = s"DayOfMonthGenerator(${base})" override def equals(o: Any) = o match { case that: DayOfMonthGenerator => that.name == this.name && this.base == that.base case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "base" -> either2json(base) ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object DayOfMonthGenerator extends DefaultJsonProtocol with TimeToJson { def apply(name: Option[String], base: String) = new DayOfMonthGenerator(name, Left(base)) def apply(name: Option[String], base: Generator[LocalDateTime]) = new DayOfMonthGenerator(name, Right(base)) def apply(json: JsValue): DayOfMonthGenerator = { val fields = json.asJsObject.fields val name = fields.get("name") .map(f => f match { case JsString(x) => x }) val base = fields("base") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]]) } new DayOfMonthGenerator(name, base) } }