Question

I wrote a Petri net domain model in pure Ruby and I plan to persist it with MongoDB. Petri net places and transitions have "arcs" (place - transition arrows) relations, and perhaps other relations, too. So far, my nets are small, so I can store them in eg. a single Ruby file, or a YAML file. But I want to already get a taste of Mongo, which, I guess, will pay off later, should the Petri nets become huge, as common in biology. To avoid the YPetri complexities, let me provide simplified example:

# Places are basically glorified variables, with current contents (marking), and
# default contents (default_marking).
#
class Place
  attr_reader :name, :default_marking
  attr_accessor :marking
  def initialize name, default_marking=0
    @name, @default_marking = name, default_marking
  end
  def reset!; @marking = default_marking end
end

# Transitions specify how marking changes when the transition "fires" (activates).
# "Arcs" attribute is a hash of { place => change } pairs.
#
class Transition
  attr_reader :name, :arcs
  def initialize( name, arcs: {} )
    @name, @arcs = name, arcs
  end
  def fire!
    arcs.each { |place, change| place.marking = place.marking + change }
  end
end

Now we can create a small Petri net:

A, B, C = Place.new( "A" ), Place.new( "B" ), Place.new( "C" )

# Marking of A is incremented by 1
AddA = Transition.new( "AddA", arcs: { A => 1 } )

# 1 piece of A dissociates into 1 piece of B and 1 piece of C
DissocA = Transition.new( "DissocA", arcs: { A => -1, B => +1, C => +1 } )

[ A, B, C ].each &:reset!
[ A, B, C ].map &:marking #=> [0, 0, 0]
AddA.fire!
[ A, B, C ].map &:marking #=> [1, 0, 0]
DissocA.fire!
[ A, B, C ].map &:marking #=> [0, 1, 1]

This should be enough to convey the general idea. I feel reluctant to rewrite my object model with the syntax of Mongo-related libraries. I just want to add "save" and "load" functionality to Place and Transition classes. I am new to MongoDB. MongoDB already works on my Debian. I just barely know how to connect to it from Ruby. I have hard times choosing the right tool. Bare mongo? Mongoid? Mongo_mapper? Anything else? Which one should I use, and how? With the code example that I provided, could you please give me a working code example of how to save and load a collection of places and transitions to / from MongoDB?

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top