class NTriple ; end class NTripleFile < NTriple attr_reader :line attr :litOffset def initialize super @litOffset=nil end def line=(_line) @line=_line @about_uri=@predicat_uri=@value_uri=@value_litteral=nil @split=@line.split(" ",3) end def about_uri @about_uri=urialias(@split[0]) if !@about_uri return @about_uri end def predicat_uri @predicat_uri=urialias(@split[1]) if !@predicat_uri return @predicat_uri end def value_uri @value_uri=urialias(@split[2]) if ! @value_uri return @value_uri end def value_litteral @value_litteral=@split[2][1,@split[2].length-lineOffset] if !@value_litteral return @value_litteral end #return number of chars to be deleted at then end of line. #number depend on file type CR/LF and on platform (ruby, jruby, ...) #This value should be computed once per file. #(A File should not mix CR/LF lines with CR lines) def lineOffset return @litOffset unless @litOffset.nil? i=@split[2].rindex(".") l=@split[2].length @litOffset=l-i+3 return @litOffset end def valueIsReference? case @split[2][0] when 60 then return true when 34 then return false else raise Warning.new,"Bad ntriple detected" end end COMMENT_SIGNATURE="#"[0] def isComment? return @line[0]==COMMENT_SIGNATURE end def isOntoMDXHeader? return @line.to_s=="#!ontomdx\n" end end module Mrdf_Model def loadMetaModelFromFile(fileName,ignoreHeader=false) @updateCount=@updateCount+1 raise Exception.new("nil fileName passed to loadMetaModelFromFile") if fileName.nil? m=fileName+".rb" #log.debug "m=#{m}" #TODO: cacheMetaModelFile forced to false # because range handling broke it if( context[:cacheMetaModelFile,false] && File.exists?(m) && File.mtime(m) > File.mtime(fileName) ) log.debug "Using cache in #{m}" require(m) loadModelFromFile(fileName,ignoreHeader) else log.debug "Generating cache in #{m}" loadModelFromFile(fileName,ignoreHeader) self.generateClassesFromMetamodel(m) end end def loadModelFromFile (srcFileName,ignoreHeader=false) fileName=translateToNt(srcFileName) @updateCount=@updateCount+1 #Crdf_Resource.new(self,"xx").initUpdate [false,true].each { |modeData| begin File.open(fileName,"r") { |aFile| parseModelStream(aFile,modeData,ignoreHeader) } rescue Exception => e raise Exception.new(e.message+"ERROR *** current file is: \"#{fileName}\"\nERROR ***\n") end } end def parseModelStream(stream,modeData,ignoreHeader=false) aFile=stream i=0 nt=NTripleFile.new() aFile.each_line { |nt.line| begin i=i+1 if(!ignoreHeader) ignoreHeader=true next if nt.isOntoMDXHeader? p="\nERROR *** " raise Exception.new("#{p}#{p}ontomdx was given a model file without#{p}a valid ontomdx plugin file header.#{p}If you have used a model file created by a modeler plugin,#{p}upgrade your plugin.#{p}Il you have used a plain RDF or RDFS file,#{p}specify ignoreHeader=true when loading model with loadModel.#{p}\n") end next if nt.isComment? parse(nt,modeData) rescue WarningRubyTypeUndefined => z log.error "WARNING ["+(modeData ? "data":"res")+" :nt.line "+i.to_s+", "+z.to_s+", Will create a default type. Possible discrepancies if defined later on. In case of problem, Consider bootstraping]:"+nt.line #log.debug "z.res=#{z.res}" z.res.createRubyClassMinimal(self) i=i-1 retry rescue WarningURIundefined => z log.error "WARNING ["+(modeData ? "data":"res")+" :line "+i.to_s+", "+z.to_s+",Will create a default entry]:"+nt.line #log.debug "z.uri=#{z.uri}" Crdf_Resource.new(self,z.uri) #parse(nt.line,modeData) i=i-1 retry rescue Warning => w log.error "WARNING ["+(modeData ? "data":"res")+" :line "+i.to_s+", "+w.to_s+"]:"+nt.line end } end PLATFORM_IS_JAVA=!(RUBY_PLATFORM.index("java").nil?) OS_IS_WINDOWS=!(ENV['OS'].index("Windows").nil?) puts(%{O/S detected: #{OS_IS_WINDOWS ? "WINDOWS" : "*nix"}}) puts(%{Ruby platform detected: #{PLATFORM_IS_JAVA ? "Java" : "C"}}) def rapperRun(param) if ! OS_IS_WINDOWS #we are not under windows #built in rapper cannot be used (it is for windows) #we use rapper available in path. cmd="rapper "+param elsif PLATFORM_IS_JAVA #We are under windows but under jruby cmd=WIN_PATH_RAPPER+" "+param else #We are under windows but under Matz Ruby Implementation (MRI) cmd='"'+WIN_PATH_RAPPER+'"'+param end return if system(cmd) raise Exception.new("\n***\n***Error running command\n***\n***#{cmd}\n***\n") end def translateToNt(srcFileName) return srcFileName if ! (srcFileName=~/.rdfs*$/ ) fileName="#{srcFileName}.cache.deleteMe.nt" if( context[:useTranslateToNtCache,false] && File.exists?(fileName) && File.mtime(fileName) > File.mtime(srcFileName) ) #log.debug "INFO:Using cache in #{fileName}" else raise "Blanc dans nom de fichier non supportes sous windows" if fileName.include?(" ") if !OS_IS_WINDOWS param=' -q -o ntriples -i rdfxml '+srcFileName+' > '+fileName else param=' -q -o ntriples -i rdfxml '+srcFileName.tr('/','\\\\')+' > '+fileName.tr('/','\\\\')+'' end rapperRun(param) end return fileName end def translateToXML(srcFileName,fileName) raise "Blanc dans nom de fichier non supportes sous windows" if fileName.include?(" ") || srcFileName.include?(" ") if !OS_IS_WINDOWS param=' -q -i ntriples -o rdfxml '+srcFileName+' > '+fileName+'' else param=' -q -i ntriples -o rdfxml '+srcFileName.tr('/','\\\\')+' > '+fileName.tr('/','\\\\')+'' end rapperRun(param) return fileName end end