Coverage Report - org.kuali.student.core.statement.naturallanguage.translators.StatementTranslator
 
Classes in this File Line Coverage Branch Coverage Complexity
StatementTranslator
81%
26/32
100%
4/4
2.143
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 14  
  */
 15  
 
 16  
 package org.kuali.student.core.statement.naturallanguage.translators;
 17  
 
 18  
 import java.util.List;
 19  
 import java.util.Locale;
 20  
 
 21  
 import org.kuali.student.common.messagebuilder.booleanmessage.BooleanMessage;
 22  
 import org.kuali.student.common.messagebuilder.booleanmessage.MessageContainer;
 23  
 import org.kuali.student.common.messagebuilder.booleanmessage.ast.BooleanMessageImpl;
 24  
 import org.kuali.student.core.statement.entity.Statement;
 25  
 import org.kuali.student.core.statement.naturallanguage.util.ReqComponentReference;
 26  
 import org.kuali.student.core.exceptions.DoesNotExistException;
 27  
 import org.kuali.student.core.exceptions.OperationFailedException;
 28  
 import org.slf4j.Logger;
 29  
 import org.slf4j.LoggerFactory;
 30  
 
 31  
 /**
 32  
  * This class translates a LU (learning unit) statement into a specific 
 33  
  * natural language.  This class is not thread safe.
 34  
  */
 35  
 public class StatementTranslator {
 36  
     /** SLF4J logging framework */
 37  1
     final static Logger logger = LoggerFactory.getLogger(StatementTranslator.class);
 38  
 
 39  
     private String language;
 40  30
         private StatementParser statementParser = new StatementParser("*", "+");
 41  
         private ReqComponentTranslator reqComponentTranslator;
 42  
         private NaturalLanguageMessageBuilder messageBuilder;
 43  
 //    private ContextRegistry<Context<StatementAnchor>> contextRegistry;
 44  
 //    private TemplateTranslator templateTranslator = new TemplateTranslator();
 45  
     
 46  
         /**
 47  
          * Constructs a new natural language translator in the 
 48  
          * default language locale.
 49  
          */
 50  30
         public StatementTranslator() {
 51  30
                 this.language = Locale.getDefault().getLanguage();
 52  30
     }
 53  
 
 54  
         /**
 55  
          * Sets the translation language.
 56  
          * 
 57  
          * @param language Translation language
 58  
          */
 59  
         public void setLanguage(final String language) {
 60  32
                 this.language = language;
 61  32
         }
 62  
 
 63  
         /**
 64  
          * Sets the requirement component translator.
 65  
          * 
 66  
          * @param reqComponentTranslator Requirement component translator
 67  
          */
 68  
         public void setReqComponentTranslator(final ReqComponentTranslator reqComponentTranslator) {
 69  30
                 this.reqComponentTranslator = reqComponentTranslator;
 70  30
         }
 71  
 
 72  
         /**
 73  
          * Sets the message builder.
 74  
          * 
 75  
          * @param messageBuilder Message builder
 76  
          */
 77  
     public void setMessageBuilder(final NaturalLanguageMessageBuilder messageBuilder) {
 78  30
                 this.messageBuilder = messageBuilder;
 79  30
     }
 80  
 
 81  
         /**
 82  
          * Translates a statement in the default language for a specific natural 
 83  
          * language usuage type (context) into natural language.
 84  
          * This method is not thread safe.
 85  
          * 
 86  
          * @param statement Statement
 87  
          * @param nlUsageTypeKey Usuage type key (context)
 88  
          * @return Natural language statement translation
 89  
          * @throws DoesNotExistException CLU or statement id does not exists
 90  
          * @throws OperationFailedException Translation failed
 91  
          */
 92  
         public String translate(final Statement statement, final String nlUsageTypeKey) throws DoesNotExistException, OperationFailedException {
 93  18
                 return translate(statement, nlUsageTypeKey, this.language);
 94  
         }
 95  
         
 96  
         /**
 97  
          * Translates a statement for a specific natural language usuage 
 98  
          * type (context) into natural language.
 99  
          * This method is not thread safe.
 100  
          * 
 101  
          * @param language Language translation
 102  
          * @param statement Statement
 103  
          * @param nlUsageTypeKey Usuage type key (context)
 104  
          * @return Natural language statement translation
 105  
          * @throws DoesNotExistException CLU or statement id does not exists
 106  
          * @throws OperationFailedException Translation failed
 107  
          */
 108  
         public String translate(final Statement statement, final String nlUsageTypeKey, final String language) throws DoesNotExistException, OperationFailedException {
 109  21
                 if(statement == null) {
 110  1
                     throw new DoesNotExistException("Statement cannot be null");
 111  
                 }
 112  
 
 113  
                 try {
 114  20
                         String booleanExpression = this.statementParser.getBooleanExpressionAsReqComponents(statement);
 115  20
                         List<ReqComponentReference> reqComponentList = this.statementParser.getLeafReqComponents(statement);
 116  20
                         String message = buildMessage(language, nlUsageTypeKey, booleanExpression, reqComponentList);
 117  
 //                String header = "";
 118  
 //                if(cluId != null && !cluId.isEmpty()) {
 119  
 //                        header = getHeader(statement, nlUsageTypeKey, cluId);
 120  
 //                }
 121  
 //                String header = getHeader(statement, nlUsageTypeKey);
 122  
 //                
 123  
 //                return header + message;
 124  20
                         return message;
 125  0
                 } catch (DoesNotExistException e) {
 126  0
                         logger.error(e.getMessage(), e);
 127  0
                         throw e;
 128  0
                 } catch (OperationFailedException e) {
 129  0
                         logger.error(e.getMessage(), e);
 130  0
                         throw e;
 131  
                 }
 132  
         }
 133  
 
 134  
         /**
 135  
          * Translates a statement directly attached to a CLU for a specific natural 
 136  
          * language usuage type (context) into natural language tree structure.
 137  
          * 
 138  
          * @param cluId Clu anchor
 139  
          * @param luStatement LU statement
 140  
          * @param nlUsageTypeKey Natural language usage type key (context)
 141  
          * @return Natural language root tree node
 142  
          * @throws DoesNotExistException CLU or statement does not exist
 143  
          * @throws OperationFailedException Translation failed
 144  
          */
 145  
 /*        public NLTranslationNodeInfo translateToTree(final String cluId, final CustomLuStatementInfo luStatement, final String nlUsageTypeKey) throws DoesNotExistException, OperationFailedException {
 146  
                 if(luStatement == null) {
 147  
 //                        return null;
 148  
                     throw new DoesNotExistException("LuStatement cannot be null");
 149  
                 }
 150  
 
 151  
                 String booleanExpression = statementParser.getBooleanExpressionAsReqComponents(luStatement);
 152  
                 String operator = (luStatement.getOperator() == null ? null : luStatement.getOperator().toString());
 153  
                 String booleanId = statementParser.getIdMap().get(luStatement.getId());
 154  
                 NLTranslationNodeInfo rootNode = new NLTranslationNodeInfo(luStatement.getId(), booleanId, operator);
 155  
                 rootNode.setBooleanExpression(booleanExpression);
 156  
 
 157  
                 createStatementTree(luStatement, rootNode, nlUsageTypeKey);
 158  
 
 159  
                 String translation = translate(cluId, luStatement, nlUsageTypeKey);
 160  
                 rootNode.setNLTranslation(translation);
 161  
 
 162  
                 return rootNode;
 163  
         }
 164  
         
 165  
         /**
 166  
          * Builds the full translated message.
 167  
          * 
 168  
          * @param nlUsageTypeKey Usuage type key
 169  
          * @param booleanExpression Boolean expression
 170  
          * @param reqComponentList Requirement component list
 171  
          * @return Translated message
 172  
          * @throws DoesNotExistException Requirement component does not exist
 173  
          * @throws OperationFailedException Translation failed
 174  
          */
 175  
         private String buildMessage(String language, String nlUsageTypeKey, String booleanExpression, List<ReqComponentReference> reqComponentList) throws DoesNotExistException, OperationFailedException {
 176  20
                 MessageContainer messageContainer = new MessageContainer();
 177  20
                 for(ReqComponentReference reqComponent : reqComponentList) {
 178  40
                         String translation = this.reqComponentTranslator.translate(reqComponent.getReqComponent(), nlUsageTypeKey, language);
 179  40
                         BooleanMessage bm = new BooleanMessageImpl(reqComponent.getBooleanId(), true, translation);
 180  40
                         messageContainer.addMessage(bm);
 181  40
                 }
 182  
                 
 183  20
                 String message = this.messageBuilder.buildMessage(language, booleanExpression, messageContainer);
 184  20
                 return message;
 185  
         }
 186  
         
 187  
         /**
 188  
          * Gets header for root <code>luStatement</code>.
 189  
          * 
 190  
          * @param statement LU statement
 191  
          * @param nlUsageTypeKey Natural language usuage type context key
 192  
          * @return Statement header
 193  
          * @throws DoesNotExistException CLU or header template does not exist
 194  
          */
 195  
         /*private String getHeader(Statement statement, String nlUsageTypeKey) throws OperationFailedException, DoesNotExistException {
 196  
         String template = getHeaderTemplate(statement, nlUsageTypeKey);
 197  
                 
 198  
         Map<String, Object> contextMap = buildContextMap(statement);
 199  
         String header = this.templateTranslator.translate(contextMap, template);
 200  
         
 201  
         return header;
 202  
         }*/
 203  
 
 204  
     /**
 205  
      * Builds a statement type context map.
 206  
      * 
 207  
          * @param statement Statement
 208  
          * @return Context map 
 209  
          * @throws DoesNotExistException
 210  
          */
 211  
         /*private Map<String, Object> buildContextMap(Statement statement) throws OperationFailedException, DoesNotExistException {
 212  
                 RefStatementRelation refStmtRel = statement.getRefStatementRelations().get(0);
 213  
                 if(refStmtRel == null) {
 214  
                 throw new DoesNotExistException("Reference statement relation not found for statement id: " + statement.getId());
 215  
                 }
 216  
 
 217  
                 StatementAnchor lua = new StatementAnchor(statement, refStmtRel.getRefObjectTypeKey(), refStmtRel.getRefObjectId());
 218  
 
 219  
                 String statementTypeKey = statement.getStatementType().getId();
 220  
                 Context<StatementAnchor> context = this.contextRegistry.get(statementTypeKey);
 221  
             if(context == null) {
 222  
                 throw new DoesNotExistException("Header context not found in registry for statement type key: " + statementTypeKey);
 223  
             }
 224  
                 Map<String, Object> contextMap = context.createContextMap(lua);
 225  
             
 226  
         return contextMap;
 227  
         }*/
 228  
 
 229  
         /**
 230  
          * Gets header template for root <code>statement</code>.
 231  
          * 
 232  
          * @param statement LU statement
 233  
          * @param nlUsageTypeKey Natural language usuage type context key
 234  
          * @return Header template
 235  
          * @throws DoesNotExistException Header template does not exist
 236  
          */
 237  
         /*private String getHeaderTemplate(Statement statement, String nlUsageTypeKey) throws DoesNotExistException {
 238  
                 for(StatementTypeHeaderTemplate header : statement.getStatementType().getStatementHeaders()) {
 239  
                         if(header.getNlUsageTypeKey().equals(nlUsageTypeKey) && header.getLanguage().equals(this.language)) {
 240  
                                 return (header.getTemplate() == null ? "" : header.getTemplate());
 241  
                         }
 242  
                 }
 243  
         throw new DoesNotExistException("Natural language usage type key '" + nlUsageTypeKey + "'" +
 244  
                         " and language code '" + this.language + "' for statement type header not found");
 245  
         }*/
 246  
 
 247  
         /**
 248  
          * Create a statement tree as <code>NLTranslationNodeInfo</code>.
 249  
          * 
 250  
          * @param luStatement LU statement
 251  
          * @param rootNode Root node to translate to
 252  
          * @param nlUsageTypeKey Natural language usuage type context key
 253  
          * @throws DoesNotExistException Requirement component does not exist
 254  
          * @throws OperationFailedException Translation failed
 255  
          */
 256  
 /*        private void createStatementTree(CustomLuStatementInfo luStatement, NLTranslationNodeInfo rootNode, String nlUsageTypeKey) 
 257  
                 throws DoesNotExistException, OperationFailedException {
 258  
                 if (luStatement.getChildren() == null || luStatement.getChildren().isEmpty()) {
 259  
                         List<NLTranslationNodeInfo> children = getReqComponents(luStatement.getRequiredComponents(), nlUsageTypeKey);
 260  
                         rootNode.setChildNodes(children);
 261  
                         rootNode.setNLTranslation(getNLTranslation(children, rootNode.getOperator()));
 262  
                         return;
 263  
                 }
 264  
 
 265  
                 for(Iterator<CustomLuStatementInfo> it = luStatement.getChildren().iterator(); it.hasNext();) {
 266  
                         CustomLuStatementInfo stmt = it.next();
 267  
                         String operator = (stmt.getOperator() == null ? null : stmt.getOperator().toString());
 268  
                         String booleanId = statementParser.getIdMap().get(stmt.getId());
 269  
                         NLTranslationNodeInfo node = new NLTranslationNodeInfo(stmt.getId(), booleanId, operator);
 270  
                         node.setParentNode(rootNode);
 271  
                         rootNode.addChildNode(node);
 272  
                         if (stmt.getChildren() == null || stmt.getChildren().isEmpty()) {
 273  
                                 List<NLTranslationNodeInfo> children = getReqComponents(stmt.getRequiredComponents(), nlUsageTypeKey);
 274  
                                 node.setChildNodes(children);
 275  
                                 node.setNLTranslation(getNLTranslation(children, node.getOperator()));
 276  
                         } else {
 277  
                                 createStatementTree(stmt, node, nlUsageTypeKey);
 278  
                         }
 279  
                 }
 280  
         }*/
 281  
         
 282  
         /**
 283  
          * Gets the node's natural language translation.
 284  
          * 
 285  
          * @param children Nodes children
 286  
          * @param operator Boolean operator
 287  
          * @return Node's natural language translation
 288  
          */
 289  
 /*        private String getNLTranslation(List<NLTranslationNodeInfo> children, String operator) {
 290  
                 StringBuilder sb = new StringBuilder();
 291  
                 for(Iterator<NLTranslationNodeInfo> it = children.iterator(); it.hasNext();) {
 292  
                         NLTranslationNodeInfo node = it.next();
 293  
                         sb.append(node.getNLTranslation());
 294  
                         if(it.hasNext()) {
 295  
                                 sb.append(" ");
 296  
                                 sb.append(operator.toLowerCase());
 297  
                                 sb.append(" ");
 298  
                         }
 299  
                 }
 300  
                 return sb.toString();
 301  
         }*/
 302  
 
 303  
         /**
 304  
          * Gets the requirement components as a list of translated nodes.
 305  
          * 
 306  
          * @param reqComponentList Requirement component list
 307  
          * @param nlUsageTypeKey Usuage type key (context)
 308  
          * @return List of translated nodes
 309  
          * @throws DoesNotExistException Requirement component does not exist
 310  
          * @throws OperationFailedException Translation fails
 311  
          */
 312  
 /*        private List<NLTranslationNodeInfo> getReqComponents(List<CustomReqComponentInfo> reqComponentList, String nlUsageTypeKey) 
 313  
                 throws DoesNotExistException, OperationFailedException {
 314  
                 List<NLTranslationNodeInfo> list = new ArrayList<NLTranslationNodeInfo>(reqComponentList.size());
 315  
                 for(CustomReqComponentInfo reqComp : reqComponentList) {
 316  
                         String translation = this.reqComponentTranslator.translate(reqComp, nlUsageTypeKey);
 317  
                         String booleanId = statementParser.getIdMap().get(reqComp.getId());
 318  
                         NLTranslationNodeInfo node = new NLTranslationNodeInfo(reqComp.getId(), booleanId, null);
 319  
                         node.setNLTranslation(translation);
 320  
                         list.add(node);
 321  
                 }
 322  
                 return list;
 323  
         }*/
 324  
 }