Search in sources :

Example 16 with TypeResolver

use of org.kie.soup.project.datamodel.commons.types.TypeResolver in project drools by kiegroup.

the class FactPopulatorTest method testCollection.

@Test
public void testCollection() throws Exception {
    TypeResolver typeResolver = getTypeResolver();
    List<Field> fieldData = new ArrayList<Field>();
    CollectionFieldData collectionFieldData = new CollectionFieldData();
    collectionFieldData.setName("cheeses");
    fieldData.add(collectionFieldData);
    collectionFieldData.getCollectionFieldList().add(new FieldData("cheeses", "=cheese1"));
    collectionFieldData.getCollectionFieldList().add(new FieldData("cheeses", "=cheese2"));
    FactData cheeseryFactData = new FactData("Cheesery", "cheesery", fieldData, false);
    FactData cheeseFactData1 = new FactData("Cheese", "cheese1", Collections.<Field>emptyList(), false);
    FactData cheeseFactData2 = new FactData("Cheese", "cheese2", Collections.<Field>emptyList(), false);
    factPopulator.add(new NewFactPopulator(populatedData, typeResolver, cheeseryFactData));
    factPopulator.add(new NewFactPopulator(populatedData, typeResolver, cheeseFactData1));
    factPopulator.add(new NewFactPopulator(populatedData, typeResolver, cheeseFactData2));
    factPopulator.populate();
    assertTrue(populatedData.containsKey("cheesery"));
    Cheesery cheesery = (Cheesery) populatedData.get("cheesery");
    assertNotNull(cheesery);
    assertEquals(2, cheesery.getCheeses().size());
    assertNotNull(cheesery.getCheeses().get(0));
    assertTrue(cheesery.getCheeses().get(0) instanceof Cheese);
    assertNotNull(cheesery.getCheeses().get(1));
    assertTrue(cheesery.getCheeses().get(1) instanceof Cheese);
}
Also used : CollectionFieldData(org.drools.workbench.models.testscenarios.shared.CollectionFieldData) FieldData(org.drools.workbench.models.testscenarios.shared.FieldData) Field(org.drools.workbench.models.testscenarios.shared.Field) ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver) TypeResolver(org.kie.soup.project.datamodel.commons.types.TypeResolver) FactData(org.drools.workbench.models.testscenarios.shared.FactData) ArrayList(java.util.ArrayList) Cheese(org.drools.workbench.models.testscenarios.backend.Cheese) Cheesery(org.drools.workbench.models.testscenarios.backend.Cheesery) CollectionFieldData(org.drools.workbench.models.testscenarios.shared.CollectionFieldData) Test(org.junit.Test)

Example 17 with TypeResolver

use of org.kie.soup.project.datamodel.commons.types.TypeResolver in project drools by kiegroup.

the class FactPopulatorTest method testCollectionFieldInFacts.

@Test
public void testCollectionFieldInFacts() throws Exception {
    TypeResolver typeResolver = getTypeResolver();
    FactData fd1 = new FactData("Cheese", "f1", Arrays.<Field>asList(new FieldData("type", ""), new FieldData("price", "42")), false);
    factPopulator.add(new NewFactPopulator(populatedData, typeResolver, fd1));
    FactData fd2 = new FactData("Cheese", "f2", Arrays.<Field>asList(new FieldData("type", ""), new FieldData("price", "43")), false);
    factPopulator.add(new NewFactPopulator(populatedData, typeResolver, fd2));
    FactData fd3 = new FactData("Cheese", "f3", Arrays.<Field>asList(new FieldData("type", ""), new FieldData("price", "45")), false);
    factPopulator.add(new NewFactPopulator(populatedData, typeResolver, fd3));
    FieldData field = new FieldData();
    field.setName("cheeses");
    field.setNature(FieldData.TYPE_COLLECTION);
    field.setValue("=[f1,f2,f3]");
    List<Field> lstField = new ArrayList<Field>();
    lstField.add(field);
    FactData lst = new FactData("Cheesery", "listChesse", lstField, false);
    factPopulator.add(new NewFactPopulator(populatedData, typeResolver, lst));
    factPopulator.populate();
    Cheesery listChesse = (Cheesery) populatedData.get("listChesse");
    Cheese f1 = (Cheese) populatedData.get("f1");
    Cheese f2 = (Cheese) populatedData.get("f2");
    Cheese f3 = (Cheese) populatedData.get("f3");
    assertEquals(3, listChesse.getCheeses().size());
    assertTrue(listChesse.getCheeses().contains(f1));
    assertTrue(listChesse.getCheeses().contains(f2));
    assertTrue(listChesse.getCheeses().contains(f3));
}
Also used : CollectionFieldData(org.drools.workbench.models.testscenarios.shared.CollectionFieldData) FieldData(org.drools.workbench.models.testscenarios.shared.FieldData) Field(org.drools.workbench.models.testscenarios.shared.Field) ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver) TypeResolver(org.kie.soup.project.datamodel.commons.types.TypeResolver) FactData(org.drools.workbench.models.testscenarios.shared.FactData) ArrayList(java.util.ArrayList) Cheese(org.drools.workbench.models.testscenarios.backend.Cheese) Cheesery(org.drools.workbench.models.testscenarios.backend.Cheesery) Test(org.junit.Test)

Example 18 with TypeResolver

use of org.kie.soup.project.datamodel.commons.types.TypeResolver in project drools by kiegroup.

the class FactVerifierTest method testVerifyAnonymousFacts.

@Test
public void testVerifyAnonymousFacts() throws Exception {
    TypeResolver typeResolver = mock(TypeResolver.class);
    FactVerifier factVerifier = new FactVerifier(new HashMap<String, Object>(), typeResolver, ksession, new HashMap<String, Object>());
    Cheese c = new Cheese();
    c.setPrice(42);
    c.setType("stilton");
    // configure the mock to return the value
    Set o = Collections.singleton((Object) c);
    when(ksession.getObjects()).thenReturn(o);
    VerifyFact vf = new VerifyFact("Cheese", new ArrayList<VerifyField>(), true);
    vf.getFieldValues().add(new VerifyField("price", "42", "=="));
    vf.getFieldValues().add(new VerifyField("type", "stilton", "=="));
    factVerifier.verify(vf);
    assertTrue(vf.wasSuccessful());
    vf = new VerifyFact("Person", new ArrayList<VerifyField>(), true);
    vf.getFieldValues().add(new VerifyField("age", "42", "=="));
    factVerifier.verify(vf);
    assertFalse(vf.wasSuccessful());
    vf = new VerifyFact("Cheese", new ArrayList<VerifyField>(), true);
    vf.getFieldValues().add(new VerifyField("price", "43", "=="));
    vf.getFieldValues().add(new VerifyField("type", "stilton", "=="));
    factVerifier.verify(vf);
    assertFalse(vf.wasSuccessful());
    assertEquals(Boolean.FALSE, vf.getFieldValues().get(0).getSuccessResult());
    vf = new VerifyFact("Cell", new ArrayList<VerifyField>(), true);
    vf.getFieldValues().add(new VerifyField("value", "43", "=="));
    factVerifier.verify(vf);
    assertFalse(vf.wasSuccessful());
    assertEquals(Boolean.FALSE, vf.getFieldValues().get(0).getSuccessResult());
}
Also used : Set(java.util.Set) VerifyField(org.drools.workbench.models.testscenarios.shared.VerifyField) TypeResolver(org.kie.soup.project.datamodel.commons.types.TypeResolver) ArrayList(java.util.ArrayList) Cheese(org.drools.workbench.models.testscenarios.backend.Cheese) VerifyFact(org.drools.workbench.models.testscenarios.shared.VerifyFact) Test(org.junit.Test)

Example 19 with TypeResolver

use of org.kie.soup.project.datamodel.commons.types.TypeResolver in project drools by kiegroup.

the class FactVerifierTest method testVerifyFactsWithOperator.

@Test
public void testVerifyFactsWithOperator() throws Exception {
    TypeResolver typeResolver = mock(TypeResolver.class);
    Cheese f1 = new Cheese("cheddar", 42);
    HashMap<String, Object> populatedData = new HashMap<String, Object>();
    populatedData.put("f1", f1);
    // configure the mock to return the value
    Set o = Collections.singleton((Object) f1);
    when(ksession.getObjects()).thenReturn(o);
    FactVerifier factVerifier = new FactVerifier(populatedData, typeResolver, ksession, new HashMap<String, Object>());
    // test all true
    VerifyFact vf = new VerifyFact();
    vf.setName("f1");
    vf.getFieldValues().add(new VerifyField("type", "cheddar", "=="));
    vf.getFieldValues().add(new VerifyField("price", "4777", "!="));
    factVerifier.verify(vf);
    for (int i = 0; i < vf.getFieldValues().size(); i++) {
        assertTrue(vf.getFieldValues().get(i).getSuccessResult());
    }
    vf = new VerifyFact();
    vf.setName("f1");
    vf.getFieldValues().add(new VerifyField("type", "cheddar", "!="));
    factVerifier.verify(vf);
    assertFalse(vf.getFieldValues().get(0).getSuccessResult());
}
Also used : Set(java.util.Set) HashMap(java.util.HashMap) VerifyField(org.drools.workbench.models.testscenarios.shared.VerifyField) TypeResolver(org.kie.soup.project.datamodel.commons.types.TypeResolver) Cheese(org.drools.workbench.models.testscenarios.backend.Cheese) VerifyFact(org.drools.workbench.models.testscenarios.shared.VerifyFact) Test(org.junit.Test)

Example 20 with TypeResolver

use of org.kie.soup.project.datamodel.commons.types.TypeResolver in project drools by kiegroup.

the class KiePackagesBuilder method createKiePackage.

private KnowledgePackageImpl createKiePackage(String name) {
    KnowledgePackageImpl kpkg = new KnowledgePackageImpl(name);
    kpkg.setClassFieldAccessorCache(new ClassFieldAccessorCache(configuration.getClassLoader()));
    TypeResolver typeResolver = new ClassTypeResolver(new HashSet<>(kpkg.getImports().keySet()), configuration.getClassLoader(), name);
    typeResolver.addImport(name + ".*");
    kpkg.setTypeResolver(typeResolver);
    return kpkg;
}
Also used : ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver) TypeResolver(org.kie.soup.project.datamodel.commons.types.TypeResolver) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver)

Aggregations

TypeResolver (org.kie.soup.project.datamodel.commons.types.TypeResolver)24 Test (org.junit.Test)16 ClassTypeResolver (org.kie.soup.project.datamodel.commons.types.ClassTypeResolver)14 FieldData (org.drools.workbench.models.testscenarios.shared.FieldData)8 Cheese (org.drools.workbench.models.testscenarios.backend.Cheese)7 FactData (org.drools.workbench.models.testscenarios.shared.FactData)7 VerifyField (org.drools.workbench.models.testscenarios.shared.VerifyField)7 HashMap (java.util.HashMap)6 VerifyFact (org.drools.workbench.models.testscenarios.shared.VerifyFact)6 ArrayList (java.util.ArrayList)5 Set (java.util.Set)5 CollectionFieldData (org.drools.workbench.models.testscenarios.shared.CollectionFieldData)5 Field (org.drools.workbench.models.testscenarios.shared.Field)4 HashSet (java.util.HashSet)3 FactAssignmentField (org.drools.workbench.models.testscenarios.shared.FactAssignmentField)3 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)2 Cheesery (org.drools.workbench.models.testscenarios.backend.Cheesery)2 Mouse (org.drools.workbench.models.testscenarios.backend.Mouse)2 OuterFact (org.drools.workbench.models.testscenarios.backend.OuterFact)2 KieSession (org.kie.api.runtime.KieSession)2