Class: Pho::ResourceHash::SetAlgebra

Inherits:
Object
  • Object
show all
Defined in:
lib/pho/resource_hash.rb

Overview

Class providing set algebra methods over triple hashes

Class Method Summary collapse

Class Method Details

.minus(first, second) ⇒ Object

Accepts two triple hashes, expressed as RDF-in-JSON and returns a new Ruby data structure that constitutes the different between the two graphs

i.e. the return value will be a hash containing the triples that are in the first graph but which are not present in the second.

first

the first graph

second

the second graph.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/pho/resource_hash.rb', line 20

def SetAlgebra.minus(first, second)
  
  difference = Hash.new        
  first.each do |uri,properties|          
    if second.has_key?(uri)           
      properties.each do |predicate,value|              
        if second[uri].has_key?(predicate)                
          #second hash has same uri and predicate, so check value arrays                
          second_value = second[uri][predicate]
          value.each do |val|
            
            if !object_in_array?(second_value, val)

              difference[uri] ||= Hash.new
              difference[uri][predicate] ||= Array.new
              difference[uri][predicate] << val                      
            end
          end
           
        else                
          #uri is in second, but not this property and value
          difference[uri] ||= Hash.new
          difference[uri][predicate] = value                
        end              
      end
    else
      #uri not in second, so pass all straight-through
      difference[uri] = properties            
    end
    
  end
  
  return difference
  
end

.object_in_array?(array, val) ⇒ Boolean

Is there an object in the specified array, that matches the provided description

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pho/resource_hash.rb', line 57

def SetAlgebra.object_in_array?(array, val)
  array.each do |entry|

    if entry["type"] == val["type"] 
       if entry["value"] == val["value"]

         if entry["type"] == "literal"
           if entry["datatype"] == val["datatype"] && 
              entry["lang"] = val["lang"]
              return true
           end                     
         end
         return true   
       end
      
    end
  end
  return false
end