<commit_before>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.uff.ic.oceano.ostra.service.behaviortable;

import br.uff.ic.oceano.core.factory.MetricManagerFactory;
import br.uff.ic.oceano.core.service.PersistenceService;
import br.uff.ic.oceano.core.tools.metrics.MetricManager;
import br.uff.ic.oceano.core.tools.metrics.expression.QMOOD;
import br.uff.ic.oceano.ostra.model.DataMiningPattern;
import br.uff.ic.oceano.ostra.model.DataMiningResult;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author DanCastellani
 */
public class BehaviorTableService implements PersistenceService {

    private static int TABLE_SIZE = 7;
    private static final String SYMBOL_PROPORTIONAL_BEHAVIOR = "+";
    private static final String SYMBOL_OPOSITE_BEHAVIOR = "-";
    private static final String SYMBOL_CONFLICTANT_BEHAVIOR = "0";
    private static Map<String, Integer> mapAttributeIndices;
    private static String[] attributeNames;

    public void setup() {
        mapAttributeIndices = new HashMap<String, Integer>();
        int indice = 1;
        //coloco tamanho = lenght + 1, pq a posição 0 ficará em branco
        attributeNames = new String[QMOOD.QMOOD_QUALITY_ATTRIBUTES.length + 1];
        for (String qaName : QMOOD.QMOOD_QUALITY_ATTRIBUTES) {
            attributeNames[indice] = qaName;
            mapAttributeIndices.put(qaName, indice++);
        }
    }

    private void updateAttributeIndiceMap(DataMiningResult currentDataMiningResult) {
        mapAttributeIndices = new HashMap<String, Integer>();

        int indice = 1;
        List<String> attributes = getAttributes(currentDataMiningResult);
        Collections.sort(attributes);
        for (String attributeName : attributes) {
            mapAttributeIndices.put(attributeName, indice++);
        }

        attributeNames = new String[mapAttributeIndices.keySet().size() + 1];
        for (String attributeName : mapAttributeIndices.keySet()) {
            MetricManager mm = (MetricManager) MetricManagerFactory.getInstance().getMetricByName(attributeName);
            System.out.println("mapAttributeIndices.get(attributeName) = " + mapAttributeIndices.get(attributeName));
            System.out.println("mm.getMetric().getAcronym() = " + mm.getMetric().getAcronym());
            attributeNames[mapAttributeIndices.get(attributeName)] = mm.getMetric().getAcronym();
        }

        TABLE_SIZE = attributeNames.length;
    }

    public synchronized Behavior[][] buildTable(DataMiningResult currentDataMiningResult) {
        updateAttributeIndiceMap(currentDataMiningResult);

        Behavior[][] behaviorTable = createDefaultTable(currentDataMiningResult);
        final String ruleMetricName = currentDataMiningResult.getRuleMetricName();

        for (DataMiningPattern dataMiningPattern : currentDataMiningResult.getDataMiningPatterns()) {
            if (dataMiningPattern.getSize() != 2) {
                continue;
            }
            final String pattern = dataMiningPattern.getPattern();
            if (pattern.contains("project-revision") || pattern.contains("rdate") || pattern.contains("rcommiter") || pattern.contains("#files")
                    || pattern.contains("rday") || pattern.contains("rhour") || pattern.contains("rcompile")) {
                continue;
            }

            final String precedentAttribute = DataMiningPattern.getAttribute(dataMiningPattern.getPrecedent());
            final String consequentAttribute = DataMiningPattern.getAttribute(dataMiningPattern.getConsequent());

            final Integer precedentIndice = mapAttributeIndices.get(precedentAttribute);
            final Integer consequentIndice = mapAttributeIndices.get(consequentAttribute);

            if (precedentIndice == null || consequentIndice == null) {
                continue;
            }

            final String precedentValue = DataMiningPattern.getValue(dataMiningPattern.getPrecedent());
            final String consequentValue = DataMiningPattern.getValue(dataMiningPattern.getConsequent());

            behaviorTable[precedentIndice][consequentIndice].getRules().add(dataMiningPattern);
            behaviorTable[precedentIndice][consequentIndice].setValue(verifyBehaviors(behaviorTable[precedentIndice][consequentIndice].getRules()));

            //update max rule metric value
            final Double highestValue = behaviorTable[precedentIndice][consequentIndice].getHighestConfidence();
            final Double actualValue = getRuleMetric(ruleMetricName, dataMiningPattern);
            if (highestValue == null || actualValue > highestValue) {
                behaviorTable[precedentIndice][consequentIndice].setHighestConfidence(actualValue);
            }
        }

        return behaviorTable;
    }

    private Behavior[][] createDefaultTable(DataMiningResult dataMiningResult) {
        Behavior[][] behaviorTable = new Behavior[TABLE_SIZE][TABLE_SIZE];
        for (int i = 0; i < behaviorTable.length; i++) {
            for (int j = 0; j < behaviorTable[i].length; j++) {
                if (j == 0 && i == 0) {
                    behaviorTable[i][j] = new Behavior(dataMiningResult);<commit_msg>Quality attribute behavior table creation optimized.<commit_after>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.uff.ic.oceano.ostra.service.behaviortable;

import br.uff.ic.oceano.core.factory.MetricManagerFactory;
import br.uff.ic.oceano.core.model.Metric;
import br.uff.ic.oceano.core.service.PersistenceService;
import br.uff.ic.oceano.core.tools.metrics.MetricManager;
import br.uff.ic.oceano.core.tools.metrics.expression.QMOOD;
import br.uff.ic.oceano.ostra.model.DataMiningPattern;
import br.uff.ic.oceano.ostra.model.DataMiningResult;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author DanCastellani
 */
public class BehaviorTableService implements PersistenceService {

    private static int TABLE_SIZE = 7;
    private static final String SYMBOL_PROPORTIONAL_BEHAVIOR = "+";
    private static final String SYMBOL_OPOSITE_BEHAVIOR = "-";
    private static final String SYMBOL_CONFLICTANT_BEHAVIOR = "0";
    private static Map<String, Integer> mapAttributeIndices;
    private static String[] attributeNames;

    public void setup() {
        mapAttributeIndices = new HashMap<String, Integer>();
        int indice = 1;
        //coloco tamanho = lenght + 1, pq a posição 0 ficará em branco
        attributeNames = new String[QMOOD.QMOOD_QUALITY_ATTRIBUTES.length + 1];
        for (String qaName : QMOOD.QMOOD_QUALITY_ATTRIBUTES) {
            attributeNames[indice] = qaName;
            mapAttributeIndices.put(qaName, indice++);
        }
    }

    private void updateAttributeIndiceMap(DataMiningResult currentDataMiningResult) {
        mapAttributeIndices = new HashMap<String, Integer>();

        int indice = 1;
        List<String> attributes = getAttributes(currentDataMiningResult);
        Collections.sort(attributes);
        for (String attributeName : attributes) {
            mapAttributeIndices.put(attributeName, indice++);
        }

        attributeNames = new String[mapAttributeIndices.keySet().size() + 1];
        for (String attributeName : mapAttributeIndices.keySet()) {
            MetricManager mm = (MetricManager) MetricManagerFactory.getInstance().getMetricByName(attributeName);
            System.out.println("mapAttributeIndices.get(attributeName) = " + mapAttributeIndices.get(attributeName));
            System.out.println("mm.getMetric().getAcronym() = " + mm.getMetric().getAcronym());
            attributeNames[mapAttributeIndices.get(attributeName)] = mm.getMetric().getAcronym();
        }

        TABLE_SIZE = attributeNames.length;
    }

    public synchronized Behavior[][] buildTable(DataMiningResult currentDataMiningResult) {
        updateAttributeIndiceMap(currentDataMiningResult);

        Behavior[][] behaviorTable = createDefaultTable(currentDataMiningResult);
        final String ruleMetricName = currentDataMiningResult.getRuleMetricName();

        for (DataMiningPattern dataMiningPattern : currentDataMiningResult.getDataMiningPatterns()) {
            if (dataMiningPattern.getSize() != 2) {
                continue;
            }
            final String pattern = dataMiningPattern.getPattern();
            if (pattern.contains("project-revision") 
                    || pattern.contains("rdate") 
                    || pattern.contains("rcommiter") 
                    || pattern.contains("#files")
                    || pattern.contains("rday") 
                    || pattern.contains("rhour") 
                    || pattern.contains("rcompile")) {
                continue;
            }

            final String precedentAttribute = DataMiningPattern.getAttribute(dataMiningPattern.getPrecedent());
            final String consequentAttribute = DataMiningPattern.getAttribute(dataMiningPattern.getConsequent());

            final Integer precedentIndice = mapAttributeIndices.get(precedentAttribute);
            final Integer consequentIndice = mapAttributeIndices.get(consequentAttribute);

            if (precedentIndice == null || consequentIndice == null) {
                continue;
            }
            Behavior behavior = behaviorTable[precedentIndice][consequentIndice];
            behavior.getRules().add(dataMiningPattern);
            behavior.setValue(verifyBehaviors(behavior.getRules()));

            //update max rule metric value
            final Double highestValue = behavior.getHighestConfidence();
            final Double actualValue = getRuleMetric(ruleMetricName, dataMiningPattern);
            if (highestValue == null || actualValue > highestValue) {
                behavior.setHighestConfidence(actualValue);
            }
        }

        return behaviorTable;
    }

    private Behavior[][] createDefaultTable(DataMiningResult dataMiningResult) {
        Behavior[][] behaviorTable = new Behavior[TABLE_SIZE][TABLE_SIZE];
        for (int i = 0; i < behaviorTable.length; i++) {
            for (int j = 0; j < behaviorTable[i].length; j++) {
                if (j == 0 && i == 0) {
                    behaviorTable[i][j] = new Behavior(dataMiningResult);
{'fraction_non_alphanumeric': 0.10119331742243437, 'fraction_numerical': 0.0024821002386634845, 'mean_word_length': 3.1604447974583003, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2566254', 'n_tokens_mistral': 2888, 'n_tokens_neox': 2764, 'n_words': 537}