Class: TaliaCore::SemanticRelation

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/talia_core/semantic_relation.rb

Overview

The basic class to represent a semantic relation, which is equivalent to an RDF triple.

The predicate is directly contained in the record is a string, while the subject refers to respective ActiveSource object.

The object of the relation refers either to another ActiveSource or to a SemanticProperty, depending on the object_type field. This is a normal polymorphic relation to the active_sources/semantic_properties table(s)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.special_typesObject

Simple hash that checks if a type if property requires “special” handling This will cause the wrapper to accept ActiveSource relations and all sources will be casted to the given type



88
89
90
91
92
# File 'lib/talia_core/semantic_relation.rb', line 88

def self.special_types
  @special_types ||= {
    N::RDF.type.to_s => N::SourceClass
  }
end

Instance Method Details

#==(compare) ⇒ Object

An item will be equal to its #value - this is a little hack that lets Enumerable#find and such methods work easily on collections of SemanticRelation.

If compare is a SemanticRelation, this will be true if both relations have the same predicate value.



62
63
64
65
66
67
68
# File 'lib/talia_core/semantic_relation.rb', line 62

def ==(compare)
  if(compare.is_a?(SemanticRelation))
    (self.predicate_uri == compare.predicate_uri) && (self.value == compare.value)
  else
    self.value == compare
  end
end

#matches?(predicate, value = nil) ⇒ Boolean

Returns true if the Relation matches the given predicate URI (and value, if given). A relation matches if the predicate of this relation is them same as the predicate given (which can be a String or a Source or a N::URI) and if the object’s value (or uri, if the object is a ActiveSource) is the same as the value given.

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/talia_core/semantic_relation.rb', line 26

def matches?(predicate, value = nil)
  if(value)
    if(value.is_a?(ActiveSource) || value.is_a?(SemanticProperty))
      (predicate_uri == predicate.to_s) && (value == object)
    else
      return false unless(object.is_a?(SemanticProperty))
      (predicate_uri == predicate.to_s) && (object.value == value)
    end
  else
    predicate_uri == predicate.to_s
  end
end

#special_object_typeObject

This will return the “object type” for the current relation. This can be used to “force” a relation for some predicates.

This will check if an entry exists for the current predicate has an entry in #special_types. If yes, the class will be returned.

If object_type returns a class, the #value method will return objects of that class for all resources.

The default case is that this returns nil, which will cause #value to return the actual “object” value for relations to resources.



81
82
83
# File 'lib/talia_core/semantic_relation.rb', line 81

def special_object_type
  self.class.special_types[predicate_uri]
end

#valueObject

Return the “value” of the relation. This is usually the same as #object, except that string values are parsed as PropertyString objects and that in case the “special type” is set the related resources are made to be objects of that type (see above).



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/talia_core/semantic_relation.rb', line 43

def value
  semprop = object.is_a?(SemanticProperty)
  if(special_object_type)
    assit(object, "Must have object for #{predicate_uri}")
    raise(ArgumentError, 'Must not have a property for a typed item') if(semprop)
    special_object_type.new(object.uri.to_s)
  elsif(semprop)
    # Plain, return the object or the value for SemanticProperties
    object.value ? PropertyString.parse(object.value) : object.value
  else
    object
  end
end