KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > xpdl > PackageValidator


1 package org.enhydra.shark.xpdl;
2
3 import java.io.ByteArrayOutputStream JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Collection JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.HashSet JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.Map JavaDoc;
11 import java.util.Set JavaDoc;
12
13 import javax.xml.parsers.DocumentBuilder JavaDoc;
14 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
15 import javax.xml.transform.Transformer JavaDoc;
16 import javax.xml.transform.TransformerFactory JavaDoc;
17 import javax.xml.transform.dom.DOMSource JavaDoc;
18 import javax.xml.transform.stream.StreamResult JavaDoc;
19
20 import org.enhydra.shark.utilities.SequencedHashMap;
21 import org.enhydra.shark.xpdl.elements.Activities;
22 import org.enhydra.shark.xpdl.elements.Activity;
23 import org.enhydra.shark.xpdl.elements.ActivitySet;
24 import org.enhydra.shark.xpdl.elements.ActualParameters;
25 import org.enhydra.shark.xpdl.elements.Application;
26 import org.enhydra.shark.xpdl.elements.ApplicationTypes;
27 import org.enhydra.shark.xpdl.elements.BlockActivity;
28 import org.enhydra.shark.xpdl.elements.DataField;
29 import org.enhydra.shark.xpdl.elements.DataType;
30 import org.enhydra.shark.xpdl.elements.Deadline;
31 import org.enhydra.shark.xpdl.elements.DeclaredType;
32 import org.enhydra.shark.xpdl.elements.FormalParameter;
33 import org.enhydra.shark.xpdl.elements.FormalParameters;
34 import org.enhydra.shark.xpdl.elements.Join;
35 import org.enhydra.shark.xpdl.elements.Package;
36 import org.enhydra.shark.xpdl.elements.PackageHeader;
37 import org.enhydra.shark.xpdl.elements.Participant;
38 import org.enhydra.shark.xpdl.elements.RedefinableHeader;
39 import org.enhydra.shark.xpdl.elements.Responsible;
40 import org.enhydra.shark.xpdl.elements.Responsibles;
41 import org.enhydra.shark.xpdl.elements.Split;
42 import org.enhydra.shark.xpdl.elements.SubFlow;
43 import org.enhydra.shark.xpdl.elements.Tool;
44 import org.enhydra.shark.xpdl.elements.Tools;
45 import org.enhydra.shark.xpdl.elements.Transition;
46 import org.enhydra.shark.xpdl.elements.TransitionRef;
47 import org.enhydra.shark.xpdl.elements.TransitionRefs;
48 import org.enhydra.shark.xpdl.elements.Transitions;
49 import org.enhydra.shark.xpdl.elements.TypeDeclaration;
50 import org.enhydra.shark.xpdl.elements.WorkflowProcess;
51 import org.w3c.dom.Document JavaDoc;
52
53 /**
54  * Represents coresponding element from XPDL schema.
55  *
56  * @author Sasa Bojanic
57  */

58 public class PackageValidator {
59
60    protected static final String JavaDoc CURRENT_XPDL_VERSION="1.0";
61
62    protected XMLInterface xmlInterface;
63    protected Package JavaDoc pkg;
64    protected boolean getExistingSchemaValidationErrors;
65    protected boolean checkExternalPackages;
66    protected boolean allowUndefinedStart;
67    protected boolean allowUndefinedEnd;
68
69    protected Map JavaDoc xpdlSchemaValidationErrors=new HashMap JavaDoc();
70    protected Map JavaDoc graphsConnectionErrors=new HashMap JavaDoc();
71    protected Map JavaDoc basicGraphConnectionErrors=new HashMap JavaDoc();
72    protected Map JavaDoc graphsConformanceErrors=new HashMap JavaDoc();
73    protected Map JavaDoc basicGraphsConformanceErrors=new HashMap JavaDoc();
74    protected Map JavaDoc logicErrors=new HashMap JavaDoc();
75    protected Map JavaDoc basicLogicErrors=new HashMap JavaDoc();
76
77    public Map JavaDoc getXPDLSchemaValidationErrors () {
78       return xpdlSchemaValidationErrors;
79    }
80
81    public Map JavaDoc getGraphsConnectionErrors (XMLComplexElement pkgOrWpOrAs) {
82       return (Map JavaDoc)graphsConnectionErrors.get(pkgOrWpOrAs);
83    }
84
85    public String JavaDoc getBasicGraphConnectionError (XMLComplexElement pkgOrWpOrAs) {
86       return (String JavaDoc)basicGraphConnectionErrors.get(pkgOrWpOrAs);
87    }
88
89    public Map JavaDoc getGraphConformanceErrors (XMLComplexElement pkgOrWpOrAs) {
90       return (Map JavaDoc)graphsConformanceErrors.get(pkgOrWpOrAs);
91    }
92
93    public List JavaDoc getBasicGraphConformanceErrors (XMLComplexElement pkgOrWpOrAs) {
94       return (List JavaDoc)basicGraphsConformanceErrors.get(pkgOrWpOrAs);
95    }
96
97    public Map JavaDoc getLogicErrors (XMLComplexElement pkgOrWpOrAs) {
98       return (Map JavaDoc)logicErrors.get(pkgOrWpOrAs);
99    }
100
101    public String JavaDoc getBasicLogicError (XMLComplexElement pkgOrWpOrAs) {
102       return (String JavaDoc)basicLogicErrors.get(pkgOrWpOrAs);
103    }
104
105    public PackageValidator(XMLInterface xmlInterface,Package JavaDoc pkg, boolean getExistingSchemaValidationErrors,
106                            boolean checkExternalPackages,boolean allowUndefinedStart,
107                            boolean allowUndefinedEnd) {
108       this.xmlInterface=xmlInterface;
109       this.pkg=pkg;
110       this.getExistingSchemaValidationErrors=getExistingSchemaValidationErrors;
111       this.checkExternalPackages=checkExternalPackages;
112       this.allowUndefinedStart=allowUndefinedStart;
113       this.allowUndefinedEnd=allowUndefinedEnd;
114    }
115
116    public boolean validateAll (boolean fullCheck) {
117       try {
118          boolean isValid=validateAgainstXPDLSchema();
119          if (fullCheck || isValid) {
120             isValid=checkPackage(fullCheck) && isValid;
121          }
122          if (fullCheck || isValid) {
123             isValid=checkGraphConnections(fullCheck) && isValid;
124          }
125          if (fullCheck || isValid) {
126             isValid=checkGraphConformance(fullCheck) && isValid;
127          }
128
129          return isValid;
130       } catch (Exception JavaDoc ex) {
131          ex.printStackTrace();
132          return false;
133       }
134    }
135
136    //********************* validation against XPDL schema *********************
137
public boolean validateAgainstXPDLSchema () {
138       if (getExistingSchemaValidationErrors) {
139          xpdlSchemaValidationErrors=xmlInterface.getParsingErrorMessages();
140          if (xpdlSchemaValidationErrors.size()>0) {
141             return false;
142          } else {
143             return true;
144          }
145       }
146       try {
147          Document JavaDoc document = null;
148
149          DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
150          DocumentBuilder JavaDoc dbuilder = dbf.newDocumentBuilder();
151          document = dbuilder.newDocument();
152          ByteArrayOutputStream JavaDoc baos=new ByteArrayOutputStream JavaDoc();
153
154          // The extended attributes for all package elements must
155
// be updated if current package is not externally
156
// referenced package.
157
//Save.updateExtendedAttributesForWorkflowProcesses();
158

159          // Here we get all document elements
160
XMLUtil.toXML(document, pkg);
161
162          // Use a Transformer for output
163
TransformerFactory JavaDoc tFactory =
164             TransformerFactory.newInstance();
165          Transformer JavaDoc transformer = tFactory.newTransformer();
166          transformer.setOutputProperty("indent","yes");
167          transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","4");
168          transformer.setOutputProperty("encoding","UTF-8");
169          DOMSource JavaDoc source = new DOMSource JavaDoc(document);
170          StreamResult JavaDoc result = new StreamResult JavaDoc(baos);
171          transformer.transform(source,result);
172
173          xmlInterface.clearParserErrorMessages();
174          xmlInterface.parseDocument(baos.toString("UTF8"),false);
175          baos.close();
176          xpdlSchemaValidationErrors=xmlInterface.getParsingErrorMessages();
177          if (xpdlSchemaValidationErrors.size()>0) {
178             return false;
179          }
180       } catch (Exception JavaDoc ex) {
181          return false;
182       }
183       return true;
184
185    }
186    //********************* Logic checking **************************************
187
public boolean checkPackage (boolean fullCheck) {
188       Map JavaDoc les=new HashMap JavaDoc();
189       logicErrors.put(pkg,les);
190       basicLogicErrors.remove(pkg);
191
192       boolean isPackageValid=true;
193       boolean invalidId=false;
194       if (!isIdValid(pkg.getId())) {
195          isPackageValid=false;
196          invalidId=true;
197          les.put(pkg,XMLUtil.getLanguageDependentString("ErrorPackageIdIsNotValid"));
198       }
199       
200       if (fullCheck || isPackageValid) {
201          checkPackageHeader(fullCheck);
202       }
203       if (fullCheck || isPackageValid) {
204          isPackageValid=checkRedefinableHeader(pkg,fullCheck) && isPackageValid;;
205       }
206       if (fullCheck || isPackageValid) {
207          isPackageValid=checkConformanceClass(fullCheck) && isPackageValid;;
208       }
209       if (fullCheck || isPackageValid) {
210          isPackageValid=checkScript(fullCheck) && isPackageValid;;
211       }
212       if ((fullCheck || isPackageValid) && checkExternalPackages) {
213          isPackageValid=checkExternalPackages(fullCheck) && isPackageValid;;
214       }
215       if (fullCheck || isPackageValid) {
216          isPackageValid=checkCollection("TypeDeclarations",pkg,fullCheck) && isPackageValid;;
217       }
218       if (fullCheck || isPackageValid) {
219          isPackageValid=checkCollection("Participants",pkg,fullCheck) && isPackageValid;;
220       }
221       if (fullCheck || isPackageValid) {
222          isPackageValid=checkCollection("Applications",pkg,fullCheck) && isPackageValid;;
223       }
224       if (fullCheck || isPackageValid) {
225          isPackageValid=checkCollection("DataFields",pkg,fullCheck) && isPackageValid;;
226       }
227       boolean areProcessesValid=true;
228       if (fullCheck || isPackageValid) {
229          areProcessesValid=checkCollection("WorkflowProcesses",pkg,fullCheck);
230          isPackageValid=areProcessesValid && isPackageValid;;
231       }
232       if (!isPackageValid) {
233          if (invalidId) {
234             basicLogicErrors.put(pkg,XMLUtil.getLanguageDependentString("ErrorPackageIdIsNotValid"));
235          } else if (!areProcessesValid){
236             basicLogicErrors.put(pkg,XMLUtil.getLanguageDependentString("ErrorOneOrMoreProcessesHaveLogicErrors"));
237          } else {
238             basicLogicErrors.put(pkg,les.values().toArray()[0]);
239          }
240       }
241       return isPackageValid;
242    }
243
244    public boolean checkPackageHeader (boolean fullCheck) {
245       PackageHeader phdr=pkg.getPackageHeader();
246       String JavaDoc xpdlv="XPDLVersion";
247       if (!phdr.getXPDLVersion().equals(CURRENT_XPDL_VERSION)) {
248          Map JavaDoc les=getLogicErrors(pkg);
249          les.put(phdr,XMLUtil.getLanguageDependentString("ErrorInvalidXPDLVersion"));
250          return false;
251       } else {
252          return true;
253       }
254    }
255
256    public boolean checkRedefinableHeader (XMLComplexElement pkgOrWp,boolean fullCheck) {
257       boolean isValid=true;
258       RedefinableHeader rh=(RedefinableHeader)pkgOrWp.get("RedefinableHeader"); //Harald Meister: use argument instead of package
259
Iterator JavaDoc rspns=((Responsibles)rh.get("Responsibles")).toElements().iterator();
260       while (rspns.hasNext()) {
261          Responsible r=(Responsible)rspns.next();
262          String JavaDoc rv=r.toValue();
263          Participant p;
264          if (pkgOrWp instanceof Package JavaDoc) {
265             p=XMLUtil.getPackage(r).getParticipant(rv);
266          } else {
267             p=XMLUtil.getWorkflowProcess(r).getParticipant(rv);
268             if (p==null) {
269                p=XMLUtil.getPackage(r).getParticipant(rv);
270             }
271          }
272          if (p==null) {
273             List JavaDoc l=XMLUtil.getAllExternalPackageIds(xmlInterface, pkg);
274             Iterator JavaDoc itp=l.iterator();
275             while (itp.hasNext()) {
276                Package JavaDoc pkg=xmlInterface.getPackageById((String JavaDoc)itp.next());
277                if (pkg!=null) {
278                   p=pkg.getParticipant(rv);
279                   if (p!=null) {
280                      break;
281                   }
282                }
283             }
284          }
285          if (p==null) {
286             isValid=false;
287             Map JavaDoc les=getLogicErrors(pkgOrWp);
288             les.put(rh,XMLUtil.getLanguageDependentString("ErrorOneOrMoreResponsibleParticipantsDoNotExist"));
289             break;
290          }
291       }
292       return isValid;
293    }
294
295    public boolean checkConformanceClass (boolean fullCheck) {
296       return true;
297    }
298
299    public boolean checkScript(boolean fullCheck) {
300       //
301
return true;
302    }
303
304    public boolean checkExternalPackages(boolean fullCheck) {
305       boolean isValid=true;
306       Map JavaDoc les=getLogicErrors(pkg);
307       Iterator JavaDoc it=pkg.getExternalPackageIds().iterator();
308       while (it.hasNext() && (fullCheck || isValid)) {
309          Package JavaDoc p=(Package JavaDoc)xmlInterface.getPackageById((String JavaDoc)it.next());
310          PackageValidator pv=new PackageValidator(xmlInterface,p,
311                                                   getExistingSchemaValidationErrors,false,allowUndefinedStart,
312                                                   allowUndefinedEnd);
313          if (!pv.validateAll(false)) {
314             isValid=false;
315             if (les!=null) {
316                les.put(p,XMLUtil.getLanguageDependentString("ErrorInvalidExternalPackage"));
317             }
318          }
319       }
320       return isValid;
321    }
322
323    public boolean checkCollection (String JavaDoc colName,XMLComplexElement cOwner,boolean fullCheck) {
324       boolean isValid=true;
325       Iterator JavaDoc it=((XMLCollection)cOwner.get(colName)).
326          toElements().iterator();
327       while (it.hasNext() && (fullCheck || isValid)) {
328          isValid=checkCollectionElement((XMLCollectionElement)it.next(),fullCheck) && isValid;
329       }
330       return isValid;
331    }
332
333    public boolean checkCollectionElement (XMLCollectionElement ce,boolean fullCheck) {
334       boolean isValid=true;
335       if (!isIdValid(ce.getId())) {
336          isValid=false;
337          XMLElement firstOwner=ce.getParent().getParent();
338          Map JavaDoc les;
339          if (firstOwner instanceof ApplicationTypes) {
340             les=getLogicErrors((XMLComplexElement)((ApplicationTypes)firstOwner).getParent().getParent().getParent());
341          } else {
342             les=getLogicErrors((XMLComplexElement)firstOwner);
343          }
344          les.put(ce,XMLUtil.getLanguageDependentString("ErrorIdIsNotValid"));
345       }
346       if (fullCheck || isValid) {
347          if (!isUniqueId((XMLCollection)ce.getParent(),ce.getId())) {
348             isValid=false;
349             XMLElement firstOwner=ce.getParent().getParent();
350             Map JavaDoc les;
351             if (firstOwner instanceof ApplicationTypes) {
352                les=getLogicErrors((XMLComplexElement)((ApplicationTypes)firstOwner).getParent().getParent());
353             } else {
354                les=getLogicErrors((XMLComplexElement)firstOwner);
355             }
356             String JavaDoc msg=(String JavaDoc)les.get(ce);
357             msg=prepareMessageString(msg);
358             msg=msg+XMLUtil.getLanguageDependentString("ErrorIdIsNotUnique");
359             les.put(ce,msg);
360          }
361       }
362       if (fullCheck || isValid) {
363          if (ce instanceof TypeDeclaration) {
364             isValid=checkTypeDeclaration((TypeDeclaration)ce,fullCheck) && isValid;
365          } else if (ce instanceof Participant) {
366             isValid=checkParticipant((Participant)ce,fullCheck) && isValid;
367          } else if (ce instanceof Application) {
368             isValid=checkApplication((Application)ce,fullCheck) && isValid;
369          } else if (ce instanceof DataField) {
370             isValid=checkDataField((DataField)ce,fullCheck) && isValid;
371          } else if (ce instanceof FormalParameter) {
372             isValid=checkFormalParameter((FormalParameter)ce,fullCheck) && isValid;
373          } else if (ce instanceof WorkflowProcess) {
374             isValid=checkWorkflowProcess((WorkflowProcess)ce,fullCheck) && isValid;
375          } else if (ce instanceof ActivitySet) {
376             isValid=checkActivitySet((ActivitySet)ce,fullCheck) && isValid;
377          } else if (ce instanceof Activity) {
378             isValid=checkActivity((Activity)ce,fullCheck) && isValid;
379          } else if (ce instanceof Transition) {
380             isValid=checkTransition((Transition)ce,fullCheck) && isValid;
381          }
382       }
383       return isValid;
384    }
385
386    public boolean checkTypeDeclaration(TypeDeclaration td,boolean fullCheck) {
387       //
388
return true;
389    }
390
391    public boolean checkParticipant (Participant p,boolean fullCheck) {
392       //
393
return true;
394    }
395
396    public boolean checkApplication (Application app,boolean fullCheck) {
397       boolean isValid=true;
398       if (app.getApplicationTypes().getChoosen() instanceof FormalParameters) {
399          FormalParameters fps=app.getApplicationTypes().getFormalParameters();
400          Iterator JavaDoc it=fps.toElements().iterator();
401          while (it.hasNext() && (fullCheck || isValid)) {
402             isValid=checkCollectionElement((XMLCollectionElement)it.next(),fullCheck) && isValid;
403          }
404       }
405       return isValid;
406    }
407
408    public boolean checkDataField (DataField df,boolean fullCheck) {
409       //
410
return checkDataType(df,fullCheck);
411    }
412
413    public boolean checkFormalParameter (FormalParameter fp,boolean fullCheck) {
414       //
415
return checkDataType(fp,fullCheck);
416    }
417
418    public boolean checkDataType (XMLCollectionElement dfOrFp,boolean fullCheck) {
419       boolean isValid=true;
420
421       DataType xpdlType=(DataType)dfOrFp.get("DataType");
422       XMLElement type=xpdlType.getDataTypes().getChoosen();
423       if (type instanceof DeclaredType) {
424          TypeDeclaration td=pkg.getTypeDeclaration(((DeclaredType)type).getId());
425          if (td==null) {
426             isValid=false;
427             XMLElement firstOwner=dfOrFp.getParent().getParent();
428             Map JavaDoc les;
429             if (dfOrFp instanceof DataField) {
430                les=getLogicErrors((XMLComplexElement)firstOwner);
431             } else {
432                if (firstOwner instanceof ApplicationTypes) {
433                   les=getLogicErrors((XMLComplexElement)((ApplicationTypes)firstOwner).getParent().getParent());
434                } else {
435                   les=getLogicErrors((XMLComplexElement)firstOwner);
436                }
437             }
438             String JavaDoc msg=(String JavaDoc)les.get(dfOrFp);
439             msg=prepareMessageString(msg);
440             msg=msg+XMLUtil.getLanguageDependentString("ErrorNonExistingDeclaredTypeReference");
441             les.put(dfOrFp,msg);
442          }
443       }
444       return isValid;
445    }
446
447    public boolean checkWorkflowProcess (WorkflowProcess wp,boolean fullCheck) {
448       Map JavaDoc les=new HashMap JavaDoc();
449       logicErrors.put(wp,les);
450       basicLogicErrors.remove(wp);
451
452       boolean notDefined=false;
453       boolean isValid=checkProcessHeader(wp,fullCheck);
454       if (fullCheck || isValid) {
455          isValid=checkRedefinableHeader(wp,fullCheck) && isValid;
456       }
457       if (fullCheck || isValid) {
458          isValid=checkCollection("FormalParameters",wp,fullCheck) && isValid;
459       }
460       if (fullCheck || isValid) {
461          isValid=checkCollection("DataFields",wp,fullCheck) && isValid;
462       }
463       if (fullCheck || isValid) {
464          isValid=checkCollection("Participants",wp,fullCheck) && isValid;
465       }
466       if (fullCheck || isValid) {
467          isValid=checkCollection("Applications",wp,fullCheck) && isValid;
468       }
469       if (fullCheck || isValid) {
470          isValid=checkCollection("ActivitySets",wp,fullCheck) && isValid;
471       }
472       if (fullCheck || isValid) {
473          if (wp.getActivities().toElements().size()==0) {
474             isValid=false;
475             notDefined=true;
476             les.put(wp,XMLUtil.getLanguageDependentString("ErrorProcessIsNotDefined"));
477          } else {
478             isValid=checkCollection("Activities",wp,fullCheck) && isValid;
479          }
480       }
481       if (fullCheck || isValid) {
482          isValid=checkCollection("Transitions",wp,fullCheck) && isValid;
483       }
484       if (!isValid) {
485          basicLogicErrors.put(wp,les.values().toArray()[0]);
486          Map JavaDoc pkgles=getLogicErrors(pkg);
487          if (pkgles!=null) {
488             if (notDefined) {
489                pkgles.put(wp,XMLUtil.getLanguageDependentString("ErrorProcessIsNotDefined"));
490             } else {
491                pkgles.put(wp,XMLUtil.getLanguageDependentString("ErrorProcessContainsOneOrMoreLogicErrors"));
492             }
493          }
494       }
495       return isValid;
496    }
497
498    public boolean checkProcessHeader(WorkflowProcess wp,boolean fullCheck) {
499       //
500
return true;
501    }
502
503    public boolean checkActivitySet (ActivitySet as,boolean fullCheck) {
504       Map JavaDoc les=new HashMap JavaDoc();
505       logicErrors.put(as,les);
506       basicLogicErrors.remove(as);
507       boolean isValid=true;
508       boolean notDefined=false;
509       if (as.getActivities().toElements().size()==0) {
510          isValid=false;
511          notDefined=true;
512          les.put(as,XMLUtil.getLanguageDependentString("ErrorBlockActivityIsNotDefined"));
513       } else {
514          isValid=checkCollection("Activities",as,fullCheck);
515       }
516       if (fullCheck || isValid) {
517          isValid=checkCollection("Transitions",as,fullCheck) && isValid;
518       }
519       if (!isValid) {
520          basicLogicErrors.put(as,getLogicErrors(as).values().toArray()[0]);
521          Map JavaDoc wples=getLogicErrors(XMLUtil.getWorkflowProcess(as));
522          Activity blockActivity=findBlockActivity(as);
523          if (!(wples==null || blockActivity==null)) {
524             if (notDefined) {
525                wples.put(blockActivity,XMLUtil.getLanguageDependentString("ErrorBlockActivityIsNotDefined"));
526             } else {
527                wples.put(blockActivity,XMLUtil.getLanguageDependentString("ErrorInnerLogicError"));
528             }
529          } else if (wples!=null) {
530             wples.put(as,XMLUtil.getLanguageDependentString("ErrorBlockActivityIsNotDefined"));
531          }
532       }
533       return isValid;
534    }
535
536    public boolean checkActivity (Activity act,boolean fullCheck) {
537       // check performer
538
boolean isValid=checkActivityPerformer(act,fullCheck);
539
540       if (!(fullCheck || isValid)) {
541          return false;
542       }
543
544       // if this is a block activity
545
int actType=act.getActivityType();
546       if (actType==XPDLConstants.ACTIVITY_TYPE_BLOCK) {
547          isValid=checkActivityBlock(act,fullCheck) && isValid;
548       } else if (actType==XPDLConstants.ACTIVITY_TYPE_TOOL) {
549          isValid=checkActivityTools(act,fullCheck) && isValid;
550       } else if (actType==XPDLConstants.ACTIVITY_TYPE_SUBFLOW) {
551          isValid=checkActivitySubFlow(act,fullCheck) && isValid;
552       }
553
554       if (!(fullCheck || isValid)) {
555          return false;
556       }
557
558       Transitions trans=(Transitions)((XMLCollectionElement)act.getParent().getParent()).get("Transitions");
559       Set JavaDoc outTrans=XMLUtil.getOutgoingTransitions(act);
560       Set JavaDoc inTrans=XMLUtil.getIncomingTransitions(act);
561
562       // check deadlines
563
isValid=checkActivityDeadlines(act,fullCheck) && isValid;
564       if (!(fullCheck || isValid)) {
565          return false;
566       }
567
568       Map JavaDoc les=getLogicErrors((XMLComplexElement)act.getParent().getParent());
569       String JavaDoc msg=(String JavaDoc)les.get(act);
570       // Split type and no. of outgoing transitions
571
Split split=XMLUtil.getSplit(act);
572       if ((split==null || split.getType().length()==0) && outTrans.size()>1) {
573          isValid=false;
574          msg=prepareMessageString(msg);
575          msg=msg+XMLUtil.getLanguageDependentString("ErrorMultipleOutgoingTransitionsWithoutSplitTypeDefined");
576          les.put(act,msg);
577       }
578
579       if (!(fullCheck || isValid)) {
580          return false;
581       }
582
583       // TransitionRefs size must be the same as the one of outgoing transitions
584
if (split!=null) {
585          TransitionRefs tRfs=split.getTransitionRefs();
586          if ((tRfs.size()!=outTrans.size()) && outTrans.size()>1 && !split.getType().equals(XPDLConstants.JOIN_SPLIT_TYPE_AND)) {
587             isValid=false;
588             msg=prepareMessageString(msg);
589             msg=msg+XMLUtil.getLanguageDependentString("ErrorNumberOfActivitiesOutgoingTransitionsAndTransitionRefsIsNotSame");
590             les.put(act,msg);
591          }
592          if (!(fullCheck || isValid)) {
593             return false;
594          }
595          // TransitionRefs must refer to valid transitions.
596
Iterator JavaDoc tRefs=tRfs.toElements().iterator();
597          boolean invalidTref=false;
598          while (tRefs.hasNext()) {
599             String JavaDoc transitionId=((TransitionRef)tRefs.next()).getId();
600             Transition t=trans.getTransition(transitionId);
601             if (t==null || !outTrans.contains(t)) {
602                isValid=false;
603                invalidTref=true;
604             }
605          }
606
607          if (invalidTref) {
608             msg=prepareMessageString(msg);
609             msg=msg+XMLUtil.getLanguageDependentString("ErrorTransitionRefIsNotValid");
610             les.put(act,msg);
611          }
612    
613          if (!(fullCheck || isValid)) {
614             return false;
615          }
616       }
617       // Join type and no. of incoming transitions
618
Join join=XMLUtil.getJoin(act);
619       if ((join==null || join.getType().length()==0) && inTrans.size()>1) {
620          isValid=false;
621          msg=prepareMessageString(msg);
622          msg=msg+XMLUtil.getLanguageDependentString(
623             "ErrorMultipleIncomingTransitionsWithoutJoinTypeDefined");
624          les.put(act,msg);
625       }
626
627       if (!(fullCheck || isValid)) {
628          return false;
629       }
630
631       isValid=checkMultipleOtherwiseOrDefaultExceptionTransitions(act,fullCheck) && isValid;
632
633       return isValid;
634    }
635
636    public boolean checkActivityPerformer (Activity act,boolean fullCheck) {
637       boolean isValid=true;
638
639       // check performer
640
String JavaDoc performer=act.getPerformer().trim();
641       // if this is not an No or Tool activity, check peformer
642
int actType=act.getActivityType();
643       boolean hasToCheck=true;
644       if (actType!=XPDLConstants.ACTIVITY_TYPE_NO || actType!=XPDLConstants.ACTIVITY_TYPE_TOOL) {
645          hasToCheck=false;
646       }
647       if (hasToCheck && performer.length()>0) {
648          isValid=false;
649          Map JavaDoc les=getLogicErrors((XMLComplexElement)act.getParent().getParent());
650          String JavaDoc msg=(String JavaDoc)les.get(act);
651          msg=prepareMessageString(msg);
652          msg=msg+XMLUtil.getLanguageDependentString("ErrorActivityCannotHavePerformer");
653          les.put(act,msg);
654       }
655       return isValid;
656    }
657
658    public boolean checkActivityTools (Activity act,boolean fullCheck) {
659       boolean isValid=true;
660       boolean nonExistingToolReference=false;
661
662       Tools tools=act.getActivityTypes().getImplementation().getImplementationTypes().getTools();
663       Iterator JavaDoc it=tools.toElements().iterator();
664       while (it.hasNext() && (fullCheck || isValid)) {
665          Tool tool=(Tool)it.next();
666          String JavaDoc toolID=tool.getId();
667          if (toolID==null || toolID.equals("")) {
668             isValid=false;
669             nonExistingToolReference=true;
670          }
671          if (!(isValid || fullCheck)) break;
672
673          WorkflowProcess wp=XMLUtil.getWorkflowProcess(act);
674          Application app=null;
675          if (toolID!=null) {
676             app=wp.getApplication(toolID);
677             if (app==null) {
678                app=pkg.getApplication(toolID);
679             }
680             if (app==null) {
681                List JavaDoc l=XMLUtil.getAllExternalPackageIds(xmlInterface, pkg);
682                Iterator JavaDoc ita=l.iterator();
683                while (ita.hasNext()) {
684                   Package JavaDoc p=xmlInterface.getPackageById((String JavaDoc)ita.next());
685                   if (p!=null) {
686                      app=p.getApplication(toolID);
687                      if (app!=null) {
688                         break;
689                      }
690                   }
691                }
692             }
693          }
694          try {
695             isValid=checkParameterMappings(tool,app,fullCheck) && isValid;
696          } catch (Exception JavaDoc ex) {}
697       }
698
699       if (!isValid) {
700          Map JavaDoc les=getLogicErrors((XMLComplexElement)act.getParent().getParent());
701          String JavaDoc msg=(String JavaDoc)les.get(act);
702          msg=prepareMessageString(msg);
703          if (nonExistingToolReference) {
704             msg+=XMLUtil.getLanguageDependentString("ErrorNonExistingToolReference");
705          } else {
706             msg+=XMLUtil.getLanguageDependentString("ErrorToolsFormalAndActualParametersDoNotMatch");
707          }
708          les.put(act,msg);
709       }
710
711       return isValid;
712    }
713
714    public boolean checkActivitySubFlow (Activity act,boolean fullCheck) {
715       boolean isValid=true;
716       boolean nonExistingProcessReference=false;
717       boolean notAllowedProcessReference=false;
718       SubFlow s=act.getActivityTypes().getImplementation().getImplementationTypes().getSubFlow();
719       String JavaDoc subflowID=s.getId();
720       if (subflowID==null || subflowID.trim().equals("")) {
721          isValid=false;
722          nonExistingProcessReference=true;
723       }
724       
725       Package JavaDoc pkg=XMLUtil.getPackage(act);
726       WorkflowProcess wp=null;
727       if (isValid) {
728          wp=pkg.getWorkflowProcess(subflowID);
729          if (wp==null) {
730             List JavaDoc l=XMLUtil.getAllExternalPackageIds(xmlInterface, pkg);
731             Iterator JavaDoc it=l.iterator();
732             while (it.hasNext()) {
733                Package JavaDoc p=xmlInterface.getPackageById((String JavaDoc)it.next());
734                if (p!=null) {
735                   wp=p.getWorkflowProcess(subflowID);
736                   if (wp!=null) {
737                      break;
738                   }
739                }
740             }
741          }
742       }
743       if ((fullCheck || isValid) && wp!=null) {
744          try {
745             isValid=checkParameterMappings(s,wp,fullCheck) && isValid;
746          } catch (Exception JavaDoc ex) {}
747       }
748
749       if (!isValid) {
750          Map JavaDoc les=getLogicErrors((XMLComplexElement)act.getParent().getParent());
751          String JavaDoc msg=(String JavaDoc)les.get(act);
752          msg=prepareMessageString(msg);
753          if (nonExistingProcessReference) {
754             msg=msg+XMLUtil.getLanguageDependentString("ErrorNonExistingProcessReference");
755          } else if (nonExistingProcessReference) {
756             msg=msg+XMLUtil.getLanguageDependentString("ErrorNotAllowedProcessReference");
757          } else {
758             msg=msg+XMLUtil.getLanguageDependentString("ErrorSubFlowFormalAndActualParametersDoNotMatch");
759          }
760          les.put(act,msg);
761       }
762       return isValid;
763    }
764
765    public boolean checkActivityBlock (Activity act,boolean fullCheck) {
766       boolean isValid=true;
767       BlockActivity blk=act.getActivityTypes().getBlockActivity();
768       String JavaDoc blockId=blk.getBlockId();
769       // check if the activity set exists
770
ActivitySet as=XMLUtil.getWorkflowProcess(act).getActivitySet(blockId);
771       // check if there is activity set with the referenced id and if
772
// block activity is inside other block activity, and references it's owner
773
if (as==null || act.getParent().getParent().equals(as)) {
774          isValid=false;
775          Map JavaDoc les=getLogicErrors((XMLComplexElement)act.getParent().getParent());
776          String JavaDoc msg=(String JavaDoc)les.get(act);
777          msg=prepareMessageString(msg);
778          if (as==null) {
779             msg=msg+XMLUtil.getLanguageDependentString("ErrorNonExistingActivitySetReference");
780          } else {
781             msg=msg+XMLUtil.getLanguageDependentString("ErrorNotAllowedActivitySetReference");
782          }
783          les.put(act,msg);
784       }
785       return isValid;
786    }
787
788    public boolean checkActivityDeadlines (Activity act,boolean fullCheck) {
789       boolean isValid=true;
790
791       Collection JavaDoc deadlines=act.getDeadlines().toElements();
792       if (deadlines.size()==0) return isValid;
793
794       Iterator JavaDoc dls=deadlines.iterator();
795       int syncCount = 0;
796       while (dls.hasNext()) {
797          Deadline dl=(Deadline)dls.next();
798          // TODO: validate condition.
799
//XMLElement dc=dl.get("DeadlineCondition");
800
if (dl.getExecution().equals(XPDLConstants.EXECUTION_SYNCHR)) {
801             syncCount++;
802          }
803       }
804
805       Map JavaDoc les=getLogicErrors((XMLComplexElement)act.getParent().getParent());
806       String JavaDoc msg=(String JavaDoc)les.get(act);
807       if (syncCount>1) {
808          isValid=false;
809          msg=prepareMessageString(msg);
810          msg+=XMLUtil.getLanguageDependentString("ErrorActivityCanHaveOnlyOneSynchronousDeadline");
811          les.put(act,msg);
812       }
813       if (!(fullCheck || isValid)) {
814          return false;
815       }
816
817       /*Iterator it=excOutTrans.iterator();
818       while (it.hasNext()) {
819          Transition t=(Transition)it.next();
820          Condition c=(Condition)t.get("Condition");
821          String ct=((Condition)t.get("Condition")).get("Type").
822             toValue().toString();
823          if (ct.equals(Condition.CONDITION_TYPE_DEFAULTEXCEPTION)) {
824             deadlineExceptions.clear();
825             break;
826          } else if (ct.equals("EXCEPTION")) {
827             deadlineExceptions.remove(c.toString().trim());
828          }
829        }*/

830
831       if (XMLUtil.getExceptionalOutgoingTransitions(act).size()==0) {
832          isValid=false;
833          msg=prepareMessageString(msg);
834          msg+=XMLUtil.getLanguageDependentString(
835             "ErrorDeadlineExceptionIsNotHandledByAnyOutgoingTransitionWithExceptionOrDefaultExceptionConditionType");
836          les.put(act,msg);
837       }
838       return isValid;
839    }
840
841    public boolean checkMultipleOtherwiseOrDefaultExceptionTransitions (Activity act,boolean fullCheck) {
842       Set JavaDoc outTrans=XMLUtil.getOutgoingTransitions(act);
843       // Check outgoing transitions
844
// do not allow more then 1 transitions of type otherwise or default_exception
845
boolean foundOtherwise=false;
846       boolean foundMultipleOtherwise=false;
847       boolean foundDefaultException=false;
848       boolean foundMultipleDefaultException=false;
849       Iterator JavaDoc ts=outTrans.iterator();
850       while (ts.hasNext()) {
851          Transition t=(Transition)ts.next();
852          String JavaDoc ct=t.getCondition().getType();
853          if (ct.equals(XPDLConstants.CONDITION_TYPE_OTHERWISE)) {
854             if (foundOtherwise) {
855                foundMultipleOtherwise=true;
856                if (foundMultipleDefaultException || !fullCheck) break;
857             } else {
858                foundOtherwise=true;
859             }
860          } else if (ct.equals(XPDLConstants.CONDITION_TYPE_DEFAULTEXCEPTION)) {
861             if (foundDefaultException) {
862                foundMultipleDefaultException=true;
863                if (foundMultipleOtherwise || !fullCheck) break;
864             } else {
865                foundDefaultException=true;
866             }
867          }
868       }
869
870       if (foundMultipleOtherwise || foundMultipleDefaultException) {
871          Map JavaDoc les=getLogicErrors((XMLComplexElement)act.getParent().getParent());
872          String JavaDoc msg=(String JavaDoc)les.get(act);
873          msg=prepareMessageString(msg);
874          if (foundMultipleDefaultException && foundMultipleOtherwise) {
875             msg=msg+XMLUtil.getLanguageDependentString(
876                "ErrorMoreThenOneOTHERWISEAndDEFAULTEXCEPTIONTypeOutgoingTransition");
877          } else if (foundMultipleOtherwise) {
878             msg=msg+XMLUtil.getLanguageDependentString(
879                "ErrorMoreThenOneOTHERWISETypeOutgoingTransition");
880          } else if (foundMultipleDefaultException) {
881             msg=msg+XMLUtil.getLanguageDependentString(
882                "ErrorMoreThenOneDEFAULTEXCEPTIONTypeOutgoingTransition");
883          }
884          les.put(act,msg);
885          return false;
886       } else {
887          return true;
888       }
889    }
890
891    public boolean checkParameterMappings (XMLComplexElement toolOrSbflw,
892                                           XMLComplexElement appOrWp,boolean fullCheck) {
893       FormalParameters fps;
894       if (appOrWp instanceof WorkflowProcess) {
895          fps=((WorkflowProcess)appOrWp).getFormalParameters();
896       } else {
897          ApplicationTypes ats=((Application)appOrWp).getApplicationTypes();
898          if (ats.getChoosen() instanceof FormalParameters) {
899             fps=ats.getFormalParameters();
900             // do not check if application is externally defined
901
} else {
902             return true;
903          }
904       }
905       ActualParameters aps=(ActualParameters)toolOrSbflw.get("ActualParameters");
906       int pm=XMLUtil.checkParameterMatching(fps,aps);
907       if (pm!=0) {
908          return false;
909       } else {
910          return true;
911       }
912    }
913
914    public boolean checkTransition (Transition transition,boolean fullCheck) {
915       boolean isValid=true;
916       Map JavaDoc les=getLogicErrors((XMLComplexElement)transition.getParent().getParent());
917       String JavaDoc msg=(String JavaDoc)les.get(transition);
918       if (XMLUtil.getFromActivity(transition)==null) {
919          isValid=false;
920          msg=prepareMessageString(msg);
921          msg+=XMLUtil.getLanguageDependentString("ErrorNonExistingFromActivityReference");
922       }
923       if (XMLUtil.getToActivity(transition)==null) {
924          isValid=false;
925          msg=prepareMessageString(msg);
926          msg+=XMLUtil.getLanguageDependentString("ErrorNonExistingToActivityReference");
927       }
928       if (!isValid) {
929          les.put(transition,msg);
930       }
931       return isValid;
932    }
933
934    public boolean isIdValid (String JavaDoc id) {
935       return XMLUtil.isIdValid(id);
936    }
937
938    public static boolean isEmpty (String JavaDoc str) {
939       if (str==null || str.trim().length()==0) {
940          return true;
941       } else {
942          return false;
943       }
944    }
945
946    public static boolean isUniqueId (XMLCollection xmlCol,String JavaDoc id) {
947       int idCnt=0;
948       Iterator JavaDoc it=xmlCol.toElements().iterator();
949       while (it.hasNext()) {
950          try {
951             XMLCollectionElement xmlce=(XMLCollectionElement)it.next();
952             String JavaDoc cId=xmlce.getId();
953             if (cId.equals(id)) {
954                idCnt++;
955                if (idCnt>1) {
956                   return false;
957                }
958             }
959          } catch (ClassCastException JavaDoc cce) {
960             return true;
961          }
962       }
963       return true;
964    }
965
966    public static void main(String JavaDoc[] args) {
967       try {
968          XMLInterfaceForJDK13 xmlI=new XMLInterfaceForJDK13();
969          Package JavaDoc pkg=xmlI.parseDocument(args[0],true);
970          PackageValidator validator = new PackageValidator(null,pkg,false,
971                                                            true,false,false);
972          if (validator.validateAll(false)) {
973             System.out.println(args[0]+" is a valid XPDL package");
974          } else {
975             System.out.println(args[0]+" is not a valid XPDL package");
976          }
977       } catch (Exception JavaDoc ex) {
978          ex.printStackTrace();
979          System.exit(1);
980       }
981    }
982
983    /** Used for debug only */
984    public static void printIM(boolean[][] im,java.util.List JavaDoc acts) {
985       if (im != null) {
986          for (int i=0; i<im.length; i++) {
987             for (int j=0; j<im[i].length; j++) {
988                System.out.print(acts.get(i)+"->"+acts.get(j)+"="+im[i][j]+" ");
989             }
990             System.out.println();
991          }
992       } else {
993          System.out.println("Passed array is null !!!");
994       }
995    }
996
997    /** Used for debug only */
998    public static void printIM2(boolean[][] im,java.util.List JavaDoc acts) {
999       System.out.println("Activities are"+acts);
1000      if (im != null) {
1001         for (int i=0; i<im.length; i++) {
1002            for (int j=0; j<im[i].length; j++) {
1003               System.out.print(((im[i][j]) ? "1":"0")+" ");
1004            }
1005            System.out.println();
1006         }
1007      } else {
1008         System.out.println("Passed array is null !!!");
1009      }
1010   }
1011
1012
1013   //************************** GRAPH CONFORMANCE CHECKING ****************************
1014
public boolean checkGraphConformance (boolean fullCheck) {
1015      boolean areGraphsConformant=true;
1016
1017      Map JavaDoc graphConformanceErrors=new HashMap JavaDoc();
1018      List JavaDoc basicGraphConformanceErrors=new ArrayList JavaDoc();
1019
1020      Iterator JavaDoc procs=pkg.getWorkflowProcesses().toElements().iterator();
1021      while (procs.hasNext()) {
1022         WorkflowProcess wp=(WorkflowProcess)procs.next();
1023         if (!checkGraphConformance(wp,fullCheck)) {
1024            areGraphsConformant=false;
1025            if (!fullCheck) {
1026               break;
1027            }
1028            String JavaDoc msg="";
1029            Iterator JavaDoc bces=getBasicGraphConformanceErrors(wp).iterator();
1030            while (bces.hasNext()) {
1031               msg=msg+bces.next().toString()+"<br>";
1032            }
1033            graphConformanceErrors.put(wp,msg);
1034         }
1035      }
1036      if (!areGraphsConformant) {
1037         basicGraphConformanceErrors.add(XMLUtil.
1038                                            getLanguageDependentString("ErrorOneOrMoreProcessesDoNotSatisfyGraphConformance"));
1039      }
1040      basicGraphsConformanceErrors.put(pkg,basicGraphConformanceErrors);
1041      graphsConformanceErrors.put(pkg,graphConformanceErrors);
1042      return areGraphsConformant;
1043   }
1044   /**
1045    * Checks if graph conforms to the given conformance class.
1046    * @return true if graph is conformant, false otherwise
1047    */

1048   public boolean checkGraphConformance(XMLCollectionElement wpOrAs,boolean fullCheck) {
1049      Map JavaDoc graphConformanceErrors=new HashMap JavaDoc();
1050      List JavaDoc basicGraphConformanceErrors=new ArrayList JavaDoc();
1051
1052      Collection JavaDoc allActs=((XMLCollection)wpOrAs.get("Activities")).toElements();
1053
1054      Package JavaDoc pkg=XMLUtil.getPackage(wpOrAs);
1055      String JavaDoc conformanceClass=pkg.getConformanceClass().getGraphConformance();
1056      // ct=0->NON_BLOCKED, ct=1->LOOP_BLOCKED, ct=2->FULL_BLOCKED, ct=-1->default NON_BLOCKED
1057
int ct=XMLUtil.getConformanceClassNo(conformanceClass);
1058
1059      Activities acts=(Activities)wpOrAs.get("Activities");
1060      SequencedHashMap activities=acts.toElementMap();
1061
1062      if (activities.size()==0) {
1063         graphsConformanceErrors.put(wpOrAs,graphConformanceErrors);
1064         basicGraphsConformanceErrors.put(wpOrAs,basicGraphConformanceErrors);
1065         return true;
1066      }
1067
1068      boolean isGraphConformant=true;
1069
1070      Set JavaDoc splitActs=XMLUtil.getSplitOrJoinActivities(activities.values(),0);
1071      Set JavaDoc joinActs=XMLUtil.getSplitOrJoinActivities(activities.values(),1);
1072
1073      Set JavaDoc noSplitActs=new HashSet JavaDoc(activities.values());
1074      noSplitActs.removeAll(splitActs);
1075
1076      GraphChecker gc=null;
1077      if (ct>0 && (isGraphConformant || fullCheck)) {
1078         boolean[][] incidenceMatrix=createIncidenceMatrix(activities);
1079         if (incidenceMatrix==null) {
1080            basicGraphConformanceErrors.add("Unexpected error");
1081            graphsConformanceErrors.put(wpOrAs,graphConformanceErrors);
1082            basicGraphsConformanceErrors.put(wpOrAs,basicGraphConformanceErrors);
1083            return false;
1084         }
1085
1086         gc=new GraphChecker(incidenceMatrix);
1087
1088         // call method to check loop cycling
1089
boolean loopError=false;
1090         if (fullCheck) {
1091            int[] loopNodes=gc.getCyclicNodes();
1092            if (loopNodes!=null) {
1093               isGraphConformant=false;
1094               loopError=true;
1095               for (int i=0; i<loopNodes.length; i++) {
1096                  Activity act=(Activity)activities.get(loopNodes[i]);
1097                  graphConformanceErrors.put(act,XMLUtil.
1098                                                getLanguageDependentString("ErrorLoopContainedActivity"));
1099               }
1100            }
1101         } else {
1102            loopError=gc.isGraphCyclic();
1103            if (loopError) {
1104               isGraphConformant=false;
1105            }
1106         }
1107         if (loopError) {
1108            basicGraphConformanceErrors.add(XMLUtil.getLanguageDependentString("ErrorTheGraphIsCyclic"));
1109         }
1110      }
1111      // Here we check FULL_BLOCK conformance
1112
if (ct==2 && (isGraphConformant || fullCheck)) {
1113         // check if there is more then one starting activity
1114
if (XMLUtil.getStartingActivities(wpOrAs).size()!=1) {
1115            isGraphConformant=false;
1116            basicGraphConformanceErrors.add(XMLUtil.getLanguageDependentString(
1117                                               "ErrorThereMustBeExactlyOneStartingActivityInFullBlockedMode"));
1118         }
1119         // check if there is more then one ending activity
1120
if ((isGraphConformant || fullCheck) && XMLUtil.getEndingActivities(wpOrAs).size()!=1) {
1121            isGraphConformant=false;
1122            basicGraphConformanceErrors.add(XMLUtil.getLanguageDependentString(
1123                                               "ErrorThereMustBeExactlyOneEndingActivityInFullBlockedMode"));
1124         }
1125
1126         // check if the number of splits and joins matches
1127
boolean smerr=false;
1128         if ((isGraphConformant || fullCheck) && splitActs.size()!=joinActs.size()) {
1129            if (splitActs.size()>joinActs.size()) {
1130               basicGraphConformanceErrors.add(XMLUtil.getLanguageDependentString(
1131                                                  "ErrorTheNumberOfSplitsAndJoinsIsNotTheSame-MoreSplits"));
1132            } else {
1133               basicGraphConformanceErrors.add(XMLUtil.getLanguageDependentString(
1134                                                  "ErrorTheNumberOfSplitsAndJoinsIsNotTheSame-MoreJoins"));
1135            }
1136            isGraphConformant=false;
1137            smerr=true;
1138         }
1139
1140         // check for split/join type mismatch
1141
if ((isGraphConformant || fullCheck) && !smerr) {
1142            if (getNoOfANDSplitsOrJoins(splitActs,0)!=getNoOfANDSplitsOrJoins(joinActs,1)) {
1143               basicGraphConformanceErrors.add(XMLUtil.getLanguageDependentString(
1144                                                  "ErrorOneOrMoreSplitsDoNotHaveCorrespondingJoinBecauseOfTypeMismatch"));
1145               isGraphConformant=false;
1146            }
1147         }
1148         // first check for correct outgoing transitions
1149
if (isGraphConformant || fullCheck) {
1150            Iterator JavaDoc it=splitActs.iterator();
1151            boolean andSplitError=false;
1152            boolean xorSplitError=false;
1153            while (it.hasNext()) {
1154               Activity act=(Activity)it.next();
1155               if (XMLUtil.isANDTypeSplitOrJoin(act, 0)) {
1156                  if (!checkANDSplit(act)) {
1157                     isGraphConformant=false;
1158                     andSplitError=true;
1159                     String JavaDoc msg=(String JavaDoc)graphConformanceErrors.get(act);
1160                     msg=prepareMessageString(msg);
1161                     msg=msg+XMLUtil.getLanguageDependentString(
1162                        "ErrorOneOrMoreConditionalOutgoingTransitions");
1163                     graphConformanceErrors.put(act,msg);
1164                     if (!fullCheck) {
1165                        break;
1166                     }
1167                  }
1168               } else {
1169                  if (!checkXORSplit(act)) {
1170                     isGraphConformant=false;
1171                     xorSplitError=true;
1172                     String JavaDoc msg=(String JavaDoc)graphConformanceErrors.get(act);
1173                     msg=prepareMessageString(msg);
1174                     msg=msg+XMLUtil.getLanguageDependentString(
1175                        "ErrorMissingOTHERWISETypeOutgoingTransition");
1176                     graphConformanceErrors.put(act,msg);
1177                     if (!fullCheck) {
1178                        break;
1179                     }
1180                  }
1181               }
1182            }
1183
1184            // check activities that has only one outgoing transition, if
1185
// there is condition on it -> report XOR split with conditional
1186
// transition error
1187
it=noSplitActs.iterator();
1188            while (it.hasNext()) {
1189               Activity act=(Activity)it.next();
1190               if (!checkXORSplit(act)) {
1191                  isGraphConformant=false;
1192                  xorSplitError=true;
1193                  String JavaDoc msg=(String JavaDoc)graphConformanceErrors.get(act);
1194                  msg=prepareMessageString(msg);
1195                  msg=msg+XMLUtil.getLanguageDependentString(
1196                     "ErrorMissingOTHERWISETypeOutgoingTransition");
1197                  graphConformanceErrors.put(act,msg);
1198                  if (!fullCheck) {
1199                     break;
1200                  }
1201               }
1202            }
1203
1204            if (andSplitError) {
1205               basicGraphConformanceErrors.add(XMLUtil.getLanguageDependentString(
1206                                                  "ErrorOneOrMoreANDSplitsHaveConditionalOutgoingTransitions"));
1207            }
1208            if (xorSplitError) {
1209               basicGraphConformanceErrors.add(XMLUtil.getLanguageDependentString(
1210                                                  "ErrorOneOrMoreXORSplitsWithConditionalTransitionsDoNotHaveOTHERWISETransition"));
1211            }
1212         }
1213
1214         // now perform search on every split activity for corresponding join activity
1215
if (isGraphConformant || fullCheck) {
1216            boolean noCorrespondingJoinError=false;
1217            Iterator JavaDoc it=splitActs.iterator();
1218            while (it.hasNext()) {
1219               Activity act=(Activity)it.next();
1220               int splitIndex=activities.indexOf(act);
1221               if (splitIndex==-1) {
1222                  basicGraphConformanceErrors.add("Unexpected error");
1223                  isGraphConformant=false;
1224                  if (!fullCheck) {
1225                     break;
1226                  } else {
1227                     continue;
1228                  }
1229               }
1230               int ji=gc.getJoinIndex(splitIndex);
1231               // The correspondin join can't be found
1232
if (ji<0) {
1233                  isGraphConformant=false;
1234                  noCorrespondingJoinError=true;
1235                  String JavaDoc msg=(String JavaDoc)graphConformanceErrors.get(act);
1236                  msg=prepareMessageString(msg);
1237                  msg=msg+XMLUtil.getLanguageDependentString("ErrorThereIsNoCorrespondingJoinActivity");
1238                  graphConformanceErrors.put(act,msg);
1239                  if (!fullCheck) {
1240                     break;
1241                  }
1242                  // if the join is found and their types are different
1243
// the graph is not conformant
1244
} else {
1245                  if (XMLUtil.isANDTypeSplitOrJoin(act, 0)!=
1246                        XMLUtil.isANDTypeSplitOrJoin((Activity)activities.get(ji), 1)) {
1247                     isGraphConformant=false;
1248                     noCorrespondingJoinError=true;
1249                     String JavaDoc msg=(String JavaDoc)graphConformanceErrors.get(act);
1250                     msg=prepareMessageString(msg);
1251                     if (XMLUtil.isANDTypeSplitOrJoin((Activity)act, ji)) {
1252                        msg=msg+XMLUtil.getLanguageDependentString(
1253                           "ErrorTheCorrespondingJoinActivityDoesNotHaveTheSameType-ANDXOR");
1254                     } else {
1255                        msg=msg+XMLUtil.getLanguageDependentString(
1256                           "ErrorTheCorrespondingJoinActivityDoesNotHaveTheSameType-XORAND");
1257                     }
1258                     graphConformanceErrors.put(act,msg);
1259                     if (!fullCheck) {
1260                        break;
1261                     }
1262                  }
1263               }
1264            }
1265            if (noCorrespondingJoinError) {
1266               basicGraphConformanceErrors.add(XMLUtil.getLanguageDependentString(
1267                                                  "ErrorOneOrMoreSplitsDoNotHaveCorrespondingJoin"));
1268            }
1269         }
1270      }
1271
1272      // if so far the graph is conformant, or the full check is required,
1273
// check the graphs block activities
1274
if (isGraphConformant || fullCheck) {
1275         Set JavaDoc blockActivities=XMLUtil.getBlockActivities(wpOrAs,false);
1276         boolean innerConformanceError=false;
1277         Iterator JavaDoc it=blockActivities.iterator();
1278         while (it.hasNext()) {
1279            Activity act=(Activity)it.next();
1280            BlockActivity ba=act.getActivityTypes().getBlockActivity();
1281            String JavaDoc asId=ba.getBlockId();
1282            ActivitySet as=XMLUtil.getWorkflowProcess(act).getActivitySet(asId);
1283            if (as!=null && !checkGraphConformance(as,false)) {
1284               isGraphConformant=false;
1285               innerConformanceError=true;
1286               String JavaDoc msg=(String JavaDoc)graphConformanceErrors.get(ba);
1287               msg=prepareMessageString(msg);
1288               msg=msg+XMLUtil.getLanguageDependentString("ErrorInnerGraphConformanceError");
1289               graphConformanceErrors.put(act,msg);
1290               graphConformanceErrors.put(act,msg);
1291               if (!fullCheck) {
1292                  break;
1293               }
1294            }
1295         }
1296         if (innerConformanceError) {
1297            basicGraphConformanceErrors.add(XMLUtil.getLanguageDependentString(
1298                                               "ErrorOneOrMoreBlockActivitiesAreNotValid"));
1299         }
1300      }
1301
1302      graphsConformanceErrors.put(wpOrAs,graphConformanceErrors);
1303      basicGraphsConformanceErrors.put(wpOrAs,basicGraphConformanceErrors);
1304      return isGraphConformant;
1305   }
1306
1307   protected boolean[][] createIncidenceMatrix (SequencedHashMap sortedMap) {
1308      int size=sortedMap.size();
1309      boolean[][] incidenceMatrix=new boolean[size][size];
1310      for (int indAct=0; indAct<size; indAct++) {
1311         Activity a=(Activity)sortedMap.get(indAct);
1312         Set JavaDoc oas=new HashSet JavaDoc();
1313         Iterator JavaDoc trs=XMLUtil.getOutgoingTransitions(a).iterator();
1314         while (trs.hasNext()) {
1315            Transition t=(Transition)trs.next();
1316            String JavaDoc aOut=t.getTo();
1317            int indOut=sortedMap.indexOf(aOut);
1318            if (indOut==-1) return null;
1319            incidenceMatrix[indAct][indOut]=true;
1320         }
1321      }
1322      return incidenceMatrix;
1323   }
1324
1325   /**
1326    * Returns the number of activities in the given set that have
1327    * split or join, depending on second parameter.
1328    * @param acts The set of activities that are searched for split or join
1329    * @param sOrJ 0 -> searching for split, otherwise, searching for join
1330    */

1331   protected int getNoOfANDSplitsOrJoins (Set JavaDoc acts,int sOrJ) {
1332      int no=0;
1333      Iterator JavaDoc it=acts.iterator();
1334      while (it.hasNext()) {
1335         Activity act=(Activity)it.next();
1336         if (sOrJ==0 && XMLUtil.isANDTypeSplitOrJoin(act, 0)) {
1337            no++;
1338         } else if (sOrJ==1 && XMLUtil.isANDTypeSplitOrJoin(act, 0)) {
1339            no++;
1340         }
1341      }
1342      return no;
1343   }
1344
1345   protected boolean checkANDSplit (Activity act) {
1346      return !hasAnyPostcondition(act);
1347   }
1348
1349   protected boolean checkXORSplit (Activity act) {
1350      // if activity has any postcondition, it must have an otherwise transition
1351
if (hasAnyPostcondition(act)) {
1352         Set JavaDoc ots=XMLUtil.getOutgoingTransitions(act);
1353         Iterator JavaDoc trs=ots.iterator();
1354         while (trs.hasNext()) {
1355            Transition t=(Transition)trs.next();
1356            if (t.getCondition().getType().equals(XPDLConstants.CONDITION_TYPE_OTHERWISE)) {
1357               return true;
1358            }
1359         }
1360         return false;
1361      } else {
1362         return true;
1363      }
1364   }
1365
1366   protected boolean hasAnyPostcondition (Activity act) {
1367      Set JavaDoc outL=XMLUtil.getOutgoingTransitions(act);
1368      Iterator JavaDoc it=outL.iterator();
1369      while (it.hasNext()) {
1370         if (!((Transition)it.next()).getCondition().toValue().equals("")) {
1371            return true;
1372         }
1373      }
1374      return false;
1375   }
1376
1377   //************************** GRAPH CONNECTIONS CHECKING ****************************
1378

1379   public boolean checkGraphConnections (boolean fullCheck) {
1380      basicGraphConnectionErrors.remove(pkg);
1381      graphsConnectionErrors.remove(pkg);
1382
1383      boolean areWellConnected=true;
1384      String JavaDoc basicGraphConnectionError;
1385      Map JavaDoc connectionErrorMessages=new HashMap JavaDoc();
1386
1387      Iterator JavaDoc procs=pkg.getWorkflowProcesses().toElements().iterator();
1388      while (procs.hasNext()) {
1389         WorkflowProcess wp=(WorkflowProcess)procs.next();
1390         if (!checkGraphConnections(wp,false)) {
1391            areWellConnected=false;
1392            if (!fullCheck) {
1393               break;
1394            }
1395            String JavaDoc msg=getBasicGraphConnectionError(wp);
1396            if (msg==null) {
1397               msg="";
1398            }
1399            connectionErrorMessages.put(wp,msg);
1400         }
1401      }
1402      if (!areWellConnected) {
1403         basicGraphConnectionError=
1404            XMLUtil.getLanguageDependentString("InformationOneOrMoreProcessesHaveImproperlyConnectedElements");
1405         basicGraphConnectionErrors.put(pkg,basicGraphConnectionError);
1406      }
1407      graphsConnectionErrors.put(pkg,connectionErrorMessages);
1408      return areWellConnected;
1409   }
1410
1411   public boolean checkGraphConnections (XMLCollectionElement wpOrAs,boolean fullCheck) {
1412      if (wpOrAs==null) return false;
1413      basicGraphConnectionErrors.remove(wpOrAs);
1414      graphsConnectionErrors.remove(wpOrAs);
1415
1416      boolean isWellConnected=true;
1417      boolean basicError=false;
1418      Map JavaDoc connectionErrorMessages=new HashMap JavaDoc();
1419
1420      Transitions ts=(Transitions)wpOrAs.get("Transitions");
1421      Collection JavaDoc acts=((Activities)wpOrAs.get("Activities")).toElements();
1422      if (acts==null || acts.size()==0) {
1423         graphsConnectionErrors.put(wpOrAs,connectionErrorMessages);
1424         return true;
1425      }
1426
1427      Set JavaDoc startActs=null;
1428      Set JavaDoc endActs=null;
1429      if (fullCheck || isWellConnected) {
1430         startActs=XMLUtil.getStartingActivities(wpOrAs);
1431         if (startActs.size()==0 && (!allowUndefinedStart || (wpOrAs instanceof ActivitySet))) {
1432            isWellConnected=false;
1433            basicError=true;
1434            String JavaDoc msg=(String JavaDoc)connectionErrorMessages.get(wpOrAs);
1435            msg=prepareMessageString(msg);
1436            msg+=XMLUtil.getLanguageDependentString("ErrorStartingActivityDoesNotExist");
1437            connectionErrorMessages.put(wpOrAs,msg);
1438         }
1439      }
1440      if (fullCheck || isWellConnected) {
1441         endActs=XMLUtil.getEndingActivities(wpOrAs);
1442         if (endActs.size()==0 && (!allowUndefinedEnd || (wpOrAs instanceof ActivitySet))) {
1443            isWellConnected=false;
1444            basicError=true;
1445            String JavaDoc msg=(String JavaDoc)connectionErrorMessages.get(wpOrAs);
1446            msg=prepareMessageString(msg);
1447            msg+=XMLUtil.getLanguageDependentString("ErrorEndingActivityDoesNotExist");
1448            connectionErrorMessages.put(wpOrAs,msg);
1449         }
1450      }
1451      if (fullCheck || isWellConnected) {
1452         Iterator JavaDoc it=acts.iterator();
1453         while (it.hasNext()) {
1454            Activity act=(Activity)it.next();
1455            String JavaDoc cem=checkActivityConnection(act,ts,startActs,endActs,fullCheck);
1456            if (cem!=null) {
1457               connectionErrorMessages.put(act,cem);
1458               isWellConnected=false;
1459               if (!fullCheck) {
1460                  break;
1461               }
1462            }
1463
1464         }
1465      }
1466
1467      if (!isWellConnected) {
1468         if (basicError) {
1469            basicGraphConnectionErrors.put(wpOrAs,connectionErrorMessages.get(wpOrAs));
1470         } else {
1471            basicGraphConnectionErrors.put(wpOrAs,
1472                                           XMLUtil.getLanguageDependentString("InformationOneOrMoreElementsAreNotProperlyConnected"));
1473         }
1474      }
1475      graphsConnectionErrors.put(wpOrAs,connectionErrorMessages);
1476      return isWellConnected;
1477   }
1478
1479   /**
1480    * Checks if given activity is well connected.
1481    * @return String describing the error, or empty string if there is no connection
1482    * error for giving activity.
1483    */

1484   public String JavaDoc checkActivityConnection (Activity act,Transitions ts,
1485                                          Set JavaDoc startActs,Set JavaDoc endActs,boolean fullCheck) {
1486
1487      String JavaDoc connectionErrorMsg="";
1488      // if this is a block activity, check inner transitions
1489
if (act.getActivityTypes().getChoosen() instanceof BlockActivity) {
1490         BlockActivity ba=act.getActivityTypes().getBlockActivity();
1491         String JavaDoc asId=ba.getBlockId();
1492         ActivitySet as=XMLUtil.getWorkflowProcess(act).getActivitySet(asId);
1493         if (as!=null) {
1494            if (!checkGraphConnections(as,false)) {
1495               connectionErrorMsg+=
1496                  XMLUtil.getLanguageDependentString("ErrorInnerTransitionError")+"; ";
1497            }
1498         }
1499      }
1500
1501      if (connectionErrorMsg.length()==0) connectionErrorMsg=null;
1502
1503      return connectionErrorMsg;
1504   }
1505
1506   protected Activity findBlockActivity (ActivitySet as) {
1507      String JavaDoc asId=as.getId();
1508      WorkflowProcess wp=XMLUtil.getWorkflowProcess(as);
1509      Set JavaDoc bas=XMLUtil.getBlockActivities(wp,true);
1510      Iterator JavaDoc it=bas.iterator();
1511      while (it.hasNext()) {
1512         Activity a=(Activity)it.next();
1513         String JavaDoc baId=a.getActivityTypes().getBlockActivity().getBlockId();
1514         if (baId.equals(asId)) {
1515            return a;
1516         }
1517      }
1518      return null;
1519   }
1520
1521   protected String JavaDoc prepareMessageString (String JavaDoc msg) {
1522      if (msg!=null) {
1523         msg=msg+"; ";
1524      } else {
1525         msg="";
1526      }
1527      return msg;
1528   }
1529
1530}
1531
Popular Tags