use of org.drools.core.rule.TypeDeclaration in project drools by kiegroup.
the class DialectUtil method rewriteModifyDescr.
private static void rewriteModifyDescr(RuleBuildContext context, JavaBlockDescr d, String originalBlock, StringBuilder consequence, Declaration declr, String obj) {
List<String> settableProperties = null;
Class<?> typeClass = findModifiedClass(context, d, declr);
TypeDeclaration typeDeclaration = typeClass == null ? null : context.getKnowledgeBuilder().getTypeDeclaration(typeClass);
boolean isPropertyReactive = typeDeclaration != null && typeDeclaration.isPropertyReactive();
if (isPropertyReactive) {
typeDeclaration.setTypeClass(typeClass);
settableProperties = typeDeclaration.getAccessibleProperties();
}
ConsequenceMetaData.Statement statement = null;
if (typeDeclaration != null) {
statement = new ConsequenceMetaData.Statement(ConsequenceMetaData.Statement.Type.MODIFY, typeClass);
context.getRule().getConsequenceMetaData().addStatement(statement);
}
BitMask modificationMask = isPropertyReactive ? getEmptyPropertyReactiveMask(settableProperties.size()) : allSetButTraitBitMask();
int end = originalBlock.indexOf("{");
if (end == -1) {
// no block
context.addError(new DescrBuildError(context.getParentDescr(), context.getRuleDescr(), null, "Block missing after modify" + d.getTargetExpression() + " ?\n"));
return;
}
addLineBreaks(consequence, originalBlock.substring(0, end));
int start = end + 1;
// adding each of the expressions:
for (String exprStr : ((JavaModifyBlockDescr) d).getExpressions()) {
end = originalBlock.indexOf(exprStr, start);
addLineBreaks(consequence, originalBlock.substring(start, end));
consequence.append(obj).append(".");
consequence.append(exprStr);
consequence.append("; ");
start = end + exprStr.length();
if (typeDeclaration != null) {
modificationMask = parseModifiedProperties(statement, settableProperties, typeDeclaration, isPropertyReactive, modificationMask, exprStr);
}
}
addLineBreaks(consequence, originalBlock.substring(end));
appendUpdateStatement(consequence, declr, obj, modificationMask, typeClass);
}
use of org.drools.core.rule.TypeDeclaration 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.rule.TypeDeclaration in project drools by kiegroup.
the class PatternBuilder method isEvent.
private boolean isEvent(RuleBuildContext context, Class<?> userProvidedClass) {
TypeDeclaration typeDeclaration = getTypeDeclaration(context, userProvidedClass);
if (typeDeclaration != null) {
return typeDeclaration.getRole() == Role.Type.EVENT;
}
Role role = userProvidedClass.getAnnotation(Role.class);
return role != null && role.value() == Role.Type.EVENT;
}
use of org.drools.core.rule.TypeDeclaration in project drools by kiegroup.
the class PatternBuilder method buildRuleBindings.
protected void buildRuleBindings(RuleBuildContext context, PatternDescr patternDescr, Pattern pattern, BindingDescr fieldBindingDescr, TypeDeclaration typeDeclaration) {
if (context.getDeclarationResolver().isDuplicated(context.getRule(), fieldBindingDescr.getVariable(), null)) {
processDuplicateBindings(fieldBindingDescr.isUnification(), patternDescr, pattern, fieldBindingDescr, fieldBindingDescr.getBindingField(), fieldBindingDescr.getVariable(), context);
if (fieldBindingDescr.isUnification()) {
return;
}
}
Declaration declr = pattern.addDeclaration(fieldBindingDescr.getVariable());
final InternalReadAccessor extractor = getFieldReadAccessor(context, fieldBindingDescr, pattern, fieldBindingDescr.getBindingField(), declr, true);
if (extractor == null) {
registerDescrBuildError(context, patternDescr, "Field Reader does not exist for declaration '" + fieldBindingDescr.getVariable() + "' in '" + fieldBindingDescr + "' in the rule '" + context.getRule().getName() + "'");
return;
}
declr.setReadAccessor(extractor);
if (typeDeclaration != null && extractor instanceof ClassFieldReader) {
addFieldToPatternWatchlist(pattern, typeDeclaration, ((ClassFieldReader) extractor).getFieldName());
}
}
use of org.drools.core.rule.TypeDeclaration in project drools by kiegroup.
the class PatternBuilder method processClassObjectType.
private void processClassObjectType(RuleBuildContext context, ObjectType objectType, Pattern pattern) {
if (objectType instanceof ClassObjectType) {
// make sure the Pattern is wired up to correct ClassObjectType and set as a target for rewiring
context.getPkg().getClassFieldAccessorStore().wireObjectType(objectType, pattern);
Class<?> cls = ((ClassObjectType) objectType).getClassType();
if (cls.getPackage() != null && !cls.getPackage().getName().equals("java.lang")) {
// register the class in its own package unless it is primitive or belongs to java.lang
TypeDeclaration typeDeclr = context.getKnowledgeBuilder().getAndRegisterTypeDeclaration(cls, cls.getPackage().getName());
context.setTypesafe(typeDeclr == null || typeDeclr.isTypesafe());
} else {
context.setTypesafe(true);
}
}
}
Aggregations