KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > test > EventsDataGenerator


1 /*
2  * MDRDataGenerator.java
3  *
4  * Created on April 9, 2002, 10:37 AM
5  */

6
7 package org.netbeans.mdr.test;
8
9 import javax.jmi.model.*;
10 import javax.jmi.reflect.*;
11
12 import java.util.Iterator JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.LinkedList JavaDoc;
15
16 import java.io.PrintStream JavaDoc;
17
18 import org.netbeans.lib.jmi.xmi.*;
19 import org.netbeans.lib.jmi.util.*;
20
21 /**
22  *
23  * @author mk97936
24  */

25 public class EventsDataGenerator extends RandomDataGenerator {
26     
27     private RefPackage extent;
28     
29     /** Creates a new instance of MDRDataGenerator */
30     public EventsDataGenerator(RefPackage pkg) {
31         super(pkg);
32         extent = pkg;
33     }
34     
35     public List JavaDoc generateAssociation(RefAssociation assocProxy, Association assoc, PrintStream JavaDoc ps, boolean planned) {
36         //System.out.println("Association name: " + assoc.getName());
37
LinkedList JavaDoc ends = new LinkedList JavaDoc();
38         
39         Iterator JavaDoc iterCont = ((Namespace) assoc).getContents().iterator();
40         while (iterCont.hasNext()) {
41             RefObject refObj = (RefObject) iterCont.next();
42             if (refObj instanceof AssociationEnd) {
43                 MofClass mofClass = (MofClass) ((AssociationEnd) refObj).getType();
44                 if (mofClass.isAbstract()) {
45                     mofClass = findSubtype(mofClass);
46                 }
47                 ends.add(mofClass);
48             }
49         }
50         
51         if (assocProxy instanceof Contains) {
52             Namespace nmspc = (Namespace) generateInstance((MofClass) ends.get(0));
53             ModelElement mdlelm = (ModelElement) generateInstance((MofClass) ends.get(1));
54             
55             assocProxy.refAddLink(nmspc, mdlelm);
56             
57             Iterator JavaDoc nmIter = nmspc.getContents().iterator();
58             while (nmIter.hasNext()) {
59                 RefObject obj = (RefObject) nmIter.next();
60                 if (obj instanceof Reference) {
61                     System.out.println(((Reference) obj).getReferencedEnd());
62                 }
63             }
64             
65             return ends;
66             
67         } else if (assocProxy instanceof Constrains) {
68             assocProxy.refAddLink((Constraint) generateInstance((MofClass) ends.get(0)),
69                 (ModelElement) generateInstance((MofClass) ends.get(1)));
70             return ends;
71         } else if (assocProxy instanceof Aliases) {
72             assocProxy.refAddLink((Import) generateInstance((MofClass) ends.get(0)),
73                 (Namespace) generateInstance((MofClass) ends.get(1)));
74             return ends;
75         } else if (assocProxy instanceof Generalizes) {
76             assocProxy.refAddLink((GeneralizableElement) generateInstance((MofClass) ends.get(0)),
77                 (GeneralizableElement) generateInstance((MofClass) ends.get(1)));
78             return ends;
79         } else if (assocProxy instanceof AttachesTo) {
80             assocProxy.refAddLink((ModelElement) generateInstance((MofClass) ends.get(0)),
81                 (Tag) generateInstance((MofClass) ends.get(1)));
82             return ends;
83         } else if (assocProxy instanceof CanRaise) {
84             assocProxy.refAddLink((Operation) generateInstance((MofClass) ends.get(0)),
85                 (MofException) generateInstance((MofClass) ends.get(1)));
86             return ends;
87         } else if (assocProxy instanceof IsOfType) {
88             assocProxy.refAddLink((Classifier) generateInstance((MofClass) ends.get(0)),
89                 (TypedElement) generateInstance((MofClass) ends.get(1)));
90             return ends;
91         }
92         
93         /*else if (assocProxy instanceof RefersTo) {
94             System.out.println("-----> Creating RefersTo");
95             assocProxy.refAddLink((Reference) generateInstance((MofClass) ends.get(0)),
96                                   (AssociationEnd) generateInstance((MofClass) ends.get(1)));
97         }*/

98         
99         // Derived associations DependsOn and Exposes cannot be created !!!
100

101         return null;
102         
103     }
104     
105     /**
106      *
107      *
108      */

109     public boolean setAttribute(RefObject refObj, PrintStream JavaDoc ps, boolean planned) {
110         boolean setAttr = false;
111         Iterator JavaDoc contents = ((Namespace) refObj.refMetaObject()).getContents().iterator();
112         while (contents.hasNext()) {
113             Object JavaDoc contObj = contents.next();
114             if (contObj instanceof Attribute) {
115                 Object JavaDoc attr = generateAttributeValue((Attribute) contObj);
116                 if ( ((Attribute) contObj).isChangeable() && ((Attribute) contObj).getMultiplicity().getUpper() == 1 ) {
117                     //System.out.println("Setting attribute: " + contObj.toString());
118
refObj.refSetValue((RefObject) contObj, attr);
119                     setAttr = true;
120                 }
121             }
122         }
123         return setAttr;
124     }
125     
126     /**
127      *
128      *
129      */

130     public boolean addAttribute(RefObject refObj, boolean remove, PrintStream JavaDoc ps, boolean planned) {
131         boolean ws = false;
132         Iterator JavaDoc contents = ((Namespace) refObj.refMetaObject()).getContents().iterator();
133         while (contents.hasNext()) {
134             Object JavaDoc contObj = contents.next();
135             if (contObj instanceof Attribute) {
136                 if ( ((Attribute) contObj).isChangeable() && ((Attribute) contObj).getMultiplicity().getUpper() == -1 ) {
137                     //System.out.println(((ModelElement) contObj).getName());
138
Classifier type = ((Attribute) contObj).getType();
139                     List JavaDoc attrValues = (List JavaDoc) refObj.refGetValue((RefObject) contObj);
140                     for (int i = 0; i < 5; i++) {
141                         Object JavaDoc val = generateValue(type);
142                         attrValues.add(val);
143                         ws = true;
144                     }
145                     if (remove) {
146                         for (int j = 0; j < 5; j++) {
147                             attrValues.remove(0);
148                         }
149                     }
150                 }
151             }
152         }
153         return ws;
154     }
155
156     public RefObject generateWrongInstance(MofClass mofClass) {
157         RefPackage refPackage = (RefPackage) elementsCache.findProxy(mofClass);
158         RefClass proxy = refPackage.refClass(mofClass);
159         List JavaDoc args = new LinkedList JavaDoc();
160         args.add(new java.util.Date JavaDoc());
161         return proxy.refCreateInstance(args);
162     }
163     
164 }
165
Popular Tags