#require "ontomde-core/exceptions.rb" require "singleton" # Helper methods to handle rdf ressources as object. module IteratorOrSingleElement def to_s r="" sep="" self.each{ |elt| r=r+sep+(elt.to_s) sep=" " } return r end def isFalse? return !isTrue? end def +(array) return self if array.kind_of?(ArrayOrSingleElementEmpty) return super(array) end STRING_TO_BOOLEAN_HASH={ "false" => false, "true" => true} def other_implem_isTrue? return false if length==0 raise Warning.new,"bad data for isTrue?(length=#{length}>1)" if length>1 ret=STRING_TO_BOOLEAN_HASH[self[0]] return ret unless ret.nil? raise Warning.new,"bad data for isTrue?(#{self[0]})" if length>1 end def isTrue? #return false if length==0 return false if empty? raise Warning.new,"bad data for isTrue?(length=#{length}>1)" if length>1 return false if self[0]=="false" return true if self[0]=="true" raise Warning.new,"bad data for isTrue?(#{self[0]})" if length>1 end end class ArrayOrSingleElement < Array include IteratorOrSingleElement end class SetOrSingleElement < Set include IteratorOrSingleElement end # Singleton of an empty ArrayOrSingleElement # Note: # This object is used for performance enhancement. class ArrayOrSingleElementEmpty #< ArrayOrSingleElement include Singleton # Size of an empty Array (always 0) def size 0 end # Size of an empty Array (always 0) def length 0 end # Iterator (does nothing) def each end # returns self as def select # quelque-soit le critere # de selection le retour est tout aussi vide return self; end # returns array def +(array) return array end # returns !isTrue? def isFalse? return !isTrue? end # returns false def isTrue? return false end # returns "" def to_s return "" end #return true def empty? return true end #returns false def include?(elt) return false end def delete(elt) return nil end def sort(&block) return self end end module Mrdfs_Literal end class Crdf_Resource # helper method used by rdf property getter # return first value contained in val ArrayOrSingleElement # # When val is empty: # log an error if warn==true # returns nil # When val contains more than one element: # log an error # returns first element found def rdf_getter_one(val,warn,propNameShort) if val.nil? || val.size==0 log_error_nil_one(propNameShort) if warn return nil end return val[0] if val.length==1 log.error "#{propNameShort}_one used with multi-value (class=#{self.class.name})(uri=#{rdf_uri})(length=#{val.length})(value=(#{val.to_s}) Returning first one found as workaround : (#{val[0]})" return val[0] end # raise an exception if value does not have a type in range array. # This method is used by rdf attributes operators. # # range: array of types # value: a value # property: name of the property, value comes from (used in error message) # src : owner of the property being processed def check_rdfType(range,value,property,src=nil) return if value.nil? return if range.empty? range.each { |r| return if value.kind_of?(r) return if r==::Mrdfs_Literal && (value.kind_of?(String) || value.kind_of?(Fixnum)) } raise Exception.new( <#{sym_array[0]}< >#{sym_array[1]}< >#{sym_array[2]}<" #log.debug sym_short ret << %{def #{sym_short} @#{sym}.nil? ? ::ArrayOrSingleElementEmpty.instance : @#{sym} end} (range.empty?&&true ? [Crdf_Resource] : range).each { |inv_res| next if inv_res==::Mrdfs_Literal inv_res.class_eval <