Class: Crdf_Resource
- Includes:
- Mmtk_helper, Mrdf_Resource
- Defined in:
- lib/ontomde-core/resource.rb,
lib/ontomde-core/meta.rb,
lib/ontomde-core/helper.rb,
lib/ontomde-core/profil.rb,
lib/ontomde-core/resource.rb,
lib/ontomde-core/resourceSet.rb,
lib/ontomde-core/custom_method_missing.rb
Overview
< Crdfs_Resource
Direct Known Subclasses
Constant Summary collapse
- VALID_RDF_URI_REGEXP =
/^[-.a-zA-Z0-9_\/:}]*$/
- @@nbrItem =
0
- @@NonResetableVariables =
List of non resetable variables
['@rdf_Repository','@rdf_type','@uri','@ext_isReferencedBy']
- @@CacheGetRubyName =
Transforms a uri to a valid ruby identifier. cf. getRubyNameInverse
Hash.new
Constants included from Mmtk_helper
Mmtk_helper::MTK_WRITE_SESSION_FILE_NAMES, Mmtk_helper::NOREVERSE, Mmtk_helper::REVERSE, Mmtk_helper::TEMP_FILE_SUFFIX, Mmtk_helper::WITHOUT_MARKER
Instance Attribute Summary collapse
-
#nbrItem ⇒ Object
readonly
Number of item loaded since the initilization of the program Used for monitoring.
-
#rdf_Repository ⇒ Object
readonly
Returns the value of attribute rdf_Repository.
-
#rdf_type ⇒ Object
Returns the value of attribute rdf_type.
-
#rdfs_label ⇒ Object
Returns the value of attribute rdfs_label.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Attributes included from Mmtk_helper
Instance Method Summary collapse
-
#check_rdfType(range, value, property, src = nil) ⇒ Object
raise an exception if value does not have a type in range array.
-
#core_set(ivar, val) ⇒ Object
Internal use.
-
#createEmptyAttributeValue ⇒ Object
Returns a new empty attribute array.
-
#createRubyClass(model) ⇒ Object
Creates a ruby structure for this element.
-
#createRubyClassMinimal(model) ⇒ Object
Internal use.
-
#force_rdf_Repository=(m) ⇒ Object
Utiliser pour un hack.
-
#forceReset! ⇒ Object
Force reset of this object properties Every resetable property is set to nil.
- #freeCacheGetRubyName ⇒ Object
-
#getRubyAttributeDefinition(model) ⇒ Object
Internal use.
-
#getRubyClassName ⇒ Object
Internal use.
-
#getRubyModuleName ⇒ Object
Internal use.
- #getRubyName ⇒ Object
-
#getRubyNameInverse(identifier) ⇒ Object
Retrieves a uri from a ruby identifier.
-
#initialize(model, uri) ⇒ Crdf_Resource
constructor
initialize a new resource and adds it to the current model.
-
#isBootstrap? ⇒ Boolean
Internal use.
-
#isResetable?(v) ⇒ Boolean
Return true if v is a restable property.
- #method_missing(methId, args = nil) ⇒ Object
- #mtk_hello ⇒ Object
-
#nbrItem_org ⇒ Object
Returns the value of attribute nbrItem.
-
#rdf_getter_inv(sym_short, &filter) ⇒ Object
helper method used by rdf property to retrieve the inverse of a property.
-
#rdf_getter_one(val, warn, propNameShort) ⇒ Object
helper method used by rdf property getter return first value contained in val ArrayOrSingleElement.
-
#rdf_uri ⇒ Object
returns resource uri.
-
#rdf_uri=(new_uri) ⇒ Object
Internal use.
-
#rdfs_subClassOfRecursive(ret = Array.new) ⇒ Object
Liste des classes dont la classe courante d�rive (directe et indirecte).
-
#set(var, val) ⇒ Object
Internal use.
- #set_rdfIsRelated(rel) ⇒ Object
-
#to_s ⇒ Object
default string converter.
-
#to_yaml ⇒ Object
TODO: use ??.
Methods included from Mrdf_Resource
#context, #dotId, #dump, #log_error_nil_one, #mtk_context, #mtk_default_context, #mtk_object_message, #mtk_related_message, #rdfx_clone, #rdfx_copyOnClone?
Methods included from Mmtk_helper
#encloseWrite, #mtk_annotation, #mtk_autoNewFileCreationCheck, #mtk_out, #mtk_protected, #mtk_qualifyBlockURI, #mtk_retrieveProtected, #mtk_shouldAlreadyBeenReversed, #mtk_sprotected, #mtk_stringWriteSession, #mtk_stss, #mtk_wprotected, #mtk_writeSession, #mtk_writeSessionNameClashErrorDetection, #notifyFileGenerated, #write
Constructor Details
#initialize(model, uri) ⇒ Crdf_Resource
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 ???
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ontomde-core/resource.rb', line 51 def initialize(model,uri) uri="_transient_#{object_id}" if uri.nil? if !uri.match(VALID_RDF_URI_REGEXP) msg="WARNING: automatic repair for uri: '#{uri}'" puts msg uri=uri.tr('<>','_') #sanitize uri #raise Exception.new(msg) end model[uri]=self @rdf_Repository=model @uri=uri @@nbrItem=@@nbrItem+1 end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(methId, args = nil) ⇒ Object
71 72 73 74 |
# File 'lib/ontomde-core/custom_method_missing.rb', line 71 def method_missing(methId,args=nil) super end |
Instance Attribute Details
#nbrItem ⇒ Object (readonly)
Number of item loaded since the initilization of the program Used for monitoring.
91 92 93 |
# File 'lib/ontomde-core/resource.rb', line 91 def nbrItem @nbrItem end |
#rdf_Repository ⇒ Object (readonly)
Returns the value of attribute rdf_Repository.
31 32 33 |
# File 'lib/ontomde-core/resource.rb', line 31 def rdf_Repository @rdf_Repository end |
#rdf_type ⇒ Object
Returns the value of attribute rdf_type.
120 121 122 |
# File 'lib/ontomde-core/resource.rb', line 120 def rdf_type @rdf_type end |
#rdfs_label ⇒ Object
Returns the value of attribute rdfs_label.
120 121 122 |
# File 'lib/ontomde-core/resource.rb', line 120 def rdfs_label @rdfs_label end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
29 30 31 |
# File 'lib/ontomde-core/resource.rb', line 29 def uri @uri end |
Instance Method Details
#check_rdfType(range, value, property, src = nil) ⇒ Object
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
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/ontomde-core/meta.rb', line 143 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( <<END ********* ILLEGAL ASSIGNEMENT TYPE ********** ** property #{property} for uri=#{src.nil? ? "" : src.rdf_uri} ** has a range of (valid value types) ** [ #{range} ] ** which is not compatible with type #{value.class.name} ** Please check your code for incorrect assignement. ** ** This model element is related to these other model elements: #{} ********* ILLEGAL ASSIGNEMENT TYPE ********** END ) end |
#core_set(ivar, val) ⇒ Object
Internal use
109 110 111 112 113 114 115 116 |
# File 'lib/ontomde-core/resource.rb', line 109 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 |
#createEmptyAttributeValue ⇒ Object
Returns a new empty attribute array
40 41 42 |
# File 'lib/ontomde-core/resource.rb', line 40 def createEmptyAttributeValue return ArrayOrSingleElement.new() end |
#createRubyClass(model) ⇒ Object
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.
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/ontomde-core/resource.rb', line 252 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 |
#createRubyClassMinimal(model) ⇒ Object
Internal use
Creates a skeleton minimal ruby class for immediate use.
230 231 232 233 234 235 |
# File 'lib/ontomde-core/resource.rb', line 230 def createRubyClassMinimal(model) return if @mtk_rubyClassGenerated return if isBootstrap? eval("class ::#{getRubyClassName} < ::Crdfs_Class\nend\n") eval("module ::#{getRubyModuleName} \nend\n") end |
#force_rdf_Repository=(m) ⇒ Object
Utiliser pour un hack
35 36 37 |
# File 'lib/ontomde-core/resource.rb', line 35 def force_rdf_Repository=(m) @rdf_Repository=m end |
#forceReset! ⇒ Object
Force reset of this object properties Every resetable property is set to nil.
77 78 79 80 81 82 83 84 85 |
# File 'lib/ontomde-core/resource.rb', line 77 def forceReset! #log.debug "forceReset #{self}" instance_variables.each { |v| if(isResetable?(v)) #log.debug "reset #{v}" instance_variable_set(v,nil) end } end |
#freeCacheGetRubyName ⇒ Object
183 184 185 |
# File 'lib/ontomde-core/resource.rb', line 183 def freeCacheGetRubyName @@CacheGetRubyName=Hash.new end |
#getRubyAttributeDefinition(model) ⇒ Object
Internal use
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/ontomde-core/resource.rb', line 203 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 |
#getRubyClassName ⇒ Object
Internal use
Returns the ruby class name corresponding to this ressource
163 164 165 |
# File 'lib/ontomde-core/resource.rb', line 163 def getRubyClassName return "C#{getRubyName}" end |
#getRubyModuleName ⇒ Object
Internal use
Returns the ruby module name corresponding to this ressource
156 157 158 |
# File 'lib/ontomde-core/resource.rb', line 156 def getRubyModuleName return "M#{getRubyName}" end |
#getRubyName ⇒ Object
186 187 188 189 190 191 192 193 194 195 |
# File 'lib/ontomde-core/resource.rb', line 186 def getRubyName x=@@CacheGetRubyName[@uri] if x.nil? x=@uri.tr('^a-zA-Z0-9','_') @@CacheGetRubyName[@uri]=x end return x #return @rdf_label if @rdf_label #return self.class.to_s # bootstrap end |
#getRubyNameInverse(identifier) ⇒ Object
Retrieves a uri from a ruby identifier
168 169 170 171 172 173 174 175 176 177 |
# File 'lib/ontomde-core/resource.rb', line 168 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 |
#isBootstrap? ⇒ Boolean
Internal use
198 199 200 |
# File 'lib/ontomde-core/resource.rb', line 198 def isBootstrap? return self.class.to_s==self.getRubyClassName end |
#isResetable?(v) ⇒ Boolean
Return true if v is a restable property.
71 72 73 |
# File 'lib/ontomde-core/resource.rb', line 71 def isResetable?(v) return ! @@NonResetableVariables.include?(v) end |
#mtk_hello ⇒ Object
56 57 58 |
# File 'lib/ontomde-core/profil.rb', line 56 def mtk_hello log.debug "\n>#{self.class.name}->Crdf_Resource::mtk_hello" end |
#nbrItem_org ⇒ Object
Returns the value of attribute nbrItem. suppress warning
88 89 90 |
# File 'lib/ontomde-core/resource.rb', line 88 def nbrItem @nbrItem end |
#rdf_getter_inv(sym_short, &filter) ⇒ Object
helper method used by rdf property to retrieve the inverse of a property.
This method return is randomly ordered
sym_short: name of the inverse property filter: method calling the direct property
(used for performance reason to avoid eval)
175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/ontomde-core/meta.rb', line 175 def rdf_getter_inv(sym_short,&filter) ret=nil ext_isReferencedBy.each { |g| next unless g.respond_to?(sym_short) next unless yield(g).include?(self) ret=::ArrayOrSingleElement.new if ret.nil? next if ret.include?(g) ret << g } return ret.nil? ? ::ArrayOrSingleElementEmpty.instance : ret end |
#rdf_getter_one(val, warn, propNameShort) ⇒ Object
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
127 128 129 130 131 132 133 134 135 |
# File 'lib/ontomde-core/meta.rb', line 127 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 |
#rdf_uri ⇒ Object
returns resource uri
144 145 146 |
# File 'lib/ontomde-core/resource.rb', line 144 def rdf_uri return @uri end |
#rdf_uri=(new_uri) ⇒ Object
Internal use
sets resource uri
139 140 141 |
# File 'lib/ontomde-core/resource.rb', line 139 def rdf_uri=(new_uri) @uri=new_uri end |
#rdfs_subClassOfRecursive(ret = Array.new) ⇒ Object
Liste des classes dont la classe courante d�rive (directe et indirecte)
238 239 240 241 242 243 244 245 |
# File 'lib/ontomde-core/resource.rb', line 238 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 |
#set(var, val) ⇒ Object
Internal use
97 98 99 100 101 102 103 104 |
# File 'lib/ontomde-core/resource.rb', line 97 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 |
#set_rdfIsRelated(rel) ⇒ Object
133 134 |
# File 'lib/ontomde-core/resource.rb', line 133 def (rel) end |
#to_s ⇒ Object
default string converter. Returns uri
149 150 151 |
# File 'lib/ontomde-core/resource.rb', line 149 def to_s return @uri.to_s #"#{@uri}" end |
#to_yaml ⇒ Object
TODO: use ??
129 130 131 |
# File 'lib/ontomde-core/resource.rb', line 129 def to_yaml 'empty' end |