Coverage Report - org.deri.wsmo4j.io.parser.wsml.RuleAnalyzer
 
Classes in this File Line Coverage Branch Coverage Complexity
RuleAnalyzer
0%
0/311
0%
0/116
0
 
 1  
 /*
 2  
  wsmo4j - a WSMO API and Reference Implementation
 3  
 
 4  
  Copyright (c) 2004-2005, OntoText Lab. / SIRMA
 5  
 
 6  
  This library is free software; you can redistribute it and/or modify it under
 7  
  the terms of the GNU Lesser General Public License as published by the Free
 8  
  Software Foundation; either version 2.1 of the License, or (at your option)
 9  
  any later version.
 10  
  This library is distributed in the hope that it will be useful, but WITHOUT
 11  
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 12  
  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 13  
  details.
 14  
  You should have received a copy of the GNU Lesser General Public License along
 15  
  with this library; if not, write to the Free Software Foundation, Inc.,
 16  
  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 17  
  */
 18  
 
 19  
 /*
 20  
  wsmo4j - a WSMO API and Reference Implementation
 21  
 
 22  
  Copyright (c) 2004-2005, OntoText Lab. / SIRMA
 23  
 
 24  
  This library is free software; you can redistribute it and/or modify it under
 25  
  the terms of the GNU Lesser General Public License as published by the Free
 26  
  Software Foundation; either version 2.1 of the License, or (at your option)
 27  
  any later version.
 28  
  This library is distributed in the hope that it will be useful, but WITHOUT
 29  
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 30  
  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 31  
  details.
 32  
  You should have received a copy of the GNU Lesser General Public License along
 33  
  with this library; if not, write to the Free Software Foundation, Inc.,
 34  
  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 35  
  */
 36  
 
 37  
 /**
 38  
  * <p>Title: WSMO4J</p>
 39  
  * <p>Description: WSMO API and a Reference Implementation</p>
 40  
  * <p>Copyright:  Copyright (c) 2004-2005</p>
 41  
  * <p>Company: OntoText Lab. / SIRMA </p>
 42  
  */
 43  
 
 44  
 package org.deri.wsmo4j.io.parser.wsml;
 45  
 
 46  
 import java.util.*;
 47  
 
 48  
 import org.deri.wsmo4j.io.parser.*;
 49  
 import org.omwg.logicalexpression.*;
 50  
 import org.omwg.logicalexpression.terms.*;
 51  
 import org.omwg.ontology.*;
 52  
 import org.wsmo.common.*;
 53  
 import org.wsmo.common.exception.*;
 54  
 import org.wsmo.factory.*;
 55  
 import org.wsmo.mediator.*;
 56  
 import org.wsmo.service.choreography.rule.*;
 57  
 import org.wsmo.service.orchestration.*;
 58  
 import org.wsmo.service.orchestration.rule.*;
 59  
 import org.wsmo.service.rule.*;
 60  
 import org.wsmo.wsml.*;
 61  
 import org.wsmo.wsml.compiler.node.*;
 62  
 
 63  
 import com.ontotext.wsmo4j.parser.*;
 64  
 import com.ontotext.wsmo4j.parser.wsml.*;
 65  
 
 66  0
 @SuppressWarnings("unchecked")
 67  
 public class RuleAnalyzer extends ASTAnalysis {
 68  
 
 69  
     private WsmoFactory wsmoFactory;
 70  
     private ChoreographyFactory factory; 
 71  
     private OrchestrationFactory oFactory;
 72  
     private LogicalExpressionFactory leFactory;
 73  
     private ASTAnalysisContainer container;
 74  
     private Stack ruleStack;
 75  
     private Stack ruleListStack;
 76  
 
 77  
     public RuleAnalyzer(ASTAnalysisContainer container, WsmoFactory wsmoFactory,
 78  
             OrchestrationFactory oFactory, ChoreographyFactory factory, 
 79  0
             LogicalExpressionFactory leFactory) {
 80  0
         if (container == null || factory == null || leFactory == null ||
 81  
                 oFactory == null || wsmoFactory == null) {
 82  0
             throw new IllegalArgumentException();
 83  
         }
 84  0
         this.container = container;
 85  0
         this.factory = factory;
 86  0
         this.oFactory = oFactory;
 87  0
         this.leFactory = leFactory;
 88  0
         this.wsmoFactory = wsmoFactory;
 89  
 
 90  
         // register the handled nodes
 91  0
         container.registerNodeHandler(ATransitions.class, this);
 92  0
         container.registerNodeHandler(AOrchestrationTransitions.class, this);
 93  0
         container.registerNodeHandler(AIfRule.class, this);
 94  0
         container.registerNodeHandler(AOrchIfOrchestrationRule.class, this);
 95  0
         container.registerNodeHandler(AChooseRule.class, this);
 96  0
         container.registerNodeHandler(AOrchChooseOrchestrationRule.class, this);
 97  0
         container.registerNodeHandler(AForallRule.class, this);
 98  0
         container.registerNodeHandler(AOrchForallOrchestrationRule.class, this);
 99  0
         container.registerNodeHandler(AOrchPerformOrchestrationRule.class, this);
 100  0
         container.registerNodeHandler(AOrchApplyMediationOrchPerformAlt.class, this);
 101  0
         container.registerNodeHandler(AOrchInvokeServiceOrchPerformAlt.class, this);
 102  0
         container.registerNodeHandler(AOrchPerformAchievegoalOrchPerformAlt.class, this);
 103  0
         container.registerNodeHandler(AOrchPerformSendOrchPerformAlt.class, this);
 104  0
         container.registerNodeHandler(AOrchPerformReceiveOrchPerformAlt.class, this);
 105  0
         container.registerNodeHandler(ARestrictedLeCondition.class, this);
 106  0
         container.registerNodeHandler(AUpdaterule.class, this);
 107  0
         container.registerNodeHandler(AFactPreferredFact.class, this);
 108  0
         container.registerNodeHandler(AFactNonpreferredFact.class, this);
 109  0
         container.registerNodeHandler(AFactMoleculeFact.class, this);
 110  0
         container.registerNodeHandler(AFactRelationFact.class, this);
 111  0
         container.registerNodeHandler(ASingleTermUpdate.class, this);
 112  0
         container.registerNodeHandler(AMoveTermUpdate.class, this);
 113  0
         container.registerNodeHandler(AAttrFactList.class, this);
 114  0
         container.registerNodeHandler(AAttrRelationAttrFactList.class, this);
 115  0
         container.registerNodeHandler(APpmediator.class, this);
 116  0
         ruleStack = container.getStack(ChoreographyRule.class);
 117  
         /* to overcome the problem that ChoreographyRules and OrchestrationRules
 118  
         do not have a common super interface, a random type is used to identify
 119  
         the stack of Choreography/OrchestratonRules */
 120  0
         ruleListStack = container.getStack(RuleAnalyzer.class);
 121  0
     }
 122  
 
 123  
     // Rules
 124  
 
 125  
     private Object lastRule;
 126  
 
 127  
     public void inATransitions(ATransitions node) {
 128  0
         ChoreographyRules rules = null;
 129  0
         Stack identifierStack = container.getStack(Identifier.class);
 130  0
         if (node.getId() instanceof AAnonymousId || node.getId() == null) {
 131  0
             rules = factory.containers.createRules(wsmoFactory.createAnonymousID(),
 132  
                     new HashSet<ChoreographyRule>());
 133  
         }
 134  
         else {
 135  0
             node.getId().apply(container.getNodeHandler(PId.class));
 136  0
             rules = factory.containers
 137  
                     .createRules((IRI) identifierStack.pop(), new HashSet<ChoreographyRule>());
 138  
         }
 139  0
         ruleListStack.push(rules);
 140  
 
 141  
         // mark the last rule
 142  0
         if (ruleStack.isEmpty()) {
 143  0
             lastRule = null;
 144  0
             return;
 145  
         }
 146  0
         lastRule = ruleStack.peek();
 147  0
     }
 148  
     
 149  
     public void inAOrchestrationTransitions(AOrchestrationTransitions node) {
 150  0
         OrchestrationRules rules = null;
 151  0
         Stack identifierStack = container.getStack(Identifier.class);
 152  0
         if (node.getId() instanceof AAnonymousId || node.getId() == null) {
 153  0
             rules = oFactory.containers.createRules(wsmoFactory.createAnonymousID(),
 154  
                     new HashSet<Rule>());
 155  
         }
 156  
         else {
 157  0
             node.getId().apply(container.getNodeHandler(PId.class));
 158  0
             rules = oFactory.containers
 159  
                     .createRules((IRI) identifierStack.pop(), new HashSet<Rule>());
 160  
         }
 161  0
         ruleListStack.push(rules);
 162  
 
 163  
         // mark the last rule
 164  0
         if (ruleStack.isEmpty()) {
 165  0
             lastRule = null;
 166  0
             return;
 167  
         }
 168  0
         lastRule = ruleStack.peek();
 169  0
     }
 170  
 
 171  
     // Transition rule
 172  
 
 173  
     public void inAIfRule(AIfRule node) {
 174  0
         ChoreographyIfThen rule = factory.transitionRules.createIfThen(null, new HashSet<ChoreographyRule>());
 175  0
         if (lastRule == null && !ruleStack.isEmpty()) { // if inner rule added to the parent
 176  0
             ((ChoreographyTransitionRule) ruleStack.peek()).addRule(rule);
 177  
         }
 178  
         else {
 179  0
             ((ChoreographyRules) ruleListStack.peek()).addRule(rule);
 180  
         }
 181  0
         ruleStack.push(rule);
 182  0
     }
 183  
     
 184  
     public void inAOrchIfOrchestrationRule(AOrchIfOrchestrationRule node) {
 185  0
         OrchestrationIfThen rule = oFactory.transitionRules.createIfThen(null, new HashSet<Rule>());
 186  0
         if (lastRule == null && !ruleStack.isEmpty()) { // if inner rule added to the parent
 187  0
             ((OrchestrationTransitionRule) ruleStack.peek()).addRule(rule);
 188  
         }
 189  
         else {
 190  0
             ((OrchestrationRules) ruleListStack.peek()).addRule(rule);
 191  
         }
 192  0
         ruleStack.push(rule);
 193  0
     }
 194  
 
 195  
     public void outAIfRule(AIfRule node) {
 196  0
         Condition con = (Condition) container.popFromStack(Condition.class, Condition.class);
 197  0
         ((ChoreographyIfThen) ruleStack.pop()).setCondition(con);
 198  0
     }
 199  
     
 200  
     public void outAOrchIfOrchestrationRule(AOrchIfOrchestrationRule node) {
 201  0
         Condition con = (Condition) container.popFromStack(Condition.class, Condition.class);
 202  0
         ((OrchestrationIfThen) ruleStack.pop()).setCondition(con);
 203  0
     }
 204  
 
 205  
     public void inAChooseRule(AChooseRule node) {
 206  0
         ChoreographyChoose rule = factory.transitionRules.createChoose(new HashSet<Variable>(), null,
 207  
                 new HashSet<ChoreographyRule>());
 208  0
         if (lastRule == null && !ruleStack.isEmpty()) { // if inner rule added to the parent
 209  0
             ((ChoreographyTransitionRule) ruleStack.peek()).addRule(rule);
 210  
         }
 211  
         else {
 212  0
             ((ChoreographyRules) ruleListStack.peek()).addRule(rule);
 213  
         }
 214  0
         ruleStack.push(rule);
 215  0
     }
 216  
     
 217  
     public void inAOrchChooseOrchestrationRule(AOrchChooseOrchestrationRule node) {
 218  0
         OrchestrationChoose rule = oFactory.transitionRules.createChoose(new HashSet<Variable>(), null,
 219  
                 new HashSet<Rule>());
 220  0
         if (lastRule == null && !ruleStack.isEmpty()) { // if inner rule added to the parent
 221  0
             ((OrchestrationTransitionRule) ruleStack.peek()).addRule(rule);
 222  
         }
 223  
         else {
 224  0
             ((OrchestrationRules) ruleListStack.peek()).addRule(rule);
 225  
         }
 226  0
         ruleStack.push(rule);
 227  0
     }
 228  
 
 229  
     public void outAChooseRule(AChooseRule node) {
 230  0
         Variable[] vars = (Variable[]) container.popFromStack(Variable[].class, Variable[].class);
 231  0
         Condition con = (Condition) container.popFromStack(Condition.class, Condition.class);
 232  0
         ChoreographyChoose rule = (ChoreographyChoose) ruleStack.pop();
 233  0
         for (int i = 0; i < vars.length; i++) {
 234  0
             rule.addVariable(vars[i]);
 235  
         }
 236  0
         rule.setCondition(con);
 237  0
     }
 238  
     
 239  
     public void outAOrchChooseOrchestrationRule(AOrchChooseOrchestrationRule node) {
 240  0
         Variable[] vars = (Variable[]) container.popFromStack(Variable[].class, Variable[].class);
 241  0
         Condition con = (Condition) container.popFromStack(Condition.class, Condition.class);
 242  0
         OrchestrationChoose rule = (OrchestrationChoose) ruleStack.pop();
 243  0
         for (int i = 0; i < vars.length; i++) {
 244  0
             rule.addVariable(vars[i]);
 245  
         }
 246  0
         rule.setCondition(con);
 247  0
     }
 248  
 
 249  
     public void inAForallRule(AForallRule node) {
 250  0
         ChoreographyForAll rule = factory.transitionRules.createForAll(new HashSet<Variable>(), null,
 251  
                 new HashSet<ChoreographyRule>());
 252  0
         if (lastRule == null && !ruleStack.isEmpty()) { // if inner rule added to the parent
 253  0
             ((ChoreographyTransitionRule) ruleStack.peek()).addRule(rule);
 254  
         }
 255  
         else {
 256  0
             ((ChoreographyRules) ruleListStack.peek()).addRule(rule);
 257  
         }
 258  0
         ruleStack.push(rule);
 259  0
     }
 260  
     
 261  
     public void inAOrchForallOrchestrationRule(AOrchForallOrchestrationRule node) {
 262  0
         OrchestrationForAll rule = oFactory.transitionRules.createForAll(new HashSet<Variable>(), null,
 263  
                 new HashSet<Rule>());
 264  0
         if (lastRule == null && !ruleStack.isEmpty()) { // if inner rule added to the parent
 265  0
             ((OrchestrationTransitionRule) ruleStack.peek()).addRule(rule);
 266  
         }
 267  
         else {
 268  0
             ((OrchestrationRules) ruleListStack.peek()).addRule(rule);
 269  
         }
 270  0
         ruleStack.push(rule);
 271  0
     }
 272  
 
 273  
     public void outAForallRule(AForallRule node) {
 274  0
         Variable[] vars = (Variable[]) container.popFromStack(Variable[].class, Variable[].class);
 275  0
         Condition con = (Condition) container.popFromStack(Condition.class, Condition.class);
 276  0
         ChoreographyForAll rule = (ChoreographyForAll) ruleStack.pop();
 277  0
         for (int i = 0; i < vars.length; i++) {
 278  0
             rule.addVariable(vars[i]);
 279  
         }
 280  0
         rule.setCondition(con);
 281  0
     }
 282  
     
 283  
     public void outAOrchForallOrchestrationRule(AOrchForallOrchestrationRule node) {
 284  0
         Variable[] vars = (Variable[]) container.popFromStack(Variable[].class, Variable[].class);
 285  0
         Condition con = (Condition) container.popFromStack(Condition.class, Condition.class);
 286  0
         OrchestrationForAll rule = (OrchestrationForAll) ruleStack.pop();
 287  0
         for (int i = 0; i < vars.length; i++) {
 288  0
             rule.addVariable(vars[i]);
 289  
         }
 290  0
         rule.setCondition(con);
 291  0
     }
 292  
     
 293  
     private IRI performId;
 294  
     
 295  
     public void inAOrchPerformOrchestrationRule(AOrchPerformOrchestrationRule node) {
 296  0
         TopEntityAnalysis.isValidTopEntityIdentifier(node.getId(), node.getTPerform());
 297  0
         node.getId().apply(container.getNodeHandler(PId.class));
 298  0
         performId = (IRI) container.popFromStack(Identifier.class, Identifier.class);
 299  0
     }
 300  
     
 301  
     public void inAOrchApplyMediationOrchPerformAlt(AOrchApplyMediationOrchPerformAlt node) {
 302  0
         TopEntityAnalysis.isValidTopEntityIdentifier(node.getId(), node.getTApplymediation());
 303  0
         node.getId().apply(container.getNodeHandler(PId.class));
 304  0
         IRI iri = (IRI) container.popFromStack(Identifier.class, Identifier.class);
 305  0
         OrchestrationApplyMediation rule = 
 306  
             oFactory.updateRules.createOrchestrationApplyMediation(performId, 
 307  
                     oFactory.updateRules.createPpMediator(iri));
 308  
         
 309  0
         if (lastRule == null && !ruleStack.isEmpty()) { // if inner rule added to the parent
 310  0
             ((OrchestrationTransitionRule) ruleStack.peek()).addRule(rule);
 311  
         }
 312  
         else {
 313  0
             ((OrchestrationRules) ruleListStack.peek()).addRule(rule);
 314  
         }
 315  0
         ruleStack.push(rule);
 316  0
     }
 317  
     
 318  
     public void outAOrchApplyMediationOrchPerformAlt(AOrchApplyMediationOrchPerformAlt node) {
 319  0
         ruleStack.pop();
 320  0
     }
 321  
     
 322  
     public void inAOrchInvokeServiceOrchPerformAlt(AOrchInvokeServiceOrchPerformAlt node) {
 323  0
         TopEntityAnalysis.isValidTopEntityIdentifier(node.getId(), node.getTInvokeservice());
 324  0
         node.getId().apply(container.getNodeHandler(PId.class));
 325  0
         IRI iri = (IRI) container.popFromStack(Identifier.class, Identifier.class);
 326  0
         OrchestrationInvokeService rule = 
 327  
             oFactory.updateRules.createOrchestrationInvokeService(performId, 
 328  
                     wsmoFactory.getWebService(iri));
 329  
         
 330  0
         if (lastRule == null && !ruleStack.isEmpty()) { // if inner rule added to the parent
 331  0
             ((OrchestrationTransitionRule) ruleStack.peek()).addRule(rule);
 332  
         }
 333  
         else {
 334  0
             ((OrchestrationRules) ruleListStack.peek()).addRule(rule);
 335  
         }
 336  0
         ruleStack.push(rule);
 337  0
     }
 338  
     
 339  
     public void outAOrchInvokeServiceOrchPerformAlt(AOrchInvokeServiceOrchPerformAlt node) {
 340  0
         ruleStack.pop();
 341  0
     }
 342  
     
 343  
     public void inAOrchPerformAchievegoalOrchPerformAlt(AOrchPerformAchievegoalOrchPerformAlt node) {
 344  0
         TopEntityAnalysis.isValidTopEntityIdentifier(node.getId(), node.getTAchievegoal());
 345  0
         node.getId().apply(container.getNodeHandler(PId.class));
 346  0
         IRI iri = (IRI) container.popFromStack(Identifier.class, Identifier.class);
 347  0
         OrchestrationAchieveGoal rule = 
 348  
             oFactory.updateRules.createOrchestrationAchieveGoal(performId, 
 349  
                     wsmoFactory.getGoal(iri));
 350  
         
 351  0
         if (lastRule == null && !ruleStack.isEmpty()) { // if inner rule added to the parent
 352  0
             ((OrchestrationTransitionRule) ruleStack.peek()).addRule(rule);
 353  
         }
 354  
         else {
 355  0
             ((OrchestrationRules) ruleListStack.peek()).addRule(rule);
 356  
         }
 357  0
         ruleStack.push(rule);
 358  0
     }
 359  
     
 360  
     public void outAOrchPerformAchievegoalOrchPerformAlt(AOrchPerformAchievegoalOrchPerformAlt node) {
 361  0
         ruleStack.pop();
 362  0
     }
 363  
     
 364  
     public void inAOrchPerformSendOrchPerformAlt(AOrchPerformSendOrchPerformAlt node) {
 365  0
         TopEntityAnalysis.isValidTopEntityIdentifier(node.getId(), node.getTTarget());
 366  0
         node.getId().apply(container.getNodeHandler(PId.class));
 367  0
         IRI iri = (IRI) container.popFromStack(Identifier.class, Identifier.class);
 368  0
         Send rule = 
 369  
             oFactory.updateRules.createSend(performId, iri);
 370  
         
 371  0
         if (lastRule == null && !ruleStack.isEmpty()) { // if inner rule added to the parent
 372  0
             ((OrchestrationTransitionRule) ruleStack.peek()).addRule(rule);
 373  
         }
 374  
         else {
 375  0
             ((OrchestrationRules) ruleListStack.peek()).addRule(rule);
 376  
         }
 377  0
         ruleStack.push(rule);
 378  0
     }
 379  
     
 380  
     public void outAOrchPerformSendOrchPerformAlt(AOrchPerformSendOrchPerformAlt node) {
 381  0
         ruleStack.pop();
 382  0
     }
 383  
     
 384  
     public void inAOrchPerformReceiveOrchPerformAlt(AOrchPerformReceiveOrchPerformAlt node) {
 385  0
         TopEntityAnalysis.isValidTopEntityIdentifier(node.getId(), node.getTSource());
 386  0
         node.getId().apply(container.getNodeHandler(PId.class));
 387  0
         IRI iri = (IRI) container.popFromStack(Identifier.class, Identifier.class);
 388  0
         Receive rule =  oFactory.updateRules.createReceive(performId, iri);
 389  
         
 390  0
         if (lastRule == null && !ruleStack.isEmpty()) { // if inner rule added to the parent
 391  0
             ((OrchestrationTransitionRule) ruleStack.peek()).addRule(rule);
 392  
         }
 393  
         else {
 394  0
             ((OrchestrationRules) ruleListStack.peek()).addRule(rule);
 395  
         }
 396  0
         ruleStack.push(rule);
 397  0
     }
 398  
     
 399  
     public void outAOrchPerformReceiveOrchPerformAlt(AOrchPerformReceiveOrchPerformAlt node) {
 400  0
         ruleStack.pop();
 401  0
     }
 402  
 
 403  
     // end Transition rule
 404  
 
 405  
     // Condition
 406  
 
 407  
     public void outARestrictedLeCondition(ARestrictedLeCondition node) {
 408  0
         LogicalExpression expr = (LogicalExpression) container.popFromStack(
 409  
                 LogicalExpression.class, LogicalExpression.class);
 410  
         
 411  
         try {
 412  0
             Condition c = factory.transitionRules.createConditionFromLogicalExpression(expr);
 413  0
             container.getStack(Condition.class).push(c);
 414  
         }
 415  0
         catch (InvalidModelException ex) {
 416  0
             ParserException pe = new ParserException(ex.getMessage(), null);
 417  0
             throw new WrappedParsingException(pe);
 418  0
         }
 419  0
     }
 420  
 
 421  
     // end Condition
 422  
     
 423  
     private PModifier modifier;
 424  
     
 425  
     public void inAUpdaterule(AUpdaterule node) {
 426  0
         modifier = node.getModifier();
 427  0
     }
 428  
 
 429  
     public void outAUpdaterule(AUpdaterule node) {
 430  0
         ChoreographyRule rule = null;
 431  0
         Stack compoundStack = container.getStack(CompoundFact[].class);
 432  0
         CompoundFact[] facts = (CompoundFact[]) compoundStack.pop();
 433  0
         if (!facts[1].isEmpty() && node.getModifier() instanceof AUpdateModifier == false) {
 434  0
             ParserException ex = new ParserException(
 435  
                     "Updates in add|delete(...) rules not allowed!", null);
 436  0
             if (modifier instanceof AAddModifier) {
 437  0
                 ex.setErrorLine(((AAddModifier) modifier).getTAdd().getLine());
 438  0
                 ex.setErrorPos(((AAddModifier) modifier).getTAdd().getPos());
 439  
             }
 440  0
             else if (modifier instanceof ADeleteModifier) {
 441  0
                 ex.setErrorLine(((ADeleteModifier) modifier).getTDelete().getLine());
 442  0
                 ex.setErrorPos(((ADeleteModifier) modifier).getTDelete().getPos());
 443  
             }
 444  0
             throw new WrappedParsingException(ex);
 445  
         }
 446  0
         if (node.getModifier() instanceof AAddModifier) {
 447  0
             rule = factory.updateRules.createAdd(facts[0]);
 448  
         }
 449  0
         else if (node.getModifier() instanceof ADeleteModifier) {
 450  0
             rule = factory.updateRules.createDelete(facts[0]);
 451  
         }
 452  
         else { // AUpdatedModifier
 453  
             try {
 454  0
                 rule = factory.updateRules.createUpdate(facts[0], facts[1]);
 455  
             }
 456  0
             catch (InvalidModelException ex) {
 457  0
                 throw new WrappedInvalidModelException(ex);
 458  0
             }
 459  
         }
 460  
 
 461  0
         if (ruleStack.isEmpty()) { // no parent = add it in the Rules
 462  0
             ((ChoreographyRules) ruleListStack.peek()).addRule(rule);
 463  
         }
 464  
         else { // if parent = add it as a sub-rule to the parent
 465  0
             if (ruleStack.peek() instanceof ChoreographyTransitionRule)
 466  0
                 ((ChoreographyTransitionRule) ruleStack.peek()).addRule(rule);
 467  
             else
 468  0
                 ((OrchestrationTransitionRule) ruleStack.peek()).addRule(rule);
 469  
         }
 470  
 
 471  
         // sanity check that all molecule buffers are empty
 472  0
         assert newMembMolecules.size() + oldMembMolecules.size() + newAttrMolecules.size()
 473  
                 + oldAttrMolecules.size() + newRelationParams.size() + oldRelationParams.size() == 0;
 474  0
     }
 475  
 
 476  
     public void outAFactPreferredFact(AFactPreferredFact node) {
 477  0
         processMoleculeFact(node.getFactUpdate(), true);
 478  0
     }
 479  
 
 480  
     public void outAFactNonpreferredFact(AFactNonpreferredFact node) {
 481  0
         processMoleculeFact(node.getFactUpdate(), true);
 482  0
     }
 483  
 
 484  
     public void outAFactMoleculeFact(AFactMoleculeFact node) {
 485  0
         processMoleculeFact(null, false);
 486  0
     }
 487  
     
 488  
     public void inAFactRelationFact(AFactRelationFact node) {
 489  0
         node.getId().apply(container.getNodeHandler(PId.class));
 490  0
         relationId = (IRI) container.popFromStack(Identifier.class, IRI.class);
 491  0
     }
 492  
     
 493  
     private Identifier relationId;
 494  
     
 495  
     public void outAFactRelationFact(AFactRelationFact node) {
 496  0
         CompoundFact[] facts = new CompoundFact[2];
 497  0
         facts[0] = factory.facts.createRelationFact(leFactory.createAtom(relationId, 
 498  
                 new LinkedList<Term>(newRelationParams)));
 499  0
         facts[1] = factory.facts.createRelationFact(leFactory.createAtom(relationId,
 500  
                 new LinkedList<Term>(oldRelationParams)));
 501  0
         container.getStack(CompoundFact[].class).push(facts);
 502  0
         newRelationParams.clear();
 503  0
         oldRelationParams.clear();
 504  0
     }
 505  
 
 506  
     private void processMoleculeFact(PFactUpdate node, boolean hasMembershipMolecules) {
 507  0
         if (hasMembershipMolecules) {
 508  0
             processMembershipMolecules(node);
 509  
         }
 510  0
         container.getStack(Term.class).pop(); // remove the instanceId
 511  0
         CompoundFact[] facts = new MoleculeFact[2];
 512  0
         facts[0] = factory.facts.createMoleculeFact(new LinkedHashSet<MembershipMolecule>(
 513  
                 newMembMolecules), new LinkedHashSet<AttributeValueMolecule>(newAttrMolecules));
 514  0
         facts[1] = factory.facts.createMoleculeFact(new LinkedHashSet<MembershipMolecule>(
 515  
                 oldMembMolecules), new LinkedHashSet<AttributeValueMolecule>(oldAttrMolecules));
 516  0
         container.getStack(CompoundFact[].class).push(facts);
 517  0
         newAttrMolecules.clear();
 518  0
         oldAttrMolecules.clear();
 519  0
         if (hasMembershipMolecules) {
 520  0
             newMembMolecules.clear();
 521  0
             oldMembMolecules.clear();
 522  
         }
 523  0
     }
 524  
 
 525  
     public void outASingleTermUpdate(ASingleTermUpdate node) {
 526  
         // if update rule add all not modified term as old
 527  
         // e.g.  update(@a x1, x2 => y2) == update(@a null => x1, x2 => y2)
 528  0
         Term term = (Term) container.getStack(Term.class).pop();
 529  0
         newRelationParams.add(term);
 530  0
         if (modifier instanceof AUpdateModifier) {
 531  0
             oldRelationParams.add(term);
 532  
         }
 533  0
     }
 534  
 
 535  
     public void outAMoveTermUpdate(AMoveTermUpdate node) {
 536  0
         newRelationParams.add((Term) container.getStack(Term.class).pop());
 537  0
         oldRelationParams.add((Term) container.getStack(Term.class).pop());
 538  0
     }
 539  
 
 540  
     // the new values of RelationFact
 541  0
     private List<Term> newRelationParams = new LinkedList<Term>();
 542  
 
 543  
     // the new parameter values
 544  0
     private List<Term> oldRelationParams = new LinkedList<Term>();
 545  
 
 546  
     // the new values of MembershipMolecule 
 547  0
     private Set<MembershipMolecule> newMembMolecules = new LinkedHashSet<MembershipMolecule>();
 548  
 
 549  
     //the values of MembershipMolecule to be replaced "instance memberOf a => replacedValue"
 550  0
     private Set<MembershipMolecule> oldMembMolecules = new LinkedHashSet<MembershipMolecule>();
 551  
 
 552  
     private void processMembershipMolecules(PFactUpdate node) {
 553  0
         Term[] membAferMoveTo = new Term[0];
 554  0
         if (node != null) {
 555  0
             membAferMoveTo = (Term[]) container.getStack(Term[].class).pop();
 556  
         }
 557  0
         Term[] membBeforeMoveTo = (Term[]) container.getStack(Term[].class).pop();
 558  0
         Term instanceId = (Term) container.getStack(Term.class).peek();
 559  
 
 560  0
         for (int i = 0; i < membBeforeMoveTo.length; i++) {
 561  0
             if (membAferMoveTo.length == 0)
 562  0
                 newMembMolecules.add(leFactory.createMemberShipMolecule(instanceId,
 563  
                         membBeforeMoveTo[i]));
 564  
             else
 565  0
                 oldMembMolecules.add(leFactory.createMemberShipMolecule(instanceId,
 566  
                         membBeforeMoveTo[i]));
 567  
         }
 568  0
         for (int i = 0; i < membAferMoveTo.length; i++) {
 569  0
             newMembMolecules.add(leFactory
 570  
                     .createMemberShipMolecule(instanceId, membAferMoveTo[i]));
 571  
         }
 572  0
     }
 573  
 
 574  
     // the new values of AttributeValueMolecule  
 575  0
     private Set<AttributeValueMolecule> newAttrMolecules = new LinkedHashSet<AttributeValueMolecule>();
 576  
 
 577  
     // the values of AttributeValueMolecule to be replaced "instance[attr hasValue a => replacedValue]"
 578  0
     private Set<AttributeValueMolecule> oldAttrMolecules = new LinkedHashSet<AttributeValueMolecule>();
 579  
 
 580  
     public void outAAttrFactList(AAttrFactList node) {
 581  0
         processAttributeValueMolecule(node.getFactUpdate());
 582  0
     }
 583  
 
 584  
     public void outAAttrRelationAttrFactList(AAttrRelationAttrFactList node) {
 585  0
         processAttributeValueMolecule(node.getFactUpdate());
 586  0
     }
 587  
 
 588  
     private void processAttributeValueMolecule(PFactUpdate node) {
 589  0
         Stack terms = container.getStack(Term.class);
 590  0
         Stack termList = container.getStack(Term[].class);
 591  
 
 592  0
         Term[] valAfterMoveTo = new Term[0];
 593  0
         if (node != null) {
 594  0
             valAfterMoveTo = (Term[]) termList.pop();
 595  
         }
 596  0
         Term attId = (Term) terms.pop();
 597  0
         Term instanceId = (Term) terms.peek();
 598  0
         Term[] valBeforeMoveTo = (Term[]) termList.pop();
 599  0
         for (int i = 0; i < valBeforeMoveTo.length; i++) {
 600  0
             if (valAfterMoveTo.length == 0)
 601  0
                 newAttrMolecules.add(leFactory.createAttributeValue(instanceId, attId,
 602  
                         valBeforeMoveTo[i]));
 603  
             else
 604  0
                 oldAttrMolecules.add(leFactory.createAttributeValue(instanceId, attId,
 605  
                         valBeforeMoveTo[i]));
 606  
         }
 607  0
         for (int i = 0; i < valAfterMoveTo.length; i++) {
 608  0
             newAttrMolecules.add(leFactory.createAttributeValue(instanceId, attId,
 609  
                     valAfterMoveTo[i]));
 610  
         }
 611  0
     }
 612  
     
 613  
     // process ppMediator
 614  
     public void inAPpmediator(APpmediator node) {
 615  0
         TopEntityAnalysis.isValidTopEntityIdentifier(node.getId(),node.getTPpmediator());
 616  0
         node.getId().apply(container.getNodeHandler(PId.class));
 617  0
         IRI iri = (IRI) container.popFromStack(Identifier.class, IRI.class);
 618  0
         Mediator mediator = oFactory.updateRules.createPpMediator(iri);
 619  
 
 620  0
         container.getStack(Entity.class).push(mediator);
 621  0
         container.getStack(TopEntity.class).push(mediator);
 622  
 
 623  0
         TopEntityAnalysis.addNamespaceAndVariant(mediator, container.getStack(Namespace.class),
 624  
                 container.getStack(AWsmlvariant.class));
 625  0
     }
 626  
     
 627  
     public void outAPpmediator(APpmediator node) {
 628  0
         container.popFromStack(Entity.class, PpMediator.class);
 629  0
     }
 630  
 }
 631  
 
 632  
 /*
 633  
  * $Log$
 634  
  * Revision 1.9  2006/11/17 13:11:14  vassil_momtchev
 635  
  * parsing of send and receieve types added
 636  
  *
 637  
  * Revision 1.8  2006/11/17 10:09:53  vassil_momtchev
 638  
  * fixed bug in orchestration parsing. the rules container was not set properly;
 639  
  *
 640  
  * Revision 1.7  2006/11/08 15:26:53  vassil_momtchev
 641  
  * support of ppMediator and orchestration perform rules added
 642  
  *
 643  
  * Revision 1.6  2006/10/24 14:11:47  vassil_momtchev
 644  
  * choreography/orchestration rules refactored. different types where appropriate now supported
 645  
  *
 646  
  * Revision 1.5  2006/04/17 09:50:09  vassil_momtchev
 647  
  * raise parser exception if non restricted LE used in condition
 648  
  *
 649  
  * Revision 1.4  2006/04/17 08:07:20  vassil_momtchev
 650  
  * bug fixed in update of RelationFact expression;
 651  
  *
 652  
  * Revision 1.3  2006/02/10 15:35:34  vassil_momtchev
 653  
  * new grammar implemented
 654  
  *
 655  
  * Revision 1.2  2006/01/31 10:33:14  vassil_momtchev
 656  
  * all facts now are parsed according the API. BogusFact type removed
 657  
  *
 658  
  * Revision 1.1  2005/12/21 11:35:56  vassil_momtchev
 659  
  * analyzer of rules created
 660  
  *
 661  
  */