| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.student.core.statement.naturallanguage.translators; |
| 17 | |
|
| 18 | |
import java.util.HashMap; |
| 19 | |
import java.util.List; |
| 20 | |
import java.util.Locale; |
| 21 | |
import java.util.Map; |
| 22 | |
|
| 23 | |
import org.kuali.student.common.exceptions.DoesNotExistException; |
| 24 | |
import org.kuali.student.common.exceptions.OperationFailedException; |
| 25 | |
import org.kuali.student.core.statement.dto.ReqComponentInfo; |
| 26 | |
import org.kuali.student.core.statement.entity.ReqComponent; |
| 27 | |
import org.kuali.student.core.statement.entity.ReqComponentType; |
| 28 | |
import org.kuali.student.core.statement.entity.ReqComponentTypeNLTemplate; |
| 29 | |
import org.kuali.student.core.statement.naturallanguage.Context; |
| 30 | |
import org.kuali.student.core.statement.naturallanguage.ContextRegistry; |
| 31 | |
import org.kuali.student.core.statement.service.impl.StatementAssembler; |
| 32 | |
import org.slf4j.Logger; |
| 33 | |
import org.slf4j.LoggerFactory; |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
public class ReqComponentTranslator { |
| 40 | |
|
| 41 | 1 | private final static Logger logger = LoggerFactory.getLogger(ReqComponentTranslator.class); |
| 42 | |
|
| 43 | |
private String language; |
| 44 | |
private ContextRegistry<Context<ReqComponentInfo>> contextRegistry; |
| 45 | 32 | private TemplateTranslator templateTranslator = new TemplateTranslator(); |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | 32 | public ReqComponentTranslator() { |
| 52 | 32 | this.language = Locale.getDefault().getLanguage(); |
| 53 | 32 | } |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
public void setLanguage(final String language) { |
| 61 | 34 | this.language = language; |
| 62 | 34 | } |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
public void setContextRegistry(final ContextRegistry<Context<ReqComponentInfo>> contextRegistry) { |
| 70 | 32 | this.contextRegistry = contextRegistry; |
| 71 | 32 | } |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
public String translate(final ReqComponent reqComponent, final String nlUsageTypeKey) throws DoesNotExistException, OperationFailedException { |
| 89 | 20 | return translate(reqComponent, nlUsageTypeKey, this.language); |
| 90 | |
} |
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
public String translate(final ReqComponent reqComponent, final String nlUsageTypeKey, final String language) throws DoesNotExistException, OperationFailedException { |
| 110 | 67 | if(reqComponent == null) { |
| 111 | 2 | throw new DoesNotExistException("ReqComponent cannot be null"); |
| 112 | |
} |
| 113 | |
|
| 114 | 65 | ReqComponentType reqComponentType = reqComponent.getRequiredComponentType(); |
| 115 | 65 | ReqComponentInfo reqComponentInfo = StatementAssembler.toReqComponentInfo(reqComponent); |
| 116 | 65 | Map<String, Object> contextMap = buildContextMap(reqComponentInfo); |
| 117 | 64 | ReqComponentTypeNLTemplate template = getTemplate(reqComponentType, nlUsageTypeKey, language); |
| 118 | |
|
| 119 | |
try { |
| 120 | 62 | String nl = this.templateTranslator.translate(contextMap, template.getTemplate()); |
| 121 | |
|
| 122 | 62 | if(logger.isInfoEnabled()) { |
| 123 | 0 | logger.info("nl="+nl); |
| 124 | |
} |
| 125 | |
|
| 126 | 62 | return nl; |
| 127 | 0 | } catch (OperationFailedException e) { |
| 128 | 0 | String msg = "Generating template for requirement component failed: "+reqComponent; |
| 129 | 0 | logger.error(msg, e); |
| 130 | 0 | throw new OperationFailedException(msg); |
| 131 | |
} |
| 132 | |
} |
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
private Map<String, Object> buildContextMap(ReqComponentInfo reqComponent) throws DoesNotExistException, OperationFailedException { |
| 142 | 65 | List<Context<ReqComponentInfo>> contextList = this.contextRegistry.get(reqComponent.getType()); |
| 143 | 65 | if(contextList == null || contextList.isEmpty()) { |
| 144 | 1 | throw new DoesNotExistException("Requirement component context not found in registry for requirement component type id: " + reqComponent.getType()); |
| 145 | |
} |
| 146 | 64 | Map<String, Object> contextMap = new HashMap<String, Object>(); |
| 147 | 64 | for(Context<ReqComponentInfo> context : contextList) { |
| 148 | 66 | Map<String, Object> cm = context.createContextMap(reqComponent); |
| 149 | 66 | contextMap.putAll(cm); |
| 150 | 66 | } |
| 151 | |
|
| 152 | 64 | if(logger.isInfoEnabled()) { |
| 153 | 0 | logger.info("contextMap="+contextMap); |
| 154 | |
} |
| 155 | |
|
| 156 | 64 | return contextMap; |
| 157 | |
} |
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
private ReqComponentTypeNLTemplate getTemplate(ReqComponentType reqComponentType, String nlUsageTypeKey, String language) throws DoesNotExistException { |
| 170 | 64 | List<ReqComponentTypeNLTemplate> templateList = reqComponentType.getNlUsageTemplates(); |
| 171 | 64 | for (ReqComponentTypeNLTemplate template : templateList) { |
| 172 | 78 | if (nlUsageTypeKey.equals(template.getNlUsageTypeKey()) && language.equals(template.getLanguage())) { |
| 173 | |
|
| 174 | 62 | if(logger.isInfoEnabled()) { |
| 175 | 0 | logger.info("template="+template); |
| 176 | |
} |
| 177 | |
|
| 178 | 62 | return template; |
| 179 | |
} |
| 180 | |
} |
| 181 | 2 | throw new DoesNotExistException("Natural language usage type key '" + nlUsageTypeKey + "'" + |
| 182 | |
" and language code '" + language + "' for requirement component type " + reqComponentType.getId() + " template not found"); |
| 183 | |
} |
| 184 | |
} |