Class TaliaCore::SemanticRelation
In: lib/talia_core/semantic_relation.rb
Parent: ActiveRecord::Base

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)

Methods

Public Class methods

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

[Source]

    # File lib/talia_core/semantic_relation.rb, line 88
88:     def self.special_types
89:       @special_types ||= {
90:         N::RDF.type.to_s => N::SourceClass
91:       }
92:     end

Public Instance methods

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.

[Source]

    # File lib/talia_core/semantic_relation.rb, line 62
62:     def ==(compare)
63:       if(compare.is_a?(SemanticRelation))
64:         (self.predicate_uri == compare.predicate_uri) && (self.value == compare.value)
65:       else
66:         self.value == compare
67:       end
68:     end

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.

[Source]

    # File lib/talia_core/semantic_relation.rb, line 26
26:     def matches?(predicate, value = nil)
27:       if(value)
28:         if(value.is_a?(ActiveSource) || value.is_a?(SemanticProperty))
29:           (predicate_uri == predicate.to_s) && (value == object)
30:         else
31:           return false unless(object.is_a?(SemanticProperty))
32:           (predicate_uri == predicate.to_s) && (object.value == value)
33:         end
34:       else
35:         predicate_uri == predicate.to_s
36:       end
37:     end

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.

[Source]

    # File lib/talia_core/semantic_relation.rb, line 81
81:     def special_object_type
82:       self.class.special_types[predicate_uri]
83:     end

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).

[Source]

    # File lib/talia_core/semantic_relation.rb, line 43
43:     def value
44:       semprop = object.is_a?(SemanticProperty)
45:       if(special_object_type)
46:         assit(object, "Must have object for #{predicate_uri}")
47:         raise(ArgumentError, 'Must not have a property for a typed item') if(semprop)
48:         special_object_type.new(object.uri.to_s)
49:       elsif(semprop)
50:         # Plain, return the object or the value for SemanticProperties
51:         object.value ? PropertyString.parse(object.value) : object.value
52:       else
53:         object
54:       end
55:     end

[Validate]