class Pione::PNML::Compiler

Public Class Methods

compile(net, option={}) click to toggle source

Compile from the net to PIONE document.

@param net [PNML::Net]

source net

@return [String]

compiled PIONE document
# File lib/pione/pnml/compiler.rb, line 10
def self.compile(net, option={})
  new(net, option).compile
end
new(net, option={}) click to toggle source
# File lib/pione/pnml/compiler.rb, line 14
def initialize(net, option={})
  @net = net
  @net_name = option[:flow_rule_name] || "Main"
  @env = option[:env] || Lang::Environment.new
  setup_env(option[:package_pione])

  @option = option
  @net_rewriter = NetRewriter.new do |rules|
    rules << IsolatedElementElimination
    rules << InvalidArcElimination
    rules << OutputReduction
    rules << InputReduction
    rules << IOExpansion
    rules << InputMergeComplement
    rules << InputParallelizationComplement
    rules << OutputDecompositionComplement
    rules << OutputSynchronizationComplement
    rules << TicketInstantiation
  end
  @actions = []
end

Public Instance Methods

compile() click to toggle source

Compile a PNML file into PIONE document as a string.

# File lib/pione/pnml/compiler.rb, line 53
def compile
  # annotations
  annotations = AnnotationExtractor.new(@net, @option).extract
  declarations = DeclarationExtractor.new(@env, @net).extract

  # apply net rewriting rules
  @net_rewriter.rewrite(@net, @env)

  # build rules
  rules, flow_elements = ConstituentRuleBuilder.new(@net, @net_name, @env).build()
  definition_main = FlowRuleBuilder.new(@net, @net_name, @env).build(
    flow_elements,
    declarations.params,
    declarations.features,
    declarations.variable_bindings
  )

  # merge literate actions
  rules.each do |rule|
    if @option[:literate_actions]
      if action = @option[:literate_actions][rule.name]
        rule.action_content = action[:content]
      end
    end
  end

  # textize
  sections = []
  if annotations and annotations.size > 0
    sections << annotations << ""
  end
  sections << definition_main.textize
  rules.each {|rule| sections << rule.textize}
  return sections.join("\n")
end
setup_env(package_pione) click to toggle source
# File lib/pione/pnml/compiler.rb, line 36
def setup_env(package_pione)
  @env = @env.setup_new_package(:pnml_compiler)

  if package_pione and package_pione.exist?
    parsed = Lang::DocumentParser.new.parse(package_pione.read)
    package_document = Lang::DocumentTransformer.new.apply(parsed, {package_name: true, filename: true})
    package_document.eval(@env)
  end

  val = Lang::KeyedSequence.new
  val = val.put(Lang::IntegerSequence.of(1), Lang::DataExprSequence.of("pnml_compiler"))
  @env.variable_set!(Lang::Variable.new("I"), val)
  @env.variable_set!(Lang::Variable.new("*"), Lang::StringSequence.of("pnml_compiler"))
  @env.variable_set!(Lang::Variable.new("O"), val)
end