use of com.github.javaparser.ast.expr.SimpleName in project drools by kiegroup.
the class KiePMMLDroolsModelFactoryUtilsTest method setConstructor.
@Test
public void setConstructor() {
Model model = new TreeModel();
PMML_MODEL pmmlModel = PMML_MODEL.byName(model.getClass().getSimpleName());
ConstructorDeclaration constructorDeclaration = MODEL_TEMPLATE.getDefaultConstructor().get();
SimpleName tableName = new SimpleName("TABLE_NAME");
String targetField = "TARGET_FIELD";
MINING_FUNCTION miningFunction = MINING_FUNCTION.CLASSIFICATION;
String kModulePackageName = getSanitizedPackageName("kModulePackageName");
KiePMMLDroolsModelFactoryUtils.setConstructor(model, constructorDeclaration, tableName, targetField, miningFunction, kModulePackageName);
Map<Integer, Expression> superInvocationExpressionsMap = new HashMap<>();
Map<String, Expression> assignExpressionMap = new HashMap<>();
assignExpressionMap.put("targetField", new StringLiteralExpr(targetField));
assignExpressionMap.put("miningFunction", new NameExpr(miningFunction.getClass().getName() + "." + miningFunction.name()));
assignExpressionMap.put("pmmlMODEL", new NameExpr(pmmlModel.getClass().getName() + "." + pmmlModel.name()));
assignExpressionMap.put("kModulePackageName", new StringLiteralExpr(kModulePackageName));
assertTrue(commonEvaluateConstructor(constructorDeclaration, tableName.asString(), superInvocationExpressionsMap, assignExpressionMap));
}
use of com.github.javaparser.ast.expr.SimpleName in project drools by kiegroup.
the class ExpressionTyper method thisExpr.
private TypedExpressionCursor thisExpr(Expression drlxExpr, List<Node> childNodes, boolean isInLineCast, java.lang.reflect.Type originalTypeCursor) {
TypedExpressionCursor teCursor;
if (childNodes.size() > 1 && !isInLineCast) {
SimpleName fieldName = null;
if (childNodes.get(1) instanceof NameExpr) {
fieldName = ((NameExpr) childNodes.get(1)).getName();
} else if (childNodes.get(1) instanceof SimpleName) {
fieldName = (SimpleName) childNodes.get(1);
}
if (fieldName != null) {
context.addReactOnProperties(getFieldName(drlxExpr, fieldName));
}
}
teCursor = new TypedExpressionCursor(new NameExpr(THIS_PLACEHOLDER), originalTypeCursor);
return teCursor;
}
use of com.github.javaparser.ast.expr.SimpleName in project drools by kiegroup.
the class ExpressionTyper method toTypedExpressionFromMethodCallOrField.
private TypedExpressionResult toTypedExpressionFromMethodCallOrField(Expression drlxExpr) {
if (drlxExpr instanceof FieldAccessExpr) {
// try to see if it's a constant
final Optional<TypedExpression> typedExpression = tryParseAsConstantField(ruleContext.getTypeResolver(), ((FieldAccessExpr) drlxExpr).getScope(), ((FieldAccessExpr) drlxExpr).getNameAsString());
if (typedExpression.isPresent()) {
return new TypedExpressionResult(typedExpression, context);
}
}
if (patternType == null && drlxExpr instanceof NullSafeFieldAccessExpr) {
// try to see if it's a constant
final Optional<TypedExpression> typedExpression = tryParseAsConstantField(ruleContext.getTypeResolver(), ((NullSafeFieldAccessExpr) drlxExpr).getScope(), ((NullSafeFieldAccessExpr) drlxExpr).getNameAsString());
if (typedExpression.isPresent()) {
return new TypedExpressionResult(typedExpression, context);
}
}
final List<Node> childrenNodes = flattenScope(ruleContext.getTypeResolver(), drlxExpr);
final Node firstChild = childrenNodes.get(0);
boolean isInLineCast = firstChild instanceof InlineCastExpr;
java.lang.reflect.Type originalTypeCursor;
final Node firstNode;
if (isInLineCast) {
InlineCastExpr inlineCast = (InlineCastExpr) firstChild;
originalTypeCursor = originalTypeCursorFromInlineCast(inlineCast);
firstNode = inlineCast.getExpression();
if (inlineCast.getExpression().isThisExpr()) {
context.setInlineCastExpression(Optional.of(new InstanceOfExpr(new NameExpr(THIS_PLACEHOLDER), (ReferenceType) inlineCast.getType())));
} else {
context.setInlineCastExpression(toTypedExpression(inlineCast.getExpression()).getTypedExpression().map(TypedExpression::getExpression).map(expr -> new InstanceOfExpr(expr, (ReferenceType) inlineCast.getType())));
}
} else {
originalTypeCursor = patternType;
firstNode = firstChild;
}
if (originalTypeCursor != null && originalTypeCursor.equals(Object.class)) {
// try infer type from the declarations
final Optional<DeclarationSpec> declarationById = ruleContext.getDeclarationById(printNode(firstChild));
originalTypeCursor = declarationById.map(d -> (java.lang.reflect.Type) d.getDeclarationClass()).orElse(originalTypeCursor);
}
final Optional<TypedExpressionCursor> teCursor = processFirstNode(drlxExpr, childrenNodes, firstNode, isInLineCast, originalTypeCursor);
if (firstNode instanceof MethodCallExpr) {
MethodCallExpr me = (MethodCallExpr) firstNode;
addReactOnProperty(me.getNameAsString(), me.getArguments());
}
if (firstNode instanceof NullSafeMethodCallExpr) {
NullSafeMethodCallExpr me = (NullSafeMethodCallExpr) firstNode;
addReactOnProperty(me.getNameAsString(), me.getArguments());
}
if (!teCursor.isPresent()) {
return new TypedExpressionResult(empty(), context);
}
Expression previous = teCursor.get().expressionCursor;
java.lang.reflect.Type typeCursor = teCursor.get().typeCursor;
List<Node> childrenWithoutFirst = childrenNodes.subList(1, childrenNodes.size());
for (Node part : childrenWithoutFirst) {
if (part instanceof SimpleName) {
String field = part.toString();
TypedExpression expression = nameExprToMethodCallExpr(field, typeCursor, previous, ruleContext);
if (expression == null) {
ruleContext.addCompilationError(new InvalidExpressionErrorResult("Unknown field " + field + " on " + typeCursor));
break;
}
typeCursor = expression.getType();
previous = expression.getExpression();
} else if (part instanceof MethodCallExpr) {
TypedExpressionCursor typedExpr = methodCallExpr((MethodCallExpr) part, typeCursor, previous);
typeCursor = typedExpr.typeCursor;
previous = typedExpr.expressionCursor;
} else if (part instanceof NullSafeMethodCallExpr) {
TypedExpressionCursor typedExpr = nullSafeMethodCallExpr((NullSafeMethodCallExpr) part, typeCursor, previous);
typeCursor = typedExpr.typeCursor;
previous = typedExpr.expressionCursor;
} else if (part instanceof InlineCastExpr && ((InlineCastExpr) part).getExpression() instanceof FieldAccessExpr) {
InlineCastExpr inlineCastExprPart = (InlineCastExpr) part;
final FieldAccessExpr fieldAccessExpr = (FieldAccessExpr) inlineCastExprPart.getExpression();
final TypedExpression toMethodCallExpr = nameExprToMethodCallExpr(fieldAccessExpr.getNameAsString(), typeCursor, previous, ruleContext);
if (toMethodCallExpr == null) {
ruleContext.addCompilationError(new InvalidExpressionErrorResult("Unknown field " + fieldAccessExpr.getNameAsString() + " on " + typeCursor));
break;
}
final Class<?> castClass = getClassFromType(ruleContext.getTypeResolver(), inlineCastExprPart.getType());
previous = addCastToExpression(castClass, toMethodCallExpr.getExpression(), false);
typeCursor = castClass;
} else if (part instanceof ArrayAccessExpr) {
final ArrayAccessExpr inlineCastExprPart = (ArrayAccessExpr) part;
TypedExpressionCursor typedExpr = arrayAccessExpr(inlineCastExprPart, typeCursor, previous).orElseThrow(() -> new NoSuchElementException("ArrayAccessExpr doesn't contain TypedExpressionCursor!"));
typeCursor = typedExpr.typeCursor;
previous = typedExpr.expressionCursor;
} else {
throw new UnsupportedOperationException();
}
}
return new TypedExpressionResult(of(new TypedExpression(previous, typeCursor, accessorToFieldName(drlxExpr))), context);
}
use of com.github.javaparser.ast.expr.SimpleName in project drools by kiegroup.
the class AbstractExpressionBuilder method containsThis.
protected boolean containsThis(TypedExpression typedExpression) {
Expression expr = typedExpression.getExpression();
Optional<String> opt = expr.findAll(NameExpr.class).stream().map(NameExpr::getName).map(SimpleName::getIdentifier).findFirst();
return opt.map(s -> s.equals(THIS_PLACEHOLDER)).orElse(false);
}
use of com.github.javaparser.ast.expr.SimpleName in project drools by kiegroup.
the class Consequence method rewriteRHS.
private boolean rewriteRHS(BlockStmt ruleBlock, BlockStmt rhs) {
AtomicBoolean requireDrools = new AtomicBoolean(false);
List<MethodCallExpr> methodCallExprs = rhs.findAll(MethodCallExpr.class);
List<AssignExpr> assignExprs = rhs.findAll(AssignExpr.class);
List<MethodCallExpr> updateExprs = new ArrayList<>();
Map<String, Type> rhsBodyDeclarations = new HashMap<>();
for (VariableDeclarator variableDeclarator : rhs.findAll(VariableDeclarator.class)) {
rhsBodyDeclarations.put(variableDeclarator.getNameAsString(), variableDeclarator.getType());
}
for (MethodCallExpr methodCallExpr : methodCallExprs) {
if (!methodCallExpr.getScope().isPresent() && isImplicitDroolsMethod(methodCallExpr)) {
if (methodCallExpr.getNameAsString().equals("insertLogical") && !TruthMaintenanceSystemFactory.present()) {
context.addCompilationError(new MissingDependencyError(TruthMaintenanceSystemFactory.NO_TMS));
}
methodCallExpr.setScope(new NameExpr("drools"));
}
if (hasDroolsScope(methodCallExpr) || hasDroolsAsParameter(methodCallExpr)) {
if (knowledgeHelperMethods.contains(methodCallExpr.getNameAsString())) {
methodCallExpr.setScope(createAsKnowledgeHelperExpression());
} else if (methodCallExpr.getNameAsString().equals("update")) {
if (methodCallExpr.toString().contains("FactHandle")) {
methodCallExpr.setScope(new NameExpr("((org.drools.modelcompiler.consequence.DroolsImpl) drools)"));
}
updateExprs.add(methodCallExpr);
} else if (methodCallExpr.getNameAsString().equals("retract")) {
methodCallExpr.setName(new SimpleName("delete"));
}
requireDrools.set(true);
}
}
Set<String> initializedBitmaskFields = new HashSet<>();
for (MethodCallExpr updateExpr : updateExprs) {
Expression argExpr = updateExpr.getArgument(0);
if (argExpr instanceof NameExpr) {
String updatedVar = ((NameExpr) argExpr).getNameAsString();
Class<?> updatedClass = classFromRHSDeclarations(rhsBodyDeclarations, updatedVar);
// We might need to generate the domain metadata class for types used in consequence
// without an explicit pattern. See CompilerTest.testConsequenceInsertThenUpdate
context.getPackageModel().registerDomainClass(updatedClass);
if (context.isPropertyReactive(updatedClass)) {
if (!initializedBitmaskFields.contains(updatedVar)) {
Set<String> modifiedProps = findModifiedProperties(methodCallExprs, updateExpr, updatedVar, updatedClass);
modifiedProps.addAll(findModifiedPropertiesFromAssignment(assignExprs, updateExpr, updatedVar, updatedClass));
MethodCallExpr bitMaskCreation = createBitMaskInitialization(updatedClass, modifiedProps);
AssignExpr bitMaskAssign = createBitMaskField(updatedVar, bitMaskCreation);
if (!DrlxParseUtil.hasDuplicateExpr(ruleBlock, bitMaskAssign)) {
ruleBlock.addStatement(bitMaskAssign);
}
}
updateExpr.addArgument("mask_" + updatedVar);
initializedBitmaskFields.add(updatedVar);
}
}
}
return requireDrools.get();
}
Aggregations