use of com.github.javaparser.ast.body.ClassOrInterfaceDeclaration in project drools by kiegroup.
the class GeneratedClassDeclaration method createBasicDeclaredClass.
private ClassOrInterfaceDeclaration createBasicDeclaredClass(String generatedClassName) {
ClassOrInterfaceDeclaration basicDeclaredClass = new ClassOrInterfaceDeclaration(nodeList(Modifier.publicModifier()), false, generatedClassName);
// Ref: {@link org.drools.core.factmodel.DefaultBeanClassBuilder} by default always receive is Serializable.
basicDeclaredClass.addImplementedType(Serializable.class.getName());
markerInterfaceAnnotations.stream().map(Class::getCanonicalName).forEach(basicDeclaredClass::addImplementedType);
// No-args ctor
basicDeclaredClass.addConstructor(Modifier.publicModifier().getKeyword());
return basicDeclaredClass;
}
use of com.github.javaparser.ast.body.ClassOrInterfaceDeclaration in project drools by kiegroup.
the class CommonCodegenUtilsTest method getMethodDeclaration.
@Test
public void getMethodDeclaration() {
final String methodName = "METHOD_NAME";
final ClassOrInterfaceDeclaration classOrInterfaceDeclaration = new ClassOrInterfaceDeclaration();
assertFalse(CommonCodegenUtils.getMethodDeclaration(classOrInterfaceDeclaration, methodName).isPresent());
classOrInterfaceDeclaration.addMethod("NOT_METHOD");
assertFalse(CommonCodegenUtils.getMethodDeclaration(classOrInterfaceDeclaration, methodName).isPresent());
classOrInterfaceDeclaration.addMethod(methodName);
assertTrue(CommonCodegenUtils.getMethodDeclaration(classOrInterfaceDeclaration, methodName).isPresent());
}
use of com.github.javaparser.ast.body.ClassOrInterfaceDeclaration in project drools by kiegroup.
the class CodegenTestUtils method commonValidateCompilation.
public static void commonValidateCompilation(BlockStmt body, List<Parameter> parameters) {
ClassOrInterfaceDeclaration classOrInterfaceType = new ClassOrInterfaceDeclaration();
classOrInterfaceType.setName("CommCodeTest");
MethodDeclaration toAdd = new MethodDeclaration();
toAdd.setType("void");
toAdd.setName("TestingMethod");
toAdd.setParameters(NodeList.nodeList(parameters));
toAdd.setBody(body);
classOrInterfaceType.addMember(toAdd);
CompilationUnit compilationUnit = StaticJavaParser.parse("");
compilationUnit.setPackageDeclaration("org.kie.pmml.compiler.commons.utils");
compilationUnit.addType(classOrInterfaceType);
Map<String, String> sourcesMap = Collections.singletonMap("org.kie.pmml.compiler.commons.utils.CommCodeTest", compilationUnit.toString());
try {
KieMemoryCompiler.compile(sourcesMap, Thread.currentThread().getContextClassLoader());
} catch (Exception e) {
fail(e.getMessage());
}
}
use of com.github.javaparser.ast.body.ClassOrInterfaceDeclaration in project drools by kiegroup.
the class PMMLRuleMapperFactory method getPMMLRuleMapperSource.
public static String getPMMLRuleMapperSource(final String fullRuleName) {
final String packageName = fullRuleName.contains(".") ? fullRuleName.substring(0, fullRuleName.lastIndexOf('.')) : "";
CompilationUnit cloneCU = JavaParserUtils.getKiePMMLModelCompilationUnit(KIE_PMML_RULE_MAPPER_CLASS_NAME, packageName, KIE_PMML_RULE_MAPPER_TEMPLATE_JAVA, KIE_PMML_RULE_MAPPER_CLASS_NAME);
ClassOrInterfaceDeclaration typeDeclaration = (ClassOrInterfaceDeclaration) cloneCU.getTypes().get(0);
FieldDeclaration ruleNameField = typeDeclaration.getFieldByName("model").orElseThrow(() -> new RuntimeException("The template " + KIE_PMML_RULE_MAPPER_TEMPLATE_JAVA + " has been modified."));
ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr();
objectCreationExpr.setType(fullRuleName);
ruleNameField.getVariables().get(0).setInitializer(objectCreationExpr);
return cloneCU.toString();
}
use of com.github.javaparser.ast.body.ClassOrInterfaceDeclaration in project drools by kiegroup.
the class KiePMMLMiningModelFactory method getKiePMMLMiningModelSourcesMapCommon.
static Map<String, String> getKiePMMLMiningModelSourcesMapCommon(final MiningModelCompilationDTO compilationDTO, final Map<String, String> toReturn) {
logger.trace("getKiePMMLMiningModelSourcesMap {} {} {}", compilationDTO.getFields(), compilationDTO.getModel(), compilationDTO.getPackageName());
if (!toReturn.containsKey(compilationDTO.getSegmentationCanonicalClassName())) {
throw new KiePMMLException("Expected generated class " + compilationDTO.getSegmentationCanonicalClassName() + " not " + "found");
}
CompilationUnit cloneCU = JavaParserUtils.getKiePMMLModelCompilationUnit(compilationDTO.getSimpleClassName(), compilationDTO.getPackageName(), KIE_PMML_MINING_MODEL_TEMPLATE_JAVA, KIE_PMML_MINING_MODEL_TEMPLATE);
ClassOrInterfaceDeclaration modelTemplate = cloneCU.getClassByName(compilationDTO.getSimpleClassName()).orElseThrow(() -> new KiePMMLException(MAIN_CLASS_NOT_FOUND + ": " + compilationDTO.getSimpleClassName()));
setConstructor(compilationDTO, modelTemplate);
toReturn.put(getFullClassName(cloneCU), cloneCU.toString());
return toReturn;
}
Aggregations