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

Quick Search    Search Deep

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


1   /* 
2    * $COPYRIGHT$
3    * $Id: Concept.java,v 1.29 2002/07/24 23:25:14 neuro Exp $
4    *
5    * Date        Author            Changes 
6    * Jul 03 2001 Remus Pereni      Created
7    */
8   package org.media.mn8;
9   
10  import org.media.mn8.concepts.StringConcept;
11  import org.media.mn8.concepts.SeriesConcept;
12  import org.media.mn8.concepts.AttributeSeries;
13  import org.media.mn8.concepts.LogicalConcept;
14  import org.media.mn8.concepts.AttributeConcept;
15  import org.media.mn8.concepts.NilConcept;
16  import org.media.mn8.concepts.Attribute;
17  import org.media.mn8.concepts.Element;
18  import org.media.mn8.concepts.Series;
19  import org.media.mn8.concepts.TypedSeries;
20  import org.media.mn8.concepts.ElementConcept;
21  import org.media.mn8.concepts.TransparentElement;
22  import org.media.mn8.event.*;
23  import org.media.mn8.util.ConceptNavPatterns;
24  
25  import java.util.Vector;
26  
27  /** 
28   * being from an mn8 script based concept or a Java one, is 
29   * registered in {@link org.media.mn8.ConceptRegister}, where it is 
30   * taken by the {@link org.media.mn8.mn8Loader} and instantiated.
31   * @author <a href="mailto:remus@nolimits.ro">Remus Pereni</a>
32   * @version $Revision: 1.29 $ $Date: 2002/07/24 23:25:14 $
33   */
34  public abstract class Concept {
35  
36  
37      protected String _conceptLabel = null;
38      protected boolean _isHidden = false;
39      protected boolean _showEmpty = true;
40      private Concept _errorHandler = null;
41      protected String _resourceURI = null;
42  
43  
44      public Concept getConceptNamed( StringConcept name ) {
45          return mn8Loader.instantiateConcept( name.toString() );
46      }
47  
48  
49      public StringConcept getConceptLabel() {
50          if( _conceptLabel == null ) {
51              _conceptLabel = getConceptDefinition().getConceptLabel().toString();
52          }
53          return new StringConcept(_conceptLabel);        
54      }
55  
56  
57      public void setConceptLabel( StringConcept label ) {
58          _conceptLabel = label.toString();
59      }
60  
61  
62      public  StringConcept getConceptType() {
63          if( getConceptDefinition() != null )
64              return getConceptDefinition().getConceptType();
65          else 
66              return new StringConcept("Concept");
67      }
68  
69  
70      public LogicalConcept isHidden () {
71          return new LogicalConcept(_isHidden);
72      }
73      
74  
75      public void setHidden(LogicalConcept isHidden) {
76          _isHidden = isHidden.getValue();
77      }
78  
79  
80      public LogicalConcept showEmpty () {
81          return new LogicalConcept(_showEmpty);
82      }
83      
84  
85      public void setShowEmpty(LogicalConcept showEmpty) {
86          _showEmpty = showEmpty.getValue();
87      }
88  
89  
90      public  SeriesConcept getConceptAttributes(){
91          AttributeSeries attributes = new AttributeSeries();
92          Vector attributeFields = getConceptAttributeFields().getVector();
93          for( int i = 0 ; i < attributeFields.size() ; i++ ) {
94              attributes.add( getConceptAttribute( ((FieldDefinition)attributeFields.elementAt(i)).getName()));
95          }
96          return attributes;        
97      }
98  
99  
100     public  LogicalConcept hasConceptAttribute( StringConcept attributeName ){
101         return new LogicalConcept(hasConceptAttribute( attributeName.toString() ));
102     }
103 
104 
105     public boolean hasConceptAttribute( String attributeName ) {
106         return getConceptDefinition().hasConceptAttribute( attributeName );
107     }
108 
109 
110     public  Concept getConceptAttribute( StringConcept attributeName ){
111         return getConceptAttribute(attributeName.toString());        
112     }
113 
114 
115     public Concept getConceptAttribute( String attributeName ) {
116 
117         if( hasConceptAttribute( attributeName )) {
118             FieldDefinition field = getConceptAttributeField( attributeName );
119             if( field != null ) {
120                 return field.getValue( this );
121             }
122         } 
123 
124         throw new AttributeNotFoundException(getConceptType().toString(), attributeName);        
125     }
126 
127 
128     public  FieldDefinition getConceptAttributeField( StringConcept attributeName ){
129         return getConceptAttributeField( attributeName.toString() );        
130     }
131 
132 
133     public  FieldDefinition getConceptAttributeField( String attributeName ){
134         return getConceptDefinition().getConceptAttributeField(attributeName );        
135     }
136 
137     
138     public SeriesConcept getConceptAttributeFields() {
139         return getConceptDefinition().getConceptAttributeFields();
140     }
141 
142 
143     public LogicalConcept hasConceptMethod( StringConcept methodSignature ) {
144         if( getConceptDefinition() != null)
145             return getConceptDefinition().hasConceptMethod( methodSignature);
146         else 
147             return LogicalConcept.FALSE;
148     }
149 
150 
151     public  SeriesConcept getConceptElements(){
152         SeriesConcept elements = new SeriesConcept();
153         Vector elementFields = getConceptElementFields().getVector();
154         for( int i =0 ; i <  elementFields.size(); i++ ) {            
155             elements.add( ((FieldDefinition)elementFields.elementAt(i)).getValue(this) );
156         }
157         return elements;        
158     }
159 
160 
161     public  LogicalConcept hasConceptElement( StringConcept elementName ){
162         return new LogicalConcept ( hasConceptElement( elementName.toString() ));        
163     }
164 
165 
166     public boolean hasConceptElement( String elementName ) {
167         return getConceptDefinition().hasConceptElement( elementName);
168     }
169 
170 
171     public  Concept getConceptElement( StringConcept elementName ){
172         return getConceptElement(elementName.toString() );        
173     }
174 
175 
176     public Concept getConceptElement( String elementName ) {
177         if( hasConceptElement( elementName )) {
178             FieldDefinition field = getConceptElementField( elementName );
179             return field.getValue( this );
180         } else {
181             throw new ElementNotFoundException(getConceptType().toString(), elementName);
182         }
183     }
184 
185 
186     public  FieldDefinition getConceptElementField( String elementName ){
187         return getConceptDefinition().getConceptElementField(elementName );        
188     }
189 
190 
191     public  SeriesConcept getConceptElementFields(){
192         return getConceptDefinition().getConceptElementFields();        
193     }
194 
195     
196     public  SeriesConcept getConceptMethods() {
197         return getConceptDefinition().getConceptMethods();
198     }
199 
200     public  SeriesConcept getConceptConstructors(){
201         return getConceptDefinition().getConceptConstructors();
202     }
203 
204 
205     public  SeriesConcept getConceptOperators(){
206         return getConceptDefinition().getConceptOperators();
207     }
208 
209 
210     public  Concept getConceptInstance(){
211         return getConceptDefinition().getConceptInstance();
212     }
213 
214 
215     public SeriesConcept getInheritedConcepts() {
216         return getConceptDefinition().getInheritedConcepts();
217     }
218 
219 
220     public SeriesConcept getAllInheritedConcepts() {
221         return getConceptDefinition().getAllInheritedConcepts();
222     }
223 
224 
225     public final LogicalConcept extendsConcept( StringConcept conceptType ) {
226         return getConceptDefinition().extendsConcept( conceptType );
227     }
228 
229 
230     public final mn8Method getConceptMethod( StringConcept methodSignature ) {
231         return getConceptDefinition().getConceptMethod( methodSignature );
232     }
233     
234 
235 
236     public abstract StringConcept toXML();
237     
238 
239     public abstract StringConcept toTXT();
240     
241 
242     public  ConceptDefinition getConceptDefinition() {
243         return ConceptConceptDefinition.getReference();
244     }
245 
246 
247     public void loadContent( Concept from ) {
248         if( from == null ) return;
249         if( mn8RuntimeFlags.isDebug() ) {
250             System.err.println("--> in concept:" + Helper.getConceptInfo(this) + "->" + Helper.getConceptInfo(from));
251         }
252 
253         Helper.loadAttributes(from, this);
254 
255         Vector elementes = from.getConceptElements().getVector();
256         ElementConcept elem = null;
257 
258         Vector localElements = getConceptElements().getVector();
259         ElementConcept localElem = null;
260 
261         outerElem:
262         for(int i = 0; i < elementes.size(); i ++ ) {
263             elem = (ElementConcept)elementes.elementAt(i);
264 
265             innerElem:
266             for( int j = 0 ; j < localElements.size(); j++) {              
267                 localElem = (ElementConcept)localElements.elementAt(j);
268 
269                 if( localElem.getLabel().toString().equalsIgnoreCase( elem.getLabel().getValue() )) {
270                     localElem.loadContent(elem);
271                     localElements.set(j, localElem);
272                     continue outerElem;
273                 }                                         
274             }
275         }        
276     }
277 
278 
279     public LogicalConcept hasPath( StringConcept path ) {
280         return hasPath( path.toString() );
281     }
282 
283 
284     public SeriesConcept getConceptsAtPath( StringConcept path ) {
285         return getConceptsAtPath(new SeriesConcept(), path.toString());
286     }
287 
288 
289     
290     public SeriesConcept getConceptsAtPath( SeriesConcept holder, String fullpath) {
291 
292         String firstItem = ConceptNavPatterns.getFirstItem( fullpath );
293         String remainingItems = ConceptNavPatterns.getRemainingItems( fullpath );
294 
295         if( ConceptNavPatterns.isAbsolutePath( firstItem ) ) {
296 
297             if( ConceptNavPatterns.isAttribute( firstItem )) {
298                 if( hasConceptAttribute(ConceptNavPatterns.getAttributeName(firstItem))){
299 
300                     if( remainingItems == null )
301                         holder.add( getAttributeValue( ConceptNavPatterns.getAttributeName(firstItem)));
302                     else 
303                         holder= subAttributeGetConcept( holder, 
304                                                         ConceptNavPatterns.getAttributeName(firstItem), 
305                                                         remainingItems);
306                 }
307             }                                                                            // is attribute
308             else { 
309                 if( areThereMoreElemenents( ConceptNavPatterns.getElementName(firstItem) ) ) {
310 
311                     Vector elm = getMeAllElements(ConceptNavPatterns.getElementName(firstItem));
312                     if( remainingItems == null ) {
313                         for ( int i=0; i < elm.size(); i ++ ) {
314                             holder.add( (Concept)  elm.elementAt(i));
315                         }
316                     } else {
317 
318                         for ( int i=0; i < elm.size(); i ++ ) {
319                             holder = ((Concept)elm.elementAt(i)).getConceptsAtPath(holder, (remainingItems.indexOf("@") == 0 ? "/" : "") + remainingItems);
320                         }
321                     }
322                 } 
323                 return holder;
324             }   
325         }
326         else {
327             if ( ConceptNavPatterns.isAttribute( firstItem )) {
328                 if( hasConceptAttribute(ConceptNavPatterns.getAttributeName(firstItem))){
329                     if( remainingItems == null ) {
330                         holder.add( getAttributeValue( ConceptNavPatterns.getAttributeName(firstItem)));
331                     } else { 
332                         holder= subAttributeGetConcept( holder, 
333                                                         ConceptNavPatterns.getAttributeName(firstItem), 
334                                                         remainingItems);
335                     }
336                 } else {
337                     holder = searchDeeperForPath(holder, fullpath);
338                 }
339             }else {
340                 if( areThereMoreElemenents( ConceptNavPatterns.getElementName(firstItem) )) {
341 
342                     Vector elm = getMeAllElements(firstItem);
343 
344                     if( remainingItems == null ) {
345                         for ( int i=0; i < elm.size(); i ++ ) {
346                             holder.add( (Concept)  elm.elementAt(i));
347                         }
348                     } else {
349                         for ( int i=0; i < elm.size(); i ++ ) {
350                             holder = ((Concept)elm.elementAt(i)).getConceptsAtPath(holder, (remainingItems.indexOf("@") == 0 ? "/" : "") + remainingItems);
351                         }
352                     }
353                 } else {
354                     holder = searchDeeperForPath(holder, fullpath);
355                 }
356                 
357             }
358         }
359         return holder;        
360     }
361 
362 
363     public SeriesConcept searchDeeperForPath(SeriesConcept holder, String path) {
364 
365         SeriesConcept elms = getConceptElements();
366         for( int i =0; i < elms.getVector().size(); i++) {
367             holder = ((Concept)elms.getVector().elementAt(i)).getConceptsAtPath( holder, path);
368         }
369         return holder;
370     }
371 
372 
373     public SeriesConcept subElementGetConcepts(SeriesConcept holder, String  elementName, String subPath) {
374         return getConceptElement( elementName).getConceptsAtPath(holder, subPath );
375     }
376 
377 
378     public SeriesConcept subAttributeGetConcept(SeriesConcept holder, String  attributeName, String subPath) {
379         return getAttributeValue( attributeName).getConceptsAtPath(holder, subPath );
380     }
381 
382 
383     /**
384      * Returns true if the concept has the specified path or not.
385      * By overriding getConceptElements or even this method elements and
386      * attributes can dynamically be added to concepts, in fact this is 
387      * the reason why it is also duplicate here.
388      * <p> The path can be given using standard mn8 navigational 
389      * patterns <tt>(/)? (elementName)? (/elementName)* (@ attributeName)?</tt>.
390      * @param fullpath the path to be looked for.
391      * @return true if it exists false otherway.
392      */
393     public LogicalConcept hasPath( String fullpath ) {
394 
395         String firstItem = ConceptNavPatterns.getFirstItem( fullpath );
396         String remainingItems = ConceptNavPatterns.getRemainingItems( fullpath );
397 
398         if( ConceptNavPatterns.isAbsolutePath( firstItem ) ) {
399 
400             if( ConceptNavPatterns.isAttribute( firstItem )) {
401                 if( !hasConceptAttribute(new StringConcept(ConceptNavPatterns.getAttributeName(firstItem))).getValue())
402                 return LogicalConcept.FALSE;
403 
404                 if( remainingItems == null ) return LogicalConcept.TRUE;
405                 else return subAttributeHasPath( ConceptNavPatterns.getAttributeName(firstItem),
406                                                  remainingItems);
407             }                                                                            // is attribute
408             else { 
409                 if ( !hasConceptElement(ConceptNavPatterns.getElementName(firstItem))){
410                     return LogicalConcept.FALSE;
411                 }
412                 if( remainingItems == null ) {
413                     return LogicalConcept.TRUE;
414                 }
415                 else {
416                     return subElementHasPath( ConceptNavPatterns.getElementName(firstItem),
417                                               remainingItems);
418                 }
419             }                                                                             // is element
420         }                                                                                 // is absolute path
421         else {
422             if ( ConceptNavPatterns.isAttribute( firstItem )) {
423                 if( hasConceptAttribute(new StringConcept(ConceptNavPatterns.getAttributeName(firstItem))).getValue())
424                     if( remainingItems == null ) return LogicalConcept.TRUE;            
425                 return searchSubElements(fullpath); 
426             }
427             else {
428                 if( hasConceptElement(ConceptNavPatterns.getElementName(firstItem))) {
429                     if( remainingItems == null ){
430                         return LogicalConcept.TRUE;            
431                     } else { 
432                         return subElementHasPath( ConceptNavPatterns.getElementName(firstItem), remainingItems);
433                     }
434                 }
435                 //we still don't have it but maybe some element does                            
436                 return searchSubElements( fullpath );
437             }
438         }
439     }
440 
441 
442     
443     /**
444      * Searches for the respective path in all the elements of this concept.
445      * @param path The path to be looked for corresponding to {@link org.media.mn8.util.ConceptNavPatterns}
446      * @return A {@link LogicalConcept} true if the path was found, false otherway.
447      * @see org.media.mn8.util.ConceptNavPatterns
448      */
449     protected LogicalConcept searchSubElements(String path) {
450         SeriesConcept elements  = getConceptElements();
451         LogicalConcept result = LogicalConcept.FALSE;
452         ElementConcept elem = null;
453         for( int i = 0 ; i < elements.getVector().size() ; i ++ ){
454                 result = subElementHasPath( ((ElementConcept)elements.getVector().elementAt(i)).getName().toString() , path );
455                 if( result.getValue() ) return result;
456         }                 
457         return result;
458     }
459     
460 
461     /**
462      * Returns if an element of this concept has the subpath specified as parameter
463      * @param elementName the name of the element
464      * @param subPath the subpath to be looked for
465      * @return true if it has it false otherway
466      */
467     public LogicalConcept subElementHasPath(String elementName, String subPath) {
468 
469         ElementConcept elem;
470         if( hasConceptElement( elementName ) ) {
471             elem = (ElementConcept) getConceptElement( elementName );
472         } else {
473             return LogicalConcept.FALSE;        
474         }
475         return elem.hasPath(subPath);        
476     }
477 
478 
479     /**
480      * Returns if an attribute of this concept has the subpath specified as parameter
481      * @param elementName the name of the element
482      * @param subPath the subpath to be looked for
483      * @return true if it has it false otherway
484      */
485     protected LogicalConcept subAttributeHasPath(String attribName, String subPath) {
486         return getConceptAttribute( attribName).hasPath( subPath );
487     }
488 
489 
490     public StringConcept getResourceURI() {
491         if( _resourceURI != null ) {
492             return new StringConcept( _resourceURI );
493         } else {
494             return new StringConcept();
495         }
496     }
497 
498 
499     public void setResourceURI(StringConcept uri) {
500         if( uri != null && uri.getValue() != null ) {
501             _resourceURI = uri.getValue();
502         } 
503     }
504     
505     
506     protected Concept getAttributeValue( String name ) {
507         Concept concept =  getConceptAttribute( name );
508         if (concept instanceof AttributeConcept) {
509             return concept;
510         }
511         return new NilConcept();
512     }
513 
514 
515     public boolean areThereMoreElemenents(String path ) {
516         return hasConceptElement(new StringConcept(path)).getValue();
517     }
518 
519     
520     public Vector getMeAllElements( String path ) {
521         Concept result = getConceptElement(path);
522         SeriesConcept elements ;
523 
524         if( result instanceof TransparentElement && ((TransparentElement)result).getValue() instanceof SeriesConcept ) {
525             elements = (SeriesConcept) ((TransparentElement)result).getValue();
526         } else {
527             elements = new SeriesConcept();
528             elements.add(result);
529         }
530 
531         return elements.getVector();
532     }
533 
534 
535     public abstract Object clone();
536 
537     
538     public Concept cloneConcept() {
539         return (Concept) clone();
540     }
541 
542 
543     public void setErrorHandler( Concept handler ) {
544         _errorHandler = handler;
545     }
546 
547 
548     public Concept getErrorHandler() {
549         return _errorHandler;
550     }
551 
552 
553 
554     public void printFields() {
555         printFields("", this);
556     }
557 
558     public void printFields( String pre, Concept con ) {
559         pre = "  " + pre;
560         FieldConcept field = null;
561         ElementConcept elem = null;
562         ElementConcept innerElem = null;
563         for( int i = 0 ; i < con.getConceptElementFields().getVector().size(); i++) {
564             
565             field = (FieldConcept)con.getConceptElementFields().getVector().elementAt(i);
566             elem =  (ElementConcept)con.getConceptElements().getVector().elementAt(i);
567 
568             String multi = "";
569 
570             if( elem.isMulti().getValue() ) {
571                 multi = "*";
572             }
573 
574             if ( elem instanceof TransparentElement && !elem.getValueType().toString().equalsIgnoreCase("element")) {
575                 System.err.println(pre + field.toTXT().toString() + multi  + "(" 
576                                    + elem.getClass().getName()
577                                    + "|" + elem.getName() + "|" + elem.getLabel() + "|" + elem.getValueType()
578                                    + "|" + elem.getValue().getClass().getName()
579                                    + ( elem.getValue() instanceof TypedSeries ? ((TypedSeries)elem.getValue()).getType().toString() : "")
580                                    );
581             } else if ( elem.getValue() instanceof ElementConcept ) {
582                 innerElem = (ElementConcept) elem.getValue();
583                 System.err.println(pre + field.toTXT().toString()  +  multi + "(" 
584                                    + innerElem.getClass().getName()
585                                    + "|" + innerElem.getName() + "|" + innerElem.getLabel() + "|" + innerElem.getValueType()
586                                    + "|" + innerElem.getValue().getClass().getName()
587                                    );
588             }
589             
590             printFields(pre, elem.getValue());
591         }
592     }
593 
594 }