# Core RDF resource management RDF_TRUE="true" RDF_FALSE="false" #Semicolumn (root) SC2="" #SC2="::" module Mrdf_Resource # Debug method # Dumps to screen the current object. def dump log.debug("\n#{self} dump #{self.class}>\n") instance_variables.each { |c| log.debug("-#{c}- #{instance_variable_get(c).to_s}\n" ) } log.debug('methods: ( ') #out.write('methods: ( ') public_methods.each { |m| log.debug( "#{m}, " ) } log.debug('<\n') end end class Crdf_Resource #< Crdfs_Resource include Mrdf_Resource attr_reader :uri attr_reader :nbrItem attr_reader :rdf_Repository @@nbrItem=0 # Utiliser pour un hack def force_rdf_Repository=(m) @rdf_Repository=m end # Returns a new empty attribute array def createEmptyAttributeValue return ArrayOrSingleElement.new() end VALID_RDF_URI_REGEXP=/^[-.a-zA-Z0-9_\/:}]*$/ #initialize a new resource and adds it to the current model. #if uri is nil, a temporary uri is automaticaly generated. #NOTE: # This uri is not suitable for use by mtk_protected. #TODO: ??? add a detection for prexisting uri ??? def initialize(model,uri) uri="_transient_#{object_id}" if uri.nil? if !uri.match(VALID_RDF_URI_REGEXP) puts "WARNING: automatic repair for uri: '#{uri}'" uri=uri.tr("<>",'_') #sanitize uri end model[uri]=self @rdf_Repository=model @uri=uri @@nbrItem=@@nbrItem+1 end #List of non resetable variables @@NonResetableVariables=['@rdf_Repository','@rdf_type','@uri',"@ext_isReferencedBy"] #Return true if v is a restable property. def isResetable?(v) return ! @@NonResetableVariables.include?(v) end # Force reset of this object properties # Every resetable property is set to nil. def forceReset! #log.debug "forceReset #{self}" instance_variables.each { |v| if(isResetable?(v)) #log.debug "reset #{v}" instance_variable_set(v,nil) end } end #suppress warning alias :nbrItem_org :nbrItem #Number of item loaded since the initilization of the program #Used for monitoring. def nbrItem @@nbrItem end #Internal use # def set(var,val) #TODO: cout: 65s -> 82 sur CRX #TODO: 21.8s -> 25,8s -> 19,5s (19/6) sur SID raise Warning.new,'null val in set' if val.nil? raise Warning.new,'null var in set' if var.nil? core_set(:"@#{var}",val) val.core_set(:@ext_isReferencedBy,self) if(val.kind_of?(Crdf_Resource)) end #Internal use # def core_set(ivar,val) old=instance_variable_defined?(ivar) ? instance_variable_get(ivar) : nil if old.nil? old=createEmptyAttributeValue instance_variable_set(ivar,old) end old.push(val) end end class Crdf_Resource attr_reader :rdf_type, :rdfs_label rdf_safe_attr_reader_many :rdfs_subClassOf attr_writer :rdf_type, :rdfs_label rdf_safe_attr_reader_many :ext_isReferencedBy #TODO: use ?? #@rubyName def to_yaml 'empty' end def set_rdfIsRelated(rel) end # Internal use # # sets resource uri def rdf_uri=(new_uri) @uri=new_uri end #returns resource uri def rdf_uri return @uri end #default string converter. Returns uri def to_s return @uri.to_s #"#{@uri}" end #Internal use # #Returns the ruby module name corresponding to this ressource def getRubyModuleName return "M#{getRubyName}" end #Internal use # #Returns the ruby class name corresponding to this ressource def getRubyClassName return "C#{getRubyName}" end #Retrieves a uri from a ruby identifier def getRubyNameInverse(identifier) found=nil rdf_Repository.each { |k,c| next if c.getRubyName!=identifier.to_s log.error("Ruby name clash for #{identifier} and #{k}") unless found.nil? found=k } log.error("No uri found for ruby identifier #{identifier} ") if found.nil? return found end #Transforms a uri to a valid ruby identifier. #cf. getRubyNameInverse def getRubyName return @uri.tr('^a-zA-Z0-9','_') #return @rdf_label if @rdf_label #return self.class.to_s # bootstrap end #Internal use def isBootstrap? return self.class.to_s==self.getRubyClassName end #Internal use def getRubyAttributeDefinition(model) #todo qqchos pour renvoyer valeur non nil cmd="" model.each { |tbl,prop| next unless prop.kind_of?(Crdf_Property) next if prop.rdfs_domain.nil? next unless prop.rdfs_domain.include?(self) cmd+=" rdf_attr :"+prop.getRubyName+",[" sep="" prop.rdfs_range.each { |x| #create module because we might be using it #before it has a chance to be created. eval %{module ::#{x.getRubyModuleName}\nend} cmd+=sep+"::"+x.getRubyModuleName sep="," } cmd+="]\n" #puts #puts rdf_attr_rubyCode(prop.getRubyName, []) } return cmd end # Internal use # # Creates a skeleton minimal ruby class for immediate use. def createRubyClassMinimal(model) return if @mtk_rubyClassGenerated return if isBootstrap? eval("class ::#{getRubyClassName} < ::Crdfs_Class\nend\n") eval("module ::#{getRubyModuleName} \nend\n") end # Liste des classes dont la classe courante d�rive (directe et indirecte) def rdfs_subClassOfRecursive(ret=Array.new) rdfs_subClassOf.each { |sub| next if ret.contains(sub) ret.push(sub) sub.rdfs_subClassOfRecursive(ret) } return ret end # Creates a ruby structure for this element. # NOTE: # generates ruby class named getRubyClassName # generates ruby modules named getRubyModuleName # multiple inheritance is handled trough multiple module inclusion. def createRubyClass(model) cmd='' return cmd if @mtk_rubyClassGenerated return cmd if isBootstrap? @mtk_rubyClassGenerated=true rdfs_subClassOf.each { |sub| cmd+=sub.createRubyClass(model) } cmd+="module ::#{self.getRubyModuleName}\n" cmd+=getRubyAttributeDefinition(model) rdfs_subClassOf.each { |sub| cmd+=' include ::' +(sub.getRubyModuleName)+"\n" } cmd+="end\n\n" cmd+="class ::#{getRubyClassName} < ::Crdfs_Class\n" cmd+=' include ' +self.getRubyModuleName+"\n" #rdfs_subClassOfRecursive.uniq.each { |c| # cmd+=' include ' +(c.getRubyModuleName)+'\n' #} cmd+="end\n\n" #puts "cmd -->#---------\n#{cmd}\n#---------------" eval(cmd) return cmd end end module Mrdf_Resource # returns this object uri converted to a dot/graphviz compatible identifier. def dotId return rdf_uri.tr('^a-zA-Z0-9','_') end end