Search in sources :

Example 51 with FactType

use of org.kie.api.definition.type.FactType in project drools by kiegroup.

the class DataDictionaryTest method testDataTypes.

@Test
public void testDataTypes() throws Exception {
    setKSession(getModelSession(source, VERBOSE));
    setKbase(getKSession().getKieBase());
    FactType gender = getKbase().getFactType(packageName, "Gender");
    FactType noclaims = getKbase().getFactType(packageName, "NoOfClaims");
    FactType scrambled = getKbase().getFactType(packageName, "Scrambled");
    FactType domicile = getKbase().getFactType(packageName, "Domicile");
    FactType agecar = getKbase().getFactType(packageName, "AgeOfCar");
    FactType amklaims = getKbase().getFactType(packageName, "AmountOfClaims");
    Object g = gender.newInstance();
    assertEquals("org.drools.pmml.pmml_4_2.test.Gender", g.getClass().getName());
    gender.set(g, "value", "M");
    assertEquals("M", gender.get(g, "value"));
    Object n = noclaims.newInstance();
    assertEquals("org.drools.pmml.pmml_4_2.test.NoOfClaims", n.getClass().getName());
    noclaims.set(n, "value", "> 3");
    assertEquals("> 3", noclaims.get(n, "value"));
    Object s = scrambled.newInstance();
    assertEquals("org.drools.pmml.pmml_4_2.test.Scrambled", s.getClass().getName());
    scrambled.set(s, "value", 1);
    assertEquals(1, scrambled.get(s, "value"));
    Object d = domicile.newInstance();
    assertEquals("org.drools.pmml.pmml_4_2.test.Domicile", d.getClass().getName());
    domicile.set(d, "value", "SomeWhere");
    assertEquals("SomeWhere", domicile.get(d, "value"));
    Object a = agecar.newInstance();
    assertEquals("org.drools.pmml.pmml_4_2.test.AgeOfCar", a.getClass().getName());
    agecar.set(a, "value", 24.3);
    assertEquals(24.3, agecar.get(a, "value"));
    Object k = amklaims.newInstance();
    assertEquals("org.drools.pmml.pmml_4_2.test.AmountOfClaims", k.getClass().getName());
    amklaims.set(k, "value", 9);
    assertEquals(9, amklaims.get(k, "value"));
    try {
        agecar.set(a, "value", "Not a String field");
        Assert.fail();
    } catch (ClassCastException cce) {
        assertEquals(24.3, agecar.get(a, "value"));
    }
    Assert.assertFalse((Boolean) agecar.get(a, "valid"));
    agecar.set(a, "valid", true);
    Assert.assertTrue((Boolean) agecar.get(a, "valid"));
    Assert.assertFalse((Boolean) agecar.get(a, "missing"));
    agecar.set(a, "missing", true);
    Assert.assertTrue((Boolean) agecar.get(a, "missing"));
    // assertFalse((Boolean) agecar.get(a, "cyclic"));
    // agecar.set(a,"cyclic",true);
    // assertTrue((Boolean) agecar.get(a,"cyclic"));
    checkGeneratedRules();
}
Also used : FactType(org.kie.api.definition.type.FactType) DroolsAbstractPMMLTest(org.drools.pmml.pmml_4_2.DroolsAbstractPMMLTest) Test(org.junit.Test)

Example 52 with FactType

use of org.kie.api.definition.type.FactType in project drools by kiegroup.

the class ClusteringTest method testCenterBasedClustering.

@Test
public void testCenterBasedClustering() throws Exception {
    setKSession(getModelSession(source1, VERBOSE));
    setKbase(getKSession().getKieBase());
    KieSession kSession = getKSession();
    // init model
    kSession.fireAllRules();
    kSession.getEntryPoint("in_Fld0").insert("y");
    kSession.getEntryPoint("in_Fld1").insert(2.0);
    kSession.getEntryPoint("in_Fld2").insert(-1.0);
    kSession.fireAllRules();
    FactType mu = kSession.getKieBase().getFactType(packageName, "DistanceMembership");
    Collection mus = kSession.getObjects(new ClassObjectFilter(mu.getFactClass()));
    assertTrue(mus.size() > 0);
    for (Object x : mus) {
        Integer ix = (Integer) mu.get(x, "index");
        String lab = (String) mu.get(x, "label");
        Double m = (Double) mu.get(x, "mu");
        if (ix == 0) {
            assertEquals("Klust1", lab);
            assertEquals(41.1, m, 0.001);
        } else if (ix == 1) {
            assertEquals("Klust2", lab);
            assertEquals(14704.428, m, 0.001);
        }
    }
    checkGeneratedRules();
}
Also used : ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) DroolsAbstractPMMLTest(org.drools.pmml.pmml_4_2.DroolsAbstractPMMLTest) Test(org.junit.Test)

Example 53 with FactType

use of org.kie.api.definition.type.FactType in project drools by kiegroup.

the class DecisionTreeTest method testMissingTreeLastChoice.

@Test
public void testMissingTreeLastChoice() throws Exception {
    PMML4Compiler compiler = new PMML4Compiler();
    PMML pmml = compiler.loadModel(PMML, ResourceFactory.newClassPathResource(source2).getInputStream());
    for (Object o : pmml.getAssociationModelsAndBaselineModelsAndClusteringModels()) {
        if (o instanceof TreeModel) {
            TreeModel tree = (TreeModel) o;
            tree.setMissingValueStrategy(MISSINGVALUESTRATEGY.LAST_PREDICTION);
        }
    }
    String theory = compiler.generateTheory(pmml);
    if (VERBOSE) {
        System.out.println(theory);
    }
    KieSession kSession = getSession(theory);
    setKSession(kSession);
    setKbase(getKSession().getKieBase());
    // init model
    kSession.fireAllRules();
    FactType tgt = kSession.getKieBase().getFactType(packageName, "Fld9");
    FactType tok = kSession.getKieBase().getFactType(PMML4Helper.pmmlDefaultPackageName(), "TreeToken");
    kSession.getEntryPoint("in_Fld1").insert(-1.0);
    kSession.getEntryPoint("in_Fld2").insert(-1.0);
    kSession.getEntryPoint("in_Fld3").insert("optA");
    kSession.fireAllRules();
    Object token = getToken(kSession);
    assertEquals(0.8, (Double) tok.get(token, "confidence"), 1e-6);
    assertEquals("null", tok.get(token, "current"));
    assertEquals(50.0, tok.get(token, "totalCount"));
    checkFirstDataFieldOfTypeStatus(tgt, true, false, "Missing", "tgtX");
    checkGeneratedRules();
}
Also used : TreeModel(org.dmg.pmml.pmml_4_2.descr.TreeModel) PMML(org.dmg.pmml.pmml_4_2.descr.PMML) KieSession(org.kie.api.runtime.KieSession) PMML4Compiler(org.drools.pmml.pmml_4_2.PMML4Compiler) FactType(org.kie.api.definition.type.FactType) DroolsAbstractPMMLTest(org.drools.pmml.pmml_4_2.DroolsAbstractPMMLTest) Test(org.junit.Test)

Example 54 with FactType

use of org.kie.api.definition.type.FactType in project drools by kiegroup.

the class DecisionTreeTest method testSimpleTreeOutput.

@Test
public void testSimpleTreeOutput() throws Exception {
    setKSession(getModelSession(source2, VERBOSE));
    setKbase(getKSession().getKieBase());
    KieSession kSession = getKSession();
    // init model
    kSession.fireAllRules();
    FactType tgt = kSession.getKieBase().getFactType(packageName, "Fld9");
    FactType tok = kSession.getKieBase().getFactType(PMML4Helper.pmmlDefaultPackageName(), "TreeToken");
    kSession.getEntryPoint("in_Fld1").insert(-1.0);
    kSession.getEntryPoint("in_Fld2").insert(-1.0);
    kSession.getEntryPoint("in_Fld3").insert("optA");
    kSession.fireAllRules();
    Object token = getToken(kSession);
    assertEquals(0.8, tok.get(token, "confidence"));
    assertEquals("null", tok.get(token, "current"));
    assertEquals(50.0, tok.get(token, "totalCount"));
    checkFirstDataFieldOfTypeStatus(tgt, true, false, "Missing", "tgtX");
    checkFirstDataFieldOfTypeStatus(kSession.getKieBase().getFactType(packageName, "OutClass"), true, false, "Missing", "tgtX");
    checkFirstDataFieldOfTypeStatus(kSession.getKieBase().getFactType(packageName, "OutProb"), true, false, "Missing", 0.8);
    checkGeneratedRules();
}
Also used : KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) DroolsAbstractPMMLTest(org.drools.pmml.pmml_4_2.DroolsAbstractPMMLTest) Test(org.junit.Test)

Example 55 with FactType

use of org.kie.api.definition.type.FactType in project drools by kiegroup.

the class DecisionTreeTest method testMissingTreeWeighted2.

@Test
public void testMissingTreeWeighted2() throws Exception {
    setKSession(getModelSession(source2, VERBOSE));
    setKbase(getKSession().getKieBase());
    KieSession kSession = getKSession();
    // init model
    kSession.fireAllRules();
    FactType tgt = kSession.getKieBase().getFactType(packageName, "Fld9");
    FactType tok = kSession.getKieBase().getFactType(PMML4Helper.pmmlDefaultPackageName(), "TreeToken");
    kSession.getEntryPoint("in_Fld1").insert(-1.0);
    kSession.getEntryPoint("in_Fld2").insert(-1.0);
    kSession.getEntryPoint("in_Fld3").insert("miss");
    kSession.fireAllRules();
    Object token = getToken(kSession);
    assertEquals(0.6, tok.get(token, "confidence"));
    assertEquals("null", tok.get(token, "current"));
    assertEquals(100.0, tok.get(token, "totalCount"));
    checkFirstDataFieldOfTypeStatus(tgt, true, false, "Missing", "tgtX");
    checkGeneratedRules();
}
Also used : KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) DroolsAbstractPMMLTest(org.drools.pmml.pmml_4_2.DroolsAbstractPMMLTest) Test(org.junit.Test)

Aggregations

FactType (org.kie.api.definition.type.FactType)208 Test (org.junit.Test)193 KieSession (org.kie.api.runtime.KieSession)114 KieBase (org.kie.api.KieBase)63 DroolsAbstractPMMLTest (org.drools.pmml.pmml_4_2.DroolsAbstractPMMLTest)45 DroolsAbstractPMMLTest (org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)31 ArrayList (java.util.ArrayList)28 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)27 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)27 KieServices (org.kie.api.KieServices)22 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)18 Resource (org.kie.api.io.Resource)17 KieBuilder (org.kie.api.builder.KieBuilder)16 ClassObjectFilter (org.kie.api.runtime.ClassObjectFilter)16 KieFileSystem (org.kie.api.builder.KieFileSystem)15 KieContainer (org.kie.api.runtime.KieContainer)15 List (java.util.List)10 Results (org.kie.api.builder.Results)10 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)9 LogicalTypeInconsistencyException (org.drools.core.factmodel.traits.LogicalTypeInconsistencyException)9