Skip to content

Commit

Permalink
'Merge'
Browse files Browse the repository at this point in the history
  • Loading branch information
mechkg committed Apr 10, 2012
2 parents 9870f93 + 720e3ea commit 7dfbb59
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.workcraft.plugins.petrify
import java.io.File
import org.workcraft.plugins.petri2.PnFormatParser
import org.workcraft.services.FileOpen
import org.workcraft.services.FileOpenJob
import org.workcraft.services.Format
import org.workcraft.scala.effects.IO._

object DotGFileOpen extends FileOpen {
val description = "Workcraft .g format importer"
val sourceFormat = Format.DotG

def checkFile(file: File) = ioPure.pure { file.getName().endsWith(".g") }

override def open(file: File) = checkFile(file).map(
if (_)
Some(FileOpenJob(
DotGParser.parseDotG (file).map {
case Left(error) => Left(error)
case Right(dotg) => PetriNetBuilder.buildPetriNet(dotg).map(pn => Right(new PetriNetModel(n)))}.join))
else
None)
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,38 +63,6 @@ object DotGParser extends Parsers with RegexParsers {
marking ++ other.marking)
}

/* class PetriNetBuilder(parseResult: DotG) {
import Direction._
import GraphElement._
import PlaceRef._
def isDummy(name: String) = parseResult.dummy.contains(name)
def stName(t: SignalTransition) = t.name + "_" + (t.direction match {
case Plus => "plus"
case Minus => "minus"
case Toggle => "toggle"
}) + (if (t.instance == 0) "" else "_" + t.instance)
def getOrCreateTransition (name: String, net: PetriNet) = net.names.get(name) match {
case Some(t:Transition) => (t, net)
case None => (new Transition, net.copy (transitions = net.transitions + t))
}
def buildPetriNet: PetriNet = parseResult.graph.foldLeft(PetriNet.Empty) {
case (net, (elem, postset)) => {
val src = elem match {
case t:SignalTransition => stName(t)
case PlaceOrDummy(name) => if (isDummy(name)) Some(name) else None
}
}
}
}*/

import Direction._
import GraphElement._
import PlaceRef._
Expand Down Expand Up @@ -140,20 +108,25 @@ object DotGParser extends Parsers with RegexParsers {

def stgFile = phrase((emptyline*) ~> stg <~ (emptyline*))

def parseDotG(file: File) = parse(stgFile, (new BufferedReader(new FileReader(file)))) match {
case Success(r, _) => Right(r)
case err => Left(err.toString)
def parseDotG(file: File): IO[Either[String, DotG]] = ioPure.pure {
parse(stgFile, (new BufferedReader(new FileReader(file)))) match {
case Success(r, _) => Right(r)
case err => Left(err.toString)
}
}

def parseTask(file: File) = new Task[DotG, String] {
def runTask(tc: TaskControl) = tc.descriptionUpdate("Reading " + file.getPath) >>=| (ioPure.pure { parseDotG(file) } map { case Right(result) => Right(result); case Left(error) => Left(Some(error)) })
def runTask(tc: TaskControl) =
(tc.descriptionUpdate("Reading " + file.getPath) >>=| (parseDotG(file))).map {
case Left(err) => Left(Some(err))
case Right(dotg) => Right(dotg)
}
}
}

object Test extends App {
println("""(?m)$""".matches("\n"))
DotGParser.parseDotG(new File("e:/winpetrify/stgshka.g")) match {
case Left(err) => println (err)
case Right(dotg) => println (PetriNetBuilder.buildPetriNet(dotg).unsafePerformIO)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ object PetriNetBuilder {
} yield {
val (implicitPlaces, net) = result
((implicitPlaces + ((t1, t2) -> p)), net.copy(arcs = arc1 :: arc2 :: net.arcs, places = p :: net.places,
labelling = net.labelling + (p -> name)))
labelling = net.labelling + (p -> name)))
}
}
case _ => throw new RuntimeException("The STG specification contains an invalid arc between places")
Expand Down

0 comments on commit 7dfbb59

Please sign in to comment.