Code Examples
A repository of 155 code examples for BeepBeep
ContextExample.java
1 /*
2  BeepBeep, an event stream processor
3  Copyright (C) 2008-2018 Sylvain HallĂ©
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published
7  by the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package xml;
19 
20 import ca.uqac.lif.cep.xml.XPathFunction;
21 import ca.uqac.lif.cep.fol.ForAll;
22 import ca.uqac.lif.cep.functions.ContextVariable;
23 import ca.uqac.lif.cep.functions.FunctionTree;
24 import ca.uqac.lif.cep.util.Bags;
25 import ca.uqac.lif.cep.util.Numbers;
26 import ca.uqac.lif.cep.xml.ParseXml;
27 import ca.uqac.lif.xml.XmlElement;
28 
29 /**
30  * Evaluate JPath expressions on JSON elements.
31  * @author Sylvain HallĂ©
32  */
33 public class ContextExample
34 {
35  public static void main(String[] args)
36  {
37  ///
38  // The domain function fetches the set all the values of elements <b>;
39  // the XPath function produces TextElements, which are converted to
40  // strings
41  FunctionTree d = new FunctionTree(
42  new Bags.ApplyToAll(Numbers.numberCast),
43  new XPathFunction("doc/a/b/text()"));
44  FunctionTree f = new FunctionTree(Numbers.isLessThan,
45  new ContextVariable("z"),
46  new FunctionTree(Numbers.numberCast,
47  new FunctionTree(Bags.anyElement,
48  new XPathFunction("doc/a[b=$z]/c/text()"))));
49  ForAll fa = new ForAll("z", d, f);
50  ///
51 
52  //!
53  // Let us evaluate this on a first document
54  Object[] out = new Object[1];
55  XmlElement x = ParseXml.instance.getValue("<doc>\n"
56  + "<a><b>1</b><c>10</c></a>\n"
57  + "<a><b>2</b><c>15</c></a>\n"
58  + "</doc>");
59  fa.evaluate(new Object[] {x}, out);
60  System.out.println(out[0]);
61  //!
62 
63  // Another one
64  x = ParseXml.instance.getValue("<doc>\n"
65  + "<a><b>1</b><c>10</c></a>\n"
66  + "<a><b>20</b><c>15</c></a>\n"
67  + "</doc>");
68  fa.evaluate(new Object[] {x}, out);
69  System.out.println(out[0]);
70  }
71 }
Evaluate JPath expressions on JSON elements.