Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • deniz.cingoez/robocup-speechrec
  • t.dilger/robocup-speechrec
  • msterz/robocup-speechrec
  • patric.steckstor/robocup-speechrec
  • jennifer.lee/robocup-speechrec
  • jharin/robocup-speechrec
  • f.thiemer/robocup-speechrec
  • augustin.harter/robocup-speechrec
  • jleichert/robocup-speechrec
9 results
Show changes
Showing
with 0 additions and 2624 deletions
package hterhors.editor.markers;
import java.util.List;
public class MarkerContainer {
private static List<ISRValidationObject> errors = null;
private static List<ISRValidationObject> warnings = null;
private static int hashCode = 0;
private static void calcErrAndWarn(String document) {
isDirty(document);
errors = IsrRuleValidator.parseErrors(document);
warnings = IsrRuleValidator.parseWarnings(document);
}
public synchronized static boolean isDirty(String document) {
int tmp = document.hashCode();
if (tmp == hashCode) {
return false;
} else {
hashCode = tmp;
return true;
}
}
public static List<ISRValidationObject> getErrors(String document) {
calcErrAndWarn(document);
return errors;
}
public static List<ISRValidationObject> getWarnings(String document) {
calcErrAndWarn(document);
return warnings;
}
}
package hterhors.editor.markers;
import java.util.List;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.IDocument;
/**
* @author Hendrik
*/
public class MarkingErrorHandler {
private IFile file;
private IDocument document;
public MarkingErrorHandler(IFile file, IDocument document) {
super();
this.file = file;
this.document = document;
}
/**
* @param document
* @uml.property name="document"
*/
public void setDocument(IDocument document) {
this.document = document;
}
public synchronized void removeExistingMarker() {
try {
file.deleteMarkers(IMarker.PROBLEM, true, IResource.DEPTH_ZERO);
} catch (CoreException e1) {
e1.printStackTrace();
}
}
public synchronized void setCurrentMarker() {
final List<ISRValidationObject> warns = MarkerContainer
.getWarnings(document.get());
final List<ISRValidationObject> errs = MarkerContainer
.getErrors(document.get());
/*
* Avoid ConcurrentModificationException
*/
final ISRValidationObject[] warnsArray;
warnsArray = warns.toArray(new ISRValidationObject[warns.size()]);
final ISRValidationObject[] errsArray;
errsArray = errs.toArray(new ISRValidationObject[errs.size()]);
for (final ISRValidationObject warn : warnsArray) {
try {
final IMarker marker;
marker = file.createMarker(IMarker.PROBLEM);
marker.setAttribute(IMarker.LINE_NUMBER,
warn.getLineNumber() + 1);
marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_WARNING);
marker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_NORMAL);
marker.setAttribute(IMarker.MESSAGE, warn.getErrorMessage());
marker.setAttribute(IMarker.CHAR_START, warn.getColumnStart());
marker.setAttribute(IMarker.CHAR_END, warn.getColumnEnd());
} catch (CoreException e) {
e.printStackTrace();
}
}
for (final ISRValidationObject error : errsArray) {
try {
final IMarker marker;
marker = file.createMarker(IMarker.PROBLEM);
marker.setAttribute(IMarker.LINE_NUMBER,
error.getLineNumber() + 1);
marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
marker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_HIGH);
marker.setAttribute(IMarker.MESSAGE, error.getErrorMessage());
marker.setAttribute(IMarker.CHAR_START, error.getColumnStart());
marker.setAttribute(IMarker.CHAR_END, error.getColumnEnd());
} catch (CoreException e) {
e.printStackTrace();
}
}
}
}
\ No newline at end of file
package hterhors.editor.outline;
import hterhors.editor.xml.XMLElement;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
/**
* @author Hendrik
*/
public class EditorContentOutlinePage extends ContentOutlinePage {
private ITextEditor editor;
private IEditorInput input;
/**
* @uml.property name="outlineContentProvider"
* @uml.associationEnd
*/
private OutlineContentProvider outlineContentProvider;
/**
* @uml.property name="outlineLabelProvider"
* @uml.associationEnd
*/
private OutlineLabelProvider outlineLabelProvider;
public EditorContentOutlinePage(ITextEditor editor) {
super();
this.editor = editor;
}
public void createControl(Composite parent) {
super.createControl(parent);
TreeViewer viewer = getTreeViewer();
outlineContentProvider = new OutlineContentProvider(
editor.getDocumentProvider());
viewer.setContentProvider(outlineContentProvider);
outlineLabelProvider = new OutlineLabelProvider();
viewer.setLabelProvider(outlineLabelProvider);
viewer.addSelectionChangedListener(this);
if (input != null)
viewer.setInput(input);
}
public void setInput(Object input) {
this.input = (IEditorInput) input;
update();
}
public void selectionChanged(SelectionChangedEvent event) {
super.selectionChanged(event);
ISelection selection = event.getSelection();
if (selection.isEmpty())
editor.resetHighlightRange();
else {
XMLElement element = (XMLElement) ((IStructuredSelection) selection)
.getFirstElement();
int start = element.getPosition().getOffset();
int length = element.getPosition().getLength();
try {
editor.setHighlightRange(start, length, true);
} catch (IllegalArgumentException x) {
editor.resetHighlightRange();
}
}
}
public void update() {
TreeViewer viewer = getTreeViewer();
if (viewer != null) {
Control control = viewer.getControl();
if (control != null && !control.isDisposed()) {
control.setRedraw(false);
viewer.setInput(input);
viewer.expandAll();
control.setRedraw(true);
}
}
}
}
\ No newline at end of file
package hterhors.editor.outline;
import hterhors.editor.xml.RootElement;
import hterhors.editor.xml.XMLAttribute;
import hterhors.editor.xml.XMLElement;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.BadPositionCategoryException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.Position;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
* @author Hendrik
*/
public class OutlineContentHandler extends DefaultHandler implements
ContentHandler {
/**
* @uml.property name="dtdTree"
* @uml.associationEnd
*/
private RootElement dtdTree;
/**
* @uml.property name="dtdElement"
* @uml.associationEnd
*/
private XMLElement dtdElement;
private Locator locator;
private IDocument document;
private String positionCategory;
public OutlineContentHandler() {
super();
}
public void setDocumentLocator(Locator locator) {
this.locator = locator;
}
public void startElement(String namespace, String localname, String qName,
Attributes attributes) throws SAXException {
int lineNumber = locator.getLineNumber() - 1;
XMLElement element = new XMLElement(localname);
int startPosition = getOffsetFromLine(lineNumber);
Position position = new Position(startPosition);
addPosition(position);
element.setPosition(position);
if (dtdTree == null) {
this.dtdTree = new RootElement();
this.dtdTree.setRootElement(element);
}
if (attributes != null) {
int attributeLength = attributes.getLength();
for (int i = 0; i < attributeLength; i++) {
String value = attributes.getValue(i);
String localName = attributes.getLocalName(i);
element.addChildAttribute(new XMLAttribute(localName, value));
}
}
if (dtdElement != null)
dtdElement.addChildElement(element);
dtdElement = element;
}
public void endElement(String namespace, String localname, String qName)
throws SAXException {
int lineNumber = locator.getLineNumber();
int endPosition = getOffsetFromLine(lineNumber);
if (dtdElement != null) {
Position position = dtdElement.getPosition();
int length = endPosition - position.getOffset();
position.setLength(length);
dtdElement = dtdElement.getParent();
}
}
private void addPosition(Position position) {
try {
document.addPosition(positionCategory, position);
} catch (BadLocationException e) {
e.printStackTrace();
} catch (BadPositionCategoryException e) {
e.printStackTrace();
}
}
public void endDocument() throws SAXException {
super.endDocument();
}
private int getOffsetFromLine(int lineNumber) {
int offset = 0;
try {
offset = document.getLineOffset(lineNumber);
} catch (BadLocationException e) {
try {
offset = document.getLineOffset(lineNumber - 1);
} catch (BadLocationException e1) {
}
}
return offset;
}
public XMLElement getRootElement() {
return dtdTree.getRootElement();
}
/**
* @param document
* @uml.property name="document"
*/
public void setDocument(IDocument document) {
this.document = document;
}
/**
* @param positionCategory
* @uml.property name="positionCategory"
*/
public void setPositionCategory(String positionCategory) {
this.positionCategory = positionCategory;
}
}
\ No newline at end of file
package hterhors.editor.outline;
import hterhors.editor.xml.XMLElement;
import hterhors.editor.xml.XMLParser;
import hterhors.editor.xmlconverter.IsrFileToXmlFileUsingXOM;
import java.util.List;
import org.eclipse.jface.text.BadPositionCategoryException;
import org.eclipse.jface.text.DefaultPositionUpdater;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IPositionUpdater;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.xml.sax.helpers.LocatorImpl;
/**
* @author Hendrik
*/
public class OutlineContentProvider implements ITreeContentProvider {
/**
* @uml.property name="root"
* @uml.associationEnd
*/
private XMLElement root = null;
private IEditorInput input;
private IDocumentProvider documentProvider;
protected final static String TAG_POSITIONS = "__tag_positions";
protected IPositionUpdater positionUpdater = new DefaultPositionUpdater(
TAG_POSITIONS);
public OutlineContentProvider(IDocumentProvider provider) {
super();
this.documentProvider = provider;
}
public Object[] getChildren(Object parentElement) {
if (parentElement == input) {
if (root == null)
return new Object[0];
List childrenDTDElements = root.getChildrenDTDElements();
if (childrenDTDElements != null)
return childrenDTDElements.toArray();
} else {
XMLElement parent = (XMLElement) parentElement;
List childrenDTDElements = parent.getChildrenDTDElements();
if (childrenDTDElements != null)
return childrenDTDElements.toArray();
}
return new Object[0];
}
public Object getParent(Object element) {
if (element instanceof XMLElement)
return ((XMLElement) element).getParent();
return null;
}
public boolean hasChildren(Object element) {
if (element == input)
return true;
else {
return ((XMLElement) element).getChildrenDTDElements().size() > 0;
}
}
public Object[] getElements(Object inputElement) {
if (root == null)
return new Object[0];
List childrenDTDElements = root.getChildrenDTDElements();
if (childrenDTDElements != null)
return childrenDTDElements.toArray();
return new Object[0];
}
public void dispose() {
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
if (oldInput != null) {
IsrFileToXmlFileUsingXOM converter = new IsrFileToXmlFileUsingXOM(
true);
IDocument xmlDoc = converter.convertFile(documentProvider
.getDocument(oldInput));
if (xmlDoc != null) {
try {
xmlDoc.removePositionCategory(TAG_POSITIONS);
} catch (BadPositionCategoryException x) {
}
xmlDoc.removePositionUpdater(positionUpdater);
}
}
input = (IEditorInput) newInput;
if (newInput != null) {
IsrFileToXmlFileUsingXOM converter = new IsrFileToXmlFileUsingXOM(
true);
IDocument xmlDoc = converter.convertFile(documentProvider
.getDocument(newInput));
if (xmlDoc != null) {
xmlDoc.addPositionCategory(TAG_POSITIONS);
xmlDoc.addPositionUpdater(positionUpdater);
XMLElement rootElement = parseRootElement(xmlDoc);
if (rootElement != null) {
root = rootElement;
}
}
}
}
private XMLElement parseRootElement(IDocument document) {
String text = document.get();
XMLElement tagPositions = parseRootElements(text, document);
return tagPositions;
}
private XMLElement parseRootElements(String text, IDocument document) {
try {
XMLParser xmlParser = new XMLParser();
OutlineContentHandler contentHandler = new OutlineContentHandler();
contentHandler.setDocument(document);
contentHandler.setPositionCategory(TAG_POSITIONS);
contentHandler.setDocumentLocator(new LocatorImpl());
xmlParser.setContentHandler(contentHandler);
xmlParser.doParse(text);
XMLElement root = contentHandler.getRootElement();
return root;
} catch (Exception e) {
return null;
}
}
}
package hterhors.editor.outline;
import hterhors.editor.xml.XMLElement;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.swt.graphics.Image;
public class OutlineLabelProvider implements ILabelProvider {
public OutlineLabelProvider() {
super();
}
public Image getImage(Object element) {
return null;
}
public String getText(Object element) {
if (element instanceof XMLElement) {
XMLElement dtdElement = (XMLElement) element;
String textToShow = dtdElement.getName();
String nameAttribute = dtdElement.getAttributeValue("id");
if (nameAttribute != null) {
if (textToShow.equals("token")) {
textToShow = nameAttribute;
} else if (textToShow.equals("one-of")
|| textToShow.equals("item")) {
} else {
textToShow += " id =\"" + nameAttribute + "\"";
}
}
return textToShow;
}
return null;
}
public void dispose() {
}
public boolean isLabelProperty(Object element, String property) {
return false;
}
public void removeListener(ILabelProviderListener listener) {
}
@Override
public void addListener(ILabelProviderListener arg0) {
}
}
\ No newline at end of file
package hterhors.editor.perspective;
import org.eclipse.ui.IFolderLayout;
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;
public class IsrPerspective implements IPerspectiveFactory {
@Override
public void createInitialLayout(IPageLayout layout) {
String editorArea = layout.getEditorArea();
IFolderLayout topLeft = layout.createFolder("topLeft",
IPageLayout.LEFT, 0.25f, editorArea);
topLeft.addView(IPageLayout.ID_PROJECT_EXPLORER);
IFolderLayout bottomLeft = layout.createFolder("bottomLeft",
IPageLayout.BOTTOM, 0.50f, "topLeft");
bottomLeft.addView(IPageLayout.ID_PROBLEM_VIEW);
layout.addView(IPageLayout.ID_OUTLINE, IPageLayout.RIGHT, 0.85f,
editorArea);
}
}
package hterhors.editor.scanners;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.text.rules.IPredicateRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.MultiLineRule;
import org.eclipse.jface.text.rules.RuleBasedPartitionScanner;
import org.eclipse.jface.text.rules.Token;
import hterhors.editor.ISRDocumentProvider;
public class ISRPartitionScanner extends RuleBasedPartitionScanner {
public final static String ISR_RULE = "isr_rule";
// public final static String ISR_START = "isr_start";
public final static String ISR_IGNORE = "isr_ignore";
public ISRPartitionScanner() {
IToken isr_rule = new Token(ISR_RULE);
// IToken isr_start = new Token(ISR_START);
IToken isr_ignore = new Token(ISR_IGNORE);
List<IPredicateRule> rules = new ArrayList<IPredicateRule>();
// rules.add(new MultiLineRule("$$S :", ";", isr_start));
rules.add(new MultiLineRule("$", ";", isr_rule));
rules.add(new MultiLineRule("%IGNORE = ", ";", isr_ignore));
setPredicateRules(rules.toArray(new IPredicateRule[rules.size()]));
}
/**
* @uml.property name="iSRDocumentProvider"
* @uml.associationEnd inverse="iSRPartitionScanner:hterhors.editor.ISRDocumentProvider"
*/
private ISRDocumentProvider isrDocumentProvider = new hterhors.editor.ISRDocumentProvider();
/**
* Getter of the property <tt>iSRDocumentProvider</tt>
* @return Returns the isrDocumentProvider.
* @uml.property name="iSRDocumentProvider"
*/
public ISRDocumentProvider getISRDocumentProvider() {
return isrDocumentProvider;
}
/**
* Setter of the property <tt>iSRDocumentProvider</tt>
* @param iSRDocumentProvider The isrDocumentProvider to set.
* @uml.property name="iSRDocumentProvider"
*/
public void setISRDocumentProvider(ISRDocumentProvider isrDocumentProvider) {
this.isrDocumentProvider = isrDocumentProvider;
}
}
package hterhors.editor.scanners;
import hterhors.editor.ColorManager;
import hterhors.editor.IISRColorConstants;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.rules.IRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.MultiLineRule;
import org.eclipse.jface.text.rules.RuleBasedScanner;
import org.eclipse.jface.text.rules.Token;
public class ISRSyntaxScanner extends RuleBasedScanner {
public ISRSyntaxScanner(ColorManager manager) {
IToken decNonTerminals = new Token(new TextAttribute(
manager.getColor(IISRColorConstants.DEC_NON_TERMINAL)));
IToken start = new Token(new TextAttribute(
manager.getColor(IISRColorConstants.START)));
IToken number = new Token(new TextAttribute(
manager.getColor(IISRColorConstants.NUMBER)));
IToken nonTerminal = new Token(new TextAttribute(
manager.getColor(IISRColorConstants.NON_TERMINAL)));
IToken or = new Token(new TextAttribute(
manager.getColor(IISRColorConstants.OR)));
IToken ignore = new Token(new TextAttribute(
manager.getColor(IISRColorConstants.IGNORE)));
IToken joker = new Token(new TextAttribute(
manager.getColor(IISRColorConstants.JOKER)));
List<IRule> ruleList = new ArrayList<IRule>();
ruleList.add(new MultiLineRule("$$S", ":", start));
ruleList.add(new MultiLineRule("%IGNORE", ";", ignore));
ruleList.add(new MultiLineRule("$$S:", " ", start));
ruleList.add(new MultiLineRule("$", ":", decNonTerminals));
ruleList.add(new MultiLineRule("$$", ":", decNonTerminals));
ruleList.add(new MultiLineRule("!", "*", joker));
ruleList.add(new MultiLineRule("!", "*\n", joker));
ruleList.add(new MultiLineRule("!", "*\t", joker));
ruleList.add(new MultiLineRule("!", "*\r", joker));
ruleList.add(new MultiLineRule("!", "*\b", joker));
ruleList.add(new MultiLineRule("!", "*\f", joker));
ruleList.add(new MultiLineRule("!", "*;", joker));
ruleList.add(new MultiLineRule("$", " ", nonTerminal));
ruleList.add(new MultiLineRule("$", "\n", nonTerminal));
ruleList.add(new MultiLineRule("$", "\t", nonTerminal));
ruleList.add(new MultiLineRule("$", "\r", nonTerminal));
ruleList.add(new MultiLineRule("$", "\b", nonTerminal));
ruleList.add(new MultiLineRule("$", "\f", nonTerminal));
ruleList.add(new MultiLineRule("$", ";", nonTerminal));
ruleList.add(new MultiLineRule("$$", " ", nonTerminal));
ruleList.add(new MultiLineRule("$$", "\n", nonTerminal));
ruleList.add(new MultiLineRule("$$", "\t", nonTerminal));
ruleList.add(new MultiLineRule("$$", "\r", nonTerminal));
ruleList.add(new MultiLineRule("$$", "\b", nonTerminal));
ruleList.add(new MultiLineRule("$$", "\f", nonTerminal));
ruleList.add(new MultiLineRule("$$", ";", nonTerminal));
ruleList.add(new MultiLineRule("|", " ", or));
ruleList.add(new MultiLineRule("|", "\n", or));
ruleList.add(new MultiLineRule("|", "\t", or));
ruleList.add(new MultiLineRule("|", "\r", or));
ruleList.add(new MultiLineRule("|", "\b", or));
ruleList.add(new MultiLineRule("|", "\f", or));
ruleList.add(new MultiLineRule("[", "]", number));
setRules(ruleList.toArray(new IRule[ruleList.size()]));
}
}
package hterhors.editor.sentenceparser;
import hterhors.editor.markers.IsrRuleValidator;
import hterhors.editor.sentenceparser.grammar.Grammar;
import hterhors.editor.sentenceparser.grammar.GrammarParser;
import hterhors.editor.sentenceparser.grammar.GrammarParser.ParseException;
import hterhors.editor.sentenceparser.grammar.Tree;
import hterhors.editor.sentenceparser.grammar.TreeFrame;
import hterhors.editor.sentenceparser.grammar.TreeParser;
import hterhors.editor.zest.SentenceGraphBuilder;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.texteditor.ITextEditor;
/**
* @author Hendrik
*/
public class ParseSentenceHandler extends AbstractHandler {
/**
* @uml.property name="parser"
* @uml.associationEnd
*/
private TreeParser parser = null;
private String result;
private String defaultString = "Example sentence,Another example";
/**
* @uml.property name="tree_frame"
* @uml.associationEnd
*/
private TreeFrame tree_frame;
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
result = "";
ITextEditor textEditor = (ITextEditor) HandlerUtil
.getActiveEditor(event);
IEditorInput input = HandlerUtil.getActiveEditorInput(event);
String grm = textEditor.getDocumentProvider().getDocument(input).get();
Shell shell = HandlerUtil.getActiveShell(event);
InputDialog dialog = new InputDialog(shell, "Sentence validator!",
"Enter the sentences to be tested, seperated with commas!",
defaultString, new IInputValidator() {
@Override
public String isValid(String sentences) {
String x = sentences.replaceAll("\\.*\\?*", "");
if (x.matches("((" + IsrRuleValidator.TERMINAL
+ " ?)+,?)+")) {
return null;
} else {
return "Wrong syntax! Sentences must look like e.g.: \"Hello Tobi\" to validate \"Hello Tobi\".\n Multiple sentences must be seperated with commas e.g.: \"Hello Tobi,Go to the kitchen\"!";
}
}
});
if (dialog.open() == IStatus.OK) {
String value = defaultString = dialog.getValue();
MessageBox pmb = new MessageBox(shell, SWT.ICON_QUESTION | SWT.YES
| SWT.NO);
pmb.setText("Show Syntax-tree");
pmb.setMessage("Do you want to display the syntax-trees-graph of the valid given sentences?\n !!ATTENTION: Each sentence will become his own window.!!");
int val = pmb.open();
boolean show;
switch (val) {
case SWT.YES:
show = true;
break;
default:
show = false;
break;
}
try {
if (doParse(parseToCFG(grm), getSentences(value), show)) {
MessageBox mb = new MessageBox(shell, SWT.ICON_WORKING);
mb.setText("Result");
mb.setMessage(result
+ "\nEvery sentence is valid against the given grammar!");
mb.open();
} else {
MessageBox mb = new MessageBox(shell, SWT.ICON_WARNING);
mb.setText("Result");
mb.setMessage(result
+ "\nOne or more sentences are not valid against the given grammar!");
mb.open();
}
} catch (ParseException e) {
MessageBox mb = new MessageBox(shell, SWT.ICON_ERROR);
mb.setText("Result");
mb.setMessage("An error has accrued, may be the grammar is invalid!");
mb.open();
}
}
return null;
}
private String[] getSentences(String value) {
String[] split = value.split(",");
String[] returnValue = new String[split.length];
for (int i = 0; i < split.length; i++) {
returnValue[i] = split[i];
}
return returnValue;
}
public boolean doParse(String grm, String[] sentences, boolean show)
throws ParseException {
Grammar g = getGrammar(grm);
if (g == null)
throw new GrammarParser.ParseException();
boolean alright = true;
SentenceGraphBuilder.clearInput();
for (int index = 0; index < sentences.length; index++) {
tree_frame = new TreeFrame();
StringTokenizer toks = new StringTokenizer(
removeIgnores(sentences[index].toLowerCase()));
Grammar.Atom[] text = new Grammar.Atom[toks.countTokens()];
boolean text_ok = true;
for (int i = 0; toks.hasMoreTokens(); i++) {
text[i] = g.getAtom(toks.nextToken());
if (text[i] == null)
text_ok = false;
}
Tree t = null;
if (text_ok) {
parser = new TreeParser(g);
t = parser.parse(text);
}
if (t == null) {
result += "FAILED!\n -> \"" + sentences[index] + "\"\n\n";
alright = false;
} else {
result += "OK!\n -> \"" + sentences[index] + "\"\n\n";
if (show) {
// TreeBuilder x = new TreeBuilder();
// x.setTree(t, sentences[index]);
tree_frame.setTree(t, sentences[index]);
tree_frame.setVisible(true);
}
}
}
return alright;
}
private String removeIgnores(String sentence) {
for (String string : ignores) {
sentence = sentence.replaceAll(" " + string + " ", " ");
}
return sentence;
}
private Grammar getGrammar(String grm) {
try {
return GrammarParser.parse(grm);
} catch (GrammarParser.ParseException e) {
e.printStackTrace();
return null;
}
}
private String parseToCFG(String grm) {
getIgnoreTerminals(grm);
StringTokenizer stringTokenizer = new StringTokenizer(grm);
String chomskyGrm = "";
String start = "";
boolean isStart = false;
String token = "";
boolean nextToken = true;
while (stringTokenizer.hasMoreTokens()) {
if (nextToken) {
token = stringTokenizer.nextToken();
}
nextToken = true;
if (token.matches(IsrRuleValidator.PRE_START_SYMBOL + ":?")) {
start += "S -> ";
isStart = true;
} else if (token.matches(IsrRuleValidator.O)) {
if (isStart) {
start += token + " ";
} else {
chomskyGrm += token + " ";
}
} else if (token.matches(IsrRuleValidator.E)) {
if (isStart) {
start += "\n";
} else {
chomskyGrm += "\n";
}
} else if (token.matches("(!\\*)?" + IsrRuleValidator.TERMINAL)) {
if (isStart) {
start += getAttributeName(token).toLowerCase() + " ";
} else {
chomskyGrm += getAttributeName(token).toLowerCase() + " ";
}
} else if (token.matches("(!\\*)?" + IsrRuleValidator.TERMINAL
+ ";")) {
if (isStart) {
start += getAttributeName(token).toLowerCase() + "\n";
} else {
chomskyGrm += getAttributeName(token).toLowerCase() + "\n";
}
} else if (token.matches(IsrRuleValidator.NON_TERMINAL_DECLARATION)) {
isStart = false;
chomskyGrm += getAttributeName(token).toUpperCase() + " -> ";
} else if (token.matches(IsrRuleValidator.NON_TERMINAL + ";")) {
if (isStart) {
start += getAttributeName(token).toUpperCase() + "\n";
} else {
chomskyGrm += getAttributeName(token).toUpperCase() + "\n";
}
} else if (stringTokenizer.hasMoreTokens()
&& token.matches(IsrRuleValidator.NON_TERMINAL)) {
String oldToken = token;
nextToken = false;
if ((token = stringTokenizer.nextToken())
.matches(IsrRuleValidator.C)) {
isStart = false;
chomskyGrm += getAttributeName(oldToken).toUpperCase()
+ " -> ";
} else {
if (isStart) {
start += getAttributeName(oldToken).toUpperCase() + " ";
} else {
chomskyGrm += getAttributeName(oldToken).toUpperCase()
+ " ";
}
}
} else if (token.matches("%IGNORE")) {
isStart = false;
while (stringTokenizer.hasMoreTokens()) {
if (stringTokenizer.nextToken().matches(
IsrRuleValidator.E + "|"
+ IsrRuleValidator.TERMINAL
+ IsrRuleValidator.E)) {
break;
}
}
}
}
return start + chomskyGrm;
}
private List<String> ignores = new ArrayList<String>();
private void getIgnoreTerminals(String grm) {
StringTokenizer stringTokenizer = new StringTokenizer(grm);
String token;
while (stringTokenizer.hasMoreTokens()) {
token = stringTokenizer.nextToken();
if (token.matches("%IGNORE")) {
String nextToken;
while (stringTokenizer.hasMoreTokens()) {
if ((nextToken = stringTokenizer.nextToken())
.matches(IsrRuleValidator.E + "|"
+ IsrRuleValidator.TERMINAL
+ IsrRuleValidator.E)) {
break;
} else if (nextToken.matches(IsrRuleValidator.TERMINAL)) {
ignores.add(nextToken);
}
}
}
}
}
private String getAttributeName(String token) {
return token.replace("$", "").replace("$$", "").replace(":", "")
.replace("!*", "").replace(";", "");
}
}
/* Copyright (c) 2003, Carl Burch. License information is located in the
* edu.csbsju.socs.grammar.Main source code and at
* www.cburch.com/proj/grammar/. */
package hterhors.editor.sentenceparser.grammar;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.HashMap;
import java.util.HashSet;
/**
* @author Hendrik
*/
public class Grammar {
private static int last_alloc = 0;
/**
* @author Hendrik
*/
public static class Element {
/**
* @uml.property name="name"
*/
private String name;
private Element() {
this.name = "temp" + last_alloc;
last_alloc++;
}
private Element(String name) {
this.name = name;
}
/**
* @return
* @uml.property name="name"
*/
public String getName() {
return name;
}
public String toString() {
return name;
}
}
public static class Symbol extends Element {
public Symbol() {
super();
}
public Symbol(String name) {
super(name);
}
}
public static class Atom extends Element {
public Atom() {
super();
}
public Atom(String name) {
super(name);
}
}
/**
* @author Hendrik
*/
public static class Rule {
/**
* @uml.property name="lhs"
* @uml.associationEnd
*/
private Symbol lhs;
/**
* @uml.property name="rhs"
* @uml.associationEnd multiplicity="(0 -1)"
*/
private Element[] rhs;
public Rule(Symbol lhs, Element[] rhs) {
this.lhs = lhs;
this.rhs = rhs;
}
public Symbol getLeftSide() {
return lhs;
}
public Element[] getRightSide() {
return rhs;
}
public String toString() {
StringBuffer ret = new StringBuffer();
ret.append(lhs + " ->");
for (int i = 0; i < rhs.length; i++) {
ret.append(" " + rhs[i]);
}
return ret.toString();
}
}
/**
* @uml.property name="root"
* @uml.associationEnd
*/
private Symbol root = null;
/**
* @uml.property name="symbols"
*/
private HashSet symbols = new HashSet();
/**
* @uml.property name="atoms"
*/
private HashMap atoms = new HashMap();
/**
* @uml.property name="rules"
*/
private HashSet rules = new HashSet();
private HashMap rule_map = new HashMap();
public Grammar() {
}
/**
* @param root
* @uml.property name="root"
*/
public void setRoot(Symbol root) {
this.root = root;
symbols.add(root);
}
public void addRule(Symbol lhs, Element[] rhs) {
add(new Rule(lhs, rhs));
}
public void add(Rule rule) {
Collection c = getRules(rule.lhs);
if (c == null) {
c = new ArrayList();
rule_map.put(rule.lhs, c);
}
c.add(rule);
rules.add(rule);
symbols.add(rule.lhs);
for (int i = 0; i < rule.rhs.length; i++) {
Element e = rule.rhs[i];
if (e instanceof Symbol)
symbols.add(e);
else
atoms.put(e.getName(), e);
}
}
/**
* @return
* @uml.property name="root"
*/
public Symbol getRoot() {
return root;
}
public Collection getRules(Symbol lhs) {
return (Collection) rule_map.get(lhs);
}
/**
* @return
* @uml.property name="rules"
*/
public Collection getRules() {
return rules;
}
/**
* @return
* @uml.property name="symbols"
*/
public Collection getSymbols() {
return symbols;
}
/**
* @return
* @uml.property name="atoms"
*/
public Collection getAtoms() {
return atoms.values();
}
public Collection getLeftSideSymbols() {
return rule_map.keySet();
}
public Atom getAtom(String name) {
return (Atom) atoms.get(name);
}
public void print(java.io.PrintStream out) {
Iterator it = rule_map.keySet().iterator();
while (it.hasNext()) {
Grammar.Symbol sym = (Grammar.Symbol) it.next();
boolean first = true;
Iterator it2 = getRules(sym).iterator();
while (it2.hasNext()) {
Rule rule = (Rule) it2.next();
out.print(first ? (sym + " ->") : " |");
first = false;
Grammar.Element[] rhs = rule.getRightSide();
for (int i = 0; i < rhs.length; i++) {
out.print(" " + rhs[i]);
}
out.println();
}
}
}
}
/* Copyright (c) 2003, Carl Burch. License information is located in the
* edu.csbsju.socs.grammar.Main source code and at
* www.cburch.com/proj/grammar/. */
package hterhors.editor.sentenceparser.grammar;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.StringTokenizer;
public class GrammarParser {
public static class ParseException extends Exception {
public ParseException() {
super();
}
}
public static Grammar parse(String text) throws ParseException {
Grammar ret = new Grammar();
StringTokenizer lines = new StringTokenizer(text, "\n");
Grammar.Symbol cur_sym = null;
Grammar.Symbol eps = new Grammar.Symbol("-");
HashMap map = new HashMap(); // names mapped to Elements
map.put("-", eps);
HashMap usage = new HashMap(); // Elements mapped to line #'s
ArrayList sequence = new ArrayList();
int line_num = 0;
while (lines.hasMoreTokens()) {
++line_num;
String line = lines.nextToken();
int comment = line.indexOf('#');
if (comment >= 0)
line = line.substring(0, comment);
StringTokenizer toks = new StringTokenizer(line);
while (toks.hasMoreTokens()) {
String tok = toks.nextToken();
System.out.println(sequence);
if (tok.equals("->")) {
if (sequence.size() == 0) {
throw new ParseException();
}
Object last_obj = sequence.remove(sequence.size() - 1);
System.out.println(last_obj);
// if (!(last_obj instanceof Grammar.Symbol)) {
// throw new ParseException();
// }
Grammar.Symbol last = (Grammar.Symbol) last_obj;
if (cur_sym == null) {
if (sequence.size() > 0) {
throw new ParseException();
}
ret.setRoot(last);
} else {
addRule(ret, cur_sym, sequence, line_num, eps);
sequence.clear();
}
cur_sym = last;
} else if (tok.equals("|")) {
if (cur_sym == null) {
throw new ParseException();
}
addRule(ret, cur_sym, sequence, line_num, eps);
sequence.clear();
} else {
Grammar.Element e = (Grammar.Element) map.get(tok);
if (e == null) {
// verify token is valid
boolean ok = true;
boolean is_sym = true;
for (int i = 0; ok && i < tok.length(); i++) {
char c = tok.charAt(i);
if (c != '_') {
if (!Character.isDigit(c)
&& !Character.isLetter(c)) {
// ok = false;
}
if (!Character.isUpperCase(c)) {
is_sym = false;
}
}
}
if (!ok) {
throw new ParseException();
}
if (is_sym)
e = new Grammar.Symbol(tok);
else
e = new Grammar.Atom(tok);
map.put(tok, e);
usage.put(e, new Integer(line_num));
}
sequence.add(e);
}
}
}
addRule(ret, cur_sym, sequence, line_num, eps);
// confirm that all symbols used are defined
if (!ret.getLeftSideSymbols().containsAll(ret.getSymbols())) {
Iterator it = ret.getSymbols().iterator();
while (it.hasNext()) {
Grammar.Symbol sym = (Grammar.Symbol) it.next();
if (ret.getRules(sym) == null) {
Integer line = (Integer) usage.get(sym);
throw new ParseException();
}
}
}
if (ret.getRoot() == null) {
throw new ParseException();
}
Collection root_rules = ret.getRules(ret.getRoot());
if (root_rules == null || root_rules.size() == 0) {
throw new ParseException();
}
return ret;
}
private static void addRule(Grammar ret, Grammar.Symbol lhs, ArrayList rhs,
int line_num, Grammar.Symbol eps) throws ParseException {
if (rhs.contains(eps)) {
// if epsilon is in sequence, make sure it is
// alone, and remove it.
if (rhs.size() > 1) {
throw new ParseException();
}
ret.addRule(lhs, new Grammar.Element[] {});
} else {
ret.addRule(lhs, toElements(rhs));
}
}
private static Grammar.Element[] toElements(ArrayList l) {
Grammar.Element[] ret = new Grammar.Element[l.size()];
int i = 0;
Iterator it = l.iterator();
while (it.hasNext()) {
ret[i] = (Grammar.Element) it.next();
i++;
}
return ret;
}
}
package hterhors.editor.sentenceparser.grammar;
/* Copyright (c) 2003, Carl Burch. License information is located in the
* edu.csbsju.socs.grammar.Main source code and at
* www.cburch.com/proj/grammar/. */
/**
* @author Hendrik
*/
public class Tree {
/**
* @uml.property name="null_children"
* @uml.associationEnd multiplicity="(0 -1)"
*/
private static final Tree[] null_children = {};
/**
* @uml.property name="data"
*/
private Object data;
/**
* @uml.property name="children"
* @uml.associationEnd multiplicity="(0 -1)"
*/
private Tree[] children;
private boolean is_leaf = false;
protected Tree(Object data) {
this(data, null_children);
this.is_leaf = true;
}
protected Tree(Object data, Tree[] children) {
this.data = data;
this.children = children;
}
/**
* @return
* @uml.property name="data"
*/
public Object getData() {
return data;
}
/**
* @param data
* @uml.property name="data"
*/
public void setData(Object data) {
this.data = data;
}
public Tree getChild(int which) {
return children[which];
}
/**
* @return
* @uml.property name="children"
*/
public Tree[] getChildren() {
return children;
}
public boolean isLeaf() {
return is_leaf;
}
public static Tree createNode(Object data, Tree[] children) {
return new Tree(data, children);
}
public static Tree createLeaf(Object data) {
return new Tree(data);
}
}
package hterhors.editor.sentenceparser.grammar;
/* Copyright (c) 2003, Carl Burch. License information is located in the
* edu.csbsju.socs.grammar.Main source code and at
* www.cburch.com/proj/grammar/. */
import hterhors.editor.zest.SentenceGraphBuilder;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.font.LineMetrics;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JPanel;
/**
* @author Hendrik
*/
public class TreeBuilder {
/**
* @uml.property name="tree"
* @uml.associationEnd
*/
private Tree tree = null;
private ArrayList tree_data = new ArrayList();
public TreeBuilder() {
}
public void setTree(Tree value, String sentence) {
if (value == null)
value = new Tree("\"" + sentence + "\" is not in grammar!");
this.tree = value;
this.tree_data = null;
tree_data = new ArrayList();
autoArrangeSub(value);
SentenceGraphBuilder.addInput(tree_data, null);
}
private TreeData autoArrangeSub(Tree t) {
Tree[] children = t.getChildren();
TreeData child = null;
if (children.length == 0) {
} else {
TreeData last = null;
for (int i = 0; i < children.length; i++) {
TreeData data = autoArrangeSub(children[i]);
if (last == null)
child = data;
else
last.sibling = data;
last = data;
}
}
TreeData ret = new TreeData(t);
ret.child = child;
for (TreeData n = child; n != null; n = n.sibling) {
n.parent = ret;
}
tree_data.add(ret);
return ret;
}
}
package hterhors.editor.sentenceparser.grammar;
/**
* @author Hendrik
*/
public class TreeData {
/**
* @uml.property name="data"
* @uml.associationEnd
*/
public Tree data;
/**
* @uml.property name="parent"
* @uml.associationEnd
*/
public TreeData parent = null;
/**
* @uml.property name="child"
* @uml.associationEnd
*/
public TreeData child = null;
/**
* @uml.property name="sibling"
* @uml.associationEnd
*/
public TreeData sibling = null;
TreeData(Tree data) {
this.data = data;
}
}
\ No newline at end of file
package hterhors.editor.sentenceparser.grammar;
/* Copyright (c) 2003, Carl Burch. License information is located in the
* edu.csbsju.socs.grammar.Main source code and at
* www.cburch.com/proj/grammar/. */
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
/**
* @author Hendrik
*/
public class TreeFrame extends JFrame {
private class FileMenu extends JMenu implements ActionListener {
private JMenuItem close = new JMenuItem();
private FileMenu() {
add(close);
close.addActionListener(this);
}
private void renewStrings() {
this.setText("Menu");
close.setText("Close");
}
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if (src == close)
TreeFrame.this.dispose();
}
}
private class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent e) {
TreeFrame.this.dispose();
}
}
/**
* @uml.property name="file_menu"
* @uml.associationEnd
*/
private FileMenu file_menu = new FileMenu();
/**
* @uml.property name="panel"
* @uml.associationEnd
*/
private TreePanel panel = new TreePanel();
private JFileChooser chooser = new JFileChooser(".");
private JScrollPane panel_scroll;
public TreeFrame() {
addWindowListener(new WindowCloser());
JMenuBar menubar = new JMenuBar();
menubar.add(file_menu);
setJMenuBar(menubar);
panel_scroll = new JScrollPane(panel);
getContentPane().add(panel_scroll, BorderLayout.CENTER);
}
public void renewStrings(String title) {
this.setTitle(title);
file_menu.renewStrings();
pack();
JFileChooser newch = new JFileChooser();
java.io.File f = chooser.getCurrentDirectory();
if (f != null)
newch.setCurrentDirectory(f);
f = chooser.getSelectedFile();
if (f != null)
newch.setSelectedFile(f);
chooser = newch;
}
public void setTree(Tree t, String sentence) {
panel.setTree(t, sentence);
renewStrings(sentence);
}
}
package hterhors.editor.sentenceparser.grammar;
/* Copyright (c) 2003, Carl Burch. License information is located in the
* edu.csbsju.socs.grammar.Main source code and at
* www.cburch.com/proj/grammar/. */
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.font.LineMetrics;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JPanel;
/**
* @author Hendrik
*/
public class TreePanel extends JPanel {
private static final int VERT_GAP = 35;
private static final int HORZ_GAP = 5;
private static final int LABEL_BORDER = 2;
private static final int BORDER = 20;
private static final int NO_CHILD_X = 3;
/**
* @author Hendrik
*/
private static class TreeData {
int x; // x-coordinate of label's center
int y;
int label_width;
int label_height;
int label_top_offs;
int label_base_offs;
/**
* @uml.property name="data"
* @uml.associationEnd
*/
Tree data;
/**
* @uml.property name="parent"
* @uml.associationEnd
*/
TreeData parent = null;
/**
* @uml.property name="child"
* @uml.associationEnd
*/
TreeData child = null;
/**
* @uml.property name="sibling"
* @uml.associationEnd
*/
TreeData sibling = null;
int width; // these are for during the auto-arrange only
int height;
TreeData(Tree data, int x, int y, int label_width, int label_height,
int label_top_offs, int label_base_offs, int width, int height) {
this.data = data;
this.x = x;
this.y = y;
this.label_width = label_width;
this.label_height = label_height;
this.label_top_offs = label_top_offs;
this.label_base_offs = label_base_offs;
this.width = width;
this.height = height;
}
}
/**
* @uml.property name="tree"
* @uml.associationEnd
*/
private Tree tree = null;
private Font font = new Font("Dialog", Font.PLAIN, 12);
private ArrayList tree_data = new ArrayList();
public TreePanel() {
this(null,null);
}
public TreePanel(Tree tree, String sentence) {
setBackground(Color.white);
if (tree != null)
setTree(tree,sentence);
setPreferredSize(new Dimension(200, 200));
}
public void setTree(Tree value, String sentence) {
if (value == null)
value = new Tree("\"" + sentence + "\" is not in grammar!");
this.tree = value;
this.tree_data = null;
autoArrange();
}
public Image getImage() {
Graphics g = getGraphics();
if (tree == null || g == null)
return null;
if (tree_data == null)
autoArrange(g);
if (tree_data == null)
return null;
Dimension dims = getPreferredSize();
int width = (int) Math.ceil(dims.getWidth());
int height = (int) Math.ceil(dims.getHeight());
Image ret = createImage(width, height);
g = ret.getGraphics();
if (g == null)
return null;
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
doPaint(g, BORDER, BORDER);
return ret;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (tree == null)
return;
if (tree_data == null)
autoArrange(g);
if (tree_data == null)
return;
Dimension size = getSize();
Dimension pref = getPreferredSize();
int x_offs = BORDER + Math.max(0, (size.width - pref.width) / 2);
int y_offs = BORDER + Math.max(0, (size.height - pref.height) / 2);
doPaint(g, x_offs, y_offs);
}
private void doPaint(Graphics g, int x_offs, int y_offs) {
// draw connecting lines
g.setColor(Color.gray);
Iterator it = tree_data.iterator();
while (it.hasNext()) {
TreeData nd = (TreeData) it.next();
// draw line to parent
TreeData pd = nd.parent;
if (pd != null) {
g.drawLine(x_offs + nd.x, y_offs + nd.y, x_offs + pd.x, y_offs
+ pd.y);
}
// if appropriate, draw X marking non-leaf with no children
Tree[] nch = nd.data.getChildren();
if (nch == null || nch.length == 0) {
if (!nd.data.isLeaf()) {
int endx = x_offs + nd.x;
int endy = y_offs + nd.y + VERT_GAP / 2;
g.drawLine(x_offs + nd.x, y_offs + nd.y, endx, endy);
g.drawLine(endx - NO_CHILD_X, endy - NO_CHILD_X, endx
+ NO_CHILD_X, endy + NO_CHILD_X);
g.drawLine(endx - NO_CHILD_X, endy + NO_CHILD_X, endx
+ NO_CHILD_X, endy - NO_CHILD_X);
}
}
}
// draw labels
g.setFont(font);
it = tree_data.iterator();
while (it.hasNext()) {
TreeData nd = (TreeData) it.next();
int x = x_offs + nd.x - nd.label_width / 2;
int y = y_offs + nd.y + nd.label_top_offs;
g.setColor(Color.white);
g.fillRect(x - LABEL_BORDER, y - LABEL_BORDER, nd.label_width + 2
* LABEL_BORDER, nd.label_height + 2 * LABEL_BORDER);
y = y_offs + nd.y + nd.label_base_offs;
g.setColor(Color.white);
g.setColor(Color.blue);
g.drawString(nd.data.getData().toString(), x, y);
}
}
public void autoArrange() {
autoArrange(getGraphics());
repaint();
}
private void autoArrange(Graphics g) {
if (g == null)
return;
FontMetrics fm = g.getFontMetrics();
if (fm == null)
return;
tree_data = new ArrayList();
g.setFont(font);
TreeData data = autoArrangeSub(tree, 0, 0, fm, g);
setPreferredSize(new Dimension(data.width + 2 * BORDER, data.height + 2
* BORDER));
invalidate();
}
private TreeData autoArrangeSub(Tree t, int x, int y, FontMetrics fm,
Graphics g) {
Tree[] children = t.getChildren();
LineMetrics lm = fm.getLineMetrics(t.getData().toString(), g);
int label_width = fm.stringWidth(t.getData().toString());
int label_height = (int) Math.ceil(lm.getAscent() + lm.getDescent());
int label_x = x + label_width / 2;
int label_y = y + fm.getAscent() / 2;
int label_base_offs = (y + fm.getAscent()) - label_y;
int label_top_offs = (int) Math.round((label_y + label_base_offs - lm
.getAscent()) - label_y);
int width = label_width;
int height = label_height;
TreeData child = null;
if (children.length == 0) {
if (!t.isLeaf()) {
height = Math.max(height, VERT_GAP / 2 + NO_CHILD_X);
width = Math.max(width, 2 * NO_CHILD_X);
}
} else {
int xpos = x;
y += VERT_GAP;
int max_height = 0;
TreeData last = null;
for (int i = 0; i < children.length; i++) {
TreeData data = autoArrangeSub(children[i], xpos, y, fm, g);
if (last == null)
child = data;
else
last.sibling = data;
last = data;
xpos += data.width + HORZ_GAP;
max_height = Math.max(max_height, data.height);
}
xpos -= HORZ_GAP;
int center = (child.x + last.x) / 2;
if (center >= label_x) {
label_x = center;
} else { // label is wider than children
int offs = label_x - center;
for (TreeData n = child; n != null; n = n.sibling) {
translateTree(n, offs, 0);
}
xpos += offs;
}
width = Math.max(label_x + label_width / 2 - x, xpos - x);
height = Math.max(label_height, VERT_GAP + max_height);
}
TreeData ret = new TreeData(t, label_x, label_y, label_width,
label_height, label_top_offs, label_base_offs, width, height);
ret.child = child;
for (TreeData n = child; n != null; n = n.sibling) {
n.parent = ret;
}
tree_data.add(ret);
return ret;
}
private void translateTree(TreeData t, int x_offs, int y_offs) {
t.x += x_offs;
t.y += y_offs;
for (TreeData n = t.child; n != null; n = n.sibling) {
translateTree(n, x_offs, y_offs);
}
}
}
package hterhors.editor.sentenceparser.grammar;
/* Copyright (c) 2003, Carl Burch. License information is located in the
* edu.csbsju.socs.grammar.Main source code and at
* www.cburch.com/proj/grammar/. */
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* @author Hendrik
*/
public class TreeParser {
private static class FinalTree extends Tree {
public FinalTree(Object data) { super(data); }
public FinalTree(Object data, Tree[] children) {
super(data, children);
}
}
/**
* @author Hendrik
*/
private static class SymbolTree {
/**
* @uml.property name="sym"
* @uml.associationEnd
*/
Grammar.Element sym;
/**
* @uml.property name="tree"
* @uml.associationEnd
*/
Tree tree;
SymbolTree(Grammar.Element sym, Tree tree) {
this.sym = sym;
this.tree = tree;
}
public int hashCode() { return sym.hashCode(); }
public boolean equals(Object other) {
return sym.equals(((SymbolTree) other).sym);
}
public String toString() { return sym.toString(); }
}
/**
* @author Hendrik
*/
private static class Single {
/**
* @uml.property name="lhs"
* @uml.associationEnd
*/
Grammar.Symbol lhs;
/**
* @uml.property name="a"
* @uml.associationEnd
*/
Grammar.Atom a;
Single(Grammar.Symbol lhs, Grammar.Atom a) {
this.lhs = lhs;
this.a = a;
}
}
/**
* @author Hendrik
*/
private static class Dual {
/**
* @uml.property name="lhs"
* @uml.associationEnd
*/
SymbolTree lhs;
/**
* @uml.property name="a"
* @uml.associationEnd
*/
Grammar.Symbol a;
/**
* @uml.property name="b"
* @uml.associationEnd
*/
Grammar.Symbol b;
Dual(SymbolTree lhs, Grammar.Symbol a, Grammar.Symbol b) {
this.lhs = lhs;
this.a = a;
this.b = b;
}
}
/**
* @uml.property name="base"
* @uml.associationEnd
*/
private Grammar base;
private HashMap singles = new HashMap();
private HashMap duals = new HashMap();
private HashMap final_orig_rules = new HashMap();
/**
* @uml.property name="null_parse"
* @uml.associationEnd
*/
private Tree null_parse = null;
public TreeParser(Grammar g) {
base = g;
HashMap orig_rules = new HashMap();
g = computeNoEpsilons(g, orig_rules);
g = computeNoUselessSymbols(g, orig_rules);
g = computeNoUnit(g, orig_rules);
this.final_orig_rules = orig_rules;
computeChomsky(g);
}
private void printRuleMap(HashMap orig_rules) {
Iterator it = orig_rules.entrySet().iterator();
while(it.hasNext()) {
Map.Entry e = (Map.Entry) it.next();
Tree t = (Tree) e.getValue();
System.err.print(e.getKey() + " : " + t.getData()
+ " /");
Tree[] children = t.getChildren();
for(int i = 0; i < children.length; i++) {
System.err.print(" " + children[i].getData());
}
System.err.println();
}
}
private Grammar computeNoUselessSymbols(Grammar g,
HashMap orig_rules) {
// determine which symbols terminate in some string
// (cf. page 89, Hopcroft and Ullman)
HashSet terminating = new HashSet();
boolean changed = true;
while(changed) {
changed = false;
Iterator it = g.getRules().iterator();
while(it.hasNext()) {
Grammar.Rule rule = (Grammar.Rule) it.next();
Grammar.Symbol lhs = rule.getLeftSide();
if(terminating.contains(lhs)) continue;
if(allSymsInSet(rule.getRightSide(), terminating)) {
terminating.add(lhs);
changed = true;
}
}
}
// compute grammar (rules in base with all symbols terminating)
Grammar ret = new Grammar();
ret.setRoot(g.getRoot());
Iterator it = g.getRules().iterator();
while(it.hasNext()) {
Grammar.Rule rule = (Grammar.Rule) it.next();
if(allSymsInSet(rule.getRightSide(), terminating)) {
ret.add(rule);
} else {
orig_rules.remove(rule);
}
}
return ret;
}
private boolean allSymsInSet(Grammar.Element[] rhs, Set query) {
for(int i = 0; i < rhs.length; i++) {
if(rhs[i] instanceof Grammar.Symbol
&& !query.contains(rhs[i])) {
return false;
}
}
return true;
}
// orig rules should be empty HashMap
private Grammar computeNoEpsilons(Grammar g, HashMap orig_rules) {
// determine nullability
HashMap nullable = new HashMap();
boolean changed = true;
while(changed) {
changed = false;
Iterator it = g.getRules().iterator();
while(it.hasNext()) {
Grammar.Rule rule = (Grammar.Rule) it.next();
Grammar.Symbol lhs = rule.getLeftSide();
if(nullable.get(lhs) != null) continue;
Grammar.Element[] rhs = rule.getRightSide();
boolean can_null = true;
for(int i = 0; can_null && i < rhs.length; i++) {
can_null = rhs[i] instanceof Grammar.Symbol
&& nullable.get(rhs[i]) != null;
}
if(can_null) {
Tree tree = treeFor(rule, nullable);
nullable.put(lhs,
new FinalTree(tree.getData(), tree.getChildren()));
changed = true;
}
}
}
// determine tree for null sentence
null_parse = (Tree) nullable.get(g.getRoot());
// compute return grammar
Grammar ret = new Grammar();
ret.setRoot(g.getRoot());
Iterator it = g.getRules().iterator();
while(it.hasNext()) {
Grammar.Rule rule = (Grammar.Rule) it.next();
Grammar.Symbol lhs = rule.getLeftSide();
Grammar.Element[] rhs = rule.getRightSide();
Tree[] known = new Tree[rhs.length];
addNonNullRules(new AddNonNullData(ret, lhs, rhs, known,
nullable, orig_rules), 0);
}
return ret;
}
private Tree treeFor(Grammar.Rule rule, HashMap tree_map) {
Grammar.Element[] rhs = rule.getRightSide();
Tree[] children = new Tree[rhs.length];
for(int i = 0; i < children.length; i++) {
if(rhs[i] instanceof Grammar.Symbol && tree_map != null) {
children[i] = (Tree) tree_map.get(rhs[i]);
} else {
children[i] = Tree.createLeaf(rhs[i]);
}
}
return Tree.createNode(rule.getLeftSide(), children);
}
/*
private Tree treeFor(Grammar.Symbol sym, HashMap rule_map) {
Grammar.Rule rule = (Grammar.Rule) rule_map.get(sym);
Grammar.Element[] rhs = rule.getRightSide();
Tree[] child = new Tree[rhs.length];
for(int i = 0; i < child.length; i++) {
if(rhs[i] instanceof Grammar.Symbol) {
child[i] = treeFor((Grammar.Symbol) rhs[i], rule_map);
} else {
child[i] = Tree.createLeaf(child[i]);
}
}
return Tree.createNode(sym, child);
}
*/
/**
* @author Hendrik
*/
private static class AddNonNullData {
/**
* @uml.property name="dest"
* @uml.associationEnd
*/
Grammar dest;
/**
* @uml.property name="lhs"
* @uml.associationEnd
*/
Grammar.Symbol lhs;
/**
* @uml.property name="rhs"
* @uml.associationEnd multiplicity="(0 -1)"
*/
Grammar.Element[] rhs;
int pos;
/**
* @uml.property name="known"
* @uml.associationEnd multiplicity="(0 -1)"
*/
Tree[] known;
HashMap nullable;
HashMap orig_rules;
AddNonNullData(Grammar dest, Grammar.Symbol lhs,
Grammar.Element[] rhs, Tree[] known,
HashMap nullable, HashMap orig_rules) {
this.dest = dest;
this.lhs = lhs;
this.rhs = rhs;
this.pos = pos;
this.known = known;
this.nullable = nullable;
this.orig_rules = orig_rules;
}
}
private void addNonNullRules(AddNonNullData data, int pos) {
if(pos == data.rhs.length) {
Tree[] children = new Tree[data.rhs.length];
int len = 0;
for(int i = 0; i < data.rhs.length; i++) {
if(data.known[i] == null) {
children[i] = Tree.createLeaf(data.rhs[i]);
++len;
} else {
children[i] = data.known[i];
}
}
if(len != 0) {
Grammar.Element[] new_rhs = new Grammar.Element[len];
int j = 0;
for(int i = 0; i < data.rhs.length; i++) {
if(data.known[i] == null) new_rhs[j++] = data.rhs[i];
}
Grammar.Rule r = new Grammar.Rule(data.lhs, new_rhs);
data.dest.add(r);
data.orig_rules.put(r, Tree.createNode(data.lhs, children));
}
} else {
Tree null_tree = (Tree) data.nullable.get(data.rhs[pos]);
if(null_tree != null) {
data.known[pos] = null_tree;
addNonNullRules(data, pos + 1);
data.known[pos] = null;
addNonNullRules(data, pos + 1);
} else {
int j = pos + 1;
while(j < data.rhs.length
&& data.nullable.get(data.rhs[j]) == null) {
++j;
}
addNonNullRules(data, j);
}
}
}
// g must have no useless symbols or epsilon-productions
private Grammar computeNoUnit(Grammar g, HashMap rule_orig) {
// divide rules into unit productions and others
HashMap goesto = new HashMap();
Grammar ret = new Grammar();
ret.setRoot(g.getRoot());
Iterator it = g.getRules().iterator();
while(it.hasNext()) {
Grammar.Rule rule = (Grammar.Rule) it.next();
Grammar.Element[] rhs = rule.getRightSide();
if(rhs.length == 1 && rhs[0] instanceof Grammar.Symbol) {
Grammar.Symbol lhs = rule.getLeftSide();
Grammar.Symbol a = (Grammar.Symbol) rhs[0];
HashSet dest = (HashSet) goesto.get(lhs);
if(dest == null) {
dest = new HashSet();
goesto.put(lhs, dest);
}
Tree t = (Tree) rule_orig.get(rule);
if(t == null) t = treeFor(rule, null);
else rule_orig.remove(rule);
dest.add(new SymbolTree(a, t));
} else {
ret.add(rule);
}
}
// compute closure of unit productions
boolean changed = true;
while(changed) {
changed = false;
it = goesto.entrySet().iterator();
while(it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Grammar.Symbol a = (Grammar.Symbol) entry.getKey();
Collection bs = (Collection) entry.getValue();
Collection bs_toadd = new java.util.LinkedList();
Iterator it2 = bs.iterator();
while(it2.hasNext()) {
SymbolTree b = (SymbolTree) it2.next();
Collection cs = (Collection) goesto.get(b.sym);
if(cs != null) {
Iterator it3 = cs.iterator();
while(it3.hasNext()) {
SymbolTree c = (SymbolTree) it3.next();
if(!bs.contains(c)) {
bs_toadd.add(new SymbolTree(c.sym,
treeJoin(b.tree, c.tree)));
changed = true;
}
}
}
}
bs.addAll(bs_toadd);
}
}
// add non-unit productions within closure
it = goesto.entrySet().iterator();
while(it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Grammar.Symbol a = (Grammar.Symbol) entry.getKey();
Collection bs = (Collection) entry.getValue();
Iterator it2 = bs.iterator();
while(it2.hasNext()) {
SymbolTree b = (SymbolTree) it2.next();
Iterator it3 = g.getRules((Grammar.Symbol) b.sym).iterator();
while(it3.hasNext()) {
Grammar.Rule rule = (Grammar.Rule) it3.next();
Grammar.Element[] rhs = rule.getRightSide();
if(rhs.length != 1
|| !(rhs[0] instanceof Grammar.Symbol)) {
Grammar.Rule r = new Grammar.Rule(a, rhs);
ret.add(r);
Tree t = (Tree) rule_orig.get(rule);
if(t == null) t = treeFor(rule, null);
rule_orig.put(r, treeJoin(b.tree, t));
}
}
}
}
return ret;
}
private Tree treeJoin(Tree a, Tree b) {
if(a == null) return null;
if(a.getData() == b.getData()) return b;
Tree[] children = a.getChildren();
for(int i = 0; i < children.length; i++) {
Tree lower = treeJoin(children[i], b);
if(lower != null) {
Tree[] new_children = new Tree[children.length];
System.arraycopy(children, 0, new_children, 0,
children.length);
new_children[i] = lower;
return Tree.createNode(a.getData(), new_children);
}
}
return null;
}
// g must have no useless symbols, epsilon-productions, or unit
// productions
private void computeChomsky(Grammar g) {
HashMap smap = new HashMap();
/*
Collection atoms = g.getAtoms();
Iterator it = atoms.iterator();
while(it.hasNext()) {
Grammar.Atom a = (Grammar.Atom) it.next();
Grammar.Symbol s = new Grammar.Symbol();
Tree t = Tree.createNode(s, new Tree[] { new Tree(a) });
HashMap syms_mapping = new HashMap();
syms_mapping.put(s, t);
singles.put(a, syms_mapping);
smap.put(a, s);
}
*/
Iterator it = g.getRules().iterator();
while(it.hasNext()) {
Grammar.Rule rule = (Grammar.Rule) it.next();
Grammar.Symbol lhs = rule.getLeftSide();
Grammar.Element[] rhs = rule.getRightSide();
if(rhs.length == 1) {
Tree map = (Tree) final_orig_rules.get(rule);
if(map == null) map = treeFor(rule, null);
Tree t = Tree.createNode(new SymbolTree(lhs, map), new Tree[] {
Tree.createLeaf(new SymbolTree(rhs[0], null))
});
getSingles(rhs[0]).put(lhs, t);
} else {
Tree t = (Tree) final_orig_rules.get(rule);
for(int i = rhs.length - 1; i >= 2; i--) {
Grammar.Symbol a = new Grammar.Symbol();
addDual(lhs, a, symFor(rhs[i], smap), t);
t = null;
lhs = a;
}
Grammar.Symbol a = symFor(rhs[0], smap);
addDual(lhs, a, symFor(rhs[1], smap), t);
}
}
}
private HashMap getSingles(Grammar.Element e) {
HashMap ret = (HashMap) singles.get(e);
if(ret == null) {
ret = new HashMap();
singles.put(e, ret);
}
return ret;
}
private Grammar.Symbol symFor(Grammar.Element e, HashMap smap) {
if(e instanceof Grammar.Symbol) return (Grammar.Symbol) e;
Grammar.Symbol ret = (Grammar.Symbol) smap.get(e);
if(ret == null) {
ret = new Grammar.Symbol();
getSingles(e).put(ret,
Tree.createNode(new SymbolTree(ret, Tree.createLeaf(e)), new Tree[] {
Tree.createLeaf(new SymbolTree(e, null))
}));
smap.put(e, ret);
}
return ret;
}
private void addDual(Grammar.Symbol lhs, Grammar.Symbol a,
Grammar.Symbol b, Tree t) {
HashSet a_map = (HashSet) duals.get(a);
if(a_map == null) {
a_map = new HashSet();
duals.put(a, a_map);
}
a_map.add(new Dual(new SymbolTree(lhs, t), a, b));
}
private void printChomsky() {
Iterator it = singles.entrySet().iterator();
while(it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Iterator it2 = ((HashMap) entry.getValue()).keySet().iterator();
while(it2.hasNext()) {
System.err.println(it2.next() + " -> "
+ entry.getKey());
}
}
it = duals.values().iterator();
while(it.hasNext()) {
HashSet entry = (HashSet) it.next();
Iterator it2 = entry.iterator();
while(it2.hasNext()) {
Dual d = (Dual) it2.next();
System.err.println(d.lhs + " -> " + d.a + " " + d.b);
}
}
}
public Tree parse(Grammar.Atom[] x) {
if(x == null || x.length == 0) return null_parse;
// CYK algorithm: V[i][j] represents all symbols that can
// derive x[i..i+j] (inclusive of x[i+j]).
int n = x.length;
Map[][] V = new Map[n][n];
for(int i = 0; i < x.length; i++) {
V[i][0] = (Map) singles.get(x[i]);
}
for(int j = 1; j < n; j++) {
for(int i = 0; i + j < n; i++) {
V[i][j] = new HashMap();
for(int k = 0; k < j; k++) { // [i..i+k] and [i+k+1..i+j]
if(V[i][k] == null) continue;
Collection entries = V[i][k].entrySet();
Iterator it = entries.iterator();
while(it.hasNext()) {
Map.Entry ae = (Map.Entry) it.next();
Grammar.Symbol a = (Grammar.Symbol) ae.getKey();
HashSet a_duals = (HashSet) duals.get(a);
if(a_duals == null) continue;
Iterator it2 = a_duals.iterator();
while(it2.hasNext()) {
Dual dual = (Dual) it2.next();
Object bt = V[i + k + 1][j - k - 1].get(dual.b);
if(bt != null) {
Tree all_t = Tree.createNode(dual.lhs,
new Tree[] { (Tree) ae.getValue(),
(Tree) bt });
V[i][j].put(dual.lhs.sym, all_t);
}
}
}
}
}
}
// now translate tree back base grammar
Tree chtree = (Tree) V[0][n - 1].get(base.getRoot());
if(chtree == null) return null;
ArrayList subtrees = translateTree(chtree);
if(subtrees.size() != 1) {
throw new RuntimeException("unexpected bonus subtrees");
}
return (Tree) subtrees.get(0);
}
private void printTree(Tree chtree, int depth) {
SymbolTree root = (SymbolTree) chtree.getData();
Tree[] children = chtree.getChildren();
for(int i = 0; i < depth; i++) System.err.print(" ");
System.err.print(root.sym);
if(root.tree != null) {
System.err.print(" (" + root.tree.getData()
+ " " + root.tree.getChildren().length + ")");
}
System.err.println();
for(int i = 0; i < children.length; i++) {
printTree(children[i], depth + 1);
}
}
private ArrayList translateTree(Tree n) {
SymbolTree root = (SymbolTree) n.getData();
Tree[] children = n.getChildren();
ArrayList ret = new ArrayList();
if(root.tree instanceof FinalTree) {
ret.add(root.tree);
} else {
for(int i = 0; i < children.length; i++) {
ret.addAll(translateTree(children[i]));
}
if(root.tree != null) {
Tree t = joinTree(root.tree, ret);
ret.add(0, t);
}
}
return ret;
}
private Tree joinTree(Tree base, ArrayList leaves) {
Tree[] children = base.getChildren();
if(base instanceof FinalTree) {
return base;
} else if(children.length == 0) {
if(leaves.size() > 0) {
Tree leaf = (Tree) leaves.get(0);
if(base.getData() == leaf.getData()) {
leaves.remove(0); // consume leaf
return leaf;
}
}
return base;
} else {
Tree[] newch = new Tree[children.length];
for(int i = 0; i < children.length; i++) {
newch[i] = joinTree(children[i], leaves);
}
return Tree.createNode(base.getData(), newch);
}
}
}
package hterhors.editor.xml;
/**
* @author Hendrik
*/
public class RootElement {
/**
* @uml.property name="rootElement"
* @uml.associationEnd
*/
private XMLElement rootElement;
/**
* @return
* @uml.property name="rootElement"
*/
public XMLElement getRootElement() {
return rootElement;
}
/**
* @param rootElement
* @uml.property name="rootElement"
*/
public void setRootElement(XMLElement rootElement) {
this.rootElement = rootElement;
}
}
\ No newline at end of file
package hterhors.editor.xml;
/**
* @author Hendrik
*/
public class XMLAttribute {
/**
* @uml.property name="name"
*/
private String name;
/**
* @uml.property name="value"
*/
private String value;
public XMLAttribute(String name) {
super();
this.name = name;
}
public XMLAttribute(String name, String value) {
super();
this.name = name;
this.value = value;
}
/**
* @return
* @uml.property name="name"
*/
public String getName() {
return name;
}
/**
* @return
* @uml.property name="value"
*/
public String getValue() {
return value;
}
}
\ No newline at end of file