Module: ActiveFedora::SemanticNode

Includes:
MediaShelfClassLevelInheritableAttributes
Included in:
Base, RelsExtDatastream
Defined in:
lib/active_fedora/semantic_node.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

PREDICATE_MAPPINGS =
Hash[:is_member_of => "isMemberOf",
:has_member => "hasMember",
:is_part_of => "isPartOf",
:has_part => "hasPart",
:is_member_of_collection => "isMemberOfCollection",
:has_collection_member => "hasCollectionMember",
:is_constituent_of => "isConstituentOf",
:has_constituent => "hasConstituent",
:is_subset_of => "isSubsetOf",
:has_subset => "hasSubset",
:is_derivation_of => "isDerivationOf",
:has_derivation => "hasDerivation",
:is_dependent_of => "isDependentOf",
:has_dependent => "hasDependent",
:is_description_of => "isDescriptionOf",
:has_description => "hasDescription",
:is_metadata_for => "isMetadataFor",
:has_metadata => "hasMetadata",
:is_annotation_of => "isAnnotationOf",
:has_annotation => "hasAnnotation",
:has_equivalent => "hasEquivalent",
:conforms_to => "conformsTo",
:has_model => "hasModel"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#internal_uriObject

Returns the value of attribute internal_uri.



6
7
8
# File 'lib/active_fedora/semantic_node.rb', line 6

def internal_uri
  @internal_uri
end

#relationshipsObject

Returns the value of attribute relationships.



6
7
8
# File 'lib/active_fedora/semantic_node.rb', line 6

def relationships
  @relationships
end

Class Method Details

.included(klass) ⇒ Object



33
34
35
# File 'lib/active_fedora/semantic_node.rb', line 33

def self.included(klass)
  klass.extend(ClassMethods)
end

Instance Method Details

#add_relationship(relationship) ⇒ Object



40
41
42
43
44
# File 'lib/active_fedora/semantic_node.rb', line 40

def add_relationship(relationship)
  # Only accept ActiveFedora::Relationships as input arguments
  assert_kind_of 'relationship',  relationship, ActiveFedora::Relationship
  register_triple(relationship.subject, relationship.predicate, relationship.object)
end

#assert_kind_of(n, o, t) ⇒ Object



36
37
38
# File 'lib/active_fedora/semantic_node.rb', line 36

def assert_kind_of(n, o,t)
  raise "Assertion failure: #{n}: #{o} is not of type #{t}" unless o.kind_of?(t)
end

#outbound_relationshipsObject



65
66
67
68
69
70
71
# File 'lib/active_fedora/semantic_node.rb', line 65

def outbound_relationships()
  if !internal_uri.nil? && !relationships[internal_uri].nil?
    return relationships[:self].merge(relationships[internal_uri]) 
  else
    return relationships[:self]
  end
end

#register_predicate(subject, predicate) ⇒ Object



58
59
60
61
62
63
# File 'lib/active_fedora/semantic_node.rb', line 58

def register_predicate(subject, predicate)
  register_subject(subject)
  if !relationships[subject].has_key?(predicate) 
    relationships[subject][predicate] = []
  end
end

#register_subject(subject) ⇒ Object



52
53
54
55
56
# File 'lib/active_fedora/semantic_node.rb', line 52

def register_subject(subject)
  if !relationships.has_key?(subject) 
      relationships[subject] = {} 
  end
end

#register_triple(subject, predicate, object) ⇒ Object



46
47
48
49
50
# File 'lib/active_fedora/semantic_node.rb', line 46

def register_triple(subject, predicate, object)
  register_subject(subject)
  register_predicate(subject, predicate)
  relationships[subject][predicate] << object
end

#relationships_from_classObject



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/active_fedora/semantic_node.rb', line 77

def relationships_from_class
  rels = {}
  self.class.relationships.each_pair do |subj, pred|
    rels[subj] = {}
    pred.each_key do |pred_key|
      rels[subj][pred_key] = []
    end
  end
  #puts "rels from class: #{rels.inspect}"
  return rels
end

#to_rels_ext(pid, relationships = self.relationships) ⇒ Object

Creates a RELS-EXT datastream for insertion into a Fedora Object Note: This method is implemented on SemanticNode instead of RelsExtDatastream because SemanticNode contains the relationships array



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/active_fedora/semantic_node.rb', line 92

def to_rels_ext(pid, relationships=self.relationships)
  starter_xml = <<-EOL
  <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
    <rdf:Description rdf:about="info:fedora/#{pid}">
    </rdf:Description>
  </rdf:RDF>
  EOL
  xml = REXML::Document.new(starter_xml)

  # Iterate through the hash of predicates, adding an element to the RELS-EXT for each "object" in the predicate's corresponding array.
  # puts ""
  # puts "Iterating through a(n) #{self.class}"
  # puts "=> whose relationships are #{self.relationships.inspect}"
  # puts "=> and whose outbound relationships are #{self.outbound_relationships.inspect}"
  self.outbound_relationships.each do |predicate, targets_array|
    targets_array.each do |target|
      # puts ". #{predicate} #{target}"
      xml.root.elements["rdf:Description"].add_element(self.class.predicate_lookup(predicate), {"xmlns" => "info:fedora/fedora-system:def/relations-external#", "rdf:resource"=>target})
    end
  end
  xml.to_s
end