Added: uima/sandbox/ruta/trunk/ruta-ep-addons/src/main/java/org/apache/uima/ruta/check/SetAnnotationModeContributionFactory.java URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-addons/src/main/java/org/apache/uima/ruta/check/SetAnnotationModeContributionFactory.java?rev=1497821&view=auto ============================================================================== --- uima/sandbox/ruta/trunk/ruta-ep-addons/src/main/java/org/apache/uima/ruta/check/SetAnnotationModeContributionFactory.java (added) +++ uima/sandbox/ruta/trunk/ruta-ep-addons/src/main/java/org/apache/uima/ruta/check/SetAnnotationModeContributionFactory.java Fri Jun 28 15:51:38 2013 @@ -0,0 +1,76 @@ +/* + * 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.check; + +import java.util.HashMap; +import java.util.List; + +import org.apache.uima.ruta.addons.RutaAddonsPlugin; +import org.eclipse.swt.SWT; +import org.eclipse.ui.PartInitException; +import org.eclipse.ui.internal.Workbench; +import org.eclipse.ui.menus.CommandContributionItem; +import org.eclipse.ui.menus.CommandContributionItemParameter; +import org.eclipse.ui.menus.ExtensionContributionFactory; +import org.eclipse.ui.menus.IContributionRoot; +import org.eclipse.ui.services.IServiceLocator; + +public class SetAnnotationModeContributionFactory extends ExtensionContributionFactory { + + public SetAnnotationModeContributionFactory() { + super(); + } + + @Override + public void createContributionItems(IServiceLocator serviceLocator, IContributionRoot additions) { + AnnotationCheckView acView; + try { + acView = (AnnotationCheckView) Workbench.getInstance().getActiveWorkbenchWindow() + .getActivePage().showView(AnnotationCheckView.ID); + AnnotationCheckComposite composite = (AnnotationCheckComposite) acView.getComposite(); + List<String> selectedTypes = composite.getSelectedTypes(); + CommandContributionItemParameter pd = new CommandContributionItemParameter(serviceLocator, + "", SetAnnotationModeHandler.MODE, SWT.PUSH); + HashMap<String, String> mapd = new HashMap<String, String>(); + pd.label = "uima.tcas.Annotation"; + mapd.put(SetAnnotationModeHandler.TYPE, pd.label); + pd.parameters = mapd; + pd.icon = RutaAddonsPlugin.getImageDescriptor("icons/font_add.png"); + CommandContributionItem itemp = new CommandContributionItem(pd); + itemp.setVisible(true); + additions.addContributionItem(itemp, null); + for (String each : selectedTypes) { + CommandContributionItemParameter p = new CommandContributionItemParameter(serviceLocator, + "", SetAnnotationModeHandler.MODE, SWT.PUSH); + p.label = each; + p.icon = RutaAddonsPlugin.getImageDescriptor("icons/font_add.png"); + HashMap<String, String> map = new HashMap<String, String>(); + map.put(SetAnnotationModeHandler.TYPE, p.label); + p.parameters = map; + CommandContributionItem item = new CommandContributionItem(p); + item.setVisible(true); + additions.addContributionItem(item, null); + } + } catch (PartInitException e) { + RutaAddonsPlugin.error(e); + } + } + +}
Added: uima/sandbox/ruta/trunk/ruta-ep-addons/src/main/java/org/apache/uima/ruta/check/SetAnnotationModeHandler.java URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-addons/src/main/java/org/apache/uima/ruta/check/SetAnnotationModeHandler.java?rev=1497821&view=auto ============================================================================== --- uima/sandbox/ruta/trunk/ruta-ep-addons/src/main/java/org/apache/uima/ruta/check/SetAnnotationModeHandler.java (added) +++ uima/sandbox/ruta/trunk/ruta-ep-addons/src/main/java/org/apache/uima/ruta/check/SetAnnotationModeHandler.java Fri Jun 28 15:51:38 2013 @@ -0,0 +1,97 @@ +/* + * 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.check; + +import java.util.Map; + +import org.apache.uima.ruta.addons.RutaAddonsPlugin; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.commands.IHandler; +import org.eclipse.core.commands.IHandlerListener; +import org.eclipse.core.runtime.Status; +import org.eclipse.ui.IWorkbenchWindow; +import org.eclipse.ui.PartInitException; +import org.eclipse.ui.commands.ICommandService; +import org.eclipse.ui.commands.IElementUpdater; +import org.eclipse.ui.handlers.HandlerUtil; +import org.eclipse.ui.menus.UIElement; + +public class SetAnnotationModeHandler implements IHandler, IElementUpdater { + + public static final String TYPE = SetAnnotationModeHandler.MODE + ".type"; + + public static final String MODE = "org.apache.uima.ruta.check.mode"; + + private String mode = "Annotation"; + + public void addHandlerListener(IHandlerListener arg0) { + + } + + public void dispose() { + + } + + public Object execute(ExecutionEvent event) throws ExecutionException { + String typeString = event.getParameter(TYPE); + if (typeString != null) { + + AnnotationCheckView acView; + try { + IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event); + acView = (AnnotationCheckView) window.getActivePage().showView(AnnotationCheckView.ID); + + AnnotationCheckComposite composite = (AnnotationCheckComposite) acView.getComposite(); + composite.setAnnotationMode(typeString); + String[] split = typeString.split("[.]"); + mode = split[split.length - 1]; + ICommandService commandService = (ICommandService) window.getService(ICommandService.class); + if (commandService != null) { + commandService.refreshElements("org.apache.uima.ruta.check.mode", null); + } + } catch (PartInitException e) { + RutaAddonsPlugin.error(e); + return Status.CANCEL_STATUS; + } + } + return Status.OK_STATUS; + } + + public void updateElement(UIElement element, @SuppressWarnings("rawtypes") + Map parameters) { + if (parameters.get(TYPE) == null) { + element.setText(mode); + } + } + + public boolean isEnabled() { + return true; + } + + public boolean isHandled() { + return true; + } + + public void removeHandlerListener(IHandlerListener arg0) { + + } + +} Added: uima/sandbox/ruta/trunk/ruta-ep-addons/src/main/java/org/apache/uima/ruta/check/UpdateTaskHandler.java URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-addons/src/main/java/org/apache/uima/ruta/check/UpdateTaskHandler.java?rev=1497821&view=auto ============================================================================== --- uima/sandbox/ruta/trunk/ruta-ep-addons/src/main/java/org/apache/uima/ruta/check/UpdateTaskHandler.java (added) +++ uima/sandbox/ruta/trunk/ruta-ep-addons/src/main/java/org/apache/uima/ruta/check/UpdateTaskHandler.java Fri Jun 28 15:51:38 2013 @@ -0,0 +1,252 @@ +/* + * 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.check; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FilenameFilter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.apache.uima.UIMAFramework; +import org.apache.uima.cas.CAS; +import org.apache.uima.cas.impl.XmiCasDeserializer; +import org.apache.uima.cas.text.AnnotationFS; +import org.apache.uima.cas.text.AnnotationIndex; +import org.apache.uima.caseditor.ide.TypeSystemLocationPropertyPage; +import org.apache.uima.resource.ResourceInitializationException; +import org.apache.uima.resource.metadata.FsIndexDescription; +import org.apache.uima.resource.metadata.TypeSystemDescription; +import org.apache.uima.ruta.addons.RutaAddonsPlugin; +import org.apache.uima.util.CasCreationUtils; +import org.apache.uima.util.InvalidXMLException; +import org.apache.uima.util.XMLInputSource; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.commands.IHandler; +import org.eclipse.core.commands.IHandlerListener; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.jobs.Job; +import org.eclipse.jface.viewers.TreePath; +import org.eclipse.jface.viewers.TreeSelection; +import org.eclipse.jface.viewers.TreeViewer; +import org.eclipse.ui.handlers.HandlerUtil; +import org.xml.sax.SAXException; + +public class UpdateTaskHandler implements IHandler { + + private class CheckAnnotationUpdateTaskJob extends Job { + + private String documentSource; + + private String typeSystem; + + private AnnotationCheckComposite composite; + + private String documentSink; + + CheckAnnotationUpdateTaskJob(String documentSource, String documentSink, String typeSystem, + AnnotationCheckComposite composite) { + super("Update annotation check task..."); + this.documentSource = documentSource; + this.documentSink = documentSink; + this.typeSystem = typeSystem; + this.composite = composite; + } + + CheckAnnotationUpdateTaskJob(AnnotationCheckComposite composite) { + this(composite.getDocumentSource(), composite.getDocumentSink(), composite.getTypeSystem(), + composite); + } + + public IStatus run(IProgressMonitor monitor) { + List<String> selectedTypes = composite.getSelectedTypes(); + + File dir = new File(documentSource); + File[] listFiles = dir.listFiles(new FilenameFilter() { + public boolean accept(File file, String string) { + return string.endsWith(".xmi"); + } + }); + if (listFiles == null) { + return Status.CANCEL_STATUS; + } + + File dataFile = new File(documentSink, "data.xml"); + List<CheckDocument> docs = new ArrayList<CheckDocument>(); + try { + docs = XMLUtils.read(dataFile); + } catch (SAXException e) { + RutaAddonsPlugin.error(e); + } catch (IOException e) { + RutaAddonsPlugin.error(e); + } + setDefaultTypeSystem(documentSource, typeSystem); + TypeSystemDescription tsd = null; + try { + tsd = UIMAFramework.getXMLParser().parseTypeSystemDescription( + new XMLInputSource(typeSystem)); + tsd.resolveImports(); + } catch (InvalidXMLException e) { + RutaAddonsPlugin.error(e); + } catch (IOException e) { + RutaAddonsPlugin.error(e); + } + CAS cas = null; + try { + cas = CasCreationUtils.createCas(tsd, null, new FsIndexDescription[0]); + } catch (ResourceInitializationException e) { + RutaAddonsPlugin.error(e); + } + + if (tsd == null || cas == null) { + return Status.CANCEL_STATUS; + } + TreePath treePath = null; + final IAnnotationCheckTreeNode root = new AnnotationCheckRootNode(); + for (File file : listFiles) { + cas.reset(); + try { + XmiCasDeserializer.deserialize(new FileInputStream(file), cas, true); + } catch (FileNotFoundException e) { + RutaAddonsPlugin.error(e); + } catch (SAXException e) { + RutaAddonsPlugin.error(e); + } catch (IOException e) { + RutaAddonsPlugin.error(e); + } + CheckDocument element = getCheckDocument(docs, file.getAbsolutePath()); + boolean documentAlreadyDone = documentAlreadyDoneforTypes(element, selectedTypes); + if (!documentAlreadyDone) { + AnnotationCheckTreeNode node = new AnnotationCheckTreeNode(root, element); + root.addChild(node); + AnnotationIndex<AnnotationFS> annotationIndex = cas.getAnnotationIndex(); + for (AnnotationFS each : annotationIndex) { + boolean annotationAlreadyDoneforTypes = annotationAlreadyDoneforTypes(each, + selectedTypes, element.checkedTypes); + if (!annotationAlreadyDoneforTypes) { + CheckElement ac = new CheckAnnotation(each); + AnnotationCheckTreeNode anode = new AnnotationCheckTreeNode(node, ac); + if (treePath == null) { + treePath = new TreePath(new Object[] { root, node, anode }); + } + node.addChild(anode); + } + } + if (treePath == null) { + treePath = new TreePath(new Object[] { root, node }); + } + } + } + final TreeSelection firstSelection = new TreeSelection(treePath); + cas.release(); + composite.setOldDocs(docs); + composite.getDisplay().asyncExec(new Runnable() { + public void run() { + TreeViewer treeView = composite.getTreeViewer(); + treeView.setInput(root); + treeView.setSelection(firstSelection, true); + } + }); + + return Status.OK_STATUS; + } + + private boolean documentAlreadyDoneforTypes(CheckDocument element, List<String> selectedTypes) { + if(element.checkedTypes.isEmpty() && !selectedTypes.isEmpty()) { + return false; + } + + boolean allCovered = true; + for (String each : selectedTypes) { + allCovered &= element.checkedTypes.contains(each); + } + return allCovered; + } + + private boolean annotationAlreadyDoneforTypes(AnnotationFS each, + Collection<String> selectedTypes, Collection<String> alreadyDone) { + String name = each.getType().getName(); + if(alreadyDone.contains(name)) { + return true; + } + return !selectedTypes.contains(name); + } + + private CheckDocument getCheckDocument(List<CheckDocument> docs, String absolutePath) { + for (CheckDocument checkDocument : docs) { + if (checkDocument.source.equals(absolutePath)) { + return checkDocument; + } + } + return new CheckDocument(absolutePath); + } + + public void setDefaultTypeSystem(String documentSource, String typeSystemLocation) { + IFile dsFile = CheckAnnotationUtils.getIFile(documentSource); + IFile tsFile = CheckAnnotationUtils.getIFile(typeSystemLocation); + IProject project = dsFile.getProject(); + String portableString = tsFile.getFullPath().toPortableString(); + TypeSystemLocationPropertyPage.setTypeSystemLocation(project, portableString); + } + + } + + public void addHandlerListener(IHandlerListener arg0) { + } + + public void dispose() { + } + + public Object execute(ExecutionEvent event) throws ExecutionException { + AnnotationCheckView acView; + try { + acView = (AnnotationCheckView) HandlerUtil.getActiveWorkbenchWindow(event).getWorkbench() + .getActiveWorkbenchWindow().getActivePage().showView(AnnotationCheckView.ID); + AnnotationCheckComposite composite = (AnnotationCheckComposite) acView.getComposite(); + CheckAnnotationUpdateTaskJob job = new CheckAnnotationUpdateTaskJob(composite); + job.schedule(); + } catch (Exception e) { + RutaAddonsPlugin.error(e); + return Status.CANCEL_STATUS; + } + return Status.OK_STATUS; + } + + public boolean isEnabled() { + return true; + } + + public boolean isHandled() { + return true; + } + + public void removeHandlerListener(IHandlerListener arg0) { + + } + +} Added: uima/sandbox/ruta/trunk/ruta-ep-addons/src/main/java/org/apache/uima/ruta/check/XMLUtils.java URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-addons/src/main/java/org/apache/uima/ruta/check/XMLUtils.java?rev=1497821&view=auto ============================================================================== --- uima/sandbox/ruta/trunk/ruta-ep-addons/src/main/java/org/apache/uima/ruta/check/XMLUtils.java (added) +++ uima/sandbox/ruta/trunk/ruta-ep-addons/src/main/java/org/apache/uima/ruta/check/XMLUtils.java Fri Jun 28 15:51:38 2013 @@ -0,0 +1,63 @@ +/* + * 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.check; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.uima.util.FileUtils; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.XMLReader; +import org.xml.sax.helpers.XMLReaderFactory; + +public class XMLUtils { + + public static void write(List<CheckDocument> docs, File file) throws IOException { + StringBuilder sb = new StringBuilder(); + sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); + sb.append("\n"); + sb.append("<documents>"); + sb.append("\n"); + for (CheckDocument checkDocument : docs) { + sb.append(checkDocument.toXML()); + } + sb.append("</documents>"); + FileUtils.saveString2File(sb.toString(), file, "UTF-8"); + } + + public static List<CheckDocument> read(File file) throws SAXException, IOException { + if(file == null || !file.exists()) { + return new ArrayList<CheckDocument>(); + } + XMLReader xmlReader = XMLReaderFactory.createXMLReader(); + FileReader reader = new FileReader(file); + InputSource inputSource = new InputSource(reader); + CheckDocumentsContentHandler handler = new CheckDocumentsContentHandler(); + xmlReader.setContentHandler(handler); + xmlReader.parse(inputSource); + return handler.getCheckDocuments(); + } + + +}