Search in sources :

Example 1 with FieldMetaModel

use of org.drools.workbench.screens.factmodel.model.FieldMetaModel 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;
}
Also used : TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) ArrayList(java.util.ArrayList) FactMetaModel(org.drools.workbench.screens.factmodel.model.FactMetaModel) DroolsParserException(org.drools.compiler.compiler.DroolsParserException) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr) FieldMetaModel(org.drools.workbench.screens.factmodel.model.FieldMetaModel) TypeFieldDescr(org.drools.compiler.lang.descr.TypeFieldDescr) StringReader(java.io.StringReader) DrlParser(org.drools.compiler.compiler.DrlParser) AnnotationMetaModel(org.drools.workbench.screens.factmodel.model.AnnotationMetaModel) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with FieldMetaModel

use of org.drools.workbench.screens.factmodel.model.FieldMetaModel 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);
    }
}
Also used : ObjectProperty(org.kie.workbench.common.services.datamodeller.core.ObjectProperty) FactMetaModel(org.drools.workbench.screens.factmodel.model.FactMetaModel) FactModels(org.drools.workbench.screens.factmodel.model.FactModels) DataObjectImpl(org.kie.workbench.common.services.datamodeller.core.impl.DataObjectImpl) FieldMetaModel(org.drools.workbench.screens.factmodel.model.FieldMetaModel) ObjectPropertyImpl(org.kie.workbench.common.services.datamodeller.core.impl.ObjectPropertyImpl) DataObject(org.kie.workbench.common.services.datamodeller.core.DataObject) DataModel(org.kie.workbench.common.services.datamodeller.core.DataModel) DataModelImpl(org.kie.workbench.common.services.datamodeller.core.impl.DataModelImpl) AnnotationMetaModel(org.drools.workbench.screens.factmodel.model.AnnotationMetaModel) KieModule(org.kie.workbench.common.services.shared.project.KieModule)

Example 3 with FieldMetaModel

use of org.drools.workbench.screens.factmodel.model.FieldMetaModel 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();
}
Also used : FieldMetaModel(org.drools.workbench.screens.factmodel.model.FieldMetaModel) AnnotationMetaModel(org.drools.workbench.screens.factmodel.model.AnnotationMetaModel)

Aggregations

AnnotationMetaModel (org.drools.workbench.screens.factmodel.model.AnnotationMetaModel)3 FieldMetaModel (org.drools.workbench.screens.factmodel.model.FieldMetaModel)3 FactMetaModel (org.drools.workbench.screens.factmodel.model.FactMetaModel)2 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 DrlParser (org.drools.compiler.compiler.DrlParser)1 DroolsParserException (org.drools.compiler.compiler.DroolsParserException)1 AnnotationDescr (org.drools.compiler.lang.descr.AnnotationDescr)1 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)1 TypeDeclarationDescr (org.drools.compiler.lang.descr.TypeDeclarationDescr)1 TypeFieldDescr (org.drools.compiler.lang.descr.TypeFieldDescr)1 FactModels (org.drools.workbench.screens.factmodel.model.FactModels)1 DataModel (org.kie.workbench.common.services.datamodeller.core.DataModel)1 DataObject (org.kie.workbench.common.services.datamodeller.core.DataObject)1 ObjectProperty (org.kie.workbench.common.services.datamodeller.core.ObjectProperty)1 DataModelImpl (org.kie.workbench.common.services.datamodeller.core.impl.DataModelImpl)1 DataObjectImpl (org.kie.workbench.common.services.datamodeller.core.impl.DataObjectImpl)1 ObjectPropertyImpl (org.kie.workbench.common.services.datamodeller.core.impl.ObjectPropertyImpl)1