Skip to content

Instantly share code, notes, and snippets.

@doczir
Created September 5, 2016 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doczir/bfe95c470599c5b8e60b400b80f92ea2 to your computer and use it in GitHub Desktop.
Save doczir/bfe95c470599c5b8e60b400b80f92ea2 to your computer and use it in GitHub Desktop.
package org.eclipse.viatra.examples.petrinet.simulator
import org.apache.log4j.Level
import org.eclipse.viatra.examples.petrinet.model.PetriNetPackage
import org.eclipse.viatra.query.runtime.api.AdvancedViatraQueryEngine
import org.eclipse.viatra.transformation.runtime.emf.modelmanipulation.SimpleModelManipulations
import org.eclipse.viatra.transformation.runtime.emf.rules.batch.BatchTransformationRuleFactory
import org.eclipse.viatra.transformation.runtime.emf.transformation.batch.BatchTransformation
import org.eclipse.viatra.transformation.runtime.emf.transformation.batch.BatchTransformationStatements
import org.eclipse.xtext.xbase.lib.Pair
class PetriNetSimulator(val engine: AdvancedViatraQueryEngine) {
val factory = BatchTransformationRuleFactory()
val transformation: BatchTransformation
val statements: BatchTransformationStatements
val manipulation: SimpleModelManipulations
val pnPackage = PetriNetPackage.eINSTANCE
val queries = PetriNetSimulatorQueries.instance()
init {
transformation = BatchTransformation.forEngine(engine).build()
statements = transformation.transformationStatements
manipulation = SimpleModelManipulations(engine)
transformation.ruleEngine.logger.level = Level.DEBUG
}
val removeTokenRule = factory.createRule<SourcePlaceMatch, SourcePlaceMatcher>().precondition(queries.sourcePlace).action {
manipulation.remove(it.pl.tokens.first())
}.build()
val addTokenRule = factory.createRule<TargetPlaceMatch, TargetPlaceMatcher>().precondition(queries.targetPlace).action {
manipulation.createChild(it.pl, pnPackage.place_Tokens, pnPackage.token)
}.build()
val fireTransitionRule = factory.createRule<FireableTransitionMatch, FireableTransitionMatcher>().precondition(queries.fireableTransition).action {
statements.fireAllCurrent(removeTokenRule, Pair<String, Any>("t", it.t))
statements.fireAllCurrent(addTokenRule, Pair<String, Any>("t", it.t))
}.build()
fun fire(maxCount: Int) {
repeat(maxCount) {
queries.getFireableTransition(engine).forEachMatch{
println(it.prettyPrint())
}
statements.fireOne(fireTransitionRule)
Thread.sleep(1000)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment