Class: Pione::PNML::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/pione/pnml/compiler.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(net, option = {}) ⇒ Compiler

Returns a new instance of Compiler.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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

Class Method Details

.compile(net, option = {}) ⇒ String

Compile from the net to PIONE document.

Parameters:

Returns:

  • (String)

    compiled PIONE document



10
11
12
# File 'lib/pione/pnml/compiler.rb', line 10

def self.compile(net, option={})
  new(net, option).compile
end

Instance Method Details

#compileObject

Compile a PNML file into PIONE document as a string.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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