require "singleton" class UriNamespace include Singleton def initialize # Les 1er sont prioritaire pour unalias ?? @urialiases=Hash.new(nil) @urialiases["http:\/\/uml\/2"]="uml" #@urialiases["http://schema.omg.org/spec/UML/2.1"]="uml" #@urialiases["http:\/\/uml\/1.4"]="uml" @urialiases["http:\/\/www.w3.org\/2000\/01\/rdf-schema"]="rdfs" @urialiases["http:\/\/www.w3.org\/1999\/02\/22-rdf-syntax-ns"]="rdf" @urialiases["http:\/\/protege.stanford.edu\/system"]="sys" @urialiases["http:\/\/protege.stanford.edu\/kb"]="kb" @urialiases["http:\/\/ft\/uml\/2"]="umlx" @urialiases["http:\/\/kb"]="ukb" @urialiases["http:\/\/orange-ftgroup.com\/2007\/rd\/xmda\/bpm"]="bpm" @urialiases["http:\/\/protege.stanford.edu\/soa"]="soa" #enterprise architect @urialiases["http:\/\/EA"]="ea" end # Returns unaliases uri for uri # Used by magic draw export plugin # NOTE: # raises an error if alias is unknown. # Example: # unalias("rdf_XYZ") return @urialises("rdf")+"#"+"XYZ" def unalias(uri) n=uri.split('_',2) @urialiases.each {|k,v| next if v!=n[0] return k+"#"+n[1] } # no alias is ok return uri #raise "unalias failed for #{uri}" end # Returns the alias associated to ns. # NOTE: # uri of the form http://orange-ft.com/[0-9]+/XyZ are aliases xyz def getNamespaceAlias(ns) _alias=@urialiases[ns] return _alias if _alias md=/^http:\/\/orange-ft.com\/[0-9]+\/([a-z0-9A-Z]+)$/.match(ns) if md.nil? _alias="ns_#{@urialiases.length.to_s}" else # La première lettre doit être minuscule _alias=md[1].downcase end addAliasHelp(_alias,ns) addAlias(_alias,ns) return _alias end def addAliasHelp(_alias,ns) #return if context[:displayAddAliasHelp,true] log.debug < 'ns1:bar' #Note: # method added for uml2_kb which stores unaliased uri in kb text field for back reference to uml model. def urialias(uri) a=uri.split("#",2) return uri if a[1].nil? ns=getNamespaceAlias(a[0]) #puts "uri=#{ns}_#{a[1]}" return "#{ns}_#{a[1]}" end end #RDF triplet handling. class NTriple attr_reader :about_uri, :predicat_uri, :value_uri, :value_litteral def valueIsReference? end # Initialization of internal cache (performance enchancement) def initialize # Le cache est remis à zéro à chaque fichier. # Les essais avec SID.nt montrent un gain de performance # de 10% sur la méthode urialias, les fichiers étant largement disjoints. #@@cacheUse=1 #@@cacheMiss=100 #log.debug "initialize triplet" @uriMappingCache=Hash.new(nil) end # Returns an aliases uri # uri is in ntriple format: '' # Optimize for ntriple file handling def urialias(uri) #log.debug "#{uri} ??" #return nil if uri == nil ret=@uriMappingCache[uri] #log.debug "cache miss %=#{1.0*@@cacheMiss/@@cacheUse*100} cacheUse=#{@@cacheUse}" #@@cacheUse=@@cacheUse+1 return ret unless ret.nil? #log.debug "cache miss" #@@cacheMiss=@@cacheMiss+1 @uriMappingCache[uri]=ret=compute_urialias(uri) #log.debug "#{uri} -> #{ret}" return ret end # Internal use def compute_urialias(uri) #renvoi une uri apres avoir remplacer le chemin par son alias raise Warning.new,"Bad NT URI (#{uri})" if uri.nil? || (uri[0] != 60) #uri=uri[1,uri.length-2] #log.debug "uri[0,1] -> '#{uri[0,1]}'" uri=uri[1,uri.length].split(">",2)[0] #log.debug "uri-->#{uri}" return ::UriNamespace.instance.urialias(uri) #a=uri.split("#",2) #return uri if a[1].nil? #r=::UriNamespace.instance.getNamespaceAlias(a[0]) #log.debug "urialias="+r+"_"+a[1] #return r+"_"+a[1] #return "#{r}_#{a[1]}" end end