Class: YOWL::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/yowl/schema.rb

Overview

Utility class providing access to information about the schema, e.g. its description, lists of classes, etc

Constant Summary collapse

@@PredefinedNamespaces =
Hash.new
@@LastUsedOntologyNumber =
0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



18
19
20
# File 'lib/yowl/schema.rb', line 18

def base
  @base
end

#classesObject (readonly)

Returns the value of attribute classes.



25
26
27
# File 'lib/yowl/schema.rb', line 25

def classes
  @classes
end

#datatype_propertiesObject (readonly)

Returns the value of attribute datatype_properties.



23
24
25
# File 'lib/yowl/schema.rb', line 23

def datatype_properties
  @datatype_properties
end

#dirObject (readonly)

Returns the value of attribute dir.



26
27
28
# File 'lib/yowl/schema.rb', line 26

def dir
  @dir
end

#fileNameObject (readonly)

Returns the value of attribute fileName.



19
20
21
# File 'lib/yowl/schema.rb', line 19

def fileName
  @fileName
end

#introductionObject (readonly)

Returns the value of attribute introduction.



22
23
24
# File 'lib/yowl/schema.rb', line 22

def introduction
  @introduction
end

#modelObject (readonly)

Returns the value of attribute model.



17
18
19
# File 'lib/yowl/schema.rb', line 17

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



21
22
23
# File 'lib/yowl/schema.rb', line 21

def name
  @name
end

#object_propertiesObject (readonly)

Returns the value of attribute object_properties.



24
25
26
# File 'lib/yowl/schema.rb', line 24

def object_properties
  @object_properties
end

#ontologyObject (readonly)

Returns the value of attribute ontology.



27
28
29
# File 'lib/yowl/schema.rb', line 27

def ontology
  @ontology
end

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/yowl/schema.rb', line 15

def options
  @options
end

#prefixesObject (readonly)

Returns the value of attribute prefixes.



20
21
22
# File 'lib/yowl/schema.rb', line 20

def prefixes
  @prefixes
end

#repositoryObject (readonly)

Returns the value of attribute repository.



16
17
18
# File 'lib/yowl/schema.rb', line 16

def repository
  @repository
end

Class Method Details

.fromFile(ontology_file_name_, repository_) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/yowl/schema.rb', line 68

def Schema.fromFile(ontology_file_name_, repository_)
  
  if repository_.options.verbose
    puts "Read Schema #{ontology_file_name_}"
  end
  prefixes, base = Schema::read_prefixes(ontology_file_name_, repository_.options)
  
  format = RDF::Format.for(ontology_file_name_)
  if format.nil?()
    format = RDF::Format.for(:file_extension => "rdf")
  end
  begin
    model = RDF::Graph.load(ontology_file_name_, { :format => format.to_sym, :prefixes => prefixes })
  rescue Addressable::URI::InvalidURIError => e
    warn "ERROR: Invalid URI Error while parsing #{ontology_file_name_}: #{e.to_s}"
    return nil
  rescue Exception => e
    warn "ERROR: #{e.class.to_s} Error while parsing #{ontology_file_name_}: #{e}"
    return nil
  end
  
  return Schema.new(repository_, model, prefixes, base, ontology_file_name_)
end

Instance Method Details

#classDiagramAsSvgObject

Generate the main Class Diagram



389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/yowl/schema.rb', line 389

def classDiagramAsSvg
  if @options.verbose
    puts "Generating SVG Class Diagram for #{name}"
  end

  g = GraphvizUtility.setDefaults(GraphViz.new(:G, :type => :digraph))
  g[:rankdir] = "LR"
  sg = g.subgraph() { |sg|
    sg[:rank => "same"]
  }
  
  nodes = Hash.new
  edges = Hash.new
  allClasses = classes.values.to_set
  nonRootClasses = allClasses
  rootClasses = root_classes.to_set
  
  #
  # Add the GraphViz nodes for each class, but do the 
  # root classes in a subgraph to keep them at top level
  #
  rootClasses.each() do |klass|
    nonRootClasses.delete(klass)
    nodes, edges = klass.addAsGraphvizNode(sg, nodes, edges)
  end
  nonRootClasses.each() do |klass|
    nodes, edges = klass.addAsGraphvizNode(g, nodes, edges)
  end
  #
  # Process edges to super classes, we can ignore the root classes here
  #
  nonRootClasses.each() do |klass|
    if @options.verbose
      puts "- Processing class #{klass.short_name}"
    end
    superClasses = klass.super_classes() 
    superClasses.each() do |superClass|
      superClassNode = nodes[superClass.uri]
      if superClassNode
        if @options.verbose
          puts "  - Processing super class #{superClass.short_name}"
        end
        Class.newGraphVizEdge(g, nodes[klass.uri], superClassNode)
      else
        if @options.verbose
          puts "  - Processing super class #{superClass.short_name}, not found"
        end
      end
    end
  end
  #
  # Process the other associations here
  #
  allClasses.each() do |domainClass|
    domainClassNode = nodes[domainClass.uri]
    if @options.verbose
      puts "  - Processing associations of class #{domainClass.short_name}:"
    end
    domainClass.associations().each() do |association|
      if @options.verbose
        puts "    - Adding edge #{association.rangeClass.short_name}, #{association.label}"
      end
      nodes, edges = association.addAsGraphVizEdge(g, nodes, edges)
    end
  end
  
  return GraphvizUtility.embeddableSvg(g)
end

#classInSchemaWithURI(uri_) ⇒ Object

Return the class with the given URI as it is defined in this schema. Do not check the imported ontologies.



226
227
228
# File 'lib/yowl/schema.rb', line 226

def classInSchemaWithURI(uri_)
  return classes[uri_.to_s]
end

#classWithURI(uri) ⇒ Object

See YOWL::Individual::classWithURI(uri)



234
235
236
237
238
239
240
241
242
243
# File 'lib/yowl/schema.rb', line 234

def classWithURI(uri)
  if @ontology
    return @ontology.classWithURI(uri)
  end
  klass = classInSchemaWithURI(uri)
  if klass
    return klass
  end
  return nil
end

#hasClasses?Boolean

Returns:

  • (Boolean)


205
206
207
# File 'lib/yowl/schema.rb', line 205

def hasClasses?
  return ! @classes.empty?
end

#individualsObject



480
481
482
483
484
485
# File 'lib/yowl/schema.rb', line 480

def individuals
  if not defined?(@individuals)
    init_individuals
  end
  return @individuals
end

#individualsNotSkosConceptsObject



473
474
475
476
477
# File 'lib/yowl/schema.rb', line 473

def individualsNotSkosConcepts
  return individuals.values.find_all do |individual|
    not individual.isSkosConceptScheme?
  end
end

#list_classesObject



216
217
218
219
# File 'lib/yowl/schema.rb', line 216

def list_classes()
  sorted = classes().sort { |x,y| x[1] <=> y[1] }
  return sorted      
end

#list_datatype_propertiesObject



200
201
202
# File 'lib/yowl/schema.rb', line 200

def list_datatype_properties()
  return datatype_properties().sort { |x,y| x[1] <=> y[1] }
end

#list_object_propertiesObject



210
211
212
# File 'lib/yowl/schema.rb', line 210

def list_object_properties()
  return object_properties().sort { |x,y| x[1] <=> y[1] }
end

#list_propertiesObject



195
196
197
# File 'lib/yowl/schema.rb', line 195

def list_properties()
  return properties().sort { |x,y| x[1] <=> y[1] }          
end

#prefixedUri(uri) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/yowl/schema.rb', line 315

def prefixedUri(uri)
  if uri.nil?
    raise "ERROR: Passed nil to Schema:prefixedUri()"
  end
  uri = uri.to_s()
  if uri.empty?
    raise "ERROR: Passed empty string to Schema:prefixedUri()"
  end
  uri = uri.gsub(",", "_")
  @prefixes.each() do |prefix, namespace|
    if uri == namespace
      return prefix.to_s
    end
    if "#{uri}/" == namespace
      return prefix.to_s
    end
    if "#{uri}#" == namespace
      return prefix.to_s
    end
    if namespace[-1..-1] != '#' and namespace[-1..-1] != '/'
      @prefixes.each_pair do |prefix, namespace|
        puts "#{@fileName}: PREFIX #{prefix}: #{namespace}"
      end
      raise "ERROR: Namespace in @prefixes of #{@fileName} without trailing hash or slash: #{namespace}"
    end
    if uri.include?(namespace)
      if @ontology and namespace == @ontology.ns
        return uri.gsub(namespace, '')
      end
      return uri.gsub(namespace, "#{prefix.to_s}:")
    end
  end
=begin
  #
  # First look up in the import hierarchy to see if we can find a prefix definition
  # there that we can use...
  #
  if @ontology
    @ontology.imports.each do |import|
      if import.importedSchema and import.importedSchema != self
        uri = import.importedSchema.prefixedUri(uri)
        if uri
          return uri
        end
      end
    end
  end
  #
  # If all else failed, lets scan all schema's for a usable prefix
  #
  @repository.schemas.values.each do |otherSchema|
    if otherSchema != self
      uri = otherSchema.prefixedUri(uri)
      if uri
        return uri
      end
    end
  end
=end
  if @ontology
    ontology_uri = @ontology.uri
    if ontology_uri == uri
      return uri
    end
    uri = uri.gsub(ontology_uri + '#', '')
    uri = uri.gsub(ontology_uri + '/', '')
  end
  return uri
end

#prefixForNamespace(namespace_) ⇒ Object



163
164
165
166
167
168
169
170
171
# File 'lib/yowl/schema.rb', line 163

def prefixForNamespace(namespace_)
  _ns = @@PredefinedNamespaces.include?(namespace_) ? @@PredefinedNamespaces[namespace_] : namespace_
  @prefixes.each() do |prefix, namespace|
    if testIsNamespace?(namespace, _ns)
      return prefix, namespace
    end
  end
  return nil, nil
end

#propertiesObject



190
191
192
# File 'lib/yowl/schema.rb', line 190

def properties()
  return @datatype_properties.merge( @object_properties )     
end

#root_classesObject



246
247
248
249
250
251
252
253
# File 'lib/yowl/schema.rb', line 246

def root_classes()
  allClasses = classes().collect() do |uri,klass|
    klass
  end
  return allClasses.to_set.delete_if() do |klass|
    klass.hasSuperClassesInSchema?
  end
end

#skosConceptSchemesObject



459
460
461
462
463
# File 'lib/yowl/schema.rb', line 459

def skosConceptSchemes
  return individuals.values.find_all do |individual|
    individual.isSkosConceptScheme?
  end
end

#skosConceptsInScheme(conceptScheme) ⇒ Object



466
467
468
469
470
# File 'lib/yowl/schema.rb', line 466

def skosConceptsInScheme(conceptScheme)
  return individuals.values.find_all do |individual|
    individual.isSkosConceptInScheme?(conceptScheme)
  end
end

#uriObject



145
146
147
148
# File 'lib/yowl/schema.rb', line 145

def uri
  return @ontology ? @ontology.uri : @base
  #return @ontology.nil? ? @base : @ontology.uri
end