Gtk.TreeModelFilter, no automatic way to show parent when using VisibleFunc? - gtk

It seems that there are two mutually-exclusive ways to use filter:
Setting a function to determine visibility.
Setting a boolean column to denote visibility.
The problem of (1) is that if any of the ancestors does not match the function, matching children will not show. For example, if the VisibleFunc returns true when the current node's name contains "b" for the following tree, only "bc" will be shown, and "ab" will not, because its parent "a" does not have "b".
a
ab
ac
b
bc
I think that in most use cases, this would not be something the user wants. I saw an existing question, and the answer was using (2) instead. Basically, what the answer does seemed to be manually traversing the tree, and if the current node matches the criteria, iterating up to the root node and changing the visibilities of all ancestors. It would work, but it seems kind of awkward, because it needs to modify the TreeStore itself and doing manual traversals.
Since I think it would be a common use case to show a matching child node, even if not all of its ancestors match, doesn't (1) have any option for this? Like, keep applying the VisibleFunc to all the descendants of a node that does not match the function, anyway, and automatically make all ancestors visible? Or, is using (2) the only way?

Related

Get children of Dom_html.element

In js_of_ocaml, is it possible to get the child nodes of Dom_html.element?
I know that the class inherits Dom.node, and thus has the childNodes method. But since it is a method from Dom.node, it returns values of Dom.node types. And I need those nodes to still be Dom_html.element, or else most methods will not be available.
Since downcasting is not possible in OCaml, I do not find any possible solution for this issue. Am I missing something or is this really impossible?
childNodes cant't be typed as a collection of Dom_html.elements because the nodes returned can, and are likely to, include nodes that are not elements, such as text nodes.
The DOM standard defines a property children on Element which would only return the elements, but that still wouldn't get you to Dom_html.element. And unfortunately it also does not seem to be included in JSOO's Dom.element.
You can use the element function of Dom.CoerceTo to safely coerce Dom.nodes to Dom.elements, but I don't think there is any generally reliable way to go from Dom.element to Dom_html.element, because the DOM is unfortunately too dynamically typed.
You might have to check the tagName manually and (unsafely) cast it using Js.Unsafe.coerce.

Scala AST avoid traversal of Tree children

I am working on a scalac plugin in which I am traversing the AST. Right now I am using a for each loop over the unit.body, which I get from the passed Global value. The issue I am facing is that due to the recursive nature of said for each traversal, I visit each Tree and its children, though I do not want to traverse the latter since I am using pattern matching to match for function calls.
So for example if I would traverse the statement:
x.do(arg1)(arg2)(arg3)
I will get the following things in my traversal:
1. x.do(arg1)(arg2)(arg3)
2. x.do(arg1)(arg2)
3. x.do(arg1)
4. x.do
5. x
6. do
7. arg1
8. arg2
9. arg3
Here I indented the Tree objects in order of traversal. Now if I would use the Apply case class to match with each of these, I would get a match out of 1,2, and 3, while I really only want 1. I thought about using a context sensitive solution (like check against the previous traversed Tree) but it is not consistent enough for my purposes. I am also not looking for specific methods, which would make matching easier, but for methods in general, but I cannot allow for these children to be counted.
Is there a better pattern I can match function calls with when writing a scalac plugin, or a better way to do the traversal, so that I do not run into this recursion issue?
I found the answer myself:
Instead of using the intuitive
for(tree <- treeObject), where treeObject : Tree
You can just use the children object in each Tree object and do the following
def func(treeObject : Tree) : Something = {
for(tree <- treeObject.children) {
if(/*your condition for further traversal*/)
func(treeObject : Tree) //RECURSE
else
someValue //do whatever and RETURN
}
}
With this approach you can set a stopping condition for traversal. So let's say you only want to look at each method invocation. A method invocation can be matched for using the Apply case class. Now you can just check whether or not your current tree matches with that pattern. If it does you do your work on it and return. If it is not traverse the Tree further by using recursion, to get possible method invocations further "down".

Drools RETE algorithm confusion

I am having an issue understanding RETE algorithm Beta node JoinNode and notNode?
Documentation says :
There are two two-input nodes, JoinNode and NotNode, and both are
types of BetaNodes. BetaNodes are used to compare 2 objects, and their
fields, to each other. The objects may be the same or different types.
By convention, we refer to the two inputs as left and right. The left
input for a BetaNode is generally a list of objects; in Drools this is
a Tuple. The right input is a single object. Two Nodes can be used to
implement 'exists' checks. BetaNodes also have memory. The left input
is called the Beta Memory and remembers all incoming tuples. The right
input is called the Alpha Memory and remembers all incoming objects.
I understood, Alpha Node: Various literal conditions for drl rules but above documentation for BetaNodes is confusing me a bit.
say below is drl condition for above diagram:
$person : Person( favouriteCheese == $cheddar )
Query: 1) what are these left and right inputs to two-input Beta Nodes exactly as explained in above documentation? I believe it's referring to facts and rules where I believe tuples would be facts?
2) notNode would be basically drl condition matching literal condition with not?
Updated question on 6Sep17:
3) I believe above diagram represent joinNode, how would notNode be represented , if above workflow is altered to suit notNode?
The condition corresponding to the diagram would be
Cheese( $name: name == "Cheddar" )
Person( favouriteCheese == $name )
Once there is a match, a new tuple consisting of the matching Cheese and Person is composed and can act as a new tuple for further matches if there is a third pattern in the condition.
A not-node would be one that asserts the non-existence of some fact. It would fire only once.
You might find a much better description of "rete" on the web.

How to check if the value is a number in Prolog manually?

How to check if the given value is a number in Prolog without using built-in predicates like number?
Let's say I have a list [a, 1, 2, 3]. I need a way to check if every element within this list is a number. The only part of the problem that bothers me is how to do the check itself without using the number predicate.
The reason why I'm trying to figure this out is that I've got a college assignment where it's specifically said not to use any of the built-in predicates.
You need some built-in predicate to solve this problem - unless you enumerate all numbers explicitly (which is not practical since there are infinitely many of them).
1
The most straight-forward would be:
maplist(number, L).
Or, recursively
allnumbers([]).
allnumbers([N|Ns]) :-
number(N),
allnumbers(Ns).
2
In a comment you say that "the value is given as an atom". That could mean that you get either [a, '1', '2'] or '[a, 1, 2]`. I assume the first. Here again, you need a built-in predicate to analyze the name. Relying on ISO-Prolog's errors we write:
numberatom(Atom) :-
atom_chars(Atom, Chs),
catch(number_chars(_, Chs), error(syntax_error(_),_), false).
Use numberatom/1 in place of number/1, So write a recurse rule or use maplist/2
3
You might want to write a grammar instead of the catch... goal. There have been many such definitions recently, you may look at this question.
4
If the entire "value" is given as an atom, you will need again atom_chars/2or you might want some implementation specific solution like atom_to_term/3 and then apply one of the solutions above.

construct a Tree of the TreeLoc of each of his nodes, in scalaz

TreeLoc is a zipper of a Tree
I have an Historic of action in a game that is stored in a tree (in order to store different final state in a save). So basically i have a TreeLoc[Action] in my Game (to know which Node is the current final state).
So now i want my user to be able to move into this historic. So what i want is to present him a Tree of his action and when he click on a Node, i calculate and replace my historic by the TreeLoc of the same root Tree but with focus on the right Node. It would be useless to create every TreeLoc associate with each Node, so i thought it would be a good solution to find a way to transform my Tree[Action] (which is the current TreeLoc[Action].toTree) into a Tree[(Action, () => TreeLoc[Action])]. How can i do such thing ?
I might not be understanding correctly, so please correct me if not.
You have a Tree[Action] which you present to the user, and when the user selects a node in that tree, and you want a TreeLoc focused on the selected node? Is that right?
Something like this?
val tree : Tree[Action] = // build your tree
def select(userSelected: Action) : Option[TreeLoc[Action]] = {
tree.loc.find(_.getLabel == userSelected)
}
The TreeLoc returned will be focused on the node that matched, but rooted in the same tree.
Obviously the find() might not be what you want, a unique id would probably be better than an equality test.