<commit_before>/*
 * Copyright 2011 The Kuali Foundation
 *
 * Licensed under the Educational Community License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.opensource.org/licenses/ecl1.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kuali.rice.krms.impl.provider.repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.kuali.rice.krms.api.engine.TermResolver;
import org.kuali.rice.krms.api.repository.ActionDefinition;
import org.kuali.rice.krms.api.repository.AgendaDefinition;
import org.kuali.rice.krms.api.repository.AgendaTreeDefinition;
import org.kuali.rice.krms.api.repository.AgendaTreeEntryDefinition;
import org.kuali.rice.krms.api.repository.AgendaTreeRuleEntry;
import org.kuali.rice.krms.api.repository.AgendaTreeSubAgendaEntry;
import org.kuali.rice.krms.api.repository.ContextDefinition;
import org.kuali.rice.krms.api.repository.PropositionDefinition;
import org.kuali.rice.krms.api.repository.RepositoryDataException;
import org.kuali.rice.krms.api.repository.RuleDefinition;
import org.kuali.rice.krms.api.repository.RuleRepositoryService;
import org.kuali.rice.krms.api.repository.TermResolverDefinition;
import org.kuali.rice.krms.api.type.KrmsTypeDefinition;
import org.kuali.rice.krms.api.type.KrmsTypeRepositoryService;
import org.kuali.rice.krms.framework.engine.Action;
import org.kuali.rice.krms.framework.engine.Agenda;
import org.kuali.rice.krms.framework.engine.AgendaTree;
import org.kuali.rice.krms.framework.engine.AgendaTreeEntry;
import org.kuali.rice.krms.framework.engine.BasicAgenda;
import org.kuali.rice.krms.framework.engine.BasicAgendaTree;
import org.kuali.rice.krms.framework.engine.BasicAgendaTreeEntry;
import org.kuali.rice.krms.framework.engine.BasicContext;
import org.kuali.rice.krms.framework.engine.BasicRule;
import org.kuali.rice.krms.framework.engine.Context;
import org.kuali.rice.krms.framework.engine.Proposition;
import org.kuali.rice.krms.framework.engine.Rule;
import org.kuali.rice.krms.framework.engine.SubAgenda;
import org.kuali.rice.krms.framework.type.TermResolverTypeService;
import org.kuali.rice.krms.impl.repository.TermBoService;
import org.kuali.rice.krms.impl.type.KrmsTypeResolver;
import org.springframework.util.CollectionUtils;

/**
 * TODO... 
 * 
 * @author Kuali Rice Team (<EMAIL>)
 *
 */
public class RepositoryToEngineTranslatorImpl implements RepositoryToEngineTranslator {

	private RuleRepositoryService ruleRepositoryService;
	private KrmsTypeRepositoryService typeRepositoryService;
	private KrmsTypeResolver typeResolver;
	private TermBoService termBoService;
	
	@Override
	public Context translateContextDefinition(ContextDefinition contextDefinition) {
		if (contextDefinition == null) {
			return null;
		}
		List<Agenda> agendas = new ArrayList<Agenda>();
		for (AgendaDefinition agendaDefinition : contextDefinition.getAgendas()) {
			Agenda agenda = translateAgendaDefinition(agendaDefinition);
			agendas.add(agenda);
		}
		
		List<TermResolverDefinition> termResolverDefs = 
			termBoService.getTermResolversByContextId(contextDefinition.getContextDefinitionId());
		
		List<TermResolver<?>> termResolvers = new ArrayList<TermResolver<?>>();

		if (!CollectionUtils.isEmpty(termResolverDefs)) for (TermResolverDefinition termResolverDef : termResolverDefs) {
			if (termResolverDef != null) {
				TermResolver<?> termResolver = translateTermResolver(termResolverDef);
				if (termResolver != null) termResolvers.add(termResolver);
			}
		}
		
		return new BasicContext(agendas, termResolvers); 
	}

	/**
	 * This method translates a {@link TermResolverDefinition} into a {@link TermResolver}
	 * 
	 * @param termResolverDef
	 * @return
	 */
	private TermResolver<?> translateTermResolver(TermResolverDefinition termResolverDef) {
		if (termResolverDef == null) throw new IllegalArgumentException("termResolverDef must not be null");
		KrmsTypeDefinition krmsTypeDef = typeRepositoryService.getTypeById(termResolverDef.getTypeId());
		if (krmsTypeDef == null) throw new IllegalStateException(TermResolverDefinition.class.getSimpleName() + 
				" has an invalid KRMS type: typeId=" + termResolverDef.getTypeId());
		TermResolverTypeService termResolverTypeService = 
			typeResolver.getTermResolverTypeService(termResolverDef, krmsTypeDef);
		
		TermResolver<?> termResolver = termResolverTypeService.loadTermResolver(termResolverDef);
		return termResolver;
	}
	
	@Override
	public Agenda translateAgendaDefinition(AgendaDefinition agendaDefinition) {
		// TODO: Handle event name.  Is it one of attributes?
		return new BasicAgenda("", agendaDefinition.getAttributes(), new LazyAgendaTree(agendaDefinition, this));
	}
		
	@Override
	public AgendaTree translateAgendaDefinitionToAgendaTree(AgendaDefinition agendaDefinition) {
		AgendaTreeDefinition agendaTreeDefinition = ruleRepositoryService.getAgendaTree(agendaDefinition.getAgendaId());
		return translateAgendaTreeDefinition(agendaTreeDefinition);
	}
	
	@Override
	public AgendaTree translateAgendaTreeDefinition(AgendaTreeDefinition agendaTreeDefinition) {
	
		List<String> ruleIds = new ArrayList<String>();
		List<String> subAgendaIds = new ArrayList<String>();
		for (AgendaTreeEntryDefinition entryDefinition : agendaTreeDefinition.getEntries()) {
			if (entryDefinition instanceof AgendaTreeRuleEntry) {
				ruleIds.add(((AgendaTreeRuleEntry)entryDefinition).getRuleId());
			} else if (entryDefinition instanceof AgendaTreeSubAgendaEntry) {
				subAgendaIds.add(((AgendaTreeSubAgendaEntry)entryDefinition).getSubAgendaId());
			} else {
				throw new IllegalStateException("Encountered invalid agenda tree entry definition class, did not understand type: " + entryDefinition);<commit_msg>KULRICE-5094: Create Term-related contracts, model objects, BOs and services<commit_after>/*
 * Copyright 2011 The Kuali Foundation
 *
 * Licensed under the Educational Community License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.opensource.org/licenses/ecl1.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kuali.rice.krms.impl.provider.repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.kuali.rice.krms.api.engine.TermResolver;
import org.kuali.rice.krms.api.repository.ActionDefinition;
import org.kuali.rice.krms.api.repository.AgendaDefinition;
import org.kuali.rice.krms.api.repository.AgendaTreeDefinition;
import org.kuali.rice.krms.api.repository.AgendaTreeEntryDefinition;
import org.kuali.rice.krms.api.repository.AgendaTreeRuleEntry;
import org.kuali.rice.krms.api.repository.AgendaTreeSubAgendaEntry;
import org.kuali.rice.krms.api.repository.ContextDefinition;
import org.kuali.rice.krms.api.repository.PropositionDefinition;
import org.kuali.rice.krms.api.repository.RepositoryDataException;
import org.kuali.rice.krms.api.repository.RuleDefinition;
import org.kuali.rice.krms.api.repository.RuleRepositoryService;
import org.kuali.rice.krms.api.repository.TermResolverDefinition;
import org.kuali.rice.krms.api.type.KrmsTypeDefinition;
import org.kuali.rice.krms.api.type.KrmsTypeRepositoryService;
import org.kuali.rice.krms.framework.engine.Action;
import org.kuali.rice.krms.framework.engine.Agenda;
import org.kuali.rice.krms.framework.engine.AgendaTree;
import org.kuali.rice.krms.framework.engine.AgendaTreeEntry;
import org.kuali.rice.krms.framework.engine.BasicAgenda;
import org.kuali.rice.krms.framework.engine.BasicAgendaTree;
import org.kuali.rice.krms.framework.engine.BasicAgendaTreeEntry;
import org.kuali.rice.krms.framework.engine.BasicContext;
import org.kuali.rice.krms.framework.engine.BasicRule;
import org.kuali.rice.krms.framework.engine.Context;
import org.kuali.rice.krms.framework.engine.Proposition;
import org.kuali.rice.krms.framework.engine.Rule;
import org.kuali.rice.krms.framework.engine.SubAgenda;
import org.kuali.rice.krms.framework.type.TermResolverTypeService;
import org.kuali.rice.krms.impl.repository.TermBoService;
import org.kuali.rice.krms.impl.type.KrmsTypeResolver;
import org.springframework.util.CollectionUtils;

/**
 * TODO... 
 * 
 * @author Kuali Rice Team (<EMAIL>)
 *
 */
public class RepositoryToEngineTranslatorImpl implements RepositoryToEngineTranslator {

	private RuleRepositoryService ruleRepositoryService;
	private KrmsTypeRepositoryService typeRepositoryService;
	private KrmsTypeResolver typeResolver;
	private TermBoService termBoService;
	
	@Override
	public Context translateContextDefinition(ContextDefinition contextDefinition) {
		if (contextDefinition == null) {
			return null;
		}
		List<Agenda> agendas = new ArrayList<Agenda>();
		for (AgendaDefinition agendaDefinition : contextDefinition.getAgendas()) {
			Agenda agenda = translateAgendaDefinition(agendaDefinition);
			agendas.add(agenda);
		}
		
		List<TermResolverDefinition> termResolverDefs = 
			termBoService.getTermResolversByContextId(contextDefinition.getContextDefinitionId());
		
		List<TermResolver<?>> termResolvers = new ArrayList<TermResolver<?>>();

		if (!CollectionUtils.isEmpty(termResolverDefs)) for (TermResolverDefinition termResolverDef : termResolverDefs) {
			if (termResolverDef != null) {
				TermResolver<?> termResolver = translateTermResolver(termResolverDef);
				if (termResolver != null) termResolvers.add(termResolver);
			}
		}
		
		return new BasicContext(agendas, termResolvers); 
	}

	/**
	 * This method translates a {@link TermResolverDefinition} into a {@link TermResolver}
	 * 
	 * @param termResolverDef
	 * @return
	 */
	private TermResolver<?> translateTermResolver(TermResolverDefinition termResolverDef) {
		if (termResolverDef == null) throw new IllegalArgumentException("termResolverDef must not be null");
		KrmsTypeDefinition krmsTypeDef = typeRepositoryService.getTypeById(termResolverDef.getTypeId());
		if (krmsTypeDef == null) throw new IllegalStateException(TermResolverDefinition.class.getSimpleName() + 
				" has an invalid KRMS type: typeId=" + termResolverDef.getTypeId());
		TermResolverTypeService termResolverTypeService = 
			typeResolver.getTermResolverTypeService(termResolverDef, krmsTypeDef);
		
		TermResolver<?> termResolver = termResolverTypeService.loadTermResolver(termResolverDef);
		// TODO: log warning when termResolver comes back null? or throw exception?
		return termResolver;
	}
	
	@Override
	public Agenda translateAgendaDefinition(AgendaDefinition agendaDefinition) {
		// TODO: Handle event name.  Is it one of attributes?
		return new BasicAgenda("", agendaDefinition.getAttributes(), new LazyAgendaTree(agendaDefinition, this));
	}
		
	@Override
	public AgendaTree translateAgendaDefinitionToAgendaTree(AgendaDefinition agendaDefinition) {
		AgendaTreeDefinition agendaTreeDefinition = ruleRepositoryService.getAgendaTree(agendaDefinition.getAgendaId());
		return translateAgendaTreeDefinition(agendaTreeDefinition);
	}
	
	@Override
	public AgendaTree translateAgendaTreeDefinition(AgendaTreeDefinition agendaTreeDefinition) {
	
		List<String> ruleIds = new ArrayList<String>();
		List<String> subAgendaIds = new ArrayList<String>();
		for (AgendaTreeEntryDefinition entryDefinition : agendaTreeDefinition.getEntries()) {
			if (entryDefinition instanceof AgendaTreeRuleEntry) {
				ruleIds.add(((AgendaTreeRuleEntry)entryDefinition).getRuleId());
			} else if (entryDefinition instanceof AgendaTreeSubAgendaEntry) {
				subAgendaIds.add(((AgendaTreeSubAgendaEntry)entryDefinition).getSubAgendaId());
			} else {
				throw new IllegalStateException("Encountered invalid agenda tree entry definition class, did not understand type: " + entryDefinition);
{'fraction_non_alphanumeric': 0.09471561113352159, 'fraction_numerical': 0.0014521984671238403, 'mean_word_length': 6.079383209594518, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6546841', 'n_tokens_mistral': 3774, 'n_tokens_neox': 3560, 'n_words': 732}