This example is included in the usecase-specs of ActiveOrient.

Thanks for bringing this up
  2 =begin
  3 BOOKs EXAMPLE
  4 
  5 There are several Books. And there is a Table with Keywords.
  6 
  7 The Keywords are associated to the books, this is realized via an Edge 
»has_content«
  8 
  9 Thus
 10 
 11       Book = @r.create_vertex_class :book
 12       Word = @r.create_vertex_class :word
 13       HC = @r.create_edge_class :has_content
 14 
 15 Books have a title and many edges to Word.
 16 Words have items, both properties use automatic indexes
 17 
 18       Book.create_property( :title, type: :string, index: :unique )
 19       Word.create_property( :item , type: :string, index: :unique )
 20 
 21 for each word:
 22       HC.create_edge from: this_book, to: this_word 
 23 
 24 To query which books have a given set of words:
 25    for each word this query loads a collection of books
 26 
 27       query = OrientSupport::OrientQuery.new  from: Word, projection: 
"expand(in('HasContent'))"#
 28    
 29    the query is completed by 
 30    
 31       query.where(  item: '{given word}'  )
 32 
 33 The pieces are tied together in a final query »q«
 34 
 35       q =  OrientSupport::OrientQuery.new projection: 'expand( $z )'
 36       desired_words = [ 'Land', 'Quartal' ]
 37       intersects =  Array.new 
 38 
 39       desired_words.each_with_index do | word, i |
 40           symbol = ( i+97 ).chr                         #  convert 1 -> 
'a' 
 41           q.let symbol => query.where(  item: word  )   #  add 
where-statement to let-block
 42           intersects << "$#{symbol}"                    #  remember 
what is added
 43       end
 44       q.let "$z = Intersect(#{intersects.join(', ')}) " #  add 
intersect-statement as last entry to let-block
 45       result = Word.query_database  q, set_from: false
 46   
 47 The generated Query »q« for two words that should appear in any book:
 48 
 49 select expand( $z ) let $a = ( select expand(in('HasContent')) from 
Word where item = 'Land' ), $b = ( select expand(in('HasContent')) from 
Word where item = 'Quartal' ), $z = Intersect($a, $b) 
 50 
 51 =end
 52 
 53 describe OrientSupport::OrientQuery do
 54   before( :all ) do
 55     ######################################## ADJUST user+password 
###################
 56     ActiveOrient::OrientDB.default_server= { user: 'hctw', password: 
'hc' }
 57     @r = ActiveOrient::OrientDB.new database: 'ArrayTest'
 58     TestQuery = @r.open_class "model_query"
 59     @record = TestQuery.create
 60   end # before
 61 
 62   context "Books Words example" , focus: true do
 63     before(:all) do
 64       @r.delete_class :book
 65       Book = @r.create_vertex_class :book
 66       Book.create_property( :title, type: :string, index: :unique )
 67       @r.delete_class :word
 68       Word = @r.create_vertex_class :word
 69       Word.create_property( :item , type: :string, index: :unique )
 70       @r.delete_class :has_content
 71       HC= @r.create_edge_class :has_content
 72 
 73     end
 74     it "check structure" do
 75       expect( @r.class_hierachie( base_class: 'V').sort ).to eq 
[Book.new.classname, Word.new.classname]
 76       expect( @r.class_hierachie( base_class: 'E') ).to eq 
[HC.new.classname]
 77     end
 78 
 79     it "put test-content" do
 80      fill_database = ->(sentence, this_book ) do
 81        sentence.split(' ').each do |x|
 82          this_word = Word.update_or_create where: { item: x }
 83          this_edge = HC.create_edge from: this_book, to: this_word  if 
this_word.present?
 84        end
 85       end
 86       words = 'Die Geschäfte in der Industrie im wichtigen 
US-Bundesstaat New York sind im August so schlecht gelaufen wie seit mehr 
als sechs Jahren nicht mehr Der entsprechende Empire-State-Index fiel 
überraschend von         …plus  Punkten im Juli auf minus 14,92 Zähler Dies 
teilte die New Yorker Notenbank Fed heut mit Bei Werten im positiven 
Bereich signalisiert das Barometer ein Wachstum Ökonomen hatten eigentlich 
mit einem Anstieg auf 5,0        …Punkte gerechnet'
 87       this_book =  Book.create title: 'first'
 88       fill_database[ words, this_book ]
 89       expect( Word.count ).to be > 10
 90 
 91      words2 = 'Das Bruttoinlandsprodukt BIP in Japan ist im zweiten 
Quartal mit einer aufs Jahr hochgerechneten Rate von Prozent geschrumpft Zu 
Jahresbeginn war die nach den USA und China drittgrößte Volkswirtschaft der 
        …Welt noch um  Prozent gewachsen Der Schwächeanfall wird auch als 
Rückschlag für Ministerpräsident Shinzo Abe gewertet der das Land mit einem 
Mix aus billigem Geld und Konjunkturprogramm aus der Flaute bringen will   
           …Allerdings wirkte sich die heutige Veröffentlichung auf die 
Märkten nur wenig aus da Ökonomen mit einem schwächeren zweiten Quartal 
gerechnet hatten'
 92      this_book =  Book.create title: 'second'
 93      fill_database[ words2, this_book ]
 94      expect( Word.count ).to be  > 100
 95     end
 96     it "Query Initialisation" do
 97       # search all books with words "Quartal" or "Landereien"
 98       query = OrientSupport::OrientQuery.new where: 
 "out('HasContent').item IN ['Quartal','Landereien']",
 99                                              from: Book
100       result= Book.query_database query
101       expect( result).to be_a Array
102       expect( result).to have_at_least(1).item
103       queried_book =  result.first
104       expect( queried_book ).to be_a ActiveOrient::Model::Book
105       expect( queried_book.title ).to eq 'second'
106 
107     end
108     it "Subquery Initialisation" do
109       # search for books wiht contain all given words 
110       query = OrientSupport::OrientQuery.new  from: Word, projection: 
"expand(in('HasContent'))"
111 
112       q =  OrientSupport::OrientQuery.new projection: 'expand( $z )'
113       intersects = Array.new
114       desired_words = [ 'Land', 'Quartal']
115       desired_words.each_with_index do | word, i |
116                symbol = ( i+97 ).chr   #  convert 1 -> 'a' 
117                q.let symbol =>  query.where(  item: word  )
118           intersects << "$#{symbol}"             
119       end
120       q.let   "$z = Intersect(#{intersects.join(', ')}) "
121       puts "generated Query:"
122       puts q.to_s
123       result = Word.query_database  q, set_from: false
124       expect( result.pop ).to be_a ActiveOrient::Model::Book
125     end
126 
127   end
128 end  # describe



-- 

--- 
You received this message because you are subscribed to the Google Groups 
"OrientDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to