View Javadoc

1   package cz.cuni.amis.pogamut.sposh.executor;
2   
3   import cz.cuni.amis.pogamut.sposh.executor.IAction;
4   import cz.cuni.amis.pogamut.sposh.executor.ISense;
5   import cz.cuni.amis.pogamut.sposh.executor.ParamInfo;
6   import cz.cuni.amis.pogamut.sposh.executor.PrimitiveInfo;
7   import java.util.Collections;
8   import java.util.HashSet;
9   import java.util.Set;
10  
11  /**
12   * Exchange container used for passing info between crawler and explorer.
13   * Contains information about primitive {@link IAction} and {@link ISense}, i.e.
14   * what is stored in {@link PrimitiveInfo} and the parameters of the primitive.
15   *
16   * @author Honza
17   */
18  public final class PrimitiveData implements Comparable<PrimitiveData> {
19  
20      /**
21       * Fully qualified name of the primitive class
22       */
23      public final String classFQN;
24      /**
25       * Name of the primitive from {@link PrimitiveInfo}, not necessary unique.
26       * Is null, if no {@link PrimitiveInfo} annotation used.
27       */
28      public final String name;
29      /**
30       * Description of the primitive from {@link PrimitiveInfo}, can be null if
31       * no annotation.
32       */
33      public final String description;
34      /**
35       * Tags for primitive
36       */
37      public final String[] tags;
38      /**
39       * Parameters used by the primitive
40       */
41      public final Set<ParamInfo> params;
42  
43      /**
44       * Create {@link PrimitiveData} about some primitive that does't have {@link PrimitiveInfo}
45       * annotation.
46       *
47       * @param classFQN fully qualified name of the primitive class.
48       */
49      public PrimitiveData(String classFQN) {
50          this(classFQN, null, null, new String[0], Collections.<ParamInfo>emptySet());
51      }
52  
53      /**
54       * Create new instance of PrimitiveData
55       *
56       * @param classFQN fully qualified name of the primitive class.
57       * @param name name of primitive from {@link PrimitiveInfo}, can be null.
58       * @param description description of primitive from {@link PrimitiveInfo},
59       * can be null.
60       * @param tags tags of the primitive
61       * @param parameters set of all parameters. Deep copy made.
62       */
63      public PrimitiveData(String classFQN, String name, String description, String[] tags, Set<ParamInfo> parameters) {
64          this.classFQN = classFQN;
65          this.name = name;
66          this.description = description;
67          this.tags = tags;
68          this.params = Collections.unmodifiableSet(new HashSet<ParamInfo>(parameters));
69      }
70  
71      /**
72       * Get simple class name (not FQN)
73       *
74       * @return simple name of classFQN
75       */
76      public String getClassName() {
77          return classFQN.replaceFirst("^.*\\.", "");
78      }
79  
80      /**
81       * Compare this data to another. First ignorcase-compare of names, if same,
82       * compare FQN of primitives.
83       *
84       * @param o The other comparison object
85       * @return
86       */
87      @Override
88      public int compareTo(PrimitiveData o) {
89          if (this == o) {
90              return 0;
91          }
92  
93          String myName = name != null ? name : getClassName();
94          String oName = o.name != null ? o.name : o.getClassName();
95  
96          int nameComparison = myName.toLowerCase().compareTo(oName.toLowerCase());
97          if (nameComparison != 0) {
98              return nameComparison;
99          }
100 
101         return classFQN.compareTo(o.classFQN);
102     }
103 
104     @Override
105     public String toString() {
106         return (name != null ? name : getClassName()) + "(" + classFQN + ")";
107     }
108 
109     /**
110      * Get HTML description of the metadata of this object.
111      * @return HTML description of the object.
112      */
113     public String getHtmlDescription() {
114         StringBuilder info = new StringBuilder();
115         info.append("<html><b>Class:</b> ");
116         info.append(classFQN);
117         if (name != null) {
118             info.append("<br/><b>Name:</b> ");
119             info.append(name);
120         }
121         if (description != null) {
122             info.append("<br/><b>Description:</b> ");
123             info.append(description);
124         }
125         if (tags.length > 0) {
126             info.append("<br/><b>Tags:</b> ");
127             for (int tagIndex = 0; tagIndex < tags.length; ++tagIndex) {
128                 info.append(tags[tagIndex]);
129                 if (tagIndex != tags.length - 1) {
130                     info.append(',');
131                     info.append(' ');
132                 }
133             }
134         }
135         info.append("</html>");
136         return info.toString();
137     }
138 }