Class: TaliaUtil::Util
- Inherits:
-
Object
- Object
- TaliaUtil::Util
- Defined in:
- lib/talia_util/util.rb
Overview
Main utility functions for Talia
Class Method Summary collapse
-
.clear_data ⇒ Object
Remove the data directories.
-
.do_migrations ⇒ Object
Do migrations.
-
.fat_record_joins ⇒ Object
Select semantic properties joined with all connected tables.
-
.fat_record_select ⇒ Object
For selecting “fat” records on the semantic properties, including the objects.
-
.flag?(the_flag) ⇒ Boolean
Check if the given flag is set on the command line.
-
.flush_db ⇒ Object
Flush the database.
-
.flush_rdf ⇒ Object
Flush the RDF store.
-
.full_reset ⇒ Object
Do a full reset of the data store.
-
.get_files ⇒ Object
Get the list of files from the “files” option.
-
.init_talia ⇒ Object
Init the talia core system.
-
.load_fixtures ⇒ Object
Load the fixtures.
-
.ontology_folder ⇒ Object
Get the configured folder for the ontologies.
-
.print_options ⇒ Object
print the common options for the tasks.
-
.rewrite_count ⇒ Object
This gives the number of triples that would be rewritten on #rewrite_rdf.
-
.rewrite_rdf ⇒ Object
Rewrite the RDF for the whole database.
-
.setup_ontologies ⇒ Object
Set up the ontologies from the given folder.
-
.talia_config ⇒ Object
Get info from the Talia configuraion.
-
.title ⇒ Object
Put the title for Talia.
Class Method Details
.clear_data ⇒ Object
Remove the data directories
115 116 117 118 119 120 |
# File 'lib/talia_util/util.rb', line 115 def clear_data data_dir = TaliaCore::CONFIG['data_directory_location'] iip_dir = TaliaCore::CONFIG['iip_root_directory_location'] FileUtils.rm_rf(data_dir) if(File.exist?(data_dir)) FileUtils.rm_rf(iip_dir) if(File.exist?(iip_dir)) end |
.do_migrations ⇒ Object
Do migrations
179 180 181 182 |
# File 'lib/talia_util/util.rb', line 179 def do_migrations migration_path = File.join("generators", "talia", "templates", "migrations") ActiveRecord::Migrator.migrate(migration_path, ENV["VERSION"] ? ENV["VERSION"].to_i : nil ) end |
.fat_record_joins ⇒ Object
Select semantic properties joined with all connected tables. TODO: Review after merging with optimized branch
209 210 211 212 213 214 |
# File 'lib/talia_util/util.rb', line 209 def fat_record_joins joins = " LEFT JOIN active_sources AS obj_sources ON semantic_relations.object_id = obj_sources.id AND semantic_relations.object_type = 'TaliaCore::ActiveSource'" joins << " LEFT JOIN semantic_properties AS obj_props ON semantic_relations.object_id = obj_props.id AND semantic_relations.object_type = 'TaliaCore::SemanticProperty'" joins << " LEFT JOIN active_sources AS subject_sources ON semantic_relations.subject_id = subject_sources.id" joins end |
.fat_record_select ⇒ Object
For selecting “fat” records on the semantic properties, including the objects. Used for rewriting the rdf. TODO: Review after merging with optimized branch
193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/talia_util/util.rb', line 193 def fat_record_select select = 'semantic_relations.id AS id, semantic_relations.created_at AS created_at, ' select << 'semantic_relations.updated_at AS updated_at, ' select << 'object_id, object_type, subject_id, predicate_uri, ' select << 'obj_props.created_at AS property_created_at, ' select << 'obj_props.updated_at AS property_updated_at, ' select << 'obj_props.value AS property_value, ' select << 'obj_sources.created_at AS object_created_at, ' select << 'obj_sources.updated_at AS object_updated_at, obj_sources.type AS object_realtype, ' select << 'obj_sources.uri AS object_uri, ' select << 'subject_sources.uri AS subject_uri' select end |
.flag?(the_flag) ⇒ Boolean
Check if the given flag is set on the command line
185 186 187 188 |
# File 'lib/talia_util/util.rb', line 185 def flag?(the_flag) assit_not_nil(the_flag) ENV[the_flag] && (ENV[the_flag] == "yes" || ENV[the_flag] == "true") end |
.flush_db ⇒ Object
Flush the database. This only flushes the main data tables!
103 104 105 106 107 |
# File 'lib/talia_util/util.rb', line 103 def flush_db [ 'active_sources', 'data_records', 'semantic_properties', 'semantic_relations', 'workflows'].reverse.each { |f| ActiveRecord::Base.connection.execute "DELETE FROM #{f}" } # Also remove the "unsaved cache" for the wrappers (may be important during testing) TaliaCore::SemanticCollectionWrapper.instance_variable_set(:'@unsaved_source_cache', {}) end |
.flush_rdf ⇒ Object
Flush the RDF store
110 111 112 |
# File 'lib/talia_util/util.rb', line 110 def flush_rdf ActiveRDF::ConnectionPool.write_adapter.clear end |
.full_reset ⇒ Object
Do a full reset of the data store
124 125 126 127 128 129 |
# File 'lib/talia_util/util.rb', line 124 def full_reset flush_db flush_rdf clear_data setup_ontologies end |
.get_files ⇒ Object
Get the list of files from the “files” option
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/talia_util/util.rb', line 9 def get_files puts "Files given: #{ENV['files']}" unless(ENV['files']) puts("This task needs files to work. Pass them like this files='something/*.x'") exit(1) end FileList.new(ENV['files']) end |
.init_talia ⇒ Object
Init the talia core system
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/talia_util/util.rb', line 45 def init_talia return if(TaliaCore::Initializer.initialized) # If we have Rails installed, just call the Rails config # instead of running the manual init if(defined?(RAILS_ROOT) && File.exist?(File.join(RAILS_ROOT, 'config', 'environment.rb'))) puts "\nInitializing Talia through Rails" load(File.join(RAILS_ROOT, 'config', 'environment.rb')) else # If options are not set, the initializer will fall back to the internal default TaliaCore::Initializer.talia_root = ENV['talia_root'] TaliaCore::Initializer.environment = ENV['environment'] config_file = ENV['config'] ? ENV['config'] : 'talia_core' # run the initializer TaliaCore::Initializer.run(config_file) do |config| unless(flag?('no_standalone')) puts "Always using standalone db from utilities." puts "Give the no_standalone option to override it." config['standalone_db'] = "true" end end end puts("\nTaliaCore initialized") # # Flush the database if requested if(flag?('reset_db')) flush_db puts "DB flushed" end # Flus the rdf if requested if(flag?('reset_rdf')) flush_rdf puts "RDF flushed" end end |
.load_fixtures ⇒ Object
Load the fixtures
169 170 171 172 173 174 175 176 |
# File 'lib/talia_util/util.rb', line 169 def load_fixtures # fixtures = ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/) : Dir.glob(File.join(File.dirname(__FILE__), 'test', 'fixtures', '*.{yml,csv}')) fixtures = [ 'active_sources', 'semantic_relations', 'semantic_properties' 'data_records'] fixtures.reverse.each { |f| ActiveRecord::Base.connection.execute "DELETE FROM #{f}" } fixtures.each do |fixture_file| Fixtures.create_fixtures(File.join('test', 'fixtures'), File.basename(fixture_file, '.*')) end end |
.ontology_folder ⇒ Object
Get the configured folder for the ontologies
21 22 23 |
# File 'lib/talia_util/util.rb', line 21 def ontology_folder ENV['ontology_folder'] || File.join(RAILS_ROOT, 'ontologies') end |
.print_options ⇒ Object
print the common options for the tasks
217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/talia_util/util.rb', line 217 def puts "\nGeneral options (not all options are valid for all tasks):" puts "files=<pattern> - Files for the task (a pattern to match the files)" puts "talia_root=<path> - Manually configure the TALIA_ROOT path" puts " (default:autodetect)" puts "reset_rdf={yes|no} - Flush the RDF store (default:no)" puts "reset_db={yes|no} - Flush the database (default:no)" puts "config=<filename> - Talia configuration file (default: talia_core)" puts "environment=<env> - Environment for configuration (default: development)" puts "data_dir=<dir> - Directory for the data files" puts "verbose={yes|no} - Show some additional info" puts "" end |
.rewrite_count ⇒ Object
This gives the number of triples that would be rewritten on #rewrite_rdf
164 165 166 |
# File 'lib/talia_util/util.rb', line 164 def rewrite_count TaliaCore::SemanticRelation.count + TaliaCore::ActiveSource.count end |
.rewrite_rdf ⇒ Object
Rewrite the RDF for the whole database. Will yield without parameters for progress-reporting blocks. FIXME: At the moment, this looses all RDF data that is not in the database.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/talia_util/util.rb', line 135 def rewrite_rdf flush_rdf # We'll get all data from single query. fat_rels = TaliaCore::SemanticRelation.find(:all, :joins => fat_record_joins, :select => fat_record_select) fat_rels.each do |rec| subject = N::URI.new(rec.subject_uri) predicate = N::URI.new(rec.predicate_uri) object = if(rec.object_uri) N::URI.new(rec.object_uri) else rec.property_value end ActiveRDF::FederationManager.add(subject, predicate, object) yield if(block_given?) end # Rewriting all the "runtime type" rdf triples # We'll select the type as something else, so that it doesn't try to do # STI instantiation (which would cause this to blow for classes that # are defined outside the core. TaliaCore::ActiveSource.find(:all, :select => 'uri, type AS runtime_type').each do |src| type = (src.runtime_type || 'ActiveSource') ActiveRDF::FederationManager.add(src, N::RDF.type, N::TALIA + type) yield if(block_given?) end end |
.setup_ontologies ⇒ Object
Set up the ontologies from the given folder
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/talia_util/util.rb', line 26 def setup_ontologies # Clear the ontologies from RDF, if possible adapter = ActiveRDF::ConnectionPool.write_adapter if(adapter.supports_context?) TaliaCore::RdfImport.clear_file_contexts else puts "WARNING: Cannot remove old ontologies, adapter doesn't support contexts." end puts "Ontologies loaded from: #{ontology_folder}" files = Dir[File.join(ontology_folder, '*.{rdf*,owl}')] ENV['rdf_syntax'] ||= 'rdfxml' params = [ENV['rdf_syntax'], files] params << :auto if(adapter.supports_context?) TaliaCore::RdfImport::import(*params) RdfUpdate::owl_to_rdfs end |
.talia_config ⇒ Object
Get info from the Talia configuraion
86 87 88 89 90 91 92 93 94 |
# File 'lib/talia_util/util.rb', line 86 def talia_config puts "Talia configuration" puts "" puts "TALIA_ROOT: #{TALIA_ROOT}" puts "Environment: #{TaliaCore::CONFIG['environment']}" puts "Standalone DB: #{TaliaCore::CONFIG['standalone_db']}" puts "Data Directory: #{TaliaCore::CONFIG['data_directory_location']}" puts "Local Domain: #{N::LOCAL}" end |