lib/active_rdf/lib/active_rdf/objectmanager/resource.rb
def method_missing(method, *args)
      
      # possibilities:
      # 1. eyal.age is a property of eyal (triple exists <eyal> <age> "30")
      # evidence: eyal age ?a, ?a is not nil (only if value exists)
      # action: return ?a
      #
      # 2. eyal's class is in domain of age, but does not have value for eyal
      # explain: eyal is a person and some other person (not eyal) has an age
      # evidence: eyal type ?c, age domain ?c
      # action: return nil
      #
      # 3. eyal.age = 30 (setting a value for a property)
      # explain: eyal has (or could have) a value for age, and we update that value
      # complication: we need to find the full URI for age (by looking at
      # possible predicates to use
      # evidence: eyal age ?o  (eyal has a value for age now, we're updating it)
      # evidence: eyal type ?c, age domain ?c (eyal could have a value for age, we're setting it)
      # action: add triple (eyal, age, 30), return 30
      #
      # 4. eyal.age is a custom-written method in class Person
      # evidence: eyal type ?c, ?c.methods includes age
      # action: inject age into eyal and invoke
      #
      # 5. eyal.age is registered abbreviation 
      # evidence: age in @predicates
      # action: return object from triple (eyal, @predicates[age], ?o)
      #
      # 6. eyal.foaf::name, where foaf is a registered abbreviation
      # evidence: foaf in Namespace.
      # action: return namespace proxy that handles 'name' invocation, by 
      # rewriting into predicate lookup (similar to case (5)
      
      $activerdflog.debug "method_missing: #{method}"
   
      
      # are we doing an update or not? 
      # checking if method ends with '='
      
      update = method.to_s[-1..-1] == '='
      methodname = if update 
        method.to_s[0..-2]
      else
        method.to_s
      end
      
      # extract single values from array unless user asked for eyal.all_age
      flatten = true
      if method.to_s[0..3] == 'all_'
        flatten = false
        methodname = methodname[4..-1]
      end
      
      # check possibility (5)
      if @predicates.include?(methodname)
        if update
          return set_predicate(@predicates[methodname], args)
        else
          return get_predicate(@predicates[methodname])
        end
      end
      
      # check possibility (6)
      if Namespace.abbreviations.include?(methodname.to_sym)
        namespace = Object.new	
        @@uri = methodname
        @@subject = self
        @@flatten = flatten
        
        # catch the invocation on the namespace
        class <<namespace
          def method_missing(localname, *values)
            update = localname.to_s[-1..-1] == '='
            predicate = if update 
                          Namespace.lookup(@@uri, localname.to_s[0..-2])
                        else
                          Namespace.lookup(@@uri, localname)
                        end
            
            if update
              @@subject.set_predicate(predicate, values)
            else
              @@subject.get_predicate(predicate, @@flatten)
            end
          end
          private(:type)
        end
        return namespace
      end
      all_instance_predicates.each do |pred|    
        next if methodname == "to_i"  
        next if pred.instance_of? String #I added this because a problem with dbpedia
        if Namespace.localname(pred) == methodname
          if update
            return set_predicate(pred, args)
          else 
            value =  get_predicate(pred, flatten)                        
            return value if value != nil
          end
        end
    end
#      candidates = if update
#                      (class_level_predicates + direct_predicates).compact.uniq
#                   else
#                     direct_predicates
#                    end
#
# 
#			# checking possibility (1) and (3)
#			candidates.each do |pred|
#				if Namespace.localname(pred) == methodname
#          if update
#            return set_predicate(pred, args)
#          else
#  
#            return get_predicate(pred, flatten)
#          end
#				end
#			end
			
			raise ActiveRdfError, "could not set #{methodname} to #{args}: no suitable 
			predicate found. Maybe you are missing some schema information?" if update

			# get/set attribute value did not succeed, so checking option (2) and (4)
			
			# checking possibility (2), it is not handled correctly above since we use
			# direct_predicates instead of class_level_predicates. If we didn't find
			# anything with direct_predicates, we need to try the
			# class_level_predicates. Only if we don't find either, we
			# throw "method_missing"
#			candidates = class_level_predicates
#
#			# if any of the class_level candidates fits the sought method, then we
#			# found situation (2), so we return nil or [] depending on the {:array =>
#			# true} value
#			if candidates.any?{|c| Namespace.localname(c) == methodname}
#				return_ary = args[0][:array] if args[0].is_a? Hash
#				if return_ary
#					return []
#				else
#					return nil
#				end
#			end
 
			# checking possibility (4)
			# TODO: implement search strategy to select in which class to invoke
			# e.g. if to_s defined in Resource and in Person we should use Person
			$activerdflog.debug "RDFS::Resource: method_missing option 4: custom class method"
			self.type.each do |klass|
    
				if klass.instance_methods.include?(method.to_s)
		 
          _dup = klass.new(uri)
					return _dup.send(method,*args)
				end
			end

			# if none of the three possibilities work out, we don't know this method
			# invocation, but we don't want to throw NoMethodError, instead we return
			# nil, so that eyal.age does not raise error, but returns nil. (in RDFS,
			# we are never sure that eyal cannot have an age, we just dont know the
			# age right now)
   
			nil
		end
# manages invocations such as eyal.age
manages invocations such as eyal.age
manages invocations such as eyal . age
def method_missing(method, *args)
       2. eyal's class is in domain of age, but does not have value for eyal
       explain: eyal is a person and some other person (not eyal) has an age
       evidence: eyal type ?c, age domain ?c
       action: return nil
       3. eyal.age = 30 (setting a value for a property)
       explain: eyal has (or could have) a value for age, and we update that value
       complication: we need to find the full URI for age (by looking at
       possible predicates to use
       evidence: eyal age ?o  (eyal has a value for age now, we're updating it)
       evidence: eyal type ?c, age domain ?c (eyal could have a value for age, we're setting it)
       action: add triple (eyal, age, 30), return 30
       4. eyal.age is a custom-written method in class Person
       evidence: eyal type ?c, ?c.methods includes age
       action: inject age into eyal and invoke
       5. eyal.age is registered abbreviation 
       evidence: age in @predicates
       action: return object from triple (eyal, @predicates[age], ?o)
       6. eyal.foaf::name, where foaf is a registered abbreviation
       evidence: foaf in Namespace.
       action: return namespace proxy that handles 'name' invocation, by 
       rewriting into predicate lookup (similar to case (5)
      $activerdflog.debug "method_missing: {method}"
       are we doing an update or not? 
       checking if method ends with '='
      update = method.to_s[-1..-1] == '='
      methodname = if update 
        method.to_s[0..-2]
      else
        method.to_s
      end
       extract single values from array unless user asked for eyal.all_age
      flatten = true
      if method.to_s[0..3] == 'all_'
        flatten = false
        methodname = methodname[4..-1]
      end
       check possibility (5)
      if @predicates.include?(methodname)
        if update
          return set_predicate(@predicates[methodname], args)
        else
          return get_predicate(@predicates[methodname])
        end
      end
       check possibility (6)
      if Namespace.abbreviations.include?(methodname.to_sym)
        namespace = Object.new	
        @@uri = methodname
        @@subject = self
        @@flatten = flatten
         catch the invocation on the namespace
        class <<namespace
          def method_missing(localname, *values)
            update = localname.to_s[-1..-1] == '='
            predicate = if update 
                          Namespace.lookup(@@uri, localname.to_s[0..-2])
                        else
                          Namespace.lookup(@@uri, localname)
                        end
            if update
              @@subject.set_predicate(predicate, values)
            else
              @@subject.get_predicate(predicate, @@flatten)
            end
          end
          private(:type)
        end
        return namespace
      end
      all_instance_predicates.each do |pred|    
        next if methodname == "to_i"  
        next if pred.instance_of? String I added this because a problem with dbpedia
        if Namespace.localname(pred) == methodname
          if update
            return set_predicate(pred, args)
          else 
            value =  get_predicate(pred, flatten)                        
            return value if value != nil
          end
        end
    end
      candidates = if update
                      (class_level_predicates + direct_predicates).compact.uniq
                   else
                     direct_predicates
                    end
			 checking possibility (1) and (3)
			candidates.each do |pred|
				if Namespace.localname(pred) == methodname
          if update
            return set_predicate(pred, args)
          else
            return get_predicate(pred, flatten)
          end
				end
			end
			raise ActiveRdfError, "could not set {methodname} to {args}: no suitable 
			predicate found. Maybe you are missing some schema information?" if update
			 get/set attribute value did not succeed, so checking option (2) and (4)
			 checking possibility (2), it is not handled correctly above since we use
			 direct_predicates instead of class_level_predicates. If we didn't find
			 anything with direct_predicates, we need to try the
			 class_level_predicates. Only if we don't find either, we
			 throw "method_missing"
			candidates = class_level_predicates
			 if any of the class_level candidates fits the sought method, then we
			 found situation (2), so we return nil or [] depending on the {:array =>
			 true} value
			if candidates.any?{|c| Namespace.localname(c) == methodname}
				return_ary = args[0][:array] if args[0].is_a? Hash
				if return_ary
					return []
				else
					return nil
				end
			end
			 checking possibility (4)
			 TODO: implement search strategy to select in which class to invoke
			 e.g. if to_s defined in Resource and in Person we should use Person
			$activerdflog.debug "RDFS::Resource: method_missing option 4: custom class method"
			self.type.each do |klass|
				if klass.instance_methods.include?(method.to_s)
          _dup = klass.new(uri)
					return _dup.send(method,*args)
				end
			end
			 if none of the three possibilities work out, we don't know this method
			 invocation, but we don't want to throw NoMethodError, instead we return
			 nil, so that eyal.age does not raise error, but returns nil. (in RDFS,
			 we are never sure that eyal cannot have an age, we just dont know the
			 age right now)
			nil
		end
def method_missing ( method , * args ) $activerdflog . debug "method_missing: #{method}" update = method . to_s [ - 1 .. - 1 ] == '=' methodname = if update method . to_s [ 0 .. - 2 ] else method . to_s end flatten = true if method . to_s [ 0 .. 3 ] == 'all_' flatten = false methodname = methodname [ 4 .. - 1 ] end if @predicates . include? ( methodname ) if update return set_predicate ( @predicates [ methodname ] , args ) else return get_predicate ( @predicates [ methodname ] ) end end if Namespace . abbreviations . include? ( methodname . to_sym ) namespace = Object . new @@uri = methodname @@subject = self @@flatten = flatten class << namespace def method_missing ( localname , * values ) update = localname . to_s [ - 1 .. - 1 ] == '=' predicate = if update Namespace . lookup ( @@uri , localname . to_s [ 0 .. - 2 ] ) else Namespace . lookup ( @@uri , localname ) end if update @@subject . set_predicate ( predicate , values ) else @@subject . get_predicate ( predicate , @@flatten ) end end private ( :type ) end return namespace end all_instance_predicates . each do | pred | next if methodname == "to_i" next if pred . instance_of? String if Namespace . localname ( pred ) == methodname if update return set_predicate ( pred , args ) else value = get_predicate ( pred , flatten ) return value if value != nil end end end raise ActiveRdfError , "could not set #{methodname} to #{args}: no suitable 
			predicate found. Maybe you are missing some schema information?" if update $activerdflog . debug "RDFS::Resource: method_missing option 4: custom class method" self . type . each do | klass | if klass . instance_methods . include? ( method . to_s ) _dup = klass . new ( uri ) return _dup . send ( method , * args ) end end nil end
manages invocations such as eyal.age
manages invocations such as eyal . age
# possibilities: # 1. eyal.age is a property of eyal (triple exists <eyal> <age> "30") # evidence: eyal age ?a, ?a is not nil (only if value exists) # action: return ?a # # 2. eyal's class is in domain of age, but does not have value for eyal # explain: eyal is a person and some other person (not eyal) has an age # evidence: eyal type ?c, age domain ?c # action: return nil # # 3. eyal.age = 30 (setting a value for a property) # explain: eyal has (or could have) a value for age, and we update that value # complication: we need to find the full URI for age (by looking at # possible predicates to use # evidence: eyal age ?o  (eyal has a value for age now, we're updating it) # evidence: eyal type ?c, age domain ?c (eyal could have a value for age, we're setting it) # action: add triple (eyal, age, 30), return 30 # # 4. eyal.age is a custom-written method in class Person # evidence: eyal type ?c, ?c.methods includes age # action: inject age into eyal and invoke # # 5. eyal.age is registered abbreviation  # evidence: age in @predicates # action: return object from triple (eyal, @predicates[age], ?o) # # 6. eyal.foaf::name, where foaf is a registered abbreviation # evidence: foaf in Namespace. # action: return namespace proxy that handles 'name' invocation, by  # rewriting into predicate lookup (similar to case (5) # are we doing an update or not?  # checking if method ends with '=' # extract single values from array unless user asked for eyal.all_age # check possibility (5) # check possibility (6) # catch the invocation on the namespace #I added this because a problem with dbpedia #      candidates = if update #                      (class_level_predicates + direct_predicates).compact.uniq #                   else #                     direct_predicates #                    end # #  #			# checking possibility (1) and (3) #			candidates.each do |pred| #				if Namespace.localname(pred) == methodname #          if update #            return set_predicate(pred, args) #          else #   #            return get_predicate(pred, flatten) #          end #				end #			end # get/set attribute value did not succeed, so checking option (2) and (4) # checking possibility (2), it is not handled correctly above since we use # direct_predicates instead of class_level_predicates. If we didn't find # anything with direct_predicates, we need to try the # class_level_predicates. Only if we don't find either, we # throw "method_missing" #			candidates = class_level_predicates # #			# if any of the class_level candidates fits the sought method, then we #			# found situation (2), so we return nil or [] depending on the {:array => #			# true} value #			if candidates.any?{|c| Namespace.localname(c) == methodname} #				return_ary = args[0][:array] if args[0].is_a? Hash #				if return_ary #					return [] #				else #					return nil #				end #			end # checking possibility (4) # TODO: implement search strategy to select in which class to invoke # e.g. if to_s defined in Resource and in Person we should use Person # if none of the three possibilities work out, we don't know this method # invocation, but we don't want to throw NoMethodError, instead we return # nil, so that eyal.age does not raise error, but returns nil. (in RDFS, # we are never sure that eyal cannot have an age, we just dont know the # age right now)
method
args