Commit 01d8a016 authored by Arthur Bit-Monnot's avatar Arthur Bit-Monnot

[pddl] Minor reorganization.

parent 387d0e6c
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
}
}
}
}
......@@ -15,9 +15,10 @@ abstract class FunctionCompat() {
def name: String
def model: FluentTemplate
def condition(e: Exp): TimedEqualAssertion
def condition(e: Exp): TimedEqualAssertion // TODO: should probably take a context
def effect(e: Exp): TimedAssignmentAssertion
}
object FunctionCompat {
def apply(pddl: NamedTypedList)(implicit ctx: Ctx): FunctionCompat = {
pddl.getTypes.asScala match {
......@@ -27,6 +28,7 @@ object FunctionCompat {
}
}
}
class DefaultPredicate(pddl: NamedTypedList)(implicit ctx: Ctx) extends FunctionCompat {
override val name: String = pddl.getName.getImage
private val tpe = pddl.getTypes.asScala match {
......@@ -35,11 +37,11 @@ class DefaultPredicate(pddl: NamedTypedList)(implicit ctx: Ctx) extends Function
}
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))
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 =>
case ast.Fluent(fun, args) if fun == name =>
TimedEqualAssertion(
Fluent(model, args.map(ctx.variable)),
PddlPredef.True,
......@@ -50,7 +52,7 @@ class DefaultPredicate(pddl: NamedTypedList)(implicit ctx: Ctx) extends Function
}
override def effect(e: Exp): TimedAssignmentAssertion = e match {
case AST.Fluent(fun, args) if fun == name =>
case ast.Fluent(fun, args) if fun == name =>
TimedAssignmentAssertion(
Fluent(model, args.map(ctx.variable)),
predef.True,
......@@ -70,11 +72,11 @@ class DefaultFunction(pddl: NamedTypedList)(implicit ctx: Ctx) extends FunctionC
}
override val model =
FluentTemplate(id(name), tpe, pddl.getArguments.asScala.map {
case AST.TypedSymbol(argName, argType) => common.Arg(id(argName), typeOf(argType))
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 =>
case ast.Eq(ast.Fluent(funName, args), ast.Cst(rhs)) if funName == name =>
TimedEqualAssertion(
Fluent(model, args.map(ctx.variable)),
rhs,
......@@ -85,7 +87,7 @@ class DefaultFunction(pddl: NamedTypedList)(implicit ctx: Ctx) extends FunctionC
}
override def effect(e: Exp): TimedAssignmentAssertion = e match {
case AST.Eq(AST.Fluent(funName, args), AST.Cst(rhs)) if funName == name =>
case ast.Eq(ast.Fluent(funName, args), ast.Cst(rhs)) if funName == name =>
TimedAssignmentAssertion(
Fluent(model, args.map(ctx.variable)),
rhs,
......
......@@ -14,11 +14,11 @@ import scala.language.implicitConversions
object Main extends App {
// val domFile = "/home/arthur/work/fape/planning/domains/blocks_ipc2/pddl/blocks_ipc2.dom.pddl"
// val pbFile = "/home/arthur/work/fape/planning/domains/blocks_ipc2/pddl/blocks_ipc2.p04-0.pb.pddl"
val domFile =
"/home/arthur/work/ext/rcll/temporal_1_robot/rcll_domain_production_durations_nors.pddl"
val pbFile = "/home/arthur/work/ext/rcll/temporal_1_robot/problem-001-r1-o1-durations.pddl"
val domFile = "/home/arthur/work/fape/planning/domains/blocks_ipc2/pddl/blocks_ipc2.dom.pddl"
val pbFile = "/home/arthur/work/fape/planning/domains/blocks_ipc2/pddl/blocks_ipc2.p04-0.pb.pddl"
// val domFile =
// "/home/arthur/work/ext/rcll/temporal_1_robot/rcll_domain_production_durations_nors.pddl"
// val pbFile = "/home/arthur/work/ext/rcll/temporal_1_robot/problem-001-r1-o1-durations.pddl"
val parser = new Parser()
parser.parse(domFile, pbFile)
......@@ -56,8 +56,8 @@ object Main extends App {
def id(name: String): Id = Id(common.RootScope, name)
def recordType(tpe: AST.Tpe): Unit = {
val AST.Tpe(name, parent) = tpe
def recordType(tpe: ast.Tpe): Unit = {
val ast.Tpe(name, parent) = tpe
assert(!hasType(name), s"type already recorded: $name")
assert(parent.forall(hasType), s"parent not recorded: $parent")
val pt = parent match {
......@@ -72,6 +72,7 @@ object Main extends App {
(name, pt) match {
case ("object", None) => rec(TypeDeclaration(Type.ObjSubType(id("object"), Type.ObjectTop)))
case (other, Some(father)) => rec(TypeDeclaration(Type.ObjSubType(id(other), father)))
case _ => unexpected
}
}
......@@ -83,16 +84,16 @@ object Main extends App {
def recordInitialState(e: Exp): Unit = {
val assertion = e match {
case AST.AssertionOnFunction(funcName) =>
case ast.AssertionOnFunction(funcName) =>
getTranslator(funcName).effect(e)
}
rec(TemporallyQualifiedAssertion(Equals(Interval(predef.Start, predef.Start)), assertion))
}
def recordGoal(e: Exp): Unit = e match {
case AST.And(goals) =>
case ast.And(goals) =>
goals.foreach(recordGoal)
case AST.AssertionOnFunction(name) =>
case ast.AssertionOnFunction(name) =>
val assertion = getTranslator(name).condition(e)
rec(
TemporallyQualifiedAssertion(
......@@ -105,12 +106,12 @@ object Main extends App {
}
val types = dom.getTypes.asScala.map {
case AST.ReadTpe(tpe: AST.Tpe) => tpe
case ast.ReadTpe(tpe: ast.Tpe) => tpe
}
val queue = mutable.Queue(types: _*)
while(queue.nonEmpty) {
queue.dequeue() match {
case x @ AST.Tpe(name, parentOpt) if !Factory.hasType(name) =>
case x @ ast.Tpe(name, parentOpt) if !Factory.hasType(name) =>
parentOpt match {
case None => Factory.recordType(x)
case Some(parent) if !Factory.hasType(parent) => queue.enqueue(x)
......@@ -125,13 +126,13 @@ object Main extends App {
// make sure we fail if part of the domain is not supported
Option(dom.getConstraints).foreach(_ => ???)
dom.getConstants.asScala.foreach {
case AST.TypedSymbol(name, tpe) => Factory.recordInstance(name, tpe)
case ast.TypedSymbol(name, tpe) => Factory.recordInstance(name, tpe)
}
dom.getDerivesPredicates.asScala.foreach(_ => ???)
pb.getObjects.asScala.foreach {
case AST.TypedSymbol(name, tpe) => Factory.recordInstance(name, tpe)
case ast.TypedSymbol(name, tpe) => Factory.recordInstance(name, tpe)
}
pb.getInit.asScala.foreach(Factory.recordInitialState)
Factory.recordGoal(pb.getGoal)
......
package dahu.planning.pddl
import dahu.planning.model.common.Term
import dahu.planning.model.common.{Predef, Term}
import dahu.planning.model.full.CommonTerm
import scala.language.implicitConversions
......@@ -8,7 +8,7 @@ import scala.language.implicitConversions
object Utils {
private var counter = 0
implicit val predef = PddlPredef
implicit val predef: Predef = PddlPredef
def next(): Int = { counter += 1; counter }
implicit def term2FullModel(v: Term): CommonTerm = CommonTerm(v)
......
package dahu.planning.pddl.ast
import dahu.planning.model.common.{Cst, IntLiteral}
import dahu.planning.pddl.PddlPredef
import fr.uga.pddl4j.parser._
import dahu.utils.errors._
import scala.collection.JavaConverters._
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])
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
}
}
}
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