You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by pk...@apache.org on 2013/07/17 10:23:19 UTC
svn commit: r1504044 [6/12] - in /uima/sandbox/ruta/trunk/ruta-ep-ide: ./
schema/ src/ src/main/ src/main/antlr3/ src/main/antlr3/org/
src/main/antlr3/org/apache/ src/main/antlr3/org/apache/uima/
src/main/antlr3/org/apache/uima/ruta/ src/main/antlr3/or...
Added: uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/builder/RutaBuilder.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/builder/RutaBuilder.java?rev=1504044&view=auto
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/builder/RutaBuilder.java (added)
+++ uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/builder/RutaBuilder.java Wed Jul 17 08:23:15 2013
@@ -0,0 +1,304 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.ruta.ide.core.builder;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.uima.ruta.engine.RutaEngine;
+import org.apache.uima.ruta.extensions.IEngineLoader;
+import org.apache.uima.ruta.extensions.IRutaActionExtension;
+import org.apache.uima.ruta.extensions.IRutaBooleanFunctionExtension;
+import org.apache.uima.ruta.extensions.IRutaConditionExtension;
+import org.apache.uima.ruta.extensions.IRutaNumberFunctionExtension;
+import org.apache.uima.ruta.extensions.IRutaStringFunctionExtension;
+import org.apache.uima.ruta.extensions.IRutaTypeFunctionExtension;
+import org.apache.uima.ruta.ide.RutaIdeCorePlugin;
+import org.apache.uima.ruta.ide.core.RutaCorePreferences;
+import org.apache.uima.ruta.ide.core.RutaExtensionManager;
+import org.apache.uima.ruta.ide.parser.ast.RutaModuleDeclaration;
+import org.eclipse.core.resources.IContainer;
+import org.eclipse.core.resources.IFolder;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.core.runtime.SubProgressMonitor;
+import org.eclipse.dltk.ast.declarations.ModuleDeclaration;
+import org.eclipse.dltk.core.DLTKCore;
+import org.eclipse.dltk.core.IScriptProject;
+import org.eclipse.dltk.core.ISourceModule;
+import org.eclipse.dltk.core.ModelException;
+import org.eclipse.dltk.core.builder.AbstractBuildParticipantType;
+import org.eclipse.dltk.core.builder.IBuildChange;
+import org.eclipse.dltk.core.builder.IBuildContext;
+import org.eclipse.dltk.core.builder.IBuildParticipant;
+import org.eclipse.dltk.core.builder.IBuildParticipantExtension;
+import org.eclipse.dltk.core.builder.IBuildParticipantExtension2;
+import org.eclipse.dltk.core.builder.IBuildParticipantExtension3;
+import org.eclipse.dltk.core.builder.IBuildState;
+import org.eclipse.jface.preference.IPreferenceStore;
+
+public class RutaBuilder extends AbstractBuildParticipantType implements IBuildParticipant,
+ IBuildParticipantExtension, IBuildParticipantExtension2, IBuildParticipantExtension3 {
+
+ private IScriptProject project;
+
+ public RutaBuilder() {
+ super();
+ }
+
+ public RutaBuilder(IScriptProject project) {
+ super();
+ this.project = project;
+ }
+
+ private void generateDescriptorResources(ISourceModule sourceModule,
+ ModuleDeclaration moduleDeclaration) {
+ IProgressMonitor monitor = createMonitor(10);
+ try {
+ IContainer container = getContainer(sourceModule);
+
+ IPath outputPath = getAbsolutePath(sourceModule);
+ IPath[] generateResources = generateResources(moduleDeclaration, outputPath, container,
+ sourceModule);
+ monitor.worked(2);
+ String defaultDescriptorLocation = RutaProjectUtils.getDefaultDescriptorLocation();
+ IFolder folder = container.getProject().getFolder(defaultDescriptorLocation);
+ for (IPath iPath : generateResources) {
+ iPath = iPath.makeRelativeTo(folder.getLocation());
+ IResource findMember = folder.findMember(iPath);
+ if (findMember != null) {
+ findMember.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
+ findMember.getParent().refreshLocal(IResource.DEPTH_ONE, new NullProgressMonitor());
+ }
+
+ }
+ folder.refreshLocal(IResource.DEPTH_INFINITE, new SubProgressMonitor(monitor,
+ generateResources.length));
+
+ monitor.worked(1);
+ } catch (ModelException e) {
+ if (DLTKCore.DEBUG_PARSER) {
+ e.printStackTrace();
+ }
+ } catch (CoreException e) {
+ if (DLTKCore.DEBUG_PARSER) {
+ e.printStackTrace();
+ }
+ }
+ monitor.done();
+ }
+
+ private IPath[] generateResources(ModuleDeclaration moduleDeclaration, IPath outputPath,
+ IContainer container, ISourceModule sourceModule) {
+ List<IPath> result = new ArrayList<IPath>();
+ if (moduleDeclaration instanceof RutaModuleDeclaration) {
+ RutaModuleDeclaration tmmd = (RutaModuleDeclaration) moduleDeclaration;
+ DescriptorManager sm = tmmd.descriptorInfo;
+ IPath pathToModule = sourceModule.getResource().getLocation();
+ String elementName = RutaProjectUtils.getModuleName(pathToModule);
+
+ IScriptProject proj = sourceModule.getScriptProject();
+ // TODO: dont use desc path!
+ IPath descPath = RutaProjectUtils.getDescriptorRootPath(proj.getProject());
+ IPath scriptPath = RutaProjectUtils.getScriptRootPath(proj.getProject());
+ IPath descPackagePath = RutaProjectUtils.getDescriptorPackagePath(sourceModule.getResource()
+ .getLocation(), proj.getProject());
+
+ String typeSystem = descPackagePath.append(elementName + "TypeSystem.xml").toPortableString();
+ String engine = descPackagePath.append(elementName + "Engine.xml").toPortableString();
+ String basicTS = descPath.append("BasicTypeSystem.xml").toPortableString();
+ String basicE = descPath.append("BasicEngine.xml").toPortableString();
+
+ // TODO: may not work with other script folders
+ IPath relativeTo = pathToModule.makeAbsolute().makeRelativeTo(scriptPath);
+ List<String> scriptPathList = new ArrayList<String>();
+ List<String> descriptorPathList = new ArrayList<String>();
+
+ // add all folders
+ try {
+ List<IFolder> scriptFolders = RutaProjectUtils.getAllScriptFolders(proj);
+ scriptPathList.addAll(RutaProjectUtils.getFolderLocations(scriptFolders));
+ } catch (CoreException e) {
+ RutaIdeCorePlugin.error(e);
+ }
+
+ try {
+ List<IFolder> descriptorFolders = RutaProjectUtils.getAllDescriptorFolders(proj
+ .getProject());
+ descriptorPathList.addAll(RutaProjectUtils.getFolderLocations(descriptorFolders));
+ } catch (Exception e) {
+ RutaIdeCorePlugin.error(e);
+ }
+
+ String[] descriptorPaths = descriptorPathList.toArray(new String[0]);
+ String[] scriptPaths = scriptPathList.toArray(new String[0]);
+ String mainScript = relativeTo.toPortableString();
+ mainScript = mainScript.replaceAll("/", ".");
+ if (mainScript.endsWith(RutaEngine.SCRIPT_FILE_EXTENSION)) {
+ mainScript = mainScript.substring(0, mainScript.length() - 5);
+ }
+ build(basicTS, basicE, typeSystem, engine, sm, mainScript, scriptPaths, descriptorPaths);
+
+ IPath tsPath = Path.fromPortableString(typeSystem);
+ IPath ePath = Path.fromPortableString(engine);
+ result.add(tsPath);
+ result.add(ePath);
+ }
+ return result.toArray(new IPath[0]);
+ }
+
+ private void build(String basicTypesystem, String basicEngine, String typeSystemDest,
+ String engineDest, DescriptorManager sm, String mainScript, String[] scriptPaths,
+ String[] enginePaths) {
+ RutaSimpleBuilder builder = null;
+ try {
+ builder = new RutaSimpleBuilder(basicTypesystem, basicEngine);
+ } catch (Exception e) {
+ DLTKCore.error(e.getMessage(), e);
+ if (DLTKCore.DEBUG_PARSER) {
+ e.printStackTrace();
+ }
+ }
+
+ IRutaConditionExtension[] conditionExtensions = RutaExtensionManager.getDefault()
+ .getRutaConditionExtensions();
+ IRutaActionExtension[] actionExtensions = RutaExtensionManager.getDefault()
+ .getRutaActionExtensions();
+ IRutaBooleanFunctionExtension[] booleanFunctionExtensions = RutaExtensionManager.getDefault()
+ .getRutaBooleanFunctionExtensions();
+ IRutaNumberFunctionExtension[] numberFunctionExtensions = RutaExtensionManager.getDefault()
+ .getRutaNumberFunctionExtensions();
+ IRutaStringFunctionExtension[] stringFunctionExtensions = RutaExtensionManager.getDefault()
+ .getRutaStringFunctionExtensions();
+ IRutaTypeFunctionExtension[] typeFunctionExtensions = RutaExtensionManager.getDefault()
+ .getRutaTypeFunctionExtensions();
+ IEngineLoader[] engineExtensions = RutaExtensionManager.getDefault().getEngineExtensions();
+
+ List<String> language = new ArrayList<String>();
+ List<String> engines = new ArrayList<String>();
+
+ for (IRutaConditionExtension each : conditionExtensions) {
+ language.add(each.getClass().getName());
+ }
+ for (IRutaActionExtension each : actionExtensions) {
+ language.add(each.getClass().getName());
+ }
+ for (IRutaBooleanFunctionExtension each : booleanFunctionExtensions) {
+ language.add(each.getClass().getName());
+ }
+ for (IRutaNumberFunctionExtension each : numberFunctionExtensions) {
+ language.add(each.getClass().getName());
+ }
+ for (IRutaStringFunctionExtension each : stringFunctionExtensions) {
+ language.add(each.getClass().getName());
+ }
+ for (IRutaTypeFunctionExtension each : typeFunctionExtensions) {
+ language.add(each.getClass().getName());
+ }
+ for (IEngineLoader each : engineExtensions) {
+ engines.add(each.getClass().getName());
+ }
+
+ try {
+ RutaBuildOptions option = new RutaBuildOptions(language, engines);
+ IPreferenceStore store = RutaIdeCorePlugin.getDefault().getPreferenceStore();
+ option.setImportByName(store.getBoolean(RutaCorePreferences.BUILDER_IMPORT_BY_NAME));
+ option.setResolveImports(store.getBoolean(RutaCorePreferences.BUILDER_RESOLVE_IMPORTS));
+ builder.build(sm, typeSystemDest, engineDest, option, mainScript, scriptPaths, enginePaths);
+ } catch (Exception e) {
+ DLTKCore.error(e.getMessage(), e);
+ if (DLTKCore.DEBUG_PARSER) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ private IProgressMonitor createMonitor(int totalWork) {
+ IProgressMonitor pm = new NullProgressMonitor();
+ pm.beginTask("Creating descriptors ", totalWork);
+ return pm;
+ }
+
+ public static IContainer getContainer(ISourceModule sourceModule) {
+ try {
+ IResource file = sourceModule.getCorrespondingResource();
+ return file.getParent();
+ } catch (ModelException e) {
+ if (DLTKCore.DEBUG_PARSER) {
+ e.printStackTrace();
+ }
+ }
+ return null;
+ }
+
+ public static IPath getAbsolutePath(ISourceModule sourceModule) {
+ try {
+ IResource file = sourceModule.getCorrespondingResource();
+ IPath absolutePath = file.getRawLocation().removeLastSegments(1);
+ return absolutePath;
+ } catch (ModelException e) {
+ if (DLTKCore.DEBUG_PARSER) {
+ e.printStackTrace();
+ }
+ }
+ return null;
+ }
+
+ public static IPath getRelativePath(ISourceModule sourceModule, String resourceName) {
+ IContainer parent = getContainer(sourceModule);
+ IPath relativePath = parent.getFullPath();
+ IPath relativeFilePath = relativePath.append(resourceName);
+ return relativeFilePath;
+ }
+
+ public void clean() {
+ }
+
+ public void prepare(IBuildChange buildChange, IBuildState buildState) throws CoreException {
+ }
+
+ public void buildExternalModule(IBuildContext context) throws CoreException {
+
+ }
+
+ public boolean beginBuild(int buildType) {
+ return buildType != RECONCILE_BUILD;
+ }
+
+ public void endBuild(IProgressMonitor monitor) {
+
+ }
+
+ public void build(IBuildContext context) throws CoreException {
+ final ModuleDeclaration ast = (ModuleDeclaration) context
+ .get(IBuildContext.ATTR_MODULE_DECLARATION);
+ ISourceModule sourceModule = context.getSourceModule();
+ generateDescriptorResources(sourceModule, ast);
+ }
+
+ @Override
+ public IBuildParticipant createBuildParticipant(IScriptProject project) throws CoreException {
+ return new RutaBuilder(project);
+ }
+}
Added: uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/builder/RutaProjectUtils.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/builder/RutaProjectUtils.java?rev=1504044&view=auto
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/builder/RutaProjectUtils.java (added)
+++ uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/builder/RutaProjectUtils.java Wed Jul 17 08:23:15 2013
@@ -0,0 +1,293 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.ruta.ide.core.builder;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.uima.ruta.engine.RutaEngine;
+import org.apache.uima.ruta.ide.core.RutaNature;
+import org.eclipse.core.resources.IFolder;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IProjectNature;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.QualifiedName;
+import org.eclipse.dltk.core.DLTKCore;
+import org.eclipse.dltk.core.IModelElement;
+import org.eclipse.dltk.core.IScriptFolder;
+import org.eclipse.dltk.core.IScriptProject;
+import org.eclipse.dltk.core.ModelException;
+import org.eclipse.jdt.core.IClasspathEntry;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jdt.internal.core.JavaProject;
+
+public class RutaProjectUtils {
+
+ public static final String JAVANATURE = "org.eclipse.jdt.core.javanature";
+
+ private static final String CDE_DATA_PATH = "CDEdataPath";
+
+ public static IPath getEngineDescriptorPath(IPath scriptPath, IProject project) {
+ String elementName = getModuleName(scriptPath);
+ IPath descPackagePath = getDescriptorPackagePath(scriptPath, project);
+ IPath result = descPackagePath.append(elementName + "Engine.xml");
+ return result;
+ }
+
+ public static IPath getTypeSystemDescriptorPath(IPath scriptPath, IProject project) {
+ String elementName = getModuleName(scriptPath);
+ IPath descPackagePath = getDescriptorPackagePath(scriptPath.makeAbsolute(), project);
+ IPath result = descPackagePath.append(elementName + "TypeSystem.xml");
+ return result;
+ }
+
+ public static IPath getDescriptorRootPath(IProject project) {
+ IPath projectPath = project.getLocation();
+ IPath descPath = projectPath.append(getDefaultDescriptorLocation());
+ return descPath;
+ }
+
+ public static List<IFolder> getAllScriptFolders(IScriptProject proj) throws CoreException {
+ List<IFolder> result = new ArrayList<IFolder>();
+ result.addAll(getScriptFolders(proj));
+ result.addAll(getReferencedScriptFolders(proj));
+ return result;
+ }
+
+ public static List<IFolder> getReferencedScriptFolders(IScriptProject proj) throws CoreException {
+ return getReferencedScriptFolders(proj, new HashSet<IProject>());
+ }
+
+ public static List<IFolder> getReferencedScriptFolders(IScriptProject proj,
+ Collection<IProject> visited) throws CoreException {
+ List<IFolder> result = new ArrayList<IFolder>();
+ IProject[] referencedProjects = proj.getProject().getReferencedProjects();
+ for (IProject eachProject : referencedProjects) {
+ if (!visited.contains(eachProject)) {
+ IScriptProject scriptProject = DLTKCore.create(eachProject);
+ result.addAll(RutaProjectUtils.getScriptFolders(scriptProject));
+ visited.add(eachProject);
+ result.addAll(getReferencedScriptFolders(scriptProject, visited));
+ }
+ }
+ return result;
+ }
+
+ public static List<IFolder> getScriptFolders(IScriptProject proj) {
+ List<IFolder> result = new ArrayList<IFolder>();
+ IScriptFolder[] scriptFolders = null;
+ try {
+ scriptFolders = proj.getScriptFolders();
+ } catch (ModelException e) {
+ // referring to a non-ruta project
+ // RutaIdePlugin.error(e);
+ }
+ if (scriptFolders != null) {
+ for (IScriptFolder eachScriptFolder : scriptFolders) {
+ IModelElement parent = eachScriptFolder.getParent();
+ IResource resource = parent.getResource();
+ if (parent != null && resource != null && resource instanceof IFolder) {
+ if (!result.contains(resource)) {
+ result.add((IFolder) resource);
+ }
+ }
+ }
+ }
+ return result;
+ }
+
+ public static List<IFolder> getAllDescriptorFolders(IProject proj) throws CoreException {
+ List<IFolder> result = new ArrayList<IFolder>();
+ result.addAll(getDescriptorFolders(proj));
+ result.addAll(getReferencedDescriptorFolders(proj));
+ return result;
+ }
+
+ public static List<IFolder> getReferencedDescriptorFolders(IProject proj) throws CoreException {
+ return getReferencedDescriptorFolders(proj, new HashSet<IProject>());
+ }
+
+ public static List<IFolder> getReferencedDescriptorFolders(IProject proj,
+ Collection<IProject> visited) throws CoreException {
+ List<IFolder> result = new ArrayList<IFolder>();
+ Collection<IProject> referencedProjects = getReferencedProjects(proj, new HashSet<IProject>());
+ for (IProject eachProject : referencedProjects) {
+ if (!visited.contains(eachProject)) {
+ result.addAll(RutaProjectUtils.getDescriptorFolders(eachProject));
+ visited.add(eachProject);
+ result.addAll(getReferencedDescriptorFolders(eachProject, visited));
+ }
+ }
+ return result;
+ }
+
+ private static Collection<IProject> getReferencedProjects(IProject proj,
+ Collection<IProject> visited) throws CoreException {
+ Collection<IProject> result = new HashSet<IProject>();
+ IProject[] referencedProjects = proj.getReferencedProjects();
+ result.addAll(Arrays.asList(referencedProjects));
+ IProjectNature nature = proj.getNature(JAVANATURE);
+ if (nature != null) {
+ JavaProject javaProject = (JavaProject) JavaCore.create(proj);
+ IClasspathEntry[] resolvedClasspath = javaProject.getResolvedClasspath();
+ for (IClasspathEntry eachCPE : resolvedClasspath) {
+ if (eachCPE.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
+ IProject project = getProject(eachCPE.getPath());
+ result.add(project);
+ }
+ }
+ }
+ return result;
+ }
+
+ public static List<IFolder> getDescriptorFolders(IProject proj) throws CoreException {
+ List<IFolder> result = new ArrayList<IFolder>();
+ IProjectNature javaNature = proj.getNature(JAVANATURE);
+ if (javaNature != null) {
+ IJavaProject javaProject = JavaCore.create(proj);
+ IPath readOutputLocation = javaProject.readOutputLocation();
+ IFolder folder = ResourcesPlugin.getWorkspace().getRoot().getFolder(readOutputLocation);
+ result.add(folder);
+ }
+ IProjectNature pearNature = proj.getNature("org.apache.uima.pear.UimaNature");
+ if (pearNature != null) {
+ IFolder findElement = proj.getFolder("desc");
+ if (findElement != null) {
+ result.add((IFolder) findElement);
+ }
+ }
+ IProjectNature rutaNature = proj.getNature(RutaNature.NATURE_ID);
+ if (rutaNature != null) {
+ IFolder findElement = proj.getFolder(getDefaultDescriptorLocation());
+ if (findElement != null) {
+ result.add((IFolder) findElement);
+ }
+ }
+ return result;
+ }
+
+ public static List<String> getFolderLocations(List<IFolder> folders) {
+ List<String> result = new ArrayList<String>();
+ if (folders == null)
+ return result;
+ for (IFolder each : folders) {
+ String portableString = each.getLocation().toPortableString();
+ result.add(portableString);
+ }
+ return result;
+ }
+
+ public static IPath getScriptRootPath(IProject project) {
+ IPath projectPath = project.getLocation();
+ IPath descPath = projectPath.append(getDefaultScriptLocation());
+ return descPath;
+ }
+
+ public static IPath getDescriptorPackagePath(IPath scriptPath, IProject project) {
+ IPath projectPath = project.getLocation().makeAbsolute();
+ IPath packagePath = scriptPath.removeLastSegments(1);
+ IPath relativePackagePath;
+ relativePackagePath = packagePath.makeRelativeTo(projectPath).removeFirstSegments(1);
+ IPath descPackagePath = projectPath.append(getDefaultDescriptorLocation());
+ descPackagePath = descPackagePath.append(relativePackagePath);
+ return descPackagePath;
+ }
+
+ public static String getModuleName(IPath path) {
+ String result = path.lastSegment();
+ int lastIndexOf = result.lastIndexOf(RutaEngine.SCRIPT_FILE_EXTENSION);
+ if (lastIndexOf != -1) {
+ result = result.substring(0, lastIndexOf);
+ }
+ return result;
+ }
+
+ public static void addProjectDataPath(IProject project, IFolder folder) throws CoreException {
+ String dataPath = project.getPersistentProperty((new QualifiedName("", CDE_DATA_PATH)));
+ if (dataPath == null) {
+ dataPath = "";
+ }
+ String addon = folder.getLocation().toPortableString();
+ if (!StringUtils.isEmpty(dataPath)) {
+ dataPath += File.pathSeparator;
+ }
+ dataPath += addon;
+ project.setPersistentProperty(new QualifiedName("", CDE_DATA_PATH), dataPath);
+ }
+
+ public static void removeProjectDataPath(IProject project, IFolder folder) throws CoreException {
+ String dataPath = project.getPersistentProperty((new QualifiedName("", CDE_DATA_PATH)));
+ if (dataPath == null) {
+ return;
+ }
+ String path = folder.getLocation().toPortableString();
+ if (!StringUtils.isEmpty(dataPath)) {
+ dataPath.replaceAll(path, "");
+ dataPath.replaceAll(File.pathSeparator + File.pathSeparator, "");
+ }
+ project.setPersistentProperty(new QualifiedName("", CDE_DATA_PATH), dataPath);
+ }
+
+ public static IProject getProject(IPath path) {
+ IResource member = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
+ if (member instanceof IProject) {
+ IProject p = (IProject) member;
+ return p;
+ }
+ return null;
+ }
+
+ public static String getDefaultInputLocation() {
+ return "input";
+ }
+
+ public static String getDefaultOutputLocation() {
+ return "output";
+ }
+
+ public static String getDefaultTestLocation() {
+ return "test";
+ }
+
+ public static String getDefaultScriptLocation() {
+ return "script";
+ }
+
+ public static String getDefaultResourcesLocation() {
+ return "resources";
+ }
+
+ public static String getDefaultDescriptorLocation() {
+ return "descriptor";
+ }
+
+ public static String getDefaultTempTestLocation() {
+ return "temp";
+ }
+}
Added: uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/builder/RutaSimpleBuilder.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/builder/RutaSimpleBuilder.java?rev=1504044&view=auto
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/builder/RutaSimpleBuilder.java (added)
+++ uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/builder/RutaSimpleBuilder.java Wed Jul 17 08:23:15 2013
@@ -0,0 +1,437 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.ruta.ide.core.builder;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.MalformedURLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.uima.ResourceSpecifierFactory;
+import org.apache.uima.UIMAFramework;
+import org.apache.uima.analysis_engine.AnalysisEngineDescription;
+import org.apache.uima.cas.CAS;
+import org.apache.uima.cas.Type;
+import org.apache.uima.cas.TypeSystem;
+import org.apache.uima.resource.ResourceInitializationException;
+import org.apache.uima.resource.ResourceManager;
+import org.apache.uima.resource.impl.ResourceManager_impl;
+import org.apache.uima.resource.metadata.Capability;
+import org.apache.uima.resource.metadata.ConfigurationParameterSettings;
+import org.apache.uima.resource.metadata.FsIndexDescription;
+import org.apache.uima.resource.metadata.Import;
+import org.apache.uima.resource.metadata.TypeDescription;
+import org.apache.uima.resource.metadata.TypeSystemDescription;
+import org.apache.uima.resource.metadata.impl.Import_impl;
+import org.apache.uima.ruta.UIMAConstants;
+import org.apache.uima.ruta.engine.RutaEngine;
+import org.apache.uima.util.CasCreationUtils;
+import org.apache.uima.util.InvalidXMLException;
+import org.apache.uima.util.XMLInputSource;
+import org.apache.uima.util.XMLSerializer;
+import org.apache.uima.util.XMLizable;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Path;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+
+public class RutaSimpleBuilder {
+
+ private static ResourceSpecifierFactory uimaFactory = UIMAFramework.getResourceSpecifierFactory();
+
+ private TypeSystemDescription typeSystemDescription;
+
+ private AnalysisEngineDescription analysisEngineDescription;
+
+ private final String defaultTypeSystem;
+
+ private ResourceManager rm;
+
+ public RutaSimpleBuilder(String defaultTypeSystem, String defaultEngine)
+ throws InvalidXMLException, IOException {
+ super();
+ this.defaultTypeSystem = defaultTypeSystem;
+ initialize(defaultEngine);
+ }
+
+ private void initialize(String defaultEngine) throws InvalidXMLException, IOException {
+ typeSystemDescription = uimaFactory.createTypeSystemDescription();
+ analysisEngineDescription = UIMAFramework.getXMLParser().parseAnalysisEngineDescription(
+ new XMLInputSource(new File(defaultEngine)));
+
+ }
+
+ public void build(DescriptorManager desc, String typeSystemOutput, String engineOutput,
+ RutaBuildOptions option, String mainScript, String[] scriptPaths, String[] enginePaths)
+ throws SAXException, RutaBuildException, InvalidXMLException, IOException,
+ ResourceInitializationException {
+
+ rm = new ResourceManager_impl();
+ String dataPath = "";
+ for (String string : enginePaths) {
+ dataPath += string + File.pathSeparator;
+ }
+ rm.setDataPath(dataPath);
+
+ Map<String, String> typeNameMap = new HashMap<String, String>();
+ Capability capability = uimaFactory.createCapability();
+ // String defaultTypeSystem2 = defaultTypeSystem.replaceAll("/", "\\\\");
+ File defaultTypeSystemFile = new File(defaultTypeSystem);
+ if (!defaultTypeSystemFile.exists()) {
+ System.out.println("Does not exist: " + defaultTypeSystemFile.getName());
+ }
+ TypeSystemDescription initialTypeSystem = UIMAFramework.getXMLParser()
+ .parseTypeSystemDescription(new XMLInputSource(defaultTypeSystemFile));
+ CAS cas = CasCreationUtils.createCas(initialTypeSystem, null, new FsIndexDescription[0]);
+ fillTypeNameMap(typeNameMap, cas.getTypeSystem());
+ cas.release();
+ List<Import> importList = new ArrayList<Import>();
+ Import_impl import_impl = new Import_impl();
+ if (option.isImportByName()) {
+ String name = initialTypeSystem.getName();
+ import_impl.setName(name);
+ } else if (option.isResolveImports()) {
+ String absoluteLocation = initialTypeSystem.getSourceUrlString();
+ import_impl.setLocation(absoluteLocation);
+ } else {
+ String relativeLocation = getRelativeLocation(defaultTypeSystemFile.getAbsolutePath(),
+ typeSystemOutput);
+ import_impl.setLocation(relativeLocation);
+ }
+ importList.add(import_impl);
+ for (String eachName : desc.getImportedTypeSystems()) {
+ String locate = RutaEngine.locate(eachName, enginePaths, ".xml");
+ if (locate == null) {
+ throw new FileNotFoundException("Build process can't find " + eachName + " in "
+ + mainScript);
+ }
+ File file = new File(locate);
+ TypeSystemDescription each = getTypeSystemDescriptor(file, option);
+ if (each != null) {
+ fillTypeNameMap(typeNameMap, each);
+ import_impl = new Import_impl();
+ if (option.isImportByName()) {
+ import_impl.setName(eachName);
+ } else if (option.isResolveImports()) {
+ String absoluteLocation = each.getSourceUrlString();
+ import_impl.setLocation(absoluteLocation);
+ } else {
+ String relativeLocation = getRelativeLocation(file.getAbsolutePath(), typeSystemOutput);
+ import_impl.setLocation(relativeLocation);
+ }
+ importList.add(import_impl);
+ } else {
+ throw new FileNotFoundException("Build process can't find " + eachName + " in "
+ + mainScript);
+ }
+ }
+ for (String eachName : desc.getImportedScripts()) {
+ String locate = RutaEngine.locate(eachName, enginePaths, "TypeSystem.xml");
+ File file = new File(locate);
+ TypeSystemDescription each = getTypeSystemDescriptor(file, option);
+ if (each != null) {
+ fillTypeNameMap(typeNameMap, each);
+ import_impl = new Import_impl();
+ if (option.isImportByName()) {
+ import_impl.setName(eachName + "TypeSystem");
+ } else if (option.isResolveImports()) {
+ String absoluteLocation = each.getSourceUrlString();
+ import_impl.setLocation(absoluteLocation);
+ } else {
+ String relativeLocation = getRelativeLocation(file.getAbsolutePath(), typeSystemOutput);
+ import_impl.setLocation(relativeLocation);
+ }
+ importList.add(import_impl);
+ } else {
+ throw new FileNotFoundException("Build process can't find " + eachName + " in "
+ + mainScript);
+ }
+ }
+
+ Import[] newImports = importList.toArray(new Import[0]);
+ typeSystemDescription.setImports(newImports);
+ if (option.isResolveImports()) {
+ try {
+ typeSystemDescription.resolveImports(rm);
+ } catch (InvalidXMLException e) {
+ throw new RutaBuildException("Failed to resolve imported Type Systems", e);
+ }
+ }
+
+ // TODO hotfixes: where do I get the final types??
+ Set<String> finalTypes = new HashSet<String>();
+ finalTypes.addAll(Arrays.asList(new String[] { "uima.cas.Boolean", "uima.cas.Byte",
+ "uima.cas.Short", "uima.cas.Integer", "uima.cas.Long", "uima.cas.Float", "uima.cas.Double",
+ "uima.cas.BooleanArray", "uima.cas.ByteArray", "uima.cas.ShortArray",
+ "uima.cas.IntegerArray", "uima.cas.LongArray", "uima.cas.FloatArray",
+ "uima.cas.DoubleArray", "uima.cas.StringArray", "uima.cas.FSArray" }));
+
+ int typeIndex = 0;
+ for (String eachType : desc.getTypeShortNames()) {
+ StringTriple typeTriple = desc.getTypeTriples().get(typeIndex);
+ typeTriple = resolveType(typeTriple, typeNameMap, mainScript);
+ if (typeSystemDescription.getType(typeTriple.getName()) != null) {
+ continue;
+ }
+ if (!finalTypes.contains(typeTriple.getParent())) {
+ TypeDescription newType = typeSystemDescription.addType(typeTriple.getName(),
+ typeTriple.getDescription(), typeTriple.getParent());
+ capability.addInputType(typeTriple.getName(), false);
+ capability.addOutputType(typeTriple.getName(), false);
+ Collection<StringTriple> collection = desc.getFeatures().get(eachType);
+ if (collection != null) {
+ for (StringTriple eachFeature : collection) {
+ eachFeature = resolveFeature(eachFeature, typeNameMap);
+ newType.addFeature(eachFeature.getName(), eachFeature.getDescription(),
+ eachFeature.getParent());
+ // capability.addInputFeature(eachFeature.getName());
+ // capability.addOutputFeature(eachFeature.getName());
+ }
+ }
+ }
+ typeIndex++;
+ }
+
+ Set<String> names = new HashSet<String>();
+ List<TypeDescription> types = new ArrayList<TypeDescription>();
+ for (TypeDescription each : typeSystemDescription.getTypes()) {
+ String name = each.getName();
+ if (!names.contains(name)) {
+ names.add(name);
+ types.add(each);
+ }
+ }
+
+ typeSystemDescription.setTypes(types.toArray(new TypeDescription[0]));
+ File typeSystemFile = getFile(typeSystemOutput);
+ typeSystemDescription.setName(mainScript + "TypeSystem");
+ typeSystemDescription.setSourceUrl(typeSystemFile.toURI().toURL());
+ TypeSystemDescription aets = uimaFactory.createTypeSystemDescription();
+ import_impl = new Import_impl();
+ if (option.isImportByName()) {
+ import_impl.setName(typeSystemDescription.getName());
+ } else {
+ String relativeLocation = getRelativeLocation(engineOutput, typeSystemOutput);
+ import_impl.setLocation(relativeLocation);
+ }
+
+ File engineFile = configureEngine(desc, engineOutput, option, mainScript, scriptPaths,
+ enginePaths, capability, import_impl, aets);
+
+ toFile(typeSystemDescription, typeSystemFile);
+ toFile(analysisEngineDescription, engineFile);
+ }
+
+ private void fillTypeNameMap(Map<String, String> typeNameMap, TypeSystem typeSystem) {
+ Iterator<Type> typeIterator = typeSystem.getTypeIterator();
+ while (typeIterator.hasNext()) {
+ Type type = (Type) typeIterator.next();
+ String shortName = type.getShortName();
+ String name = type.getName();
+ typeNameMap.put(shortName, name);
+ }
+ }
+
+ private boolean fillTypeNameMap(Map<String, String> typeNameMap, TypeSystemDescription desc) {
+ boolean contained = false;
+ for (TypeDescription each : desc.getTypes()) {
+ String name = each.getName();
+ int lastIndexOf = name.lastIndexOf(".");
+ String shortName = name.substring(lastIndexOf + 1, name.length());
+ typeNameMap.put(shortName, name);
+ }
+ return contained;
+ }
+
+ private StringTriple resolveFeature(StringTriple eachFeature, Map<String, String> types) {
+ String parent = eachFeature.getParent();
+ String name = eachFeature.getName();
+ parent = translate(parent);
+ if (parent.indexOf(".") == -1 && types.containsKey(parent)) {
+ parent = types.get(parent);
+ }
+ return new StringTriple(name, eachFeature.getDescription(), parent);
+ }
+
+ private StringTriple resolveType(StringTriple typeTriple, Map<String, String> types,
+ String packageName) {
+ String parent = typeTriple.getParent();
+ String name = typeTriple.getName();
+ if (parent == null) {
+ parent = "uima.tcas.Annotation";
+ }
+ parent = translate(parent);
+ name = translate(name);
+ if (parent.indexOf(".") == -1 && types.containsKey(parent)) {
+ parent = types.get(parent);
+ }
+ if (name.indexOf(".") == -1) {
+ if (types.containsKey(name)) {
+ name = types.get(name);
+ } else {
+ name = packageName + "." + name;
+ }
+ }
+ int lastIndexOf = name.lastIndexOf(".");
+ String shortName = name.substring(lastIndexOf + 1, name.length());
+ types.put(shortName, name);
+ return new StringTriple(name, typeTriple.getDescription(), parent);
+ }
+
+ private String translate(String name) {
+ if (name == null) {
+ return null;
+ }
+ if (name.equals("Annotation")) {
+ return "uima.tcas.Annotation";
+ } else if (name.equals("STRING")) {
+ return UIMAConstants.TYPE_STRING;
+ } else if (name.equals("INT")) {
+ return UIMAConstants.TYPE_INTEGER;
+ } else if (name.equals("DOUBLE")) {
+ return UIMAConstants.TYPE_DOUBLE;
+ } else if (name.equals("FLOAT")) {
+ return UIMAConstants.TYPE_FLOAT;
+ } else if (name.equals("BOOLEAN")) {
+ return UIMAConstants.TYPE_BOOLEAN;
+ } else if (name.equals("TYPE")) {
+ return UIMAConstants.TYPE_STRING;
+ }
+ return name;
+ }
+
+ private File configureEngine(DescriptorManager desc, String engineOutput,
+ RutaBuildOptions option, String mainScript, String[] scriptPaths,
+ String[] descriptorPaths, Capability capability, Import_impl import_impl,
+ TypeSystemDescription aets) throws MalformedURLException {
+ aets.setImports(new Import[] { import_impl });
+ analysisEngineDescription.getAnalysisEngineMetaData().setTypeSystem(aets);
+ File file = getFile(engineOutput);
+ analysisEngineDescription.setSourceUrl(file.toURI().toURL());
+
+ if (!desc.getTypeShortNames().isEmpty()) {
+ Capability[] capabilities = analysisEngineDescription.getAnalysisEngineMetaData()
+ .getCapabilities();
+ Capability[] newArray = new Capability[capabilities.length + 1];
+ System.arraycopy(capabilities, 0, newArray, 0, capabilities.length);
+ newArray[capabilities.length] = capability;
+ analysisEngineDescription.getAnalysisEngineMetaData().setCapabilities(newArray);
+ }
+
+ analysisEngineDescription.getAnalysisEngineMetaData().getConfigurationParameterSettings()
+ .setParameterValue(RutaEngine.MAIN_SCRIPT, mainScript);
+ analysisEngineDescription.getAnalysisEngineMetaData().getConfigurationParameterSettings()
+ .setParameterValue(RutaEngine.SCRIPT_PATHS, scriptPaths);
+ analysisEngineDescription.getAnalysisEngineMetaData().getConfigurationParameterSettings()
+ .setParameterValue(RutaEngine.DESCRIPTOR_PATHS, descriptorPaths);
+ String[] parameterValue = (String[]) analysisEngineDescription.getAnalysisEngineMetaData()
+ .getConfigurationParameterSettings().getParameterValue(RutaEngine.RESOURCE_PATHS);
+ Set<String> resourceLocations = new HashSet<String>();
+
+ if (parameterValue != null && parameterValue.length != 0) {
+ resourceLocations.addAll(Arrays.asList(parameterValue));
+ }
+ for (String string : descriptorPaths) {
+ File descDir = new File(string);
+ File defaultResourceDir = new File(descDir.getParent(),
+ RutaProjectUtils.getDefaultResourcesLocation());
+ resourceLocations.add(defaultResourceDir.getAbsolutePath());
+ }
+ analysisEngineDescription.getAnalysisEngineMetaData().getConfigurationParameterSettings()
+ .setParameterValue(RutaEngine.RESOURCE_PATHS, resourceLocations.toArray(new String[0]));
+
+ String[] additionalScriptsArray = desc.getImportedScripts().toArray(new String[] {});
+ analysisEngineDescription.getAnalysisEngineMetaData().getConfigurationParameterSettings()
+ .setParameterValue(RutaEngine.ADDITIONAL_SCRIPTS, additionalScriptsArray);
+
+ String[] additionalEnginesArray = desc.getImportedEngines().toArray(new String[] {});
+ analysisEngineDescription.getAnalysisEngineMetaData().getConfigurationParameterSettings()
+ .setParameterValue(RutaEngine.ADDITIONAL_ENGINES, additionalEnginesArray);
+
+ String[] additionalUimafitEnginesArray = desc.getImportedUimafitEngines().toArray(
+ new String[] {});
+ analysisEngineDescription
+ .getAnalysisEngineMetaData()
+ .getConfigurationParameterSettings()
+ .setParameterValue(RutaEngine.ADDITIONAL_UIMAFIT_ENGINES, additionalUimafitEnginesArray);
+
+ analysisEngineDescription.getAnalysisEngineMetaData().setTypeSystem(typeSystemDescription);
+
+ configureExtensions(option);
+ return file;
+ }
+
+ private String getRelativeLocation(String target, String base) {
+ IPath targetPath = Path.fromOSString(target);
+ IPath basePath = Path.fromPortableString(base);
+ IPath result = targetPath.makeRelativeTo(basePath);
+ // TODO remove the first part! Should be correct in first place!
+ result = result.removeFirstSegments(1);
+ return result.toPortableString();
+ }
+
+ private void configureExtensions(RutaBuildOptions options) {
+ ConfigurationParameterSettings configurationParameterSettings = analysisEngineDescription
+ .getAnalysisEngineMetaData().getConfigurationParameterSettings();
+
+ List<String> languageExtensions = options.getLanguage();
+
+ configurationParameterSettings.setParameterValue(RutaEngine.ADDITIONAL_EXTENSIONS,
+ languageExtensions.toArray(new String[0]));
+ configurationParameterSettings.setParameterValue(RutaEngine.ADDITIONAL_ENGINE_LOADERS, options
+ .getEngines().toArray(new String[0]));
+ }
+
+ private TypeSystemDescription getTypeSystemDescriptor(File file, RutaBuildOptions option)
+ throws InvalidXMLException, IOException {
+ TypeSystemDescription tsdesc = UIMAFramework.getXMLParser().parseTypeSystemDescription(
+ new XMLInputSource(file));
+ if (option.isResolveImports()) {
+ tsdesc.resolveImports(rm);
+ }
+ return tsdesc;
+ }
+
+ private File getFile(String location) {
+ return new File(location);
+ }
+
+ private void toFile(XMLizable desc, File destination) throws SAXException, FileNotFoundException {
+ destination.getParentFile().mkdirs();
+ OutputStream out = new FileOutputStream(destination);
+ XMLSerializer sax = new XMLSerializer(out);
+ ContentHandler ch = sax.getContentHandler();
+ ch.startDocument();
+ desc.toXML(ch);
+ ch.endDocument();
+ }
+
+}
Added: uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/builder/StringTriple.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/builder/StringTriple.java?rev=1504044&view=auto
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/builder/StringTriple.java (added)
+++ uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/builder/StringTriple.java Wed Jul 17 08:23:15 2013
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.ruta.ide.core.builder;
+
+public class StringTriple {
+
+ private final String name;
+
+ private final String description;
+
+ private final String parent;
+
+ public StringTriple(String name, String description, String parent) {
+ super();
+ this.name = name;
+ this.description = description;
+ this.parent = parent;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+
+ public String getParent() {
+ return parent;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == this)
+ return true;
+ if (!(obj instanceof StringTriple)) {
+ return false;
+ }
+ StringTriple t = (StringTriple) obj;
+ return name.equals(t.getName()) && parent.equals(t.getParent());
+ }
+
+ @Override
+ public int hashCode() {
+ return name.hashCode() + parent.hashCode() * 37;
+ }
+}
Added: uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/CompletionOnKeywordArgumentOrFunctionArgument.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/CompletionOnKeywordArgumentOrFunctionArgument.java?rev=1504044&view=auto
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/CompletionOnKeywordArgumentOrFunctionArgument.java (added)
+++ uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/CompletionOnKeywordArgumentOrFunctionArgument.java Wed Jul 17 08:23:15 2013
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.ruta.ide.core.codeassist;
+
+import org.apache.uima.ruta.ide.parser.ast.RutaStatement;
+import org.eclipse.dltk.ast.ASTNode;
+import org.eclipse.dltk.ast.ASTVisitor;
+import org.eclipse.dltk.ast.references.SimpleReference;
+import org.eclipse.dltk.codeassist.complete.ICompletionOnKeyword;
+import org.eclipse.dltk.utils.CorePrinter;
+
+public class CompletionOnKeywordArgumentOrFunctionArgument extends SimpleReference implements
+ ICompletionOnKeyword {
+
+ private String[] possibleKeywords;
+
+ private RutaStatement statement;
+
+ private ASTNode completionNode;
+
+ public CompletionOnKeywordArgumentOrFunctionArgument(String token, ASTNode completionNode,
+ RutaStatement node, String[] KeywordspossibleKeywords) {
+ super(completionNode.sourceStart(), completionNode.sourceEnd(), token);
+ this.possibleKeywords = KeywordspossibleKeywords;
+ this.statement = node;
+ this.completionNode = completionNode;
+ }
+
+ public CompletionOnKeywordArgumentOrFunctionArgument(String token, RutaStatement node,
+ String[] possibleKeywords, int position) {
+ super(position, position, token);
+ this.possibleKeywords = possibleKeywords;
+ this.statement = node;
+ this.completionNode = null;
+ }
+
+ public char[] getToken() {
+ if (getName() != null) {
+ return getName().toCharArray();
+ }
+ return "".toCharArray();
+ }
+
+ public String[] getPossibleKeywords() {
+ return this.possibleKeywords;
+ }
+
+ @Override
+ public void printNode(CorePrinter output) {
+ }
+
+ @Override
+ public void traverse(ASTVisitor pVisitor) throws Exception {
+ }
+
+ public boolean canCompleteEmptyToken() {
+ return true;
+ }
+
+ public RutaStatement getStatement() {
+ return this.statement;
+ }
+
+ public int argumentIndex() {
+ if (this.completionNode == null) {
+ if (this.statement.getCount() == 1) {
+ return 1;
+ }
+ if (statement.getCount() > 2 && statement.getAt(0).sourceEnd() <= sourceStart()
+ && sourceEnd() <= statement.getAt(1).sourceStart()) {
+ return 1;
+ }
+ return -1;
+ }
+ for (int i = 0; i < this.statement.getCount(); ++i) {
+ if (this.statement.getAt(i).equals(this.completionNode)) {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ public ASTNode getCompletionNode() {
+ return this.completionNode;
+ }
+}
Added: uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/CompletionOnKeywordOrFunction.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/CompletionOnKeywordOrFunction.java?rev=1504044&view=auto
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/CompletionOnKeywordOrFunction.java (added)
+++ uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/CompletionOnKeywordOrFunction.java Wed Jul 17 08:23:15 2013
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.ruta.ide.core.codeassist;
+
+import org.eclipse.dltk.ast.ASTNode;
+import org.eclipse.dltk.ast.ASTVisitor;
+import org.eclipse.dltk.ast.references.SimpleReference;
+import org.eclipse.dltk.codeassist.complete.ICompletionOnKeyword;
+import org.eclipse.dltk.utils.CorePrinter;
+
+public class CompletionOnKeywordOrFunction extends SimpleReference implements ICompletionOnKeyword {
+
+ private String[] possibleKeywords;
+
+ public CompletionOnKeywordOrFunction(String token, ASTNode completionNode, ASTNode node,
+ String[] possibleKeywords) {
+ super(completionNode.sourceStart(), completionNode.sourceEnd(), token);
+ this.possibleKeywords = possibleKeywords;
+ }
+
+ public char[] getToken() {
+ return getName().toCharArray();
+ }
+
+ public String[] getPossibleKeywords() {
+ return this.possibleKeywords;
+ }
+
+ @Override
+ public void printNode(CorePrinter output) {
+ }
+
+ @Override
+ public void traverse(ASTVisitor pVisitor) throws Exception {
+ }
+
+ public boolean canCompleteEmptyToken() {
+ return true;
+ }
+}
Added: uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/CompletionOnVariable.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/CompletionOnVariable.java?rev=1504044&view=auto
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/CompletionOnVariable.java (added)
+++ uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/CompletionOnVariable.java Wed Jul 17 08:23:15 2013
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.ruta.ide.core.codeassist;
+
+import org.eclipse.dltk.ast.ASTNode;
+import org.eclipse.dltk.ast.references.SimpleReference;
+
+public class CompletionOnVariable extends SimpleReference {
+ private ASTNode parentNode;
+
+ private ASTNode inNode;
+
+ private ASTNode completionNode;
+
+ private boolean canHandleEmpty;
+
+ private boolean provideDollar = true;
+
+ public CompletionOnVariable(String completionToken, ASTNode completionNode, ASTNode node,
+ ASTNode inNode, boolean canHandleEmpty) {
+ super(completionNode.sourceStart(), completionNode.sourceEnd(), completionToken);
+ this.parentNode = node;
+ this.completionNode = completionNode;
+ this.inNode = inNode;
+ this.canHandleEmpty = canHandleEmpty;
+ }
+
+ public CompletionOnVariable(String completionToken, ASTNode completionNode, ASTNode node,
+ ASTNode inNode, boolean canHandleEmpty, boolean provideDollar) {
+ this(completionToken, completionNode, node, inNode, canHandleEmpty);
+ this.provideDollar = provideDollar;
+ }
+
+ public ASTNode getParentNode() {
+ return this.parentNode;
+ }
+
+ public ASTNode getInNode() {
+ return this.inNode;
+ }
+
+ public ASTNode getCompletionNode() {
+ return this.completionNode;
+ }
+
+ public char[] getToken() {
+ return this.getName().toCharArray();
+ }
+
+ public boolean canHandleEmpty() {
+ return this.canHandleEmpty;
+ }
+
+ public boolean getProvideDollar() {
+ return this.provideDollar;
+ }
+}
Added: uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaASTUtil.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaASTUtil.java?rev=1504044&view=auto
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaASTUtil.java (added)
+++ uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaASTUtil.java Wed Jul 17 08:23:15 2013
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.ruta.ide.core.codeassist;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.uima.ruta.ide.core.parser.RutaParseUtils;
+import org.eclipse.dltk.ast.ASTNode;
+import org.eclipse.dltk.ast.ASTVisitor;
+import org.eclipse.dltk.ast.declarations.MethodDeclaration;
+import org.eclipse.dltk.ast.declarations.ModuleDeclaration;
+import org.eclipse.dltk.ast.declarations.TypeDeclaration;
+import org.eclipse.dltk.ast.expressions.Expression;
+import org.eclipse.dltk.ast.statements.Block;
+import org.eclipse.dltk.core.DLTKCore;
+
+public class RutaASTUtil {
+ public static List getStatements(ASTNode node) {
+ if (node instanceof ModuleDeclaration) {
+ return ((ModuleDeclaration) node).getStatements();
+ } else if (node instanceof TypeDeclaration) {
+ return ((TypeDeclaration) node).getStatements();
+ } else if (node instanceof MethodDeclaration) {
+ return ((MethodDeclaration) node).getStatements();
+ } else if (node instanceof Block) {
+ return ((Block) node).getStatements();
+ } else {
+ final List innerBlockStatements = new ArrayList();
+ // Lets traverse to see inner blocks.
+ ASTVisitor visitor = new ASTVisitor() {
+ @Override
+ public boolean visit(Expression s) throws Exception {
+ if (s instanceof Block) {
+ List tStatements = ((Block) s).getStatements();
+ innerBlockStatements.addAll(tStatements);
+ }
+ return false;
+ }
+
+ @Override
+ public boolean visit(MethodDeclaration s) throws Exception {
+ return false;
+ }
+
+ @Override
+ public boolean visit(TypeDeclaration s) throws Exception {
+ return false;
+ }
+ };
+ try {
+ node.traverse(visitor);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return innerBlockStatements;
+ }
+ // return null;
+ }
+
+ /**
+ * We need to extend all statements to end of lines or to begining of next statement. This is
+ * needed to for correct completion in statements. Such as variable completion and so on.
+ */
+ public static void extendStatements(ASTNode node, String content) {
+ List statements = getStatements(node);
+
+ if (statements != null) {
+ int len = statements.size();
+ for (int i = 0; i < len; ++i) {
+ ASTNode nde = (ASTNode) statements.get(i);
+
+ extendStatement(nde, content);
+ extendStatements(nde, content);
+ }
+ }
+ }
+
+ public static void extendStatement(ASTNode node, String content) {
+ int newValueStart = startLineOrSymbol(node, content);
+ int newValueEnd = endLineOrSymbol(node, content);
+ if (DLTKCore.DEBUG_COMPLETION) {
+ if (node.sourceEnd() != newValueEnd || node.sourceStart() != newValueStart) {
+ // TODO
+ }
+ }
+ node.setStart(newValueStart);
+ node.setEnd(newValueEnd);
+ }
+
+ public static int endLineOrSymbol(ASTNode node, String content) {
+ return RutaParseUtils.endLineOrSymbol(node.sourceEnd(), content);
+ }
+
+ public static int startLineOrSymbol(ASTNode node, String content) {
+ return RutaParseUtils.startLineOrSymbol(node.sourceStart(), content);
+ }
+}
Added: uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaAssistParser.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaAssistParser.java?rev=1504044&view=auto
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaAssistParser.java (added)
+++ uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/core/codeassist/RutaAssistParser.java Wed Jul 17 08:23:15 2013
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.ruta.ide.core.codeassist;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.uima.ruta.ide.core.RutaNature;
+import org.eclipse.dltk.ast.ASTNode;
+import org.eclipse.dltk.ast.declarations.ModuleDeclaration;
+import org.eclipse.dltk.ast.parser.ISourceParser;
+import org.eclipse.dltk.codeassist.IAssistParser;
+import org.eclipse.dltk.core.DLTKLanguageManager;
+
+public abstract class RutaAssistParser implements IAssistParser {
+ public static final int MODULE = 0;// IRutaKeywords.MODULE;
+
+ public static final int NAMESPACE = 1;// IRutaKeywords.NAMESPACE;
+
+ public static final int FUNCTION = 2;// IRutaKeywords.FUNCTION;
+
+ public static final int EXEC_EXPRESSION = 3;// IRutaKeywords.EXEC_EXPRESSION;
+
+ protected ISourceParser parser = null;
+
+ protected ModuleDeclaration module;
+
+ protected ASTNode assistNodeParent = null;
+
+ public RutaAssistParser() {
+ // try {
+ this.parser = DLTKLanguageManager.getSourceParser(RutaNature.NATURE_ID);
+ // } catch (CoreException e) {
+ // if (DLTKCore.DEBUG) {
+ // e.printStackTrace();
+ // }
+ // }
+ }
+
+ public ASTNode getAssistNodeParent() {
+ return assistNodeParent;
+ }
+
+ protected void findElementsTo(List statements, ASTNode node, List elements) {
+ if (statements == null) {
+ return;
+ }
+ Iterator i = statements.iterator();
+ while (i.hasNext()) {
+ ASTNode n = (ASTNode) i.next();
+ if (n.equals(node)) {
+ elements.add(n);
+ return;
+ }
+ if (n.sourceStart() <= node.sourceStart() && node.sourceEnd() <= n.sourceEnd()) {
+ elements.add(n);
+ findElementsTo(RutaASTUtil.getStatements(n), node, elements);
+ return;
+ }
+ }
+
+ }
+
+ protected List findLevelsTo(ASTNode astNodeParent) {
+ List elements = new ArrayList();
+ if (this.module != null || astNodeParent instanceof ModuleDeclaration) {
+ if (this.module == null) {
+ this.module = (ModuleDeclaration) astNodeParent;
+ }
+ elements.add(this.module);
+ findElementsTo(RutaASTUtil.getStatements(this.module), astNodeParent, elements);
+ }
+ return elements;
+ }
+
+ public void setSource(ModuleDeclaration unit) {
+ this.module = unit;
+ }
+
+ // public ModuleDeclaration parse(ISourceModule sourceUnit) {
+ //
+ // module = this.parser.parse(sourceUnit., null);
+ // module.rebuild();
+ //
+ // // RutaASTUtil.extendStatements(module, sourceUnit
+ // // .getSourceContents());
+ //
+ // return module;
+ // }
+
+ public ModuleDeclaration getModule() {
+ return this.module;
+ }
+}