use of org.kie.soup.project.datamodel.commons.types.TypeResolver in project drools by kiegroup.
the class FactVerifierTest method testVerifyFactExplanation.
@Test
public void testVerifyFactExplanation() throws Exception {
Cheese f1 = new Cheese();
f1.setType(null);
TypeResolver typeResolver = mock(TypeResolver.class);
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>());
VerifyFact vf = new VerifyFact();
vf.setName("f1");
vf.getFieldValues().add(new VerifyField("type", "boo", "!="));
factVerifier.verify(vf);
VerifyField vfl = vf.getFieldValues().get(0);
assertEquals("[f1] field [type] was not [boo].", vfl.getExplanation());
}
use of org.kie.soup.project.datamodel.commons.types.TypeResolver in project drools by kiegroup.
the class ClassHierarchyManager method mergeFields.
protected void mergeFields(String simpleSuperTypeName, String superTypePackageName, String fullSuper, TypeDeclarationDescr typeDescr, List<TypeDefinition> unresolvedTypes, Map<String, AbstractClassTypeDeclarationDescr> unprocessableDescrs, TypeResolver resolver) {
Map<String, TypeFieldDescr> fieldMap = new LinkedHashMap<String, TypeFieldDescr>();
boolean isNovel = TypeDeclarationUtils.isNovelClass(typeDescr, kbuilder.getPackageRegistry(typeDescr.getNamespace()));
PackageRegistry registry = kbuilder.getPackageRegistry(superTypePackageName);
InternalKnowledgePackage pack = null;
if (registry != null) {
pack = registry.getPackage();
}
if (unprocessableDescrs.containsKey(fullSuper)) {
unprocessableDescrs.put(typeDescr.getType().getFullName(), typeDescr);
return;
}
// if a class is declared in DRL, its package can't be null? The default package is replaced by "defaultpkg"
boolean isSuperClassTagged = false;
// in the same package, or in a previous one
boolean isSuperClassDeclared = true;
if (pack != null) {
// look for the supertype declaration in available packages
TypeDeclaration superTypeDeclaration = pack.getTypeDeclaration(simpleSuperTypeName);
if (superTypeDeclaration != null && superTypeDeclaration.getTypeClassDef() != null) {
ClassDefinition classDef = superTypeDeclaration.getTypeClassDef();
// inherit fields
for (org.kie.api.definition.type.FactField fld : classDef.getFields()) {
TypeFieldDescr inheritedFlDescr = buildInheritedFieldDescrFromDefinition(fld, typeDescr);
fieldMap.put(inheritedFlDescr.getFieldName(), inheritedFlDescr);
}
// new classes are already distinguished from tagged external classes
isSuperClassTagged = !superTypeDeclaration.isNovel();
}
} else {
isSuperClassDeclared = false;
}
// look for the class externally
if (!isSuperClassDeclared || isSuperClassTagged) {
try {
Class superKlass;
if (registry != null) {
superKlass = registry.getTypeResolver().resolveType(fullSuper);
} else {
// if the supertype has not been declared, and we have got so far, it means that this class is not novel
superKlass = resolver.resolveType(fullSuper);
}
buildDescrsFromFields(superKlass, typeDescr, registry, fieldMap);
} catch (ClassNotFoundException cnfe) {
unprocessableDescrs.put(typeDescr.getType().getFullName(), typeDescr);
return;
}
}
// notice that it is not possible to override a field changing its type
for (String fieldName : typeDescr.getFields().keySet()) {
if (fieldMap.containsKey(fieldName)) {
String type1 = fieldMap.get(fieldName).getPattern().getObjectType();
String type2 = typeDescr.getFields().get(fieldName).getPattern().getObjectType();
if (type2.lastIndexOf(".") < 0) {
try {
TypeResolver typeResolver = kbuilder.getPackageRegistry(typeDescr.getNamespace()).getTypeResolver();
type1 = typeResolver.resolveType(type1).getName();
type2 = typeResolver.resolveType(type2).getName();
// now that we are at it... this will be needed later anyway
fieldMap.get(fieldName).getPattern().setObjectType(type1);
typeDescr.getFields().get(fieldName).getPattern().setObjectType(type2);
} catch (ClassNotFoundException cnfe) {
// will fail later
}
}
boolean clash = !type1.equals(type2);
TypeFieldDescr overriding = null;
if (clash) {
// this may still be an override using a subclass of the original type
try {
Class<?> sup = resolver.resolveType(type1);
Class<?> loc = resolver.resolveType(type2);
if (sup.isAssignableFrom(loc)) {
clash = false;
// mark as non inherited so that a new field is actually built
overriding = fieldMap.get(fieldName);
}
} catch (ClassNotFoundException cnfe) {
// not much to do
}
}
if (clash) {
kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Cannot redeclare field '" + fieldName + " from " + type1 + " to " + type2));
typeDescr.setType(null, null);
return;
} else {
String initVal = fieldMap.get(fieldName).getInitExpr();
TypeFieldDescr fd = typeDescr.getFields().get(fieldName);
if (fd.getInitExpr() == null) {
fd.setInitExpr(initVal);
}
fd.setInherited(fieldMap.get(fieldName).isInherited());
fd.setOverriding(overriding);
for (String key : fieldMap.get(fieldName).getAnnotationNames()) {
if (fd.getAnnotation(key) == null) {
fd.addAnnotation(fieldMap.get(fieldName).getAnnotation(key));
}
}
if (fd.getIndex() < 0) {
fd.setIndex(fieldMap.get(fieldName).getIndex());
}
}
}
fieldMap.put(fieldName, typeDescr.getFields().get(fieldName));
}
typeDescr.setFields(fieldMap);
}
use of org.kie.soup.project.datamodel.commons.types.TypeResolver in project drools by kiegroup.
the class FactAssignmentFieldPopulatorTest method testMatryoshkaDollSituation.
@Test
public void testMatryoshkaDollSituation() throws Exception {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
TypeResolver typeResolver = new ClassTypeResolver(new HashSet<String>(), classLoader);
typeResolver.addImport("org.drools.workbench.models.testscenarios.backend.MatryoshkaDoll");
MatryoshkaDoll matryoshkaDoll = new MatryoshkaDoll();
FactAssignmentField factAssignmentField = createFactAssignmentField();
addFactAssignmentFieldIntoFactAssignmentField(factAssignmentField, 5);
FactAssignmentFieldPopulator factAssignmentFieldPopulator = new FactAssignmentFieldPopulator(matryoshkaDoll, factAssignmentField, typeResolver);
factAssignmentFieldPopulator.populate(new HashMap<String, Object>());
assertNotNull(matryoshkaDoll.getMatryoshkaDoll());
assertNotNull(matryoshkaDoll.getMatryoshkaDoll().getMatryoshkaDoll());
assertNotNull(matryoshkaDoll.getMatryoshkaDoll().getMatryoshkaDoll().getMatryoshkaDoll());
assertNotNull(matryoshkaDoll.getMatryoshkaDoll().getMatryoshkaDoll().getMatryoshkaDoll().getMatryoshkaDoll());
assertNotNull(matryoshkaDoll.getMatryoshkaDoll().getMatryoshkaDoll().getMatryoshkaDoll().getMatryoshkaDoll().getMatryoshkaDoll());
assertNotNull(matryoshkaDoll.getMatryoshkaDoll().getMatryoshkaDoll().getMatryoshkaDoll().getMatryoshkaDoll().getMatryoshkaDoll().getMatryoshkaDoll());
assertNull(matryoshkaDoll.getMatryoshkaDoll().getMatryoshkaDoll().getMatryoshkaDoll().getMatryoshkaDoll().getMatryoshkaDoll().getMatryoshkaDoll().getMatryoshkaDoll());
}
use of org.kie.soup.project.datamodel.commons.types.TypeResolver in project drools by kiegroup.
the class FactPopulatorTest method testPopulateNested.
@Test
public void testPopulateNested() throws Exception {
TypeResolver typeResolver = getTypeResolver();
FactData cheeseFactData = new FactData("Cheese", "c1", Arrays.<Field>asList(new FieldData("type", "cheddar"), new FieldData("price", "42")), false);
factPopulator.add(new NewFactPopulator(populatedData, typeResolver, cheeseFactData));
FactData outerFactData = new FactData("OuterFact", "p1", Arrays.<Field>asList(new FieldData("name", "mic"), new FieldData("innerFact", "=c1")), false);
factPopulator.add(new NewFactPopulator(populatedData, typeResolver, outerFactData));
factPopulator.populate();
assertTrue(populatedData.containsKey("c1"));
assertTrue(populatedData.containsKey("p1"));
OuterFact o = (OuterFact) populatedData.get("p1");
assertEquals(populatedData.get("c1"), o.getInnerFact());
}
use of org.kie.soup.project.datamodel.commons.types.TypeResolver in project drools by kiegroup.
the class FactPopulatorTest method testCollectionSums.
@Test
public void testCollectionSums() throws Exception {
TypeResolver typeResolver = getTypeResolver();
List<Field> fieldData = new ArrayList<Field>();
CollectionFieldData collectionFieldData = new CollectionFieldData();
collectionFieldData.setName("list");
fieldData.add(collectionFieldData);
collectionFieldData.getCollectionFieldList().add(new FieldData("list", "=1+3"));
FactData wrapperFactData = new FactData("MyCollectionWrapper", "wrapper", fieldData, false);
factPopulator.add(new NewFactPopulator(populatedData, typeResolver, wrapperFactData));
factPopulator.populate();
assertTrue(populatedData.containsKey("wrapper"));
MyCollectionWrapper wrapper = (MyCollectionWrapper) populatedData.get("wrapper");
assertNotNull(wrapper);
assertEquals(1, wrapper.getList().size());
assertNotNull(wrapper.getList().get(0));
assertEquals(4, wrapper.getList().get(0));
}
Aggregations