Search in sources :

Example 21 with DataModel

use of org.kie.workbench.common.services.datamodeller.core.DataModel in project kie-wb-common by kiegroup.

the class GenerationEngineTest method testJavaClassStringGeneration.

@Test
public void testJavaClassStringGeneration() {
    DataModel dataModel = dataModelOracleDriver.createModel();
    DataObject object1 = dataModel.addDataObject("com.test.Object1");
    DataObject object2 = dataModel.addDataObject("com.test.sub.Object2");
    Annotation label = new AnnotationImpl(annotationDefinitions.get(org.kie.api.definition.type.Label.class.getName()));
    label.setValue("value", "Object1 Label");
    object1.addAnnotation(label);
    Annotation classReactive = new AnnotationImpl(annotationDefinitions.get(org.kie.api.definition.type.ClassReactive.class.getName()));
    object1.addAnnotation(classReactive);
    Annotation propReactive = new AnnotationImpl(annotationDefinitions.get(org.kie.api.definition.type.PropertyReactive.class.getName()));
    object1.addAnnotation(propReactive);
    Annotation role = new AnnotationImpl(annotationDefinitions.get(org.kie.api.definition.type.Role.class.getName()));
    role.setValue("value", org.kie.api.definition.type.Role.Type.EVENT.name());
    object1.addAnnotation(role);
    ObjectProperty prop1 = object1.addProperty("attribute1", "java.lang.String");
    ObjectProperty prop2 = object1.addProperty("attribute2", "java.lang.Boolean");
    ObjectProperty prop3 = object1.addProperty("attribute3", object2.getClassName());
    ObjectProperty prop4 = object1.addProperty("attribute4", "long");
    Annotation key = new AnnotationImpl(annotationDefinitions.get(org.kie.api.definition.type.Key.class.getName()));
    Annotation position = new AnnotationImpl(annotationDefinitions.get(org.kie.api.definition.type.Position.class.getName()));
    position.setValue("value", "0");
    prop4.addAnnotation(key);
    prop4.addAnnotation(position);
    position = new AnnotationImpl(annotationDefinitions.get(org.kie.api.definition.type.Position.class.getName()));
    position.setValue("value", "1");
    prop1.addAnnotation(key);
    prop1.addAnnotation(position);
    position = new AnnotationImpl(annotationDefinitions.get(org.kie.api.definition.type.Position.class.getName()));
    position.setValue("value", "2");
    prop2.addAnnotation(key);
    prop2.addAnnotation(position);
    position = new AnnotationImpl(annotationDefinitions.get(org.kie.api.definition.type.Position.class.getName()));
    position.setValue("value", "3");
    prop3.addAnnotation(position);
    GenerationContext generationContext = new GenerationContext(dataModel);
    try {
        String result = engine.generateJavaClassString(generationContext, object1);
        logger.debug(result);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ObjectProperty(org.kie.workbench.common.services.datamodeller.core.ObjectProperty) AnnotationImpl(org.kie.workbench.common.services.datamodeller.core.impl.AnnotationImpl) Annotation(org.kie.workbench.common.services.datamodeller.core.Annotation) DataObject(org.kie.workbench.common.services.datamodeller.core.DataObject) DataModel(org.kie.workbench.common.services.datamodeller.core.DataModel) Test(org.junit.Test)

Example 22 with DataModel

use of org.kie.workbench.common.services.datamodeller.core.DataModel in project kie-wb-common by kiegroup.

the class GenerationEngineTest method testAllFieldsConstructorStringGeneration.

@Test
public void testAllFieldsConstructorStringGeneration() {
    DataModel dataModel = dataModelOracleDriver.createModel();
    DataObject object1 = dataModel.addDataObject("com.test.Object1");
    DataObject object2 = dataModel.addDataObject("com.test.sub.Object2");
    object1.addProperty("attribute1", "java.lang.String");
    object1.addProperty("attribute2", "java.lang.Boolean");
    object1.addProperty("attribute3", object2.getClassName());
    GenerationContext generationContext = new GenerationContext(dataModel);
    try {
        String result = engine.generateAllFieldsConstructorString(generationContext, object1);
        assertEquals(results.getProperty("testAllFieldsConstructorStringGeneration"), result);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : DataObject(org.kie.workbench.common.services.datamodeller.core.DataObject) DataModel(org.kie.workbench.common.services.datamodeller.core.DataModel) Test(org.junit.Test)

Example 23 with DataModel

use of org.kie.workbench.common.services.datamodeller.core.DataModel in project kie-wb-common by kiegroup.

the class JavaRoasterModelDriver method loadDataObject.

public ModelDriverResult loadDataObject(final String source, final Path path) throws ModelDriverException {
    ModelDriverResult result = new ModelDriverResult();
    DataModel dataModel = createModel();
    result.setDataModel(dataModel);
    if (source == null || "".equals(source)) {
        logger.debug("source: " + source + " is empty.");
        result.addError(new DriverError("Source is empty", Paths.convert(path)));
        return result;
    }
    try {
        JavaType<?> javaType = Roaster.parse(source);
        if (javaType.isClass()) {
            if (javaType.getSyntaxErrors() != null && !javaType.getSyntaxErrors().isEmpty()) {
                // if a file has parsing errors it will be skipped.
                addSyntaxErrors(result, path, javaType.getSyntaxErrors());
            } else {
                try {
                    // try to load the data object.
                    Pair<DataObject, List<ObjectProperty>> pair = parseDataObject((JavaClassSource) javaType);
                    dataModel.addDataObject(pair.getK1());
                    result.setClassPath(pair.getK1().getClassName(), Paths.convert(path));
                    result.setUnmanagedProperties(pair.getK1().getClassName(), pair.getK2());
                } catch (ModelDriverException e) {
                    logger.error("An error was produced when source: " + source + " was being loaded into a DataObject.", e);
                    addModelDriverError(result, path, e);
                }
            }
        } else {
            logger.debug("No Class definition was found for source: " + source + ", it will be skipped.");
        }
    } catch (ParserException e) {
        result.addError(new DriverError(e.getMessage(), Paths.convert(path)));
    } catch (Exception e) {
        // Unexpected exception.
        logger.error(errorMessage(MODEL_LOAD_GENERIC_ERROR, javaRootPath.toUri()), e);
        throw new ModelDriverException(errorMessage(MODEL_LOAD_GENERIC_ERROR, javaRootPath.toUri()), e);
    }
    return result;
}
Also used : ParserException(org.jboss.forge.roaster.ParserException) DataObject(org.kie.workbench.common.services.datamodeller.core.DataObject) DataModel(org.kie.workbench.common.services.datamodeller.core.DataModel) List(java.util.List) ArrayList(java.util.ArrayList) ModelDriverResult(org.kie.workbench.common.services.datamodeller.driver.model.ModelDriverResult) ModelDriverException(org.kie.workbench.common.services.datamodeller.driver.ModelDriverException) ModelDriverException(org.kie.workbench.common.services.datamodeller.driver.ModelDriverException) IOException(java.io.IOException) ParserException(org.jboss.forge.roaster.ParserException) DriverError(org.kie.workbench.common.services.datamodeller.driver.model.DriverError)

Example 24 with DataModel

use of org.kie.workbench.common.services.datamodeller.core.DataModel in project kie-wb-common by kiegroup.

the class JavaRoasterModelDriver method loadModel.

@Override
public ModelDriverResult loadModel() throws ModelDriverException {
    ModelDriverResult result = new ModelDriverResult();
    DataModel dataModel;
    String fileContent;
    dataModel = createModel();
    result.setDataModel(dataModel);
    List<Path> rootPaths = new ArrayList<Path>();
    rootPaths.add(javaRootPath);
    Collection<FileUtils.ScanResult> scanResults = FileUtils.getInstance().scan(ioService, rootPaths, ".java", true);
    if (scanResults != null) {
        for (FileUtils.ScanResult scanResult : scanResults) {
            logger.debug("Starting file loading into model, file: " + scanResult.getFile());
            fileContent = ioService.readAllString(scanResult.getFile());
            if (fileContent == null || "".equals(fileContent)) {
                logger.debug("file: " + scanResult.getFile() + " is empty.");
                result.addError(new DriverError("File has no content", Paths.convert(scanResult.getFile())));
                continue;
            }
            try {
                JavaType<?> javaType = Roaster.parse(fileContent);
                final boolean isManaged = isManagedJavaType(javaType);
                final boolean vetoed = (isManaged ? isVetoed(javaType) : false);
                if (isManaged && !vetoed) {
                    if (javaType.getSyntaxErrors() != null && !javaType.getSyntaxErrors().isEmpty()) {
                        // if a file has parsing errors it will be skipped.
                        addSyntaxErrors(result, scanResult.getFile(), javaType.getSyntaxErrors());
                    } else if (javaType.isEnum()) {
                        loadFromJavaEnum((JavaEnumSource) javaType, scanResult.getFile(), dataModel, result);
                    } else {
                        loadFromJavaClass((JavaClassSource) javaType, scanResult.getFile(), dataModel, result);
                    }
                } else if (vetoed) {
                    logger.debug("The class, {}, in the file, {}, was vetoed and will be skipped.", javaType.getQualifiedName(), scanResult.getFile());
                } else {
                    logger.debug("File: " + scanResult.getFile() + " do not contain a managed java type, it will be skipped.");
                }
            } catch (ParserException e) {
                result.addError(new DriverError(e.getMessage(), Paths.convert(scanResult.getFile())));
            } catch (Exception e) {
                // Unexpected error.
                logger.error(errorMessage(MODEL_LOAD_GENERIC_ERROR, javaRootPath.toUri()), e);
                throw new ModelDriverException(errorMessage(MODEL_LOAD_GENERIC_ERROR, javaRootPath.toUri()), e);
            }
        }
    }
    return result;
}
Also used : Path(org.uberfire.java.nio.file.Path) ParserException(org.jboss.forge.roaster.ParserException) FileUtils(org.kie.workbench.common.services.datamodeller.util.FileUtils) ArrayList(java.util.ArrayList) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) ModelDriverException(org.kie.workbench.common.services.datamodeller.driver.ModelDriverException) ModelDriverException(org.kie.workbench.common.services.datamodeller.driver.ModelDriverException) IOException(java.io.IOException) ParserException(org.jboss.forge.roaster.ParserException) DriverError(org.kie.workbench.common.services.datamodeller.driver.model.DriverError) DataModel(org.kie.workbench.common.services.datamodeller.core.DataModel) JavaEnumSource(org.jboss.forge.roaster.model.source.JavaEnumSource) ModelDriverResult(org.kie.workbench.common.services.datamodeller.driver.model.ModelDriverResult)

Example 25 with DataModel

use of org.kie.workbench.common.services.datamodeller.core.DataModel in project kie-wb-common by kiegroup.

the class DataModelOracleModelDriver method loadModel.

public DataModel loadModel(ModuleDataModelOracle oracleDataModel, ClassLoader moduleClassLoader) throws ModelDriverException {
    DataModel dataModel = createModel();
    logger.debug("Adding oracleDataModel: " + oracleDataModel + " to dataModel: " + dataModel);
    String[] factTypes = DataModelOracleUtilities.getFactTypes(oracleDataModel);
    ObjectSource source = null;
    if (factTypes != null && factTypes.length > 0) {
        for (int i = 0; i < factTypes.length; i++) {
            // skip .drl declared fact types.
            source = factSource(oracleDataModel, factTypes[i]);
            if (source != null && (ObjectSource.INTERNAL.equals(source) || ObjectSource.DEPENDENCY.equals(source))) {
                addFactType(dataModel, oracleDataModel, factTypes[i], source, moduleClassLoader);
            }
        }
    } else {
        logger.debug("oracleDataModel hasn't defined fact types");
    }
    return dataModel;
}
Also used : DataModel(org.kie.workbench.common.services.datamodeller.core.DataModel) ObjectSource(org.kie.workbench.common.services.datamodeller.core.ObjectSource)

Aggregations

DataModel (org.kie.workbench.common.services.datamodeller.core.DataModel)31 DataObject (org.kie.workbench.common.services.datamodeller.core.DataObject)25 Test (org.junit.Test)21 ObjectProperty (org.kie.workbench.common.services.datamodeller.core.ObjectProperty)14 Annotation (org.kie.workbench.common.services.datamodeller.core.Annotation)9 AnnotationImpl (org.kie.workbench.common.services.datamodeller.core.impl.AnnotationImpl)9 ArrayList (java.util.ArrayList)6 ObjectPropertyImpl (org.kie.workbench.common.services.datamodeller.core.impl.ObjectPropertyImpl)6 ModelDriverResult (org.kie.workbench.common.services.datamodeller.driver.model.ModelDriverResult)6 DataModelImpl (org.kie.workbench.common.services.datamodeller.core.impl.DataModelImpl)5 IOException (java.io.IOException)4 KieModule (org.kie.workbench.common.services.shared.project.KieModule)4 List (java.util.List)3 ModelDriverException (org.kie.workbench.common.services.datamodeller.driver.ModelDriverException)3 BufferedReader (java.io.BufferedReader)2 File (java.io.File)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 URI (java.net.URI)2 Arrays (java.util.Arrays)2