lib/openwfe/flowexpressionid.rb in ruote-0.9.19 vs lib/openwfe/flowexpressionid.rb in ruote-0.9.20
- old
+ new
@@ -1,46 +1,33 @@
-#
#--
-# Copyright (c) 2005-2008, John Mettraux, OpenWFE.org
-# All rights reserved.
+# Copyright (c) 2005-2009, John Mettraux, jmettraux@gmail.com
#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions are met:
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
#
-# . Redistributions of source code must retain the above copyright notice, this
-# list of conditions and the following disclaimer.
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
#
-# . Redistributions in binary form must reproduce the above copyright notice,
-# this list of conditions and the following disclaimer in the documentation
-# and/or other materials provided with the distribution.
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
#
-# . Neither the name of the "OpenWFE" nor the names of its contributors may be
-# used to endorse or promote products derived from this software without
-# specific prior written permission.
-#
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
-# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
-# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-# POSSIBILITY OF SUCH DAMAGE.
+# Made in Japan and Costa Rica.
#++
-#
-#
-# "hecho en Costa Rica"
-# enhanced in Japan
-#
-# john.mettraux@openwfe.org
-#
+require 'yaml'
+
module OpenWFE
#
# A FlowExpressionId is a unique identifier for a FlowExpression (an atomic
# piece of a process instance).
@@ -73,19 +60,26 @@
alias :expname :expression_name
alias :wfurl :workflow_definition_url
alias :wfname :workflow_definition_name
alias :wfrevision :workflow_definition_revision
- #--
+ #
# a trick : returns self...
#
- #def fei
- # self
- #end
- #++
+ def fei
+ self
+ end
#
+ # Returns a String that can be used to identify the FlowExpression[Id]
+ # in a storage.
+ #
+ def as_string_key
+ "#{@workflow_instance_id} #{@expression_name} #{@expression_id}"
+ end
+
+ #
# This method return @workflow_instance_id. If parent is set to
# true, if will return the same result as
# parent_workflow_instance_id().
#
def wfid (parent=false)
@@ -128,26 +122,53 @@
end
#
# Rebuilds a FlowExpressionId from its Hash representation.
#
- def FlowExpressionId.from_h (h)
+ def self.from_h (h)
- FIELDS.inject FlowExpressionId.new do |fei, f|
- fei.instance_variable_set("@#{f}", h[f] || h[f.to_s])
- fei
+ FIELDS.inject(FlowExpressionId.new) do |fei, f|
+ fei.instance_variable_set("@#{f}", h[f] || h[f.to_s]); fei
end
end
+ #
+ # Builds a new FlowExpressionId out of a hash (overriding some defaults)
+ #
+ def self.new_fei (h={})
+
+ from_h({
+ :owfe_version => OPENWFERU_VERSION,
+ :engine_id => 'engine',
+ :workflow_definition_url => 'no-url',
+ :workflow_definition_name => 'no-name',
+ :workflow_definition_revision => '0',
+ :workflow_instance_id => '-1',
+ :expression_name => 'no-exp',
+ :expression_id => '0'
+ }.merge(h))
+ end
+
+ #
+ # full blown hash
+ #
def hash
to_s.hash
end
+ #
+ # short hash (used by the cached expression storage among others)
+ #
+ def short_hash
+
+ "#{wfid} #{expid} #{expname}".hash
+ end
+
def == (other)
- return false if not other.kind_of?(FlowExpressionId)
+ return false unless other.is_a?(FlowExpressionId)
#return self.to_s == other.to_s
# no perf gain
@workflow_instance_id == other.workflow_instance_id and
@@ -177,14 +198,13 @@
def ancestor_of? (other_fei)
o = other_fei.dup
o.expression_name = @expression_name
o.expression_id = @expression_id
-
return false unless self == o
- OpenWFE::starts_with other_fei.expression_id, @expression_id
+ other_fei.expid[0, @expression_id.length] == @expression_id
end
#
# Returns a deep copy of this FlowExpressionId instance.
#
@@ -212,25 +232,25 @@
#
# Useful for unique identifier in URIs.
#
def to_web_s
- wid = wfid.gsub("\.", "_")
- eid = expid.gsub("\.", "_")
+ wid = wfid.gsub("\.", '_')
+ eid = expid.gsub("\.", '_')
URI.escape("#{wid}__#{eid}")
end
#
# Splits the web fei into the workflow instance id and the expression
# id.
#
def self.split_web_s (s)
- i = s.rindex("__")
+ i = s.rindex('__')
- [ s[0..i-1].gsub("\_", "."), s[i+2..-1].gsub("\_", ".") ]
+ [ s[0..i-1].gsub("\_", '.'), s[i+2..-1].gsub("\_", '.') ]
end
#
# Yet another debugging method. Just returns the sub_instance_id and
# the expression_id, in a string.
@@ -245,25 +265,25 @@
# For example, if the wfid is "1234.0.1", this method will
# return "1234".
#
def parent_workflow_instance_id
- FlowExpressionId.to_parent_wfid workflow_instance_id
+ FlowExpressionId.to_parent_wfid(workflow_instance_id)
end
alias :parent_wfid :parent_workflow_instance_id
#
- # Returns "" if this expression id belongs to a top process,
+ # Returns '' if this expression id belongs to a top process,
# returns something like ".0" or ".1.3" if this exp id belongs to
# an expression in a subprocess.
# (Only used in some unit tests for now)
#
def sub_instance_id
- i = workflow_instance_id.index(".")
- return "" unless i
+ i = workflow_instance_id.index('.')
+ return '' unless i
workflow_instance_id[i..-1]
end
#
# If this flow expression id belongs to a sub instance, a call to
@@ -275,22 +295,22 @@
# If the flow expression id doesn't belong to a sub instance, nil
# will be returned.
#
def last_sub_instance_id
- i = workflow_instance_id.rindex(".")
+ i = workflow_instance_id.rindex('.')
return nil unless i
workflow_instance_id[i+1..-1]
end
#
# Returns true if this flow expression id belongs to a process
# which is not a subprocess.
#
def is_in_parent_process?
- (sub_instance_id == "")
+ (sub_instance_id == '')
end
#
# Returns the last part of the expression_id. For example, if
# the expression_id is "0.1.0.4", "4" will be returned.
@@ -310,19 +330,17 @@
#
def self.to_fei (string)
fei = FlowExpressionId.new
- ss = string.split(" ")
+ ss = string.split
- #require 'pp'; puts; pp ss
+ ss = ss[1..-1] if ss[0] == '('
- ss = ss[1..-1] if ss[0] == "("
-
fei.owfe_version = ss[1]
- ssRawEngineId = ss[2].split("/")
+ ssRawEngineId = ss[2].split('/')
fei.engine_id = ssRawEngineId[0]
#fei.initial_engine_id = ssRawEngineId[1]
fei.workflow_definition_url = ss[3]
fei.workflow_definition_name = ss[4]
@@ -330,29 +348,48 @@
fei.workflow_instance_id = ss[6]
fei.expression_name = ss[7]
fei.expression_id = ss[8][0..-2]
fei.expression_id = fei.expression_id[0..-2] \
- if fei.expression_id[-1, 1] == ")"
+ if fei.expression_id[-1, 1] == ')'
fei
end
#
# An alias for to_fei(string)
#
def self.from_s (string)
- to_fei string
+ to_fei(string)
end
#
# If wfid is already a 'parent wfid' (no sub id), returns it. Else
# returns the parent wfid (whatever is before the first ".").
#
def self.to_parent_wfid (wfid)
- wfid.split(".").first
+ wfid.split('.').first
+ end
+
+ #
+ # custom yaml serialization
+
+ yaml_as("tag:ruby.yaml.org,2002:#{self}")
+
+ def to_yaml (opts={}) #:nodoc#
+ YAML::quick_emit(self.object_id, opts) do |out|
+ out.map(taguri) { |map| map.add('s', to_s) }
+ end
+ end
+
+ def self.yaml_new (klass, tag, val) #:nodoc#
+ begin
+ FlowExpressionId.to_fei(val['s'])
+ rescue Exception => e
+ raise "failed to decode FlowExpressionId out of '#{s}', #{e}"
+ end
end
end
end