use of org.drools.core.factmodel.ClassDefinition in project drools by kiegroup.
the class DialectUtil method parseInsertDescr.
private static void parseInsertDescr(RuleBuildContext context, JavaBlockDescr block) {
String expr = block.getTargetExpression();
if (expr.startsWith("new ")) {
int argsStart = expr.indexOf('(');
if (argsStart > 0) {
String className = expr.substring(4, argsStart).trim();
Class<?> typeClass = findClassByName(context, className);
TypeDeclaration typeDeclaration = typeClass == null ? null : context.getKnowledgeBuilder().getTypeDeclaration(typeClass);
if (typeDeclaration != null) {
ConsequenceMetaData.Statement statement = new ConsequenceMetaData.Statement(ConsequenceMetaData.Statement.Type.INSERT, typeClass);
context.getRule().getConsequenceMetaData().addStatement(statement);
String constructorParams = expr.substring(argsStart + 1, expr.indexOf(')')).trim();
List<String> args = splitArgumentsList(constructorParams);
ClassDefinition classDefinition = typeDeclaration.getTypeClassDef();
List<FactField> fields = classDefinition.getFields();
if (args.size() == fields.size()) {
for (int i = 0; i < args.size(); i++) {
statement.addField(fields.get(i).getName(), args.get(i));
}
}
}
}
}
}
use of org.drools.core.factmodel.ClassDefinition in project drools by kiegroup.
the class StandaloneTraitFactory method extendAsProperTrait.
private Class<T> extendAsProperTrait(Class<T> trait) {
String extName = trait.getName() + TraitFactory.SUFFIX;
if (!classLoader.isClassInUse(extName)) {
try {
ClassDefinition extDef = new ClassDefinition(extName);
extDef.setSuperClass(Object.class.getName());
AnnotationDefinition annot = new AnnotationDefinition(Trait.class.getName());
extDef.addAnnotation(annot);
String[] supers = new String[] { Thing.class.getName(), trait.getName() };
extDef.setInterfaces(supers);
byte[] ext = getComponentFactory().getClassBuilderFactory().getTraitBuilder().buildClass(extDef, classLoader);
Class<?> klass = registerAndLoadTypeDefinition(extName, ext);
ClassDefinition tDef = buildClassDefinition(trait, trait);
tDef.setDefinedClass(klass);
getTraitRegistry().addTrait(tDef);
} catch (Exception e) {
e.printStackTrace();
}
}
try {
return (Class<T>) Class.forName(extName, false, classLoader);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
use of org.drools.core.factmodel.ClassDefinition 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.drools.core.factmodel.ClassDefinition in project drools by kiegroup.
the class TypeDeclarationCache method setClassDefinitionOnTypeDeclaration.
private ClassDefinition setClassDefinitionOnTypeDeclaration(Class<?> cls, TypeDeclaration typeDeclaration) {
ClassDefinition clsDef = new ClassDefinition();
ClassDefinitionFactory.populateDefinitionFromClass(clsDef, typeDeclaration.getResource(), cls, cls.getAnnotation(Trait.class) != null);
typeDeclaration.setTypeClassDef(clsDef);
return clsDef;
}
use of org.drools.core.factmodel.ClassDefinition in project drools by kiegroup.
the class PatternBuilder method processPositional.
protected void processPositional(final RuleBuildContext context, final PatternDescr patternDescr, final Pattern pattern, final ExprConstraintDescr descr) {
if (descr.getType() == ExprConstraintDescr.Type.POSITIONAL && pattern.getObjectType() instanceof ClassObjectType) {
Class<?> klazz = ((ClassObjectType) pattern.getObjectType()).getClassType();
TypeDeclaration tDecl = context.getKnowledgeBuilder().getTypeDeclaration(klazz);
if (tDecl == null) {
registerDescrBuildError(context, patternDescr, "Unable to find @positional definitions for :" + klazz + "\n");
return;
}
ClassDefinition clsDef = tDecl.getTypeClassDef();
if (clsDef == null) {
registerDescrBuildError(context, patternDescr, "Unable to find @positional field " + descr.getPosition() + " for class " + tDecl.getTypeName() + "\n");
return;
}
FieldDefinition field = clsDef.getField(descr.getPosition());
if (field == null) {
registerDescrBuildError(context, patternDescr, "Unable to find @positional field " + descr.getPosition() + " for class " + tDecl.getTypeName() + "\n");
return;
}
String expr = descr.getExpression();
boolean isSimpleIdentifier = isIdentifier(expr);
if (isSimpleIdentifier) {
// create a binding
BindingDescr binder = new BindingDescr();
binder.setUnification(true);
binder.setExpression(field.getName());
binder.setVariable(descr.getExpression());
buildRuleBindings(context, patternDescr, pattern, binder);
} else {
// create a constraint
build(context, patternDescr, pattern, descr, field.getName() + " == " + descr.getExpression());
}
}
}
Aggregations