Search in sources :

Example 1 with Record

use of org.kie.dmn.validation.dtanalysis.mcdc.MCDCAnalyser.Record in project drools by kiegroup.

the class ExampleMCDCTest method test1.

@Test
public void test1() throws Exception {
    final String resourceFileName = "example1.dmn";
    List<DMNMessage> validate = validator.validate(getReader(resourceFileName), ANALYZE_DECISION_TABLE, COMPUTE_DECISION_TABLE_MCDC);
    DTAnalysis analysis = getAnalysis(validate, "_452a0adf-dd49-47c3-b02d-fe0ad45902c7");
    Collection<Record> mcdcCases = computeMCDCCases(analysis.getMCDCSelectedBlocks());
    assertThat(mcdcCases, hasSize(16));
    assertMCDCCases(resourceFileName, analysis.getSource(), mcdcCases);
    // debugOutputAndOpenXLSX(analysis.getSource(), analysis.getMCDCSelectedBlocks());
    String mcdc2tck = MCDC2TCKGenerator.mcdc2tck(analysis.getSource(), analysis.getMCDCSelectedBlocks());
// debugTCKXML(analysis.getSource(), mcdc2tck);
}
Also used : DMNMessage(org.kie.dmn.api.core.DMNMessage) Record(org.kie.dmn.validation.dtanalysis.mcdc.MCDCAnalyser.Record) DTAnalysis(org.kie.dmn.validation.dtanalysis.model.DTAnalysis) Test(org.junit.Test) AbstractDTAnalysisTest(org.kie.dmn.validation.dtanalysis.AbstractDTAnalysisTest)

Example 2 with Record

use of org.kie.dmn.validation.dtanalysis.mcdc.MCDCAnalyser.Record in project drools by kiegroup.

the class MCDC2TCKGenerator method mcdc2tck.

public static String mcdc2tck(DecisionTable dt, List<PosNegBlock> selectedBlocks) throws JAXBException {
    ObjectFactory factory = new ObjectFactory();
    TestCases testCases = factory.createTestCases();
    Set<Record> mcdcRecords = new LinkedHashSet<>();
    int testCaseId = 1;
    for (PosNegBlock b : selectedBlocks) {
        boolean add = mcdcRecords.add(b.posRecord);
        if (add) {
            appendRecordToTestCases(dt, testCases, String.valueOf(testCaseId), b.posRecord);
            testCaseId++;
        }
        for (Record negRecord : b.negRecords) {
            add = mcdcRecords.add(negRecord);
            if (add) {
                appendRecordToTestCases(dt, testCases, String.valueOf(testCaseId), negRecord);
                testCaseId++;
            }
        }
    }
    JAXBContext jaxbContext = JAXBContext.newInstance(TestCases.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    StringWriter sw = new StringWriter();
    jaxbMarshaller.marshal(testCases, sw);
    return sw.toString();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Marshaller(javax.xml.bind.Marshaller) ObjectFactory(org.kie.dmn.validation.dtanalysis.mcdc.dmntck.ObjectFactory) StringWriter(java.io.StringWriter) TestCases(org.kie.dmn.validation.dtanalysis.mcdc.dmntck.TestCases) PosNegBlock(org.kie.dmn.validation.dtanalysis.mcdc.MCDCAnalyser.PosNegBlock) Record(org.kie.dmn.validation.dtanalysis.mcdc.MCDCAnalyser.Record) JAXBContext(javax.xml.bind.JAXBContext)

Example 3 with Record

use of org.kie.dmn.validation.dtanalysis.mcdc.MCDCAnalyser.Record in project drools by kiegroup.

the class ExampleMCDCTest method assertMCDCCases.

private void assertMCDCCases(final String resourceFileName, DecisionTable sourceDT, Collection<Record> mcdcCases) {
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime(resourceFileName, ExampleMCDCTest.class);
    MCDCListener mcdcListener = new MCDCListener();
    runtime.addListener(mcdcListener);
    DMNModel dmnModel = runtime.getModels().get(0);
    assertThat(dmnModel, notNullValue());
    for (Record mcdcCase : mcdcCases) {
        mcdcListener.selectedRule.clear();
        DMNContext context = runtime.newContext();
        for (int i = 0; i < mcdcCase.enums.length; i++) {
            context.set(sourceDT.getInput().get(i).getInputExpression().getText(), mcdcCase.enums[i]);
        }
        DMNResult evaluateAll = runtime.evaluateAll(dmnModel, context);
        LOG.debug("{}", evaluateAll);
        assertThat(mcdcListener.selectedRule, hasItems(mcdcCase.ruleIdx + 1));
    }
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNContext(org.kie.dmn.api.core.DMNContext) Record(org.kie.dmn.validation.dtanalysis.mcdc.MCDCAnalyser.Record) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel)

Example 4 with Record

use of org.kie.dmn.validation.dtanalysis.mcdc.MCDCAnalyser.Record in project drools by kiegroup.

the class ExampleMCDCTest method test2.

@Test
public void test2() throws Exception {
    final String resourceFileName = "example2.dmn";
    List<DMNMessage> validate = validator.validate(getReader(resourceFileName), ANALYZE_DECISION_TABLE, COMPUTE_DECISION_TABLE_MCDC);
    DTAnalysis analysis = getAnalysis(validate, "_e31c78b7-63ef-4112-a0bc-b0546043ebe9");
    Collection<Record> mcdcCases = computeMCDCCases(analysis.getMCDCSelectedBlocks());
    assertThat(mcdcCases, hasSize(14));
    assertMCDCCases(resourceFileName, analysis.getSource(), mcdcCases);
    // debugOutputAndOpenXLSX(analysis.getSource(), analysis.getMCDCSelectedBlocks());
    String mcdc2tck = MCDC2TCKGenerator.mcdc2tck(analysis.getSource(), analysis.getMCDCSelectedBlocks());
// debugTCKXML(analysis.getSource(), mcdc2tck);
}
Also used : DMNMessage(org.kie.dmn.api.core.DMNMessage) Record(org.kie.dmn.validation.dtanalysis.mcdc.MCDCAnalyser.Record) DTAnalysis(org.kie.dmn.validation.dtanalysis.model.DTAnalysis) Test(org.junit.Test) AbstractDTAnalysisTest(org.kie.dmn.validation.dtanalysis.AbstractDTAnalysisTest)

Example 5 with Record

use of org.kie.dmn.validation.dtanalysis.mcdc.MCDCAnalyser.Record in project drools by kiegroup.

the class ExampleMCDCTest method debugOutputAndOpenXLSX.

public static void debugOutputAndOpenXLSX(DecisionTable dt, List<PosNegBlock> selectedBlocks) {
    XSSFWorkbook wb = new XSSFWorkbook();
    CreationHelper ch = wb.getCreationHelper();
    CellStyle styleColumn = wb.createCellStyle();
    styleColumn.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());
    styleColumn.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    Font font = wb.createFont();
    font.setColor(IndexedColors.RED.getIndex());
    CellStyle styleRepeated = wb.createCellStyle();
    styleRepeated.setFont(font);
    Sheet sheet = wb.createSheet("MC DC Analysis");
    int r = 0, c = 0;
    Row row = sheet.createRow(r++);
    row.createCell(c++).setCellValue(ch.createRichTextString("Rule:"));
    for (InputClause input : dt.getInput()) {
        row.createCell(c++).setCellValue(input.getInputExpression().getText());
    }
    for (OutputClause output : dt.getOutput()) {
        row.createCell(c++).setCellValue(dt.getOutputLabel());
    }
    row.createCell(c++).setCellValue(ch.createRichTextString("Rule:"));
    for (InputClause input : dt.getInput()) {
        row.createCell(c++).setCellValue(input.getInputExpression().getText());
    }
    for (OutputClause output : dt.getOutput()) {
        row.createCell(c++).setCellValue(dt.getOutputLabel());
    }
    Set<Record> mcdcRecords = new LinkedHashSet<>();
    for (PosNegBlock b : selectedBlocks) {
        row = sheet.createRow(r++);
        c = 0;
        boolean add = mcdcRecords.add(b.posRecord);
        Cell ruleIdxCell = row.createCell(c++);
        ruleIdxCell.setCellValue(b.posRecord.ruleIdx + 1);
        if (!add) {
            ruleIdxCell.setCellStyle(styleRepeated);
        }
        for (int i = 0; i < b.posRecord.enums.length; i++) {
            Object en = b.posRecord.enums[i];
            Cell enCell = row.createCell(c++);
            enCell.setCellValue(en.toString());
            if (!add) {
                enCell.setCellStyle(styleRepeated);
            }
            if (b.cMarker == i) {
                enCell.setCellStyle(styleColumn);
            }
        }
        for (Object out : b.posRecord.output) {
            Cell outCell = row.createCell(c++);
            outCell.setCellValue(out.toString());
            if (!add) {
                outCell.setCellStyle(styleRepeated);
            }
        }
        for (int i = 0; i < b.negRecords.size(); i++) {
            Record negRecord = b.negRecords.get(i);
            if (i != 0) {
                row = sheet.createRow(r++);
                c = 0 + 1 + b.posRecord.enums.length + b.posRecord.output.size();
            }
            add = mcdcRecords.add(negRecord);
            ruleIdxCell = row.createCell(c++);
            ruleIdxCell.setCellValue(negRecord.ruleIdx + 1);
            if (!add) {
                ruleIdxCell.setCellStyle(styleRepeated);
            }
            for (Object en : negRecord.enums) {
                Cell enCell = row.createCell(c++);
                enCell.setCellValue(en.toString());
                if (!add) {
                    enCell.setCellStyle(styleRepeated);
                }
            }
            for (Object out : negRecord.output) {
                Cell outCell = row.createCell(c++);
                outCell.setCellValue(out.toString());
                if (!add) {
                    outCell.setCellStyle(styleRepeated);
                }
            }
        }
    }
    File file;
    try {
        file = Files.createTempFile("mcdc " + dt.getOutputLabel(), ".xlsx").toFile();
        OutputStream fileOut = new FileOutputStream(file);
        wb.write(fileOut);
        wb.close();
    } catch (IOException e) {
        e.printStackTrace();
        // terminate early;
        return;
    }
    if (Desktop.isDesktopSupported()) {
        try {
            Desktop.getDesktop().open(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        throw new UnsupportedOperationException();
    }
    LOG.trace(System.getProperty("java.io.tmpdir"));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) PosNegBlock(org.kie.dmn.validation.dtanalysis.mcdc.MCDCAnalyser.PosNegBlock) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Font(org.apache.poi.ss.usermodel.Font) OutputClause(org.kie.dmn.model.api.OutputClause) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Record(org.kie.dmn.validation.dtanalysis.mcdc.MCDCAnalyser.Record) CellStyle(org.apache.poi.ss.usermodel.CellStyle) Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) File(java.io.File) InputClause(org.kie.dmn.model.api.InputClause)

Aggregations

Record (org.kie.dmn.validation.dtanalysis.mcdc.MCDCAnalyser.Record)5 LinkedHashSet (java.util.LinkedHashSet)2 Test (org.junit.Test)2 DMNMessage (org.kie.dmn.api.core.DMNMessage)2 AbstractDTAnalysisTest (org.kie.dmn.validation.dtanalysis.AbstractDTAnalysisTest)2 PosNegBlock (org.kie.dmn.validation.dtanalysis.mcdc.MCDCAnalyser.PosNegBlock)2 DTAnalysis (org.kie.dmn.validation.dtanalysis.model.DTAnalysis)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 StringWriter (java.io.StringWriter)1 JAXBContext (javax.xml.bind.JAXBContext)1 Marshaller (javax.xml.bind.Marshaller)1 Cell (org.apache.poi.ss.usermodel.Cell)1 CellStyle (org.apache.poi.ss.usermodel.CellStyle)1 CreationHelper (org.apache.poi.ss.usermodel.CreationHelper)1 Font (org.apache.poi.ss.usermodel.Font)1 Row (org.apache.poi.ss.usermodel.Row)1 Sheet (org.apache.poi.ss.usermodel.Sheet)1