#:include: ../shared/license.rdoc module Mrdf_Model end class Crdf_Resource #True if resource is a bootstrap resource. #A bootstrap resource is a resource existing prior to model creation (not loaded from external configuration file). #Loaded resources are built using bootstrap resouces as references. rdf_safe_attr_reader_many :ext_isBootstrap end #Class meant to store every element of a model. class Crdf_Model #< Hash include Mrdf_Model attr_reader :updateCount def length @resourceSet.length end def empty? return @resourceSet.empty? end def to_s log.debug 'Model' end # Returns the ressource associated to the uri key. def [](key) return @resourceSet[key] end # Internal use def []=(key,res) @resourceSet[key]=res end # Standard each iterator def each(&block_expr) @resourceSet.each &block_expr end # Standard find_all iterator def find_all(&block_expr) @resourceSet.find_all &block_expr end # Standard each_value iterator def each_value(&block_expr) @resourceSet.each_value &block_expr end # # Standard reject iterator def reject(&block_expr) @resourceSet.reject &block_expr end # Initialize model internal structures. def initialize super @resourceSet=Hash.new #@fileGenerated=Array.new @updateCount=0 #@@mtk_out=nil #bootstrap Crdf_Resource.new(self,'rdf_Resource'); Crdf_Resource.new(self,'rdfs_Resource'); # le père des héritages Crdf_Resource.new(self,'rdfs_Class'); Crdf_Resource.new(self,'rdf_Property'); Crdf_Resource.new(self,'rdfs_Literal'); #bootstrap pour module kb Crdf_Resource.new(self,'kb_UML_METACLASS'); Crdf_Resource.new(self,'kb_UML_SLOT'); self.each {|k,c| c.ext_isBootstrap='true'} end #returns ressource associated to uri stored in model def getRes(uri) raise Warning.new,'nil uri' if uri.nil? obj=self[uri] raise WarningURIundefined.new(uri),"--uri undefined \"#{uri}\" " if obj.nil? return obj end # internal use def parse(nt,modeData) if modeData parseModeData(nt) else parseModeResource(nt) end end # internal use @@RDF_type=['rdf_type'] # internal use def predicat_isRDFTYPE(predicat) return @@RDF_type.include?(predicat) end # internal use def dynNewInstance(value_res,about_uri) cmd=value_res.getRubyClassName+'.new(self,about_uri)' return eval(cmd) end # internal use def parseModeResource(nt) return if !predicat_isRDFTYPE(nt.predicat_uri) about_res=self[nt.about_uri] if about_res about_res.forceReset!() else value_res=self.getRes(nt.value_uri) begin self[nt.about_uri]=about_res=dynNewInstance(value_res,nt.about_uri) about_res.rdf_type=value_res #about_res.set(rubyIdentifier(predicat_uri),value_res) # ref rescue => e log.debug "e-->#{e}" #TODO: cause probable raise WarningRubyTypeUndefined.new(nt.about_uri,value_res),"Ruby Type #{value_res.getRubyClassName} Undefined (cause probable!)" end return end end # internal use def parseModeData(nt) return if predicat_isRDFTYPE(nt.predicat_uri) subject=self.getRes(nt.about_uri) if nt.valueIsReference? subject.set(rubyIdentifier(nt.predicat_uri),self.getRes(nt.value_uri)) else subject.set(rubyIdentifier(nt.predicat_uri),nt.value_litteral) end end # internal use def rubyIdentifier(str) return str.tr('^a-zA-Z0-9','_') end # internal use def generateClassesFromMetamodel(outRubyModelFileName) log.info("ecriture du metamodele dans: #{outRubyModelFileName}") #f=File.open(outRubyModelFileName,File::CREAT|File::TRUNC|File::RDWR, 0644) mtk_default_context(:logFileWrite => false) { mtk_writeSession(outRubyModelFileName) { f=self f.write("# Debut du fichier #{outRubyModelFileName}\n") f.write("# Ce fichier a ete genere automatiquement\n") f.write("# Genere le:#{Time.now}\n") self. find_all { |a,x| x.kind_of?(Crdfs_Class) }. each { |a,x| #log.debug "gen -> #{x} #{x.class}" cmd=x.createRubyClass(self) f.write(cmd) } f.write("# Fin du fichier #{outRubyModelFileName}\n") }} #f.close end end