orientdb-time-graph 0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,63 @@
1
+ #ActiveOrient::Model.orientdb_class name: 'time_base', superclass: 'V'
2
+ class TG::Tag # < TG::TimeBase
3
+ def _monat
4
+ query.nodes :in, via: TG::DAY_OF
5
+ end
6
+
7
+ def die_stunde h
8
+ h.to_i >0 && h.to_i<31 ? out_tg_time_of[h].in : nil
9
+ end
10
+
11
+ def stunde *key
12
+ if key.empty?
13
+ out_tg_time_of.in
14
+ else
15
+ key= key.first if key.is_a?(Array) && key.size == 1
16
+ nodes( :out, via: TG::TIME_OF, where: { value: key } ).execute
17
+ end
18
+ end
19
+
20
+
21
+ def monat
22
+ _monat.execute.first
23
+ # in_tg_day_of.out.first
24
+ end
25
+
26
+ def datum
27
+ # three queries are fired
28
+ # todo
29
+ # formulate a match query providing anythin in just one query
30
+ m = monat
31
+ Date.new m.jahr.value, m.value, value # "#{ value}.#{m.value}.#{m.jahr.value}"
32
+ end
33
+
34
+ =begin
35
+ Fetches the vertex corresponding to the given date
36
+
37
+ (optional: executes the provided block on the vertex )
38
+
39
+ Example:
40
+
41
+ start_date= Date.new(2018,4,25)
42
+ TG::Tag.fetch( start_date){ |x| x.datum }
43
+ => ["25.4.2018"]
44
+ TG::Tag.fetch( start_date){ |x| x.data_nodes( ML::L_OHLC).map &:to_human }
45
+ INFO->select expand ( outE('tg_day_of').in[ value = 25 ] ) from
46
+ ( select expand ( outE('tg_month_of').in[ value = 4 ] ) from
47
+ ( select from tg_jahr where value = 2018 ) )
48
+ INFO->select expand ( outE('ml_l_ohlc').in ) from #82:8927
49
+ => ["<Ohlc[161:22]: in: {ML::L_OHLC=>1},
50
+ close : 3485.83, high : 3500.6, low : 3464.35, open : 3500.6, time : 2018-04-25 00:00:00,
51
+ trades : 1831, volume : 0, wap : 0.0>"]
52
+ =end
53
+ def self.fetch datum , &b # parameter: a date
54
+ # query_database( "select expand (out_tg_day_of.in[value = #{datum.day}]) from (select expand (out_tg_month_of.in[value = #{datum.month}]) from (select from tg_jahr where value = #{datum.year} ) ) ") &.first
55
+ q = OrientSupport::OrientQuery.new from: TG::Jahr, where: { value: datum.year }
56
+ w = OrientSupport::OrientQuery.new from: q
57
+ w.nodes :out, via: TG::MONTH_OF, where: { value: datum.month }
58
+ x = OrientSupport::OrientQuery.new from: w
59
+ x.nodes :out, via: TG::DAY_OF, where: { value: datum.day }
60
+ x.execute.first
61
+ end
62
+
63
+ end
@@ -0,0 +1,101 @@
1
+ class TG::TimeBase # < V
2
+
3
+ =begin
4
+ Searches for specific value records
5
+
6
+ Examples
7
+ Monat[8] --> Array of all August-month-records
8
+ Jahr[1930 .. 1945]
9
+ =end
10
+ def self.[] *key
11
+ # result = OrientSupport::Array.new( work_on: self,
12
+ # work_with: db.execute{ "select from #{ref_name} #{db.compose_where( value: key.analyse)}" } )
13
+ key= key.first if key.is_a?(Array) && key.size == 1
14
+ q= query.where( value: key).execute(reduce: true)
15
+ # result= query_database q
16
+ # result.size == 1 ? result.first : result # return object if only one record is fetched
17
+ end
18
+
19
+ =begin
20
+ Moves horizontally within the grid
21
+ i.e
22
+ the_day = "4.8.2000".to_tg
23
+ the_day.move(9).datum # => "13.8.2000"
24
+ the_day.move(-9).datum # => "26.7.2000"
25
+ =end
26
+ def move count
27
+ dir = count <0 ? 'in' : 'out'
28
+ r= db.execute { "select from ( traverse #{dir}(\"tg_grid_of\") from #{rrid} while $depth <= #{count.abs}) where $depth = #{count.abs} " }
29
+ if r.size == 1
30
+ r.first
31
+ else
32
+ nil
33
+ end
34
+ end
35
+
36
+
37
+ def analyse_key key # :nodoc:
38
+
39
+ new_key= if key.first.is_a?(Range)
40
+ key.first
41
+ elsif key.size ==1
42
+ key.first
43
+ else
44
+ key
45
+ end
46
+ end
47
+ =begin
48
+ Get the nearest horizontal neighbours
49
+
50
+ Takes one or two parameters.
51
+
52
+ (TG::TimeBase.instance).environment: count_of_previous_nodes, count_of_future_nodes
53
+
54
+ Default: return the previous and next 10 items
55
+
56
+ "22.4.1967".to_tg.environment.datum
57
+ => ["12.4.1967", "13.4.1967", "14.4.1967", "15.4.1967", "16.4.1967", "17.4.1967", "18.4.1967", "19.4.1967", "20.4.1967", "21.4.1967", "22.4.1967", "23.4.1967", "24.4.1967", "25.4.1967", "26.4.1967", "27.4.1967", "28.4.1967", "29.4.1967", "30.4.1967", "1.5.1967", "2.5.1967"]
58
+
59
+ It returns an OrientSupport::Array of TG::TimeBase-Objects
60
+
61
+
62
+
63
+ =end
64
+
65
+ # def environment previous_items = 10, next_items = nil
66
+ # next_items = previous_items if next_items.nil? # default : symmetric fetching
67
+ #
68
+ # my_query = -> (count) do
69
+ # dir = count <0 ? 'in' : 'out'
70
+ # db.execute { "select from ( traverse #{dir}(\"tg_grid_of\") from #{rrid} while $depth <= #{count.abs}) where $depth >=1 " } # don't fetch self and return an Array
71
+ # end
72
+ # prev_result = previous_items.zero? ? [] : my_query[ -previous_items.abs ]
73
+ # next_result = next_items.zero? ? [] : my_query[ next_items.abs ]
74
+ # # return a collection suitable for further operations
75
+ # OrientSupport::Array.new work_on: self, work_with: (prev_result.reverse << self | next_result )
76
+ #
77
+ # end
78
+ #
79
+
80
+ def environment previous_items = 10, next_items = nil
81
+ _environment(prevopus_items, next_items).execute
82
+ end
83
+
84
+ def _environment previous_items, next_items = nil
85
+ q = ->(**p){ OrientSupport::OrientQuery.new **p }
86
+
87
+ next_items = previous_items if next_items.nil? # default : symmetric fetching
88
+ local_var = :a
89
+ statement = q[]
90
+ { in: previous_items , out: next_items}.each do | dir, items|
91
+ traverse_query = query kind: 'traverse', while: "$depth <= #{items}"
92
+ traverse_query.nodes dir, via: TG::GRID_OF, expand: false
93
+
94
+ statement.let local_var => q[ from: traverse_query, where: '$depth >=1' ]
95
+ local_var = local_var.succ
96
+ end
97
+ statement.let '$c= UNIONALL($a,$b) '
98
+ statement.expand( '$c') # returns the statement
99
+ end
100
+
101
+ end
@@ -0,0 +1,4 @@
1
+ #ActiveOrient::Model.orientdb_class name: 'time_base', superclass: 'V'
2
+ class TG::TIME_OF # < E
3
+
4
+ end
@@ -0,0 +1,123 @@
1
+ class V
2
+
3
+ # If the time_graph is used any Vertex class inherents the methods defined below.
4
+ #
5
+ # Thus
6
+ #
7
+ # if the vertices are connected via "...grid.." edges, they can be accessed by
8
+ # * next
9
+ # * prev
10
+ # * move( count )
11
+ # * + (count)
12
+ # * - (count)
13
+ #
14
+ def next
15
+ nodes( :out, via: /grid/ , expand: true).first
16
+ end
17
+ def prev
18
+ nodes( :in, via: /grid/ , expand: true).first
19
+ end
20
+
21
+ # simplified and specialized form of traverse
22
+ #
23
+ # we follow the graph (Direction: out)
24
+ #
25
+ # start = "1.1.2000".to_tg.nodes( via: /temp/ ).first
26
+ # or
27
+ # start = the_asset.at("1.1.2000")
28
+ #
29
+ # start.vector(10)
30
+ #
31
+ # start.vector(10, function: :median){"mean" }
32
+ #
33
+ # with
34
+ # function: one of 'eval, min, max, sum abs, decimal, avg, count,mode, median, percentil, variance, stddev'
35
+ # and a block, specifying the property to work with
36
+ #
37
+ def vector length, where: nil, function: nil, start_at: 0
38
+ dir = length <0 ? :in : :out ;
39
+ the_vector_query = traverse dir, via: /grid/, depth: length.abs, where: where, execute: false
40
+ # the_vector_query.while "inE('ml_has_ohlc').out != #{to_tg.rrid} " if to_tg.present? future use
41
+ t= OrientSupport::OrientQuery.new from: the_vector_query
42
+ t.where "$depth >= #{start_at}"
43
+ if block_given?
44
+ if function.present?
45
+ t.projection( "#{function}(#{yield})")
46
+ t.execute{ |result| result.to_a.flatten.last}.first
47
+ else
48
+ t.projection yield
49
+ t.execute{ |result| result.to_a.flatten.last}.compact
50
+ end
51
+ else
52
+ t.execute
53
+ end
54
+ end
55
+
56
+
57
+
58
+
59
+
60
+ =begin
61
+ Moves horizontally within the grid
62
+ i.e
63
+ the_day = "4.8.2000".to_tg
64
+ the_day.move(9).datum # => "13.8.2000"
65
+ the_day.move(-9).datum # => "26.7.2000"
66
+ =end
67
+ def move count
68
+ dir = count <0 ? :in : :out
69
+ edge_class = detect_edges( dir, /grid/, expand: false )
70
+ q1 = OrientSupport::OrientQuery.new( kind: :traverse )
71
+ .while( " $depth <= #{count.abs}")
72
+ .from( self )
73
+ .nodes( dir, via: edge_class, expand: false)
74
+
75
+ q2= OrientSupport::OrientQuery.new from: q1, where: "$depth = #{count.abs} "
76
+ r = query q2
77
+
78
+
79
+ # r= db.execute { "select from ( traverse #{dir}(\"tg_grid_of\") from #{rrid} while $depth <= #{count.abs}) where $depth = #{count.abs} " }
80
+ if r.size == 1
81
+ r.first
82
+ else
83
+ nil
84
+ end
85
+ end
86
+ =begin
87
+ Get the node (item) grids in the future
88
+
89
+ i.e.
90
+ the_month = TG::Jahr[2000].monat(8).pop
91
+ the_month.value # -> 8
92
+ future_month = the_month + 6
93
+ future_month.value # -> 2
94
+ =end
95
+ def move_ item
96
+ move item
97
+ end
98
+ alias :+ :move_
99
+ =begin
100
+ Get the node (item) grids in the past
101
+
102
+ i.e.
103
+ the_day = "4.8.2000".to_tg
104
+ past_day = the_day - 6
105
+ past_day.datum # => "29.7.2000"
106
+ =end
107
+ def move__ item
108
+ move -item
109
+ end
110
+
111
+ alias :- :move__
112
+
113
+ # it is assumed, that any connection to the time-graph is done with an
114
+ # edge-class containing "has", ie: has_temperature, has_ohlc, has_an_appointment_with
115
+ def datum
116
+ nodes( :in, via: /has/ ).first.datum
117
+ end
118
+
119
+
120
+ def to_tg
121
+ nodes( :in, via: /has/ ).first
122
+ end
123
+ end
@@ -0,0 +1,25 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "orientdb-time-graph"
6
+ s.version = File.open('VERSION').read.strip
7
+ s.authors = ["Hartmut Bischoff"]
8
+ s.email = ["topofocus@gmail.com"]
9
+ s.homepage = 'https://github.com/topofocus/orientdb_time_graph'
10
+ s.licenses = ['MIT']
11
+ s.summary = 'Implementation of a time graph in active.orient'
12
+ s.description = ''
13
+ s.platform = Gem::Platform::RUBY
14
+ s.required_ruby_version = '>= 2.6'
15
+ s.date = Time.now.strftime "%Y-%m-%d"
16
+ s.test_files = `git ls-files -- {spec,features}/*`.split("\n")
17
+ s.require_paths = ["lib"]
18
+ s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ s.bindir = "exe"
20
+ s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+
22
+ s.add_development_dependency "bundler"
23
+ s.add_dependency 'active-orient'
24
+
25
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: orientdb-time-graph
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.7'
5
+ platform: ruby
6
+ authors:
7
+ - Hartmut Bischoff
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-12-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: active-orient
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: ''
42
+ email:
43
+ - topofocus@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - Guardfile
51
+ - LICENSE
52
+ - README.md
53
+ - VERSION
54
+ - bin/console
55
+ - config/boot.rb
56
+ - config/config.yml
57
+ - config/connect.yml
58
+ - lib/init_db.rb
59
+ - lib/orientdb_time_graph.rb
60
+ - lib/setup.rb
61
+ - lib/support.rb
62
+ - lib/time_graph.rb
63
+ - model/e.rb
64
+ - model/tg/day_of.rb
65
+ - model/tg/grid_of.rb
66
+ - model/tg/jahr.rb
67
+ - model/tg/monat.rb
68
+ - model/tg/month_of.rb
69
+ - model/tg/stunde.rb
70
+ - model/tg/tag.rb
71
+ - model/tg/time_base.rb
72
+ - model/tg/time_of.rb
73
+ - model/v.rb
74
+ - orientdb_time_graph.gemspec
75
+ homepage: https://github.com/topofocus/orientdb_time_graph
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '2.6'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubygems_version: 3.0.4
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Implementation of a time graph in active.orient
98
+ test_files: []