001    package com.hammurapi.reasoning.impl;
002    
003    import java.util.ArrayList;
004    
005    import org.eclipse.emf.common.util.TreeIterator;
006    import org.eclipse.emf.ecore.EObject;
007    
008    import com.hammurapi.flow.Flow;
009    import com.hammurapi.flow.Node;
010    import com.hammurapi.flow.Pin;
011    import com.hammurapi.flow.Transition;
012    import com.hammurapi.flow.runtime.impl.PassThroughNode;
013    import com.hammurapi.flow.runtime.impl.PassThroughTransition;
014    
015    public class PassThroughOptimizer implements RuleSetFlowOptimizer {
016    
017            // TODO - Copy connection keys during optimization.
018            
019            @Override
020            public void optimize(Flow ruleSetFlow) {                
021                    boolean optimized;
022                    Z: do {
023                            optimized = false;
024                            TreeIterator<EObject> rcit = ruleSetFlow.eAllContents();
025                            while (rcit.hasNext()) {
026                                    EObject next = rcit.next();
027                                    if (next instanceof Node) {                                     
028                                            Node node = (Node) next;
029                                            if (PassThroughNode.class.getName().equals(node.getType())) {
030                                                    Pin pin = node.getPin().get(0);
031                                                    if (pin.getInput().size()==1) {
032                                                            Transition input = pin.getInput().get(0);
033                                                            if (PassThroughTransition.class.getName().equals(input.getType())) {
034                                                                    for (Transition output: new ArrayList<Transition>(pin.getOutput())) {
035                                                                            output.setFromPin(input.getFromPin());
036                                                                    }
037                                                                    input.setFromPin(null);
038                                                                    ruleSetFlow.getFlowElement().remove(input);
039                                                                    ruleSetFlow.getFlowElement().remove(node);
040                                                                    optimized = true;
041                                                                    continue Z;
042                                                            }
043                                                    }
044                                                    
045                                                    if (pin.getOutput().size()==1) {
046                                                            Transition output = pin.getOutput().get(0);
047                                                            if (PassThroughTransition.class.getName().equals(output.getType())) {
048                                                                    for (Transition input: new ArrayList<Transition>(pin.getInput())) {
049                                                                            input.setToPin(output.getToPin());
050                                                                    }
051                                                                    output.setToPin(null);
052                                                                    ruleSetFlow.getFlowElement().remove(output);
053                                                                    ruleSetFlow.getFlowElement().remove(node);
054                                                                    optimized = true;
055                                                                    continue Z;
056                                                            }
057                                                    }
058                                            }
059                                    }
060                            }                                               
061                    } while (optimized);            
062            }
063            
064            @Override
065            public int getOrder() {
066                    return 0;
067            }
068    
069    }