Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/media/mn8/ConceptDefinition.java


1   /* 
2    * $copyright$
3    * $Id: ConceptDefinition.java,v 1.34 2002/06/05 14:55:26 neuro Exp $
4    *
5    * Date        Author            Changes 
6    * Jul 03 2001 Remus Pereni      Created
7    */
8   package org.media.mn8;
9   
10  
11  import org.media.mn8.concepts.*;
12  import org.media.mn8.event.*;
13  import org.media.mn8.util.ConceptNavPatterns;
14  import java.util.Vector;
15  import java.util.Hashtable;
16  import java.util.Enumeration;
17  
18  /** 
19   * being from an mn8 script based concept or a Java one, is 
20   * registered in {@link org.media.mn8.ConceptRegister}, where it is 
21   * taken by the {@link org.media.mn8.mn8Loader} and instantiated.
22   * @author <a href="mailto:remus@nolimits.ro">Remus Pereni</a>
23   * @version $Revision: 1.34 $ $Date: 2002/06/05 14:55:26 $
24   */
25  public abstract class ConceptDefinition {
26  
27      private Vector _inheritance = new Vector();
28  
29      private Hashtable _methodTable = new Hashtable();
30      private Hashtable _cameleonMethodTable;
31      private Hashtable _constructorTable = new Hashtable();
32      private Hashtable _operationTable = new Hashtable();
33  
34      private Vector _elementTable = new Vector();
35      private Vector _elementNameTable = new Vector();
36  
37      private Hashtable _attributeTable = new Hashtable();
38     
39  
40      public abstract StringConcept getConceptLabel();
41  
42      public abstract  StringConcept getConceptType();
43  
44      public abstract SeriesConcept getInheritedConcepts();
45  
46  
47  
48      /**
49       * A method also representing the static method Concept.hasConceptAttribute:String
50       * which checks if an concept has an attribute named attributeName.
51       * @param attributeName the name of the attribute to be looked for.
52       * @return true if exists false otherway.
53       */
54      public LogicalConcept hasConceptAttribute(StringConcept attributeName) {
55         return new LogicalConcept( hasConceptAttribute(attributeName.toString()) ) ;
56      }
57  
58  
59      /**
60       * Same as #hasConceptAttribute(StringConcept) in functionality but used internally where
61       * a possible conversion to StringConcept might not be wanted.
62       * @param attributeName the name of the attribute to be looked for.
63       * @return true if exists false otherway.
64       */
65      public boolean hasConceptAttribute(String attributeName) {
66          attributeName = attributeName.toLowerCase();
67          if( attributeName.trim().startsWith("@"))
68              attributeName = attributeName.substring(1);
69          return _attributeTable.containsKey( attributeName ) ;
70      }
71  
72  
73      public Concept getConceptAttribute(Concept on, StringConcept attributeName) {
74          attributeName = attributeName.toLowerCase();
75          if ( _attributeTable.containsKey( attributeName.getValue() )) {
76              FieldConcept attribute = (FieldConcept)_attributeTable.get( attributeName.getValue() );
77              AttributeConcept attr = (AttributeConcept) attribute.getValue( on);
78              return attr ;
79          }
80          else {
81              throw new AttributeNotFoundException(on.getConceptType().toString(), attributeName.toString());
82          }
83      }
84  
85  
86      public SeriesConcept getConceptElementFields() {
87          return new SeriesConcept(_elementTable);
88      }
89  
90  
91      public Vector getConceptElementsFieldName() {
92          return _elementNameTable;
93      }
94  
95  
96      public SeriesConcept getConceptAttributeFields() {
97          return new SeriesConcept( new Vector( _attributeTable.values() ));
98      }
99  
100 
101     public Hashtable getConceptAttributeFieldsAsHashtable() {
102         return _attributeTable;
103     }
104 
105 
106     public FieldDefinition getConceptAttributeField( StringConcept attributeName) {
107         attributeName = attributeName.toLowerCase();
108         return getConceptAttributeField( attributeName.toString() );
109     }
110 
111 
112     public FieldDefinition getConceptAttributeField( String attributeName ) {
113         attributeName = attributeName.toLowerCase();
114         if( _attributeTable.containsKey(attributeName) ) {
115             return (FieldDefinition) _attributeTable.get( attributeName );
116         } else {
117             throw new FieldNotFoundException( getConceptType().toString(), attributeName);
118         }
119     }
120 
121 
122     /**
123      * Checks if the current concept has an element with the requested name.
124      * @param elementName a <code>StringConcept</code> representing the name of the element.
125      * @return True if the element us available, false otherway.
126      */
127     public LogicalConcept hasConceptElement(StringConcept elementName) {
128        return new LogicalConcept( hasConceptElement( elementName.toString())) ;
129     }
130 
131 
132     /**
133      * Performs the same function as {@link #hasConceptElement(StringConcept)}
134      * but is used for internal operation where a conversion to StringConcept might
135      * not be wanted.
136      * @param elementName the name of the element to be looked for.
137      * @return true if the element is available, false otherways.
138      */
139     public boolean hasConceptElement(String elementName) {
140        return  _elementNameTable.contains(elementName.toLowerCase()) ;
141     }
142 
143 
144     public Concept getConceptElement(Concept on, StringConcept elementName) {
145         if ( _elementNameTable.contains( elementName.getValue().toLowerCase() )) {
146             FieldConcept element = (FieldConcept)_elementTable.elementAt( _elementNameTable.indexOf(elementName.getValue().toLowerCase()));
147             Element elem = (Element) element.getValue( on);
148             return elem.getElementValue();
149         }
150         else {
151             throw new ElementNotFoundException( on.getConceptType().toString(), elementName.toString());
152         }
153     }
154 
155 
156     public SeriesConcept getConceptMethods() {
157         return new SeriesConcept( new Vector( _methodTable.values() ));
158     }
159 
160 
161     public SeriesConcept getCameleonConceptMethods() {
162         if( _cameleonMethodTable == null) 
163             initializeCameleonMethodTable();
164         return new SeriesConcept( new Vector( _cameleonMethodTable.values() ));
165     }
166 
167 
168     public SeriesConcept getConceptConstructors() {
169         return new SeriesConcept( new Vector( _constructorTable.values() ));
170     }
171 
172 
173     public SeriesConcept getConceptOperators() {
174         return new SeriesConcept( new Vector( _operationTable.values() ));
175     }
176 
177 
178     public Concept getConceptInstance() {
179         return new JavaConcept();
180     }
181 
182 
183     public Vector getInheritanceList() {
184         return _inheritance;
185     }
186 
187 
188     public final void putOperator( mn8Method method) {
189         _operationTable.remove(method.getSignature());
190         _operationTable.put( method.getSignature(), method );
191 
192     }
193     
194 
195     public final void putMethod( mn8Method method) {
196         if ( method.getName().getValue().toLowerCase().equals("create") ){
197             _constructorTable.remove(method.getSignature().toLowerCase());
198             _constructorTable.put( method.getSignature().toLowerCase(), method );            
199         }
200         else{
201             _methodTable.remove(method.getSignature().toLowerCase());
202             _methodTable.put( method.getSignature().toLowerCase(), method );
203         }
204     }
205 
206 
207     public final void removeMethod( String methodSignature) {
208         if ( methodSignature.startsWith("create:") )
209             _constructorTable.remove( methodSignature );
210         else
211             if ( methodSignature.startsWith("op") )
212                 _operationTable.remove( methodSignature );
213             else
214                 _methodTable.remove( methodSignature );
215     }
216 
217     
218     public mn8Method getConstructor( String methodSignature ) {
219         return (mn8Method) _constructorTable.get( methodSignature.toLowerCase() );
220     }
221 
222 
223     public Hashtable getInheritanceMethods() {
224         Hashtable result = new Hashtable( _operationTable );
225         result.putAll( _methodTable );
226         return result;
227     }
228 
229 
230     public void putAttribute( FieldConcept attributeDef ) {
231         _attributeTable.put( attributeDef.getName().toLowerCase(), attributeDef );
232     }
233 
234 
235     public void removeAttribute ( String attribName ) {
236         attribName = attribName.toLowerCase();
237         _attributeTable.remove( attribName );
238     }
239 
240     // aicea vine modificat
241     public FieldDefinition getAttribute( String attribName ) {
242         attribName = attribName.toLowerCase();
243         return (FieldDefinition) _attributeTable.get( attribName );
244     }
245 
246 
247     public FieldDefinition getAttributeField( String attribName ) {
248         attribName = attribName.toLowerCase();
249         return (FieldDefinition) _attributeTable.get( attribName );
250     }
251 
252     
253     public void putElement( FieldConcept elementDef ) {
254         _elementTable.add(elementDef );
255         _elementNameTable.add(elementDef.getName().toLowerCase());
256     }
257         
258 
259     public FieldDefinition getElement( String elementName ) {
260         int idx = _elementNameTable.indexOf(elementName.toLowerCase());
261         if( idx != -1 ) {
262             return (FieldDefinition) _elementTable.elementAt( idx );
263         }
264         else 
265             return null;
266     }
267 
268 
269     public FieldDefinition getConceptElementField( StringConcept elementName) {
270         return getConceptElementField( elementName.toString() );
271     }
272 
273 
274     public FieldDefinition getConceptElementField( String elementName ) {
275         int idx = _elementNameTable.indexOf(elementName.toLowerCase());
276         if( idx != -1 ) {
277             return (FieldDefinition) _elementTable.elementAt( idx );
278         }
279         else {
280             throw new FieldNotFoundException(getConceptType().toString(), elementName);
281         }            
282     }
283 
284 
285     public final mn8Method getConceptOperator(StringConcept methodSignature) {
286         return getConceptOperator( methodSignature.getValue() );
287     }
288 
289 
290     public final mn8Method getConceptOperator(String methodSignature) {
291         mn8Method result = null;
292         if( ( result = (mn8Method)_operationTable.get(methodSignature)) != null ){
293             return result;
294         }
295         else {
296             result = mn8JavaMethod.getMethodUsingInheritance( methodSignature, 
297                                                               getConceptOperators().getValue());
298             if( result != null )
299                 return result;
300         }
301         throw new RuntimeException("Unable to find operator with signature: " + 
302                                    methodSignature + 
303                                    " in concept type: " + getConceptType());
304     }
305 
306 
307     public final mn8Method getConceptMethod(StringConcept methodSignature) {
308         return getConceptMethod( methodSignature.getValue() );
309     }
310 
311 
312     public LogicalConcept hasConceptMethod( StringConcept methodSignature ) {
313         try {
314             if( getConceptMethod(methodSignature) != null )
315                 return new LogicalConcept(true);
316             else
317                 return new LogicalConcept(false);
318         } catch (RuntimeException ex) {
319             return new LogicalConcept(false);
320         }
321     }
322 
323 
324     public final mn8Method getConceptMethod(String methodSignature) {
325         methodSignature = methodSignature.toLowerCase();
326         mn8Method result = null;
327         if( (result = (mn8Method) _methodTable.get(methodSignature)) != null) {
328             return result ;
329         }
330         else {
331             result = mn8JavaMethod.getMethodUsingInheritance( methodSignature, 
332                                                               getConceptMethods().getValue());
333             if( result != null )
334                 return result;
335         }
336 
337         throw new MethodNotFoundException(getConceptType().toString(), methodSignature);
338     }
339 
340 
341 
342     public final mn8Method getConceptConstructor(StringConcept methodSignature) {
343         return getConceptConstructor(methodSignature.toString());
344     }
345 
346 
347 
348     public final mn8Method getConceptConstructor(String methodSignature) {
349         mn8Method result = null;
350         if( (result = (mn8Method) _constructorTable.get(methodSignature)) != null) {
351             return result ;
352         } else {
353             result = mn8JavaMethod.getMethodUsingInheritance( methodSignature,
354                                                               getConceptConstructors().getValue());
355             if( result != null )
356                 return result;
357         }
358 
359         throw new RuntimeException("Unable to find constructor with signature: " + 
360                                    methodSignature);
361     }
362 
363 
364     public final LogicalConcept extendsConcept( StringConcept conceptType ) {
365         if( conceptType.getValue().toLowerCase().equals("concept"))
366             return new LogicalConcept(true);
367         else {
368             if( conceptType.getValue().toLowerCase().equals(getConceptType().getValue().toLowerCase())) {
369                 return new LogicalConcept(true);       
370             }
371             else {
372                 SeriesConcept select = getInheritedConcepts().selectSIMEXP( conceptType.getValue() );
373                 if( select.getValue().size() > 0 )
374                     return new LogicalConcept(true);
375                 else 
376                     return new LogicalConcept(false);                    
377             }
378         }
379     }
380 
381 
382 
383     public static void main (String[] args) {
384         try {
385             Class _class = Class.forName(args[0]);
386             java.lang.reflect.Method reference = _class.getDeclaredMethod("getReference", null);
387             ConceptConceptDefinition instance = (ConceptConceptDefinition)
388                 reference.invoke(reference, null);
389             System.out.println("--" + instance.getClass().getName());
390             System.out.println(instance.getConceptMethods().toTXT().getValue());
391 
392         } catch (Exception ex) {
393             ex.printStackTrace();
394         }
395 
396     }
397 
398 
399     public SeriesConcept getAllInheritedConcepts() {
400         Vector allInheritedConcepts = new Vector();
401         Vector newSet = null;
402         allInheritedConcepts.addAll(getInheritedConcepts().getVector());
403         allInheritedConcepts.remove( new StringConcept("Concept"));
404 
405         for(int cnt = 0; cnt < allInheritedConcepts.size() ; cnt ++) {
406             newSet = addInheritedConcepts((StringConcept) allInheritedConcepts.elementAt(cnt));
407 
408             if( newSet != null && newSet.size() > 0 ) {
409                 newSet.remove( new StringConcept("Concept"));
410                 allInheritedConcepts.addAll(newSet);
411             }
412         }
413         allInheritedConcepts.add(new StringConcept("Concept"));
414         return new SeriesConcept( allInheritedConcepts );
415     }
416 
417     
418     private Vector addInheritedConcepts(StringConcept ofConcept) {
419         Vector result =  mn8Loader.getConceptDefinition(ofConcept.toString()).getInheritedConcepts().getVector();
420         if( result != null && result.size() > 0 ) {
421             return result;
422         } else {
423             return null;
424         }
425     }
426 
427 
428     protected void initializeCameleonMethodTable() {
429         Enumeration methods = _methodTable.elements();
430         mn8Method method = null;
431         _cameleonMethodTable = new Hashtable();
432 
433         while( methods.hasMoreElements() ) {
434             method = (mn8Method) methods.nextElement();
435 
436             if( method instanceof mn8JavaMethod &&
437                 method.getDeclaringConcept().toString().equals( getConceptType().toString()) )
438                 _cameleonMethodTable.put( method.getSignature(), method );
439             
440         }
441     }
442 
443 
444 }