TreeMatcher.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-6.3.0).
003  * Copyright (c) 2007-2021 Franz Wilhelmstötter
004  *
005  * Licensed under the Apache License, Version 2.0 (the "License");
006  * you may not use this file except in compliance with the License.
007  * You may obtain a copy of the License at
008  *
009  *      http://www.apache.org/licenses/LICENSE-2.0
010  *
011  * Unless required by applicable law or agreed to in writing, software
012  * distributed under the License is distributed on an "AS IS" BASIS,
013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014  * See the License for the specific language governing permissions and
015  * limitations under the License.
016  *
017  * Author:
018  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
019  */
020 package io.jenetics.ext.rewriting;
021 
022 import static java.util.Objects.requireNonNull;
023 
024 import java.util.stream.Stream;
025 
026 import io.jenetics.ext.util.Tree;
027 
028 /**
029  * Implementation of a pattern based tree matcher. It allows you to iterate over
030  * all matches of a tree for a given pattern.
031  *
032  <pre>{@code
033  * final TreePattern<String> pattern = TreePattern.compile("add($x,$y)");
034  * final Tree<String, ?> tree = TreeNode.parse("add(1,add(2,3))");
035  * final TreeMatcher<String> matcher = pattern.matcher(tree);
036  * matcher.results().forEach(r -> System.out.println(r.tree().toParenthesesString()));
037  * // Prints:
038  * // add(1,add(2,3))
039  * // add(2,3)
040  * }</pre>
041  *
042  @see TreePattern#matcher(Tree)
043  *
044  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
045  @version 5.0
046  @since 5.0
047  */
048 public final class TreeMatcher<V> {
049 
050     private final TreePattern<V> _pattern;
051     private final Tree<V, ?> _tree;
052 
053     private TreeMatcher(final TreePattern<V> pattern, final Tree<V, ?> tree) {
054         _pattern = requireNonNull(pattern);
055         _tree = requireNonNull(tree);
056     }
057 
058     /**
059      * Return the underlying pattern of {@code this} matcher.
060      *
061      @return the underlying tree pattern
062      */
063     public TreePattern<V> pattern() {
064         return _pattern;
065     }
066 
067     /**
068      * Return the matching tree.
069      *
070      @return the matching tree
071      */
072     public Tree<V, ?> tree() {
073         return _tree;
074     }
075 
076     /**
077      * Tests if the tree matches the pattern.
078      *
079      @return {@code true} if the tree matches against the pattern,
080      *         {@code false} otherwise
081      @throws NullPointerException if the given predicate is {@code null}
082      */
083     public boolean matches() {
084         return _pattern.matches(_tree);
085     }
086 
087     /**
088      * Return all matching <em>sub</em>-trees.
089      *
090      @return all matching sub-trees
091      @throws NullPointerException if the given predicate is {@code null}
092      */
093     public Stream<TreeMatchResult<V>> results() {
094         return _tree.stream()
095             .flatMap(tree -> _pattern.match(tree).stream());
096     }
097 
098     static <V> TreeMatcher<V> of(
099         final TreePattern<V> pattern,
100         final Tree<V, ?> tree
101     ) {
102         return new TreeMatcher<>(pattern, tree);
103     }
104 
105 }