Sha256: 159659b4bc4e52c01b3b094d87dd4a4d721f7358a8b9d2387d7f5aee74765783

Contents?: true

Size: 1.53 KB

Versions: 6

Compression:

Stored size: 1.53 KB

Contents

module Pione
  module PNML
    class Reader
      # Read a PNML file at the location and return `PNML::Net`.
      #
      # @param location [Location::DataLocation]
      #   PNML file's location
      def self.read(location)
        new(location.read).read
      end

      def initialize(src)
        @doc = REXML::Document.new(src)
      end

      def read
        Net.new.tap do |net|
          net.transitions = find_transitions(@doc, net)
          net.places = find_places(@doc, net)
          net.arcs = find_arcs(@doc, net)
        end
      end

      # Find all transtions in the document.
      def find_transitions(doc, net)
        REXML::XPath.match(doc, "/pnml/net/transition").map do |elt|
          id = elt.attribute("id").value
          name = REXML::XPath.first(elt, "name/text").texts.map{|text| text.value}.join("")
          Transition.new(net, id, name)
        end
      end

      def find_places(doc, net)
        REXML::XPath.match(doc, "/pnml/net/place").map do |elt|
          id = elt.attribute("id").value
          name = REXML::XPath.first(elt, "name/text").texts.map{|text| text.value}.join("")
          Place.new(net, id, name)
        end
      end

      def find_arcs(doc, net)
        REXML::XPath.match(doc, "/pnml/net/arc").map do |elt|
          id = elt.attribute("id").value
          source_id = elt.attribute("source").value # source place(data) id
          target_id = elt.attribute("target").value # target transition(rule) id
          Arc.new(net, id, source_id, target_id)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
pione-0.5.0 lib/pione/pnml/reader.rb
pione-0.5.0.alpha.2 lib/pione/pnml/reader.rb
pione-0.5.0.alpha.1 lib/pione/pnml/reader.rb
pione-0.4.2 lib/pione/pnml/reader.rb
pione-0.4.1 lib/pione/pnml/reader.rb
pione-0.4.0 lib/pione/pnml/reader.rb