use of org.drools.workbench.screens.factmodel.model.AnnotationMetaModel in project drools-wb by kiegroup.
the class FactModelPersistence method toModel.
private static List<FactMetaModel> toModel(String drl) throws DroolsParserException {
Preconditions.checkNotNull(drl, "The string representing DRL can't be null!");
if (drl.startsWith("#advanced") || drl.startsWith("//advanced")) {
throw new DroolsParserException("Using advanced editor");
}
final DrlParser parser = new DrlParser();
final StringReader reader = new StringReader(drl);
final PackageDescr pkg = parser.parse(reader);
if (parser.hasErrors()) {
throw new DroolsParserException("The model drl " + drl + " is not valid");
}
if (pkg == null) {
return emptyList();
}
final List<TypeDeclarationDescr> types = pkg.getTypeDeclarations();
final List<FactMetaModel> list = new ArrayList<FactMetaModel>(types.size());
for (final TypeDeclarationDescr td : types) {
final FactMetaModel mm = new FactMetaModel();
mm.setName(td.getTypeName());
mm.setSuperType(td.getSuperTypeName());
final Map<String, TypeFieldDescr> fields = td.getFields();
for (Map.Entry<String, TypeFieldDescr> en : fields.entrySet()) {
final String fieldName = en.getKey();
final TypeFieldDescr descr = en.getValue();
final FieldMetaModel fm = new FieldMetaModel(fieldName, descr.getPattern().getObjectType());
mm.getFields().add(fm);
}
for (final AnnotationDescr descr : td.getAnnotations()) {
final String annotationName = descr.getName();
final Map<String, String> values = extractStringValues(descr);
final AnnotationMetaModel am = new AnnotationMetaModel(annotationName, values);
mm.getAnnotations().add(am);
}
list.add(mm);
}
return list;
}
use of org.drools.workbench.screens.factmodel.model.AnnotationMetaModel in project drools-wb by kiegroup.
the class DecisionTableXLSToDecisionTableGuidedConverter method makeNewJavaTypes.
private void makeNewJavaTypes(final Path context, final List<String> declaredTypes, final ConversionResult result) {
if (declaredTypes == null || declaredTypes.isEmpty()) {
return;
}
final KieModule module = moduleService.resolveModule(context);
for (String declaredType : declaredTypes) {
final FactModels factModels = FactModelPersistence.unmarshal(declaredType);
final String packageName = factModels.getPackageName();
final DataModel dataModel = new DataModelImpl();
for (FactMetaModel factMetaModel : factModels.getModels()) {
final DataObject dataObject = new DataObjectImpl(packageName, factMetaModel.getName());
dataObject.setSuperClassName(factMetaModel.getSuperType());
final List<AnnotationMetaModel> annotationMetaModel = factMetaModel.getAnnotations();
addAnnotations(dataObject, annotationMetaModel);
final List<FieldMetaModel> fields = factMetaModel.getFields();
for (FieldMetaModel fieldMetaModel : fields) {
final String fieldName = fieldMetaModel.name;
final String fieldType = fieldMetaModel.type;
// Guvnor 5.5 (and earlier) does not have MultipleType
boolean isMultiple = false;
ObjectProperty property = new ObjectPropertyImpl(fieldName, fieldType, isMultiple);
// field has no annotation in Guvnor 5.5 (and earlier)
dataObject.addProperty(property);
result.addMessage("Created Java Type " + getJavaTypeFQCN(dataObject), ConversionMessageType.INFO);
}
dataModel.getDataObjects().add(dataObject);
}
modellerService.saveModel(dataModel, module);
}
}
use of org.drools.workbench.screens.factmodel.model.AnnotationMetaModel in project drools-wb by kiegroup.
the class DecisionTableXLSToDecisionTableGuidedConverter method addAnnotations.
private void addAnnotations(final DataObject dataObject, final List<AnnotationMetaModel> annotationMetaModelList) {
for (AnnotationMetaModel annotationMetaModel : annotationMetaModelList) {
final String name = annotationMetaModel.name;
final Map<String, String> values = annotationMetaModel.values;
Annotation annotation;
String key = DroolsDomainAnnotations.VALUE_PARAM;
String value = "";
if (values.size() > 0) {
key = values.keySet().iterator().next();
value = values.values().iterator().next();
}
if ("Role".equals(name)) {
annotation = new AnnotationImpl(annotationDefinitions.get(DroolsDomainAnnotations.ROLE_ANNOTATION));
annotation.setValue(key, value);
dataObject.addAnnotation(annotation);
} else if ("Position".equals(name)) {
annotation = new AnnotationImpl(annotationDefinitions.get(DroolsDomainAnnotations.POSITION_ANNOTATION));
annotation.setValue(key, value);
dataObject.addAnnotation(annotation);
} else if ("Equals".equals(name)) {
annotation = new AnnotationImpl(annotationDefinitions.get(DroolsDomainAnnotations.KEY_ANNOTATION));
annotation.setValue(key, value);
dataObject.addAnnotation(annotation);
}
}
}
use of org.drools.workbench.screens.factmodel.model.AnnotationMetaModel in project drools-wb by kiegroup.
the class FactModelPersistence method toDRL.
private static String toDRL(final FactMetaModel mm) {
final StringBuilder sb = new StringBuilder();
sb.append("declare ").append(mm.getName());
if (mm.hasSuperType()) {
sb.append(" extends ");
sb.append(mm.getSuperType());
}
for (int i = 0; i < mm.getAnnotations().size(); i++) {
AnnotationMetaModel a = mm.getAnnotations().get(i);
sb.append("\n\t");
sb.append(buildAnnotationDRL(a));
}
for (int i = 0; i < mm.getFields().size(); i++) {
FieldMetaModel f = mm.getFields().get(i);
sb.append("\n\t");
sb.append(f.name).append(": ").append(f.type);
}
sb.append("\nend");
return sb.toString();
}
Aggregations