Antlr4 Listener subtree check condition - go

I'm new to Antlr, pardon me for basic question, I'm trying to validate the below statement, like if while_condition contains f_lastmove.. do something The while_condition can have other conditions as well. How can I drill down the while_condition? I'm using Listener Pattern with Golang.

I don't know Go, but in Java you could do something like this:
// In this example, the grammar is called `T.g4`
class WhileLastMoveListener extends TBaseListener {
private boolean insideWhileCondition = false;
#Override
public void enterWhile_condition(TParser.While_conditionContext ctx) {
this.insideWhileCondition = true;
}
#Override
public void exitWhile_condition(TParser.While_conditionContext ctx) {
this.insideWhileCondition = false;
}
#Override
public void enterF_lastmove(TParser.F_lastmoveContext ctx) {
if (this.insideWhileCondition) {
// Found a `f_lastmove` rule inside a while `while_condition`
}
}
}

Related

How to add user defined functions in Apache Storm

I am trying to solve a problem using Apache Storm. I have the following queries.
Is there any method to add user defined functions in Bolts other than the built in functions like execute(), prepare() etc? If possible, how to call such a function from execute()?
Also is it possible to add a 'recursive function' kind of logic in a Bolt?
Of course you can add any method to you bolt, and yes, it can also be recursive. I am not sure what you mean by "how to call such a function from execute() -- just call it from there -- it's a regular method:
public class MyBolt extends IRichBolt {
void prepare(Map stormConf, TopologyContext context, OutputCollector collector) { /* put your code here */ }
void cleanup() { /* put your code here */ }
void declareOutputFields(OutputFieldsDeclarer declarer) { /* put your code here */ }
Map<String, Object> getComponentConfiguration() { /* put your code here */ }
void execute(Tuple input) {
// just call the new methods
int x = myFirstFunction();
mySecondFunction(5);
}
// can also be public or protected etc (any return type or parameters are ok)
private int myFirstFunction() {
return 0;
}
// recursive
private void mySecondFunction(int a) {
while(--a > 0) {
mySecondFunction(a);
}
}
}

Eclipse Scout Neon ListBox: execValidateValue(..) not working

I have List box in new Neon Scout and I would like to validate value that was set.
I have implemented execValidateValue method :
#Override
protected Set<String> execValidateValue(final Set<String> rawValue) {
if (rawValue.contains(CONSTANT.UNKNOWN)) {
final Set<String> unknownSet = new HashSet<String>();
unknownSet.add(CONSTANT.UNKNOWN);
return super.execValidateValue(unknownSet);
}
return super.execValidateValue(rawValue);
}
but it doesn't seams to have any effect. While debugging I see that inside setValue(VALUE rawValue) method updateDisplayText(validatedValue) is called with right list of strings.
Why is that? Is there something that I did wrong?
Marko
You are right... If a value is changed during the validation (in execValidateValue(VALUE rawValue)) as suggested by the JavaDoc, the value is stored correctly in the Scout Model but the change is not reflected in the HTML-UI.
With the help of Samuel Renold, I have asked the team about it: The HTML-UI will be fixed to reflect the change in the UI. See bug 493778.
Test code for the Demo Widgets Application. Change the DefaultField in the ListBoxForm.
#Order(20)
public class DefaultField extends AbstractListBox<Color> {
#Override
protected Class<? extends ICodeType<?, Color>> getConfiguredCodeType() {
return ColorsCodeType.class;
}
#Override
protected Set<Color> execValidateValue(Set<Color> rawValue) {
System.out.println(">> execValidateValue");
printColors(rawValue);
if (rawValue != null && rawValue.contains(Color.RED)) {
return super.execValidateValue(Collections.singleton(Color.RED));
}
return super.execValidateValue(rawValue);
}
private void printColors(Set<Color> rawValue) {
if (rawValue != null) {
for (Color color : rawValue) {
System.out.print(color + ", ");
}
System.out.println("");
}
else {
System.out.println("null");
}
}
#Override
protected void execChangedValue() {
System.out.println(">> execValidateValue");
printColors(getValue());
}
#Override
protected int getConfiguredGridH() {
return 5;
}
#Override
protected String getConfiguredLabel() {
return TEXTS.get("Default");
}
}
The wrong behaviour can also be reproduced in Scout 4 (this release is end-of-life)

AjaxLink updateAjaxAttributes - AjaxCallListener getPrecondition

I stucked during migration from Wicket 1.5 to 6.6.
Earlier AjaxLink allowed to override getAjaxCallDecorator() method and wrap the script with preDecorateScript(CharSequence script).
Now recommended way is using AjaxCallListener with getPrecondition(Component component) method. But how can I wrap the source script using Component component parameter?
Dont know if i understand you correctly. We do it this way:
public class MyAjaxCallListener implements IAjaxCallListener{
#Override
public CharSequence getBeforeHandler(Component component) {
return null;
}
#Override
public CharSequence getPrecondition(Component component) {
return YOUR_SCRIPT;
}
// ... not needed overrides can return null
}
Then you add it through an Behavior to your AjaxLink.
ajaxLink.add(new AjaxEventBehavior("onclick") {
#Override
protected void onSubmit(AjaxRequestTarget target) {
//do stuff
}
#Override
protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
super.updateAjaxAttributes(attributes);
attributes.getAjaxCallListeners().add(new MyAjaxCallListener());
}
});

Minimal implementation of JavaFX TextInputArea

I'm investigating the best way to write a rich text editor in JavaFX - don't mention the HTMLEditor to me: we've spent literally months hacking at it and I could write reams about why it isn't suitable for our purposes! Choice at the moment is to extend AnchorPane and do all of the layout, navigation etc. from scratch or to extend TextInputArea, which looks as though it would help. Anyone have their own implementation of that or would like to propose a minimal implementation?
FWIW here's a scrap from me:
public class TryPain3 extends TextInputControl {
private AnchorPane rootNode = new AnchorPane();
public TryPain3() {
super(new Content() {
private String text = "";
#Override
public String get(int i, int i1) {
return text.substring(i, i1);
}
#Override
public void insert(int i, String string, boolean bln) {
}
#Override
public void delete(int i, int i1, boolean bln) {
}
#Override
public int length() {
return text.length();
}
#Override
public String get() {
return text;
}
#Override
public void addListener(ChangeListener<? super String> cl) {
}
#Override
public void removeListener(ChangeListener<? super String> cl) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
#Override
public String getValue() {
return text;
}
#Override
public void addListener(InvalidationListener il) {
}
#Override
public void removeListener(InvalidationListener il) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});
setEditable(true);
Text text1 = new Text("fred was here");
text1.setFont(Font.font("Tahoma", FontWeight.NORMAL, 18));
text1.setTextAlignment(TextAlignment.LEFT);
text1.setFontSmoothingType(FontSmoothingType.LCD);
rootNode.getChildren().add(text1);
setSkin(new TP3Skin(this, rootNode));
}
class TP3Skin implements Skin<TryPain3> {
TryPain3 tp;
Node root;
public TP3Skin(TryPain3 tp, Node root) {
this.tp = tp;
this.root = root;
}
#Override
public TryPain3 getSkinnable() {
return tp;
}
#Override
public Node getNode() {
return root;
}
#Override
public void dispose() {
tp = null;
rootNode = null;
}
}
}
It looks as though the skin is not optional.
Questions I'd like to find out are things like:
how is the UI supposed to be drawn - I'm quite happy to code it from scratch but how to get benefit of calls to forward() as an example
should the UI creation be done in the Skin?
whether the base class deals with things like where to put the cursor if you click on a bit of text
I'm sure other questions will arise from this.
You may want to try next JavaFX 8.0 control TextFlow, which allows aggregation of various text styles. See examples here: https://wikis.oracle.com/display/OpenJDK/Rich+Text+API+Samples
JavaFX 8 is part of JDK8. So you can download developers build here http://jdk8.java.net/download.html and it will include JavaFX and new TextFlow control.

dynamically register transaction listener with spring?

I have a springframework application in which I would like to add a transaction listener to a transaction which is currently in progress. The motivation is to trigger a post commit action which notifies downstream systems. I am using #Transactional to wrap a transaction around some service method -- which is where I want to create/register the post transaction listener. I want to do something "like" the following.
public class MyService {
#Transaction
public void doIt() {
modifyObjects();
// something like this
getTransactionManager().registerPostCommitAction(new
TransactionSynchronizationAdapter() {
public void afterCommit() {
notifyDownstream();
}
});
}
}
Spring has a TransactionSynchronization interface and adapter class which seems exactly what I want; however it is not immediately clear how to register one dynamically with either the current transaction, or the transaction manager. I would rather not subclass JtaTransactionManager if I can avoid it.
Q: Has anyone done this before.
Q: what is the simplest way to register my adapter?
Actually it was not as hard as I thought; spring has a static helper class that puts the 'right' stuff into the thread context.
TransactionSynchronizationManager.registerSynchronization(
new TransactionSynchronizationAdapter() {
#Override
public void afterCommit() {
s_logger.info("TRANSACTION COMPLETE!!!");
}
}
);
you could use an aspect to match transactional methods aspect in your service to accomplish this:
#Aspect
public class AfterReturningExample {
#AfterReturning("execution(* com.mypackage.MyService.*(..))")
public void afterReturning() {
// ...
}
}
Here is a more complete solution I did for a similar problem that with wanting my messages sent after transactions are committed (I could have used RabbitMQ TX but they are rather slow).
public class MessageBusUtils {
public static Optional<MessageBusResourceHolder> getTransactionalResourceHolder(TxMessageBus messageBus) {
if ( ! TransactionSynchronizationManager.isActualTransactionActive()) {
return Optional.absent();
}
MessageBusResourceHolder o = (MessageBusResourceHolder) TransactionSynchronizationManager.getResource(messageBus);
if (o != null) return Optional.of(o);
o = new MessageBusResourceHolder();
TransactionSynchronizationManager.bindResource(messageBus, o);
o.setSynchronizedWithTransaction(true);
if (TransactionSynchronizationManager.isSynchronizationActive()) {
TransactionSynchronizationManager.registerSynchronization(new MessageBusResourceSynchronization(o, messageBus));
}
return Optional.of(o);
}
private static class MessageBusResourceSynchronization extends ResourceHolderSynchronization<MessageBusResourceHolder, TxMessageBus> {
private final TxMessageBus messageBus;
private final MessageBusResourceHolder holder;
public MessageBusResourceSynchronization(MessageBusResourceHolder resourceHolder, TxMessageBus resourceKey) {
super(resourceHolder, resourceKey);
this.messageBus = resourceKey;
this.holder = resourceHolder;
}
#Override
protected void cleanupResource(MessageBusResourceHolder resourceHolder, TxMessageBus resourceKey,
boolean committed) {
resourceHolder.getPendingMessages().clear();
}
#Override
public void afterCompletion(int status) {
if (status == TransactionSynchronization.STATUS_COMMITTED) {
for (Object o : holder.getPendingMessages()) {
messageBus.post(o, false);
}
}
else {
holder.getPendingMessages().clear();
}
super.afterCompletion(status);
}
}
}
public class MessageBusResourceHolder extends ResourceHolderSupport {
private List<Object> pendingMessages = Lists.newArrayList();
public void addMessage(Object message) {
pendingMessages.add(message);
}
protected List<Object> getPendingMessages() {
return pendingMessages;
}
}
Now in your class where you actually send the message you will do
#Override
public void postAfterCommit(Object o) {
Optional<MessageBusResourceHolder> holder = MessageBusTxUtils.getTransactionalResourceHolder(this);
if (holder.isPresent()) {
holder.get().addMessage(o);
}
else {
post(o, false);
}
}
Sorry for the long winded coding samples but hopefully that will show someone how to do something after a commit.
Does it make sense to override the transaction manager on the commit and rollback methods, calling super.commit() right at the beginning.

Resources