Commit 09ad28b8 authored by Arthur Bit-Monnot's avatar Arthur Bit-Monnot

[anml] Change package layout

parent adb217e9
......@@ -15,7 +15,8 @@ case class Action[F[_]](name: String,
object Action {
var counter = 0
def instance(template: ActionTemplate, ctx: ProblemContext): Action[Tentative] = {
def instance(template: ActionTemplate, ctx: ProblemContext)(
implicit predef: Predef): Action[Tentative] = {
counter += 1
val act = ActionInstantiation.instance(template, s"${template.name}_$counter")
val argsRewrite: Arg => Tentative[Literal] = {
......
......@@ -255,7 +255,7 @@ case class ProblemContext(intTag: BoxedInt[Literal],
object ProblemContext {
import Type._
def extract(m: Seq[InModuleBlock]): ProblemContext = {
def extract(m: Seq[InModuleBlock])(implicit predef: Predef): ProblemContext = {
val objectTypes = m.collect { case TypeDeclaration(t: ObjType) => t }
val objectSubtypes = mutable.LinkedHashMap[ObjType, mutable.Set[ObjType]]()
val instances = m
......
......@@ -3,9 +3,11 @@ package dahu.planner
import dahu.planning.model.core.{ActionTemplate, Statement}
import dahu.utils.errors._
import java.io.{File, FileWriter}
import dahu.planning.anml.parser._
import dahu.planning.model.core
import dahu.model.input.Tentative
import dahu.planning.model.common.Predef
import monix.eval.{MVar, Task}
import scala.concurrent.duration._
......@@ -109,22 +111,23 @@ object Main extends App {
def solve(problemFile: File, deadline: Long)(implicit cfg: Config): Option[String] = {
info("Parsing...")
dahu.planning.parse(problemFile) match {
case dahu.planning.Success(model) =>
parse(problemFile) match {
case ParseSuccess(model) =>
solveIncremental(model, cfg.maxInstances, deadline)
case dahu.planning.ParseError(fail) =>
case fail: ParseFailure =>
println("Parsing failed:")
println(fail.format)
sys.exit(1)
case dahu.planning.Crash(msg, err) =>
println(s"Parser crashed: $msg")
err.foreach(_.printStackTrace())
case ParserCrash(err, _) =>
println(s"Parser crashed.")
err.printStackTrace()
sys.exit(1)
}
}
def solveIncremental(model: core.CoreModel, maxSteps: Int, deadline: Long)(
implicit cfg: Config): Option[String] = {
implicit cfg: Config,
predef: Predef): Option[String] = {
val q = new java.util.concurrent.ConcurrentLinkedQueue[Integer]()
for(i <- cfg.minInstances to cfg.maxInstances)
q.add(i)
......@@ -161,7 +164,8 @@ object Main extends App {
}
def solveIncrementalStep(model: core.CoreModel, step: Int, deadline: Long)(
implicit cfg: Config): Option[String] = {
implicit cfg: Config,
predef: Predef): Option[String] = {
if(System.currentTimeMillis() >= deadline)
return None
......
package dahu.planning.parsing.anml
package dahu.planning.anml.parser
import dahu.planning.model.common._
import dahu.planning.model.common.Type._
......
package dahu.planning.parsing.anml
package dahu.planning.anml.parser
import java.io.{File, IOException}
......@@ -503,14 +503,14 @@ class AnmlModuleParser(val initialModel: Model) extends AnmlParser(initialModel)
funcDecl.map(fd => {
val id = Id(t.asScope, fd.id.name)
val functionScope = t.asScope + id.name
val selfArg = new Arg(new Id(functionScope, "self"), t)
val selfArg = Arg(Id(functionScope, "self"), t)
val params = selfArg +: fd.func.params.map(arg =>
new Arg(new Id(functionScope, arg.id.name), arg.typ))
Arg(Id(functionScope, arg.id.name), arg.typ))
val template = fd.func match {
case _: FluentTemplate => new FluentTemplate(id, fd.func.typ, params)
case _: ConstantTemplate => new ConstantTemplate(id, fd.func.typ, params)
case _: FluentTemplate => FluentTemplate(id, fd.func.typ, params)
case _: ConstantTemplate => ConstantTemplate(id, fd.func.typ, params)
}
new FunctionDeclaration(template)
FunctionDeclaration(template)
})
}
......@@ -632,8 +632,8 @@ object Parser {
/** Parses an ANML string. If the previous model parameter is Some(m), then the result
* of parsing will be appended to m.
**/
def parse(input: String, previousModel: Option[Model] = None): ParseResult = {
def formatFailure(failure: Failure[Char, String]): ParseFailure = {
def parse(input: String, previousModel: Option[Model] = None): ParseResult[Model] = {
def formatFailure(failure: Failure[Char, String]): ParserFailure = {
def toLineAndColumn(lines: Seq[String], index: Int, lineNumber: Int = 0): (String, Int, Int) =
lines match {
case Seq(head, _*) if index <= head.length =>
......@@ -646,7 +646,7 @@ object Parser {
val (faultyLine, faultyLineNumber, faultyColumnNumber) =
toLineAndColumn(input.split('\n'), failure.index)
ParseFailure(faultyLine, faultyLineNumber, faultyColumnNumber, failure.lastParser, None)
ParserFailure(faultyLine, faultyLineNumber, faultyColumnNumber, failure.lastParser, None)
}
Try {
......@@ -660,24 +660,24 @@ object Parser {
}
} match {
case scala.util.Success(x) => x
case scala.util.Failure(e) => UnidentifiedError(e, None)
case scala.util.Failure(e) => ParserCrash(e, None)
}
}
private def parseFromFile(file: File, previousModel: Option[Model] = None): ParseResult = {
private def parseFromFile(file: File, previousModel: Option[Model] = None): ParseResult[Model] = {
Try {
val source = scala.io.Source.fromFile(file)
val input: String = source.getLines.mkString("\n")
source.close()
parse(input, previousModel) match {
case x: ParseSuccess => x
case x: ParseFailure => x.copy(file = Some(file))
case x: ParserFailure => x.copy(file = Some(file))
case x => x
}
} match {
case scala.util.Success(x) => x
case scala.util.Failure(e: IOException) =>
FileAccessError(file, e)
case scala.util.Failure(e) => UnidentifiedError(e, Some(file))
case scala.util.Failure(e) => ParserCrash(e, Some(file))
}
}
......@@ -686,7 +686,7 @@ object Parser {
* If the file name is formated as "XXXX.YYY.pb.anml", the file "XXXX.dom.anml" will be parsed
* first and its content prepended to the model.
**/
def parse(file: File): ParseResult = {
def parse(file: File): ParseResult[Model] = {
file.getName.split('.') match {
case Array(domId, pbId, "pb", "anml") =>
// file name formated as domainID.pbID.pb.anml, load domainID.dom.anml first
......@@ -700,36 +700,3 @@ object Parser {
}
}
}
trait ParseResult {
def flatMap(f: Model => ParseResult): ParseResult
}
case class ParseSuccess(model: Model) extends ParseResult {
override def flatMap(f: Model => ParseResult): ParseResult = f(model)
}
trait GenFailure extends ParseResult {
def format: String
override def flatMap(f: Model => ParseResult): ParseResult = this
}
case class FileAccessError(file: File, throwable: Throwable) extends GenFailure {
override def format: String =
s"Error while trying to read: $file:\n" + throwable.getLocalizedMessage
}
case class UnidentifiedError(throwable: Throwable, file: Option[File]) extends GenFailure {
def format: String =
s"Error while processing ANML input${file.map(" [" + _ + "]").getOrElse("")}:\n" + throwable.getLocalizedMessage
}
case class ParseFailure(faultyLine: String,
lineIndex: Int,
columnIndex: Int,
lastParser: Parser[Any],
file: Option[File])
extends GenFailure {
override def format: String = {
s"Could not parse anml string at (${lineIndex + 1}:${columnIndex + 1}) ${file.map("[" + _.toString + "]").getOrElse("")}:\n" +
faultyLine + "\n" +
" " * columnIndex + "^\n" +
s"Expected: $lastParser"
}
}
package dahu.planning
package dahu.planning.anml.parser
import java.io.File
import dahu.planning._
import dahu.planning.analysis.{AbstractionHierarchyType, DetailedKnoblock, Knoblock}
import dahu.planning.model.format.Formatter
import dahu.planning.model.{core, full}
......@@ -49,15 +50,16 @@ object ParserApp {
)
}
def handleResult[T](res: Result[T], handler: T => Unit): Unit = {
def handleResult[T](res: ParseResult[T], handler: T => Unit): Unit = {
res match {
case Success(model) =>
case ParseSuccess(model) =>
handler(model)
case ParseError(failure) =>
println(failure.format)
case x: Failure =>
println(s"Crash: $x")
sys.exit(1)
case ParserCrash(e, _) =>
System.err.println(s"Crash: $e")
throw e
case failure: ParseFailure =>
System.err.println(failure.format)
throw new Exception(failure.toString)
}
}
......
package dahu.planning.anml.parser
import java.io.File
import ParserApi.baseApi._
sealed trait ParseResult[+T] {
def map[B](f: T => B): ParseResult[B]
def flatMap[B](f: T => ParseResult[B]): ParseResult[B]
}
case class ParseSuccess[T](result: T) extends ParseResult[T] {
override def map[B](f: (T) => B): ParseSuccess[B] = ParseSuccess(f(result))
override def flatMap[B](f: T => ParseResult[B]): ParseResult[B] = f(result)
}
sealed trait ParseFailure extends ParseResult[Nothing] {
override def map[B](f: Nothing => B): ParseResult[B] = this
def format: String
override def flatMap[B](f: Nothing => ParseResult[B]): ParseResult[B] = this
}
final case class FileAccessError(file: File, throwable: Throwable) extends ParseFailure {
override def format: String =
s"Error while trying to read: $file:\n" + throwable.getLocalizedMessage
}
final case class ParserFailure(faultyLine: String,
lineIndex: Int,
columnIndex: Int,
lastParser: Parser[Any],
file: Option[File])
extends ParseFailure {
override def format: String = {
s"Could not parse anml string at (${lineIndex + 1}:${columnIndex + 1}) ${file.map("[" + _.toString + "]").getOrElse("")}:\n" +
faultyLine + "\n" +
" " * columnIndex + "^\n" +
s"Expected: $lastParser"
}
}
final case class ParserCrash(throwable: Throwable, file: Option[File])
extends ParseResult[Nothing] {
def format: String =
s"Error while processing ANML input${file.map(" [" + _ + "]").getOrElse("")}:\n" + throwable.getLocalizedMessage
override def map[B](f: Nothing => B): ParseResult[B] = this
override def flatMap[B](f: Nothing => ParseResult[B]): ParseResult[B] = this
}
package dahu.planning.anml
import java.io.File
import dahu.planning.model.core.CoreModel
import dahu.planning.model.transforms.FullToCore
import dahu.planning.model.{core, full}
import dahu.planning.parsing.anml
package object parser {
implicit val predef = AnmlPredef
def parse(anml: String): ParseResult[core.CoreModel] =
parseToFull(anml).map(FullToCore.trans(_))
def parse(anml: File): ParseResult[CoreModel] =
parseToFull(anml).map(FullToCore.trans(_))
def parseToFull(anmlString: String): ParseResult[full.Model] = {
Parser.parse(anmlString)
}
def parseToFull(anmlFile: File): ParseResult[full.Model] = {
Parser.parse(anmlFile)
}
}
package dahu
import java.io.File
import dahu.planning.model.common.Predef
import dahu.planning.model.core.CoreModel
import dahu.planning.model.transforms.FullToCore
import dahu.planning.model.{core, full}
import dahu.planning.parsing.anml
import dahu.planning.parsing.anml.AnmlPredef
package object planning {
implicit val predef = AnmlPredef
sealed trait Result[+T] {
def map[B](f: T => B): Result[B]
}
sealed trait Failure extends Result[Nothing] {
override def map[B](f: Nothing => B): Result[B] = this
}
case class ParseError(failure: anml.GenFailure) extends Failure
case class Crash(msg: String, throwable: Option[Throwable]) extends Failure
case class Success[T](result: T) extends Result[T] {
override def map[B](f: (T) => B): Success[B] = Success(f(result))
}
def parse(anml: String): Result[core.CoreModel] =
parseToFull(anml).map(FullToCore.trans(_))
def parse(anml: File): Result[CoreModel] =
parseToFull(anml).map(FullToCore.trans(_))
def parseToFull(anmlString: String): Result[full.Model] = {
anml.Parser.parse(anmlString) match {
case anml.ParseSuccess(model) =>
Success(model)
case x: anml.GenFailure =>
ParseError(x)
}
}
def parseToFull(anmlFile: File): Result[full.Model] = {
anml.Parser.parse(anmlFile) match {
case anml.ParseSuccess(model) =>
Success(model)
case x: anml.GenFailure =>
ParseError(x)
}
}
}
package dahu.planning.parsing.anml
package dahu.planning.anml.parser
import org.scalatest.FunSuite
......@@ -10,11 +10,12 @@ class AnmlParsingTest extends FunSuite {
case ParseSuccess(module) =>
println("PARSED:\n" + anml + "\n")
println("AS:\n" + module + "\n\n")
case x: ParseFailure =>
case x: ParserFailure =>
fail(s"Could not parse anml string: $anml\n\n${x.format}")
case UnidentifiedError(err, _) =>
case ParserCrash(err, _) =>
err.printStackTrace()
fail(s"Exception raised while parsing")
case x: FileAccessError => ???
}
}
}
......@@ -24,8 +25,9 @@ class AnmlParsingTest extends FunSuite {
Parser.parse(anml) match {
case ParseSuccess(module) =>
fail(s"Following anml string should be invalid:\n$anml\n\nParsed to:\n $module")
case x: ParseFailure =>
case x: ParserFailure =>
println(s"Detected invalid ANML:\n${x.format}")
case x => fail(s"Unexpected output: $x")
}
}
}
......@@ -40,11 +42,12 @@ class AnmlParsingTest extends FunSuite {
case ParseSuccess(module) =>
println("PARSED:\n")
println("AS:\n" + module + "\n\n")
case x: ParseFailure =>
case x: ParserFailure =>
fail(s"Could not parse anml string:\n${x.format}")
case UnidentifiedError(err, _) =>
case ParserCrash(err, _) =>
err.printStackTrace()
fail(s"Exception raised while parsing")
case _ => ???
}
}
}
package dahu.planning.model.transforms
import dahu.planning.anml.parser
import dahu.planning.anml.parser.{AnmlPredef, ParseSuccess}
import dahu.planning.model.common.Predef
import org.scalatest.FunSuite
import dahu.planning
class ActionInstantiationTest extends FunSuite {
import dahu.planning.model.core._
implicit val predef: Predef = AnmlPredef
import dahu.planning.model.core._
test("action instantiation") {
planning.parse("""
parser.parse("""
|fluent boolean sv;
|action Template(boolean x, boolean y) {
| duration == 10;
......@@ -17,7 +20,7 @@ class ActionInstantiationTest extends FunSuite {
| [all] contains id: sv == x :-> y;
|};
""".stripMargin) match {
case planning.Success(model) =>
case ParseSuccess(model) =>
model.collectFirst { case x: ActionTemplate => x } match {
case Some(act) =>
val name = "myInstance"
......
package dahu.planning.model.transforms
import dahu.planning.anml.parser.{AnmlPredef, InputAnmlModels}
import dahu.planning.model.common.Predef
import dahu.planning.model.core._
import dahu.planning.parsing.anml.{InputAnmlModels, ParseFailure, ParseSuccess, Parser}
import dahu.planning.anml.parser._
import org.scalatest.FunSuite
class FullToCoreTest extends FunSuite {
implicit val predef: Predef = AnmlPredef
def asCore(anml: String): Either[String, CoreModel] = {
Parser.parse(anml) match {
case ParseSuccess(mod) =>
......@@ -20,7 +24,7 @@ class FullToCoreTest extends FunSuite {
Parser.parse(anml) match {
case ParseSuccess(module) =>
println(FullToCore.trans(module).mkString("\n"))
case x: ParseFailure =>
case x: ParserFailure =>
// ignore, this should be caught by the ANML parsing tests
}
}
......
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