lib/rtm/javatmapi/core/topic.rb in rtm-javatmapi-0.2.0 vs lib/rtm/javatmapi/core/topic.rb in rtm-javatmapi-0.2.1

- old
+ new

@@ -1,13 +1,13 @@ # Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig. # License: Apache License, Version 2.0 module Java::OrgTmapiCore::Topic - - #include RTM::Topic + + include RTM::Topic extend Superiseable - + # Returns all Names and Occurrences parented by this Topic. # # :call-seq: # children -> Array of Names and/or Occurrences # @@ -26,11 +26,11 @@ # the identifiers as Name type. # # If the identifier equals :default, all Names # which have the default Name type are returned. # - # Identifier may be a Topic/Topic-Reference or Symbol; + # Identifier may be a topic reference or Symbol; # or an Array of these. # # :call-seq: # names -> Set of Names # names(identifier) -> Set of Names @@ -59,11 +59,11 @@ # of this Topic for which the Occurrence type equals identifier. # If several identifiers are given, returns the union # of all Occurrences of this Topic, which have one of # the identifiers as Occurrence type. # - # Identifier may be a Topic/Topic-Reference or an Array of these. + # Identifier may be a topic reference or an Array of these. # # :call-seq: # occurrences -> Set of Occurrences # occurrences(identifier) -> Set of Occurrences # @@ -85,11 +85,11 @@ superised # Creates a Name for this Topic with the specified value. # Value has to be a String. # - # Type may be a Topic/Topic-Reference. + # Type may be a topic reference. # If given, type specifies the Name Type; # else the created Name will have the default name type. # # Scope must be an Array of Topics/Topic-References. # If given, scope specifies the Name scope; else the Name is @@ -102,22 +102,22 @@ # create_name(type, value, scope) -> Name # def create_name(arg1,arg2 = :nothing, arg3 = :nothing) if (arg2 == :nothing) && (arg3 == :nothing) #arg1 = value raise "create_name(value): value must be a String" unless arg1.is_a?(String) - return createName(arg1,[].to_java(org.tmapi.core.Topic)) + return createName(arg1,new_empty_java_topic_array) end if (arg3 == :nothing) if arg2.is_a?(Array) #arg1 = value, arg2 = scope raise "create_name(value, scope): value must be a String" unless arg1.is_a?(String) arg2.each do |theme| raise "create_name(value, scope): scope must be an Array of Topics/Topic-References" unless (theme.is_a?(String) || theme.is_a?(Java::OrgTmapiCore::Topic) || theme.is_a?(Java::OrgTmapiCore::Locator)) end - return arg2.empty? ? createName(arg1,[].to_java(org.tmapi.core.Topic)) : createName(arg1,topic_map.get!(arg2)) + return arg2.empty? ? createName(arg1,new_empty_java_topic_array) : createName(arg1,topic_map.get!(arg2)) elsif arg2.is_a?(String) #arg1 = type, arg2 = value raise "create_name(type, value): type must be a Topic/Topic-Reference" unless (arg1.is_a?(String) || arg1.is_a?(Java::OrgTmapiCore::Topic) || arg1.is_a?(Java::OrgTmapiCore::Locator)) - return createName(topic_map.get!(arg1),arg2,[].to_java(org.tmapi.core.Topic)) + return createName(topic_map.get!(arg1),arg2,new_empty_java_topic_array) else raise "create_name(?,?): arguments don't match" end end #arg1 = type, arg2 = value, arg3 = scope @@ -132,16 +132,16 @@ superised # Creates an Occurrence for this Topic with the specified type and value and # optional scope and datatype. # - # Type specifies the Occurrence type and may be a Topic, String or Locator. + # Type specifies the Occurrence type and may be a topic reference. # # Value may be a Locator (then the datatype is set to xsd:AnyURI and # must not be given) or String. # - # Scope may be an Array of Topics/Topic-References. If no scope + # Scope may be an Array of topic reference. If no scope # is given, the Occurrence will be in the unconstrained scope. # # Datatype may be a String or Locator and indicates the datatype of the value. # # :call-seq: @@ -149,27 +149,59 @@ # create_occurrence(type, value, {:scope => scope}) -> Occurrence # create_occurrence(type, value, {:datatype => datatype, :scope => scope}) -> Occurrence # create_occurrence(type, value, {:datatype => datatype}) -> Occurrence # def create_occurrence(type,value,params={}) - if params[:scope] - raise("scope must be an Array") unless params[:scope].is_a?(Array) - if (value.is_a? Java::OrgTmapiCore::Locator) || !params[:datatype] - return createOccurrence(topic_map.get!(type),value,topic_map.get!(params[:scope])) - else # datatype given, no locator - return createOccurrence(topic_map.get!(type),value,topic_map.create_locator(params[:datatype]),topic_map.get!(params[:scope])) + # creating a topic for the type + type = topic_map.get!(type) + # reading the datatype if given. nil otherwise + datatype = params[:datatype] + # reading value and setting datatype if not done before + unless datatype + if value.is_a?(Java::OrgTmapiCore::Locator) + datatype = RTM::PSI[:IRI] + elsif value.is_a?(String) + datatype = RTM::PSI[:String] + elsif value.is_a?(Float) + datatype = RTM::PSI[:Float] + elsif value.is_a?(Fixnum) + datatype = RTM::PSI[:Long] + elsif value.is_a?(Bignum) + datatype = RTM::PSI[:Integer] + elsif value.is_a?(DateTime) #DateTime is a Time and a Date + datatype ||= RTM::PSI[:DateTime] + elsif value.is_a?(Time) + datatype ||= RTM::PSI[:Time] + elsif value.is_a?(Date) + datatype ||= RTM::PSI[:Date] + else + raise NoDatatypeHandlerAvailableException.new(value) end - else #no scope - if (value.is_a? Java::OrgTmapiCore::Locator) || !params[:datatype] - return createOccurrence(topic_map.get!(type),value,[].to_java(org.tmapi.core.Topic)) - else #datatype given, no locator - return createOccurrence(topic_map.get!(type),value,topic_map.create_locator(params[:datatype]),[].to_java(org.tmapi.core.Topic)) + end + # creating locator for datatype + datatype = topic_map.create_locator(datatype) + # value must be a String + value = value.to_s + # if datatype is xsd:anyURI -> resolve value against base_iri + if datatype.reference == RTM::PSI[:IRI] + value = topic_map.create_locator(value).reference + end + + begin + if params[:scope] + raise("scope must be an Array") unless params[:scope].is_a?(Array) + + return createOccurrence(type, value, datatype, topic_map.get!(params[:scope])) + + else #no scope + return createOccurrence(type, value, datatype, new_empty_java_topic_array) end + rescue java.lang.IllegalArgumentException => iae + raise(iae.message) end end - # Returns the TopicMap the Topic belongs to. # # :call-seq: # parent -> TopicMap # @@ -178,11 +210,11 @@ end alias :reverse_children :parent # Calls TMAPI TopicMap.getIndex which # returns the index for the TypeInstanceIndex. - # + # # :call-seq: # type_instance_index -> TypeInstanceIndex # def type_instance_index topic_map.type_instance_index @@ -192,13 +224,13 @@ # Returns the Roles played by this Topic. # # If given, rtype specifies the type of the Roles to be returned; # If given, atype specifies the type of the Association # from which the returned Roles must be part of. - # - # rtype and atype may be a Topic/Topic-Reference or Arrays of these types. # + # rtype and atype may be a topic reference or Arrays of these types. + # # The result may be empty. # # :call-seq: # roles -> Array of Roles # roles(rtype) -> Array of Roles @@ -261,61 +293,77 @@ # # :call-seq: # add_subject_identifier(identifier) # def add_subject_identifier(identifier) - if identifier.is_a? Java::OrgTmapiCore::Locator - addSubjectIdentifier(identifier) - else - addSubjectIdentifier(topic_map.create_locator(identifier)) + begin + if identifier.is_a? Java::OrgTmapiCore::Locator + addSubjectIdentifier(identifier) + else + addSubjectIdentifier(topic_map.create_locator(identifier)) + end + rescue org.tmapi.core.IdentityConstraintException => ice + # TODO add condition to switch off automerge + self.mergeIn(self.topic_map.get(identifier)) end end - alias :subject_identifiers= :add_subject_identifier superised - # Adds a subject locator to the current topic. - # Identifier may be a String or Locator. - # - # :call-seq: - # add_subject_locator(identifier) - # - def add_subject_locator(identifier) - # TODO Spec - if identifier.is_a? Java::OrgTmapiCore::Locator - addSubjectLocator(identifier) - else - addSubjectLocator(topic_map.create_locator(identifier)) - end - end - alias :subject_locators= :add_subject_locator - - superised # Removes a subject identifier from the this Topic, # if present. # # Identifier may be a String or Locator. # # :call-seq: # remove_subject_identifier(identifier) # def remove_subject_identifier(identifier) - identifier = topic_map.create_locator(identifier) if identifier.is_a? String + identifier = topic_map.create_locator(identifier) if identifier.is_a? String removeSubjectIdentifier(identifier) if identifier.is_a? Java::OrgTmapiCore::Locator end + # Returns the subject locators assigned to the current topic. + # + # :call-seq: + # subject_locators -> Set of Locators + # + def subject_locators + getSubjectLocators + end superised + # Adds a subject locator to the current topic. + # Identifier may be a String or Locator. + # + # :call-seq: + # add_subject_locator(identifier) + # + def add_subject_locator(identifier) + # TODO Spec + begin + if identifier.is_a? Java::OrgTmapiCore::Locator + addSubjectLocator(identifier) + else + addSubjectLocator(topic_map.create_locator(identifier)) + end + rescue org.tmapi.core.IdentityConstraintException => ice + # TODO add condition to switch off automerge + self.mergeIn(self.topic_map.topic_by_subject_locator(identifier)) + end + end + + superised # Removes a subject locator from the this Topic, # if present. # # Identifier may be a String or Locator. # # :call-seq: # remove_subject_locator(identifier) # def remove_subject_locator(identifier) - identifier = topic_map.create_locator(identifier) if identifier.is_a? String + identifier = topic_map.create_locator(identifier) if identifier.is_a? String removeSubjectLocator(identifier) if identifier.is_a? Java::OrgTmapiCore::Locator end # superised # # Removes an item identifer from the this Topic, @@ -330,16 +378,16 @@ # identifier = topic_map.create_locator(identifier) if identifier.is_a? String # removeItemIdentifier(identifier) if identifier.is_a? Java::OrgTmapiCore::Locator # end superised - # Adds a type given by the identifier to this Topic. + # Adds a type given by the identifier to this Topic. # - # Identifier may be a Topic or Topic-Reference. + # Identifier may be a topic reference. # # :call-seq: - # add_type(identifier) + # add_type(identifier) # def add_type(identifier) if identifier.is_a?(Java::OrgTmapiCore::Topic) addType(identifier) elsif identifier.is_a?(Java::OrgTmapiCore::Locator) || identifier.is_a?(String) @@ -349,11 +397,11 @@ end end # Adds one or several types given by the identifiers. # - # Identifier may be a Topic or Topic-Reference. + # Identifier may be a topic reference. # # :call-seq: # add_types(identifier1, identifier2, ...) # def add_types(*args) @@ -362,16 +410,16 @@ add_type(identifier) end end - # Adds an instance given by the identifier to this Topic. + # Adds an instance given by the identifier to this Topic. # - # Identifier may be a Topic or Topic-Reference. + # Identifier may be a topic reference. # # :call-seq: - # add_instance(identifier) + # add_instance(identifier) # def add_instance(identifier) if identifier.is_a?(Java::OrgTmapiCore::Topic) identifier.addType(self) elsif identifier.is_a?(Java::OrgTmapiCore::Locator) || identifier.is_a?(String) @@ -381,11 +429,11 @@ end end # Adds one or several instances given by the identifiers. # - # Identifier may be a Topic or Topic-Reference. + # Identifier may be a topic reference. # # :call-seq: # add_instances(identifier1, identifier2, ...) # def add_instances(*args) @@ -414,21 +462,21 @@ # :call-seq: # reified = identifier # def reified=(identifier) if identifier.is_a?(Java::OrgTmapiCore::Reifiable) - identifier.reifier = self + identifier.reifier = self return end raise("reified=: Identifier may be a Name, Variant, Occurrence, Association, Role or TopicMap") end superised - # Removes a type given by the identifier from this Topic, + # Removes a type given by the identifier of this topic, # if present. # - # Identifier may be a Topic or Topic-Reference. + # Identifier may be a topic reference. # # :call-seq: # remove_type(identifier) # def remove_type(identifier) @@ -438,17 +486,40 @@ identifier = topic_map.get(identifier) removeType(identifier) if identifier end end + # Removes an instance given by the identifier of this topic, + # if present. + # + # Identifier may be a topic reference. + # + # :call-seq: + # remove_instance(identifier) + # + def remove_instance(identifier) + identifier = topic_map.get(identifier) + return unless identifier + identifier.removeType(self) + end # TODO extend this, maybe allow for a removal of any topic no matter how important # it may be for the whole topic map def force_remove self.roles_played.to_a.clone.each{|p| p.parent.remove} self.remove end + private + def new_empty_java_topic_array + [].to_java(org.tmapi.core.Topic) + end + + class NoDatatypeHandlerAvailableException < Exception + def initialize(value) + super "Cannot map #{value} to an occurrence value+datatype. Maybe you need to add a handler for your Ruby object type." + end + end end