Changes between Version 3 and Version 4 of HelloWorld


Ignore:
Timestamp:
03/24/11 09:38:29 (9 years ago)
Author:
masc01
Comment:

updated to version that works with SEMAINE-3.1

Legend:

Unmodified
Added
Removed
Modified
  • HelloWorld

    v3 v4  
    3232 1 public class HelloAnalyser extends Component { 
    3333 2  
    34  3   private XMLSender emotionSender = new XMLSender("semaine.data.hello.emotion", "EmotionML", getName()); 
     34 3   private XMLSender emotionSender =  
     35         new XMLSender("semaine.data.hello.emotion", "EmotionML", getName()); 
    3536 4  
    3637 5   public HelloAnalyser() throws JMSException { 
     
    444513     String input = m.getText(); 
    454614     if (input.contains("very")) arousalValue = 1; 
    46 15     else if (input.contains("a bit")) arousalValue = -1; 
     4715     else if (input.contains("a bit")) arousalValue = 0; 
    474816     if (input.contains("happy")) valenceValue = 1; 
    48 17     else if (input.contains("sad")) valenceValue = -1; 
     4917     else if (input.contains("sad")) valenceValue = 0; 
    495018     Document emotionML = createEmotionML(arousalValue, valenceValue); 
    505119     emotionSender.sendXML(emotionML, meta.getTime()); 
     
    525321  
    535422   private Document createEmotionML(int arousalValue, int valenceValue) { 
    54 23     Document emotionML = XMLTool.newDocument(EmotionML.ROOT_ELEMENT, EmotionML.namespaceURI); 
    55 24     Element emotion = XMLTool.appendChildElement(emotionML.getDocumentElement(), EmotionML.E_EMOTION); 
    56 25     Element dimensions = XMLTool.appendChildElement(emotion, EmotionML.E_DIMENSIONS); 
    57 26     dimensions.setAttribute(EmotionML.A_SET, "arousalValence"); 
    58 27     Element arousal = XMLTool.appendChildElement(dimensions, EmotionML.E_AROUSAL); 
     5523     Document emotionML = XMLTool.newDocument(EmotionML.ROOT_TAGNAME, 
     56                   EmotionML.namespaceURI); 
     5724     Element emotion = XMLTool.appendChildElement( 
     58                   emotionML.getDocumentElement(), EmotionML.E_EMOTION); 
     5925     emotion.setAttribute(EmotionML.A_DIMENSION_VOCABULARY, 
     60                   EmotionML.VOC_FSRE_DIMENSION_DEFINITION); 
     6126     Element arousal = XMLTool.appendChildElement(emotion, 
     62                   EmotionML.E_DIMENSION); 
     6327     arousal.setAttribute(EmotionML.A_NAME, 
     64                   EmotionML.VOC_FSRE_DIMENSION_AROUSAL); 
    596528     arousal.setAttribute(EmotionML.A_VALUE, String.valueOf(arousalValue)); 
    60 29     Element valence = XMLTool.appendChildElement(dimensions, EmotionML.E_VALENCE); 
    61 30     valence.setAttribute(EmotionML.A_VALUE, String.valueOf(valenceValue)); 
    62 31     return emotionML; 
    63 32   } 
    64 33 } 
     6629     Element valence = XMLTool.appendChildElement(emotion, 
     67                   EmotionML.E_DIMENSION); 
     6830     valence.setAttribute(EmotionML.A_NAME, 
     69                   EmotionML.VOC_FSRE_DIMENSION_VALENCE); 
     7031     valence.setAttribute(EmotionML.A_VALUE, String.valueOf(valenceValue)); 
     7132     return emotionML; 
     7233   } 
     7334 } 
    6574}}} 
    6675 
     
    7079||||||Valence|| 
    7180||||||-||0||+|| 
    72 ||Arousal||+||8-(||8-|||8-)|| 
    73 ||||0||:-(||:-|||:-)|| 
    74 ||||-||*-(||*-|||*-)|| 
     81||Arousal||+||8-(||8-| || 8-)|| 
     82||||0||:-(||:-| || :-)|| 
     83||||-||*-(||*-| ||*-)|| 
    7584 
    7685The output of the Hello system should be an emoticon representing an area in the arousal-valence plane as shown in the Table above. The !EmoticonOutput component registers an XML Receiver (l. 5) to the Topic that the !HelloAnalyser sends to. Whenever a message is received, the `react()` method is called (l. 8), which analyses the XML document in terms of EmotionML markup (l. 10-12), and extracts the arousal and valence values (l. 14-15). The emotion display is rendered as a function of these values (l. 17-19). 
     
    8493 6   } 
    8594 7  
    86  8   @Override protected void react(SEMAINEMessage m) throws MessageFormatException { 
     95 8   @Override protected void react(SEMAINEMessage m) 
     96                   throws MessageFormatException { 
    8797 9     SEMAINEXMLMessage xm = (SEMAINEXMLMessage) m; 
    88 10     Element dimensions = (Element) xm.getDocument().getElementsByTagNameNS( 
    89                                 EmotionML.namespaceURI, EmotionML.E_DIMENSIONS).item(0); 
    90 11     Element arousal = XMLTool.needChildElementByTagNameNS(dimensions, EmotionML.E_AROUSAL,  
    91                                 EmotionML.namespaceURI); 
    92 12     Element valence = XMLTool.needChildElementByTagNameNS(dimensions, EmotionML.E_VALENCE,  
    93                                 EmotionML.namespaceURI); 
    94 13 
    95 14     float a = Float.parseFloat(arousal.getAttribute(EmotionML.A_VALUE)); 
    96 15     float v = Float.parseFloat(valence.getAttribute(EmotionML.A_VALUE)); 
    97 16       
    98 17     String eyes  = a > 0.3 ? "8"/*active*/   : a < -0.3 ? "*"/*passive*/  : ":"/*neutral*/; 
    99 18     String mouth = v > 0.3 ? ")"/*positive*/ : v < -0.3 ? "("/*negative*/ : "|"/*neutral*/; 
    100 19     System.out.println(eyes+"-"+mouth); 
    101 20   } 
    102 21 } 
     9810     Element emotion = (Element) xm.getDocument().getElementsByTagNameNS( 
     99                   EmotionML.namespaceURI, EmotionML.E_EMOTION).item(0); 
     10011     String vocabularyURI = emotion.getAttribute( 
     101                   EmotionML.A_DIMENSION_VOCABULARY); 
     10212     if (!vocabularyURI.equals(EmotionML.VOC_FSRE_DIMENSION_DEFINITION)) 
     103                   return; 
     10413     List<Element> dimensions = XMLTool.getChildrenByLocalNameNS(emotion, 
     105                   EmotionML.E_DIMENSION, EmotionML.namespaceURI); 
     10614     float a = 0.5f, v = 0.5f; // neutral values 
     10715     boolean haveSomething = false; 
     10816     for (Element dim : dimensions) { 
     10917       String name = dim.getAttribute(EmotionML.A_NAME); 
     11018       float value = Float.parseFloat(dim.getAttribute(EmotionML.A_VALUE)); 
     11119       if (name.equals(EmotionML.VOC_FSRE_DIMENSION_AROUSAL)) { 
     11220         a = value; 
     11321         haveSomething = true; 
     11422       } else if (name.equals(EmotionML.VOC_FSRE_DIMENSION_VALENCE)) { 
     11523         v = value; 
     11624         haveSomething = true; 
     11725       } 
     11826     } 
     11927     if (!haveSomething) return; 
     12028     String eyes  = a > 0.66 ? "8" /*active*/   :  
     121                      a < 0.33 ? "*" /*passive*/  : 
     122                                 ":" /*neutral*/; 
     12329     String mouth = v > 0.66 ? ")" /*positive*/ :  
     124                      v < 0.33 ? "(" /*negative*/ :  
     125                                 "|" /*neutral*/; 
     12630     System.out.println(eyes+"-"+mouth); 
     12731   } 
     12832 } 
    103129}}} 
    104130 
     
    117143The system is started in the same way as all Java-based SEMAINE API systems: 
    118144 
    119 {{{activemq; java eu.semaine.system.ComponentRunner example-hello.config}}} 
     145{{{java -cp 'lib/*' eu.semaine.system.ComponentRunner config/example-hello.config}}} 
    120146 
    121147The following figure shows a screenshot of the resulting message flow graph. As the communication passes via the middleware  ActiveMQ, the system would behave in the exact same way if the four components were started as separate processes, on different machines, or if some of them were written in C++ rather than Java.