Commit 387d0e6c authored by Arthur Bit-Monnot's avatar Arthur Bit-Monnot

[pddl] Split into multiple files.

parent 0b41be4b
......@@ -272,7 +272,7 @@ package object full {
trait Ctx {
def scope: Scope
def id(name: String): Id = new Id(scope, name)
def id(name: String): Id = Id(scope, name)
def parent: Option[Ctx]
def name: String
......
package dahu.planning.pddl
import dahu.planning.model.common.{Cst, IntLiteral}
import fr.uga.pddl4j.parser._
import dahu.utils.errors._
import scala.collection.JavaConverters._
object AST {
object AssertionOnFunction {
def unapply(e: Exp): Option[String] = e match {
case Fluent(name, _) => Some(name)
case Eq(Fluent(name, _), _) => Some(name)
case _ => None
}
}
object Fluent {
def unapply(exp: Exp): Option[(String, List[String])] = {
if(exp.getConnective == Connective.ATOM || exp.getConnective == Connective.FN_HEAD) {
exp.getAtom.asScala.toList.map(_.getImage) match {
case head :: tail => Some((head, tail))
case _ => None
}
} else {
None
}
}
}
object Cst {
def unapply(e: Exp): Option[Cst] = {
if(e.getConnective == Connective.NUMBER)
Some(IntLiteral(PddlPredef.discretize(e.getValue)))
else
None
}
}
object Eq {
def unapply(e: Exp): Option[(Exp, Exp)] = {
if(e.getConnective == Connective.FN_ATOM) {
e.getChildren.asScala.toList match {
case lhs :: rhs :: Nil => Some((lhs, rhs))
case _ => unexpected
}
} else {
None
}
}
}
case class Tpe(name: String, parent: Option[String])
case class ReadTypedSymbol(name: String, tpe: String)
object ReadTpe {
def unapply(e: TypedSymbol): Option[Tpe] =
if(e.getKind.name() == "TYPE") {
e.getTypes.asScala.map(_.getImage).toList match {
case Nil => Some(Tpe(e.getImage, None))
case parent :: Nil => Some(Tpe(e.getImage, Some(parent)))
case _ => None
}
} else
None
}
object And {
def unapply(e: Exp): Option[List[Exp]] = {
if(e.getConnective == Connective.AND) {
Some(e.getChildren.asScala.toList)
} else {
None
}
}
}
object TypedSymbol {
def unapply(e: TypedSymbol): Option[(String, String)] = {
if(e.getKind == Symbol.Kind.VARIABLE || e.getKind == Symbol.Kind.CONSTANT) {
val name =
if(e.getImage.startsWith("?"))
e.getImage.drop(1)
else
e.getImage
e.getTypes.asScala.toList match {
case tpe :: Nil => Some((name, tpe.getImage))
case _ => None
}
} else {
None
}
}
}
}
package dahu.planning.pddl
import dahu.planning.model.common.{Id, Type}
import dahu.planning.model.full.StaticExpr
trait Ctx {
def typeOf(name: String): Type
def id(name: String): Id
def variable(name: String): StaticExpr
def nextId(): String
}
object Ctx {
def typeOf(name: String)(implicit ctx: Ctx): Type = ctx.typeOf(name)
def id(name: String)(implicit ctx: Ctx): Id = ctx.id(name)
}
package dahu.planning.pddl
import dahu.planning.model.common
import dahu.planning.model.common._
import dahu.planning.model.full._
import dahu.planning.pddl.Utils._
import dahu.planning.pddl.Ctx._
import dahu.utils.errors._
import fr.uga.pddl4j.parser._
import scala.collection.JavaConverters._
abstract class FunctionCompat() {
def name: String
def model: FluentTemplate
def condition(e: Exp): TimedEqualAssertion
def effect(e: Exp): TimedAssignmentAssertion
}
object FunctionCompat {
def apply(pddl: NamedTypedList)(implicit ctx: Ctx): FunctionCompat = {
pddl.getTypes.asScala match {
case Seq() => new DefaultPredicate(pddl)
case Seq(tpe) => new DefaultFunction(pddl)
case _ => unexpected
}
}
}
class DefaultPredicate(pddl: NamedTypedList)(implicit ctx: Ctx) extends FunctionCompat {
override val name: String = pddl.getName.getImage
private val tpe = pddl.getTypes.asScala match {
case Seq() => PddlPredef.Boolean
case _ => unexpected
}
override val model =
FluentTemplate(ctx.id(name), tpe, pddl.getArguments.asScala.map {
case AST.TypedSymbol(argName, argType) => common.Arg(ctx.id(argName), ctx.typeOf(argType))
})
override def condition(e: Exp): TimedEqualAssertion = e match {
case AST.Fluent(fun, args) if fun == name =>
TimedEqualAssertion(
Fluent(model, args.map(ctx.variable)),
PddlPredef.True,
None, //TODO
ctx.nextId()
)
case _ => unexpected
}
override def effect(e: Exp): TimedAssignmentAssertion = e match {
case AST.Fluent(fun, args) if fun == name =>
TimedAssignmentAssertion(
Fluent(model, args.map(ctx.variable)),
predef.True,
None, //TODO
dahu.planning.model.reservedPrefix + next()
)
case _ => unexpected
}
}
class DefaultFunction(pddl: NamedTypedList)(implicit ctx: Ctx) extends FunctionCompat {
override val name: String = pddl.getName.getImage
private val tpe = pddl.getTypes.asScala match {
case Seq(t) => typeOf(t.getImage)
case _ => unexpected
}
override val model =
FluentTemplate(id(name), tpe, pddl.getArguments.asScala.map {
case AST.TypedSymbol(argName, argType) => common.Arg(id(argName), typeOf(argType))
})
override def condition(e: Exp): TimedEqualAssertion = e match {
case AST.Eq(AST.Fluent(funName, args), AST.Cst(rhs)) if funName == name =>
TimedEqualAssertion(
Fluent(model, args.map(ctx.variable)),
rhs,
None, //TODO
dahu.planning.model.reservedPrefix + next()
)
case _ => unexpected
}
override def effect(e: Exp): TimedAssignmentAssertion = e match {
case AST.Eq(AST.Fluent(funName, args), AST.Cst(rhs)) if funName == name =>
TimedAssignmentAssertion(
Fluent(model, args.map(ctx.variable)),
rhs,
None, //TODO
dahu.planning.model.reservedPrefix + next()
)
case _ => unexpected
}
}
package dahu.planning.pddl
import dahu.planning.model.common._
import dahu.planning.model.common.Type._
import dahu.planning.model.full._
object PddlPredef extends Predef {
private val scope = RootScope + "_predef_"
override val Time: IRealType = IntSubType(scope / "time", Integers)
override val Boolean: BooleanType = BooleanType(scope / "boolean")
override val True: Instance = Instance(scope / "true", Boolean)
override val False: Instance = Instance(scope / "false", Boolean)
override val Start = LocalVar(scope / "start", Time)
override val End = LocalVar(scope / "end", Time)
val Number = IntSubType(RootScope / "number", Integers)
val discretization = 1000
def discretize(d: Double): Int = (d * discretization).toInt
override def baseModel: Model =
(Model() ++ Seq(
TypeDeclaration(ObjectTop),
TypeDeclaration(Boolean),
TypeDeclaration(Reals),
TypeDeclaration(Integers),
TypeDeclaration(Time),
TypeDeclaration(Number),
InstanceDeclaration(True),
InstanceDeclaration(False),
LocalVarDeclaration(Start),
LocalVarDeclaration(End),
)).getOrElse(sys.error("Could not instantiate base model"))
}
package dahu.planning.pddl
import dahu.planning.model.common.Term
import dahu.planning.model.full.CommonTerm
import scala.language.implicitConversions
object Utils {
private var counter = 0
implicit val predef = PddlPredef
def next(): Int = { counter += 1; counter }
implicit def term2FullModel(v: Term): CommonTerm = CommonTerm(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