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);
}
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));
}
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());
}
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());
}
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;
}
Aggregations