Commit d69f442b authored by Arthur Bit-Monnot's avatar Arthur Bit-Monnot

Move action instantiation to its own file.

parent a8d930b6
......@@ -14,12 +14,14 @@ package object core {
sealed trait Declaration[T] {
def id: Id
}
case class TimepointDeclaration(tp: Timepoint) extends Declaration[Timepoint] with Statement {
final case class TimepointDeclaration(tp: Timepoint)
extends Declaration[Timepoint]
with Statement {
override def id: Id = tp.id
override def toString: String = s"timepoint $id"
}
case class TypeDeclaration(typ: Type) extends Declaration[Type] with InModuleBlock {
final case class TypeDeclaration(typ: Type) extends Declaration[Type] with InModuleBlock {
override def id: Id = typ.id
override def toString: String = s"type $id" + {
if(typ.parent.isDefined) " < " + typ.parent.get else ""
......@@ -29,22 +31,22 @@ package object core {
def variable: Term
def id: Id = variable.id
}
case class LocalVarDeclaration(variable: LocalVar)
final case class LocalVarDeclaration(variable: LocalVar)
extends VarDeclaration[LocalVar]
with Statement {
override def toString: String = s"constant ${variable.typ} ${variable.id}"
}
case class InstanceDeclaration(instance: Instance)
final case class InstanceDeclaration(instance: Instance)
extends VarDeclaration[Instance]
with InModuleBlock {
override def variable: Instance = instance
override def toString: String = s"instance ${instance.typ} ${instance.id}"
}
case class ArgDeclaration(arg: Arg) extends VarDeclaration[Arg] with InActionBlock {
final case class ArgDeclaration(arg: Arg) extends VarDeclaration[Arg] with InActionBlock {
override def variable: Arg = arg
override def toString: String = s"${arg.typ.id} ${arg.id}"
}
case class FunctionDeclaration(func: FunctionTemplate)
final case class FunctionDeclaration(func: FunctionTemplate)
extends Declaration[FunctionTemplate]
with InModuleBlock {
override def id: Id = func.id
......@@ -62,17 +64,6 @@ package object core {
def wrapped: Seq[Block]
}
sealed trait Expr
sealed trait StaticExpr extends Expr {
def typ: Type
}
sealed trait SymExpr extends Expr {
def typ: Type
}
sealed trait TimedSymExpr extends SymExpr
sealed trait StaticSymExpr extends SymExpr with StaticExpr
sealed trait Function {
def template: FunctionTemplate
def params: Seq[Term]
......@@ -87,7 +78,7 @@ package object core {
class BoundConstant(override val template: ConstantTemplate, override val params: Seq[Cst])
extends Constant(template, params)
case class Fluent(override val template: FluentTemplate, override val params: Seq[Term])
final case class Fluent(override val template: FluentTemplate, override val params: Seq[Term])
extends Function {
require(template.params.size == params.size)
template.params.zip(params).foreach {
......@@ -98,16 +89,17 @@ package object core {
}
sealed trait StaticAssertion extends Statement
case class StaticEqualAssertion(left: Term, right: Term) extends StaticAssertion {
final case class StaticEqualAssertion(left: Term, right: Term) extends StaticAssertion {
override def toString: String = s"$left == $right"
}
case class StaticDifferentAssertion(left: Term, right: Term) extends StaticAssertion {
final case class StaticDifferentAssertion(left: Term, right: Term) extends StaticAssertion {
override def toString: String = s"$left != $right"
}
case class StaticAssignmentAssertion(left: BoundConstant, right: Cst) extends StaticAssertion {
final case class StaticAssignmentAssertion(left: BoundConstant, right: Cst)
extends StaticAssertion {
override def toString: String = s"$left := $right"
}
case class BindAssertion(constant: Constant, variable: LocalVar) extends StaticAssertion {
final case class BindAssertion(constant: Constant, variable: LocalVar) extends StaticAssertion {
override def toString: String = s"$constant == $variable"
}
......@@ -126,22 +118,22 @@ package object core {
def valueAfterChange: Term
}
case class TimedEqualAssertion(start: TPRef, end: TPRef, fluent: Fluent, value: Term)
final case class TimedEqualAssertion(start: TPRef, end: TPRef, fluent: Fluent, value: Term)
extends TimedAssertion
with RequiresSupport {
override def toString: String = s"[$start, $end] $fluent == $value"
}
case class TimedAssignmentAssertion(start: TPRef, end: TPRef, fluent: Fluent, value: Term)
final case class TimedAssignmentAssertion(start: TPRef, end: TPRef, fluent: Fluent, value: Term)
extends TimedAssertion
with ProvidesChange {
override def valueAfterChange: Term = value
override def toString: String = s"[$start,$end] $fluent := $value"
}
case class TimedTransitionAssertion(start: TPRef,
end: TPRef,
fluent: Fluent,
from: Term,
to: Term)
final case class TimedTransitionAssertion(start: TPRef,
end: TPRef,
fluent: Fluent,
from: Term,
to: Term)
extends TimedAssertion
with RequiresSupport
with ProvidesChange {
......@@ -153,16 +145,16 @@ package object core {
object IntExpr {
def apply(lit: Int): IntExpr = VarIntExpr(IntLiteral(lit))
}
case class VarIntExpr(e: Term) extends IntExpr {
require(e.typ.id.name == "integer")
final case class VarIntExpr(e: Term) extends IntExpr {
require(e.typ == Type.Integers)
override def toString: String = e.toString
}
case class Minus(e: IntExpr) extends IntExpr
case class Add(lhs: IntExpr, rhs: IntExpr) extends IntExpr
final case class Minus(e: IntExpr) extends IntExpr
final case class Add(lhs: IntExpr, rhs: IntExpr) extends IntExpr
/** A timepoint, declared when appearing in the root of a context.*/
case class TPRef(id: Timepoint, delay: IntExpr = IntExpr(0)) {
final case class TPRef(id: Timepoint, delay: IntExpr = IntExpr(0)) {
override def toString: String =
id.toString + (delay match {
......@@ -189,7 +181,8 @@ package object core {
override def toString: String = s"$from <= $to"
}
case class ActionTemplate(scope: InnerScope, content: Seq[InActionBlock]) extends InModuleBlock {
final case class ActionTemplate(scope: InnerScope, content: Seq[InActionBlock])
extends InModuleBlock {
def name: String = scope.name
lazy val args: Seq[Arg] = content.collect { case ArgDeclaration(a) => a }
......@@ -208,56 +201,12 @@ package object core {
}
.getOrElse(sys.error("No end timepoint in this action"))
/** Builds a new action instance with the given name*/
def instance(instanceName: String): Action = {
val instanceScope: InnerScope = scope.parent + instanceName
val trans: Id => Id = x => {
var transScope: Scope => Scope = null
transScope = {
case s if s == scope => scope.parent + instanceName
case RootScope => RootScope
case InnerScope(s, name) => transScope(s) + name
}
x match { case Id(s, name) => Id(transScope(s), name) }
}
// import landscaper.transformations._
//
// // landscaper has problem with the new ADT, provide some help
// implicit val x1 = Trans[Id, Id, StaticExpr]
// implicit val x2 = Trans[Id, Id, VarIntExpr]
// implicit val x3 = Trans[Id, Id, IntExpr]
// implicit val x4 = Trans[Id, Id, TPRef]
// implicit val x5 = Trans[Id, Id, TBefore] // ok
// implicit val x6 = Trans[Id, Id, StaticAssertion] //ok
// implicit val x7 = Trans[Id, Id, ArgDeclaration] //ok
// implicit val x8 = Trans[Id, Id, TimedAssignmentAssertion] //ok
// implicit val x9 = Trans[Id, Id, TimedEqualAssertion] //ok
// implicit val x10 = Trans[Id, Id, TimedTransitionAssertion] //ok
// implicit val x11 = Trans[Id, Id, TimedAssertion] //ok
// implicit val x12 = Trans[Id, Id, LocalVarDeclaration] //ok
// implicit val x13 = Trans[Id, Id, TimepointDeclaration] //ok
//
// // derivation of Trans[Id,Id,Statement] never ends for an obscure reason, dispatch manually
// implicit val x14: Trans.Aux[Id, Id, Statement, Statement] = new Trans[Id, Id, Statement] {
// override type Result = Statement
// override def rewrite(f: Id => Id, in: Statement): Result = in match {
// case x: TimedAssertion => x11.rewrite(f, x)
// case x: StaticAssertion => x6.rewrite(f, x)
// case x: TBefore => x5.rewrite(f, x)
// case x: ArgDeclaration => x7.rewrite(f, x)
// case x: LocalVarDeclaration => x12.rewrite(f, x)
// case x: TimepointDeclaration => x13.rewrite(f, x)
// }
// }
// val instanceContent = content.map(s => x14.rewrite(trans, s))
// Action(instanceScope, instanceContent, this)
???
}
}
/** Instance of an action template */
case class Action(scope: InnerScope, content: Seq[InActionBlock], template: ActionTemplate) {
final case class Action(scope: InnerScope,
content: Seq[InActionBlock],
template: ActionTemplate) {
def name: String = scope.name
val args: Seq[Arg] = content.collect { case ArgDeclaration(a) => a }
......
package copla.lang.model.transforms
import copla.lang.model.common._
import copla.lang.model.core._
import landscaper.transformations._
object ActionInstantiation {
private[this] object implicits {
type Up[T] = Trans.Aux[Id, Id, T, T]
def instance[T](implicit ev: Up[T]): Up[T] = ev
// landscaper has problem with the new ADT, provide some help
implicit val x3 = instance[IntExpr]
implicit val x4 = instance[TPRef]
implicit val x5 = instance[TBefore] // ok
implicit val x6 = instance[StaticAssertion] //ok
implicit val x7 = instance[ArgDeclaration] //ok
implicit val x8 = instance[TimedAssignmentAssertion] //ok
implicit val x9 = instance[TimedEqualAssertion] //ok
implicit val x10 = instance[TimedTransitionAssertion] //ok
implicit val x11 = instance[TimedAssertion] //ok
implicit val x12 = instance[LocalVarDeclaration] //ok
implicit val x13 = instance[TimepointDeclaration] //ok
// derivation of Trans[Id,Id,InActionBlock] never ends for an obscure reason, dispatch manually
implicit val main: Up[InActionBlock] = new Trans[Id, Id, InActionBlock] {
override type Result = InActionBlock
override def rewrite(f: Id => Id, in: InActionBlock): Result = in match {
case x: TimedAssertion => x11.rewrite(f, x)
case x: StaticAssertion => x6.rewrite(f, x)
case x: TBefore => x5.rewrite(f, x)
case x: ArgDeclaration => x7.rewrite(f, x)
case x: LocalVarDeclaration => x12.rewrite(f, x)
case x: TimepointDeclaration => x13.rewrite(f, x)
}
}
}
/** Builds a new action instance with the given name*/
def instance(template: ActionTemplate, instanceName: String): Action = {
val instanceScope: InnerScope = template.scope.parent + instanceName
val trans: Id => Id = x => {
var transScope: Scope => Scope = null
transScope = {
case s if s == template.scope => template.scope.parent + instanceName
case RootScope => RootScope
case InnerScope(s, name) => transScope(s) + name
}
x match { case Id(s, name) => Id(transScope(s), name) }
}
//
val instanceContent = template.content.map(s => implicits.main.rewrite(trans, s))
Action(instanceScope, instanceContent, template)
}
}
......@@ -247,7 +247,7 @@ abstract class AnmlParser(val initialContext: Ctx) {
}
val intExpr: Parser[IntExpr] =
staticExpr.namedFilter(_.typ.id.name == "integer", "of-type-integer").map(full.GenIntExpr(_))
staticExpr.namedFilter(_.typ == Type.Integers, "of-type-integer").map(full.GenIntExpr(_))
val expr: Parser[Expr] =
timedSymExpr | staticExpr
......
......@@ -22,7 +22,7 @@ class ActionInstantiationTest extends FunSuite {
model.collectFirst { case x: ActionTemplate => x } match {
case Some(act) =>
val name = "myInstance"
val instance = act.instance(name)
val instance = ActionInstantiation.instance(act, name)
assert(instance.name == name)
// check that the name template has disappeared
......
......@@ -68,6 +68,7 @@ lazy val anml = project
"com.lihaoyi" %% "fastparse" % "1.0.0",
"com.github.scopt" %% "scopt" % "3.7.0",
"com.github.arthur-bit-monnot" %% "landscaper" % "0.1.2",
"com.propensive" %% "magnolia" % "0.7.1",
"org.scalatest" %% "scalatest" % "3.0.5" % "test"
))
......
......@@ -42,7 +42,8 @@ object anml extends Module {
def ivyDeps = Agg(
ivy"com.lihaoyi::fastparse:1.0.0",
ivy"com.github.scopt::scopt:3.7.0",
ivy"com.github.arthur-bit-monnot::landscaper:0.1.2"
ivy"com.github.arthur-bit-monnot::landscaper:0.1.2",
ivy"com.propensive::magnolia:0.7.1"
)
object tests extends Tests {
......
......@@ -2,6 +2,7 @@ package dahu.planner
import copla.lang.model.common._
import copla.lang.model.core._
import copla.lang.model.transforms.ActionInstantiation
import dahu.model.input.{Ident, Input, Tentative}
case class Action[F[_]](name: String,
......@@ -15,7 +16,7 @@ object Action {
def instance(template: ActionTemplate, ctx: ProblemContext): Action[Tentative] = {
counter += 1
val act = template.instance(s"${template.name}_$counter")
val act = ActionInstantiation.instance(template, s"${template.name}_$counter")
val argsRewrite: Arg => Tentative[Literal] = {
case a @ Arg(_, tpe) => Input(Ident(a))(ctx.specializedTags(tpe))
}
......
......@@ -297,10 +297,7 @@ case class Chronicle(ctx: ProblemContext,
def extended(e: InActionBlock)(implicit argRewrite: Arg => Tentative[Literal]): Chronicle =
e match {
case _: TypeDeclaration => this
case _: InstanceDeclaration => this
case _: TimepointDeclaration => this
case _: FunctionDeclaration => this
case _: LocalVarDeclaration => this
case _: ArgDeclaration => this
case BindAssertion(c, v) =>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment