Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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