use of org.drools.workbench.screens.factmodel.model.FactMetaModel 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<>(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.FactMetaModel in project drools-wb by kiegroup.
the class FactModelPersistence method marshal.
public static String marshal(final FactModels content) {
final StringBuilder sb = new StringBuilder();
PackageNameWriter.write(sb, content);
ImportsWriter.write(sb, content);
for (final FactMetaModel factMetaModel : content.getModels()) {
sb.append(toDRL(factMetaModel)).append("\n\n");
}
return sb.toString().trim();
}
use of org.drools.workbench.screens.factmodel.model.FactMetaModel 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.getName();
final String fieldType = fieldMetaModel.getType();
// 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.FactMetaModel in project drools-wb by kiegroup.
the class FactModelPersistence method unmarshal.
public static FactModels unmarshal(final String content) {
try {
// De-serialize model
final List<FactMetaModel> models = toModel(content);
final FactModels factModels = new FactModels();
factModels.getModels().addAll(models);
// De-serialize Package name
final String packageName = PackageNameParser.parsePackageName(content);
factModels.setPackageName(packageName);
// De-serialize imports
final Imports imports = ImportsParser.parseImports(content);
factModels.setImports(imports);
return factModels;
} catch (final DroolsParserException e) {
throw new RuntimeException(e);
}
// TODO {porcelli} needs define error handling strategy
// log.error( "Unable to parse the DRL for the model - falling back to text (" + e.getMessage() + ")" );
// RuleContentText text = new RuleContentText();
// text.content = item.getContent();
// asset.setContent( text );
}
Aggregations