strolch-website/docs/documentation/searches/index.html

53 lines
16 KiB
HTML

<!doctype html><html lang=en class="js csstransforms3d"><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=generator content="Hugo 0.80.0"><meta name=description content="Strolch is a parameterized framework for use on servers and IoT"><meta name=author content="Strolch"><link rel=icon href=/favicon.ico type=image/ico><title>Searches - Strolch</title><link href=/css/nucleus.css?1626091328 rel=stylesheet><link href=/css/fontawesome-all.min.css?1626091328 rel=stylesheet><link href=/css/hybrid.css?1626091328 rel=stylesheet><link href=/css/featherlight.min.css?1626091328 rel=stylesheet><link href=/css/perfect-scrollbar.min.css?1626091328 rel=stylesheet><link href=/css/auto-complete.css?1626091328 rel=stylesheet><link href=/css/atom-one-dark-reasonable.css?1626091328 rel=stylesheet><link href=/css/theme.css?1626091328 rel=stylesheet><link href=/css/hugo-theme.css?1626091328 rel=stylesheet><link href=/css/theme-green.css?1626091328 rel=stylesheet><script src=/js/jquery-3.3.1.min.js?1626091328></script><style>:root #header+#content>#left>#rlblock_left{display:none!important}</style></head><body data-url=/documentation/searches/><nav id=sidebar><div id=header-wrapper><div id=header><a id=logo href=/><img src=/logo.png></a></div><div class=searchbox><label for=search-by><i class="fas fa-search"></i></label><input data-search-input id=search-by type=search placeholder=Search...>
<span data-search-clear><i class="fas fa-times"></i></span></div><script type=text/javascript src=/js/lunr.min.js?1626091328></script><script type=text/javascript src=/js/auto-complete.js?1626091328></script><script type=text/javascript>var baseurl="https:\/\/strolch.li\/";</script><script type=text/javascript src=/js/search.js?1626091328></script></div><section id=homelinks><ul><li><a class=padding href=/><i class="fas fa-home"></i>Home</a></li></ul></section><div class=highlightable><ul class=topics><li data-nav-id=/api/ title=API class=dd-item><a href=/api/>API</a></li><li data-nav-id=/history/ title=History class=dd-item><a href=/history/>History</a></li><li data-nav-id=/documentation/ title=Documentation class="dd-item
parent"><a href=/documentation/>Documentation</a><ul><li data-nav-id=/documentation/architecture/ title=Architecture class=dd-item><a href=/documentation/architecture/>Architecture</a></li><li data-nav-id=/documentation/model/ title=Model class=dd-item><a href=/documentation/model/>Model</a></li><li data-nav-id=/documentation/do-and-donts/ title="Do and Don't" class=dd-item><a href=/documentation/do-and-donts/>Do and Don't</a></li><li data-nav-id=/documentation/runtime-configuration/ title="Runtime Configuration" class=dd-item><a href=/documentation/runtime-configuration/>Runtime Configuration</a></li><li data-nav-id=/documentation/realms/ title=Realms class=dd-item><a href=/documentation/realms/>Realms</a></li><li data-nav-id=/documentation/components/ title=Components class=dd-item><a href=/documentation/components/>Components</a></li><li data-nav-id=/documentation/services-and-commands/ title="Services and Commands" class=dd-item><a href=/documentation/services-and-commands/>Services and Commands</a></li><li data-nav-id=/documentation/searches/ title=Searches class="dd-item active"><a href=/documentation/searches/>Searches</a></li><li data-nav-id=/documentation/queries/ title=Queries class=dd-item><a href=/documentation/queries/>Queries</a></li><li data-nav-id=/documentation/transactions/ title=Transactions class=dd-item><a href=/documentation/transactions/>Transactions</a></li><li data-nav-id=/documentation/policies/ title=Policies class=dd-item><a href=/documentation/policies/>Policies</a></li><li data-nav-id=/documentation/observers/ title=Observers class=dd-item><a href=/documentation/observers/>Observers</a></li><li data-nav-id=/documentation/versioning/ title=Versioning class=dd-item><a href=/documentation/versioning/>Versioning</a></li><li data-nav-id=/documentation/reports/ title=Reports class=dd-item><a href=/documentation/reports/>Reports</a></li><li data-nav-id=/documentation/priviles/ title=Privileges class=dd-item><a href=/documentation/priviles/>Privileges</a></li></ul></li><li data-nav-id=/plc/ title=PLC class=dd-item><a href=/plc/>PLC</a><ul><li data-nav-id=/plc/architecture/ title=Architecture class=dd-item><a href=/plc/architecture/>Architecture</a></li><li data-nav-id=/plc/example-set-up/ title="Example Set-Up" class=dd-item><a href=/plc/example-set-up/>Example Set-Up</a></li></ul></li><li data-nav-id=/tutorial/ title=Tutorial class=dd-item><a href=/tutorial/>Tutorial</a><ul><li data-nav-id=/tutorial/configuration/ title=Configuration class=dd-item><a href=/tutorial/configuration/>Configuration</a></li><li data-nav-id=/tutorial/model/ title=Model class=dd-item><a href=/tutorial/model/>Model</a></li><li data-nav-id=/tutorial/crud-book/ title="CRUD Book" class=dd-item><a href=/tutorial/crud-book/>CRUD Book</a></li></ul></li><li data-nav-id=/download/ title=Download class=dd-item><a href=/download/>Download</a></li><li data-nav-id=/development/ title=Development class=dd-item><a href=/development/>Development</a></li></ul><section id=shortcuts><h3>More</h3><ul><li><a class=padding href=https://strolch.li/tags><i class="fas fa-tags"></i>Tags</a></li><li><a class=padding href=https://github.com/strolch-li><i class="fab fa-github"></i>GitHub project</a></li></ul></section><section id=footer><p>Built with <a href=https://github.com/matcornic/hugo-theme-learn><i class="fas fa-heart"></i></a>from <a href=https://getgrav.org>Grav</a> and <a href=https://gohugo.io/>Hugo</a></p></section></div></nav><section id=body><div id=overlay></div><div class="padding highlightable"><div><div id=top-bar><div id=top-github-link><a class=github-link title="Edit this page" href=https://github.com/Pi4J/pi4j.github.io/tree/main/contentdocumentation/searches.md target=blank><i class="fas fa-code-branch"></i><span id=top-github-link-text>Edit this page</span></a></div><div id=breadcrumbs itemscope itemtype=http://data-vocabulary.org/Breadcrumb><span id=sidebar-toggle-span><a href=# id=sidebar-toggle data-sidebar-toggle><i class="fas fa-bars"></i></a></span><span id=toc-menu><i class="fas fa-list-alt"></i></span><span class=links><a href=/>Strolch Overview</a> > <a href=/documentation/>Documentation</a> > Searches</span></div><div class=progress><div class=wrapper><nav id=TableOfContents><ul><li><a href=#searches>Searches</a></li></ul></nav></div></div></div></div><div id=head-tags></div><div id=body-inner><h1>Searches</h1><h2 id=searches>Searches</h2><p>As is custom for every framework, querying, or searching, the model must be
possible. Strolch searches are implemented using the <code>StrolchSearch</code> class and
one of its concrete implementations: <code>ResourceSearch</code>, <code>OrderSearch</code>,
<code>ActivitySearch</code>.</p><p>A Strolch element always has two identifiers: <code>Type</code> and <code>Id</code>. The type is
important as it classifies an element. So if a car and a house would be modelled
in Strolch, then those would both be a <code>Resource</code>, but one of type <code>Car</code>
and the other of type <code>House</code>. Both would have different parameters. Thus when
searching for objects, the first thing to do is define the type of object being
searched.</p><p>The Strolch search API is very expressive and offers multiple ways to perform
the same search. The search API consists of three components: The search
classes, the search expressions and the search predicates. The concept was taken
from the <a href=https://camel.apache.org/>Apache Camel</a> project.</p><p>There are four main search classes:</p><ul><li>RootElementSearch - search for any of Resource, Order or Activity elements</li><li>ResourceSearch - search for Resources</li><li>OrderSearch - search for Orders</li><li>ActivitySearch - search for Activities</li></ul><p>No search is useful without a <code>where</code> clause, which are called search
expressions. When writing a search, there are multiple ways to add such where
clauses. Either</p><ul><li>override the <code>define()</code>-method in your sub class and add the where clauses by
calling the <code>where()</code> method, or</li><li>define special methods on the class e.g. <code>byColor()</code> which also calls the
<code>where()</code>-method to add a search expression, or</li><li>directly call the <code>where()</code>-method after instantiating a search.</li></ul><p>When extending the class, then the search expressions are available as methods
on the super class, otherwise you can statically import them from
<a href=https://github.com/4treesCH/strolch/blob/develop/li.strolch.agent/src/main/java/li/strolch/search/ExpressionsSupport.java>ExpressionsSupport</a>
.</p><p>And of course a where clause needs operators, which are called search
predicates. Just as search expressions are available in sub classes, so are
search predicates and can also be statically imported through
<a href=https://github.com/4treesCH/strolch/blob/develop/li.strolch.agent/src/main/java/li/strolch/search/PredicatesSupport.java>PredicatesSupport</a>
.</p><p>Examples of search expressions with search predicates follow:</p><div class=highlight><pre style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-java data-lang=java>ResourceSearch search<span style=color:#f92672>=</span><span style=color:#66d9ef>new</span> ResourceSearch<span style=color:#f92672>();</span>
<span style=color:#75715e>// predicate either as parameter, or chained
</span><span style=color:#75715e></span>search<span style=color:#f92672>.</span><span style=color:#a6e22e>where</span><span style=color:#f92672>(</span>id<span style=color:#f92672>().</span><span style=color:#a6e22e>isEqualTo</span><span style=color:#f92672>(</span><span style=color:#e6db74>&#34;myId&#34;</span><span style=color:#f92672>));</span>
search<span style=color:#f92672>.</span><span style=color:#a6e22e>where</span><span style=color:#f92672>(</span>id<span style=color:#f92672>(</span>isEqualTo<span style=color:#f92672>(</span><span style=color:#e6db74>&#34;myId&#34;</span><span style=color:#f92672>)));</span>
<span style=color:#75715e>// negating
</span><span style=color:#75715e></span>search<span style=color:#f92672>.</span><span style=color:#a6e22e>where</span><span style=color:#f92672>(</span>id<span style=color:#f92672>(</span>isEqualTo<span style=color:#f92672>(</span><span style=color:#e6db74>&#34;myId&#34;</span><span style=color:#f92672>)).</span><span style=color:#a6e22e>not</span><span style=color:#f92672>());</span>
search<span style=color:#f92672>.</span><span style=color:#a6e22e>where</span><span style=color:#f92672>(</span>param<span style=color:#f92672>(</span><span style=color:#e6db74>&#34;bagId&#34;</span><span style=color:#f92672>,</span><span style=color:#e6db74>&#34;paramId&#34;</span><span style=color:#f92672>).</span><span style=color:#a6e22e>isIn</span><span style=color:#f92672>(</span>Arrays<span style=color:#f92672>.</span><span style=color:#a6e22e>asList</span><span style=color:#f92672>(</span><span style=color:#e6db74>&#34;red&#34;</span><span style=color:#f92672>,</span><span style=color:#e6db74>&#34;blue&#34;</span><span style=color:#f92672>,</span><span style=color:#e6db74>&#34;green&#34;</span><span style=color:#f92672>)));</span>
search<span style=color:#f92672>.</span><span style=color:#a6e22e>where</span><span style=color:#f92672>(</span>paramNull<span style=color:#f92672>(</span><span style=color:#e6db74>&#34;bagId&#34;</span><span style=color:#f92672>,</span><span style=color:#e6db74>&#34;paramId&#34;</span><span style=color:#f92672>)));</span>
<span style=color:#75715e>// boolean operations
</span><span style=color:#75715e></span>search<span style=color:#f92672>.</span><span style=color:#a6e22e>where</span><span style=color:#f92672>(</span>id<span style=color:#f92672>(</span>isEqualTo<span style=color:#f92672>(</span><span style=color:#e6db74>&#34;myId&#34;</span><span style=color:#f92672>))</span> <span style=color:#75715e>//
</span><span style=color:#75715e></span> <span style=color:#f92672>.</span><span style=color:#a6e22e>or</span><span style=color:#f92672>(</span>name<span style=color:#f92672>(</span>isEqualTo<span style=color:#f92672>(</span><span style=color:#e6db74>&#34;myName&#34;</span><span style=color:#f92672>))));</span>
</code></pre></div><p>Note how the predicates can be chained to the search expression, or passed as a
parameter to the expression.</p><p>In addition to using predefined search search expressions, one can also just
pass a lambda expression which performs a custom filter:</p><div class=highlight><pre style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-java data-lang=java>personSearch<span style=color:#f92672>.</span><span style=color:#a6e22e>where</span><span style=color:#f92672>(</span>person <span style=color:#f92672>-&gt;</span> person<span style=color:#f92672>.</span><span style=color:#a6e22e>getName</span><span style=color:#f92672>().</span><span style=color:#a6e22e>length</span><span style=color:#f92672>()</span> <span style=color:#f92672>==</span> 3<span style=color:#f92672>);</span>
</code></pre></div><p>See
the <a href=https://github.com/4treesCH/strolch/blob/develop/li.strolch.agent/src/test/java/li/strolch/search/StrolchSearchTest.java>StrolchSearchTest</a>
for many ways in which you can implement tests.</p><div class="notices tip"><p>Note that strolch searches requires privileges, thus when you
use a strolch search, add it to the role of the user in <code>PrivilegeRoles.xml</code>:</p></div><div class=highlight><pre style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-xml data-lang=xml>
<span style=color:#f92672>&lt;Privilege</span> <span style=color:#a6e22e>name=</span><span style=color:#e6db74>&#34;li.strolch.search.StrolchSearch&#34;</span> <span style=color:#a6e22e>policy=</span><span style=color:#e6db74>&#34;DefaultPrivilege&#34;</span><span style=color:#f92672>&gt;</span>
<span style=color:#f92672>&lt;Allow&gt;</span>internal
<span style=color:#f92672>&lt;/Allow&gt;</span> <span style=color:#75715e>&lt;!-- internal used for when the search is done in an internal service --&gt;</span>
<span style=color:#f92672>&lt;Allow&gt;</span>li.strolch.bookshop.search.BookSearch<span style=color:#f92672>&lt;/Allow&gt;</span>
<span style=color:#f92672>&lt;/Privilege&gt;</span>
</code></pre></div><footer class=footline></footer></div></div><div id=navigation><a class="nav nav-prev" href=/documentation/services-and-commands/ title="Services and Commands"><i class="fa fa-chevron-left"></i></a><a class="nav nav-next" href=/documentation/queries/ title=Queries style=margin-right:0><i class="fa fa-chevron-right"></i></a></div></section><div style=left:-1000px;overflow:scroll;position:absolute;top:-1000px;border:none;box-sizing:content-box;height:200px;margin:0;padding:0;width:200px><div style=border:none;box-sizing:content-box;height:200px;margin:0;padding:0;width:200px></div></div><script src=/js/clipboard.min.js?1626091328></script><script src=/js/perfect-scrollbar.min.js?1626091328></script><script src=/js/perfect-scrollbar.jquery.min.js?1626091328></script><script src=/js/jquery.sticky.js?1626091328></script><script src=/js/featherlight.min.js?1626091328></script><script src=/js/highlight.pack.js?1626091328></script><script>hljs.initHighlightingOnLoad();</script><script src=/js/modernizr.custom-3.6.0.js?1626091328></script><script src=/js/learn.js?1626091328></script><script src=/js/hugo-learn.js?1626091328></script><script src=/mermaid/mermaid.js?1626091328></script><script>mermaid.initialize({startOnLoad:true});</script></body></html>