Class: PublishMyData::DataCube::Cube

Inherits:
Object
  • Object
show all
Includes:
CubeResults
Defined in:
app/models/publish_my_data/data_cube/cube.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CubeResults

#loop_and_page_sparql_query, #uris_and_labels_only

Constructor Details

#initialize(dataset) ⇒ Cube

Returns a new instance of Cube.



10
11
12
# File 'app/models/publish_my_data/data_cube/cube.rb', line 10

def initialize(dataset)
  @dataset = dataset
end

Instance Attribute Details

#datasetObject (readonly)

Returns the value of attribute dataset.



8
9
10
# File 'app/models/publish_my_data/data_cube/cube.rb', line 8

def dataset
  @dataset
end

Instance Method Details

#area_dimensionObject

 the (one and only) area dimenson for this cube.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/publish_my_data/data_cube/cube.rb', line 60

def area_dimension

  # NOTE: finds any properties which are any level of descendant of sdmxDim:refArea

  query = "PREFIX qb: <http://purl.org/linked-data/cube#>
  PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
  PREFIX sdmxDim: <http://purl.org/linked-data/sdmx/2009/dimension#>

  SELECT DISTINCT ?uri ?label WHERE {
            
    ?uri a ?dimensionType .
    ?uri a qb:DimensionProperty .

    OPTIONAL {
      ?uri rdfs:label ?label .           
    }
    GRAPH <#{dataset.data_graph_uri}> {
      ?s ?uri ?o .
    }
  
    {
      { ?uri rdfs:subPropertyOf+ sdmxDim:refArea }
      UNION
      { ?uri a sdmxDim:refArea }              
    }
  }"

  uris_and_labels_only(Tripod::SparqlClient::Query.select(query)).first
end

#csv_observations(rows_dimension_uri, columns_dimension_uri, locked_dimensions = {}, order_desc = false, order_by_column_uri = nil) ⇒ Object

For a given row and column dimension, and a hash of locked dimensions uri => value



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/models/publish_my_data/data_cube/cube.rb', line 121

def csv_observations(rows_dimension_uri, columns_dimension_uri, locked_dimensions={}, order_desc=false, order_by_column_uri=nil)
  measure_property_uri = measure_property[:uri].to_s

  page = 1
  per_page = 5000
  page_of_results = nil
  results = []

  while page == 1 || page_of_results.size == per_page
    sparql = paged_observations_sparql(page, per_page, rows_dimension_uri, columns_dimension_uri, measure_property_uri, locked_dimensions, order_desc, order_by_column_uri)
    page_of_results = Tripod::SparqlClient::Query.select(sparql)
    results += page_of_results
    page += 1
  end

  csv_data(results, rows_dimension_uri, columns_dimension_uri, locked_dimensions)
end

#dimension_objectsObject



54
55
56
57
# File 'app/models/publish_my_data/data_cube/cube.rb', line 54

def dimension_objects
  dim_objs = dimensions.map { |d| PublishMyData::DataCube::Dimension.new(d[:uri], self, d[:label]) }
  dim_objs.sort{ |x,y| y.size <=> x.size } # ordered by size desc
end

#dimensionsObject

a collection of dimension properties for this cube



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/publish_my_data/data_cube/cube.rb', line 36

def dimensions

  query = "PREFIX qb: <http://purl.org/linked-data/cube#>
  PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

  SELECT DISTINCT ?uri ?label WHERE {
    ?uri a qb:DimensionProperty .
    OPTIONAL {
      ?uri rdfs:label ?label .
    }
    GRAPH <#{dataset.data_graph_uri}> {
      ?s ?uri ?o .
    }
  }"

  uris_and_labels_only(Tripod::SparqlClient::Query.select(query))
end

#grid_observations(page, per_page, rows_dimension_uri, columns_dimension_uri, locked_dimensions = {}, order_desc = false, order_by_column_uri = nil) ⇒ Object

For a given row and column dimension, and a hash of locked dimensions uri => value Returns an arry of hashes for the page, with one row per item in the array.



112
113
114
115
116
117
# File 'app/models/publish_my_data/data_cube/cube.rb', line 112

def grid_observations(page, per_page, rows_dimension_uri, columns_dimension_uri, locked_dimensions={}, order_desc=false, order_by_column_uri=nil)
  measure_property_uri = measure_property[:uri].to_s
  sparql = grid_rows_observations_sparql(page, per_page, rows_dimension_uri, columns_dimension_uri, measure_property_uri, locked_dimensions, order_desc, order_by_column_uri)
  results = Tripod::SparqlClient::Query.select(sparql)
  grid_data(results, rows_dimension_uri, columns_dimension_uri)
end

#measure_propertyObject

the (one and only) measure property for this cube.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/publish_my_data/data_cube/cube.rb', line 91

def measure_property

  query = "PREFIX qb: <http://purl.org/linked-data/cube#>
  PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

  SELECT DISTINCT ?uri ?label WHERE {
    ?uri a qb:MeasureProperty .
    OPTIONAL {
      ?uri rdfs:label ?label .
    }
    GRAPH <#{dataset.data_graph_uri}> {
      ?s ?uri ?o .
    }
  }"

  uris_and_labels_only(Tripod::SparqlClient::Query.select(query)).first
end


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/publish_my_data/data_cube/cube.rb', line 14

def recommended_dimensions
  sorted_dims = dimension_objects

  largest_dimension = sorted_dims.first
  second_largest_dimension = sorted_dims[1]

  locked_dims = {}

  # other dims
  (2...sorted_dims.length).each do |i|
    dim = sorted_dims[i]
    locked_dims[dim.uri] = dim.values.first[:uri]
  end

  {
    rows_dimension: largest_dimension.uri,
    columns_dimension: second_largest_dimension.uri,
    locked_dimensions: locked_dims
  }
end